33 * @package WordPress
44 * @subpackage Settings-API class
55 * @author Ralf Albert
6- * @version 0.5.2
6+ * @version 0.6.0
77 * @license GPL
88 */
99
@@ -76,7 +76,9 @@ class Easy_Settings_API
7676 * All settings
7777 * @var array
7878 */
79- protected $ _settings = array ();
79+ protected static $ _settings = array ();
80+
81+ protected static $ static_settings = array ();
8082
8183 /**
8284 *
@@ -196,8 +198,8 @@ public function setup( array $settings = null) {
196198 'users ' , 'options ' , 'management ' , 'menu '
197199 );
198200
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 ' ;
201203
202204 // extract vars from $_settings
203205 // copy needed vars from array $_settings to the class-vars
@@ -208,11 +210,13 @@ public function setup( array $settings = null) {
208210 'sections ' , 'section_desc ' , 'settings_fields ' ,
209211 );
210212
211- foreach ( $ this -> _settings as $ key => &$ value ){
213+ foreach ( self :: $ _settings as $ key => &$ value ){
212214 if ( in_array ( $ key , $ whitelist_vars ) )
213215 $ this ->$ key = $ value ;
214216 }
215217
218+ self ::$ static_settings = self ::$ _settings ;
219+
216220 return true ;
217221 }
218222
@@ -304,7 +308,7 @@ protected function set_output(){
304308 * @access public
305309 */
306310 public function get_settings () {
307- return $ this -> _settings ;
311+ return self :: $ _settings ;
308312 }
309313
310314 /**
@@ -331,11 +335,11 @@ public function set_settings( array $settings = null, $defaults = array() ){
331335
332336 // if defaults are set, merging them with settings
333337 if ( ! empty ( $ defaults ) )
334- $ this -> _settings = wp_parse_args ( $ settings , $ defaults );
338+ self :: $ _settings = wp_parse_args ( $ settings , $ defaults );
335339 else
336- $ this -> _settings = $ settings ;
340+ self :: $ _settings = $ settings ;
337341
338- return $ this -> _settings ;
342+ return self :: $ _settings ;
339343
340344 }
341345
@@ -350,7 +354,13 @@ public function set_settings( array $settings = null, $defaults = array() ){
350354 */
351355 public function add_page () {
352356 $ 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+
354364 }
355365
356366 /**
@@ -507,32 +517,83 @@ public function display_settings_field( array $args = array() ) {
507517
508518 }
509519
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 ) )
513533 return false ;
514534
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 ' ] );
519539 }
520540
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 ;
524544
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 ' ) );
526547
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 ' ] );
532594 }
533595
534- return true ;
535- }
596+ }
536597
537598 /* --------------- sanitizing --------------- */
538599 /**
0 commit comments