/* * Foo_Plugin_Options class * * A helper class for storing all your plugin options as a single WP option. Multi-site friendly. * * Version: 2.1 * Author: Brad Vincent * Author URI: http://fooplugins.com * License: GPL2 */ if ( !class_exists( 'Foo_Plugin_Options_v2_1' ) ) { class Foo_Plugin_Options_v2_1 { /** * @var string The name of the option that will be saved to the options table. */ protected $option_name; /** * Foo_Plugin_Options Constructor * * @param string $option_name The name of the single option we want to save in the options table. Usually the plugin slug. */ function __construct($option_name) { $this->option_name = $option_name; } /** * Private function used to return the merged array of all options. * @return array */ private function get_options() { //get the options based on the type of install (multisite or not) if ( is_network_admin() ) { $options = get_site_option( $this->option_name ); } else { $options = wp_parse_args( get_option( $this->option_name ), get_site_option( $this->option_name ) ); } //get some defaults (if available) $default_options = apply_filters( $this->option_name . '-default_options', array() ); //merge! return wp_parse_args( $options, $default_options ); } /** * Returns all the options in an array * @return array */ public function get_all() { return $this->get_options(); } /** * Save an individual option. * * @param string $key The key of the individual option that will be stored. * @param mixed $value The value of the individual option that will be stored. */ public function save($key, $value) { //first get the options $options = $this->get_options(); if ( !$options ) { //no options have been saved yet, so add it if ( is_network_admin() ) { add_site_option( $this->option_name, array($key => $value) ); } else { add_option( $this->option_name, array($key => $value) ); } } else { //update the existing option $options[$key] = $value; if ( is_network_admin() ) { update_site_option( $this->option_name, $options ); } else { update_option( $this->option_name, $options ); } } } /** * Get an individual option. * * @param string $key The key of the individual option that will be stored. * @param mixed $default Optional. The default value to return if the key was not found. * * @return mixed */ public function get($key, $default = false) { $options = $this->get_options(); if ( $options ) { return (array_key_exists( $key, $options )) ? $options[$key] : $default; } return $default; } /** * Delete an individual option. * * @param $key The key of the individual option we want to delete. */ public function delete($key) { $options = $this->get_options(); if ( $options ) { unset($options[$key]); if ( is_network_admin() ) { update_site_option( $this->option_name, $options ); } else { update_option( $this->option_name, $options ); } } } /** * Used to determine if an option is checked (for checkbox options only). * @param string $key * @param bool $default * * @return bool */ function is_checked($key, $default = false) { $options = $this->get_options(); if ($options) { return array_key_exists($key, $options); } return $default; } } } /* * Foo Plugin Settings * * A helpful class to handle settings for a plugin * * Version: 2.0 * Author: Brad Vincent * Author URI: http://fooplugins.com * License: GPL2 */ if ( !class_exists( 'Foo_Plugin_Settings_v2_0' ) ) { class Foo_Plugin_Settings_v2_0 { protected $plugin_slug; protected $_settings = array(); //the plugin settings array protected $_settings_sections = array(); //the plugin sections array protected $_settings_tabs = array(); //the plugin tabs array protected $_admin_errors = false; //store of admin errors function __construct($plugin_slug) { $this->plugin_slug = $plugin_slug; } function get_tabs() { return $this->_settings_tabs; } //check if we have any setting of a certain type function has_setting_of_type($type) { foreach ( $this->_settings as $setting ) { if ( $setting['type'] == $type ) return true; } return false; } function has_tab($tab_id) { return array_key_exists( $tab_id, $this->_settings_tabs ); } // add a setting tab function add_tab($tab_id, $title) { if ( !$this->has_tab( $tab_id ) ) { //pre action do_action( $this->plugin_slug . '-before_settings_tab', $tab_id, $title ); $tab = array( 'id' => $tab_id, 'title' => $title ); $this->_settings_tabs[$tab_id] = $tab; //post action do_action( $this->plugin_slug . '-after_settings_tab', $tab_id, $title ); } } function has_section($section_id) { return array_key_exists( $section_id, $this->_settings_sections ); } // add a setting section function add_section($section_id, $title, $desc = '') { //check we have the section if ( !$this->has_section( $section_id ) ) { //pre action do_action( $this->plugin_slug . '-before_settings_section', $section_id, $title, $desc ); $section = array( 'id' => $section_id, 'title' => $title, 'desc' => $desc ); $this->_settings_sections[$section_id] = $section; $section_callback = create_function( '', 'echo "' . $desc . '";' ); add_settings_section( $section_id, $title, $section_callback, $this->plugin_slug ); //post action do_action( $this->plugin_slug . '-after_settings_section', $section_id, $title, $desc ); } } function add_section_to_tab($tab_id, $section_id, $title, $desc = '') { if ( array_key_exists( $tab_id, $this->_settings_tabs ) ) { //get the correct section id for the tab $section_id = $tab_id . '-' . $section_id; //add the section to the tab if ( !array_key_exists( $section_id, $this->_settings_sections ) ) { $this->_settings_tabs[$tab_id]['sections'][$section_id] = $section_id; } //add the section $this->add_section( $section_id, $title, $desc ); } return $section_id; } function add_settings($settings = false) { if ( !is_array( $settings ) || ( !array_key_exists( 'settings', $settings ) ) ) return; foreach($settings['settings'] as $setting) { //add a tab if needed $tab_id = foo_safe_get( $setting, 'tab', false ); if ($tab_id !== false && !$this->has_tab( $tab_id ) && array_key_exists( 'tabs', $settings ) && array_key_exists( $tab_id, $settings['tabs'] ) ) { $tab = $settings['tabs'][$tab_id]; $this->add_tab( $tab_id, $tab ); } //add a section if needed $section_id = foo_safe_get( $setting, 'section', false ); if ($section_id !== false && !$this->has_section( $section_id ) && array_key_exists( 'sections', $settings ) && array_key_exists( $section_id, $settings['sections'] ) ) { $section = $settings['sections'][$section_id]; $this->add_section_to_tab( $tab_id, $section_id, $section['name'] ); } $this->add_setting( $setting ); } } // add a settings field function add_setting($args = array()) { $defaults = array( 'id' => 'default_field', 'title' => 'Default Field', 'desc' => '', 'default' => '', 'placeholder' => '', 'type' => 'text', 'section' => '', 'choices' => array(), 'class' => '', 'tab' => '' ); //only declare up front so no debug warnings are shown $title = $type = $id = $desc = $default = $placeholder = $choices = $class = $section = $tab = null; extract( wp_parse_args( $args, $defaults ) ); $field_args = array( 'type' => $type, 'id' => $id, 'desc' => $desc, 'default' => $default, 'placeholder' => $placeholder, 'choices' => $choices, 'label_for' => $id, 'class' => $class ); if ( count( $this->_settings ) == 0 ) { //only do this once register_setting( $this->plugin_slug, $this->plugin_slug, array($this, 'validate') ); } $this->_settings[] = $args; $section_id = foo_convert_to_key( $section ); //check we have the tab if ( !empty($tab) ) { $tab_id = foo_convert_to_key( $tab ); //add the tab $this->add_tab( $tab_id, foo_title_case( $tab ) ); //add the section $section_id = $this->add_section_to_tab( $tab_id, $section_id, foo_title_case( $section ) ); } else { //just add the section $this->add_section( $section_id, foo_title_case( $section ) ); } do_action( $this->plugin_slug . '-before_setting', $args ); //add the setting! add_settings_field( $id, $title, array($this, 'render'), $this->plugin_slug, $section_id, $field_args ); do_action( $this->plugin_slug . '-after_setting', $args ); } // render HTML for individual settings function render($args = array()) { //only declare up front so no debug warnings are shown $type = $id = $desc = $default = $placeholder = $choices = $class = $section = $tab = null; extract( $args ); $options = get_option($this->plugin_slug); if ( function_exists( 'is_multisite' ) && is_multisite() ) { //if we are in the network settings then use site options directly. if (is_network_admin()) { $options = get_site_option($this->plugin_slug); } else { $site_options = get_site_option($this->plugin_slug); $options = wp_parse_args($options, $site_options); } } $has_options = $options !== false; if ( !isset($options[$id]) && $type != 'checkbox' ) { $options[$id] = $default; } $field_class = ''; if ( $class != '' ) { $field_class = ' class="' . $class . '"'; } $errors = get_settings_errors( $id ); do_action( $this->plugin_slug . '-before_settings_render', $args ); switch ( $type ) { case 'heading': echo '' . $desc; break; case 'html': echo $desc; break; case 'checkbox': $checked = ''; if (isset($options[$id]) && $options[$id] == 'on') { $checked = ' checked="checked"'; } else if ($options === false && $default == 'on') { $checked = ' checked="checked"'; } else if ($has_options === false && $default == 'on') { $checked = ' checked="checked"'; } //echo ''; echo ' '; break; case 'select': echo ''; foreach ( $choices as $value => $label ) { $selected = ''; if ( $options[$id] == $value ) { $selected = ' selected="selected"'; } echo ''; } echo ''; break; case 'radio': $i = 0; $saved_value = $options[$id]; if ( empty($saved_value) ) { $saved_value = $default; } foreach ( $choices as $value => $label ) { $selected = ''; if ( $saved_value == $value ) { $selected = ' checked="checked"'; } echo ' '; if ( $i < count( $choices ) - 1 ) { echo '
'; } $i++; } break; case 'textarea': echo '' . esc_attr( $options[$id] ) . ''; break; case 'password': echo ''; break; case 'text': echo ''; break; case 'checkboxlist': $i = 0; foreach ( $choices as $value => $label ) { $checked = ''; if ( isset($options[$id][$value]) && $options[$id][$value] == 'true' ) { $checked = 'checked="checked"'; } echo ' '; if ( $i < count( $choices ) - 1 ) { echo '
'; } $i++; } break; case 'image': echo ''; echo ''; break; default: do_action( $this->plugin_slug . '-settings_custom_type_render', $args ); break; } do_action( $this->plugin_slug . '-after_settings_render', $args ); if ( is_array( $errors ) ) { foreach ( $errors as $error ) { echo "{$error['message']}"; } } if ( $type != 'checkbox' && $type != 'heading' && $type != 'html' && $desc != '' ) { echo '
' . $desc . ''; } } // validate our settings function validate($input) { //check to see if the options were reset if ( isset ($input['reset-defaults']) ) { delete_option( $this->plugin_slug ); delete_option( $this->plugin_slug . '_valid' ); delete_option( $this->plugin_slug . '_valid_expires' ); add_settings_error( 'reset', 'reset_error', __( 'Settings restored to default values', $this->plugin_slug ), 'updated' ); return false; } // if (empty($input['sample_text'])) { // // add_settings_error( // 'sample_text', // setting title // 'sample_text_error', // error ID // 'Please enter some sample text', // error message // 'error' // type of message // ); // // } foreach ( $this->_settings as $setting ) { $this->validate_setting( $setting, $input ); } return $input; } function validate_setting($setting, &$input) { //validate a single setting if ( $setting['type'] == 'checkboxlist' ) { unset($checkboxarray); foreach ( $setting['choices'] as $value => $label ) { if ( !empty($input[$setting['id'] . '|' . $value]) ) { // If it's not null, make sure it's true, add it to an array $checkboxarray[$value] = 'true'; } else { $checkboxarray[$value] = 'false'; } } if ( !empty($checkboxarray) ) { $input[$setting['id']] = $checkboxarray; } } } } } /* * Foo Plugin TextDomain Class * * A helpful class to handle loading plugin language files * * Version: 1.0 * Author: Brad Vincent * Author URI: http://fooplugins.com * License: GPL2 */ if ( !class_exists( 'Foo_Plugin_TextDomain_v1_0' ) ) { class Foo_Plugin_TextDomain_v1_0 { /** * Loads the plugin language files * * @access public * @since 1.0 * * @param string $plugin_file The main plugin file * @param string $plugin_slug The plugin slug/folder name * @param string $language_directory The plugin language directory relative to the main plugin file. Default directory is /languages/ * * @return void */ public static function load_textdomain($plugin_file, $plugin_slug, $language_directory = '/languages/') { // Set filter for plugin's languages directory $lang_dir = dirname( plugin_basename( $plugin_file ) ) . $language_directory; $lang_dir = apply_filters( $plugin_slug . '_languages_directory', $lang_dir ); // Traditional WordPress plugin locale filter $locale = apply_filters( 'plugin_locale', get_locale(), $plugin_slug ); $mo_file = sprintf( '%1$s-%2$s.mo', $plugin_slug, $locale ); // Setup paths to current locale file $mo_file_local = $lang_dir . $mo_file; $mo_file_global = WP_LANG_DIR . "/{$plugin_slug}/" . $mo_file; if ( file_exists( $mo_file_global ) ) { // Look in global /wp-content/languages/plugin-slug/ folder load_textdomain( $plugin_slug, $mo_file_global ); } elseif ( file_exists( $mo_file_local ) ) { // Look in local /wp-content/plugins/plugin-slug/languages/ folder load_textdomain( $plugin_slug, $mo_file_local ); } else { // Load the default language files load_plugin_textdomain( $plugin_slug, false, $lang_dir ); } } } }