= 3.5 if( function_exists( 'wp_enqueue_media' ) ) { wp_enqueue_media(); wp_enqueue_script( 'theme-option-media-uploader-3.5', $directory_uri . '/cyberchimps/options/lib/js/media-uploader-new.js', array( 'jquery' ) ); } // Enqueued scripts wp_enqueue_script( 'jquery-ui-core' ); wp_enqueue_script( 'jquery-ui-sortable' ); // Adding JS to support drag and drop in theme options wp_enqueue_script( 'jquery-touch-punch-min', $directory_uri . '/cyberchimps/lib/js/touch-punch-min.js', array( 'jquery' ) ); wp_enqueue_script( 'jquery-touch-sense', $directory_uri . '/cyberchimps/lib/js/touch-sensitive.js', array( 'jquery' ) ); wp_enqueue_script( 'thickbox' ); wp_enqueue_script( 'color-picker', $directory_uri . '/cyberchimps/options/lib/js/colorpicker.js', array( 'jquery' ) ); wp_enqueue_script( 'media-uploader', $directory_uri . '/cyberchimps/options/lib/js/options-medialibrary-uploader.js', array( 'jquery' ) ); wp_enqueue_script( 'options-custom', $directory_uri . '/cyberchimps/options/lib/js/options-custom.js', array( 'jquery' ) ); wp_enqueue_script( 'bootstrap-js', $directory_uri . '/cyberchimps/lib/bootstrap/js/bootstrap.min.js', array( 'jquery' ) ); wp_enqueue_script( 'google-fonts', $directory_uri . '/cyberchimps/options/lib/js/font_inline_plugin.js', array( 'jquery' ) ); } // Load options customizer file add_action( 'init', 'cyberchimps_load_customizer' ); function cyberchimps_load_customizer() { require_once dirname( __FILE__ ) . '/options-customizer.php'; } // add core and theme settings to options page add_action( 'admin_init', 'cyberchimps_admin_init' ); function cyberchimps_admin_init() { // Load options media uploader require_once dirname( __FILE__ ) . '/options-medialibrary-uploader.php'; // register theme options settings register_setting( 'cyberchimps_options', 'cyberchimps_options', 'cyberchimps_options_validate' ); // add all core settings // Create sections $sections_list = cyberchimps_get_sections(); cyberchimps_create_sections( $sections_list ); // Create fields $fields_list = cyberchimps_get_fields(); cyberchimps_create_fields( $fields_list ); } // create and display theme options page function cyberchimps_options_page() { settings_errors(); ?>

'; echo '

' . esc_html( $heading['title'] ) . '

'; if( isset( $heading['description'] ) ) { echo '

' . esc_html( $heading['description'] ) . '

'; } cyberchimps_do_settings_sections( $heading['id'] ); echo '
'; } ?>
'; if( $section['title'] ) { echo "

{$section['title']}

