From f61ad89d08631228773d30d44c34778ef0f9b745 Mon Sep 17 00:00:00 2001 From: Gaurav Mittal Date: Wed, 20 Jan 2016 11:45:22 +0530 Subject: [PATCH 1/2] Changes in code to choose whether to save option as theme_mod or option both for customizer and admin options --- lib/class-customizer.php | 1 + lib/class-customizer.php.bak | 402 ++++++++++++++ lib/class-option-multicheck.php | 3 +- lib/class-option-multicheck.php.bak | 158 ++++++ lib/class-option.php | 30 +- lib/class-option.php.bak | 402 ++++++++++++++ lib/class-titan-framework.php | 4 +- lib/class-titan-framework.php.bak | 807 ++++++++++++++++++++++++++++ 8 files changed, 1796 insertions(+), 11 deletions(-) create mode 100644 lib/class-customizer.php.bak create mode 100644 lib/class-option-multicheck.php.bak create mode 100644 lib/class-option.php.bak create mode 100644 lib/class-titan-framework.php.bak diff --git a/lib/class-customizer.php b/lib/class-customizer.php index 75669226..dc419b9a 100644 --- a/lib/class-customizer.php +++ b/lib/class-customizer.php @@ -375,6 +375,7 @@ public function register( $wp_customize ) { $wp_customize->add_setting( $option->getID(), array( 'default' => $option->settings['default'], 'transport' => $transport, + 'type' =>$option->settings['save_type'] ) ); } diff --git a/lib/class-customizer.php.bak b/lib/class-customizer.php.bak new file mode 100644 index 00000000..75669226 --- /dev/null +++ b/lib/class-customizer.php.bak @@ -0,0 +1,402 @@ + '', // Name of the menu item + // 'parent' => null, // slug of parent, if blank, then this is a top level menu + 'id' => '', // Unique ID of the menu item + 'panel' => '', // The Name of the panel to create + 'panel_desc' => '', // The description to display on the panel + 'panel_id' => '', // The panel ID to create / add to. If this is blank & `panel` is given, this will be generated + 'capability' => 'edit_theme_options', // User role + // 'icon' => 'dashicons-admin-generic', // Menu icon for top level menus only + 'desc' => '', // Description + 'position' => 30,// Menu position for top level menus only + ); + + public $settings; + public $options = array(); + public $owner; + + // Makes sure we only load live previewing CSS only once + private static $namespacesWithPrintedPreviewCSS = array(); + + function __construct( $settings, $owner ) { + $this->owner = $owner; + + $this->settings = array_merge( $this->defaultSettings, $settings ); + + if ( empty( $this->settings['name'] ) ) { + $this->settings['name'] = __( 'More Options', TF_I18NDOMAIN ); + } + + if ( empty( $this->settings['id'] ) ) { + $this->settings['id'] = $this->owner->optionNamespace . '_' . str_replace( ' ', '-', trim( strtolower( $this->settings['name'] ) ) ); + } + + if ( empty( $this->settings['panel_id'] ) && ! empty( $this->settings['panel'] ) ) { + $this->settings['panel_id'] = $this->owner->optionNamespace . '_' . str_replace( ' ', '-', trim( strtolower( $this->settings['panel'] ) ) ); + } + + // Register the customizer control. + add_action( 'customize_register', array( $this, 'register' ) ); + + // Enqueue required customizer styles & scripts. + tf_add_action_once( 'customize_controls_enqueue_scripts', array( $this, 'loadUploaderScript' ) ); + + // Clear local storage, we use it for remembering modified customizer values. + tf_add_action_once( 'customize_controls_print_footer_scripts', array( $this, 'initLocalStorage' ) ); + + // Generate the custom CSS for live previews. + tf_add_action_once( 'wp_ajax_tf_generate_customizer_css', array( $this, 'ajaxGenerateCustomizerCSS' ) ); + + // Modify the values of the options for the generation of CSS with the values from the customizer $_POST. + global $wp_customize; + if ( isset( $wp_customize ) ) { + tf_add_filter_once( 'tf_pre_get_value_' . $this->owner->optionNamespace, array( $this, 'useCustomizerModifiedValue' ), 10, 3 ); + } + } + + public function loadUploaderScript() { + wp_enqueue_media(); + wp_enqueue_script( 'tf-theme-customizer-serialize', TitanFramework::getURL( '../js/min/serialize-min.js', __FILE__ ) ); + wp_enqueue_style( 'tf-admin-theme-customizer-styles', TitanFramework::getURL( '../css/admin-theme-customizer-styles.css', __FILE__ ) ); + } + + public function getID() { + return $this->settings['id']; + } + + + /** + * Ajax handler for generating CSS based on the existing options with values changed to + * match the customizer modified values. + * + * @since 1.9.2 + * + * @return void + */ + public function ajaxGenerateCustomizerCSS() { + + // This value is passed back to the live preview ajax handler in $this->livePreviewMainScript() + $generated = array( + 'css' => '', + ); + + foreach ( TitanFramework::getAllInstances() as $framework ) { + + // Modify the values of the options for the generation of CSS with the values from the customizer $_POST. + $namespace = $framework->optionNamespace; + add_filter( "tf_pre_get_value_{$namespace}", array( $this, 'useCustomizerModifiedValue' ), 10, 3 ); + + // Generate our new CSS based on the customizer values + $css = $framework->cssInstance->generateCSS(); + + $generated['css'] .= $css; + + /** + * Allow options to add customizer live preview parameters. The tf_generate_customizer_preview_js hook allows for manipulating these values. + * + * @since 1.9.2 + * + * @see tf_generate_customizer_preview_js + */ + $generated = apply_filters( "tf_generate_customizer_preview_css_{$namespace}", $generated ); + + } + + wp_send_json_success( $generated ); + } + + + /** + * Override the getOption value with the customizer value which comes from the $_POST array + * + * @since 1.9.2 + * + * @param mixed $value The value of the option. + * @param int $postID The post ID if there is one (always null in this case). + * @param TitanFrameworkOption $option The option being parsed. + * + * @return mixed The new value + * + * @see tf_pre_get_value_{namespace} + */ + public function useCustomizerModifiedValue( $value, $postID, $option ) { + if ( empty( $_POST ) ) { + return $value; + } + if ( ! is_array( $_POST ) ) { + return $value; + } + if ( array_key_exists( $option->getID(), $_POST ) ) { + return $_POST[ $option->getID() ]; + } + + if ( ! empty( $_POST['customized'] ) ) { + $customizedSettings = (array) json_decode( stripslashes( $_POST['customized'] ) ); + if ( is_array( $customizedSettings ) && ! empty( $customizedSettings ) ) { + if ( array_key_exists( $option->getID(), $customizedSettings ) ) { + return $customizedSettings[ $option->getID() ]; + } + } + } + return $value; + } + + + /** + * Prints the script that clears the JS local storage when customizer loads, this ensures we start fresh. + * Use localStorage so we can still use values when the customizer refreshes. + * + * @since 1.9.2 + * + * @return void + */ + public function initLocalStorage() { + ?> + + + + options as $option ) { + + if ( empty( $option->settings['css'] ) && empty( $option->settings['livepreview'] ) ) { + continue; + } + + // Print the starting script tag. + if ( ! $printStart ) { + $printStart = true; + ?> + + owner->optionNamespace, self::$namespacesWithPrintedPreviewCSS ) ) { + self::$namespacesWithPrintedPreviewCSS[] = $this->owner->optionNamespace; + + echo ''; + } + } + + public function register( $wp_customize ) { + add_action( 'wp_head', array( $this, 'printPreviewCSS' ), 1000 ); + + // Create the panel + if ( ! empty( $this->settings['panel_id'] ) ) { + $existingPanels = $wp_customize->panels(); + + if ( ! array_key_exists( $this->settings['panel_id'], $existingPanels ) ) { + $wp_customize->add_panel( $this->settings['panel_id'], array( + 'title' => $this->settings['panel'], + 'priority' => $this->settings['position'], + 'capability' => $this->settings['capability'], + 'description' => ! empty( $this->settings['panel_desc'] ) ? $this->settings['panel_desc'] : '', + ) ); + } + } + + // Create the section + $existingSections = $wp_customize->sections(); + + if ( ! array_key_exists( $this->settings['id'], $existingSections ) ) { + $wp_customize->add_section( $this->settings['id'], array( + 'title' => $this->settings['name'], + 'priority' => $this->settings['position'], + 'description' => $this->settings['desc'], + 'capability' => $this->settings['capability'], + 'panel' => empty( $this->settings['panel_id'] ) ? '' : $this->settings['panel_id'], + ) ); + } + + // Unfortunately we have to call each option's register from here + foreach ( $this->options as $index => $option ) { + if ( ! empty( $option->settings['id'] ) ) { + + $namespace = $this->owner->optionNamespace; + $option_type = $option->settings['type']; + $transport = empty( $option->settings['livepreview'] ) && empty( $option->settings['css'] ) ? 'refresh' : 'postMessage'; + + // Allow options to override the transport parameter + if ( ! empty( $option->settings['transport'] ) ) { + $transport = $option->settings['transport']; + } + + /** + * Allow options to override the transport mode of an option in the customizer + * + * @since 1.9.2 + */ + $transport = apply_filters( "tf_customizer_transport_{$option_type}_{$namespace}", $transport, $option ); + + $wp_customize->add_setting( $option->getID(), array( + 'default' => $option->settings['default'], + 'transport' => $transport, + ) ); + } + + // We add the index here, this will be used to order the controls because of this minor bug: + // https://core.trac.wordpress.org/ticket/20733 + $option->registerCustomizerControl( $wp_customize, $this, $index + 100 ); + } + + add_action( 'wp_footer', array( $this, 'livePreview' ) ); + tf_add_action_once( 'wp_footer', array( $this, 'livePreviewMainScript' ) ); + } + + public function createOption( $settings ) { + if ( ! apply_filters( 'tf_create_option_continue_' . $this->owner->optionNamespace, true, $settings ) ) { + return null; + } + + $obj = TitanFrameworkOption::factory( $settings, $this ); + $this->options[] = $obj; + + do_action( 'tf_create_option_' . $this->owner->optionNamespace, $obj ); + + return $obj; + } +} diff --git a/lib/class-option-multicheck.php b/lib/class-option-multicheck.php index 8b61acef..083831a7 100644 --- a/lib/class-option-multicheck.php +++ b/lib/class-option-multicheck.php @@ -39,13 +39,14 @@ public function cleanValueForSaving( $value ) { return array(); } if ( is_serialized( $value ) ) { + $value = unserialize($value); return $value; } // CSV if ( is_string( $value ) ) { $value = explode( ',', $value ); } - return serialize( $value ); + return implode( ',',$value ); } public function cleanValueForGetting( $value ) { diff --git a/lib/class-option-multicheck.php.bak b/lib/class-option-multicheck.php.bak new file mode 100644 index 00000000..8b61acef --- /dev/null +++ b/lib/class-option-multicheck.php.bak @@ -0,0 +1,158 @@ + array(), + ); + + /* + * Display for options and meta + */ + public function display() { + $this->echoOptionHeader( true ); + + echo '
'; + + $savedValue = $this->getValue(); + + foreach ( $this->settings['options'] as $value => $label ) { + printf('
', + $this->getID() . $value, + $this->getID() . $value, + $this->getID(), + esc_attr( $value ), + checked( in_array( $value, $savedValue ), true, false ), + $label + ); + } + + echo '
'; + + $this->echoOptionFooter( false ); + } + + public function cleanValueForSaving( $value ) { + if ( empty( $value ) ) { + return array(); + } + if ( is_serialized( $value ) ) { + return $value; + } + // CSV + if ( is_string( $value ) ) { + $value = explode( ',', $value ); + } + return serialize( $value ); + } + + public function cleanValueForGetting( $value ) { + if ( empty( $value ) ) { + return array(); + } + if ( is_array( $value ) ) { + return $value; + } + if ( is_serialized( $value ) ) { + return unserialize( $value ); + } + if ( is_string( $value ) ) { + return explode( ',', $value ); + } + } + + /* + * Display for theme customizer + */ + public function registerCustomizerControl( $wp_customize, $section, $priority = 1 ) { + $wp_customize->add_control( new TitanFrameworkOptionMulticheckControl( $wp_customize, $this->getID(), array( + 'label' => $this->settings['name'], + 'section' => $section->settings['id'], + 'settings' => $this->getID(), + 'description' => $this->settings['desc'], + 'options' => $this->settings['options'], + 'priority' => $priority, + ) ) ); + } +} + + +/* + * WP_Customize_Control with description + */ +add_action( 'customize_register', 'registerTitanFrameworkOptionMulticheckControl', 1 ); +function registerTitanFrameworkOptionMulticheckControl() { + class TitanFrameworkOptionMulticheckControl extends WP_Customize_Control { + public $description; + public $options; + + private static $firstLoad = true; + + // Since theme_mod cannot handle multichecks, we will do it with some JS + public function render_content() { + // the saved value is an array. convert it to csv + if ( is_array( $this->value() ) ) { + $savedValueCSV = implode( ',', $this->value() ); + $values = $this->value(); + } else { + $savedValueCSV = $this->value(); + $values = explode( ',', $this->value() ); + } + + if ( self::$firstLoad ) { + self::$firstLoad = false; + + ?> + + description ) ) { + $description = "

