Skip to content

Commit

Permalink
v0.6.0
Browse files Browse the repository at this point in the history
-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
  • Loading branch information
RalfAlbert committed Oct 22, 2011
1 parent 1c97231 commit 18935af
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 30 deletions.
117 changes: 89 additions & 28 deletions class-easy_settings_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @package WordPress
* @subpackage Settings-API class
* @author Ralf Albert
* @version 0.5.2
* @version 0.6.0
* @license GPL
*/

Expand Down Expand Up @@ -76,7 +76,9 @@ class Easy_Settings_API
* All settings
* @var array
*/
protected $_settings = array();
protected static $_settings = array();

protected static $static_settings = array();

/**
*
Expand Down Expand Up @@ -196,8 +198,8 @@ public function setup( array $settings = null) {
'users', 'options', 'management', 'menu'
);

if ( ! in_array( $this->_settings['menu_position'], $whitelist_where ) )
$this->_settings['menu_position'] = 'options';
if ( ! in_array( self::$_settings['menu_position'], $whitelist_where ) )
self::$_settings['menu_position'] = 'options';

// extract vars from $_settings
// copy needed vars from array $_settings to the class-vars
Expand All @@ -208,11 +210,13 @@ public function setup( array $settings = null) {
'sections', 'section_desc', 'settings_fields',
);

foreach( $this->_settings as $key => &$value ){
foreach( self::$_settings as $key => &$value ){
if( in_array( $key, $whitelist_vars ) )
$this->$key = $value;
}

self::$static_settings = self::$_settings;

return true;
}

Expand Down Expand Up @@ -304,7 +308,7 @@ protected function set_output(){
* @access public
*/
public function get_settings() {
return $this->_settings;
return self::$_settings;
}

/**
Expand All @@ -331,11 +335,11 @@ public function set_settings( array $settings = null, $defaults = array() ){

// if defaults are set, merging them with settings
if( ! empty( $defaults ) )
$this->_settings = wp_parse_args( $settings, $defaults );
self::$_settings = wp_parse_args( $settings, $defaults );
else
$this->_settings = $settings;
self::$_settings = $settings;

return $this->_settings;
return self::$_settings;

}

Expand All @@ -350,7 +354,13 @@ public function set_settings( array $settings = null, $defaults = array() ){
*/
public function add_page() {
$where = 'add_' . $this->menu_position . '_page';
$this->_settings['admin_page'] = $where( $this->page_title, $this->menu_title, $this->capability, $this->page_slug, array( &$this, 'display_page' ) );
self::$_settings['admin_page'] = $where( $this->page_title, $this->menu_title, $this->capability, $this->page_slug, array( &$this, 'display_page' ) );

// register javascript(s) if set
if( ! empty( self::$_settings['js_scripts'] ) && is_array( self::$_settings['js_scripts'] ) ){
self::register_js( self::$_settings['js_scripts'] );
}

}

