Options_Framework_Admin

The Onetone Options Framework Admin class.

Defined (1)

The class is defined in the following location(s).

/admin/includes/class-options-framework-admin.php  
  1. class Options_Framework_Admin { 
  2.  
  3. /** 
  4. * Page hook for the options screen 
  5. * @since 1.7.0 
  6. * @type string 
  7. */ 
  8. protected $options_screen = null; 
  9.  
  10. /** 
  11. * Hook in the scripts and styles 
  12. * @since 1.7.0 
  13. */ 
  14. public function init() { 
  15.  
  16. // Gets options to load 
  17. $options = & Options_Framework::_optionsframework_options(); 
  18.  
  19. // Checks if options are available 
  20. if ( $options ) { 
  21.  
  22. // Add the options page and menu item. 
  23. add_action( 'admin_menu', array( $this, 'add_custom_options_page' ) ); 
  24.  
  25. // Add the required scripts and styles 
  26. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); 
  27. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); 
  28.  
  29. // Settings need to be registered after admin_init 
  30. add_action( 'admin_init', array( $this, 'settings_init' ) ); 
  31.  
  32. // Adds options menu to the admin bar 
  33. add_action( 'wp_before_admin_bar_render', array( $this, 'optionsframework_admin_bar' ) ); 
  34.  
  35.  
  36.  
  37. /** 
  38. * Registers the settings 
  39. * @since 1.7.0 
  40. */ 
  41. function settings_init() { 
  42.  
  43. // Get the option name 
  44. $options_framework = new Options_Framework; 
  45. $name = $options_framework->get_option_name(); 
  46.  
  47. // Registers the settings fields and callback 
  48. register_setting( 'optionsframework', $name, array ( $this, 'validate_options' ) ); 
  49.  
  50. // Displays notice after options save 
  51. add_action( 'optionsframework_after_validate', array( $this, 'save_options_notice' ) ); 
  52.  
  53.  
  54. /** 
  55. * Define menu options 
  56. * Examples usage: 
  57. * add_filter( 'optionsframework_menu', function( $menu ) { 
  58. * $menu['page_title'] = 'The Options'; 
  59. * $menu['menu_title'] = 'The Options'; 
  60. * return $menu; 
  61. * }); 
  62. * @since 1.7.0 
  63. */ 
  64. static function menu_settings() { 
  65.  
  66. $menu = array( 
  67.  
  68. // Modes: submenu, menu 
  69. 'mode' => 'submenu',  
  70.  
  71. // Submenu default settings 
  72. 'page_title' => __( 'Theme Options', 'onetone' ),  
  73. 'menu_title' => __( 'Theme Options', 'onetone' ),  
  74. 'capability' => 'edit_theme_options',  
  75. 'menu_slug' => 'options-framework',  
  76. 'parent_slug' => 'themes.php',  
  77.  
  78. // Menu default settings 
  79. 'icon_url' => 'dashicons-admin-generic',  
  80. 'position' => '61' 
  81.  
  82. ); 
  83.  
  84. return apply_filters( 'optionsframework_menu', $menu ); 
  85.  
  86. /** 
  87. * Add a subpage called "Theme Options" to the appearance menu. 
  88. * @since 1.7.0 
  89. */ 
  90. function add_custom_options_page() { 
  91.  
  92. $menu = $this->menu_settings(); 
  93.  
  94. // If you want a top level menu, see this Gist: 
  95. // https://gist.github.com/devinsays/884d6abe92857a329d99 
  96.  
  97. // Code removed because it conflicts with .org theme check. 
  98.  
  99. $this->options_screen = add_theme_page( 
  100. $menu['page_title'],  
  101. $menu['menu_title'],  
  102. $menu['capability'],  
  103. $menu['menu_slug'],  
  104. array( $this, 'options_page' ) 
  105. ); 
  106.  
  107.  
  108. /** 
  109. * Loads the required stylesheets 
  110. * @since 1.7.0 
  111. */ 
  112.  
  113. function enqueue_admin_styles( $hook ) { 
  114.  
  115. if ( $this->options_screen != $hook ) 
  116. return; 
  117.  
  118. wp_enqueue_style( 'optionsframework', OPTIONS_FRAMEWORK_DIRECTORY . 'css/optionsframework.css', array(), Options_Framework::VERSION ); 
  119. wp_enqueue_style( 'wp-color-picker' ); 
  120.  
  121. /** 
  122. * Loads the required javascript 
  123. * @since 1.7.0 
  124. */ 
  125. function enqueue_admin_scripts( $hook ) { 
  126.  
  127. if ( $this->options_screen != $hook ) 
  128. return; 
  129.  
  130. // Enqueue custom option panel JS 
  131. wp_enqueue_script( 
  132. 'options-custom',  
  133. OPTIONS_FRAMEWORK_DIRECTORY . 'js/options-custom.js',  
  134. array( 'jquery', 'wp-color-picker' ),  
  135. Options_Framework::VERSION 
  136. ); 
  137.  
  138. // Inline scripts from options-interface.php 
  139. add_action( 'admin_head', array( $this, 'of_admin_head' ) ); 
  140.  
  141. function of_admin_head() { 
  142. // Hook to add custom scripts 
  143. do_action( 'optionsframework_custom_scripts' ); 
  144.  
  145. /** 
  146. * Builds out the options panel. 
  147. * If we were using the Settings API as it was intended we would use 
  148. * do_settings_sections here. But as we don't want the settings wrapped in a table,  
  149. * we'll call our own custom optionsframework_fields. See options-interface.php 
  150. * for specifics on how each individual field is generated. 
  151. * Nonces are provided using the settings_fields() 
  152. * @since 1.7.0 
  153. */ 
  154. function options_page() { ?> 
  155.  
  156. <div id="optionsframework-wrap" class="wrap"> 
  157.  
  158. <?php $menu = $this->menu_settings(); ?> 
  159. <h2><?php echo esc_html( $menu['page_title'] ); ?> <?php do_action( 'optionsframework_page_title_after' ); ?></h2> 
  160.  
  161. <h2 class="nav-tab-wrapper"> 
  162. <?php echo Options_Framework_Interface::optionsframework_tabs(); ?> 
  163. </h2> 
  164.  
  165. <?php settings_errors( 'options-framework' ); ?> 
  166.  
  167. <div id="optionsframework-metabox" class="metabox-holder"> 
  168. <div id="optionsframework" class="postbox"> 
  169. <form action="options.php" method="post"> 
  170. <?php settings_fields( 'optionsframework' ); ?> 
  171. <?php Options_Framework_Interface::optionsframework_fields(); /** Settings */ ?> 
  172. <div id="optionsframework-submit"> 
  173. <input type="submit" class="button-primary" name="update" value="<?php esc_attr_e( 'Save Options', 'onetone' ); ?>" /> 
  174. <input type="submit" class="reset-button button-secondary" name="reset" value="<?php esc_attr_e( 'Restore Defaults', 'onetone' ); ?>" onclick="return confirm( '<?php print esc_js( __( 'Click OK to reset. Any theme settings will be lost!', 'onetone' ) ); ?>' );" /> 
  175. <div class="clear"></div> 
  176. </div> 
  177. </form> 
  178. </div> <!-- / #container --> 
  179. </div> 
  180. <?php do_action( 'optionsframework_after' ); ?> 
  181. </div> <!-- / .wrap --> 
  182.  
  183. <?php 
  184.  
  185. /** 
  186. * Validate Options. 
  187. * This runs after the submit/reset button has been clicked and 
  188. * validates the inputs. 
  189. * @uses $_POST['reset'] to restore default options 
  190. */ 
  191. function validate_options( $input ) { 
  192.  
  193. /** 
  194. * Restore Defaults. 
  195. * In the event that the user clicked the "Restore Defaults" 
  196. * button, the options defined in the theme's options.php 
  197. * file will be added to the option for the active theme. 
  198. */ 
  199.  
  200. if ( isset( $_POST['reset'] ) ) { 
  201. add_settings_error( 'options-framework', 'restore_defaults', __( 'Default options restored.', 'onetone' ), 'updated fade' ); 
  202. return $this->get_default_values(); 
  203.  
  204. /** 
  205. * Update Settings 
  206. * This used to check for $_POST['update'], but has been updated 
  207. * to be compatible with the theme customizer introduced in WordPress 3.4 
  208. */ 
  209.  
  210. $clean = array(); 
  211. $options = & Options_Framework::_optionsframework_options(); 
  212.  
  213. foreach ( $options as $option ) { 
  214.  
  215. if ( ! isset( $option['id'] ) ) { 
  216. continue; 
  217.  
  218. if ( ! isset( $option['type'] ) ) { 
  219. continue; 
  220.  
  221. $id = preg_replace( '/[^a-zA-Z0-9._\-]/', '', strtolower( $option['id'] ) ); 
  222.  
  223. // Set checkbox to false if it wasn't sent in the $_POST 
  224. if ( 'checkbox' == $option['type'] && ! isset( $input[$id] ) ) { 
  225. $input[$id] = false; 
  226.  
  227. // Set each item in the multicheck to false if it wasn't sent in the $_POST 
  228. if ( 'multicheck' == $option['type'] && ! isset( $input[$id] ) ) { 
  229. foreach ( $option['options'] as $key => $value ) { 
  230. $input[$id][$key] = false; 
  231.  
  232. // For a value to be submitted to database it must pass through a sanitization filter 
  233. if ( has_filter( 'of_sanitize_' . $option['type'] ) ) { 
  234. $clean[$id] = apply_filters( 'of_sanitize_' . $option['type'], $input[$id], $option ); 
  235.  
  236. // Hook to run after validation 
  237. do_action( 'optionsframework_after_validate', $clean ); 
  238.  
  239. return $clean; 
  240.  
  241. /** 
  242. * Display message when options have been saved 
  243. */ 
  244.  
  245. function save_options_notice() { 
  246.  
  247. add_settings_error( 'options-framework', 'save_options', __( 'Options saved.', 'onetone' ), 'updated fade' ); 
  248.  
  249.  
  250.  
  251. /** 
  252. * Get the default values for all the theme options 
  253. * Get an array of all default values as set in 
  254. * options.php. The 'id', 'std' and 'type' keys need 
  255. * to be defined in the configuration array. In the 
  256. * event that these keys are not present the option 
  257. * will not be included in this function's output. 
  258. * @return array Re-keyed options configuration array. 
  259. */ 
  260. function get_default_values() { 
  261. global $onetone_restore; 
  262. $output = array(); 
  263. $config = & Options_Framework::_optionsframework_options(); 
  264. foreach ( (array) $config as $option ) { 
  265. if ( ! isset( $option['id'] ) ) { 
  266. continue; 
  267. if ( ! isset( $option['std'] ) ) { 
  268. continue; 
  269. if ( ! isset( $option['type'] ) ) { 
  270. continue; 
  271. if ( has_filter( 'of_sanitize_' . $option['type'] ) ) { 
  272. $output[$option['id']] = apply_filters( 'of_sanitize_' . $option['type'], $option['std'], $option ); 
  273. return $output; 
  274.  
  275. /** 
  276. * Add options menu item to admin bar 
  277. */ 
  278.  
  279. function optionsframework_admin_bar() { 
  280.  
  281. $menu = $this->menu_settings(); 
  282.  
  283. global $wp_admin_bar; 
  284.  
  285. if ( 'menu' == $menu['mode'] ) { 
  286. $href = admin_url( 'admin.php?page=' . $menu['menu_slug'] ); 
  287. } else { 
  288. $href = admin_url( 'themes.php?page=' . $menu['menu_slug'] ); 
  289.  
  290. $args = array( 
  291. 'parent' => 'appearance',  
  292. 'id' => 'of_theme_options',  
  293. 'title' => $menu['menu_title'],  
  294. 'href' => $href 
  295. ); 
  296.  
  297. $wp_admin_bar->add_menu( apply_filters( 'optionsframework_admin_bar', $args ) ); 
  298.