3
3
* @package WordPress
4
4
* @subpackage Settings-API class
5
5
* @author Ralf Albert
6
- * @version 0.5.2
6
+ * @version 0.6.0
7
7
* @license GPL
8
8
*/
9
9
@@ -76,7 +76,9 @@ class Easy_Settings_API
76
76
* All settings
77
77
* @var array
78
78
*/
79
- protected $ _settings = array ();
79
+ protected static $ _settings = array ();
80
+
81
+ protected static $ static_settings = array ();
80
82
81
83
/**
82
84
*
@@ -196,8 +198,8 @@ public function setup( array $settings = null) {
196
198
'users ' , 'options ' , 'management ' , 'menu '
197
199
);
198
200
199
- if ( ! in_array ( $ this -> _settings ['menu_position ' ], $ whitelist_where ) )
200
- $ this -> _settings ['menu_position ' ] = 'options ' ;
201
+ if ( ! in_array ( self :: $ _settings ['menu_position ' ], $ whitelist_where ) )
202
+ self :: $ _settings ['menu_position ' ] = 'options ' ;
201
203
202
204
// extract vars from $_settings
203
205
// copy needed vars from array $_settings to the class-vars
@@ -208,11 +210,13 @@ public function setup( array $settings = null) {
208
210
'sections ' , 'section_desc ' , 'settings_fields ' ,
209
211
);
210
212
211
- foreach ( $ this -> _settings as $ key => &$ value ){
213
+ foreach ( self :: $ _settings as $ key => &$ value ){
212
214
if ( in_array ( $ key , $ whitelist_vars ) )
213
215
$ this ->$ key = $ value ;
214
216
}
215
217
218
+ self ::$ static_settings = self ::$ _settings ;
219
+
216
220
return true ;
217
221
}
218
222
@@ -304,7 +308,7 @@ protected function set_output(){
304
308
* @access public
305
309
*/
306
310
public function get_settings () {
307
- return $ this -> _settings ;
311
+ return self :: $ _settings ;
308
312
}
309
313
310
314
/**
@@ -331,11 +335,11 @@ public function set_settings( array $settings = null, $defaults = array() ){
331
335
332
336
// if defaults are set, merging them with settings
333
337
if ( ! empty ( $ defaults ) )
334
- $ this -> _settings = wp_parse_args ( $ settings , $ defaults );
338
+ self :: $ _settings = wp_parse_args ( $ settings , $ defaults );
335
339
else
336
- $ this -> _settings = $ settings ;
340
+ self :: $ _settings = $ settings ;
337
341
338
- return $ this -> _settings ;
342
+ return self :: $ _settings ;
339
343
340
344
}
341
345
@@ -350,7 +354,13 @@ public function set_settings( array $settings = null, $defaults = array() ){
350
354
*/
351
355
public function add_page () {
352
356
$ where = 'add_ ' . $ this ->menu_position . '_page ' ;
353
- $ this ->_settings ['admin_page ' ] = $ where ( $ this ->page_title , $ this ->menu_title , $ this ->capability , $ this ->page_slug , array ( &$ this , 'display_page ' ) );
357
+ self ::$ _settings ['admin_page ' ] = $ where ( $ this ->page_title , $ this ->menu_title , $ this ->capability , $ this ->page_slug , array ( &$ this , 'display_page ' ) );
358
+
359
+ // register javascript(s) if set
360
+ if ( ! empty ( self ::$ _settings ['js_scripts ' ] ) && is_array ( self ::$ _settings ['js_scripts ' ] ) ){
361
+ self ::register_js ( self ::$ _settings ['js_scripts ' ] );
362
+ }
363
+
354
364
}
355
365
356
366
/**
@@ -507,32 +517,83 @@ public function display_settings_field( array $args = array() ) {
507
517
508
518
}
509
519
510
- public function enqueue_js ( array $ src = null ){
511
- // no $src, no action
512
- if ( empty ( $ src ) )
520
+ /**
521
+ *
522
+ * Register JavaScript(s) for the optionspage
523
+ * If this method is called before the optionspage was added, than the javascripts only will be registered
524
+ * If the JavaScripts are already registered, than they will be enqueued
525
+ * @param array $scripts Array with (string) tag, (string) source, (array) dependencies, (string) version, (bool) in_footer
526
+ * @return bool true|false|number of registered scripts
527
+ * @since 0.6
528
+ * @access public static
529
+ */
530
+ public static function register_js ( array $ scripts = null ){
531
+ // no $scripts, no action
532
+ if ( empty ( $ scripts ) )
513
533
return false ;
514
534
515
- // just set $js_src , optionspage wasn't added yet. e.g. for external calls
516
- if ( empty ( $ this -> _settings ['admin_page ' ] ) ){
517
- $ this -> _settings ['js_src ' ] = $ src ;
518
- return sizeof ( $ this -> _settings ['js_src ' ] );
535
+ // just set js_scripts , optionspage wasn't added yet. e.g. for external calls
536
+ if ( empty ( self :: $ _settings ['admin_page ' ] ) ){
537
+ self :: $ _settings ['js_scripts ' ] = $ scripts ;
538
+ return sizeof ( self :: $ _settings ['js_scripts ' ] );
519
539
}
520
540
521
- //optionspage was already added. set js_src if it isn't set yet
522
- if ( empty ( $ this -> _settings ['js_src ' ] ) )
523
- $ this -> _settings ['js_src ' ] = $ src ;
541
+ //optionspage was already added. set js_scripts if it isn't set yet
542
+ if ( empty ( self :: $ _settings ['js_scripts ' ] ) )
543
+ self :: $ _settings ['js_scripts ' ] = $ scripts ;
524
544
525
- $ name = $ this ->_settings ('page_slug ' );
545
+ // optionspage was added, js_src was set, add actionhook
546
+ add_action ( 'load- ' . self ::$ _settings ['admin_page ' ], array ( __CLASS__ , 'enqueue_scripts ' ) );
526
547
527
- foreach ( $ this ->_settings ['js_src ' ] as $ key => $ val ){
528
- if ( ! is_string ($ key ) )
529
- $ key = $ name .$ key ;
530
- //TODO: pruefen ob $src eine datei und lesbar ist
531
- add_action ( 'load- ' .$ name , $ src );
548
+ return true ;
549
+ }
550
+
551
+ /**
552
+ *
553
+ * Enqueue Scripts
554
+ * Enqueue registered JavaScripts
555
+ * @param none (use in $_settings stored sorces)
556
+ * @return void
557
+ * @since 0.6
558
+ * @access public static
559
+ */
560
+ public static function enqueue_scripts (){
561
+ // no scripts, no action
562
+ if ( empty ( self ::$ _settings ['js_scripts ' ] ) || empty ( self ::$ _settings ['page_slug ' ] ))
563
+ return false ;
564
+
565
+ // use the page_slug as part of the tag if no tag was set
566
+ $ name = self ::$ _settings ['page_slug ' ];
567
+
568
+ foreach ( self ::$ _settings ['js_scripts ' ] as $ tag => $ values ){
569
+ // no tag was set
570
+ if ( ! is_string ( $ tag ) )
571
+ $ tag = $ name . '- ' . $ tag ;
572
+
573
+ // the simplest way, $values is just a string. make $values an array
574
+ if ( ! is_array ( $ values ) ){
575
+ $ values = array ( 'src ' => $ values );
576
+
577
+ }
578
+
579
+ $ defaults = array ( 'src ' => false ,
580
+ 'dependencies ' => array (),
581
+ 'version ' => false ,
582
+ 'in_footer ' => true
583
+ );
584
+ $ values = wp_parse_args ( $ values , $ defaults );
585
+
586
+ if ( ! is_array ( $ values ['dependencies ' ] ) )
587
+ $ values ['dependencies ' ] = (array ) $ values ['dependencies ' ];
588
+
589
+ // maybe no source was set. but don't care about if $src exists or is readable!!!
590
+ if ( ! $ values ['src ' ] )
591
+ continue ;
592
+
593
+ wp_enqueue_script ( $ tag , $ values ['src ' ], $ values ['dependencies ' ], $ values ['version ' ], $ values ['in_footer ' ] );
532
594
}
533
595
534
- return true ;
535
- }
596
+ }
536
597
537
598
/* --------------- sanitizing --------------- */
538
599
/**
0 commit comments