Options_Framework_Admin

The SKT Black Options Framework Admin class.

Defined (1)

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

/inc/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' => __( 'Black Theme Options', 'skt-black' ),  
  73. 'menu_title' => __( 'Theme Options', 'skt-black' ),  
  74. 'capability' => 'edit_theme_options',  
  75. 'menu_slug' => 'black-options',  
  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'] ); ?></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 style="padding-bottom:10px;"><a href="<?php echo esc_url(SKT_PRO_THEME_URL);?>" target="_blank"><?php _e('Buy PRO version for only $39 with more features', 'skt-black') ;?></a></div> 
  169. <div id="optionsframework" class="postbox" style="width:72%; float:left;"> 
  170. <form action="options.php" method="post"> 
  171. <?php settings_fields( 'optionsframework' ); ?> 
  172. <?php Options_Framework_Interface::optionsframework_fields(); /** Settings */ ?> 
  173.  
  174. <div id="optionsframework-submit"> 
  175. <input type="submit" class="button-primary" name="update" value="<?php esc_attr_e( 'Save Options', 'skt-black' ); ?>" /> 
  176. <input type="submit" class="reset-button button-secondary" name="reset" value="<?php esc_attr_e( 'Restore Defaults', 'skt-black' ); ?>" onclick="return confirm( '<?php print esc_js( __( 'Click OK to reset. Any theme settings will be lost!', 'skt-black' ) ); ?>' );" /> 
  177. <div class="clear"></div> 
  178. </div> 
  179. </form> 
  180. </div> <!-- / #container --> 
  181. <div style="width:25%; float:right;" class="postbox-container side"> 
  182. <div class="meta-box-sortables ui-sortable"> 
  183. <div class="postbox"> 
  184. <h3 class="hndle"><span><?php _e('Our Themes', 'skt-black'); ?></span></h3> 
  185. <div class="inside"> 
  186. <p><a target="_blank" href="<?php echo esc_url(SKT_THEME_URL); ?>"><img style="max-width:100%" src="<?php echo get_template_directory_uri()?>/images/sktskill.jpg"></a></p> 
  187. </div> 
  188. </div> 
  189. <div class="postbox"> 
  190. <h3 class="hndle"><span><?php _e('Need more features?', 'skt-black'); ?></span></h3> 
  191. <div class="inside"> 
  192. <p><em><a href="<?php echo esc_url(SKT_PRO_THEME_URL);?>" target="_blank"><?php _e('Buy PRO version for only $39 with more features', 'skt-black'); ?></a></em></p> 
  193. </div> 
  194. </div> 
  195. <div class="postbox"> 
  196. <h3 class="hndle"><span><?php _e('Documentation', 'skt-black'); ?></span></h3> 
  197. <div class="inside"> 
  198. <p><em><?php _e('For documentation and support check this link', 'skt-black'); ?> : <strong><a href="<?php echo esc_url(SKT_THEME_DOC); ?>" target="_blank"><?php _e('Black Theme Documentation', 'skt-black'); ?></a></strong></em></p> 
  199. </div> 
  200. </div> 
  201.  
  202. </div> 
  203. </div> 
  204. </div> 
  205. <?php do_action( 'optionsframework_after' ); ?> 
  206. </div> <!-- / .wrap --> 
  207.  
  208. <?php 
  209.  
  210. /** 
  211. * Validate Options. 
  212. * This runs after the submit/reset button has been clicked and 
  213. * validates the inputs. 
  214. * @uses $_POST['reset'] to restore default options 
  215. */ 
  216. function validate_options( $input ) { 
  217.  
  218. /** 
  219. * Restore Defaults. 
  220. * In the event that the user clicked the "Restore Defaults" 
  221. * button, the options defined in the theme's options.php 
  222. * file will be added to the option for the active theme. 
  223. */ 
  224.  
  225. if ( isset( $_POST['reset'] ) ) { 
  226. add_settings_error( 'options-framework', 'restore_defaults', __( 'Default options restored.', 'skt-black' ), 'updated fade' ); 
  227. return $this->get_default_values(); 
  228.  
  229. /** 
  230. * Update Settings 
  231. * This used to check for $_POST['update'], but has been updated 
  232. * to be compatible with the theme customizer introduced in WordPress 3.4 
  233. */ 
  234.  
  235. $clean = array(); 
  236. $options = & Options_Framework::_optionsframework_options(); 
  237. foreach ( $options as $option ) { 
  238.  
  239. if ( ! isset( $option['id'] ) ) { 
  240. continue; 
  241.  
  242. if ( ! isset( $option['type'] ) ) { 
  243. continue; 
  244.  
  245. $id = preg_replace( '/[^a-zA-Z0-9._\-]/', '', strtolower( $option['id'] ) ); 
  246.  
  247. // Set checkbox to false if it wasn't sent in the $_POST 
  248. if ( 'checkbox' == $option['type'] && ! isset( $input[$id] ) ) { 
  249. $input[$id] = false; 
  250.  
  251. // Set each item in the multicheck to false if it wasn't sent in the $_POST 
  252. if ( 'multicheck' == $option['type'] && ! isset( $input[$id] ) ) { 
  253. foreach ( $option['options'] as $key => $value ) { 
  254. $input[$id][$key] = false; 
  255.  
  256. // For a value to be submitted to database it must pass through a sanitization filter 
  257. if ( has_filter( 'of_sanitize_' . $option['type'] ) ) { 
  258. $clean[$id] = apply_filters( 'of_sanitize_' . $option['type'], $input[$id], $option ); 
  259.  
  260. // Hook to run after validation 
  261. do_action( 'optionsframework_after_validate', $clean ); 
  262.  
  263. return $clean; 
  264.  
  265. /** 
  266. * Display message when options have been saved 
  267. */ 
  268.  
  269. function save_options_notice() { 
  270. add_settings_error( 'options-framework', 'save_options', __( 'Options saved.', 'skt-black' ), 'updated fade' ); 
  271.  
  272. /** 
  273. * Get the default values for all the theme options 
  274. * Get an array of all default values as set in 
  275. * options.php. The 'id', 'std' and 'type' keys need 
  276. * to be defined in the configuration array. In the 
  277. * event that these keys are not present the option 
  278. * will not be included in this function's output. 
  279. * @return array Re-keyed options configuration array. 
  280. */ 
  281. function get_default_values() { 
  282. $output = array(); 
  283. $config = & Options_Framework::_optionsframework_options(); 
  284. foreach ( (array) $config as $option ) { 
  285. if ( ! isset( $option['id'] ) ) { 
  286. continue; 
  287. if ( ! isset( $option['std'] ) ) { 
  288. continue; 
  289. if ( ! isset( $option['type'] ) ) { 
  290. continue; 
  291. if ( has_filter( 'of_sanitize_' . $option['type'] ) ) { 
  292. $output[$option['id']] = apply_filters( 'of_sanitize_' . $option['type'], $option['std'], $option ); 
  293. return $output; 
  294.  
  295. /** 
  296. * Add options menu item to admin bar 
  297. */ 
  298.  
  299. function optionsframework_admin_bar() { 
  300.  
  301. $menu = $this->menu_settings(); 
  302.  
  303. global $wp_admin_bar; 
  304.  
  305. if ( 'menu' == $menu['mode'] ) { 
  306. $href = admin_url( 'admin.php?page=' . $menu['menu_slug'] ); 
  307. } else { 
  308. $href = admin_url( 'themes.php?page=' . $menu['menu_slug'] ); 
  309.  
  310. $args = array( 
  311. 'parent' => 'appearance',  
  312. 'id' => 'of_theme_options',  
  313. 'title' => $menu['menu_title'],  
  314. 'href' => $href 
  315. ); 
  316.  
  317. $wp_admin_bar->add_menu( apply_filters( 'optionsframework_admin_bar', $args ) ); 
  318.