\n"; } // wrapper div of all field-container divs echo '
'; //Hook before section options do_action( $jquery_click_section_hook . '_before' ); call_user_func( $section['callback'], $section ); if( isset( $wp_settings_fields ) && isset( $wp_settings_fields[$page] ) && isset( $wp_settings_fields[$page][$section['id']] ) ) { cyberchimps_do_settings_fields( $page, $section['id'] ); } //Hook after section options do_action( $jquery_click_section_hook . '_after' ); echo '
'; // .field-container ends echo '
'; } } /** * forked version of core function do_settings_fields() * modified core code to remove table cell markup and apply custom markup * returns mixed data */ function cyberchimps_do_settings_fields( $page, $section ) { global $wp_settings_fields; if( !isset( $wp_settings_fields ) || !isset( $wp_settings_fields[$page] ) || !isset( $wp_settings_fields[$page][$section] ) ) { return; } foreach( (array)$wp_settings_fields[$page][$section] as $field ) { call_user_func( $field['callback'], $field['args'] ); } } function cyberchimps_get_headings() { $headings_list = array(); // pull in both default sections and users custom sections return apply_filters( 'cyberchimps_heading_list', $headings_list ); } function cyberchimps_get_sections() { $sections_list = array(); // pull in both default sections and users custom sections return apply_filters( 'cyberchimps_section_list', $sections_list ); } function cyberchimps_get_fields() { $fields_list = array(); // pull in both default fields and users custom fields return apply_filters( 'cyberchimps_field_list', $fields_list ); } function cyberchimps_create_sections( $sections ) { if( empty( $sections ) ) { return false; } // add in error checking and proper validation, escaping, and translation calls foreach( $sections as $section ) { if( cyberchimps_section_exists( $section['heading'], $section['id'] ) ) { continue; } else { add_settings_section( $section['id'], $section['label'], 'cyberchimps_sections_callback', $section['heading'] ); } } } function cyberchimps_drag_drop_field( $value ) { // Set directory uri $directory_uri = get_template_directory_uri(); $option_name = 'cyberchimps_options'; $settings = get_option( $option_name ); $val = ''; $output = ''; // If the option is already saved, ovveride $val if( ( $value['type'] != 'heading' ) && ( $value['type'] != 'info' ) ) { if( isset( $settings[( $value['id'] )] ) ) { // Assign empty array if the array returns null if( $settings[( $value['id'] )] != "" ) { $val = $settings[( $value['id'] )]; } else { $val = null; } // Striping slashes of non-array options if( !is_array( $val ) ) { $val = stripslashes( $val ); } } } // Set default value to $val if( empty( $val ) ) { if( isset( $value['std'] ) ) { if( is_array( $value['std'] ) ) { $val = array_keys( $value['std'] ); } else { $val = array_keys( explode( $value['std'] ) ); } } } $output .= "
"; $output .= "
"; $output .= "
Inactive Elements
"; $output .= "
"; if( is_array( $val ) ) { foreach( $value['options'] as $key => $option ) { if( in_array( $key, $val ) ) { continue; } $output .= "
"; $output .= ''; $output .= "{$option}"; $output .= "
"; } } $output .= "
"; $output .= "
"; $output .= '
'; $output .= "
"; $output .= "
Active Elements
"; $output .= "
Drag & Drop Elements
"; $output .= "
"; if( is_array( $val ) ) { foreach( $val as $key ) { if( !array_key_exists( $key, $value['options'] ) ) { continue; } $output .= "
"; $output .= ''; $output .= "{$value['options'][$key]}"; $output .= "
"; } } $output .= "
"; $output .= ""; $output .= "
"; $output .= '
'; $output .= '
'; $output .= "
"; echo $output; } function cyberchimps_sections_callback( $section_passed ) { $sections = cyberchimps_get_sections(); if( empty( $sections ) && empty( $section_passed ) ) { return false; } foreach( $sections as $section ) { if( $section_passed['id'] == $section['id'] ) { echo '

'; if( isset( $section['description'] ) ) { echo $section['description']; } echo '