/**
Expand Down Expand Up @@ -507,32 +517,83 @@ public function display_settings_field( array $args = array() ) {

}

public function enqueue_js( array $src = null ){
// no $src, no action
if( empty( $src ) )
/**
*
* Register JavaScript(s) for the optionspage
* If this method is called before the optionspage was added, than the javascripts only will be registered
* If the JavaScripts are already registered, than they will be enqueued
* @param array $scripts Array with (string) tag, (string) source, (array) dependencies, (string) version, (bool) in_footer
* @return bool true|false|number of registered scripts
* @since 0.6
* @access public static
*/
public static function register_js( array $scripts = null ){
// no $scripts, no action
if( empty( $scripts ) )
return false;

// just set $js_src, optionspage wasn't added yet. e.g. for external calls
if( empty( $this->_settings['admin_page'] ) ){
$this->_settings['js_src'] = $src;
return sizeof( $this->_settings['js_src'] );
// just set js_scripts, optionspage wasn't added yet. e.g. for external calls
if( empty( self::$_settings['admin_page'] ) ){
self::$_settings['js_scripts'] = $scripts;
return sizeof( self::$_settings['js_scripts'] );
}

//optionspage was already added. set js_src if it isn't set yet
if( empty( $this->_settings['js_src'] ) )
$this->_settings['js_src'] = $src;
//optionspage was already added. set js_scripts if it isn't set yet
if( empty( self::$_settings['js_scripts'] ) )
self::$_settings['js_scripts'] = $scripts;

$name = $this->_settings('page_slug');
// optionspage was added, js_src was set, add actionhook
add_action( 'load-' . self::$_settings['admin_page'], array( __CLASS__, 'enqueue_scripts' ) );

foreach( $this->_settings['js_src'] as $key => $val ){
if( ! is_string($key) )
$key = $name.$key;
//TODO: pruefen ob $src eine datei und lesbar ist
add_action( 'load-'.$name, $src );
return true;
}

/**
*
* Enqueue Scripts
* Enqueue registered JavaScripts
* @param none (use in $_settings stored sorces)
* @return void
* @since 0.6
* @access public static
*/
public static function enqueue_scripts(){
// no scripts, no action
if( empty( self::$_settings['js_scripts'] ) || empty( self::$_settings['page_slug'] ))
return false;

// use the page_slug as part of the tag if no tag was set
$name = self::$_settings['page_slug'];

foreach( self::$_settings['js_scripts'] as $tag => $values ){
// no tag was set
if( ! is_string( $tag ) )
$tag = $name . '-' . $tag;

// the simplest way, $values is just a string. make $values an array
if( ! is_array( $values ) ){
$values = array( 'src' => $values );

}

$defaults = array( 'src' => false,
'dependencies' => array(),
'version' => false,
'in_footer' => true
);
$values = wp_parse_args( $values, $defaults );

if( ! is_array( $values['dependencies'] ) )
$values['dependencies'] = (array) $values['dependencies'];

// maybe no source was set. but don't care about if $src exists or is readable!!!
if( ! $values['src'] )
continue;

wp_enqueue_script( $tag, $values['src'], $values['dependencies'], $values['version'], $values['in_footer'] );
}

return true;
}
}

/* --------------- sanitizing --------------- */
/**
Expand Down
5 changes: 5 additions & 0 deletions js/alert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Second demo JavaScript for Easy Settings API Class
*/

alert( 'Second JavaScript demo was loaded' );
8 changes: 8 additions & 0 deletions js/demo_js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Demo JavaScript for Easy Settings API Class
*/

jQuery(document).ready(function($){
console.log('JavaScript was loaded');
//alert('JavaScript was loaded');
});
42 changes: 40 additions & 2 deletions sac_demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,53 @@ public function __construct()
'capability' => 'manage_options',
'icon' => 'icon-options-general',

/*
* Enqueue JavaScript
*
* Simple entry: tag => source (script will be enqueued with no dependencies, in head)
*
* Entry with params: tag => array( params )
*
* Params are:
* - src: full path to file
* - dependencies: dependencies as array
* - version: js version
* - in_footer: load script in footer (true) or head (false)
*/
'js_scripts' => array(
'sac-demo2-js' => plugins_url( 'js/alert.js', __FILE__ ),

'sac-demo-js' => array(
'src' => plugins_url('/js/demo_js.js', __FILE__ ),
'dependencies' => array( 'jquery' ),
'version' => false,
'in_footer' => true
),

// some other javascripts
),

/*
* Sections
*/
'sections' => array(
'general' => __('General Settings'),
'multi' => __('Multiple Choice')
),

/*
* Section descriptions
*/
'section_desc' => array(
'general' => __('Description for general settings (optional).'),
'multi' => __('More than one choice are available.'),
),

/*
* Settings fields
*
* Each field can define some params. Minimum are 'id', 'type' and 'title'
*/
'settings_fields' => array(
array(
'id' => 'demo_heading',
Expand All @@ -142,7 +179,7 @@ public function __construct()
'callback' => array( __CLASS__, 'custom_callback' ),
// each single array-element is passed as single argument to the
// callback-function.
// all keys in an associatively array will be lost.
// all keys in an associativ array will be lost.
// if an array should passed as argument to the callback-function,
// it must be itself an array.
'arguments' => array( 'one' => 'eins', array( 'two' => 'zwei' ) ),
Expand Down Expand Up @@ -252,7 +289,8 @@ public function __construct()
'section' => 'multi',
),

)
), // end_settings-fields

);

// create the options-page
Expand Down

0 comments on commit 18935af

Please sign in to comment.