WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-scripts.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * [DRAFT]
6  *
7  * THE FOLLOWING CODE IS A DRAFT. FEEL FREE TO USE IT TO MAKE SOME EXPERIMENTS, BUT DO NOT USE IT IN ANY CASE IN
8  * PRODUCTION ENVIRONMENT. ALL CLASSES AND RELATIVE METHODS BELOW CAN CHNAGE IN THE FUTURE RELEASES.
9  *
10  */
11 
23 class WPDKScripts {
24 
25  // @todo List of standard WordPress scripts handles
26  const JQUERY = 'jquery';
27 
28  protected $scripts = array();
29 
37  public function __construct()
38  {
39  //global $wp_scripts;
40  }
41 
53  public function hasScripts( $handle, $comp = '&' )
54  {
55  global $wp_scripts;
56 
57  if ( isset( $wp_scripts->registered ) && !empty( $handle ) ) {
58 
59  // Sanitize input
60  if ( is_string( $handle ) ) {
61  $handles = explode( ',', $handle );
62  }
63  elseif ( is_array( $handle ) ) {
64  $handles = $handle;
65  }
66  else {
67  return false;
68  }
69 
70  // AND
71  if ( '&' == $comp || 'AND' == strtoupper( $comp ) ) {
72  foreach ( $handles as $handle ) {
73  if ( !isset( $wp_scripts->registered[$handle] ) ) {
74  return false;
75  }
76  }
77  return true;
78  }
79 
80  // OR
81  if ( '|' == $comp || 'OR' == strtoupper( $comp ) ) {
82  foreach ( $handles as $handle ) {
83  if ( isset( $wp_scripts->registered[$handle] ) ) {
84  return true;
85  }
86  }
87  return false;
88  }
89  }
90  return false;
91  }
92 
143  public function registerScripts( $scripts, $path = '', $deps = array(), $version = false, $in_footer = true )
144  {
145  if ( !empty( $scripts ) && is_array( $scripts ) ) {
146  foreach ( $scripts as $filename => $info ) {
147 
148  // Case 1
149  if ( is_array( $info ) ) {
150  $handle = isset( $info['handle'] ) ? $info['handle'] : sanitize_title( WPDKFilesystem::filename( $filename ) );
151  $_path = isset( $info['path'] ) ? $info['path'] : $path;
152  $_deps = isset( $info['deps'] ) ? $info['deps'] : $deps;
153 
154  // Sanitize $deps
155  if ( is_string( $_deps ) ) {
156  $_deps = explode( ',', $_deps );
157  }
158 
159  $_version = isset( $info['version'] ) ? $info['version'] : $version;
160  $_in_footer = isset( $info['footer'] ) ? $info['footer'] : $in_footer;
161  $src = sprintf( '%s%s', trailingslashit( $_path ), $filename );
162  }
163  elseif ( is_string( $info ) ) {
164  $handle = sanitize_title( WPDKFilesystem::filename( $info ) );
165  $_path = $path;
166  $_deps = $deps;
167 
168  // Sanitize $deps
169  if ( is_string( $_deps ) ) {
170  $_deps = explode( ',', $_deps );
171  }
172 
173  $_version = $version;
174  $_in_footer = $in_footer;
175  $src = sprintf( '%s%s', trailingslashit( $_path ), $info );
176  }
177  // Cha!
178  else {
179  return false;
180  }
181 
182  // Stability
183  if ( !empty( $handle ) && !empty( $src ) ) {
184  wp_register_script( $handle, $src, $_deps, $_version, $_in_footer );
185  }
186  }
187  }
188  return true;
189  }
190 
205  public static function enqueue_script_pages( $pages, $handle, $src = false, $deps = array(), $ver = false,
206  $in_footer = false )
207  {
208  if ( empty( $pages ) ) {
209  return false;
210  }
211  if ( is_string( $pages ) ) {
212  $pages = array( $pages );
213  }
214 
215  // Any single or array
216  $handles = (array)$handle;
217  foreach ( $pages as $slug ) {
218  if ( is_page( $slug ) ) {
219  foreach ( $handles as $handle ) {
220  wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
221  }
222  break;
223  }
224  }
225  return true;
226  }
227 
241  public static function enqueue_style_pages( $pages, $handle, $src = false, $deps = array(), $ver = false )
242  {
243  if ( empty( $pages ) ) {
244  return false;
245  }
246  if ( is_string( $pages ) ) {
247  $pages = array( $pages );
248  }
249 
250  // Any single or array
251  $handles = (array)$handle;
252  foreach ( $pages as $slug ) {
253  if ( is_page( $slug ) ) {
254  foreach ( $handles as $handle ) {
255  wp_enqueue_style( $handle, $src, $deps, $ver );
256  }
257  break;
258  }
259  }
260  return true;
261  }
262 
263 
264 
279  public static function enqueue_script_page_templates( $page_templates, $handle, $src = false, $deps = array(),
280  $ver = false, $in_footer = false )
281  {
282  if ( empty( $page_templates ) ) {
283  return false;
284  }
285  if ( is_string( $page_templates ) ) {
286  $page_templates = array( $page_templates );
287  }
288 
289  // Any single or array
290  $handles = (array)$handle;
291  foreach ( $page_templates as $slug ) {
292  if ( is_page_template( $slug ) ) {
293  foreach ( $handles as $handle ) {
294  wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
295  }
296  break;
297  }
298  }
299  return true;
300  }
301 
315  public static function enqueue_style_page_templates( $page_templates, $handle, $src = false, $deps = array(), $ver = false )
316  {
317  if ( empty( $page_templates ) ) {
318  return false;
319  }
320  if ( is_string( $page_templates ) ) {
321  $page_templates = array( $page_templates );
322  }
323 
324  // Any single or array
325  $handles = (array)$handle;
326  foreach ( $page_templates as $slug ) {
327  if ( is_page_template( $slug ) ) {
328  foreach ( $handles as $handle ) {
329  wp_enqueue_style( $handle, $src, $deps, $ver );
330  }
331  break;
332  }
333  }
334  return true;
335  }
336 
337 
338 }
339