'; } } } /** * custom function that checks if the section has been run through add_settings_section() function * returns bool value true if section exists and false if it does not */ function cyberchimps_section_exists( $heading, $section ) { global $wp_settings_sections; if( isset( $wp_settings_sections[$heading][$section] ) ) { return true; } return false; } function cyberchimps_create_fields( $fields ) { if( empty( $fields ) ) { return false; } // loop through and create each field foreach( $fields as $field_args ) { $field_defaults = array( 'id' => false, 'name' => __( 'Default Field', 'cyberchimps_core' ), 'callback' => 'cyberchimps_fields_callback', 'section' => 'cyberchimps_default_section', 'heading' => 'cyberchimps_default_heading', ); $field_args = wp_parse_args( $field_args, $field_defaults ); if( empty( $field_args['id'] ) ) { continue; } elseif( !cyberchimps_section_exists( $field_args['heading'], $field_args['section'] ) ) { continue; } else { add_settings_field( $field_args['id'], $field_args['name'], $field_args['callback'], $field_args['heading'], $field_args['section'], $field_args ); } } } function cyberchimps_fields_callback( $value ) { global $allowedtags; $option_name = 'cyberchimps_options'; $settings = get_option( $option_name ); $val = ''; $select_value = ''; $checked = ''; $output = ''; // Set default value to $val if( isset( $value['std'] ) ) { $val = $value['std']; } // If the option is already saved, ovveride $val if( ( $value['type'] != 'heading' ) && ( $value['type'] != 'info' ) ) { if( isset( $settings[( $value['id'] )] ) ) { $val = $settings[( $value['id'] )]; // Striping slashes of non-array options if( !is_array( $val ) ) { $val = stripslashes( $val ); } } } // If there is no class set make it blank if( !isset( $value['class'] ) ) { $value['class'] = ''; } // If there is a description save it for labels $explain_value = ''; if( isset( $value['desc'] ) ) { $explain_value = $value['desc']; } // field wrapper $output .= '
'; // Output field name if( $value['name'] && $value['type'] != 'info' && $value['type'] != 'welcome' && $value['type'] != 'toggle' ) { $output .= ''; } switch( $value['type'] ) { // Basic text input case 'text': $output .= ''; break; // Same as text but allows some basic a, href, title, br, em and strong html, check the sanitization case 'text_html': $output .= ''; break; // Textarea case 'textarea': $rows = '8'; if( isset( $value['settings']['rows'] ) ) { $custom_rows = $value['settings']['rows']; if( is_numeric( $custom_rows ) ) { $rows = $custom_rows; } } $val = stripslashes( $val ); $output .= ''; break; // Unfiltered Textarea case 'unfiltered_textarea': $rows = '8'; if( isset( $value['settings']['rows'] ) ) { $custom_rows = $value['settings']['rows']; if( is_numeric( $custom_rows ) ) { $rows = $custom_rows; } } $val = stripslashes( $val ); $output .= ''; break; // css Textarea case 'csstextarea': $rows = '8'; if( isset( $value['settings']['rows'] ) ) { $custom_rows = $value['settings']['rows']; if( is_numeric( $custom_rows ) ) { $rows = $custom_rows; } } $val = stripslashes( $val ); $output .= ''; $output .= '
'; break; // Select Box case 'select': $output .= ''; break; // Radio Box case "radio": $name = $option_name . '[' . $value['id'] . ']'; $val = ( $val != '' ) ? $val : $value['std']; foreach( $value['options'] as $key => $option ) { $id = $option_name . '-' . $value['id'] . '-' . $key; $output .= '
'; } break; // Image Selectors case "images": $name = $option_name . '[' . $value['id'] . ']'; $output .= '
'; foreach( $value['options'] as $key => $option ) { $selected = ''; $checked = ''; if( $val != '' ) { if( $val == $key ) { $selected = ' of-radio-img-selected'; $checked = ' checked="checked"'; } } $output .= '
'; $output .= '
' . esc_html( $key ) . '
'; $output .= '' . $option . '
'; } $output .= '
'; break; // Checkbox case "checkbox": $output .= '
'; $output .= '
'; break; // Multicheck case "multicheck": foreach( $value['options'] as $key => $option ) { $checked = ''; $label = $option; $option = preg_replace( '/[^a-zA-Z0-9._\-]/', '', strtolower( $key ) ); $id = $option_name . '-' . $value['id'] . '-' . $option; $name = $option_name . '[' . $value['id'] . '][' . $option . ']'; if( isset( $val[$option] ) ) { $checked = checked( $val[$option], 1, false ); } $output .= '
'; } break; // Toggle Switch case "toggle": $checked = ""; if( $val ) { $checked = 'checked="checked"'; } $output .= '
'; break; // Color picker case "color": $output .= '
'; $output .= '
'; break; // Uploader case "upload": $output .= cyberchimps_medialibrary_uploader( $value['class'], $value['id'], $val, null, $explain_value ); break; // Typography case 'typography': unset( $font_size, $font_style, $font_face, $font_color ); $typography_defaults = array( 'size' => '', 'face' => '', 'style' => '', 'color' => '' ); $typography_stored = wp_parse_args( $val, $typography_defaults ); $typography_options = array( 'sizes' => cyberchimps_recognized_font_sizes(), 'faces' => cyberchimps_recognized_font_faces(), 'styles' => cyberchimps_recognized_font_styles(), 'color' => true ); if( isset( $value['options'] ) ) { $typography_options = wp_parse_args( $value['options'], $typography_options ); } // Font Size if( $typography_options['sizes'] ) { $font_size = ''; } // Font Face if( $typography_options['faces'] ) { $font_face = ''; } // Font Styles if( $typography_options['styles'] ) { $font_style = ''; } // Font Color if( $typography_options['color'] ) { $font_color = '
'; $font_color .= '
'; } // Allow modification/injection of typography fields $typography_fields = compact( 'font_size', 'font_face', 'font_style', 'font_color' ); $typography_fields = apply_filters( 'cyberchimps_typography_fields', $typography_fields, $typography_stored, $option_name, $value ); $output .= implode( '', $typography_fields ); break; // Background case 'background': $background = $val; // Background Color $output .= '
'; $output .= '
'; // Background Image - New AJAX Uploader using Media Library if( !isset( $background['image'] ) ) { $background['image'] = ''; } $output .= cyberchimps_medialibrary_uploader( $value['class'], $value['id'], $background['image'], null, '', 0, 'image' ); $class = 'of-background-properties'; if( '' == $background['image'] ) { $class .= ' hide'; } $output .= '
'; // Background Repeat $output .= ''; // Background Position $output .= ''; // Background Attachment $output .= ''; $output .= '
'; break; // Editor case 'editor': $output .= '
' . wp_kses( $explain_value, $allowedtags ) . '
' . "\n"; echo $output; $textarea_name = esc_attr( $option_name . '[' . $value['id'] . ']' ); $default_editor_settings = array( 'textarea_name' => $textarea_name, 'media_buttons' => false, 'tinymce' => array( 'plugins' => 'wordpress' ) ); $editor_settings = array(); if( isset( $value['settings'] ) ) { $editor_settings = $value['settings']; } $editor_settings = array_merge( $editor_settings, $default_editor_settings ); wp_editor( $val, $value['id'], $editor_settings ); $output = ''; break; // Info case "info": $id = ''; $class = 'section'; if( isset( $value['id'] ) ) { $id = 'id="' . esc_attr( $value['id'] ) . '" '; } if( isset( $value['type'] ) ) { $class .= ' section-' . $value['type']; } if( isset( $value['class'] ) ) { $class .= ' ' . $value['class']; } $output .= '
' . "\n"; if( isset( $value['name'] ) ) { $output .= '

