From 18935af413763a13d6302794fd6ce586f1569c8c Mon Sep 17 00:00:00 2001 From: Ralf Albert Date: Sun, 16 Oct 2011 15:01:14 +0200 Subject: [PATCH] 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 --- class-easy_settings_api.php | 117 +++++++++++++++++++++++++++--------- js/alert.js | 5 ++ js/demo_js.js | 8 +++ sac_demo.php | 42 ++++++++++++- 4 files changed, 142 insertions(+), 30 deletions(-) create mode 100644 js/alert.js create mode 100644 js/demo_js.js diff --git a/class-easy_settings_api.php b/class-easy_settings_api.php index e0600e8..910c3a2 100644 --- a/class-easy_settings_api.php +++ b/class-easy_settings_api.php @@ -3,7 +3,7 @@ * @package WordPress * @subpackage Settings-API class * @author Ralf Albert - * @version 0.5.2 + * @version 0.6.0 * @license GPL */ @@ -76,7 +76,9 @@ class Easy_Settings_API * All settings * @var array */ - protected $_settings = array(); + protected static $_settings = array(); + + protected static $static_settings = array(); /** * @@ -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 @@ -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; } @@ -304,7 +308,7 @@ protected function set_output(){ * @access public */ public function get_settings() { - return $this->_settings; + return self::$_settings; } /** @@ -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; } @@ -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'] ); + } + } /** @@ -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 --------------- */ /** diff --git a/js/alert.js b/js/alert.js new file mode 100644 index 0000000..d470e36 --- /dev/null +++ b/js/alert.js @@ -0,0 +1,5 @@ +/** + * Second demo JavaScript for Easy Settings API Class + */ + +alert( 'Second JavaScript demo was loaded' ); \ No newline at end of file diff --git a/js/demo_js.js b/js/demo_js.js new file mode 100644 index 0000000..d2486a0 --- /dev/null +++ b/js/demo_js.js @@ -0,0 +1,8 @@ +/** + * Demo JavaScript for Easy Settings API Class + */ + +jQuery(document).ready(function($){ + console.log('JavaScript was loaded'); + //alert('JavaScript was loaded'); +}); \ No newline at end of file diff --git a/sac_demo.php b/sac_demo.php index de832d2..04d24b6 100644 --- a/sac_demo.php +++ b/sac_demo.php @@ -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', @@ -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' ) ), @@ -252,7 +289,8 @@ public function __construct() 'section' => 'multi', ), - ) + ), // end_settings-fields + ); // create the options-page