" . $this->description . '

'; + } + ?> + + '', 'example' => '', // An example value for this field, will be displayed in a + + 'save_type' => 'option' ); /** @@ -148,9 +150,11 @@ public function getValue( $postID = null ) { } if ( $this->type == self::TYPE_ADMIN ) { - - $value = $this->getFramework()->getInternalAdminPageOption( $this->settings['id'], $this->settings['default'] ); - + if($this->settings['save_type'] == 'theme_mod'){ + $value = get_theme_mod( $this->getID(), $this->settings['default'] ); + }else{ + $value = $this->getFramework()->getInternalAdminPageOption( $this->settings['id'], $this->settings['default'] ); + } } else if ( $this->type == self::TYPE_META ) { if ( empty( $postID ) ) { @@ -167,9 +171,11 @@ public function getValue( $postID = null ) { } else { $value = $this->settings['default']; } - } else if ( $this->type == self::TYPE_CUSTOMIZER ) { + } else if ( $this->type == self::TYPE_CUSTOMIZER && $this->settings['save_type'] == 'theme_mod') { $value = get_theme_mod( $this->getID(), $this->settings['default'] ); - } + }else if($this->type == self::TYPE_CUSTOMIZER){ + $value = $this->getFramework()->getInternalAdminPageOption( $this->settings['id'], $this->settings['default'] ); + } /** * Allow others to change the value of the option before it gets cleaned @@ -199,8 +205,11 @@ public function setValue( $value, $postID = null ) { $value = $this->cleanValueForSaving( $value ); if ( $this->type == self::TYPE_ADMIN ) { - + if($this->settings['save_type'] == 'theme_mod'){ + $value = set_theme_mod( $this->getID(), $value ); + }else{ $this->getFramework()->setInternalAdminPageOption( $this->settings['id'], $value ); + } } else if ( $this->type == self::TYPE_META ) { @@ -214,11 +223,13 @@ public function setValue( $value, $postID = null ) { update_post_meta( $postID, $this->getID(), $value ); - } else if ( $this->type == self::TYPE_CUSTOMIZER ) { + } else if ( $this->type == self::TYPE_CUSTOMIZER && $this->settings['save_type'] == 'theme_mod' ) { set_theme_mod( $this->getID(), $value ); - } + }else if($this->type == self::TYPE_CUSTOMIZER){ + $this->getFramework()->setInternalAdminPageOption( $this->settings['id'], $value ); + } do_action( 'tf_set_value_' . $this->settings['type'] . '_' . $this->getOptionNamespace(), $value, $postID, $this ); @@ -256,6 +267,9 @@ public function getOptionNamespace() { } public function getID() { + if($this->type == self::TYPE_CUSTOMIZER && $this->settings['save_type'] == 'option'){ + return $this->getOptionNamespace().'_options['.$this->settings['id'].']'; + } return $this->getOptionNamespace() . '_' . $this->settings['id']; } diff --git a/lib/class-option.php.bak b/lib/class-option.php.bak new file mode 100644 index 00000000..9d4cedef --- /dev/null +++ b/lib/class-option.php.bak @@ -0,0 +1,402 @@ + 'text', + + /** + * The name of the option, for display purposes only. + * + * @since 1.0 + * @var string + */ + 'name' => '', + + /** + * The description to display together with this option. + * + * @since 1.0 + * @var string + */ + 'desc' => '', + + /** + * A unique ID for this option. This ID will be used to get the value for this option. + * + * @since 1.0 + * @var string + */ + 'id' => '', + + /** + * (Optional) The default value for this option. + * + * @since 1.0 + * @var mixed + */ + 'default' => '', + + /** + * (Optional) jQuery code that updates something in your site in the live preview. Only used when the option is placed in a theme customizer section. + * + * @since 1.0 + * @var string + * @see http://www.titanframework.net/livepreview-parameter + */ + 'livepreview' => '', // jQuery script to update something in the site. For theme customizer only + + /** + * (Optional) CSS rules to be used with this option. Only used when the option is placed in an admin page / panel or a theme customizer section. + * @since 1.0 + * @var string + * @see http://www.titanframework.net/generate-css-automatically-for-your-options/ + */ + 'css' => '', + + /** + * (Optional) If true, the option will not be displayed, but will still be accessible using `getOption`. This is helpful for deprecating old settings, while still making your project backward compatible. + * @since 1.8 + * @var bool + */ + 'hidden' => false, + + /** + * (Optional) The transport parameter in the Customizer is automatically set. Use this to override the transport value. Value can be blank, 'refresh' or 'postMessage' + * @since 1.9.3 + * @var string + */ + 'transport' => '', + + 'example' => '', // An example value for this field, will be displayed in a + + 'save_type' => 'option' + ); + + /** + * Default settings specific for this option. This is overridden by each option class + * @var array + */ + public $defaultSecondarySettings = array(); + + public static function factory( $settings, $owner ) { + $settings = array_merge( self::$defaultSettings, $settings ); + + $className = 'TitanFrameworkOption' . str_replace( ' ', '', ucwords( str_replace( '-', ' ', $settings['type'] ) ) ); + + // assume all the classes are already required + if ( ! class_exists( $className ) && ! class_exists( $settings['type'] ) ) { + TitanFramework::displayFrameworkError( + sprintf( __( 'Option type or extended class %s does not exist.', TF_I18NDOMAIN ), '' . $settings['type'] . '', $settings ), + $settings ); + return null; + } + + if ( class_exists( $className ) ) { + $obj = new $className( $settings, $owner ); + return $obj; + } + + $className = $settings['type']; + $obj = new $className( $settings, $owner ); + return $obj; + } + + function __construct( $settings, $owner ) { + $this->owner = $owner; + + $this->settings = array_merge( self::$defaultSettings, $this->defaultSecondarySettings ); + $this->settings = array_merge( $this->settings, $settings ); + + $this->type = is_a( $owner, 'TitanFrameworkMetaBox' ) ? self::TYPE_META : self::TYPE_ADMIN; + $this->type = is_a( $owner, 'TitanFrameworkCustomizer' ) ? self::TYPE_CUSTOMIZER : $this->type; + + // Generate a unique ID depending on the settings for those without IDs + if ( empty( $this->settings['id'] ) && $this->settings['type'] != 'save' ) { + $this->settings['id'] = substr( md5( serialize( $this->settings ) . serialize( $this->owner->settings ) ), 0, 16 ); + } + } + + + public function getValue( $postID = null ) { + + $value = false; + + if ( empty( $this->settings['id'] ) ) { + return $value; + } + + if ( $this->type == self::TYPE_ADMIN ) { + if($this->settings['save_type'] == 'theme_mod'){ + $value = get_theme_mod( $this->getID(), $this->settings['default'] ); + }else{ + $value = $this->getFramework()->getInternalAdminPageOption( $this->settings['id'], $this->settings['default'] ); + } + } else if ( $this->type == self::TYPE_META ) { + + if ( empty( $postID ) ) { + $postID = $this->owner->postID; + } + // If no $postID is given, try and get it if we are in a loop. + if ( empty( $postID ) && ! is_admin() && get_post() != null ) { + $postID = get_the_ID(); + } + + // for meta options, use the default value for new posts/pages + if ( metadata_exists( 'post', $postID, $this->getID() ) ) { + $value = get_post_meta( $postID, $this->getID(), true ); + } else { + $value = $this->settings['default']; + } + } else if ( $this->type == self::TYPE_CUSTOMIZER && $this->settings['save_type'] == 'theme_mod') { + $value = get_theme_mod( $this->getID(), $this->settings['default'] ); + }else if($this->type == self::TYPE_CUSTOMIZER){ + $value = $this->getFramework()->getInternalAdminPageOption( $this->settings['id'], $this->settings['default'] ); + } + + /** + * Allow others to change the value of the option before it gets cleaned + * + * @since 1.9.2 + */ + $value = apply_filters( 'tf_pre_get_value_' . $this->getOptionNamespace(), $value, $postID, $this ); + + // Apply cleaning method for the value (for serialized data, slashes, etc). + $value = $this->cleanValueForGetting( $value ); + + /** + * Allow others to change the value of the option after it gets cleaned + * + * @since 1.9 + */ + return apply_filters( 'tf_get_value_' . $this->settings['type'] . '_' . $this->getOptionNamespace(), $value, $postID, $this ); + } + + + /** + * + */ + public function setValue( $value, $postID = null ) { + + // Apply cleaning method for the value (for serialized data, slashes, etc). + $value = $this->cleanValueForSaving( $value ); + + if ( $this->type == self::TYPE_ADMIN ) { + + $this->getFramework()->setInternalAdminPageOption( $this->settings['id'], $value ); + + } else if ( $this->type == self::TYPE_META ) { + + if ( empty( $postID ) ) { + $postID = $this->owner->postID; + } + // If no $postID is given, try and get it if we are in a loop. + if ( empty( $postID ) && ! is_admin() && get_post() != null ) { + $postID = get_the_ID(); + } + + update_post_meta( $postID, $this->getID(), $value ); + + } else if ( $this->type == self::TYPE_CUSTOMIZER ) { + + set_theme_mod( $this->getID(), $value ); + + } + + do_action( 'tf_set_value_' . $this->settings['type'] . '_' . $this->getOptionNamespace(), $value, $postID, $this ); + + return true; + } + + + /** + * Gets the framework instance currently used + * + * @return TitanFramework + * @since 1.3 + */ + protected function getFramework() { + if ( is_a( $this->owner, 'TitanFrameworkAdminTab' ) ) { + // a tab's parent is an admin panel + return $this->owner->owner->owner; + } else { + // an admin panel's parent is the framework + // a meta panel's parent is the framework + // a theme customizer's parent is the framework + return $this->owner->owner; + } + } + + + /** + * Gets the option namespace used in the framework instance currently used + * + * @return string The option namespace + * @since 1.0 + */ + public function getOptionNamespace() { + return $this->getFramework()->optionNamespace; + } + + public function getID() { + return $this->getOptionNamespace() . '_' . $this->settings['id']; + } + + public function __call( $name, $args ) { + $default = is_array( $args ) && count( $args ) ? $args[0] : ''; + if ( stripos( $name, 'get' ) == 0 ) { + $setting = strtolower( substr( $name, 3 ) ); + return empty( $this->settings[ $setting ] ) ? $default : $this->settings[ $setting ]; + } + return $default; + } + + protected function echoOptionHeader( $showDesc = false ) { + // Allow overriding for custom styling + $useCustom = false; + $useCustom = apply_filters( 'tf_use_custom_option_header', $useCustom ); + $useCustom = apply_filters( 'tf_use_custom_option_header_' . $this->getOptionNamespace(), $useCustom ); + if ( $useCustom ) { + do_action( 'tf_custom_option_header', $this ); + do_action( 'tf_custom_option_header_' . $this->getOptionNamespace(), $this ); + return; + } + + $id = $this->getID(); + $name = $this->getName(); + $evenOdd = self::$rowIndex++ % 2 == 0 ? 'odd' : 'even'; + + $style = $this->getHidden() == true ? 'style="display: none"' : ''; + + ?> + > + + + + + getDesc(); + if ( ! empty( $desc ) && $showDesc ) : + ?> +

+ getOptionNamespace(), $useCustom ); + if ( $useCustom ) { + do_action( 'tf_custom_option_header', $this ); + do_action( 'tf_custom_option_header_' . $this->getOptionNamespace(), $this ); + return; + } + + $id = $this->getID(); + $name = $this->getName(); + $evenOdd = self::$rowIndex++ % 2 == 0 ? 'odd' : 'even'; + + $style = $this->getHidden() == true ? 'style="display: none"' : ''; + + ?> + > + + getOptionNamespace(), $useCustom ); + if ( $useCustom ) { + do_action( 'tf_custom_option_footer', $this ); + do_action( 'tf_custom_option_footer_' . $this->getOptionNamespace(), $this ); + return; + } + + $desc = $this->getDesc(); + if ( ! empty( $desc ) && $showDesc ) : + ?> +