' . esc_html( $value['name'] ) . '

' . "\n"; } if( $value['desc'] ) { $output .= apply_filters( 'cyberchimps_sanitize_info', $value['desc'] ) . "\n"; } $output .= '
' . "\n"; break; // Welcome case "welcome": $id = ''; $class = 'section'; if( isset( $value['id'] ) ) { $id = 'id="' . esc_attr( $value['id'] ) . '" '; } if( isset( $value['type'] ) ) { $class .= ' section-' . $value['type']; } if( isset( $value['class'] ) ) { $class .= ' ' . $value['class']; } $output .= '
' . "\n"; if( isset( $value['name'] ) ) { $output .= '

' . esc_html( apply_filters( 'cyberchimps_help_sub_heading', $value['name'] ) ) . '

' . "\n"; } $output .= apply_filters( 'cyberchimps_sanitize_info', apply_filters( 'cyberchimps_help_description', '' ) ) . "\n"; $output .= '
' . "\n"; break; case "export": $output .= ""; break; case "import": $output .= ""; break; } if( ( $value['type'] != "heading" ) && ( $value['type'] != "info" ) && ( $value['type'] != "welcome" ) && ( $value['type'] != "upload" ) ) { if( ( $value['type'] != "checkbox" ) && ( $value['type'] != "editor" ) ) { $output .= '
' . wp_kses( $explain_value, $allowedtags ) . '
' . "\n"; } } // end field wrapper $output .= '
'; echo $output; } /** * Validate options * * Validate theme options before updating to database. */ function cyberchimps_options_validate( $input ) { // Theme option import functionality if( isset( $_POST['import'] ) ) { if( trim( $_POST['import'] ) ) { $string = stripslashes( trim( $_POST['import'] ) ); $try = unserialize( $string ); if( $try ) { add_settings_error( 'cyberchimps_options', 'imported_success', __( 'Options Imported', 'cyberchimps_core' ), 'updated fade' ); return $try; } else { add_settings_error( 'cyberchimps_options', 'imported_failed', __( 'Invalid Data for Import', 'cyberchimps_core' ), 'error fade' ); } } } /* * Restore Defaults. * * In the event that the user clicked the "Restore Defaults" * button, the options defined in the theme's options.php * file will be added to the option for the active theme. */ if( isset( $_POST['reset'] ) ) { add_settings_error( 'cyberchimps_options', 'restore_defaults', __( 'Default options restored.', 'cyberchimps_core' ), 'updated fade' ); return cyberchimps_get_default_values(); } /* * Update Settings * * This used to check for $_POST['update'], but has been updated * to be compatible with the theme customizer introduced in WordPress 3.4 */ else { $clean = array(); $options = cyberchimps_get_fields(); foreach( $options as $option ) { if( !isset( $option['id'] ) ) { continue; } if( !isset( $option['type'] ) ) { continue; } $id = preg_replace( '/[^a-zA-Z0-9._\-]/', '', strtolower( $option['id'] ) ); // Set upload to false if it wasn't sent in the $_POST if( 'info' == $option['type'] && !isset( $input[$id] ) ) { $input[$id] = false; } // Set upload to false if it wasn't sent in the $_POST if( 'upload' == $option['type'] && !isset( $input[$id] ) ) { $input[$id] = false; } // Set radio to false if it wasn't sent in the $_POST if( 'radio' == $option['type'] && !isset( $input[$id] ) ) { $input[$id] = false; } // Set toggle to false if it wasn't sent in the $_POST if( 'toggle' == $option['type'] && !isset( $input[$id] ) ) { $input[$id] = false; } // Set checkbox to false if it wasn't sent in the $_POST if( 'checkbox' == $option['type'] && !isset( $input[$id] ) ) { $input[$id] = false; } // Set checkbox to false if it wasn't sent in the $_POST if( 'images' == $option['type'] && !isset( $input[$id] ) ) { $input[$id] = false; } // Set each item in the multicheck to false if it wasn't sent in the $_POST if( 'multicheck' == $option['type'] && !isset( $input[$id] ) ) { foreach( $option['options'] as $key => $value ) { $input[$id][$key] = false; } } // For a value to be submitted to database it must pass through a sanitization filter if( has_filter( 'cyberchimps_sanitize_' . $option['type'] ) ) { $clean[$id] = apply_filters( 'cyberchimps_sanitize_' . $option['type'], $input[$id], $option ); } } do_action( 'cyberchimps_options_before_save', $input ); add_settings_error( 'cyberchimps_options', 'save_options', __( 'Options saved.', 'cyberchimps_core' ), 'updated fade' ); return $clean; } } /** * Format Configuration Array. * * Get an array of all default values as set in * options.php. The 'id','std' and 'type' keys need * to be defined in the configuration array. In the * event that these keys are not present the option * will not be included in this function's output. * * @return array Rey-keyed options configuration array. * * @access private */ function cyberchimps_get_default_values() { $output = array(); $config = cyberchimps_get_fields(); foreach( (array)$config as $option ) { if( !isset( $option['id'] ) ) { continue; } if( !isset( $option['std'] ) ) { continue; } if( !isset( $option['type'] ) ) { continue; } if( has_filter( 'cyberchimps_sanitize_' . $option['type'] ) ) { $output[$option['id']] = apply_filters( 'cyberchimps_sanitize_' . $option['type'], $option['std'], $option ); } } return $output; } /** * Add Theme Options menu item to Admin Bar. */ function cyberchimps_admin_bar() { global $wp_admin_bar; $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'cyberchimps_options_page', 'title' => __( 'Theme Options', 'cyberchimps_core' ), 'href' => admin_url( 'themes.php?page=cyberchimps-theme-options' ) ) ); }