'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.
?>