From a2ae01154cba403b7d070160fba5e73afa5846fd Mon Sep 17 00:00:00 2001 From: Ralf Albert Date: Sun, 23 Oct 2011 01:44:30 +0200 Subject: [PATCH] v0.6.2 -add: load custom stylesheets -add: folder css -add: css/demostyle.css -add: array with stylesheets to sac_demo.php -change: deactivate javascripts and stylesheets in sac_demo.php --- class-easy_settings_api.php | 62 +++++++++++++++++++++++++++++++------ css/demostyle.css | 2 ++ sac_demo.php | 21 ++++++++++--- 3 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 css/demostyle.css diff --git a/class-easy_settings_api.php b/class-easy_settings_api.php index 84b0827..bba204f 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.6.1 + * @version 0.6.2 * @license GPL */ @@ -78,8 +78,6 @@ class Easy_Settings_API */ protected static $_settings = array(); - protected static $static_settings = array(); - /** * * Single settings @@ -227,8 +225,6 @@ public function setup( array $settings = null) { $this->$key = $value; } - self::$static_settings = self::$_settings; - return true; } @@ -242,10 +238,12 @@ public function setup( array $settings = null) { * @access public */ public function init(){ + // show errors add_action( 'admin_notices', array( &$this, 'show_errors' ) ); + // add page to admin-menu and register settings add_action( 'admin_menu', array( &$this, 'add_page' ) ); - add_action( 'admin_init', array( &$this, 'register_settings' ) ); + add_action( 'admin_init', array( &$this, 'register_settings' ) ); } /** @@ -337,6 +335,7 @@ protected function get_options( $options_name = '' ){ return $this->options; } + /** * * Returns the html-output-object @@ -352,6 +351,7 @@ protected function get_output(){ return $this->output; } + /** * @@ -382,6 +382,7 @@ protected function set_output(){ return $this->output; } + /** * * Getter for settings @@ -393,6 +394,7 @@ protected function set_output(){ public function get_settings() { return self::$_settings; } + /** * @@ -444,6 +446,7 @@ public function set_settings( array $settings = null, $defaults = array() ){ } + /** * * Add the page to the admin menu. Store page-hook in $_settings. @@ -461,8 +464,12 @@ public function add_page() { if( ! empty( self::$_settings['js_scripts'] ) && is_array( self::$_settings['js_scripts'] ) ){ self::register_js( self::$_settings['js_scripts'] ); } - + + // hook up custom stylesheets + if( ! empty( self::$_settings['styles'] ) ) + add_action( 'admin_print_styles-' . self::$_settings['admin_page'], array( &$this, 'enqueue_styles' ) ); } + /** * @@ -488,6 +495,7 @@ public function register_settings() { unset( $slug, $title, $field ); } + /** * @@ -514,6 +522,7 @@ public function create_setting( array $args = array() ) { unset( $args, $defaults ); } + /** * * Display the page @@ -537,6 +546,7 @@ public function display_page() { $this->output->display_page( $args ); } + /** * @@ -551,6 +561,7 @@ public function display_section( array $section ) { echo $this->section_descs[$section['id']]; } + /** * * Outputs the HTML for every single setting field @@ -617,6 +628,7 @@ public function display_settings_field( array $args = array() ) { } } + /** * @@ -645,7 +657,7 @@ public static function register_js( array $scripts = null ){ // optionspage was added, js_src was set, add actionhook add_action( 'load-' . self::$_settings['admin_page'], array( __CLASS__, 'enqueue_scripts' ) ); - + return true; } @@ -664,12 +676,12 @@ public static function enqueue_scripts(){ return false; // use the page_slug as part of the tag if no tag was set - $name = self::$_settings['page_slug']; + $slug = self::$_settings['page_slug']; foreach( self::$_settings['js_scripts'] as $tag => $values ){ // no tag was set if( ! is_string( $tag ) ) - $tag = $name . '-' . $tag; + $tag = $slug.'_'.$tag; // the simplest way, $values is just a string. make $values an array if( ! is_array( $values ) ){ @@ -696,6 +708,36 @@ public static function enqueue_scripts(){ } + /** + * + * Enqueue stylesheets + * Enqueue stylesheets, hooked by admin_print_styles- + * @param none + * @return void + * @since 0.6.2 + * @access public + */ + public function enqueue_styles(){ + if( ! empty( self::$_settings['styles'] ) ){ + $styles = self::$_settings['styles']; + $slug = self::$_settings['page_slug']; + $count = 0; + + // first check if $styles is an array. if not, use the page_slug as tag + if( ! is_array( $styles ) ) + $styles = array( $slug => $styles ); + + foreach( $styles as $tag => $source ){ + // if no tag is set, use page_slug+index as tag + if( ! is_string( $tag ) ) + $tag = $slug.'_'.$tag; + + wp_enqueue_style( $tag, $source, false, false, 'all' ); + } + + } + } + /* --------------- sanitizing --------------- */ /** * diff --git a/css/demostyle.css b/css/demostyle.css new file mode 100644 index 0000000..cc3432a --- /dev/null +++ b/css/demostyle.css @@ -0,0 +1,2 @@ +/* simple demo style */ +.wrap{ background: yellow; } \ No newline at end of file diff --git a/sac_demo.php b/sac_demo.php index dca1d7d..2c490aa 100644 --- a/sac_demo.php +++ b/sac_demo.php @@ -3,7 +3,7 @@ * @package WordPress * @subpackage Easy Settings API Class * @author Ralf Albert - * @version 0.3.1 + * @version 0.6.2 * */ @@ -14,7 +14,7 @@ * Domain Path: /languages * Description: Demo for the the Easy Settings-API Class (ESAC). This plugin create a simple option page to show all available setting fields * Author: Ralf Albert - * Version: 0.6.1 + * Version: 0.6.2 * Author URI: http://neun12.de/ * Licence: GPL */ @@ -128,7 +128,7 @@ public function __construct() * - 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__ ), @@ -141,7 +141,20 @@ public function __construct() // some other javascripts ), - +*/ + /* + * Enqueue stylesheets + * + * tag => source + * or simply the path to the stylesheet. than the page_slug will be use as tag + * + */ +/* + 'styles' => array( 'first-demo-style' => plugins_url( 'css/demostyle.css', __FILE__ ), + 'second-demo-style' => plugins_url( 'css/demostyle.css', __FILE__ ), + plugins_url( 'css/demostyle.css', __FILE__ ), + ), +*/ /* * Sections */