+ getExample(); + if ( ! empty( $example ) ) : + ?> +

+ + + + getOptionNamespace(), $useCustom ); + if ( $useCustom ) { + do_action( 'tf_custom_option_footer', $this ); + do_action( 'tf_custom_option_footer_' . $this->getOptionNamespace(), $this ); + return; + } + + ?> + + + adminOptions ) ) { - $this->adminOptions = unserialize( $currentOptions ); + $this->adminOptions = $currentOptions; } if ( empty( $this->adminOptions ) ) { @@ -305,7 +305,7 @@ public function saveInternalAdminPageOptions() { // Run this first to ensure that adminOptions carries all our admin page options. $this->getInternalAdminOptions(); - update_option( $this->optionNamespace . '_options', serialize( $this->adminOptions ) ); + update_option( $this->optionNamespace . '_options', $this->adminOptions ); do_action( 'tf_save_options_' . $this->optionNamespace ); return $this->adminOptions; } diff --git a/lib/class-titan-framework.php.bak b/lib/class-titan-framework.php.bak new file mode 100644 index 00000000..a69bcd21 --- /dev/null +++ b/lib/class-titan-framework.php.bak @@ -0,0 +1,807 @@ + 'generate', // If 'generate', Titan will try and generate a cacheable + // CSS file (or inline if it can't). + // If 'inline', CSS will be printed out in the head tag, + // If false, CSS will not be generated nor printed. + ); + + + /** + * Gets an instance of the framework for the namespace + * + * @since 1.0 + * + * @param string $optionNamespace The namespace to get options from. + * + * @return TitanFramework + */ + public static function getInstance( $optionNamespace ) { + + // Clean namespace. + $optionNamespace = str_replace( ' ', '-', trim( strtolower( $optionNamespace ) ) ); + + foreach ( self::$instances as $instance ) { + if ( $instance->optionNamespace == $optionNamespace ) { + return $instance; + } + } + + $newInstance = new TitanFramework( $optionNamespace ); + self::$instances[] = $newInstance; + return $newInstance; + } + + + /** + * Gets all active instances of Titan Framework + * + * @since 1.9.2 + * + * @return array An array of TitanFramework objects + */ + public static function getAllInstances() { + return self::$instances; + } + + + /** + * Creates a new TitanFramework object + * + * @since 1.0 + * + * @param string $optionNamespace The namespace to get options from. + */ + function __construct( $optionNamespace ) { + + // Clean namespace. + $optionNamespace = str_replace( ' ', '-', trim( strtolower( $optionNamespace ) ) ); + + $this->optionNamespace = $optionNamespace; + $this->settings = $this->defaultSettings; + + do_action( 'tf_init', $this ); + do_action( 'tf_init_' . $this->optionNamespace, $this ); + + $this->cssInstance = new TitanFrameworkCSS( $this ); + + add_action( 'admin_enqueue_scripts', array( $this, 'loadAdminScripts' ) ); + add_action( 'tf_create_option_' . $this->optionNamespace, array( $this, 'rememberAllOptions' ) ); + add_filter( 'tf_create_option_continue_' . $this->optionNamespace, array( $this, 'removeChildThemeOptions' ), 10, 2 ); + + // Create a save option filter for customizer options. + add_filter( 'pre_update_option', array( $this, 'addCustomizerSaveFilter' ), 10, 3 ); + } + + + /** + * Action hook on tf_create_option to remember all the options, used to ensure that our + * serialized option does not get cluttered with unused options + * + * @since 1.2.1 + * + * @param TitanFrameworkOption $option The option that was just created. + * + * @return void + */ + public function rememberAllOptions( $option ) { + if ( ! empty( $option->settings['id'] ) ) { + + if ( is_admin() && isset( $this->optionsUsed[ $option->settings['id'] ] ) ) { + self::displayFrameworkError( + sprintf( __( 'All option IDs per namespace must be unique. The id %s has been used multiple times.', TF_I18NDOMAIN ), + '' . $option->settings['id'] . '' + ) + ); + } + + $this->optionsUsed[ $option->settings['id'] ] = $option; + } + } + + + /** + * Loads all the admin scripts used by Titan Framework + * + * @since 1.0 + * + * @param string $hook The slug of admin page that called the enqueue. + * + * @return void + */ + public function loadAdminScripts( $hook ) { + + // Get all options panel IDs. + $panel_ids = array(); + if ( ! empty( $this->mainContainers['admin-page'] ) ) { + foreach ( $this->mainContainers['admin-page'] as $admin_panel ) { + $panel_ids[] = $admin_panel->panelID; + } + } + + // Only enqueue scripts if we're on a Titan options page. + if ( in_array( $hook, $panel_ids ) || ! empty( $this->mainContainers['meta-box'] ) ) { + wp_enqueue_media(); + wp_enqueue_script( 'tf-serialize', TitanFramework::getURL( '../js/min/serialize-min.js', __FILE__ ) ); + wp_enqueue_script( 'tf-styling', TitanFramework::getURL( '../js/min/admin-styling-min.js', __FILE__ ) ); + wp_enqueue_style( 'tf-admin-styles', TitanFramework::getURL( '../css/admin-styles.css', __FILE__ ) ); + } + } + + + /** + * Gets all the admin page options (not meta & customizer) and loads them from the database into + * a class variable. This is needed because all our admin page options are contained in a single entry. + * + * @since 1.9 + * + * @return array All admin options currently in the instance + */ + protected function getInternalAdminOptions() { + if ( empty( $this->adminOptions ) ) { + $this->adminOptions = array(); + } + + if ( ! empty( $this->adminOptions ) ) { + return $this->adminOptions; + } + + // Check if we have options saved already. + $currentOptions = get_option( $this->optionNamespace . '_options' ); + + // First time run, this action hook can be used to trigger something. + if ( false === $currentOptions ) { + do_action( 'tf_init_no_options_' . $this->optionNamespace ); + } + + // Put all the available options in our global variable for future checking. + if ( ! empty( $currentOptions ) && ! count( $this->adminOptions ) ) { + $this->adminOptions = unserialize( $currentOptions ); + } + + if ( empty( $this->adminOptions ) ) { + $this->adminOptions = array(); + } + + return $this->adminOptions; + } + + + /** + * Gets the admin page option that's loaded into the instance, used by the option class + * + * @since 1.9 + * + * @param string $optionName The ID of the option (not namespaced). + * @param mixed $defaultValue The default value to return if the option isn't available yet. + * + * @return mixed The option value + * + * @see TitanFrameworkOption->getValue() + */ + public function getInternalAdminPageOption( $optionName, $defaultValue = false ) { + + // Run this first to ensure that adminOptions carries all our admin page options. + $this->getInternalAdminOptions(); + + if ( array_key_exists( $optionName, $this->adminOptions ) ) { + return $this->adminOptions[ $optionName ]; + } else { + return $defaultValue; + } + } + + + /** + * Sets the admin page option that's loaded into the instance, used by the option class. + * Doesn't perform a save, only sets the value in the class variable. + * + * @since 1.9 + * + * @param string $optionName The ID of the option (not namespaced). + * @param mixed $value The value to set. + * + * @return bool Always returns true + * + * @see TitanFrameworkOption->setValue() + */ + public function setInternalAdminPageOption( $optionName, $value ) { + + // Run this first to ensure that adminOptions carries all our admin page options. + $this->getInternalAdminOptions(); + + $this->adminOptions[ $optionName ] = $value; + return true; + } + + + /** + * Saves all the admin (not meta & customizer) options which are currently loaded into this instance + * + * @since 1.0 + * + * @return array All admin options currently in the instance + */ + public function saveInternalAdminPageOptions() { + + // Run this first to ensure that adminOptions carries all our admin page options. + $this->getInternalAdminOptions(); + + update_option( $this->optionNamespace . '_options', serialize( $this->adminOptions ) ); + do_action( 'tf_save_options_' . $this->optionNamespace ); + return $this->adminOptions; + } + + + /** + * Create a admin page + * + * @deprecated 1.9 Use createContainer() with 'type' => 'admin-page' or createAdminPanel() instead. + * @since 1.0 + * + * @param array $settings The arguments for creating the admin page. + * + * @return TitanFrameworkAdminPage The created admin page + */ + public function createAdminPanel( $settings ) { + // _deprecated_function( __FUNCTION__, '1.9', 'createAdminPage' ); + return $this->createAdminPage( $settings ); + } + + + /** + * Create a admin page + * + * @since 1.0 + * + * @param array $settings The arguments for creating the admin page. + * + * @return TitanFrameworkAdminPage The created admin page + */ + public function createAdminPage( $settings ) { + $settings['type'] = 'admin-page'; + $container = $this->createContainer( $settings ); + do_action( 'tf_admin_panel_created_' . $this->optionNamespace, $container ); + return $container; + } + + + /** + * Create a meta box + * + * @since 1.0 + * + * @param array $settings The arguments for creating the meta box. + * + * @return TitanFrameworkMetaBox The created meta box + */ + public function createMetaBox( $settings ) { + $settings['type'] = 'meta-box'; + return $this->createContainer( $settings ); + } + + + /** + * Create a customizer section + * + * @deprecated 1.9 Use createContainer() with 'type' => 'customizer' or createCustomizer instead. + * @since 1.0 + * + * @param array $settings The arguments for creating a customizer section. + * + * @return TitanFrameworkCustomizer The created section + */ + public function createThemeCustomizerSection( $settings ) { + // _deprecated_function( __FUNCTION__, '1.9', 'createContainer' ); + return $this->createCustomizer( $settings ); + } + + + /** + * Create a customizer section + * + * @since 1.9 + * + * @param array $settings The arguments for creating a customizer section. + * + * @return TitanFrameworkCustomizer The created section + */ + public function createCustomizer( $settings ) { + $settings['type'] = 'customizer'; + $container = $this->createContainer( $settings ); + do_action( 'tf_theme_customizer_created_' . $this->optionNamespace, $container ); + return $container; + } + + + /** + * Creates a container (e.g. admin page, meta box, customizer section) depending + * on the `type` parameter given in $settings + * + * @since 1.9 + * + * @param array $settings The arguments for creating the container. + * + * @return TitanFrameworkCustomizer|TitanFrameworkAdminPage|TitanFrameworkMetaBox The created container + */ + public function createContainer( $settings ) { + if ( empty( $settings['type'] ) ) { + self::displayFrameworkError( sprintf( __( '%s needs a %s parameter.', TF_I18NDOMAIN ), '' . __FUNCTION__ . '', 'type' ) ); + return; + } + + $type = strtolower( $settings['type'] ); + $class = 'TitanFramework' . str_replace( ' ', '', ucfirst( str_replace( '-', ' ', $settings['type'] ) ) ); + $action = str_replace( '-', '_', $type ); + $container = false; + + if ( ! class_exists( $class ) ) { + self::displayFrameworkError( sprintf( __( 'Container of type %s, does not exist.', TF_I18NDOMAIN ), '' . $settings['type'] . '' ) ); + return; + } + + // Create the container object. + $container = new $class( $settings, $this ); + if ( empty( $this->mainContainers[ $type ] ) ) { + $this->mainContainers[ $type ] = array(); + } + + $this->mainContainers[ $type ][] = $container; + + do_action( 'tf_' . $action . '_created_' . $this->optionNamespace, $container ); + + return $container; + } + + + /** + * A function available ONLY to CHILD themes to stop the creation of options + * created by the PARENT theme. + * + * @since 1.2.1 + * @access public + * + * @param string $optionName The id of the option to remove / stop from being created. + * + * @return void + */ + public function removeOption( $optionName ) { + $this->optionsToRemove[] = $optionName; + } + + + /** + * Hook to the tf_create_option_continue filter, to check whether or not to continue + * adding an option (if the option id was used in $titan->removeOption). + * + * @since 1.2.1 + * @access public + * + * @param boolean $continueCreating If true, the option will be created. + * @param array $optionSettings The settings for the option to be created. + * + * @return boolean If true, continue with creating the option. False to stop it.. + */ + public function removeChildThemeOptions( $continueCreating, $optionSettings ) { + if ( ! count( $this->optionsToRemove ) ) { + return $continueCreating; + } + if ( empty( $optionSettings['id'] ) ) { + return $continueCreating; + } + if ( in_array( $optionSettings['id'], $this->optionsToRemove ) ) { + return false; + } + return $continueCreating; + } + + + /** + * Get an option + * + * @since 1.0 + * + * @param string $optionName The name of the option. + * @param int $postID The post ID if this is a meta option. + * + * @return mixed The option value + */ + public function getOption( $optionName, $postID = null ) { + $value = false; + + // Get the option value. + if ( array_key_exists( $optionName, $this->optionsUsed ) ) { + $option = $this->optionsUsed[ $optionName ]; + $value = $option->getValue( $postID ); + } + + return apply_filters( 'tf_get_option_' . $this->optionNamespace, $value, $optionName, $postID ); + } + + + /** + * Gets a set of options. Pass an associative array containing the option names as keys and + * the values you want to be retained if the option names are not implemented. + * + * @since 1.9 + * + * @param array $optionArray An associative array containing option names as keys. + * @param int $postID The post ID if this is a meta option. + * + * @return array An array containing the values saved. + * + * @see $this->getOption() + */ + public function getOptions( $optionArray, $postID = null ) { + foreach ( $optionArray as $optionName => $originalValue ) { + if ( array_key_exists( $optionName, $this->optionsUsed ) ) { + $optionArray[ $optionName ] = $this->getOption( $optionName, $postID ); + } + } + return apply_filters( 'tf_get_options_' . $this->optionNamespace, $optionArray, $postID ); + } + + + /** + * Sets an option + * + * @since 1.0 + * + * @param string $optionName The name of the option to save. + * @param mixed $value The value of the option. + * @param int $postID The ID of the parent post if this is a meta box option. + * + * @return boolean Always returns true + */ + public function setOption( $optionName, $value, $postID = null ) { + + // Get the option value. + if ( array_key_exists( $optionName, $this->optionsUsed ) ) { + $option = $this->optionsUsed[ $optionName ]; + $option->setValue( $value, $postID ); + } + + do_action( 'tf_set_option_' . $this->optionNamespace, $optionName, $value, $postID ); + + return true; + } + + + /** + * Deletes ALL the options for the namespace. Even deletes all meta found in all posts. + * Mainly used for unit tests + * + * @since 1.9 + * + * @return void + */ + public function deleteAllOptions() { + + // Delete all admin options. + delete_option( $this->optionNamespace . '_options' ); + $this->adminOptions = array(); + + // Delete all meta options. + global $wpdb; + $allPosts = $wpdb->get_results( 'SELECT ID FROM ' . $wpdb->posts, ARRAY_A ); + if ( ! empty( $allPosts ) ) { + foreach ( $allPosts as $row ) { + $allMeta = get_post_meta( $row['ID'] ); + + // Only remove meta data that the framework created. + foreach ( $allMeta as $metaName => $dummy ) { + if ( stripos( $metaName, $this->optionNamespace . '_' ) === 0 ) { + delete_post_meta( $row['ID'], $metaName ); + } + } + } + } + + // Delete all theme mods. + $allThemeMods = get_theme_mods(); + if ( ! empty( $allThemeMods ) && is_array( $allThemeMods ) ) { + foreach ( $allThemeMods as $optionName => $dummy ) { + + // Only remove theme mods that the framework created. + if ( stripos( $optionName, $this->optionNamespace . '_' ) === 0 ) { + remove_theme_mod( $optionName ); + } + } + } + } + + + /** + * Generates style rules which can use options as their values + * + * @since 1.0 + * + * @param string $CSSString The styles to render. + * + * @return void + */ + public function createCSS( $CSSString ) { + $this->cssInstance->addCSS( $CSSString ); + } + + + /** + * Displays an error notice + * + * @since 1.0 + * + * @param string $message The error message to display. + * @param array|object $errorObject The object to dump inside the error message. + * + * @return void + */ + public static function displayFrameworkError( $message, $errorObject = null ) { + // Clean up the debug object for display. e.g. If this is a setting, we can have lots of blank values. + if ( is_array( $errorObject ) ) { + foreach ( $errorObject as $key => $val ) { + if ( '' === $val ) { + unset( $errorObject[ $key ] ); + } + } + } + + // Display an error message. + ?> +
Error: + + +
+ +
+ settings[ $setting ]; + $this->settings[ $setting ] = $value; + + do_action( 'tf_setting_' . $setting . '_changed_' . $this->optionNamespace, $value, $oldValue ); + } + + + /** + * Gets the CSS generated + * + * @since 1.6 + * + * @return string The generated CSS + */ + public function generateCSS() { + return $this->cssInstance->generateCSS(); + } + + + + /** + * Adds a 'tf_save_option_{namespace}_{optionID}' filter to all Customizer options + * which are just about to be saved + * + * This uses the `pre_update_option` filter to check all the options being saved if it's + * a theme_mod option. It further checks whether these are Titan customizer options, + * then attaches the new hook into those. + * + * @since 1.8 + * + * @param mixed $value The value to be saved in the options. + * @param string $optionName The option name. + * @param mixed $oldValue The previously stored value. + * + * @return mixed The modified value to save + * + * @see pre_update_option filter + */ + public function addCustomizerSaveFilter( $value, $optionName, $oldValue ) { + + $theme = get_option( 'stylesheet' ); + + // Intercept theme mods only. + if ( strpos( $optionName, 'theme_mods_' . $theme ) !== 0 ) { + return $value; + } + + // We expect theme mods to be an array. + if ( ! is_array( $value ) ) { + return $value; + } + + // Checks whether a Titan customizer is in place. + $customizerUsed = false; + + // Go through all our customizer options and filter them for saving. + $optionIDs = array(); + if ( ! empty( $this->mainContainers['customizer'] ) ) { + foreach ( $this->mainContainers['customizer'] as $customizer ) { + foreach ( $customizer->options as $option ) { + if ( ! empty( $option->settings['id'] ) ) { + $optionID = $option->settings['id']; + $themeModName = $this->optionNamespace . '_' . $option->settings['id']; + + if ( ! array_key_exists( $themeModName, $value ) ) { + continue; + } + + $customizerUsed = true; + + // Try and unserialize if possible. + $tempValue = $value[ $themeModName ]; + if ( is_serialized( $tempValue ) ) { + $tempValue = unserialize( $tempValue ); + } + + // Hook 'tf_save_option_{namespace}'. + $newValue = apply_filters( 'tf_save_option_' . $this->optionNamespace, $tempValue, $option->settings['id'] ); + + // Hook 'tf_save_option_{namespace}_{optionID}'. + $newValue = apply_filters( 'tf_save_option_' . $themeModName, $tempValue ); + + // We mainly check for equality here so that we won't have to serialize IF the value wasn't touched anyway. + if ( $newValue != $tempValue ) { + if ( is_array( $newValue ) ) { + $newValue = serialize( $newValue ); + } + + $value[ $themeModName ] = $newValue; + } + } + } + } + } + + if ( $customizerUsed ) { + /** This action is documented in class-admin-page.php */ + $namespace = $this->optionNamespace; + do_action( "tf_pre_save_options_{$namespace}", $this->mainContainers['customizer'] ); + } + + return $value; + } +} From 26918ec8beec9d9a33eef9dbe2555bf5216a03f4 Mon Sep 17 00:00:00 2001 From: Gaurav Mittal Date: Wed, 20 Jan 2016 11:47:07 +0530 Subject: [PATCH 2/2] backup files removed --- lib/class-customizer.php.bak | 402 -------------- lib/class-option-multicheck.php.bak | 158 ------ lib/class-option.php.bak | 402 -------------- lib/class-titan-framework.php.bak | 807 ---------------------------- 4 files changed, 1769 deletions(-) delete mode 100644 lib/class-customizer.php.bak delete mode 100644 lib/class-option-multicheck.php.bak delete mode 100644 lib/class-option.php.bak delete mode 100644 lib/class-titan-framework.php.bak diff --git a/lib/class-customizer.php.bak b/lib/class-customizer.php.bak deleted file mode 100644 index 75669226..00000000 --- a/lib/class-customizer.php.bak +++ /dev/null @@ -1,402 +0,0 @@ - '', // Name of the menu item - // 'parent' => null, // slug of parent, if blank, then this is a top level menu - 'id' => '', // Unique ID of the menu item - 'panel' => '', // The Name of the panel to create - 'panel_desc' => '', // The description to display on the panel - 'panel_id' => '', // The panel ID to create / add to. If this is blank & `panel` is given, this will be generated - 'capability' => 'edit_theme_options', // User role - // 'icon' => 'dashicons-admin-generic', // Menu icon for top level menus only - 'desc' => '', // Description - 'position' => 30,// Menu position for top level menus only - ); - - public $settings; - public $options = array(); - public $owner; - - // Makes sure we only load live previewing CSS only once - private static $namespacesWithPrintedPreviewCSS = array(); - - function __construct( $settings, $owner ) { - $this->owner = $owner; - - $this->settings = array_merge( $this->defaultSettings, $settings ); - - if ( empty( $this->settings['name'] ) ) { - $this->settings['name'] = __( 'More Options', TF_I18NDOMAIN ); - } - - if ( empty( $this->settings['id'] ) ) { - $this->settings['id'] = $this->owner->optionNamespace . '_' . str_replace( ' ', '-', trim( strtolower( $this->settings['name'] ) ) ); - } - - if ( empty( $this->settings['panel_id'] ) && ! empty( $this->settings['panel'] ) ) { - $this->settings['panel_id'] = $this->owner->optionNamespace . '_' . str_replace( ' ', '-', trim( strtolower( $this->settings['panel'] ) ) ); - } - - // Register the customizer control. - add_action( 'customize_register', array( $this, 'register' ) ); - - // Enqueue required customizer styles & scripts. - tf_add_action_once( 'customize_controls_enqueue_scripts', array( $this, 'loadUploaderScript' ) ); - - // Clear local storage, we use it for remembering modified customizer values. - tf_add_action_once( 'customize_controls_print_footer_scripts', array( $this, 'initLocalStorage' ) ); - - // Generate the custom CSS for live previews. - tf_add_action_once( 'wp_ajax_tf_generate_customizer_css', array( $this, 'ajaxGenerateCustomizerCSS' ) ); - - // Modify the values of the options for the generation of CSS with the values from the customizer $_POST. - global $wp_customize; - if ( isset( $wp_customize ) ) { - tf_add_filter_once( 'tf_pre_get_value_' . $this->owner->optionNamespace, array( $this, 'useCustomizerModifiedValue' ), 10, 3 ); - } - } - - public function loadUploaderScript() { - wp_enqueue_media(); - wp_enqueue_script( 'tf-theme-customizer-serialize', TitanFramework::getURL( '../js/min/serialize-min.js', __FILE__ ) ); - wp_enqueue_style( 'tf-admin-theme-customizer-styles', TitanFramework::getURL( '../css/admin-theme-customizer-styles.css', __FILE__ ) ); - } - - public function getID() { - return $this->settings['id']; - } - - - /** - * Ajax handler for generating CSS based on the existing options with values changed to - * match the customizer modified values. - * - * @since 1.9.2 - * - * @return void - */ - public function ajaxGenerateCustomizerCSS() { - - // This value is passed back to the live preview ajax handler in $this->livePreviewMainScript() - $generated = array( - 'css' => '', - ); - - foreach ( TitanFramework::getAllInstances() as $framework ) { - - // Modify the values of the options for the generation of CSS with the values from the customizer $_POST. - $namespace = $framework->optionNamespace; - add_filter( "tf_pre_get_value_{$namespace}", array( $this, 'useCustomizerModifiedValue' ), 10, 3 ); - - // Generate our new CSS based on the customizer values - $css = $framework->cssInstance->generateCSS(); - - $generated['css'] .= $css; - - /** - * Allow options to add customizer live preview parameters. The tf_generate_customizer_preview_js hook allows for manipulating these values. - * - * @since 1.9.2 - * - * @see tf_generate_customizer_preview_js - */ - $generated = apply_filters( "tf_generate_customizer_preview_css_{$namespace}", $generated ); - - } - - wp_send_json_success( $generated ); - } - - - /** - * Override the getOption value with the customizer value which comes from the $_POST array - * - * @since 1.9.2 - * - * @param mixed $value The value of the option. - * @param int $postID The post ID if there is one (always null in this case). - * @param TitanFrameworkOption $option The option being parsed. - * - * @return mixed The new value - * - * @see tf_pre_get_value_{namespace} - */ - public function useCustomizerModifiedValue( $value, $postID, $option ) { - if ( empty( $_POST ) ) { - return $value; - } - if ( ! is_array( $_POST ) ) { - return $value; - } - if ( array_key_exists( $option->getID(), $_POST ) ) { - return $_POST[ $option->getID() ]; - } - - if ( ! empty( $_POST['customized'] ) ) { - $customizedSettings = (array) json_decode( stripslashes( $_POST['customized'] ) ); - if ( is_array( $customizedSettings ) && ! empty( $customizedSettings ) ) { - if ( array_key_exists( $option->getID(), $customizedSettings ) ) { - return $customizedSettings[ $option->getID() ]; - } - } - } - return $value; - } - - - /** - * Prints the script that clears the JS local storage when customizer loads, this ensures we start fresh. - * Use localStorage so we can still use values when the customizer refreshes. - * - * @since 1.9.2 - * - * @return void - */ - public function initLocalStorage() { - ?> - - - - options as $option ) { - - if ( empty( $option->settings['css'] ) && empty( $option->settings['livepreview'] ) ) { - continue; - } - - // Print the starting script tag. - if ( ! $printStart ) { - $printStart = true; - ?> - - owner->optionNamespace, self::$namespacesWithPrintedPreviewCSS ) ) { - self::$namespacesWithPrintedPreviewCSS[] = $this->owner->optionNamespace; - - echo ''; - } - } - - public function register( $wp_customize ) { - add_action( 'wp_head', array( $this, 'printPreviewCSS' ), 1000 ); - - // Create the panel - if ( ! empty( $this->settings['panel_id'] ) ) { - $existingPanels = $wp_customize->panels(); - - if ( ! array_key_exists( $this->settings['panel_id'], $existingPanels ) ) { - $wp_customize->add_panel( $this->settings['panel_id'], array( - 'title' => $this->settings['panel'], - 'priority' => $this->settings['position'], - 'capability' => $this->settings['capability'], - 'description' => ! empty( $this->settings['panel_desc'] ) ? $this->settings['panel_desc'] : '', - ) ); - } - } - - // Create the section - $existingSections = $wp_customize->sections(); - - if ( ! array_key_exists( $this->settings['id'], $existingSections ) ) { - $wp_customize->add_section( $this->settings['id'], array( - 'title' => $this->settings['name'], - 'priority' => $this->settings['position'], - 'description' => $this->settings['desc'], - 'capability' => $this->settings['capability'], - 'panel' => empty( $this->settings['panel_id'] ) ? '' : $this->settings['panel_id'], - ) ); - } - - // Unfortunately we have to call each option's register from here - foreach ( $this->options as $index => $option ) { - if ( ! empty( $option->settings['id'] ) ) { - - $namespace = $this->owner->optionNamespace; - $option_type = $option->settings['type']; - $transport = empty( $option->settings['livepreview'] ) && empty( $option->settings['css'] ) ? 'refresh' : 'postMessage'; - - // Allow options to override the transport parameter - if ( ! empty( $option->settings['transport'] ) ) { - $transport = $option->settings['transport']; - } - - /** - * Allow options to override the transport mode of an option in the customizer - * - * @since 1.9.2 - */ - $transport = apply_filters( "tf_customizer_transport_{$option_type}_{$namespace}", $transport, $option ); - - $wp_customize->add_setting( $option->getID(), array( - 'default' => $option->settings['default'], - 'transport' => $transport, - ) ); - } - - // We add the index here, this will be used to order the controls because of this minor bug: - // https://core.trac.wordpress.org/ticket/20733 - $option->registerCustomizerControl( $wp_customize, $this, $index + 100 ); - } - - add_action( 'wp_footer', array( $this, 'livePreview' ) ); - tf_add_action_once( 'wp_footer', array( $this, 'livePreviewMainScript' ) ); - } - - public function createOption( $settings ) { - if ( ! apply_filters( 'tf_create_option_continue_' . $this->owner->optionNamespace, true, $settings ) ) { - return null; - } - - $obj = TitanFrameworkOption::factory( $settings, $this ); - $this->options[] = $obj; - - do_action( 'tf_create_option_' . $this->owner->optionNamespace, $obj ); - - return $obj; - } -} diff --git a/lib/class-option-multicheck.php.bak b/lib/class-option-multicheck.php.bak deleted file mode 100644 index 8b61acef..00000000 --- a/lib/class-option-multicheck.php.bak +++ /dev/null @@ -1,158 +0,0 @@ - array(), - ); - - /* - * Display for options and meta - */ - public function display() { - $this->echoOptionHeader( true ); - - echo '
'; - - $savedValue = $this->getValue(); - - foreach ( $this->settings['options'] as $value => $label ) { - printf('
', - $this->getID() . $value, - $this->getID() . $value, - $this->getID(), - esc_attr( $value ), - checked( in_array( $value, $savedValue ), true, false ), - $label - ); - } - - echo '
'; - - $this->echoOptionFooter( false ); - } - - public function cleanValueForSaving( $value ) { - if ( empty( $value ) ) { - return array(); - } - if ( is_serialized( $value ) ) { - return $value; - } - // CSV - if ( is_string( $value ) ) { - $value = explode( ',', $value ); - } - return serialize( $value ); - } - - public function cleanValueForGetting( $value ) { - if ( empty( $value ) ) { - return array(); - } - if ( is_array( $value ) ) { - return $value; - } - if ( is_serialized( $value ) ) { - return unserialize( $value ); - } - if ( is_string( $value ) ) { - return explode( ',', $value ); - } - } - - /* - * Display for theme customizer - */ - public function registerCustomizerControl( $wp_customize, $section, $priority = 1 ) { - $wp_customize->add_control( new TitanFrameworkOptionMulticheckControl( $wp_customize, $this->getID(), array( - 'label' => $this->settings['name'], - 'section' => $section->settings['id'], - 'settings' => $this->getID(), - 'description' => $this->settings['desc'], - 'options' => $this->settings['options'], - 'priority' => $priority, - ) ) ); - } -} - - -/* - * WP_Customize_Control with description - */ -add_action( 'customize_register', 'registerTitanFrameworkOptionMulticheckControl', 1 ); -function registerTitanFrameworkOptionMulticheckControl() { - class TitanFrameworkOptionMulticheckControl extends WP_Customize_Control { - public $description; - public $options; - - private static $firstLoad = true; - - // Since theme_mod cannot handle multichecks, we will do it with some JS - public function render_content() { - // the saved value is an array. convert it to csv - if ( is_array( $this->value() ) ) { - $savedValueCSV = implode( ',', $this->value() ); - $values = $this->value(); - } else { - $savedValueCSV = $this->value(); - $values = explode( ',', $this->value() ); - } - - if ( self::$firstLoad ) { - self::$firstLoad = false; - - ?> - - description ) ) { - $description = "

" . $this->description . '

'; - } - ?> - - 'text', - - /** - * The name of the option, for display purposes only. - * - * @since 1.0 - * @var string - */ - 'name' => '', - - /** - * The description to display together with this option. - * - * @since 1.0 - * @var string - */ - 'desc' => '', - - /** - * A unique ID for this option. This ID will be used to get the value for this option. - * - * @since 1.0 - * @var string - */ - 'id' => '', - - /** - * (Optional) The default value for this option. - * - * @since 1.0 - * @var mixed - */ - 'default' => '', - - /** - * (Optional) jQuery code that updates something in your site in the live preview. Only used when the option is placed in a theme customizer section. - * - * @since 1.0 - * @var string - * @see http://www.titanframework.net/livepreview-parameter - */ - 'livepreview' => '', // jQuery script to update something in the site. For theme customizer only - - /** - * (Optional) CSS rules to be used with this option. Only used when the option is placed in an admin page / panel or a theme customizer section. - * @since 1.0 - * @var string - * @see http://www.titanframework.net/generate-css-automatically-for-your-options/ - */ - 'css' => '', - - /** - * (Optional) If true, the option will not be displayed, but will still be accessible using `getOption`. This is helpful for deprecating old settings, while still making your project backward compatible. - * @since 1.8 - * @var bool - */ - 'hidden' => false, - - /** - * (Optional) The transport parameter in the Customizer is automatically set. Use this to override the transport value. Value can be blank, 'refresh' or 'postMessage' - * @since 1.9.3 - * @var string - */ - 'transport' => '', - - 'example' => '', // An example value for this field, will be displayed in a - - 'save_type' => 'option' - ); - - /** - * Default settings specific for this option. This is overridden by each option class - * @var array - */ - public $defaultSecondarySettings = array(); - - public static function factory( $settings, $owner ) { - $settings = array_merge( self::$defaultSettings, $settings ); - - $className = 'TitanFrameworkOption' . str_replace( ' ', '', ucwords( str_replace( '-', ' ', $settings['type'] ) ) ); - - // assume all the classes are already required - if ( ! class_exists( $className ) && ! class_exists( $settings['type'] ) ) { - TitanFramework::displayFrameworkError( - sprintf( __( 'Option type or extended class %s does not exist.', TF_I18NDOMAIN ), '' . $settings['type'] . '', $settings ), - $settings ); - return null; - } - - if ( class_exists( $className ) ) { - $obj = new $className( $settings, $owner ); - return $obj; - } - - $className = $settings['type']; - $obj = new $className( $settings, $owner ); - return $obj; - } - - function __construct( $settings, $owner ) { - $this->owner = $owner; - - $this->settings = array_merge( self::$defaultSettings, $this->defaultSecondarySettings ); - $this->settings = array_merge( $this->settings, $settings ); - - $this->type = is_a( $owner, 'TitanFrameworkMetaBox' ) ? self::TYPE_META : self::TYPE_ADMIN; - $this->type = is_a( $owner, 'TitanFrameworkCustomizer' ) ? self::TYPE_CUSTOMIZER : $this->type; - - // Generate a unique ID depending on the settings for those without IDs - if ( empty( $this->settings['id'] ) && $this->settings['type'] != 'save' ) { - $this->settings['id'] = substr( md5( serialize( $this->settings ) . serialize( $this->owner->settings ) ), 0, 16 ); - } - } - - - public function getValue( $postID = null ) { - - $value = false; - - if ( empty( $this->settings['id'] ) ) { - return $value; - } - - if ( $this->type == self::TYPE_ADMIN ) { - if($this->settings['save_type'] == 'theme_mod'){ - $value = get_theme_mod( $this->getID(), $this->settings['default'] ); - }else{ - $value = $this->getFramework()->getInternalAdminPageOption( $this->settings['id'], $this->settings['default'] ); - } - } else if ( $this->type == self::TYPE_META ) { - - if ( empty( $postID ) ) { - $postID = $this->owner->postID; - } - // If no $postID is given, try and get it if we are in a loop. - if ( empty( $postID ) && ! is_admin() && get_post() != null ) { - $postID = get_the_ID(); - } - - // for meta options, use the default value for new posts/pages - if ( metadata_exists( 'post', $postID, $this->getID() ) ) { - $value = get_post_meta( $postID, $this->getID(), true ); - } else { - $value = $this->settings['default']; - } - } else if ( $this->type == self::TYPE_CUSTOMIZER && $this->settings['save_type'] == 'theme_mod') { - $value = get_theme_mod( $this->getID(), $this->settings['default'] ); - }else if($this->type == self::TYPE_CUSTOMIZER){ - $value = $this->getFramework()->getInternalAdminPageOption( $this->settings['id'], $this->settings['default'] ); - } - - /** - * Allow others to change the value of the option before it gets cleaned - * - * @since 1.9.2 - */ - $value = apply_filters( 'tf_pre_get_value_' . $this->getOptionNamespace(), $value, $postID, $this ); - - // Apply cleaning method for the value (for serialized data, slashes, etc). - $value = $this->cleanValueForGetting( $value ); - - /** - * Allow others to change the value of the option after it gets cleaned - * - * @since 1.9 - */ - return apply_filters( 'tf_get_value_' . $this->settings['type'] . '_' . $this->getOptionNamespace(), $value, $postID, $this ); - } - - - /** - * - */ - public function setValue( $value, $postID = null ) { - - // Apply cleaning method for the value (for serialized data, slashes, etc). - $value = $this->cleanValueForSaving( $value ); - - if ( $this->type == self::TYPE_ADMIN ) { - - $this->getFramework()->setInternalAdminPageOption( $this->settings['id'], $value ); - - } else if ( $this->type == self::TYPE_META ) { - - if ( empty( $postID ) ) { - $postID = $this->owner->postID; - } - // If no $postID is given, try and get it if we are in a loop. - if ( empty( $postID ) && ! is_admin() && get_post() != null ) { - $postID = get_the_ID(); - } - - update_post_meta( $postID, $this->getID(), $value ); - - } else if ( $this->type == self::TYPE_CUSTOMIZER ) { - - set_theme_mod( $this->getID(), $value ); - - } - - do_action( 'tf_set_value_' . $this->settings['type'] . '_' . $this->getOptionNamespace(), $value, $postID, $this ); - - return true; - } - - - /** - * Gets the framework instance currently used - * - * @return TitanFramework - * @since 1.3 - */ - protected function getFramework() { - if ( is_a( $this->owner, 'TitanFrameworkAdminTab' ) ) { - // a tab's parent is an admin panel - return $this->owner->owner->owner; - } else { - // an admin panel's parent is the framework - // a meta panel's parent is the framework - // a theme customizer's parent is the framework - return $this->owner->owner; - } - } - - - /** - * Gets the option namespace used in the framework instance currently used - * - * @return string The option namespace - * @since 1.0 - */ - public function getOptionNamespace() { - return $this->getFramework()->optionNamespace; - } - - public function getID() { - return $this->getOptionNamespace() . '_' . $this->settings['id']; - } - - public function __call( $name, $args ) { - $default = is_array( $args ) && count( $args ) ? $args[0] : ''; - if ( stripos( $name, 'get' ) == 0 ) { - $setting = strtolower( substr( $name, 3 ) ); - return empty( $this->settings[ $setting ] ) ? $default : $this->settings[ $setting ]; - } - return $default; - } - - protected function echoOptionHeader( $showDesc = false ) { - // Allow overriding for custom styling - $useCustom = false; - $useCustom = apply_filters( 'tf_use_custom_option_header', $useCustom ); - $useCustom = apply_filters( 'tf_use_custom_option_header_' . $this->getOptionNamespace(), $useCustom ); - if ( $useCustom ) { - do_action( 'tf_custom_option_header', $this ); - do_action( 'tf_custom_option_header_' . $this->getOptionNamespace(), $this ); - return; - } - - $id = $this->getID(); - $name = $this->getName(); - $evenOdd = self::$rowIndex++ % 2 == 0 ? 'odd' : 'even'; - - $style = $this->getHidden() == true ? 'style="display: none"' : ''; - - ?> - > - - - - - getDesc(); - if ( ! empty( $desc ) && $showDesc ) : - ?> -

- getOptionNamespace(), $useCustom ); - if ( $useCustom ) { - do_action( 'tf_custom_option_header', $this ); - do_action( 'tf_custom_option_header_' . $this->getOptionNamespace(), $this ); - return; - } - - $id = $this->getID(); - $name = $this->getName(); - $evenOdd = self::$rowIndex++ % 2 == 0 ? 'odd' : 'even'; - - $style = $this->getHidden() == true ? 'style="display: none"' : ''; - - ?> - > - - getOptionNamespace(), $useCustom ); - if ( $useCustom ) { - do_action( 'tf_custom_option_footer', $this ); - do_action( 'tf_custom_option_footer_' . $this->getOptionNamespace(), $this ); - return; - } - - $desc = $this->getDesc(); - if ( ! empty( $desc ) && $showDesc ) : - ?> -

