Options_Framework_Admin

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