Untitled diff

Created Diff never expires
3 removals
481 lines
23 additions
495 lines
<?php
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class TitanFramework {
class TitanFramework {
public $optionNamespace; // Options will be prefixed with this
public $optionNamespace; // Options will be prefixed with this
private $adminPanels = array();
private $adminPanels = array();
private $metaBoxes = array();
private $metaBoxes = array();
private $themeCustomizerSections = array();
private $themeCustomizerSections = array();
private $widgetAreas = array();
private $widgetAreas = array();
private $googleFontsOptions = array();
private $googleFontsOptions = array();
// We store option ids which should not be created here (see removeOption)
// We store option ids which should not be created here (see removeOption)
private $optionsToRemove = array();
private $optionsToRemove = array();
private static $instances = array();
private static $instances = array();
private $allOptionIDs = array();
private $allOptionIDs = array();
private $allOptions;
private $allOptions;
private $isNetwork = false;
public $cssInstance;
public $cssInstance;
// We store the options (with IDs) here, used for ensuring our serialized option
// We store the options (with IDs) here, used for ensuring our serialized option
// value doesn't get cluttered with unused options
// value doesn't get cluttered with unused options
public $optionsUsed = array();
public $optionsUsed = array();
public static function getInstance( $optionNamespace ) {
public static function getInstance( $optionNamespace, $isNetwork = false ) {
// Clean namespace
// Clean namespace
$optionNamespace = str_replace( ' ', '-', trim( strtolower( $optionNamespace ) ) );
$optionNamespace = str_replace( ' ', '-', trim( strtolower( $optionNamespace ) ) );
foreach ( self::$instances as $instance ) {
foreach ( self::$instances as $instance ) {
if ( $instance->optionNamespace == $optionNamespace ) {
if ( $instance->optionNamespace == $optionNamespace ) {
return $instance;
return $instance;
}
}
}
}
$newInstance = new TitanFramework( $optionNamespace );
$newInstance = new TitanFramework( $optionNamespace, $isNetwork );
self::$instances[] = $newInstance;
self::$instances[] = $newInstance;
return $newInstance;
return $newInstance;
}
}
function __construct( $optionNamespace ) {
function __construct( $optionNamespace, $isNetwork = false ) {
// Clean namespace
// Clean namespace
$optionNamespace = str_replace( ' ', '-', trim( strtolower( $optionNamespace ) ) );
$optionNamespace = str_replace( ' ', '-', trim( strtolower( $optionNamespace ) ) );
$this->optionNamespace = $optionNamespace;
$this->optionNamespace = $optionNamespace;
$this->isNetwork = $isNetwork;
do_action( 'tf_init', $this );
do_action( 'tf_init', $this );
do_action( 'tf_init_' . $this->optionNamespace, $this );
do_action( 'tf_init_' . $this->optionNamespace, $this );
$this->cssInstance = new TitanFrameworkCSS( $this );
$this->cssInstance = new TitanFrameworkCSS( $this );
add_action( 'after_setup_theme', array( $this, 'getAllOptions' ), 1 );
add_action( 'after_setup_theme', array( $this, 'getAllOptions' ), 1 );
add_action( 'after_setup_theme', array( $this, 'updateOptionDBListing' ) );
add_action( 'after_setup_theme', array( $this, 'updateOptionDBListing' ) );
if ( is_admin() ) {
if ( is_admin() ) {
add_action( 'after_setup_theme', array( $this, 'updateThemeModListing' ) );
add_action( 'after_setup_theme', array( $this, 'updateThemeModListing' ) );
add_action( 'after_setup_theme', array( $this, 'updateMetaDbListing' ) );
add_action( 'after_setup_theme', array( $this, 'updateMetaDbListing' ) );
add_action( 'tf_create_option_' . $this->optionNamespace, array( $this, "verifyUniqueIDs" ) );
add_action( 'tf_create_option_' . $this->optionNamespace, array( $this, "verifyUniqueIDs" ) );
}
}
add_action( 'admin_enqueue_scripts', array( $this, "loadAdminScripts" ) );
add_action( 'admin_enqueue_scripts', array( $this, "loadAdminScripts" ) );
add_action( 'wp_enqueue_scripts', array( $this, "loadFrontEndScripts" ) );
add_action( 'wp_enqueue_scripts', array( $this, "loadFrontEndScripts" ) );
add_action( 'tf_create_option_' . $this->optionNamespace, array( $this, "rememberGoogleFonts" ) );
add_action( 'tf_create_option_' . $this->optionNamespace, array( $this, "rememberGoogleFonts" ) );
add_action( 'tf_create_option_' . $this->optionNamespace, array( $this, "rememberAllOptions" ) );
add_action( 'tf_create_option_' . $this->optionNamespace, array( $this, "rememberAllOptions" ) );
add_filter( 'tf_create_option_continue_' . $this->optionNamespace, array( $this, "removeChildThemeOptions" ), 10, 2 );
add_filter( 'tf_create_option_continue_' . $this->optionNamespace, array( $this, "removeChildThemeOptions" ), 10, 2 );
}
}
/**
/**
* Checks all the ids and shows a warning when multiple occurances of an id is found.
* Checks all the ids and shows a warning when multiple occurances of an id is found.
* This is to ensure that there won't be any option conflicts
* This is to ensure that there won't be any option conflicts
*
*
* @param TitanFrameworkOption $option The object just created
* @param TitanFrameworkOption $option The object just created
* @return null
* @return null
* @since 1.1.1
* @since 1.1.1
*/
*/
public function verifyUniqueIDs( $option ) {
public function verifyUniqueIDs( $option ) {
if ( empty( $option->settings['id'] ) ) {
if ( empty( $option->settings['id'] ) ) {
return;
return;
}
}
if ( in_array( $option->settings['id'], $this->allOptionIDs ) ) {
if ( in_array( $option->settings['id'], $this->allOptionIDs ) ) {
self::displayFrameworkError(
self::displayFrameworkError(
sprintf( __( 'All option IDs must be unique. The id %s has been used multiple times.', TF_I18NDOMAIN ),
sprintf( __( 'All option IDs must be unique. The id %s has been used multiple times.', TF_I18NDOMAIN ),
'<code>' . $option->settings['id'] . '</code>'
'<code>' . $option->settings['id'] . '</code>'
)
)
);
);
} else {
} else {
$this->allOptionIDs[] = $option->settings['id'];
$this->allOptionIDs[] = $option->settings['id'];
}
}
}
}
public function rememberGoogleFonts( $option ) {
public function rememberGoogleFonts( $option ) {
if ( is_a( $option, 'TitanFrameworkOptionSelectGooglefont' ) ) {
if ( is_a( $option, 'TitanFrameworkOptionSelectGooglefont' ) ) {
if ( $option->settings['enqueue'] ) {
if ( $option->settings['enqueue'] ) {
$this->googleFontsOptions[] = $option;
$this->googleFontsOptions[] = $option;
}
}
}
}
}
}
/**
/**
* Action hook on tf_create_option to remember all the options, used to
* Action hook on tf_create_option to remember all the options, used to
* ensure that our serialized option does not get cluttered with unused
* ensure that our serialized option does not get cluttered with unused
* options
* options
*
*
* @access public
* @access public
* @param TitanFrameworkOption $option The option that was just created
* @param TitanFrameworkOption $option The option that was just created
* @return void
* @return void
* @since 1.2.1
* @since 1.2.1
*/
*/
public function rememberAllOptions( $option ) {
public function rememberAllOptions( $option ) {
if ( ! empty( $option->settings['id'] ) ) {
if ( ! empty( $option->settings['id'] ) ) {
$this->optionsUsed[ $option->settings['id'] ] = $option;
$this->optionsUsed[ $option->settings['id'] ] = $option;
}
}
}
}
public function loadFrontEndScripts() {
public function loadFrontEndScripts() {
foreach ( $this->googleFontsOptions as $googleFontOption ) {
foreach ( $this->googleFontsOptions as $googleFontOption ) {
$font = $this->getOption( $googleFontOption->settings['id'] );
$font = $this->getOption( $googleFontOption->settings['id'] );
if ( empty( $font ) ) {
if ( empty( $font ) ) {
continue;
continue;
}
}
wp_enqueue_style(
wp_enqueue_style(
'tf-google-webfont-' . strtolower( str_replace( ' ', '-', $font['name'] ) ),
'tf-google-webfont-' . strtolower( str_replace( ' ', '-', $font['name'] ) ),
TitanFrameworkOptionSelectGooglefont::formScript( $font )
TitanFrameworkOptionSelectGooglefont::formScript( $font )
);
);
}
}
}
}
public function loadAdminScripts() {
public function loadAdminScripts() {
wp_enqueue_media();
wp_enqueue_media();
wp_enqueue_script( 'tf-serialize', TitanFramework::getURL( 'js/serialize.js', __FILE__ ) );
wp_enqueue_script( 'tf-serialize', TitanFramework::getURL( 'js/serialize.js', __FILE__ ) );
wp_enqueue_script( 'tf-styling', TitanFramework::getURL( 'js/admin-styling.js', __FILE__ ) );
wp_enqueue_script( 'tf-styling', TitanFramework::getURL( 'js/admin-styling.js', __FILE__ ) );
wp_enqueue_style( 'tf-admin-styles', TitanFramework::getURL( 'css/admin-styles.css', __FILE__ ) );
wp_enqueue_style( 'tf-admin-styles', TitanFramework::getURL( 'css/admin-styles.css', __FILE__ ) );
wp_enqueue_style( 'tf-font-awesome', TitanFramework::getURL( 'css/font-awesome/css/font-awesome.min.css', __FILE__ ) );
wp_enqueue_style( 'tf-font-awesome', TitanFramework::getURL( 'css/font-awesome/css/font-awesome.min.css', __FILE__ ) );
}
}
public function getAllOptions() {
public function getAllOptions() {
if ( empty( $this->allOptions ) ) {
if ( empty( $this->allOptions ) ) {
$this->allOptions = array();
$this->allOptions = array();
}
}
if ( empty( $this->allOptions[$this->optionNamespace] ) ) {
if ( empty( $this->allOptions[$this->optionNamespace] ) ) {
$this->allOptions[$this->optionNamespace] = array();
$this->allOptions[$this->optionNamespace] = array();
} else {
} else {
return $this->allOptions[$this->optionNamespace];
return $this->allOptions[$this->optionNamespace];
}
}
// Check if we have options saved already
// Check if we have options saved already
$currentOptions = get_option( $this->optionNamespace . '_options' );
if ( ! $this->isNetwork ) {
$currentOptions = get_option( $this->optionNamespace . '_options' );
} else {
$currentOptions = get_site_option( $this->optionNamespace . '_options' );
}
// First time run, this action hook can be used to trigger something
// First time run, this action hook can be used to trigger something
if ( $currentOptions === false ) {
if ( $currentOptions === false ) {
do_action( 'tf_init_no_options_' . $this->optionNamespace );
do_action( 'tf_init_no_options_' . $this->optionNamespace );
}
}
// Put all the available options in our global variable for future checking
// Put all the available options in our global variable for future checking
if ( ! empty( $currentOptions ) && ! count( $this->allOptions[$this->optionNamespace] ) ) {
if ( ! empty( $currentOptions ) && ! count( $this->allOptions[$this->optionNamespace] ) ) {
$this->allOptions[$this->optionNamespace] = unserialize( $currentOptions );
$this->allOptions[$this->optionNamespace] = unserialize( $currentOptions );
}
}
return $this->allOptions[$this->optionNamespace];
return $this->allOptions[$this->optionNamespace];
}
}
public function saveOptions() {
public function saveOptions() {
update_option( $this->optionNamespace . '_options', serialize( $this->allOptions[$this->optionNamespace] ) );
if ( ! $this->isNetwork ) {
update_option( $this->optionNamespace . '_options', serialize( $this->allOptions[$this->optionNamespace] ) );
} else {
update_site_option( $this->optionNamespace . '_options', serialize( $this->allOptions[$this->optionNamespace] ) );
}
return $this->allOptions[$this->optionNamespace];
return $this->allOptions[$this->optionNamespace];
}
}
/*
/*
* Cleans up the meta options in the database for our namespace.
* Cleans up the meta options in the database for our namespace.
* Remove unused stuff and add in the default values for new stuff
* Remove unused stuff and add in the default values for new stuff
*/
*/
public function updateMetaDbListing() {
public function updateMetaDbListing() {
// TODO
// TODO
}
}
/*
/*
* Cleans up the theme mods in the database for our namespace.
* Cleans up the theme mods in the database for our namespace.
* Remove unused stuff and add in the default values for new stuff
* Remove unused stuff and add in the default values for new stuff
*/
*/
public function updateThemeModListing() {
public function updateThemeModListing() {
$allThemeMods = get_theme_mods();
$allThemeMods = get_theme_mods();
// For fresh installs there won't be any theme mods yet
// For fresh installs there won't be any theme mods yet
if ( $allThemeMods === false ) {
if ( $allThemeMods === false ) {
$allThemeMods = array();
$allThemeMods = array();
}
}
$allThemeModKeys = array_fill_keys( array_keys( $allThemeMods ), null );
$allThemeModKeys = array_fill_keys( array_keys( $allThemeMods ), null );
// Check existing theme mods
// Check existing theme mods
foreach ( $this->themeCustomizerSections as $section ) {
foreach ( $this->themeCustomizerSections as $section ) {
foreach ( $section->options as $option ) {
foreach ( $section->options as $option ) {
if ( ! isset( $allThemeMods[$option->getID()] ) ) {
if ( ! isset( $allThemeMods[$option->getID()] ) ) {
set_theme_mod( $option->getID(), $option->settings['default'] );
set_theme_mod( $option->getID(), $option->settings['default'] );
}
}
unset( $allThemeModKeys[$option->getID()] );
unset( $allThemeModKeys[$option->getID()] );
}
}
}
}
// Remove all unused theme mods
// Remove all unused theme mods
if ( count( $allThemeModKeys ) ) {
if ( count( $allThemeModKeys ) ) {
foreach ( $allThemeModKeys as $optionName => $dummy ) {
foreach ( $allThemeModKeys as $optionName => $dummy ) {
// Only remove theme mods that the framework created
// Only remove theme mods that the framework created
if ( stripos( $optionName, $this->optionNamespace . '_' ) === 0 ) {
if ( stripos( $optionName, $this->optionNamespace . '_' ) === 0 ) {
remove_theme_mod( $optionName );
remove_theme_mod( $optionName );
}
}
}
}
}
}
}
}
/*
/*
* Cleans up the options present in the database for our namespace.
* Cleans up the options present in the database for our namespace.
* Remove unused stuff and add in the default values for new stuff
* Remove unused stuff and add in the default values for new stuff
*/
*/
public function updateOptionDBListing() {
public function updateOptionDBListing() {
// Get also a list of all option keys
// Get also a list of all option keys
$allOptionKeys = array_fill_keys( array_keys( $this->allOptions[$this->optionNamespace] ), null );
$allOptionKeys = array_fill_keys( array_keys( $this->allOptions[$this->optionNamespace] ), null );
// Check whether options have changed / added
// Check whether options have changed / added
$changed = false;
$changed = false;
foreach ( $this->adminPanels as $panel ) {
foreach ( $this->adminPanels as $panel ) {
// Check existing options
// Check existing options
foreach ( $panel->options as $option ) {
foreach ( $panel->options as $option ) {
if ( empty( $option->settings['id'] ) ) {
if ( empty( $option->settings['id'] ) ) {
continue;
continue;
}
}
if ( ! isset( $this->allOptions[$this->optionNamespace][$option->settings['id']] ) ) {
if ( ! isset( $this->allOptions[$this->optionNamespace][$option->settings['id']] ) ) {
$this->allOptions[$this->optionNamespace][$option->settings['id']] = $option->settings['default'];
$this->allOptions[$this->optionNamespace][$option->settings['id']] = $option->settings['default'];
$changed = true;
$changed = true;
}
}
unset( $allOptionKeys[$option->settings['id']] );
unset( $allOptionKeys[$option->settings['id']] );
// Clean the value for retrieval
// Clean the value for retrieval
$this->allOptions[$this->optionNamespace][$option->settings['id']] =
$this->allOptions[$this->optionNamespace][$option->settings['id']] =
$option->cleanValueForGetting( $this->allOptions[$this->optionNamespace][$option->settings['id']] );
$option->cleanValueForGetting( $this->allOptions[$this->optionNamespace][$option->settings['id']] );
}
}
// Check existing options
// Check existing options
foreach ( $panel->tabs as $tab ) {
foreach ( $panel->tabs as $tab ) {
foreach ( $tab->options as $option ) {
foreach ( $tab->options as $option ) {
if ( empty( $option->settings['id'] ) ) {
if ( empty( $option->settings['id'] ) ) {
continue;
continue;
}
}
if ( ! isset( $this->allOptions[$this->optionNamespace][$option->settings['id']] ) ) {
if ( ! isset( $this->allOptions[$this->optionNamespace][$option->settings['id']] ) ) {
$this->allOptions[$this->optionNamespace][$option->settings['id']] = $option->settings['default'];
$this->allOptions[$this->optionNamespace][$option->settings['id']] = $option->settings['default'];
$changed = true;
$changed = true;
}
}
unset( $allOptionKeys[$option->settings['id']] );
unset( $allOptionKeys[$option->settings['id']] );
// Clean the value for retrieval
// Clean the value for retrieval
$this->allOptions[$this->optionNamespace][$option->settings['id']] =
$this->allOptions[$this->optionNamespace][$option->settings['id']] =
$option->cleanValueForGetting( $this->allOptions[$this->optionNamespace][$option->settings['id']] );
$option->cleanValueForGetting( $this->allOptions[$this->optionNamespace][$option->settings['id']] );
}
}
}
}
}
}
// Remove all unused keys
// Remove all unused keys
if ( count( $allOptionKeys ) ) {
if ( count( $allOptionKeys ) ) {
foreach ( $allOptionKeys as $optionName => $dummy ) {
foreach ( $allOptionKeys as $optionName => $dummy ) {
unset( $this->allOptions[$this->optionNamespace][$optionName] );
unset( $this->allOptions[$this->optionNamespace][$optionName] );
}
}
$changed = true;
$changed = true;
}
}
// New options have been added, save the default values
// New options have been added, save the default values
if ( $changed ) {
if ( $changed ) {
update_option( $this->optionNamespace . '_options', serialize( $this->allOptions[$this->optionNamespace] ) );
if ( ! $this->isNetwork ) {
update_option( $this->optionNamespace . '_options', serialize( $this->allOptions[$this->optionNamespace] ) );
} else {
update_site_option( $this->optionNamespace . '_options', serialize( $this->allOptions[$this->optionNamespace] ) );
}
}
}
}
}
public function createAdminPanel( $settings ) {
public function createAdminPanel( $settings ) {
$obj = new TitanFrameworkAdminPanel( $settings, $this );
$obj = new TitanFrameworkAdminPanel( $settings, $this );
$this->adminPanels[] = $obj;
$this->adminPanels[] = $obj;
return $obj;
return $obj;
}
}
public function createMetaBox( $settings ) {
public function createMetaBox( $settings ) {
$obj = new TitanFrameworkMetaBox( $settings, $this );
$obj = new TitanFrameworkMetaBox( $settings, $this );
$this->metaBoxes[] = $obj;
$this->metaBoxes[] = $obj;
return $obj;
return $obj;
}
}
public function createThemeCustomizerSection( $settings ) {
public function createThemeCustomizerSection( $settings ) {
$obj = new TitanFrameworkThemeCustomizerSection( $settings, $this );
$obj = new TitanFrameworkThemeCustomizerSection( $settings, $this );
$this->themeCustomizerSections[] = $obj;
$this->themeCustomizerSections[] = $obj;
return $obj;
return $obj;
}
}
/**
/**
* A function available ONLY to CHILD themes to stop the creation of options
* A function available ONLY to CHILD themes to stop the creation of options
* created by the PARENT theme.
* created by the PARENT theme.
*
*
* @access public
* @access public
* @param string $optionName The id of the option to remove / stop from being created
* @param string $optionName The id of the option to remove / stop from being created
* @return void
* @return void
* @since 1.2.1
* @since 1.2.1
*/
*/
public function removeOption( $optionName ) {
public function removeOption( $optionName ) {
$this->optionsToRemove[] = $optionName;
$this->optionsToRemove[] = $optionName;
}
}
/**
/**
* Hook to the tf_create_option_continue filter, to check whether or not to continue
* 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).
* adding an option (if the option id was used in $titan->removeOption).
*
*
* @access public
* @access public
* @param boolean $continueCreating If true, the option will be created
* @param boolean $continueCreating If true, the option will be created
* @param array $optionSettings The settings for the option to 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.
* @return boolean If true, continue with creating the option. False to stop it.
* @since 1.2.1
* @since 1.2.1
*/
*/
public function removeChildThemeOptions( $continueCreating, $optionSettings ) {
public function removeChildThemeOptions( $continueCreating, $optionSettings ) {
if ( ! count( $this->optionsToRemove ) ) {
if ( ! count( $this->optionsToRemove ) ) {
return $continueCreating;
return $continueCreating;
}
}
if ( empty( $optionSettings['id'] ) ) {
if ( empty( $optionSettings['id'] ) ) {
return $continueCreating;
return $continueCreating;
}
}
if ( in_array( $optionSettings['id'], $this->optionsToRemove ) ) {
if ( in_array( $optionSettings['id'], $this->optionsToRemove ) ) {
return false;
return false;
}
}
return $continueCreating;
return $continueCreating;
}
}
public function getOption( $optionName, $postID = null ) {
public function getOption( $optionName, $postID = null ) {
$value = null;
$value = null;
// Get the option value
// Get the option value
if ( array_key_exists( $optionName, $this->optionsUsed ) ) {
if ( array_key_exists( $optionName, $this->optionsUsed ) ) {
$option = $this->optionsUsed[ $optionName ];
$option = $this->optionsUsed[ $optionName ];
// Admin page options
// Admin page options
if ( $option->type == TitanFrameworkOption::TYPE_ADMIN ) {
if ( $option->type == TitanFrameworkOption::TYPE_ADMIN ) {
// this is blank if called too early. getOption should be called inside a hook or template
// this is blank if called too early. getOption should be called inside a hook or template
if ( ! is_array( $this->allOptions ) ) {
if ( ! is_array( $this->allOptions ) ) {
self::displayFrameworkError( sprintf( __( 'Wrong usage of %s, this should be called inside a hook or from within a theme file.', TF_I18NDOMAIN ), '<code>getOption</code>' ) );
self::displayFrameworkError( sprintf( __( 'Wrong usage of %s, this should be called inside a hook or from within a theme file.', TF_I18NDOMAIN ), '<code>getOption</code>' ) );
return null;
return null;
}
}
$value = $this->allOptions[ $this->optionNamespace ][ $optionName ];
$value = $this->allOptions[ $this->optionNamespace ][ $optionName ];
// Meta box options
// Meta box options
} else if ( $option->type == TitanFrameworkOption::TYPE_META ) {
} else if ( $option->type == TitanFrameworkOption::TYPE_META ) {
// If no $postID is given, try and get it if we are in a loop
// If no $postID is given, try and get it if we are in a loop
if ( empty( $postID ) && ! is_admin() ) {
if ( empty( $postID ) && ! is_admin() ) {
if ( get_post() != null ) {
if ( get_post() != null ) {
$postID = get_the_ID();
$postID = get_the_ID();
}
}
}
}
$value = get_post_meta( $postID, $this->optionNamespace . '_' . $optionName, true );
$value = get_post_meta( $postID, $this->optionNamespace . '_' . $optionName, true );
// Theme customizer options
// Theme customizer options
} else if ( $option->type == TitanFrameworkOption::TYPE_CUSTOMIZER ) {
} else if ( $option->type == TitanFrameworkOption::TYPE_CUSTOMIZER ) {
$value = get_theme_mod( $this->optionNamespace . '_' . $optionName );
$value = get_theme_mod( $this->optionNamespace . '_' . $optionName );
}
}
}
}
// Apply cleaning method for the value (for serialized data, slashes, etc)
// Apply cleaning method for the value (for serialized data, slashes, etc)
if ( $value !== null ) {
if ( $value !== null ) {
if ( ! empty( $this->optionsUsed[$optionName] ) ) {
if ( ! empty( $this->optionsUsed[$optionName] ) ) {
$value = $this->optionsUsed[$optionName]->cleanValueForGetting( $value );
$value = $this->optionsUsed[$optionName]->cleanValueForGetting( $value );
}
}
}
}
return $value;
return $value;
}
}
public function setOption( $optionName, $value, $postID = null ) {
public function setOption( $optionName, $value, $postID = null ) {
// Apply cleaning method for the value (for serialized data, slashes, etc)
// Apply cleaning method for the value (for serialized data, slashes, etc)
if ( ! empty( $this->optionsUsed[$optionName] ) ) {
if ( ! empty( $this->optionsUsed[$optionName] ) ) {
$value = $this->optionsUsed[$optionName]->cleanValueForSaving( $value );
$value = $this->optionsUsed[$optionName]->cleanValueForSaving( $value );
}
}
if ( empty( $postID ) ) {
if ( empty( $postID ) ) {
// option
// option
if ( ! is_array( $this->allOptions ) ) {
if ( ! is_array( $this->allOptions ) ) {
// this is blank if called too early. getOption should be called inside a hook or template
// this is blank if called too early. getOption should be called inside a hook or template
self::displayFrameworkError( sprintf( __( 'Wrong usage of %s, this should be called inside a hook or from within a theme file.', TF_I18NDOMAIN ), '<code>setOption</code>' ) );
self::displayFrameworkError( sprintf( __( 'Wrong usage of %s, this should be called inside a hook or from within a theme file.', TF_I18NDOMAIN ), '<code>setOption</code>' ) );
return '';
return '';
}
}
if ( array_key_exists( $optionName, $this->allOptions[$this->optionNamespace] ) ) {
if ( array_key_exists( $optionName, $this->allOptions[$this->optionNamespace] ) ) {
$this->allOptions[$this->optionNamespace][$optionName] = $value;
$this->allOptions[$this->optionNamespace][$optionName] = $value;
} else {
} else {
// customizer
// customizer
set_theme_mod( $this->optionNamespace . '_' . $optionName, $value );
set_theme_mod( $this->optionNamespace . '_' . $optionName, $value );
}
}
} else {
} else {
// meta
// meta
return update_post_meta( $postID, $this->optionNamespace . '_' . $optionName, $value );
return update_post_meta( $postID, $this->optionNamespace . '_' . $optionName, $value );
}
}
return $value;
return $value;
}
}
public function createWidgetArea( $settings ) {
public function createWidgetArea( $settings ) {
$obj = new TitanFrameworkWidgetArea( $settings, $this );
$obj = new TitanFrameworkWidgetArea( $settings, $this );
$this->widgetAreas[] = $obj;
$this->widgetAreas[] = $obj;
return $obj;
return $obj;
}
}
public function createCSS( $CSSString ) {
public function createCSS( $CSSString ) {
$this->cssInstance->addCSS( $CSSString );
$this->cssInstance->addCSS( $CSSString );
}
}
public function createShortcode( $settings ) {
public function createShortcode( $settings ) {
do_action( 'tf_create_shortcode', $settings );
do_action( 'tf_create_shortcode', $settings );
do_action( 'tf_create_shortcode_' . $this->optionNamespace, $settings );
do_action( 'tf_create_shortcode_' . $this->optionNamespace, $settings );
}
}
public static function displayFrameworkError( $message, $errorObject = null ) {
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
// 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 ) ) {
if ( is_array( $errorObject ) ) {
foreach ( $errorObject as $key => $val ) {
foreach ( $errorObject as $key => $val ) {
if ( $val === '' ) {
if ( $val === '' ) {
unset( $errorObject[$key] );
unset( $errorObject[$key] );
}
}
}
}
}
}
// Display an error message
// Display an error message
?>
?>
<div style='margin: 20px'><strong><?php echo TF_NAME ?> Error:</strong>
<div style='margin: 20px'><strong><?php echo TF_NAME ?> Error:</strong>
<?php echo $message ?>
<?php echo $message ?>
<?php
<?php
if ( ! empty( $errorObject ) ):
if ( ! empty( $errorObject ) ):
?>
?>
<pre><code style="display: inline-block; padding: 10px"><?php echo print_r( $errorObject, true ) ?></code></pre>
<pre><code style="display: inline-block; padding: 10px"><?php echo print_r( $errorObject, true ) ?></code></pre>
<?php
<?php
endif;
endif;
?>
?>
</div>
</div>
<?php
<?php
}
}
/**
/**
* Acts the same way as plugins_url( 'script', __FILE__ ) but returns then correct url
* Acts the same way as plugins_url( 'script', __FILE__ ) but returns then correct url
* when called from inside a theme.
* when called from inside a theme.
*
*
* @param string $script the script to get the url to, relative to $file
* @param string $script the script to get the url to, relative to $file
* @param string $file the current file, should be __FILE__
* @param string $file the current file, should be __FILE__
* @return string the url to $script
* @return string the url to $script
* @since 1.1.2
* @since 1.1.2
*/
*/
public static function getURL( $script, $file ) {
public static function getURL( $script, $file ) {
$parentTheme = trailingslashit( get_template_directory() );
$parentTheme = trailingslashit( get_template_directory() );
$childTheme = trailingslashit( get_stylesheet_directory() );
$childTheme = trailingslashit( get_stylesheet_directory() );
$plugin = trailingslashit( dirname( $file ) );
$plugin = trailingslashit( dirname( $file ) );
// framework is in a parent theme
// framework is in a parent theme
if ( stripos( $file, $parentTheme ) !== false ) {
if ( stripos( $file, $parentTheme ) !== false ) {
$dir = trailingslashit( dirname( str_replace( $parentTheme, '', $file ) ) );
$dir = trailingslashit( dirname( str_replace( $parentTheme, '', $file ) ) );
if ( $dir == './' ) {
if ( $dir == './' ) {
$dir = '';
$dir = '';
}
}
return trailingslashit( get_template_directory_uri() ) . $dir . $script;
return trailingslashit( get_template_directory_uri() ) . $dir . $script;
// framework is in a child theme
// framework is in a child theme
} else if ( stripos( $file, $childTheme ) !== false ) {
} else if ( stripos( $file, $childTheme ) !== false ) {
$dir = trailingslashit( dirname( str_replace( $childTheme, '', $file ) ) );
$dir = trailingslashit( dirname( str_replace( $childTheme, '', $file ) ) );
if ( $dir == './' ) {
if ( $dir == './' ) {
$dir = '';
$dir = '';
}
}
return trailingslashit( get_stylesheet_directory_uri() ) . $dir . $script;
return trailingslashit( get_stylesheet_directory_uri() ) . $dir . $script;
}
}
// framework is a or in a plugin
// framework is a or in a plugin
return plugins_url( $script, $file );
return plugins_url( $script, $file );
}
}
}
}
?>
?>