- getExample(); - if ( ! empty( $example ) ) : - ?> -

- - - - getOptionNamespace(), $useCustom ); - if ( $useCustom ) { - do_action( 'tf_custom_option_footer', $this ); - do_action( 'tf_custom_option_footer_' . $this->getOptionNamespace(), $this ); - return; - } - - ?> - - - 'generate', // If 'generate', Titan will try and generate a cacheable - // CSS file (or inline if it can't). - // If 'inline', CSS will be printed out in the head tag, - // If false, CSS will not be generated nor printed. - ); - - - /** - * Gets an instance of the framework for the namespace - * - * @since 1.0 - * - * @param string $optionNamespace The namespace to get options from. - * - * @return TitanFramework - */ - public static function getInstance( $optionNamespace ) { - - // Clean namespace. - $optionNamespace = str_replace( ' ', '-', trim( strtolower( $optionNamespace ) ) ); - - foreach ( self::$instances as $instance ) { - if ( $instance->optionNamespace == $optionNamespace ) { - return $instance; - } - } - - $newInstance = new TitanFramework( $optionNamespace ); - self::$instances[] = $newInstance; - return $newInstance; - } - - - /** - * Gets all active instances of Titan Framework - * - * @since 1.9.2 - * - * @return array An array of TitanFramework objects - */ - public static function getAllInstances() { - return self::$instances; - } - - - /** - * Creates a new TitanFramework object - * - * @since 1.0 - * - * @param string $optionNamespace The namespace to get options from. - */ - function __construct( $optionNamespace ) { - - // Clean namespace. - $optionNamespace = str_replace( ' ', '-', trim( strtolower( $optionNamespace ) ) ); - - $this->optionNamespace = $optionNamespace; - $this->settings = $this->defaultSettings; - - do_action( 'tf_init', $this ); - do_action( 'tf_init_' . $this->optionNamespace, $this ); - - $this->cssInstance = new TitanFrameworkCSS( $this ); - - add_action( 'admin_enqueue_scripts', array( $this, 'loadAdminScripts' ) ); - add_action( 'tf_create_option_' . $this->optionNamespace, array( $this, 'rememberAllOptions' ) ); - add_filter( 'tf_create_option_continue_' . $this->optionNamespace, array( $this, 'removeChildThemeOptions' ), 10, 2 ); - - // Create a save option filter for customizer options. - add_filter( 'pre_update_option', array( $this, 'addCustomizerSaveFilter' ), 10, 3 ); - } - - - /** - * Action hook on tf_create_option to remember all the options, used to ensure that our - * serialized option does not get cluttered with unused options - * - * @since 1.2.1 - * - * @param TitanFrameworkOption $option The option that was just created. - * - * @return void - */ - public function rememberAllOptions( $option ) { - if ( ! empty( $option->settings['id'] ) ) { - - if ( is_admin() && isset( $this->optionsUsed[ $option->settings['id'] ] ) ) { - self::displayFrameworkError( - sprintf( __( 'All option IDs per namespace must be unique. The id %s has been used multiple times.', TF_I18NDOMAIN ), - '' . $option->settings['id'] . '' - ) - ); - } - - $this->optionsUsed[ $option->settings['id'] ] = $option; - } - } - - - /** - * Loads all the admin scripts used by Titan Framework - * - * @since 1.0 - * - * @param string $hook The slug of admin page that called the enqueue. - * - * @return void - */ - public function loadAdminScripts( $hook ) { - - // Get all options panel IDs. - $panel_ids = array(); - if ( ! empty( $this->mainContainers['admin-page'] ) ) { - foreach ( $this->mainContainers['admin-page'] as $admin_panel ) { - $panel_ids[] = $admin_panel->panelID; - } - } - - // Only enqueue scripts if we're on a Titan options page. - if ( in_array( $hook, $panel_ids ) || ! empty( $this->mainContainers['meta-box'] ) ) { - wp_enqueue_media(); - wp_enqueue_script( 'tf-serialize', TitanFramework::getURL( '../js/min/serialize-min.js', __FILE__ ) ); - wp_enqueue_script( 'tf-styling', TitanFramework::getURL( '../js/min/admin-styling-min.js', __FILE__ ) ); - wp_enqueue_style( 'tf-admin-styles', TitanFramework::getURL( '../css/admin-styles.css', __FILE__ ) ); - } - } - - - /** - * Gets all the admin page options (not meta & customizer) and loads them from the database into - * a class variable. This is needed because all our admin page options are contained in a single entry. - * - * @since 1.9 - * - * @return array All admin options currently in the instance - */ - protected function getInternalAdminOptions() { - if ( empty( $this->adminOptions ) ) { - $this->adminOptions = array(); - } - - if ( ! empty( $this->adminOptions ) ) { - return $this->adminOptions; - } - - // Check if we have options saved already. - $currentOptions = get_option( $this->optionNamespace . '_options' ); - - // First time run, this action hook can be used to trigger something. - if ( false === $currentOptions ) { - do_action( 'tf_init_no_options_' . $this->optionNamespace ); - } - - // Put all the available options in our global variable for future checking. - if ( ! empty( $currentOptions ) && ! count( $this->adminOptions ) ) { - $this->adminOptions = unserialize( $currentOptions ); - } - - if ( empty( $this->adminOptions ) ) { - $this->adminOptions = array(); - } - - return $this->adminOptions; - } - - - /** - * Gets the admin page option that's loaded into the instance, used by the option class - * - * @since 1.9 - * - * @param string $optionName The ID of the option (not namespaced). - * @param mixed $defaultValue The default value to return if the option isn't available yet. - * - * @return mixed The option value - * - * @see TitanFrameworkOption->getValue() - */ - public function getInternalAdminPageOption( $optionName, $defaultValue = false ) { - - // Run this first to ensure that adminOptions carries all our admin page options. - $this->getInternalAdminOptions(); - - if ( array_key_exists( $optionName, $this->adminOptions ) ) { - return $this->adminOptions[ $optionName ]; - } else { - return $defaultValue; - } - } - - - /** - * Sets the admin page option that's loaded into the instance, used by the option class. - * Doesn't perform a save, only sets the value in the class variable. - * - * @since 1.9 - * - * @param string $optionName The ID of the option (not namespaced). - * @param mixed $value The value to set. - * - * @return bool Always returns true - * - * @see TitanFrameworkOption->setValue() - */ - public function setInternalAdminPageOption( $optionName, $value ) { - - // Run this first to ensure that adminOptions carries all our admin page options. - $this->getInternalAdminOptions(); - - $this->adminOptions[ $optionName ] = $value; - return true; - } - - - /** - * Saves all the admin (not meta & customizer) options which are currently loaded into this instance - * - * @since 1.0 - * - * @return array All admin options currently in the instance - */ - public function saveInternalAdminPageOptions() { - - // Run this first to ensure that adminOptions carries all our admin page options. - $this->getInternalAdminOptions(); - - update_option( $this->optionNamespace . '_options', serialize( $this->adminOptions ) ); - do_action( 'tf_save_options_' . $this->optionNamespace ); - return $this->adminOptions; - } - - - /** - * Create a admin page - * - * @deprecated 1.9 Use createContainer() with 'type' => 'admin-page' or createAdminPanel() instead. - * @since 1.0 - * - * @param array $settings The arguments for creating the admin page. - * - * @return TitanFrameworkAdminPage The created admin page - */ - public function createAdminPanel( $settings ) { - // _deprecated_function( __FUNCTION__, '1.9', 'createAdminPage' ); - return $this->createAdminPage( $settings ); - } - - - /** - * Create a admin page - * - * @since 1.0 - * - * @param array $settings The arguments for creating the admin page. - * - * @return TitanFrameworkAdminPage The created admin page - */ - public function createAdminPage( $settings ) { - $settings['type'] = 'admin-page'; - $container = $this->createContainer( $settings ); - do_action( 'tf_admin_panel_created_' . $this->optionNamespace, $container ); - return $container; - } - - - /** - * Create a meta box - * - * @since 1.0 - * - * @param array $settings The arguments for creating the meta box. - * - * @return TitanFrameworkMetaBox The created meta box - */ - public function createMetaBox( $settings ) { - $settings['type'] = 'meta-box'; - return $this->createContainer( $settings ); - } - - - /** - * Create a customizer section - * - * @deprecated 1.9 Use createContainer() with 'type' => 'customizer' or createCustomizer instead. - * @since 1.0 - * - * @param array $settings The arguments for creating a customizer section. - * - * @return TitanFrameworkCustomizer The created section - */ - public function createThemeCustomizerSection( $settings ) { - // _deprecated_function( __FUNCTION__, '1.9', 'createContainer' ); - return $this->createCustomizer( $settings ); - } - - - /** - * Create a customizer section - * - * @since 1.9 - * - * @param array $settings The arguments for creating a customizer section. - * - * @return TitanFrameworkCustomizer The created section - */ - public function createCustomizer( $settings ) { - $settings['type'] = 'customizer'; - $container = $this->createContainer( $settings ); - do_action( 'tf_theme_customizer_created_' . $this->optionNamespace, $container ); - return $container; - } - - - /** - * Creates a container (e.g. admin page, meta box, customizer section) depending - * on the `type` parameter given in $settings - * - * @since 1.9 - * - * @param array $settings The arguments for creating the container. - * - * @return TitanFrameworkCustomizer|TitanFrameworkAdminPage|TitanFrameworkMetaBox The created container - */ - public function createContainer( $settings ) { - if ( empty( $settings['type'] ) ) { - self::displayFrameworkError( sprintf( __( '%s needs a %s parameter.', TF_I18NDOMAIN ), '' . __FUNCTION__ . '', 'type' ) ); - return; - } - - $type = strtolower( $settings['type'] ); - $class = 'TitanFramework' . str_replace( ' ', '', ucfirst( str_replace( '-', ' ', $settings['type'] ) ) ); - $action = str_replace( '-', '_', $type ); - $container = false; - - if ( ! class_exists( $class ) ) { - self::displayFrameworkError( sprintf( __( 'Container of type %s, does not exist.', TF_I18NDOMAIN ), '' . $settings['type'] . '' ) ); - return; - } - - // Create the container object. - $container = new $class( $settings, $this ); - if ( empty( $this->mainContainers[ $type ] ) ) { - $this->mainContainers[ $type ] = array(); - } - - $this->mainContainers[ $type ][] = $container; - - do_action( 'tf_' . $action . '_created_' . $this->optionNamespace, $container ); - - return $container; - } - - - /** - * A function available ONLY to CHILD themes to stop the creation of options - * created by the PARENT theme. - * - * @since 1.2.1 - * @access public - * - * @param string $optionName The id of the option to remove / stop from being created. - * - * @return void - */ - public function removeOption( $optionName ) { - $this->optionsToRemove[] = $optionName; - } - - - /** - * Hook to the tf_create_option_continue filter, to check whether or not to continue - * adding an option (if the option id was used in $titan->removeOption). - * - * @since 1.2.1 - * @access public - * - * @param boolean $continueCreating If true, the option will be created. - * @param array $optionSettings The settings for the option to be created. - * - * @return boolean If true, continue with creating the option. False to stop it.. - */ - public function removeChildThemeOptions( $continueCreating, $optionSettings ) { - if ( ! count( $this->optionsToRemove ) ) { - return $continueCreating; - } - if ( empty( $optionSettings['id'] ) ) { - return $continueCreating; - } - if ( in_array( $optionSettings['id'], $this->optionsToRemove ) ) { - return false; - } - return $continueCreating; - } - - - /** - * Get an option - * - * @since 1.0 - * - * @param string $optionName The name of the option. - * @param int $postID The post ID if this is a meta option. - * - * @return mixed The option value - */ - public function getOption( $optionName, $postID = null ) { - $value = false; - - // Get the option value. - if ( array_key_exists( $optionName, $this->optionsUsed ) ) { - $option = $this->optionsUsed[ $optionName ]; - $value = $option->getValue( $postID ); - } - - return apply_filters( 'tf_get_option_' . $this->optionNamespace, $value, $optionName, $postID ); - } - - - /** - * Gets a set of options. Pass an associative array containing the option names as keys and - * the values you want to be retained if the option names are not implemented. - * - * @since 1.9 - * - * @param array $optionArray An associative array containing option names as keys. - * @param int $postID The post ID if this is a meta option. - * - * @return array An array containing the values saved. - * - * @see $this->getOption() - */ - public function getOptions( $optionArray, $postID = null ) { - foreach ( $optionArray as $optionName => $originalValue ) { - if ( array_key_exists( $optionName, $this->optionsUsed ) ) { - $optionArray[ $optionName ] = $this->getOption( $optionName, $postID ); - } - } - return apply_filters( 'tf_get_options_' . $this->optionNamespace, $optionArray, $postID ); - } - - - /** - * Sets an option - * - * @since 1.0 - * - * @param string $optionName The name of the option to save. - * @param mixed $value The value of the option. - * @param int $postID The ID of the parent post if this is a meta box option. - * - * @return boolean Always returns true - */ - public function setOption( $optionName, $value, $postID = null ) { - - // Get the option value. - if ( array_key_exists( $optionName, $this->optionsUsed ) ) { - $option = $this->optionsUsed[ $optionName ]; - $option->setValue( $value, $postID ); - } - - do_action( 'tf_set_option_' . $this->optionNamespace, $optionName, $value, $postID ); - - return true; - } - - - /** - * Deletes ALL the options for the namespace. Even deletes all meta found in all posts. - * Mainly used for unit tests - * - * @since 1.9 - * - * @return void - */ - public function deleteAllOptions() { - - // Delete all admin options. - delete_option( $this->optionNamespace . '_options' ); - $this->adminOptions = array(); - - // Delete all meta options. - global $wpdb; - $allPosts = $wpdb->get_results( 'SELECT ID FROM ' . $wpdb->posts, ARRAY_A ); - if ( ! empty( $allPosts ) ) { - foreach ( $allPosts as $row ) { - $allMeta = get_post_meta( $row['ID'] ); - - // Only remove meta data that the framework created. - foreach ( $allMeta as $metaName => $dummy ) { - if ( stripos( $metaName, $this->optionNamespace . '_' ) === 0 ) { - delete_post_meta( $row['ID'], $metaName ); - } - } - } - } - - // Delete all theme mods. - $allThemeMods = get_theme_mods(); - if ( ! empty( $allThemeMods ) && is_array( $allThemeMods ) ) { - foreach ( $allThemeMods as $optionName => $dummy ) { - - // Only remove theme mods that the framework created. - if ( stripos( $optionName, $this->optionNamespace . '_' ) === 0 ) { - remove_theme_mod( $optionName ); - } - } - } - } - - - /** - * Generates style rules which can use options as their values - * - * @since 1.0 - * - * @param string $CSSString The styles to render. - * - * @return void - */ - public function createCSS( $CSSString ) { - $this->cssInstance->addCSS( $CSSString ); - } - - - /** - * Displays an error notice - * - * @since 1.0 - * - * @param string $message The error message to display. - * @param array|object $errorObject The object to dump inside the error message. - * - * @return void - */ - public static function displayFrameworkError( $message, $errorObject = null ) { - // Clean up the debug object for display. e.g. If this is a setting, we can have lots of blank values. - if ( is_array( $errorObject ) ) { - foreach ( $errorObject as $key => $val ) { - if ( '' === $val ) { - unset( $errorObject[ $key ] ); - } - } - } - - // Display an error message. - ?> -
Error: - - -
- -
- settings[ $setting ]; - $this->settings[ $setting ] = $value; - - do_action( 'tf_setting_' . $setting . '_changed_' . $this->optionNamespace, $value, $oldValue ); - } - - - /** - * Gets the CSS generated - * - * @since 1.6 - * - * @return string The generated CSS - */ - public function generateCSS() { - return $this->cssInstance->generateCSS(); - } - - - - /** - * Adds a 'tf_save_option_{namespace}_{optionID}' filter to all Customizer options - * which are just about to be saved - * - * This uses the `pre_update_option` filter to check all the options being saved if it's - * a theme_mod option. It further checks whether these are Titan customizer options, - * then attaches the new hook into those. - * - * @since 1.8 - * - * @param mixed $value The value to be saved in the options. - * @param string $optionName The option name. - * @param mixed $oldValue The previously stored value. - * - * @return mixed The modified value to save - * - * @see pre_update_option filter - */ - public function addCustomizerSaveFilter( $value, $optionName, $oldValue ) { - - $theme = get_option( 'stylesheet' ); - - // Intercept theme mods only. - if ( strpos( $optionName, 'theme_mods_' . $theme ) !== 0 ) { - return $value; - } - - // We expect theme mods to be an array. - if ( ! is_array( $value ) ) { - return $value; - } - - // Checks whether a Titan customizer is in place. - $customizerUsed = false; - - // Go through all our customizer options and filter them for saving. - $optionIDs = array(); - if ( ! empty( $this->mainContainers['customizer'] ) ) { - foreach ( $this->mainContainers['customizer'] as $customizer ) { - foreach ( $customizer->options as $option ) { - if ( ! empty( $option->settings['id'] ) ) { - $optionID = $option->settings['id']; - $themeModName = $this->optionNamespace . '_' . $option->settings['id']; - - if ( ! array_key_exists( $themeModName, $value ) ) { - continue; - } - - $customizerUsed = true; - - // Try and unserialize if possible. - $tempValue = $value[ $themeModName ]; - if ( is_serialized( $tempValue ) ) { - $tempValue = unserialize( $tempValue ); - } - - // Hook 'tf_save_option_{namespace}'. - $newValue = apply_filters( 'tf_save_option_' . $this->optionNamespace, $tempValue, $option->settings['id'] ); - - // Hook 'tf_save_option_{namespace}_{optionID}'. - $newValue = apply_filters( 'tf_save_option_' . $themeModName, $tempValue ); - - // We mainly check for equality here so that we won't have to serialize IF the value wasn't touched anyway. - if ( $newValue != $tempValue ) { - if ( is_array( $newValue ) ) { - $newValue = serialize( $newValue ); - } - - $value[ $themeModName ] = $newValue; - } - } - } - } - } - - if ( $customizerUsed ) { - /** This action is documented in class-admin-page.php */ - $namespace = $this->optionNamespace; - do_action( "tf_pre_save_options_{$namespace}", $this->mainContainers['customizer'] ); - } - - return $value; - } -}