Skip to content

Commit 18935af

Browse files
committed
v0.6.0
-add: method register_js to class-easy_settings_api.php -add: method enqueue_js to class-easy_settings_api.php -change: settings-array in sac_demo.php -add: folder js -add: demo_js.js to /js -add: alert.js to /js
1 parent 1c97231 commit 18935af

File tree

4 files changed

+142
-30
lines changed

4 files changed

+142
-30
lines changed

class-easy_settings_api.php

Lines changed: 89 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
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
/**

js/alert.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Second demo JavaScript for Easy Settings API Class
3+
*/
4+
5+
alert( 'Second JavaScript demo was loaded' );

js/demo_js.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Demo JavaScript for Easy Settings API Class
3+
*/
4+
5+
jQuery(document).ready(function($){
6+
console.log('JavaScript was loaded');
7+
//alert('JavaScript was loaded');
8+
});

sac_demo.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,53 @@ public function __construct()
115115
'capability' => 'manage_options',
116116
'icon' => 'icon-options-general',
117117

118+
/*
119+
* Enqueue JavaScript
120+
*
121+
* Simple entry: tag => source (script will be enqueued with no dependencies, in head)
122+
*
123+
* Entry with params: tag => array( params )
124+
*
125+
* Params are:
126+
* - src: full path to file
127+
* - dependencies: dependencies as array
128+
* - version: js version
129+
* - in_footer: load script in footer (true) or head (false)
130+
*/
131+
'js_scripts' => array(
132+
'sac-demo2-js' => plugins_url( 'js/alert.js', __FILE__ ),
133+
134+
'sac-demo-js' => array(
135+
'src' => plugins_url('/js/demo_js.js', __FILE__ ),
136+
'dependencies' => array( 'jquery' ),
137+
'version' => false,
138+
'in_footer' => true
139+
),
140+
141+
// some other javascripts
142+
),
143+
144+
/*
145+
* Sections
146+
*/
118147
'sections' => array(
119148
'general' => __('General Settings'),
120149
'multi' => __('Multiple Choice')
121150
),
122151

152+
/*
153+
* Section descriptions
154+
*/
123155
'section_desc' => array(
124156
'general' => __('Description for general settings (optional).'),
125157
'multi' => __('More than one choice are available.'),
126158
),
127159

160+
/*
161+
* Settings fields
162+
*
163+
* Each field can define some params. Minimum are 'id', 'type' and 'title'
164+
*/
128165
'settings_fields' => array(
129166
array(
130167
'id' => 'demo_heading',
@@ -142,7 +179,7 @@ public function __construct()
142179
'callback' => array( __CLASS__, 'custom_callback' ),
143180
// each single array-element is passed as single argument to the
144181
// callback-function.
145-
// all keys in an associatively array will be lost.
182+
// all keys in an associativ array will be lost.
146183
// if an array should passed as argument to the callback-function,
147184
// it must be itself an array.
148185
'arguments' => array( 'one' => 'eins', array( 'two' => 'zwei' ) ),
@@ -252,7 +289,8 @@ public function __construct()
252289
'section' => 'multi',
253290
),
254291

255-
)
292+
), // end_settings-fields
293+
256294
);
257295

258296
// create the options-page

0 commit comments

Comments
 (0)