set_options(); // Set the currently saved options $this->set_defaults(); // Set the default options $this->set_select_options(); // Set the options available for each select } /** * Add the necessary theme support */ public function after_setup_theme(){ /** Set the CSS to remove the space typically alocated to the admin bar */ add_theme_support('admin-bar', array('callback' => array(&$this, 'on_admin_bar'))); } /** * Set the CSS to remove the space typically alocated to the admin bar */ public function on_admin_bar(){ ?> page_hook = add_options_page( __('Admin Bar Button Settings', 'djg-admin-bar-button'), // Page title __('Admin Bar Button', 'djg-admin-bar-button'), // Menu title 'manage_options', // Required capability 'djg-admin-bar-button', // Page slug array(&$this, 'on_show_page') // Rendering callback ); } /** * Register the settings that can be saved by this plugin */ public function on_admin_init(){ add_action('load-'.$this->page_hook, array(&$this, 'on_admin_load')); // Set information that can only be gathered once the page has loaded register_setting( 'admin_bar_button_group', // Group name 'admin_bar_button', // Option name array(&$this, 'on_save_settings') // Sanatize options callback ); /*----------------------------------------------- Admin Bar Button settings -----------------------------------------------*/ add_settings_section( 'abb_button_section', // ID __('Admin Bar Button Settings', 'djg-admin-bar-button'), // Title false, // Callback 'djg_admin_bar_button' // Page ); add_settings_field( 'text', // ID __('Button Text', 'djg-admin-bar-button'), // Title array($this, '_option_button_text'), // Callback 'djg_admin_bar_button', // Page 'abb_button_section', // Section array( // Args 'label_for' => 'text' ) ); add_settings_field( 'text_direction', __('Text Direction', 'djg-admin-bar-button'), array($this, '_option_text_direction'), 'djg_admin_bar_button', 'abb_button_section', array( 'label_for' => 'text_direction' ) ); add_settings_field( 'button_position', __('Position on the Screen', 'djg-admin-bar-button'), array($this, '_option_button_position'), 'djg_admin_bar_button', 'abb_button_section', array( 'label_for' => 'button_position' ) ); add_settings_field( 'button_activate', __('Button Activated On', 'djg-admin-bar-button'), array($this, '_option_button_activate'), 'djg_admin_bar_button', 'abb_button_section', array( 'label_for' => 'button_activate' ) ); add_settings_field( 'button_duration', __('Slide Duration (milliseconds)', 'djg-admin-bar-button'), array($this, '_option_button_duration'), 'djg_admin_bar_button', 'abb_button_section', array( 'label_for' => 'button_duration' ) ); add_settings_field( 'button_direction', __('Slide Direction', 'djg-admin-bar-button'), array($this, '_option_button_direction'), 'djg_admin_bar_button', 'abb_button_section', array( 'label_for' => 'button_direction' ) ); /*----------------------------------------------- Admin Bar settings -----------------------------------------------*/ add_settings_section( 'abb_bar_section', // ID __('WordPress Admin Bar Settings', 'djg-admin-bar-button'), // Title false, // Callback 'djg_admin_bar_button' // Page ); add_settings_field( 'bar_duration', // ID __('Slide Duration (milliseconds)', 'djg-admin-bar-button'), // Title array($this, '_option_bar_duration'), // Callback 'djg_admin_bar_button', // Page 'abb_bar_section', // Section array( // Args 'label_for' => 'bar_duration' ) ); add_settings_field( 'bar_direction', __('Slide Direction', 'djg-admin-bar-button'), array($this, '_option_bar_direction'), 'djg_admin_bar_button', 'abb_bar_section', array( 'label_for' => 'bar_direction' ) ); add_settings_field( 'show_time', __('Show Time (milliseconds)', 'djg-admin-bar-button'), array($this, '_option_show_time'), 'djg_admin_bar_button', 'abb_bar_section', array( 'label_for' => 'show_time' ) ); } /** * Grab the current screen and add contextual help */ public function on_admin_load(){ add_action('admin_enqueue_scripts', array(&$this, 'on_admin_enqueue_scripts')); // Enqueue the necessary admin scripts/styeles add_action('admin_print_styles-'.$this->page_hook, array(&$this, 'on_admin_print_styles')); // Print the necessary admin styles $this->screen = get_current_screen(); // Grab the current screen $this->screen->set_help_sidebar($this->do_help_sidebar()); $this->screen->add_help_tab(array( 'id' => 'description', 'title' => __('Description'), 'callback' => array(&$this, 'do_help_description') )); $this->screen->add_help_tab(array( 'id' => 'faq', 'title' => __('FAQ'), 'callback' => array(&$this, 'do_help_faq') )); $this->screen->add_help_tab(array( 'id' => 'support', 'title' => __('Support'), 'callback' => array(&$this, 'do_help_support') )); $this->screen->add_help_tab(array( 'id' => 'donate', 'title' => __('Donate'), 'callback' => array(&$this, 'do_help_donate') )); } /** * Enqueue the necessary admin scripts/styles */ public function on_admin_enqueue_scripts(){ /** Enqueue the required scripts/styles */ wp_enqueue_script('djg-admin-bar-admin', plugins_url('adminBar-admin.js?scope=admin-bar-button', __FILE__ ), array('jquery-ui-widget', 'jquery-effects-slide')); } /** * Enqueue the necessary admin styles */ public function on_admin_print_styles(){ ?>

