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_action('after_setup_theme', array(&$this, 'after_setup_theme')); // Add the necessary theme support
add_action('wp_enqueue_scripts', array(&$this, 'on_wp_enqueue_scripts')); // Enqueue the necessary front end scripts/styeles
add_action('wp_head', array(&$this, 'on_wp_head')); // Output the necessary CSS/JS directly into the head of the front end
add_action('admin_bar_menu', array(&$this, 'on_admin_bar_menu'), 999); // If nevessary, add the hide button to the admin menu
add_action('admin_menu', array(&$this, 'on_admin_menu')); // Add the Admin Bar Button options Settings menu
add_action('admin_init', array(&$this, 'on_admin_init')); // Register the settings that can be saved by this plugin
}
/**
* 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(){
?>
get_value('show_hide_button') || is_admin()) :
return;
endif;
$button_text = 'Hide '.$this->get_value('text');
$args = array(
'id' => 'hide',
'parent' => 'top-secondary',
'title' => sprintf('%1$s', $button_text),
'href' => '#',
'meta' => array('title' => esc_attr(strip_tags($button_text)))
);
$wp_admin_bar->add_node($args);
}
/**
* Add the Admin Bar Button options Settings menu
*/
public function on_admin_menu(){
$this->page_hook = add_options_page(
__('Admin Bar Button Settings', $this->plugin_text_domain), // Page title
__('Admin Bar Button', $this->plugin_text_domain), // Menu title
'manage_options', // Required capability
$this->plugin_slug, // 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', $this->plugin_text_domain), // Title
false, // Callback
'djg_admin_bar_button' // Page
);
add_settings_field(
'text', // ID
__('Button Text', $this->plugin_text_domain), // 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', $this->plugin_text_domain),
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', $this->plugin_text_domain),
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', $this->plugin_text_domain),
array($this, '_option_button_activate'),
'djg_admin_bar_button',
'abb_button_section',
array(
'label_for' => 'button_activate'
)
);
add_settings_field(
'button_animate',
__('Animate', $this->plugin_text_domain),
array($this, '_option_button_animate'),
'djg_admin_bar_button',
'abb_button_section',
array(
'label_for' => 'button_animate'
)
);
add_settings_field(
'button_duration',
__('Slide Duration (milliseconds)', $this->plugin_text_domain),
array($this, '_option_button_duration'),
'djg_admin_bar_button',
'abb_button_section',
array(
'label_for' => 'button_duration'
)
);
add_settings_field(
'button_direction',
__('Slide Direction', $this->plugin_text_domain),
array($this, '_option_button_direction'),
'djg_admin_bar_button',
'abb_button_section',
array(
'label_for' => 'button_direction'
)
);
/*-----------------------------------------------
WordPress Admin Bar settings
-----------------------------------------------*/
add_settings_section(
'abb_bar_section', // ID
__('WordPress Admin Bar Settings', $this->plugin_text_domain), // Title
false, // Callback
'djg_admin_bar_button' // Page
);
add_settings_field(
'bar_animate', // ID
__('Animate', $this->plugin_text_domain), // Title
array($this, '_option_bar_animate'), // Callback
'djg_admin_bar_button', // Page
'abb_bar_section', // Section
array( // Args
'label_for' => 'bar_animate'
)
);
add_settings_field(
'bar_duration',
__('Slide Duration (milliseconds)', $this->plugin_text_domain),
array($this, '_option_bar_duration'),
'djg_admin_bar_button',
'abb_bar_section',
array(
'label_for' => 'bar_duration'
)
);
add_settings_field(
'bar_direction',
__('Slide Direction', $this->plugin_text_domain),
array($this, '_option_bar_direction'),
'djg_admin_bar_button',
'abb_bar_section',
array(
'label_for' => 'bar_direction'
)
);
add_settings_field(
'bar_shown_behaviour',
__('Admin Bar Behaviour', $this->plugin_text_domain),
array($this, '_option_bar_shown_behaviour'),
'djg_admin_bar_button',
'abb_bar_section',
array(
'label_for' => 'bar_shown_behaviour'
)
);
add_settings_field(
'show_time',
__('Show Time (milliseconds)', $this->plugin_text_domain),
array($this, '_option_show_time'),
'djg_admin_bar_button',
'abb_bar_section',
array(
'label_for' => 'show_time'
)
);
add_settings_field(
'show_hide_button',
__('Show the Hide Button', $this->plugin_text_domain),
array($this, '_option_show_hide_button'),
'djg_admin_bar_button',
'abb_bar_section',
array(
'label_for' => 'show_hide_button'
)
);
}
/**
* 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-tabs'));
wp_enqueue_style('djg-admin-bar-admin', plugins_url('adminBar-admin.css?scope=admin-bar-button', __FILE__));
}
/**
* 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 animate */
if(isset($input['button_animate'])) :
$new_input['button_animate'] = (array_key_exists($input['button_animate'], $this->select_options['button_animate'])) ? $input['button_animate'] : $this->defaults['button_animate'];
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 animate */
if(isset($input['bar_animate'])) :
$new_input['bar_animate'] = (array_key_exists($input['bar_animate'], $this->select_options['bar_animate'])) ? $input['bar_animate'] : $this->defaults['bar_animate'];
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;
/** Bar shown behaviour */
if(isset($input['bar_shown_behaviour'])) :
$new_input['bar_shown_behaviour'] = (array_key_exists($input['bar_shown_behaviour'], $this->select_options['bar_shown_behaviour'])) ? $input['bar_shown_behaviour'] : $this->defaults['bar_shown_behaviour'];
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;
/** Show hide button */
if(isset($input['show_hide_button'])) :
$new_input['show_hide_button'] = (isset($input['show_hide_button'])) ? intval($input['show_hide_button']) : intval($this->defaults['show_hide_button']);
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', $this->plugin_text_domain),
'text_direction' => 'ltr',
'button_position' => 'top-left',
'button_activate' => 'both',
'button_animate' => 'yes',
'button_duration' => 500,
'button_direction' => 'left',
'bar_animate' => 'yes',
'bar_duration' => 500,
'bar_direction' => 'right',
'bar_shown_behaviour' => 'go',
'show_time' => 5000,
'show_hide_button' => 1,
);
}
/**
* Set the options that are available for each of the