This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!

Untitled

By: SRD75 on Dec 26th, 2012  |  syntax: PHP  |  size: 13.04 KB  |  views: 36  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3. Description: A framework for building theme options.
  4. Author: Devin Price
  5. Author URI: http://www.wptheming.com
  6. License: GPLv2
  7. Version: 1.4
  8. */
  9.  
  10. /*
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  24. */
  25.  
  26. /* If the user can't edit theme options, no use running this plugin */
  27.  
  28. add_action('init', 'optionsframework_rolescheck' );
  29.  
  30. function optionsframework_rolescheck () {
  31.         if ( current_user_can( 'edit_theme_options' ) ) {
  32.                 // If the user can edit theme options, let the fun begin!
  33.                 add_action( 'admin_menu', 'optionsframework_add_page');
  34.                 add_action( 'admin_init', 'optionsframework_init' );
  35.                 add_action( 'admin_init', 'optionsframework_mlu_init' );
  36.                 add_action( 'wp_before_admin_bar_render', 'optionsframework_adminbar' );
  37.         }
  38. }
  39.  
  40. /* Loads the file for option sanitization */
  41.  
  42. add_action('init', 'optionsframework_load_sanitization' );
  43.  
  44. function optionsframework_load_sanitization() {
  45.         require_once dirname( __FILE__ ) . '/options-sanitize.php';
  46. }
  47.  
  48. /*
  49.  * Creates the settings in the database by looping through the array
  50.  * we supplied in options.php.  This is a neat way to do it since
  51.  * we won't have to save settings for headers, descriptions, or arguments.
  52.  *
  53.  * Read more about the Settings API in the WordPress codex:
  54.  * http://codex.wordpress.org/Settings_API
  55.  *
  56.  */
  57.  
  58. function optionsframework_init() {
  59.  
  60.         // Include the required files
  61.         require_once dirname( __FILE__ ) . '/options-interface.php';
  62.         require_once dirname( __FILE__ ) . '/options-medialibrary-uploader.php';
  63.        
  64.         // Loads the options array from the theme
  65.         if ( $optionsfile = locate_template( array('options.php') ) ) {
  66.                 require_once($optionsfile);
  67.         }
  68.         else if (file_exists( dirname( __FILE__ ) . '/options.php' ) ) {
  69.                 require_once dirname( __FILE__ ) . '/options.php';
  70.         }
  71.        
  72.         $optionsframework_settings = get_option('optionsframework' );
  73.        
  74.         // Updates the unique option id in the database if it has changed
  75.         optionsframework_option_name();
  76.        
  77.         // Gets the unique id, returning a default if it isn't defined
  78.         if ( isset($optionsframework_settings['id']) ) {
  79.                 $option_name = $optionsframework_settings['id'];
  80.         }
  81.         else {
  82.                 $option_name = 'optionsframework';
  83.         }
  84.        
  85.         // If the option has no saved data, load the defaults
  86.         if ( ! get_option($option_name) ) {
  87.                 optionsframework_setdefaults();
  88.         }
  89.        
  90.         // Registers the settings fields and callback
  91.         register_setting( 'optionsframework', $option_name, 'optionsframework_validate' );
  92.        
  93.         // Change the capability required to save the 'optionsframework' options group.
  94.         add_filter( 'option_page_capability_optionsframework', 'optionsframework_page_capability' );
  95. }
  96.  
  97. /**
  98.  * Ensures that a user with the 'edit_theme_options' capability can actually set the options
  99.  * See: http://core.trac.wordpress.org/ticket/14365
  100.  *
  101.  * @param string $capability The capability used for the page, which is manage_options by default.
  102.  * @return string The capability to actually use.
  103.  */
  104.  
  105. function optionsframework_page_capability( $capability ) {
  106.         return 'edit_theme_options';
  107. }
  108.  
  109. /*
  110.  * Adds default options to the database if they aren't already present.
  111.  * May update this later to load only on plugin activation, or theme
  112.  * activation since most people won't be editing the options.php
  113.  * on a regular basis.
  114.  *
  115.  * http://codex.wordpress.org/Function_Reference/add_option
  116.  *
  117.  */
  118.  
  119. function optionsframework_setdefaults() {
  120.        
  121.         $optionsframework_settings = get_option('optionsframework');
  122.  
  123.         // Gets the unique option id
  124.         $option_name = $optionsframework_settings['id'];
  125.        
  126.         /*
  127.          * Each theme will hopefully have a unique id, and all of its options saved
  128.          * as a separate option set.  We need to track all of these option sets so
  129.          * it can be easily deleted if someone wishes to remove the plugin and
  130.          * its associated data.  No need to clutter the database.  
  131.          *
  132.          */
  133.        
  134.         if ( isset($optionsframework_settings['knownoptions']) ) {
  135.                 $knownoptions =  $optionsframework_settings['knownoptions'];
  136.                 if ( !in_array($option_name, $knownoptions) ) {
  137.                         array_push( $knownoptions, $option_name );
  138.                         $optionsframework_settings['knownoptions'] = $knownoptions;
  139.                         update_option('optionsframework', $optionsframework_settings);
  140.                 }
  141.         } else {
  142.                 $newoptionname = array($option_name);
  143.                 $optionsframework_settings['knownoptions'] = $newoptionname;
  144.                 update_option('optionsframework', $optionsframework_settings);
  145.         }
  146.        
  147.         // Gets the default options data from the array in options.php
  148.         $options = optionsframework_options();
  149.        
  150.         // If the options haven't been added to the database yet, they are added now
  151.         $values = of_get_default_values();
  152.        
  153.         if ( isset($values) ) {
  154.                 add_option( $option_name, $values ); // Add option with default settings
  155.         }
  156. }
  157.  
  158. /* Add a subpage called "Theme Options" to the appearance menu. */
  159.  
  160. if ( !function_exists( 'optionsframework_add_page' ) ) {
  161.  
  162.         function optionsframework_add_page() {
  163.                 $of_page = add_theme_page(__('Theme Options', 'options_framework_theme'), __('Theme Options', 'options_framework_theme'), 'edit_theme_options', 'options-framework','optionsframework_page');
  164.                
  165.                 // Load the required CSS and javscript
  166.                 add_action('admin_enqueue_scripts', 'optionsframework_load_scripts');
  167.                 add_action( 'admin_print_styles-' . $of_page, 'optionsframework_load_styles' );
  168.         }
  169.        
  170. }
  171.  
  172. /* Loads the CSS */
  173.  
  174. function optionsframework_load_styles() {
  175.         wp_enqueue_style('optionsframework', OPTIONS_FRAMEWORK_DIRECTORY . 'css/optionsframework.css');
  176.         if ( !wp_style_is( 'wp-color-picker','registered' ) ) {
  177.                 wp_register_style('wp-color-picker', OPTIONS_FRAMEWORK_DIRECTORY . 'css/color-picker.min.css');
  178.         }
  179.         wp_enqueue_style( 'wp-color-picker' );
  180. }
  181.  
  182. /* Loads the javascript */
  183.  
  184. function optionsframework_load_scripts($hook) {
  185.  
  186.         if ( 'appearance_page_options-framework' != $hook )
  187.         return;
  188.  
  189.         // Enqueue colorpicker scripts for versions below 3.5
  190.         // for compatibility
  191.        
  192.         if ( !wp_script_is( 'wp-color-picker', 'registered' ) ) {
  193.                 wp_register_script( 'iris', OPTIONS_FRAMEWORK_DIRECTORY . 'js/iris.min.js', array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), false, 1 );
  194.                 wp_register_script( 'wp-color-picker', OPTIONS_FRAMEWORK_DIRECTORY . 'js/color-picker.min.js', array( 'jquery', 'iris' ) );
  195.                 $colorpicker_l10n = array(
  196.                         'clear' => __( 'Clear' ),
  197.                         'defaultString' => __( 'Default' ),
  198.                         'pick' => __( 'Select Color' )
  199.                 );
  200.                 wp_localize_script( 'wp-color-picker', 'wpColorPickerL10n', $colorpicker_l10n );
  201.         }
  202.        
  203.         // Enqueue custom option panel JS
  204.         wp_enqueue_script( 'options-custom', OPTIONS_FRAMEWORK_DIRECTORY . 'js/options-custom.js', array( 'jquery','wp-color-picker' ) );
  205.  
  206.         // Inline scripts from options-interface.php
  207.         add_action( 'admin_head', 'of_admin_head' );
  208. }
  209.  
  210. function of_admin_head() {
  211.         // Hook to add custom scripts
  212.         do_action( 'optionsframework_custom_scripts' );
  213. }
  214.  
  215. /*
  216.  * Builds out the options panel.
  217.  *
  218.  * If we were using the Settings API as it was likely intended we would use
  219.  * do_settings_sections here.  But as we don't want the settings wrapped in a table,
  220.  * we'll call our own custom optionsframework_fields.  See options-interface.php
  221.  * for specifics on how each individual field is generated.
  222.  *
  223.  * Nonces are provided using the settings_fields()
  224.  *
  225.  */
  226.  
  227. if ( !function_exists( 'optionsframework_page' ) ) :
  228. function optionsframework_page() {
  229.         settings_errors(); ?>
  230.  
  231.         <div id="optionsframework-wrap" class="wrap">
  232.     <?php screen_icon( 'themes' ); ?>
  233.     <h2 class="nav-tab-wrapper">
  234.         <?php echo optionsframework_tabs(); ?>
  235.     </h2>
  236.  
  237.     <div id="optionsframework-metabox" class="metabox-holder">
  238.             <div id="optionsframework" class="postbox">
  239.                         <form action="options.php" method="post">
  240.                         <?php settings_fields('optionsframework'); ?>
  241.                         <?php optionsframework_fields(); /* Settings */ ?>
  242.                         <div id="optionsframework-submit">
  243.                                 <input type="submit" class="button-primary" name="update" value="<?php esc_attr_e( 'Save Options', 'options_framework_theme' ); ?>" />
  244.                                 <input type="submit" class="reset-button button-secondary" name="reset" value="<?php esc_attr_e( 'Restore Defaults', 'options_framework_theme' ); ?>" onclick="return confirm( '<?php print esc_js( __( 'Click OK to reset. Any theme settings will be lost!', 'options_framework_theme' ) ); ?>' );" />
  245.                                 <div class="clear"></div>
  246.                         </div>
  247.                         </form>
  248.                 </div> <!-- / #container -->
  249.         </div>
  250.         <?php do_action('optionsframework_after'); ?>
  251.         </div> <!-- / .wrap -->
  252.        
  253. <?php
  254. }
  255. endif;
  256.  
  257. /**
  258.  * Validate Options.
  259.  *
  260.  * This runs after the submit/reset button has been clicked and
  261.  * validates the inputs.
  262.  *
  263.  * @uses $_POST['reset'] to restore default options
  264.  */
  265. function optionsframework_validate( $input ) {
  266.  
  267.         /*
  268.          * Restore Defaults.
  269.          *
  270.          * In the event that the user clicked the "Restore Defaults"
  271.          * button, the options defined in the theme's options.php
  272.          * file will be added to the option for the active theme.
  273.          */
  274.  
  275.         if ( isset( $_POST['reset'] ) ) {
  276.                 add_settings_error( 'options-framework', 'restore_defaults', __( 'Default options restored.', 'options_framework_theme' ), 'updated fade' );
  277.                 return of_get_default_values();
  278.         }
  279.        
  280.         /*
  281.          * Update Settings
  282.          *
  283.          * This used to check for $_POST['update'], but has been updated
  284.          * to be compatible with the theme customizer introduced in WordPress 3.4
  285.          */
  286.          
  287.         $clean = array();
  288.         $options = optionsframework_options();
  289.         foreach ( $options as $option ) {
  290.  
  291.                 if ( ! isset( $option['id'] ) ) {
  292.                         continue;
  293.                 }
  294.  
  295.                 if ( ! isset( $option['type'] ) ) {
  296.                         continue;
  297.                 }
  298.  
  299.                 $id = preg_replace( '/[^a-zA-Z0-9._\-]/', '', strtolower( $option['id'] ) );
  300.  
  301.                 // Set checkbox to false if it wasn't sent in the $_POST
  302.                 if ( 'checkbox' == $option['type'] && ! isset( $input[$id] ) ) {
  303.                         $input[$id] = false;
  304.                 }
  305.  
  306.                 // Set each item in the multicheck to false if it wasn't sent in the $_POST
  307.                 if ( 'multicheck' == $option['type'] && ! isset( $input[$id] ) ) {
  308.                         foreach ( $option['options'] as $key => $value ) {
  309.                                 $input[$id][$key] = false;
  310.                         }
  311.                 }
  312.  
  313.                 // For a value to be submitted to database it must pass through a sanitization filter
  314.                 if ( has_filter( 'of_sanitize_' . $option['type'] ) ) {
  315.                         $clean[$id] = apply_filters( 'of_sanitize_' . $option['type'], $input[$id], $option );
  316.                 }
  317.         }
  318.        
  319.         // Hook to run after validation
  320.         do_action( 'optionsframework_after_validate', $clean );
  321.        
  322.         return $clean;
  323. }
  324.  
  325. /**
  326.  * Display message when options have been saved
  327.  */
  328.  
  329. function optionsframework_save_options_notice() {
  330.         add_settings_error( 'options-framework', 'save_options', __( 'Options saved.', 'options_framework_theme' ), 'updated fade' );
  331. }
  332.  
  333. add_action( 'optionsframework_after_validate', 'optionsframework_save_options_notice' );
  334.  
  335. /**
  336.  * Format Configuration Array.
  337.  *
  338.  * Get an array of all default values as set in
  339.  * options.php. The 'id','std' and 'type' keys need
  340.  * to be defined in the configuration array. In the
  341.  * event that these keys are not present the option
  342.  * will not be included in this function's output.
  343.  *
  344.  * @return    array     Rey-keyed options configuration array.
  345.  *
  346.  * @access    private
  347.  */
  348.  
  349. function of_get_default_values() {
  350.         $output = array();
  351.         $config = optionsframework_options();
  352.         foreach ( (array) $config as $option ) {
  353.                 if ( ! isset( $option['id'] ) ) {
  354.                         continue;
  355.                 }
  356.                 if ( ! isset( $option['std'] ) ) {
  357.                         continue;
  358.                 }
  359.                 if ( ! isset( $option['type'] ) ) {
  360.                         continue;
  361.                 }
  362.                 if ( has_filter( 'of_sanitize_' . $option['type'] ) ) {
  363.                         $output[$option['id']] = apply_filters( 'of_sanitize_' . $option['type'], $option['std'], $option );
  364.                 }
  365.         }
  366.         return $output;
  367. }
  368.  
  369. /**
  370.  * Add Theme Options menu item to Admin Bar.
  371.  */
  372.  
  373. function optionsframework_adminbar() {
  374.  
  375.         global $wp_admin_bar;
  376.  
  377.         $wp_admin_bar->add_menu( array(
  378.                         'parent' => 'appearance',
  379.                         'id' => 'of_theme_options',
  380.                         'title' => __( 'Theme Options', 'options_framework_theme' ),
  381.                         'href' => admin_url( 'themes.php?page=options-framework' )
  382.                 ));
  383. }
  384.  
  385. if ( ! function_exists( 'of_get_option' ) ) {
  386.  
  387.         /**
  388.          * Get Option.
  389.          *
  390.          * Helper function to return the theme option value.
  391.          * If no value has been saved, it returns $default.
  392.          * Needed because options are saved as serialized strings.
  393.          */
  394.          
  395.         function of_get_option( $name, $default = false ) {
  396.                 $config = get_option( 'optionsframework' );
  397.  
  398.                 if ( ! isset( $config['id'] ) ) {
  399.                         return $default;
  400.                 }
  401.  
  402.                 $options = get_option( $config['id'] );
  403.  
  404.                 if ( isset( $options[$name] ) ) {
  405.                         return $options[$name];
  406.                 }
  407.  
  408.                 return $default;
  409.         }
  410. }
clone this paste RAW Paste Data