* @copyright Copyright (c) 2013, Derek Herman * @since 2.0 */ /** * Registers the Theme Option page * * @uses ot_register_settings() * * @return void * * @access public * @since 2.1 */ if ( ! function_exists( 'ot_register_theme_options_page' ) ) { function ot_register_theme_options_page() { /* get the settings array */ $get_settings = get_option( ot_settings_id() ); /* sections array */ $sections = isset( $get_settings['sections'] ) ? $get_settings['sections'] : array(); /* settings array */ $settings = isset( $get_settings['settings'] ) ? $get_settings['settings'] : array(); /* contexual_help array */ $contextual_help = isset( $get_settings['contextual_help'] ) ? $get_settings['contextual_help'] : array(); /* build the Theme Options */ if ( function_exists( 'ot_register_settings' ) && OT_USE_THEME_OPTIONS ) { ot_register_settings( array( array( 'id' => ot_options_id(), 'pages' => array( array( 'id' => 'ot_theme_options', 'parent_slug' => apply_filters( 'ot_theme_options_parent_slug', 'themes.php' ), 'page_title' => apply_filters( 'ot_theme_options_page_title', __( 'Theme Options', 'option-tree' ) ), 'menu_title' => apply_filters( 'ot_theme_options_menu_title', __( 'Theme Options', 'option-tree' ) ), 'capability' => $caps = apply_filters( 'ot_theme_options_capability', 'edit_theme_options' ), 'menu_slug' => apply_filters( 'ot_theme_options_menu_slug', 'ot-theme-options' ), 'icon_url' => apply_filters( 'ot_theme_options_icon_url', null ), 'position' => apply_filters( 'ot_theme_options_position', null ), 'updated_message' => apply_filters( 'ot_theme_options_updated_message', __( 'Theme Options updated.', 'option-tree' ) ), 'reset_message' => apply_filters( 'ot_theme_options_reset_message', __( 'Theme Options reset.', 'option-tree' ) ), 'button_text' => apply_filters( 'ot_theme_options_button_text', __( 'Save Changes', 'option-tree' ) ), 'contextual_help' => apply_filters( 'ot_theme_options_contextual_help', $contextual_help ), 'sections' => apply_filters( 'ot_theme_options_sections', $sections ), 'settings' => apply_filters( 'ot_theme_options_settings', $settings ) ) ) ) ) ); // Filters the options.php to add the minimum user capabilities. add_filter( 'option_page_capability_' . ot_options_id(), create_function( '$caps', "return '$caps';" ), 999 ); } } } /** * Registers the Settings page * * @uses ot_register_settings() * * @return void * * @access public * @since 2.1 */ if ( ! function_exists( 'ot_register_settings_page' ) ) { function ot_register_settings_page() { global $ot_has_custom_theme_options; // Display UI Builder admin notice if ( OT_SHOW_OPTIONS_UI == true && isset( $_REQUEST['page'] ) && $_REQUEST['page'] == 'ot-settings' && ( $ot_has_custom_theme_options == true || has_action( 'admin_init', 'custom_theme_options' ) || has_action( 'init', 'custom_theme_options' ) ) ) { function ot_has_custom_theme_options() { echo '
' . __( 'The Theme Options UI Builder is being overridden by a custom file in your theme. Any changes you make via the UI Builder will not be saved.', 'option-tree' ) . '
width', '' . $field_id . '' ), 'error' );
}
// Validate color
if ( $key == 'color' && ! empty( $value ) ) {
$input[$key] = ot_validate_setting( $value, 'colorpicker', $field_id );
}
// Unset keys with empty values.
if ( empty( $value ) ) {
unset( $input[$key] );
}
}
if ( empty( $input ) ) {
$input = '';
}
} else if ( 'box-shadow' == $type ) {
// Validate inset
$input['inset'] = isset( $input['inset'] ) ? 'inset' : '';
// Validate offset-x
$input['offset-x'] = ot_validate_setting( $input['offset-x'], 'text', $field_id );
// Validate offset-y
$input['offset-y'] = ot_validate_setting( $input['offset-y'], 'text', $field_id );
// Validate blur-radius
$input['blur-radius'] = ot_validate_setting( $input['blur-radius'], 'text', $field_id );
// Validate spread-radius
$input['spread-radius'] = ot_validate_setting( $input['spread-radius'], 'text', $field_id );
// Validate color
$input['color'] = ot_validate_setting( $input['color'], 'colorpicker', $field_id );
// Unset keys with empty values.
foreach( $input as $key => $value ) {
if ( empty( $value ) ) {
unset( $input[$key] );
}
}
// Set empty array to empty string.
if ( empty( $input ) ) {
$input = '';
}
} else if ( 'colorpicker' == $type ) {
/* return empty & set error */
if ( 0 === preg_match( '/^#([a-f0-9]{6}|[a-f0-9]{3})$/i', $input ) && 0 === preg_match( '/^rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9\.]{1,4})\s*\)/i', $input ) ) {
$input = '';
add_settings_error( 'option-tree', 'invalid_hex', sprintf( __( 'The %s Colorpicker only allows valid hexadecimal or rgba values.', 'option-tree' ), '' . $field_id . '' ), 'error' );
}
} else if ( 'colorpicker-opacity' == $type ) {
// Not allowed
if ( is_array( $input ) ) {
$input = '';
}
// Validate color
$input = ot_validate_setting( $input, 'colorpicker', $field_id );
} else if ( in_array( $type, array( 'css', 'javascript', 'text', 'textarea', 'textarea-simple' ) ) ) {
if ( ! current_user_can( 'unfiltered_html' ) && OT_ALLOW_UNFILTERED_HTML == false ) {
$input = wp_kses_post( $input );
}
} else if ( 'dimension' == $type ) {
// Loop over array and set error keys or unset key from array.
foreach( $input as $key => $value ) {
if ( ! empty( $value ) && ! is_numeric( $value ) && $key !== 'unit' ) {
$errors[] = $key;
}
if ( empty( $value ) ) {
unset( $input[$key] );
}
}
/* return 0 & set error */
if ( isset( $errors ) ) {
foreach( $errors as $error ) {
$input[$error] = '0';
add_settings_error( 'option-tree', 'invalid_dimension_' . $error, sprintf( __( 'The %s input field for %s only allows numeric values.', 'option-tree' ), '' . $error . '', '' . $field_id . '' ), 'error' );
}
}
if ( empty( $input ) ) {
$input = '';
}
} else if ( 'google-fonts' == $type ) {
unset($input['%key%']);
// Loop over array and check for values
if ( is_array( $input ) && ! empty( $input ) ) {
$input = array_values( $input );
}
// No value; set to empty
if ( empty( $input ) ) {
$input = '';
}
} else if ( 'link-color' == $type ) {
// Loop over array and check for values
if ( is_array( $input ) && ! empty( $input ) ) {
foreach( $input as $key => $value ) {
if ( ! empty( $value ) ) {
$input[$key] = ot_validate_setting( $input[$key], 'colorpicker', $field_id . '-' . $key );
$has_value = true;
}
}
}
// No value; set to empty
if ( ! isset( $has_value ) ) {
$input = '';
}
} else if ( 'measurement' == $type ) {
$input[0] = sanitize_text_field( $input[0] );
// No value; set to empty
if ( empty( $input[0] ) && empty( $input[1] ) ) {
$input = '';
}
} else if ( 'spacing' == $type ) {
// Loop over array and set error keys or unset key from array.
foreach( $input as $key => $value ) {
if ( ! empty( $value ) && ! is_numeric( $value ) && $key !== 'unit' ) {
$errors[] = $key;
}
if ( empty( $value ) ) {
unset( $input[$key] );
}
}
/* return 0 & set error */
if ( isset( $errors ) ) {
foreach( $errors as $error ) {
$input[$error] = '0';
add_settings_error( 'option-tree', 'invalid_spacing_' . $error, sprintf( __( 'The %s input field for %s only allows numeric values.', 'option-tree' ), '' . $error . '', '' . $field_id . '' ), 'error' );
}
}
if ( empty( $input ) ) {
$input = '';
}
} else if ( 'typography' == $type && isset( $input['font-color'] ) ) {
$input['font-color'] = ot_validate_setting( $input['font-color'], 'colorpicker', $field_id );
// Loop over array and check for values
foreach( $input as $key => $value ) {
if ( ! empty( $value ) ) {
$has_value = true;
}
}
// No value; set to empty
if ( ! isset( $has_value ) ) {
$input = '';
}
} else if ( 'upload' == $type ) {
if( filter_var( $input, FILTER_VALIDATE_INT ) === FALSE ) {
$input = esc_url_raw( $input );
}
} else if ( 'gallery' == $type ) {
$input = trim( $input );
} else if ( 'social-links' == $type ) {
// Loop over array and check for values, plus sanitize the text field
foreach( (array) $input as $key => $value ) {
if ( ! empty( $value ) && is_array( $value ) ) {
foreach( (array) $value as $item_key => $item_value ) {
if ( ! empty( $item_value ) ) {
$has_value = true;
$input[$key][$item_key] = sanitize_text_field( $item_value );
}
}
}
}
// No value; set to empty
if ( ! isset( $has_value ) ) {
$input = '';
}
}
$input = apply_filters( 'ot_after_validate_setting', $input, $type, $field_id );
return $input;
}
}
/**
* Setup the default admin styles
*
* @return void
*
* @access public
* @since 2.0
*/
if ( ! function_exists( 'ot_admin_styles' ) ) {
function ot_admin_styles() {
global $wp_styles, $post;
/* execute styles before actions */
do_action( 'ot_admin_styles_before' );
/* load WP colorpicker */
wp_enqueue_style( 'wp-color-picker' );
/* load admin styles */
wp_enqueue_style( 'ot-admin-css', OT_URL . 'assets/css/ot-admin.css', false, OT_VERSION );
/* load the RTL stylesheet */
$wp_styles->add_data( 'ot-admin-css','rtl', true );
/* Remove styles added by the Easy Digital Downloads plugin */
if ( isset( $post->post_type ) && $post->post_type == 'post' )
wp_dequeue_style( 'jquery-ui-css' );
/**
* Filter the screen IDs used to dequeue `jquery-ui-css`.
*
* @since 2.5.0
*
* @param array $screen_ids An array of screen IDs.
*/
$screen_ids = apply_filters( 'ot_dequeue_jquery_ui_css_screen_ids', array(
'toplevel_page_ot-settings',
'optiontree_page_ot-documentation',
'appearance_page_ot-theme-options'
) );
/* Remove styles added by the WP Review plugin and any custom pages added through filtering */
if ( in_array( get_current_screen()->id, $screen_ids ) ) {
wp_dequeue_style( 'plugin_name-admin-ui-css' );
wp_dequeue_style( 'jquery-ui-css' );
}
/* execute styles after actions */
do_action( 'ot_admin_styles_after' );
}
}
/**
* Setup the default admin scripts
*
* @uses add_thickbox() Include Thickbox for file uploads
* @uses wp_enqueue_script() Add OptionTree scripts
* @uses wp_localize_script() Used to include arbitrary Javascript data
*
* @return void
*
* @access public
* @since 2.0
*/
if ( ! function_exists( 'ot_admin_scripts' ) ) {
function ot_admin_scripts() {
/* execute scripts before actions */
do_action( 'ot_admin_scripts_before' );
if ( function_exists( 'wp_enqueue_media' ) ) {
/* WP 3.5 Media Uploader */
wp_enqueue_media();
} else {
/* Legacy Thickbox */
add_thickbox();
}
/* load jQuery-ui slider */
wp_enqueue_script( 'jquery-ui-slider' );
/* load jQuery-ui datepicker */
wp_enqueue_script( 'jquery-ui-datepicker' );
/* load WP colorpicker */
wp_enqueue_script( 'wp-color-picker' );
/* Load Ace Editor for CSS Editing */
wp_enqueue_script( 'ace-editor', 'https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js', null, '1.1.3' );
/* load jQuery UI timepicker addon */
wp_enqueue_script( 'jquery-ui-timepicker', OT_URL . 'assets/js/vendor/jquery/jquery-ui-timepicker.js', array( 'jquery', 'jquery-ui-slider', 'jquery-ui-datepicker' ), '1.4.3' );
/* load all the required scripts */
wp_enqueue_script( 'ot-admin-js', OT_URL . 'assets/js/ot-admin.js', array( 'jquery', 'jquery-ui-tabs', 'jquery-ui-sortable', 'jquery-ui-slider', 'wp-color-picker', 'ace-editor', 'jquery-ui-datepicker', 'jquery-ui-timepicker' ), OT_VERSION );
/* create localized JS array */
$localized_array = array(
'ajax' => admin_url( 'admin-ajax.php' ),
'upload_text' => apply_filters( 'ot_upload_text', __( 'Send to OptionTree', 'option-tree' ) ),
'remove_media_text' => __( 'Remove Media', 'option-tree' ),
'reset_agree' => __( 'Are you sure you want to reset back to the defaults?', 'option-tree' ),
'remove_no' => __( 'You can\'t remove this! But you can edit the values.', 'option-tree' ),
'remove_agree' => __( 'Are you sure you want to remove this?', 'option-tree' ),
'activate_layout_agree' => __( 'Are you sure you want to activate this layout?', 'option-tree' ),
'setting_limit' => __( 'Sorry, you can\'t have settings three levels deep.', 'option-tree' ),
'delete' => __( 'Delete Gallery', 'option-tree' ),
'edit' => __( 'Edit Gallery', 'option-tree' ),
'create' => __( 'Create Gallery', 'option-tree' ),
'confirm' => __( 'Are you sure you want to delete this Gallery?', 'option-tree' ),
'date_current' => __( 'Today', 'option-tree' ),
'date_time_current' => __( 'Now', 'option-tree' ),
'date_close' => __( 'Close', 'option-tree' ),
'replace' => __( 'Featured Image', 'option-tree' ),
'with' => __( 'Image', 'option-tree' )
);
/* localized script attached to 'option_tree' */
wp_localize_script( 'ot-admin-js', 'option_tree', $localized_array );
/* execute scripts after actions */
do_action( 'ot_admin_scripts_after' );
}
}
/**
* Returns the ID of a custom post type by post_title.
*
* @uses get_results()
*
* @return int
*
* @access public
* @since 2.0
*/
if ( ! function_exists( 'ot_get_media_post_ID' ) ) {
function ot_get_media_post_ID() {
// Option ID
$option_id = 'ot_media_post_ID';
// Get the media post ID
$post_ID = get_option( $option_id, false );
// Add $post_ID to the DB
if ( $post_ID === false || empty( $post_ID ) ) {
global $wpdb;
// Get the media post ID
$post_ID = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE `post_title` = 'Media' AND `post_type` = 'option-tree' AND `post_status` = 'private'" );
// Add to the DB
if ( $post_ID !== null )
update_option( $option_id, $post_ID );
}
return $post_ID;
}
}
/**
* Register custom post type & create the media post used to attach images.
*
* @uses get_results()
*
* @return void
*
* @access public
* @since 2.0
*/
if ( ! function_exists( 'ot_create_media_post' ) ) {
function ot_create_media_post() {
$regsiter_post_type = 'register_' . 'post_type';
$regsiter_post_type( 'option-tree', array(
'labels' => array( 'name' => __( 'Option Tree', 'option-tree' ) ),
'public' => false,
'show_ui' => false,
'capability_type' => 'post',
'exclude_from_search' => true,
'hierarchical' => false,
'rewrite' => false,
'supports' => array( 'title', 'editor' ),
'can_export' => false,
'show_in_nav_menus' => false
) );
/* look for custom page */
$post_id = ot_get_media_post_ID();
/* no post exists */
if ( $post_id == 0 ) {
/* create post object */
$_p = array();
$_p['post_title'] = 'Media';
$_p['post_name'] = 'media';
$_p['post_status'] = 'private';
$_p['post_type'] = 'option-tree';
$_p['comment_status'] = 'closed';
$_p['ping_status'] = 'closed';
/* insert the post into the database */
wp_insert_post( $_p );
}
}
}
/**
* Setup default settings array.
*
* @return void
*
* @access public
* @since 2.0
*/
if ( ! function_exists( 'ot_default_settings' ) ) {
function ot_default_settings() {
global $wpdb, $table_prefix;
if ( ! get_option( ot_settings_id() ) ) {
$section_count = 0;
$settings_count = 0;
$settings = array();
if ( count( $wpdb->get_results( "SHOW TABLES LIKE '{$table_prefix}option_tree'" ) ) == 1 && $old_settings = $wpdb->get_results( "SELECT * FROM {$table_prefix}option_tree ORDER BY item_sort ASC" ) ) {
foreach ( $old_settings as $setting ) {
/* heading is a section now */
if ( $setting->item_type == 'heading' ) {
/* add section to the sections array */
$settings['sections'][$section_count]['id'] = $setting->item_id;
$settings['sections'][$section_count]['title'] = $setting->item_title;
/* save the last section id to use in creating settings */
$section = $setting->item_id;
/* increment the section count */
$section_count++;
} else {
/* add setting to the settings array */
$settings['settings'][$settings_count]['id'] = $setting->item_id;
$settings['settings'][$settings_count]['label'] = $setting->item_title;
$settings['settings'][$settings_count]['desc'] = $setting->item_desc;
$settings['settings'][$settings_count]['section'] = $section;
$settings['settings'][$settings_count]['type'] = ot_map_old_option_types( $setting->item_type );
$settings['settings'][$settings_count]['std'] = '';
$settings['settings'][$settings_count]['class'] = '';
/* textarea rows */
$rows = '';
if ( in_array( $settings['settings'][$settings_count]['type'], array( 'css', 'javascript', 'textarea' ) ) ) {
if ( (int) $setting->item_options > 0 ) {
$rows = (int) $setting->item_options;
} else {
$rows = 15;
}
}
$settings['settings'][$settings_count]['rows'] = $rows;
/* post type */
$post_type = '';
if ( in_array( $settings['settings'][$settings_count]['type'], array( 'custom-post-type-select', 'custom-post-type-checkbox' ) ) ) {
if ( '' != $setting->item_options ) {
$post_type = $setting->item_options;
} else {
$post_type = 'post';
}
}
$settings['settings'][$settings_count]['post_type'] = $post_type;
/* choices */
$choices = array();
if ( in_array( $settings['settings'][$settings_count]['type'], array( 'checkbox', 'radio', 'select' ) ) ) {
if ( '' != $setting->item_options ) {
$choices = ot_convert_string_to_array( $setting->item_options );
}
}
$settings['settings'][$settings_count]['choices'] = $choices;
$settings_count++;
}
}
/* make sure each setting has a section just incase */
if ( isset( $settings['sections'] ) && isset( $settings['settings'] ) ) {
foreach( $settings['settings'] as $k => $setting ) {
if ( '' == $setting['section'] ) {
$settings['settings'][$k]['section'] = $settings['sections'][0]['id'];
}
}
}
}
/* if array if not properly formed create fallback settings array */
if ( ! isset( $settings['sections'] ) || ! isset( $settings['settings'] ) ) {
$settings = array(
'sections' => array(
array(
'id' => 'general',
'title' => __( 'General', 'option-tree' )
)
),
'settings' => array(
array(
'id' => 'sample_text',
'label' => __( 'Sample Text Field Label', 'option-tree' ),
'desc' => __( 'Description for the sample text field.', 'option-tree' ),
'section' => 'general',
'type' => 'text',
'std' => '',
'class' => '',
'rows' => '',
'post_type' => '',
'choices' => array()
)
)
);
}
/* update the settings array */
update_option( ot_settings_id(), $settings );
/* get option tree array */
$options = get_option( ot_options_id() );
/* validate options */
if ( is_array( $options ) ) {
foreach( $settings['settings'] as $setting ) {
if ( isset( $options[$setting['id']] ) ) {
$content = ot_stripslashes( $options[$setting['id']] );
$options[$setting['id']] = ot_validate_setting( $content, $setting['type'], $setting['id'] );
}
}
/* execute the action hook and pass the theme options to it */
do_action( 'ot_before_theme_options_save', $options );
/* update the option tree array */
update_option( ot_options_id(), $options );
}
}
}
}
/**
* Helper function to update the CSS option type after save.
*
* @return void
*
* @access public
* @since 2.0
*/
if ( ! function_exists( 'ot_save_css' ) ) {
function ot_save_css( $options ) {
/* grab a copy of the settings */
$settings = get_option( ot_settings_id() );
/* has settings */
if ( isset( $settings['settings'] ) ) {
/* loop through sections and insert CSS when needed */
foreach( $settings['settings'] as $k => $setting ) {
/* is the CSS option type */
if ( isset( $setting['type'] ) && 'css' == $setting['type'] ) {
/* insert CSS into dynamic.css */
if ( isset( $options[$setting['id']] ) && '' !== $options[$setting['id']] ) {
ot_insert_css_with_markers( $setting['id'], $options[$setting['id']] );
/* remove old CSS from dynamic.css */
} else {
ot_remove_old_css( $setting['id'] );
}
}
}
}
}
}
/**
* Helper function to load filters for XML mime type.
*
* @return void
*
* @access public
* @since 2.0
*/
if ( ! function_exists( 'ot_add_xml_to_upload_filetypes' ) ) {
function ot_add_xml_to_upload_filetypes() {
add_filter( 'upload_mimes', 'ot_upload_mimes' );
add_filter( 'wp_mime_type_icon', 'ot_xml_mime_type_icon', 10, 2 );
}
}
/**
* Filter 'upload_mimes' and add xml.
*
* @param array $mimes An array of valid upload mime types
* @return array
*
* @access public
* @since 2.0
*/
if ( ! function_exists( 'ot_upload_mimes' ) ) {
function ot_upload_mimes( $mimes ) {
$mimes['xml'] = 'application/xml';
return $mimes;
}
}
/**
* Filters 'wp_mime_type_icon' and have xml display as a document.
*
* @param string $icon The mime icon
* @param string $mime The mime type
* @return string
*
* @access public
* @since 2.0
*/
if ( ! function_exists( 'ot_xml_mime_type_icon' ) ) {
function ot_xml_mime_type_icon( $icon, $mime ) {
if ( $mime == 'application/xml' || $mime == 'text/xml' )
return wp_mime_type_icon( 'document' );
return $icon;
}
}
/**
* Import before the screen is displayed.
*
* @return void
*
* @access public
* @since 2.0
*/
if ( ! function_exists( 'ot_import' ) ) {
function ot_import() {
/* check and verify import xml nonce */
if ( isset( $_POST['import_xml_nonce'] ) && wp_verify_nonce( $_POST['import_xml_nonce'], 'import_xml_form' ) ) {
/* import input value */
$file = isset( $_POST['import_xml'] ) ? esc_url( $_POST['import_xml'] ) : '';
/* validate xml file */
if ( preg_match( "/(.xml)$/i", $file ) && class_exists( 'SimpleXMLElement' ) ) {
$settings = ot_import_xml( $file );
}
/* default message */
$message = 'failed';
/* cleanup, save, & show success message */
if ( isset( $settings ) && ! empty( $settings ) ) {
/* delete file */
if ( $file ) {
global $wpdb;
$attachmentid = $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} WHERE guid='$file'" );
wp_delete_attachment( $attachmentid, true );
}
/* update settings */
update_option( ot_settings_id(), $settings );
/* set message */
$message = 'success';
}
/* redirect */
wp_redirect( add_query_arg( array( 'action' => 'import-xml', 'message' => $message ), $_POST['_wp_http_referer'] ) );
exit;
}
/* check and verify import settings nonce */
if ( isset( $_POST['import_settings_nonce'] ) && wp_verify_nonce( $_POST['import_settings_nonce'], 'import_settings_form' ) ) {
/* textarea value */
$textarea = isset( $_POST['import_settings'] ) ? unserialize( ot_decode( $_POST['import_settings'] ) ) : '';
/* default message */
$message = 'failed';
/* is array: save & show success message */
if ( is_array( $textarea ) ) {
update_option( ot_settings_id(), $textarea );
$message = 'success';
}
/* redirect */
wp_redirect( add_query_arg( array( 'action' => 'import-settings', 'message' => $message ), $_POST['_wp_http_referer'] ) );
exit;
}
/* check and verify import theme options data nonce */
if ( isset( $_POST['import_data_nonce'] ) && wp_verify_nonce( $_POST['import_data_nonce'], 'import_data_form' ) ) {
/* default message */
$message = 'failed';
/* textarea value */
$options = isset( $_POST['import_data'] ) ? unserialize( ot_decode( $_POST['import_data'] ) ) : '';
/* get settings array */
$settings = get_option( ot_settings_id() );
/* has options */
if ( is_array( $options ) ) {
/* validate options */
if ( is_array( $settings ) ) {
foreach( $settings['settings'] as $setting ) {
if ( isset( $options[$setting['id']] ) ) {
$content = ot_stripslashes( $options[$setting['id']] );
$options[$setting['id']] = ot_validate_setting( $content, $setting['type'], $setting['id'] );
}
}
}
/* execute the action hook and pass the theme options to it */
do_action( 'ot_before_theme_options_save', $options );
/* update the option tree array */
update_option( ot_options_id(), $options );
$message = 'success';
}
/* redirect accordingly */
wp_redirect( add_query_arg( array( 'action' => 'import-data', 'message' => $message ), $_POST['_wp_http_referer'] ) );
exit;
}
/* check and verify import layouts nonce */
if ( isset( $_POST['import_layouts_nonce'] ) && wp_verify_nonce( $_POST['import_layouts_nonce'], 'import_layouts_form' ) ) {
/* default message */
$message = 'failed';
/* textarea value */
$layouts = isset( $_POST['import_layouts'] ) ? unserialize( ot_decode( $_POST['import_layouts'] ) ) : '';
/* get settings array */
$settings = get_option( ot_settings_id() );
/* has layouts */
if ( is_array( $layouts ) ) {
/* validate options */
if ( is_array( $settings ) ) {
foreach( $layouts as $key => $value ) {
if ( $key == 'active_layout' )
continue;
$options = unserialize( ot_decode( $value ) );
foreach( $settings['settings'] as $setting ) {
if ( isset( $options[$setting['id']] ) ) {
$content = ot_stripslashes( $options[$setting['id']] );
$options[$setting['id']] = ot_validate_setting( $content, $setting['type'], $setting['id'] );
}
}
$layouts[$key] = ot_encode( serialize( $options ) );
}
}
/* update the option tree array */
if ( isset( $layouts['active_layout'] ) ) {
$new_options = unserialize( ot_decode( $layouts[$layouts['active_layout']] ) );
/* execute the action hook and pass the theme options to it */
do_action( 'ot_before_theme_options_save', $new_options );
update_option( ot_options_id(), $new_options );
}
/* update the option tree layouts array */
update_option( ot_layouts_id(), $layouts );
$message = 'success';
}
/* redirect accordingly */
wp_redirect( add_query_arg( array( 'action' => 'import-layouts', 'message' => $message ), $_POST['_wp_http_referer'] ) );
exit;
}
return false;
}
}
/**
* Export before the screen is displayed.
*
* @return void
*
* @access public
* @since 2.0.8
*/
if ( ! function_exists( 'ot_export' ) ) {
function ot_export() {
/* check and verify export settings file nonce */
if ( isset( $_POST['export_settings_file_nonce'] ) && wp_verify_nonce( $_POST['export_settings_file_nonce'], 'export_settings_file_form' ) ) {
ot_export_php_settings_array();
}
}
}
/**
* Reusable XMl import helper function.
*
* @param string $file The path to the file.
* @return mixed False or an array of settings.
*
* @access public
* @since 2.0.8
*/
if ( ! function_exists( 'ot_import_xml' ) ) {
function ot_import_xml( $file ) {
$get_data = wp_remote_get( $file );
if ( is_wp_error( $get_data ) )
return false;
$rawdata = isset( $get_data['body'] ) ? $get_data['body'] : false;
if ( $rawdata ) {
$section_count = 0;
$settings_count = 0;
$section = '';
$settings = array();
$xml = new SimpleXMLElement( $rawdata );
foreach ( $xml->row as $value ) {
/* heading is a section now */
if ( $value->item_type == 'heading' ) {
/* add section to the sections array */
$settings['sections'][$section_count]['id'] = (string) $value->item_id;
$settings['sections'][$section_count]['title'] = (string) $value->item_title;
/* save the last section id to use in creating settings */
$section = (string) $value->item_id;
/* increment the section count */
$section_count++;
} else {
/* add setting to the settings array */
$settings['settings'][$settings_count]['id'] = (string) $value->item_id;
$settings['settings'][$settings_count]['label'] = (string) $value->item_title;
$settings['settings'][$settings_count]['desc'] = (string) $value->item_desc;
$settings['settings'][$settings_count]['section'] = $section;
$settings['settings'][$settings_count]['type'] = ot_map_old_option_types( (string) $value->item_type );
$settings['settings'][$settings_count]['std'] = '';
$settings['settings'][$settings_count]['class'] = '';
/* textarea rows */
$rows = '';
if ( in_array( $settings['settings'][$settings_count]['type'], array( 'css', 'javascript', 'textarea' ) ) ) {
if ( (int) $value->item_options > 0 ) {
$rows = (int) $value->item_options;
} else {
$rows = 15;
}
}
$settings['settings'][$settings_count]['rows'] = $rows;
/* post type */
$post_type = '';
if ( in_array( $settings['settings'][$settings_count]['type'], array( 'custom-post-type-select', 'custom-post-type-checkbox' ) ) ) {
if ( '' != (string) $value->item_options ) {
$post_type = (string) $value->item_options;
} else {
$post_type = 'post';
}
}
$settings['settings'][$settings_count]['post_type'] = $post_type;
/* choices */
$choices = array();
if ( in_array( $settings['settings'][$settings_count]['type'], array( 'checkbox', 'radio', 'select' ) ) ) {
if ( '' != (string) $value->item_options ) {
$choices = ot_convert_string_to_array( (string) $value->item_options );
}
}
$settings['settings'][$settings_count]['choices'] = $choices;
$settings_count++;
}
}
/* make sure each setting has a section just incase */
if ( isset( $settings['sections'] ) && isset( $settings['settings'] ) ) {
foreach( $settings['settings'] as $k => $setting ) {
if ( '' == $setting['section'] ) {
$settings['settings'][$k]['section'] = $settings['sections'][0]['id'];
}
}
}
return $settings;
}
return false;
}
}
/**
* Export the Theme Mode theme-options.php
*
* @return attachment
*
* @access public
* @since 2.0.8
*/
if ( ! function_exists( 'ot_export_php_settings_array' ) ) {
function ot_export_php_settings_array() {
$content = '';
$build_settings = '';
$contextual_help = '';
$sections = '';
$settings = '';
$option_tree_settings = get_option( ot_settings_id(), array() );
// Domain string helper
function ot_I18n_string( $string ) {
if ( ! empty( $string ) && isset( $_POST['domain'] ) && ! empty( $_POST['domain'] ) ) {
$domain = str_replace( ' ', '-', trim( $_POST['domain'] ) );
return "__( '$string', '$domain' )";
}
return "'$string'";
}
header( "Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header( "Pragma: no-cache ");
header( "Content-Description: File Transfer" );
header( 'Content-Disposition: attachment; filename="theme-options.php"');
header( "Content-Type: application/octet-stream");
header( "Content-Transfer-Encoding: binary" );
/* build contextual help content */
if ( isset( $option_tree_settings['contextual_help']['content'] ) ) {
$help = '';
foreach( $option_tree_settings['contextual_help']['content'] as $value ) {
$_id = isset( $value['id'] ) ? $value['id'] : '';
$_title = ot_I18n_string( isset( $value['title'] ) ? str_replace( "'", "\'", $value['title'] ) : '' );
$_content = ot_I18n_string( isset( $value['content'] ) ? html_entity_decode( str_replace( "'", "\'", $value['content'] ) ) : '' );
$help.= "
array(
'id' => '$_id',
'title' => $_title,
'content' => $_content
),";
}
$help = substr_replace( $help, '' , -1 );
$contextual_help = "
'content' => array( $help
),";
}
/* build contextual help sidebar */
if ( isset( $option_tree_settings['contextual_help']['sidebar'] ) ) {
$contextual_help.= "
'sidebar' => " . ot_I18n_string( html_entity_decode( str_replace( "'", "\'", $option_tree_settings['contextual_help']['sidebar'] ) ) );
}
/* check that $contexual_help has a value and add to $build_settings */
if ( '' != $contextual_help ) {
$build_settings.= "
'contextual_help' => array( $contextual_help
),";
}
/* build sections */
if ( isset( $option_tree_settings['sections'] ) ) {
foreach( $option_tree_settings['sections'] as $value ) {
$_id = isset( $value['id'] ) ? $value['id'] : '';
$_title = ot_I18n_string( isset( $value['title'] ) ? str_replace( "'", "\'", $value['title'] ) : '' );
$sections.= "
array(
'id' => '$_id',
'title' => $_title
),";
}
$sections = substr_replace( $sections, '' , -1 );
}
/* check that $sections has a value and add to $build_settings */
if ( '' != $sections ) {
$build_settings.= "
'sections' => array( $sections
)";
}
/* build settings */
if ( isset( $option_tree_settings['settings'] ) ) {
foreach( $option_tree_settings['settings'] as $value ) {
$_id = isset( $value['id'] ) ? $value['id'] : '';
$_label = ot_I18n_string( isset( $value['label'] ) ? str_replace( "'", "\'", $value['label'] ) : '' );
$_desc = ot_I18n_string( isset( $value['desc'] ) ? str_replace( "'", "\'", $value['desc'] ) : '' );
$_std = isset( $value['std'] ) ? str_replace( "'", "\'", $value['std'] ) : '';
$_type = isset( $value['type'] ) ? $value['type'] : '';
$_section = isset( $value['section'] ) ? $value['section'] : '';
$_rows = isset( $value['rows'] ) ? $value['rows'] : '';
$_post_type = isset( $value['post_type'] ) ? $value['post_type'] : '';
$_taxonomy = isset( $value['taxonomy'] ) ? $value['taxonomy'] : '';
$_min_max_step = isset( $value['min_max_step'] ) ? $value['min_max_step'] : '';
$_class = isset( $value['class'] ) ? $value['class'] : '';
$_condition = isset( $value['condition'] ) ? $value['condition'] : '';
$_operator = isset( $value['operator'] ) ? $value['operator'] : '';
$choices = '';
if ( isset( $value['choices'] ) && ! empty( $value['choices'] ) ) {
foreach( $value['choices'] as $choice ) {
$_choice_value = isset( $choice['value'] ) ? str_replace( "'", "\'", $choice['value'] ) : '';
$_choice_label = ot_I18n_string( isset( $choice['label'] ) ? str_replace( "'", "\'", $choice['label'] ) : '' );
$_choice_src = isset( $choice['src'] ) ? str_replace( "'", "\'", $choice['src'] ) : '';
$choices.= "
array(
'value' => '$_choice_value',
'label' => $_choice_label,
'src' => '$_choice_src'
),";
}
$choices = substr_replace( $choices, '' , -1 );
$choices = ",
'choices' => array( $choices
)";
}
$std = "'$_std'";
if ( is_array( $_std ) ) {
$std_array = array();
foreach( $_std as $_sk => $_sv ) {
$std_array[] = "'$_sk' => '$_sv'";
}
$std = 'array(
' . implode( ",\n", $std_array ) . '
)';
}
$setting_settings = '';
if ( isset( $value['settings'] ) && ! empty( $value['settings'] ) ) {
foreach( $value['settings'] as $setting ) {
$_setting_id = isset( $setting['id'] ) ? $setting['id'] : '';
$_setting_label = ot_I18n_string( isset( $setting['label'] ) ? str_replace( "'", "\'", $setting['label'] ) : '' );
$_setting_desc = ot_I18n_string( isset( $setting['desc'] ) ? str_replace( "'", "\'", $setting['desc'] ) : '' );
$_setting_std = isset( $setting['std'] ) ? $setting['std'] : '';
$_setting_type = isset( $setting['type'] ) ? $setting['type'] : '';
$_setting_rows = isset( $setting['rows'] ) ? $setting['rows'] : '';
$_setting_post_type = isset( $setting['post_type'] ) ? $setting['post_type'] : '';
$_setting_taxonomy = isset( $setting['taxonomy'] ) ? $setting['taxonomy'] : '';
$_setting_min_max_step = isset( $setting['min_max_step'] ) ? $setting['min_max_step'] : '';
$_setting_class = isset( $setting['class'] ) ? $setting['class'] : '';
$_setting_condition = isset( $setting['condition'] ) ? $setting['condition'] : '';
$_setting_operator = isset( $setting['operator'] ) ? $setting['operator'] : '';
$setting_choices = '';
if ( isset( $setting['choices'] ) && ! empty( $setting['choices'] ) ) {
foreach( $setting['choices'] as $setting_choice ) {
$_setting_choice_value = isset( $setting_choice['value'] ) ? $setting_choice['value'] : '';
$_setting_choice_label = ot_I18n_string( isset( $setting_choice['label'] ) ? str_replace( "'", "\'", $setting_choice['label'] ) : '' );
$_setting_choice_src = isset( $setting_choice['src'] ) ? str_replace( "'", "\'", $setting_choice['src'] ) : '';
$setting_choices.= "
array(
'value' => '$_setting_choice_value',
'label' => $_setting_choice_label,
'src' => '$_setting_choice_src'
),";
}
$setting_choices = substr_replace( $setting_choices, '' , -1 );
$setting_choices = ",
'choices' => array( $setting_choices
)";
}
$setting_std = "'$_setting_std'";
if ( is_array( $_setting_std ) ) {
$setting_std_array = array();
foreach( $_setting_std as $_ssk => $_ssv ) {
$setting_std_array[] = "'$_ssk' => '$_ssv'";
}
$setting_std = 'array(
' . implode( ",\n", $setting_std_array ) . '
)';
}
$setting_settings.= "
array(
'id' => '$_setting_id',
'label' => $_setting_label,
'desc' => $_setting_desc,
'std' => $setting_std,
'type' => '$_setting_type',
'rows' => '$_setting_rows',
'post_type' => '$_setting_post_type',
'taxonomy' => '$_setting_taxonomy',
'min_max_step'=> '$_setting_min_max_step',
'class' => '$_setting_class',
'condition' => '$_setting_condition',
'operator' => '$_setting_operator'$setting_choices
),";
}
$setting_settings = substr_replace( $setting_settings, '' , -1 );
$setting_settings = ",
'settings' => array( $setting_settings
)";
}
$settings.= "
array(
'id' => '$_id',
'label' => $_label,
'desc' => $_desc,
'std' => $std,
'type' => '$_type',
'section' => '$_section',
'rows' => '$_rows',
'post_type' => '$_post_type',
'taxonomy' => '$_taxonomy',
'min_max_step'=> '$_min_max_step',
'class' => '$_class',
'condition' => '$_condition',
'operator' => '$_operator'$choices$setting_settings
),";
}
$settings = substr_replace( $settings, '' , -1 );
}
/* check that $sections has a value and add to $build_settings */
if ( '' != $settings ) {
$build_settings.= ",
'settings' => array( $settings
)";
}
$content.= " $section ) {
/* remove from array if missing values */
if ( ( ! isset( $section['title'] ) && ! isset( $section['id'] ) ) || ( '' == $section['title'] && '' == $section['id'] ) ) {
unset( $settings['sections'][$k] );
} else {
/* validate label */
if ( '' != $section['title'] ) {
$settings['sections'][$k]['title'] = wp_kses_post( $section['title'] );
}
/* missing title set to unfiltered ID */
if ( ! isset( $section['title'] ) || '' == $section['title'] ) {
$settings['sections'][$k]['title'] = wp_kses_post( $section['id'] );
/* missing ID set to title */
} else if ( ! isset( $section['id'] ) || '' == $section['id'] ) {
$section['id'] = wp_kses_post( $section['title'] );
}
/* sanitize ID once everything has been checked first */
$settings['sections'][$k]['id'] = ot_sanitize_option_id( wp_kses_post( $section['id'] ) );
}
}
$settings['sections'] = ot_stripslashes( $settings['sections'] );
}
/* validate settings by looping over array as many times as it takes */
if ( isset( $settings['settings'] ) ) {
$settings['settings'] = ot_validate_settings_array( $settings['settings'] );
}
/* validate contextual_help */
if ( isset( $settings['contextual_help']['content'] ) ) {
/* fix numeric keys since drag & drop will change them */
$settings['contextual_help']['content'] = array_values( $settings['contextual_help']['content'] );
/* loop through content */
foreach( $settings['contextual_help']['content'] as $k => $content ) {
/* remove from array if missing values */
if ( ( ! isset( $content['title'] ) && ! isset( $content['id'] ) ) || ( '' == $content['title'] && '' == $content['id'] ) ) {
unset( $settings['contextual_help']['content'][$k] );
} else {
/* validate label */
if ( '' != $content['title'] ) {
$settings['contextual_help']['content'][$k]['title'] = wp_kses_post( $content['title'] );
}
/* missing title set to unfiltered ID */
if ( ! isset( $content['title'] ) || '' == $content['title'] ) {
$settings['contextual_help']['content'][$k]['title'] = wp_kses_post( $content['id'] );
/* missing ID set to title */
} else if ( ! isset( $content['id'] ) || '' == $content['id'] ) {
$content['id'] = wp_kses_post( $content['title'] );
}
/* sanitize ID once everything has been checked first */
$settings['contextual_help']['content'][$k]['id'] = ot_sanitize_option_id( wp_kses_post( $content['id'] ) );
}
/* validate textarea description */
if ( isset( $content['content'] ) ) {
$settings['contextual_help']['content'][$k]['content'] = wp_kses_post( $content['content'] );
}
}
}
/* validate contextual_help sidebar */
if ( isset( $settings['contextual_help']['sidebar'] ) ) {
$settings['contextual_help']['sidebar'] = wp_kses_post( $settings['contextual_help']['sidebar'] );
}
$settings['contextual_help'] = ot_stripslashes( $settings['contextual_help'] );
/* default message */
$message = 'failed';
/* is array: save & show success message */
if ( is_array( $settings ) ) {
/* WPML unregister ID's that have been removed */
if ( function_exists( 'icl_unregister_string' ) ) {
$current = get_option( ot_settings_id() );
$options = get_option( ot_options_id() );
if ( isset( $current['settings'] ) ) {
/* Empty ID array */
$new_ids = array();
/* Build the WPML IDs array */
foreach( $settings['settings'] as $setting ) {
if ( $setting['id'] ) {
$new_ids[] = $setting['id'];
}
}
/* Remove missing IDs from WPML */
foreach( $current['settings'] as $current_setting ) {
if ( ! in_array( $current_setting['id'], $new_ids ) ) {
if ( ! empty( $options[$current_setting['id']] ) && in_array( $current_setting['type'], array( 'list-item', 'slider' ) ) ) {
foreach( $options[$current_setting['id']] as $key => $value ) {
foreach( $value as $ckey => $cvalue ) {
ot_wpml_unregister_string( $current_setting['id'] . '_' . $ckey . '_' . $key );
}
}
} else if ( ! empty( $options[$current_setting['id']] ) && $current_setting['type'] == 'social-icons' ) {
foreach( $options[$current_setting['id']] as $key => $value ) {
foreach( $value as $ckey => $cvalue ) {
ot_wpml_unregister_string( $current_setting['id'] . '_' . $ckey . '_' . $key );
}
}
} else {
ot_wpml_unregister_string( $current_setting['id'] );
}
}
}
}
}
update_option( ot_settings_id(), $settings );
$message = 'success';
}
/* redirect */
wp_redirect( add_query_arg( array( 'action' => 'save-settings', 'message' => $message ), $_POST['_wp_http_referer'] ) );
exit;
}
return false;
}
}
/**
* Validate the settings array before save.
*
* This function will loop over the settings array as many
* times as it takes to validate every sub setting.
*
* @param array $settings The array of settings.
* @return array
*
* @access public
* @since 2.0
*/
if ( ! function_exists( 'ot_validate_settings_array' ) ) {
function ot_validate_settings_array( $settings = array() ) {
/* validate settings */
if ( count( $settings ) > 0 ) {
/* fix numeric keys since drag & drop will change them */
$settings = array_values( $settings );
/* loop through settings */
foreach( $settings as $k => $setting ) {
/* remove from array if missing values */
if ( ( ! isset( $setting['label'] ) && ! isset( $setting['id'] ) ) || ( '' == $setting['label'] && '' == $setting['id'] ) ) {
unset( $settings[$k] );
} else {
/* validate label */
if ( '' != $setting['label'] ) {
$settings[$k]['label'] = wp_kses_post( $setting['label'] );
}
/* missing label set to unfiltered ID */
if ( ! isset( $setting['label'] ) || '' == $setting['label'] ) {
$settings[$k]['label'] = $setting['id'];
/* missing ID set to label */
} else if ( ! isset( $setting['id'] ) || '' == $setting['id'] ) {
$setting['id'] = wp_kses_post( $setting['label'] );
}
/* sanitize ID once everything has been checked first */
$settings[$k]['id'] = ot_sanitize_option_id( wp_kses_post( $setting['id'] ) );
}
/* validate description */
if ( '' != $setting['desc'] ) {
$settings[$k]['desc'] = wp_kses_post( $setting['desc'] );
}
/* validate choices */
if ( isset( $setting['choices'] ) ) {
/* loop through choices */
foreach( $setting['choices'] as $ck => $choice ) {
/* remove from array if missing values */
if ( ( ! isset( $choice['label'] ) && ! isset( $choice['value'] ) ) || ( '' == $choice['label'] && '' == $choice['value'] ) ) {
unset( $setting['choices'][$ck] );
} else {
/* missing label set to unfiltered ID */
if ( ! isset( $choice['label'] ) || '' == $choice['label'] ) {
$setting['choices'][$ck]['label'] = wp_kses_post( $choice['value'] );
/* missing value set to label */
} else if ( ! isset( $choice['value'] ) || '' == $choice['value'] ) {
$setting['choices'][$ck]['value'] = ot_sanitize_option_id( wp_kses_post( $choice['label'] ) );
}
}
}
/* update keys and push new array values */
$settings[$k]['choices'] = array_values( $setting['choices'] );
}
/* validate sub settings */
if ( isset( $setting['settings'] ) ) {
$settings[$k]['settings'] = ot_validate_settings_array( $setting['settings'] );
}
}
}
/* return array but strip those damn slashes out first!!! */
return ot_stripslashes( $settings );
}
}
/**
* Save layouts array before the screen is displayed.
*
* @return void
*
* @access public
* @since 2.0
*/
if ( ! function_exists( 'ot_modify_layouts' ) ) {
function ot_modify_layouts() {
/* check and verify modify layouts nonce */
if ( isset( $_POST['option_tree_modify_layouts_nonce'] ) && wp_verify_nonce( $_POST['option_tree_modify_layouts_nonce'], 'option_tree_modify_layouts_form' ) ) {
/* previous layouts value */
$option_tree_layouts = get_option( ot_layouts_id() );
/* new layouts value */
$layouts = isset( $_POST[ot_layouts_id()] ) ? $_POST[ot_layouts_id()] : '';
/* rebuild layout array */
$rebuild = array();
/* validate layouts */
if ( is_array( $layouts ) && ! empty( $layouts ) ) {
/* setup active layout */
if ( isset( $layouts['active_layout'] ) && ! empty( $layouts['active_layout'] ) ) {
$rebuild['active_layout'] = $layouts['active_layout'];
}
/* add new and overwrite active layout */
if ( isset( $layouts['_add_new_layout_'] ) && ! empty( $layouts['_add_new_layout_'] ) ) {
$rebuild['active_layout'] = ot_sanitize_layout_id( $layouts['_add_new_layout_'] );
$rebuild[$rebuild['active_layout']] = ot_encode( serialize( get_option( ot_options_id() ) ) );
}
$first_layout = '';
/* loop through layouts */
foreach( $layouts as $key => $layout ) {
/* skip over active layout key */
if ( $key == 'active_layout' )
continue;
/* check if the key exists then set value */
if ( isset( $option_tree_layouts[$key] ) && ! empty( $option_tree_layouts[$key] ) ) {
$rebuild[$key] = $option_tree_layouts[$key];
if ( '' == $first_layout ) {
$first_layout = $key;
}
}
}
if ( isset( $rebuild['active_layout'] ) && ! isset( $rebuild[$rebuild['active_layout']] ) && ! empty( $first_layout ) ) {
$rebuild['active_layout'] = $first_layout;
}
}
/* default message */
$message = 'failed';
/* is array: save & show success message */
if ( count( $rebuild ) > 1 ) {
/* rebuild the theme options */
$rebuild_option_tree = unserialize( ot_decode( $rebuild[$rebuild['active_layout']] ) );
if ( is_array( $rebuild_option_tree ) ) {
/* execute the action hook and pass the theme options to it */
do_action( 'ot_before_theme_options_save', $rebuild_option_tree );
update_option( ot_options_id(), $rebuild_option_tree );
}
/* rebuild the layouts */
update_option( ot_layouts_id(), $rebuild );
/* change message */
$message = 'success';
} else if ( count( $rebuild ) <= 1 ) {
/* delete layouts option */
delete_option( ot_layouts_id() );
/* change message */
$message = 'deleted';
}
/* redirect */
if ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] == apply_filters( 'ot_theme_options_menu_slug', 'ot-theme-options' ) ) {
$query_args = add_query_arg( array( 'settings-updated' => 'layout' ), remove_query_arg( array( 'action', 'message' ), $_POST['_wp_http_referer'] ) );
} else {
$query_args = add_query_arg( array( 'action' => 'save-layouts', 'message' => $message ), $_POST['_wp_http_referer'] );
}
wp_redirect( $query_args );
exit;
}
return false;
}
}
/**
* Helper function to display alert messages.
*
* @param array Page array
* @return mixed
*
* @access public
* @since 2.0
*/
if ( ! function_exists( 'ot_alert_message' ) ) {
function ot_alert_message( $page = array() ) {
if ( empty( $page ) )
return false;
$before = apply_filters( 'ot_before_page_messages', '', $page );
if ( $before ) {
return $before;
}
$action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : '';
$message = isset( $_REQUEST['message'] ) ? $_REQUEST['message'] : '';
$updated = isset( $_REQUEST['settings-updated'] ) ? $_REQUEST['settings-updated'] : '';
if ( $action == 'save-settings' ) {
if ( $message == 'success' ) {
return '' . __( 'Settings updated.', 'option-tree' ) . '
' . __( 'Settings could not be saved.', 'option-tree' ) . '
' . __( 'Settings Imported.', 'option-tree' ) . '
' . __( 'Settings could not be imported.', 'option-tree' ) . '
' . __( 'Data Imported.', 'option-tree' ) . '
' . __( 'Data could not be imported.', 'option-tree' ) . '
' . __( 'Layouts Imported.', 'option-tree' ) . '
' . __( 'Layouts could not be imported.', 'option-tree' ) . '
' . __( 'Layouts Updated.', 'option-tree' ) . '
' . __( 'Layouts could not be updated.', 'option-tree' ) . '
' . __( 'Layouts have been deleted.', 'option-tree' ) . '
' . __( 'Layout activated.', 'option-tree' ) . '
' . $page['reset_message'] . '
' . $page['updated_message'] . '
http://' ),
'type' => 'text',
)
), $id );
return $settings;
}
}
/**
* Inserts CSS with field_id markers.
*
* Inserts CSS into a dynamic.css file, placing it between
* BEGIN and END field_id markers. Replaces existing marked info,
* but still retains surrounding data.
*
* @param string $field_id The CSS option field ID.
* @param array $options The current option_tree array.
* @return bool True on write success, false on failure.
*
* @access public
* @since 1.1.8
* @updated 2.0.12
*/
if ( ! function_exists( 'ot_insert_css_with_markers' ) ) {
function ot_insert_css_with_markers( $field_id = '', $insertion = '', $meta = false ) {
/* missing $field_id or $insertion exit early */
if ( '' == $field_id || '' == $insertion )
return;
/* path to the dynamic.css file */
$filepath = get_stylesheet_directory() . '/dynamic.css';
if ( is_multisite() ) {
$multisite_filepath = get_stylesheet_directory() . '/dynamic-' . get_current_blog_id() . '.css';
if ( file_exists( $multisite_filepath ) ) {
$filepath = $multisite_filepath;
}
}
/* allow filter on path */
$filepath = apply_filters( 'css_option_file_path', $filepath, $field_id );
/* grab a copy of the paths array */
$ot_css_file_paths = get_option( 'ot_css_file_paths', array() );
if ( is_multisite() ) {
$ot_css_file_paths = get_blog_option( get_current_blog_id(), 'ot_css_file_paths', $ot_css_file_paths );
}
/* set the path for this field */
$ot_css_file_paths[$field_id] = $filepath;
/* update the paths */
if ( is_multisite() ) {
update_blog_option( get_current_blog_id(), 'ot_css_file_paths', $ot_css_file_paths );
} else {
update_option( 'ot_css_file_paths', $ot_css_file_paths );
}
/* insert CSS into file */
if ( file_exists( $filepath ) ) {
$insertion = ot_normalize_css( $insertion );
$regex = "/{{([a-zA-Z0-9\_\-\#\|\=]+)}}/";
$marker = $field_id;
/* Match custom CSS */
preg_match_all( $regex, $insertion, $matches );
/* Loop through CSS */
foreach( $matches[0] as $option ) {
$value = '';
$option_id = str_replace( array( '{{', '}}' ), '', $option );
$option_array = explode( '|', $option_id );
$option_type = ot_get_option_type_by_id( $option_id );
/* get the array value */
if ( $meta ) {
global $post;
$value = get_post_meta( $post->ID, $option_array[0], true );
} else {
$options = get_option( ot_options_id() );
if ( isset( $options[$option_array[0]] ) ) {
$value = $options[$option_array[0]];
}
}
if ( is_array( $value ) ) {
if ( ! isset( $option_array[1] ) ) {
/* Measurement */
if ( isset( $value[0] ) && isset( $value[1] ) ) {
/* set $value with measurement properties */
$value = $value[0].$value[1];
/* Border */
} else if ( ot_array_keys_exists( $value, array( 'width', 'unit', 'style', 'color' ) ) && ! ot_array_keys_exists( $value, array( 'top', 'right', 'bottom', 'left', 'height', 'inset', 'offset-x', 'offset-y', 'blur-radius', 'spread-radius' ) ) ) {
$border = array();
$unit = ! empty( $value['unit'] ) ? $value['unit'] : 'px';
if ( ! empty( $value['width'] ) )
$border[] = $value['width'].$unit;
if ( ! empty( $value['style'] ) )
$border[] = $value['style'];
if ( ! empty( $value['color'] ) )
$border[] = $value['color'];
/* set $value with border properties or empty string */
$value = ! empty( $border ) ? implode( ' ', $border ) : '';
/* Box Shadow */
} else if ( ot_array_keys_exists( $value, array( 'inset', 'offset-x', 'offset-y', 'blur-radius', 'spread-radius', 'color' ) ) && ! ot_array_keys_exists( $value, array( 'width', 'height', 'unit', 'style', 'top', 'right', 'bottom', 'left' ) ) ) {
/* set $value with box-shadow properties or empty string */
$value = ! empty( $value ) ? implode( ' ', $value ) : '';
/* Dimension */
} else if ( ot_array_keys_exists( $value, array( 'width', 'height', 'unit' ) ) && ! ot_array_keys_exists( $value, array( 'style', 'color', 'top', 'right', 'bottom', 'left' ) ) ) {
$dimension = array();
$unit = ! empty( $value['unit'] ) ? $value['unit'] : 'px';
if ( ! empty( $value['width'] ) )
$dimension[] = $value['width'].$unit;
if ( ! empty( $value['height'] ) )
$dimension[] = $value['height'].$unit;
/* set $value with dimension properties or empty string */
$value = ! empty( $dimension ) ? implode( ' ', $dimension ) : '';
/* Spacing */
} else if ( ot_array_keys_exists( $value, array( 'top', 'right', 'bottom', 'left', 'unit' ) ) && ! ot_array_keys_exists( $value, array( 'width', 'height', 'style', 'color' ) ) ) {
$spacing = array();
$unit = ! empty( $value['unit'] ) ? $value['unit'] : 'px';
if ( ! empty( $value['top'] ) )
$spacing[] = $value['top'].$unit;
if ( ! empty( $value['right'] ) )
$spacing[] = $value['right'].$unit;
if ( ! empty( $value['bottom'] ) )
$spacing[] = $value['bottom'].$unit;
if ( ! empty( $value['left'] ) )
$spacing[] = $value['left'].$unit;
/* set $value with spacing properties or empty string */
$value = ! empty( $spacing ) ? implode( ' ', $spacing ) : '';
/* typography */
} else if ( ot_array_keys_exists( $value, array( 'font-color', 'font-family', 'font-size', 'font-style', 'font-variant', 'font-weight', 'letter-spacing', 'line-height', 'text-decoration', 'text-transform' ) ) ) {
$font = array();
if ( ! empty( $value['font-color'] ) )
$font[] = "color: " . $value['font-color'] . ";";
if ( ! empty( $value['font-family'] ) ) {
foreach ( ot_recognized_font_families( $marker ) as $key => $v ) {
if ( $key == $value['font-family'] ) {
$font[] = "font-family: " . $v . ";";
}
}
}
if ( ! empty( $value['font-size'] ) )
$font[] = "font-size: " . $value['font-size'] . ";";
if ( ! empty( $value['font-style'] ) )
$font[] = "font-style: " . $value['font-style'] . ";";
if ( ! empty( $value['font-variant'] ) )
$font[] = "font-variant: " . $value['font-variant'] . ";";
if ( ! empty( $value['font-weight'] ) )
$font[] = "font-weight: " . $value['font-weight'] . ";";
if ( ! empty( $value['letter-spacing'] ) )
$font[] = "letter-spacing: " . $value['letter-spacing'] . ";";
if ( ! empty( $value['line-height'] ) )
$font[] = "line-height: " . $value['line-height'] . ";";
if ( ! empty( $value['text-decoration'] ) )
$font[] = "text-decoration: " . $value['text-decoration'] . ";";
if ( ! empty( $value['text-transform'] ) )
$font[] = "text-transform: " . $value['text-transform'] . ";";
/* set $value with font properties or empty string */
$value = ! empty( $font ) ? implode( "\n", $font ) : '';
/* background */
} else if ( ot_array_keys_exists( $value, array( 'background-color', 'background-image', 'background-repeat', 'background-attachment', 'background-position', 'background-size' ) ) ) {
$bg = array();
if ( ! empty( $value['background-color'] ) )
$bg[] = $value['background-color'];
if ( ! empty( $value['background-image'] ) ) {
/* If an attachment ID is stored here fetch its URL and replace the value */
if ( wp_attachment_is_image( $value['background-image'] ) ) {
$attachment_data = wp_get_attachment_image_src( $value['background-image'], 'original' );
/* check for attachment data */
if ( $attachment_data ) {
$value['background-image'] = $attachment_data[0];
}
}
$bg[] = 'url("' . $value['background-image'] . '")';
}
if ( ! empty( $value['background-repeat'] ) )
$bg[] = $value['background-repeat'];
if ( ! empty( $value['background-attachment'] ) )
$bg[] = $value['background-attachment'];
if ( ! empty( $value['background-position'] ) )
$bg[] = $value['background-position'];
if ( ! empty( $value['background-size'] ) )
$size = $value['background-size'];
/* set $value with background properties or empty string */
$value = ! empty( $bg ) ? 'background: ' . implode( " ", $bg ) . ';' : '';
if ( isset( $size ) ) {
if ( ! empty( $bg ) ) {
$value.= apply_filters( 'ot_insert_css_with_markers_bg_size_white_space', "\n\x20\x20", $option_id );
}
$value.= "background-size: $size;";
}
}
} else {
$value = $value[$option_array[1]];
}
}
/* If an attachment ID is stored here fetch its URL and replace the value */
if ( $option_type == 'upload' && wp_attachment_is_image( $value ) ) {
$attachment_data = wp_get_attachment_image_src( $value, 'original' );
/* check for attachment data */
if ( $attachment_data ) {
$value = $attachment_data[0];
}
}
// Filter the CSS
$value = apply_filters( 'ot_insert_css_with_markers_value', $value, $option_id );
/* insert CSS, even if the value is empty */
$insertion = stripslashes( str_replace( $option, $value, $insertion ) );
}
// Can't write to the file so we error out
if ( ! is_writable( $filepath ) ) {
add_settings_error( 'option-tree', 'dynamic_css', sprintf( __( 'Unable to write to file %s.', 'option-tree' ), '' . $filepath . '' ), 'error' );
return false;
}
/* create array from the lines of code */
$markerdata = explode( "\n", implode( '', file( $filepath ) ) );
/* can't write to the file return false */
if ( ! $f = ot_file_open( $filepath, 'w' ) ) {
return false;
}
$searching = true;
$foundit = false;
/* has array of lines */
if ( ! empty( $markerdata ) ) {
/* foreach line of code */
foreach( $markerdata as $n => $markerline ) {
/* found begining of marker, set $searching to false */
if ( $markerline == "/* BEGIN {$marker} */" )
$searching = false;
/* keep rewrite each line of CSS */
if ( $searching == true ) {
if ( $n + 1 < count( $markerdata ) )
ot_file_write( $f, "{$markerline}\n" );
else
ot_file_write( $f, "{$markerline}" );
}
/* found end marker write code */
if ( $markerline == "/* END {$marker} */" ) {
ot_file_write( $f, "/* BEGIN {$marker} */\n" );
ot_file_write( $f, "{$insertion}\n" );
ot_file_write( $f, "/* END {$marker} */\n" );
$searching = true;
$foundit = true;
}
}
}
/* nothing inserted, write code. DO IT, DO IT! */
if ( ! $foundit ) {
ot_file_write( $f, "/* BEGIN {$marker} */\n" );
ot_file_write( $f, "{$insertion}\n" );
ot_file_write( $f, "/* END {$marker} */\n" );
}
/* close file */
ot_file_close( $f );
return true;
}
return false;
}
}
/**
* Remove old CSS.
*
* Removes CSS when the textarea is empty, but still retains surrounding styles.
*
* @param string $field_id The CSS option field ID.
* @return bool True on write success, false on failure.
*
* @access public
* @since 2.0
*/
if ( ! function_exists( 'ot_remove_old_css' ) ) {
function ot_remove_old_css( $field_id = '' ) {
/* missing $field_id string */
if ( '' == $field_id )
return false;
/* path to the dynamic.css file */
$filepath = get_stylesheet_directory() . '/dynamic.css';
/* allow filter on path */
$filepath = apply_filters( 'css_option_file_path', $filepath, $field_id );
/* remove CSS from file */
if ( is_writeable( $filepath ) ) {
/* get each line in the file */
$markerdata = explode( "\n", implode( '', file( $filepath ) ) );
/* can't write to the file return false */
if ( ! $f = ot_file_open( $filepath, 'w' ) )
return false;
$searching = true;
/* has array of lines */
if ( ! empty( $markerdata ) ) {
/* foreach line of code */
foreach ( $markerdata as $n => $markerline ) {
/* found begining of marker, set $searching to false */
if ( $markerline == "/* BEGIN {$field_id} */" )
$searching = false;
/* $searching is true, keep rewrite each line of CSS */
if ( $searching == true ) {
if ( $n + 1 < count( $markerdata ) )
ot_file_write( $f, "{$markerline}\n" );
else
ot_file_write( $f, "{$markerline}" );
}
/* found end marker delete old CSS */
if ( $markerline == "/* END {$field_id} */" ) {
ot_file_write( $f, "" );
$searching = true;
}
}
}
/* close file */
ot_file_close( $f );
return true;
}
return false;
}
}
/**
* Normalize CSS
*
* Normalize & Convert all line-endings to UNIX format.
*
* @param string $css
* @return string
*
* @access public
* @since 1.1.8
* @updated 2.0
*/
if ( ! function_exists( 'ot_normalize_css' ) ) {
function ot_normalize_css( $css ) {
/* Normalize & Convert */
$css = str_replace( "\r\n", "\n", $css );
$css = str_replace( "\r", "\n", $css );
/* Don't allow out-of-control blank lines */
$css = preg_replace( "/\n{2,}/", "\n\n", $css );
return $css;
}
}
/**
* Helper function to loop over the option types.
*
* @param array $type The current option type.
*
* @return string
*
* @access public
* @since 2.0
*/
if ( ! function_exists( 'ot_loop_through_option_types' ) ) {
function ot_loop_through_option_types( $type = '', $child = false ) {
$content = '';
$types = ot_option_types_array();
if ( $child )
unset($types['list-item']);
foreach( $types as $key => $value )
$content.= '';
return $content;
}
}
/**
* Helper function to loop over choices.
*
* @param string $name The form element name.
* @param array $choices The array of choices.
*
* @return string
*
* @access public
* @since 2.0
*/
if ( ! function_exists( 'ot_loop_through_choices' ) ) {
function ot_loop_through_choices( $name, $choices = array() ) {
$content = '';
foreach( (array) $choices as $key => $choice )
$content.= 'OptionTree->Documentation for more information on which ones.', 'option-tree' ) . '0,100,1 (slide from 0-100 in intervals of 1). The three values represent the minimum, maximum, and step options and will only affect the Numeric Slider option type.', 'option-tree' ) . 'value is a placeholder for your condition, which can be in the form of %s.', 'option-tree' ), 'field_id:is(value), field_id:not(value), field_id:contains(value), field_id:less_than(value), field_id:less_than_or_equal_to(value), field_id:greater_than(value), or field_id:greater_than_or_equal_to(value)' ) . 'tags */ $string = str_replace( array( "\n", "
" ), "", $string );
/* replace
with \r */
$string = str_replace( array( "
", "
", "
" ), "\r", $string );
/* replace
[video]' ),
'std' => '',
'type' => 'textarea'
)
)
), $pages );
}
/**
* Returns an array with the post format audio metabox.
*
* @param mixed $pages Excepts a comma separated string or array of
* post_types and is what tells the metabox where to
* display. Default 'post'.
* @return array
*
* @access public
* @since 2.4.0
*/
function ot_meta_box_post_format_audio( $pages = 'post' ) {
if ( ! current_theme_supports( 'post-formats' ) || ! in_array( 'audio', current( get_theme_support( 'post-formats' ) ) ) )
return false;
if ( is_string( $pages ) )
$pages = explode( ',', $pages );
return apply_filters( 'ot_meta_box_post_format_audio', array(
'id' => 'ot-post-format-audio',
'title' => __( 'Audio', 'option-tree' ),
'desc' => '',
'pages' => $pages,
'context' => 'side',
'priority' => 'low',
'fields' => array(
array(
'id' => '_format_audio_embed',
'label' => '',
'desc' => sprintf( __( 'Embed audio from services like SoundCloud and Rdio. You can find a list of supported oEmbed sites in the %1$s. Alternatively, you could use the built-in %2$s shortcode.', 'option-tree' ), '' . __( 'Wordpress Codex', 'option-tree' ) .'', '[audio]' ),
'std' => '',
'type' => 'textarea'
)
)
), $pages );
}
/**
* Returns the option type by ID.
*
* @param string $option_id The option ID
* @return string $settings_id The settings array ID
* @return string The option type.
*
* @access public
* @since 2.4.2
*/
if ( ! function_exists( 'ot_get_option_type_by_id' ) ) {
function ot_get_option_type_by_id( $option_id, $settings_id = '' ) {
if ( empty( $settings_id ) ) {
$settings_id = ot_settings_id();
}
$settings = get_option( $settings_id, array() );
if ( isset( $settings['settings'] ) ) {
foreach( $settings['settings'] as $value ) {
if ( $option_id == $value['id'] && isset( $value['type'] ) ) {
return $value['type'];
}
}
}
return false;
}
}
/* End of file ot-functions-admin.php */
/* Location: ./includes/ot-functions-admin.php */