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(){ ?> '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_direction', __('Slide Direction', 'djg-admin-bar-button'), array($this, '_option_button_direction'), 'djg_admin_bar_button', 'abb_button_section', array( 'label_for' => 'button_direction' ) ); 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' ) ); /*----------------------------------------------- Admin Bar settings -----------------------------------------------*/ add_settings_section( 'abb_bar_section', // ID __('What about the Amdin Bar itself?', 'djg-admin-bar-button'), // Title false, // Callback 'djg_admin_bar_button' // Page ); add_settings_field( 'bar_direction', // ID __('Slide Direction', 'djg-admin-bar-button'), // Title array($this, '_option_bar_direction'), // Callback 'djg_admin_bar_button', // Page 'abb_bar_section', // Section array( // Args 'label_for' => 'bar_direction' ) ); add_settings_field( 'bar_duration', __('Slide Duration (milliseconds)', 'djg-admin-bar-button'), array($this, '_option_bar_duration'), 'djg_admin_bar_button', 'abb_bar_section', array( 'label_for' => 'bar_duration' ) ); 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' ) ); } /** * Render the plugin page */ public function on_show_page(){ ?>

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 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; /** Button duration */ if(isset($input['button_duration'])) : $time = absint($input['button_duration']); $new_input['button_duration'] = ($time > 0) ? $time : $this->defaults['button_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; /** Bar duration */ if(isset($input['bar_duration'])) : $time = absint($input['bar_duration']); $new_input['bar_duration'] = ($time > 0) ? $time : $this->defaults['bar_duration']; endif; /** Show time */ if(isset($input['show_time'])) : $time = absint($input['show_time']); $new_input['show_time'] = ($time > 0) ? $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' => 'left', 'button_direction' => 'left', 'button_duration' => 500, 'bar_direction' => 'right', 'bar_duration' => 500, 'show_time' => 5000 ); } /** * Set the options that are available for each of the 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' => '' ); $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 */ ); if($description) : printf( "\n\t".'

%1$s

'."\n", $description /** %1$s - A brief description of the option */ ); endif; } /** * 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; if($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_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') ) ); } /** * 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 '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') ) ); } /** * 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 '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]; } } ?>