set_defaults(); // Set the default options $this->set_select_options(); // Set the options available for each select $new_input = array(); // Create a new array to hold the sanitized options /** Button text */ if(isset($input['text'])) : $text = sanitize_text_field($input['text']); $new_input['text'] = ($text !== '') ? $text : $this->defaults['text']; endif; /** Text direction */ if(isset($input['text_direction'])) : $new_input['text_direction'] = (array_key_exists($input['text_direction'], $this->select_options['text_direction'])) ? $input['text_direction'] : $this->defaults['text_direction']; endif; /** Button position */ if(isset($input['button_position'])) : $new_input['button_position'] = (array_key_exists($input['button_position'], $this->select_options['button_position'])) ? $input['button_position'] : $this->defaults['button_position']; endif; /** Button activate */ if(isset($input['button_activate'])) : $new_input['button_activate'] = (array_key_exists($input['button_activate'], $this->select_options['button_activate'])) ? $input['button_activate'] : $this->defaults['button_activate']; endif; /** Button duration */ if(isset($input['button_duration'])) : $time = absint($input['button_duration']); $new_input['button_duration'] = ($time >= 0) ? $time : $this->defaults['button_duration']; endif; /** Button direction */ if(isset($input['button_direction'])) : $new_input['button_direction'] = (array_key_exists($input['button_direction'], $this->select_options['button_direction'])) ? $input['button_direction'] : $this->defaults['button_direction']; endif; /** Bar duration */ if(isset($input['bar_duration'])) : $time = absint($input['bar_duration']); $new_input['bar_duration'] = ($time >= 0) ? $time : $this->defaults['bar_duration']; endif; /** Bar direction */ if(isset($input['bar_direction'])) : $new_input['bar_direction'] = (array_key_exists($input['bar_direction'], $this->select_options['bar_direction'])) ? $input['bar_direction'] : $this->defaults['bar_direction']; endif; /** Show time */ if(isset($input['show_time'])) : $time = absint($input['show_time']); $new_input['show_time'] = ($time > 2000) ? $time : $this->defaults['show_time']; endif; return $new_input; } /** * Set the $options, grabbed from the 'wp_options' DB table */ private function set_options(){ $this->options = get_option('admin_bar_button'); } /** * Set the default values, used if a value is not set when the 'on_show_page' or 'on_save_settings' methods are called */ private function set_defaults(){ $this->dafaults = array( 'text' => __('Admin bar', 'djg-admin-bar-button'), 'text_direction' => 'ltr', 'button_position' => 'top-left', 'button_activate' => 'both', 'button_duration' => 500, 'button_direction' => 'left', 'bar_duration' => 500, 'bar_direction' => 'right', 'show_time' => 5000 ); } /** * Set the options that are available for each of the do_option_text($id, $args); break; case 'select' : $this->do_option_select($id, $args); break; endswitch; } /** * Output a text option * * @parma required string $id The ID of the option that is to be output * @param array $args The arguments to use for the option that is to be output */ private function do_option_text($id, $args = array()){ $defaults = array( 'name' => '', 'value' => false, 'class' => '', 'description' => false, 'tips' => false ); $args = wp_parse_args($args, $defaults); extract($args, EXTR_OVERWRITE); $name = ($name !== '') ? $name : $id; printf( "\n\t".''."\n", $id, /** %1$s - The ID of the input */ $class, /** %2$s - The class of the input */ $name, /** %3$s - The name of the select */ $value /** %4$s - The value of the option */ ); $this->do_tips($tips); $this->do_description($description); } /** * Output a '."\n", $id, /** %1$s - The ID of the select */ $class, /** %2$s - The class of the select */ $name /** %3$s - The name of the select */ ); if($optgroup) : printf( '', $optgroup /** %1$s - The title of the Option Group for this set of options */ ); endif; foreach($options as $option => $text) : $is_selected = ($option === $selected) ? ' selected="true"' : false; printf( "\t\t".''."\n", $option, /** %1$s - The option value */ $is_selected, /** %2$s - Whether or not the option is selected */ $text /** %3$s - The option text */ ); endforeach; if($optgroup) : echo ''; endif; echo "\t".''."\n"; endif; $this->do_tips($tips); $this->do_description($description); } /** * Output a tip next do an option * * @since 2.2 * @param required array $tips The tips to output (must be an array of arrays) */ private function do_tips($tips){ if(!empty($tips)) : echo ''; foreach($tips as $tip) : if(is_array($tip)) : printf( "\n\t".'%1$s: %2$s'."\n", $tip[0], /** %1$s - The tip prefix */ $tip[1] /** %2$s - The tip to display */ ); elseif(is_string($tip)) : printf( "\n\t".'%1$s'."\n", $tip /** %1$s - The tip to display */ ); endif; endforeach; echo ''; endif; } /** * Output a description underneath an option * * @since 2.2 * @param required mixed $description The description to output */ private function do_description($description){ if(is_string($description)) : printf( "\n\t".'

%1$s

'."\n", $description /** %1$s - A brief description of the option */ ); endif; } /** * Callback for outputting the 'text' option */ public function _option_button_text(){ $value = $this->get_value('text'); // Get the value currently saved for this option $this->do_option( 'text', // Option type 'text', // ID array( // Args 'name' => 'admin_bar_button[text]', 'value' => $value, 'class' => 'regular-text' ) ); } /** * Callback for outputting the 'text_direction' option */ public function _option_text_direction(){ $options = $this->select_options['text_direction']; // Get the valid options for this setting $selected = $this->get_value('text_direction'); // Get the value currently selected for this option $this->do_option( 'select', // Option type 'text_direction', // ID array( // Args 'name' => 'admin_bar_button[text_direction]', 'options' => $options, 'selected' => $selected ) ); } /** * Callback for outputting the 'button_position' option */ public function _option_button_position(){ $options = $this->select_options['button_position']; // Get the valid options for this setting $selected = $this->get_value('button_position'); // Get the value currently selected for this option $this->do_option( 'select', // Option type 'button_position', // ID array( // Args 'name' => 'admin_bar_button[button_position]', 'options' => $options, 'selected' => $selected ) ); } /** * Callback for outputting the 'button_activate' option */ public function _option_button_activate(){ $options = $this->select_options['button_activate']; // Get the valid options for this setting $selected = $this->get_value('button_activate'); // Get the value currently selected for this option $this->do_option( 'select', // Option type 'button_activate', // ID array( // Args 'name' => 'admin_bar_button[button_activate]', 'options' => $options, 'selected' => $selected, 'description' => __('The actions will activate the Admin Bar.', 'djg-admin-bar-button') ) ); } /** * Callback for outputting the 'button_duration' option */ public function _option_button_duration(){ $value = $this->get_value('button_duration'); // Get the value currently saved for this option $this->do_option( 'text', // Option type 'button_duration', // ID array( // Args 'name' => 'admin_bar_button[button_duration]', 'value' => $value, 'class' => 'regular-text', 'description' => __('The time that it takes for the Admin Bar Button to slide off of (and on to) the screen.', 'djg-admin-bar-button') ) ); } /** * Callback for outputting the 'button_direction' option */ public function _option_button_direction(){ $options = $this->select_options['button_direction']; // Get the valid options for this setting $selected = $this->get_value('button_direction'); // Get the value currently selected for this option $this->do_option( 'select', // Option type 'button_direction', // ID array( // Args 'name' => 'admin_bar_button[button_direction]', 'options' => $options, 'selected' => $selected, 'description' => __('The side of the screen from which the Admin Bar Button will exit (and enter).', 'djg-admin-bar-button'), 'tips' => array( array( __('Tip', 'djg-admin-bar-button'), __('Ignored if Admin Bar Button Slide Duration is \'0\'', 'djg-admin-bar-button') ) ) ) ); } /** * Callback for outputting the 'bar_duration' option */ public function _option_bar_duration(){ $value = $this->get_value('bar_duration'); // Get the value currently saved for this option $this->do_option( 'text', // Option type 'bar_duration', // ID array( // Args 'name' => 'admin_bar_button[bar_duration]', 'value' => $value, 'class' => 'regular-text', 'description' => __('The time that it takes for the Admin Bar to slide on to (and off of) the screen.', 'djg-admin-bar-button') ) ); } /** * Callback for outputting the 'bar_direction' option */ public function _option_bar_direction(){ $options = $this->select_options['bar_direction']; // Get the valid options for this setting $selected = $this->get_value('bar_direction'); // Get the value currently selected for this option $this->do_option( 'select', // Option type 'bar_direction', // ID array( // Args 'name' => 'admin_bar_button[bar_direction]', 'options' => $options, 'selected' => $selected, 'description' => __('The side of the screen from which the Admin Bar will enter (and exit).', 'djg-admin-bar-button'), 'tips' => array( array( __('Tip', 'djg-admin-bar-button'), __('Ignored if Admin Bar Slide Duration is \'0\'', 'djg-admin-bar-button') ) ) ) ); } /** * Callback for outputting the 'show_time' option */ public function _option_show_time(){ $value = $this->get_value('show_time'); // Get the value currently saved for this option $this->do_option( 'text', // Option type 'show_time', // ID array( // Args 'name' => 'admin_bar_button[show_time]', 'value' => $value, 'class' => 'regular-text', 'description' => __('The time that the Admin Bar will be visible for, when shown.', 'djg-admin-bar-button') ) ); } /** * Get the value of an option, checking first for a saved setting and then taking the default * * @param required string $option The option to get a value for * @return mixed The value for the selected option */ private function get_value($option){ return isset($this->options[$option]) ? $this->options[$option] : $this->dafaults[$option]; } } ?>