PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/themes/wp-foundation/admin/options-framework.php

https://gitlab.com/Blueprint-Marketing/interoccupy.net
PHP | 394 lines | 186 code | 79 blank | 129 comment | 32 complexity | a435a2480594411dd61d0a2e62106061 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: Options Framework
  4. Plugin URI: http://www.wptheming.com
  5. Description: A framework for building theme options.
  6. Version: 0.8
  7. Author: Devin Price
  8. Author URI: http://www.wptheming.com
  9. License: GPLv2
  10. */
  11. /*
  12. This program is free software; you can redistribute it and/or
  13. modify it under the terms of the GNU General Public License
  14. as published by the Free Software Foundation; either version 2
  15. of the License, or (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  23. */
  24. /* Basic plugin definitions */
  25. define('OPTIONS_FRAMEWORK_VERSION', '0.9');
  26. /* Make sure we don't expose any info if called directly */
  27. if ( !function_exists( 'add_action' ) ) {
  28. echo "Hi there! I'm just a little plugin, don't mind me.";
  29. exit;
  30. }
  31. /* If the user can't edit theme options, no use running this plugin */
  32. add_action('init', 'optionsframework_rolescheck' );
  33. function optionsframework_rolescheck () {
  34. if ( current_user_can( 'edit_theme_options' ) ) {
  35. // If the user can edit theme options, let the fun begin!
  36. add_action( 'admin_menu', 'optionsframework_add_page');
  37. add_action( 'admin_init', 'optionsframework_init' );
  38. add_action( 'admin_init', 'optionsframework_mlu_init' );
  39. }
  40. }
  41. /* Loads the file for option sanitization */
  42. add_action('init', 'optionsframework_load_sanitization' );
  43. function optionsframework_load_sanitization() {
  44. require_once dirname( __FILE__ ) . '/options-sanitize.php';
  45. }
  46. /*
  47. * Creates the settings in the database by looping through the array
  48. * we supplied in options.php. This is a neat way to do it since
  49. * we won't have to save settings for headers, descriptions, or arguments.
  50. *
  51. * Read more about the Settings API in the WordPress codex:
  52. * http://codex.wordpress.org/Settings_API
  53. *
  54. */
  55. function optionsframework_init() {
  56. // Include the required files
  57. require_once dirname( __FILE__ ) . '/options-interface.php';
  58. require_once dirname( __FILE__ ) . '/options-medialibrary-uploader.php';
  59. // Loads the options array from the theme
  60. if ( $optionsfile = locate_template( array('options.php') ) ) {
  61. require_once($optionsfile);
  62. }
  63. else if (file_exists( dirname( __FILE__ ) . '/options.php' ) ) {
  64. require_once dirname( __FILE__ ) . '/options.php';
  65. }
  66. $optionsframework_settings = get_option('optionsframework' );
  67. // Updates the unique option id in the database if it has changed
  68. optionsframework_option_name();
  69. // Gets the unique id, returning a default if it isn't defined
  70. if ( isset($optionsframework_settings['id']) ) {
  71. $option_name = $optionsframework_settings['id'];
  72. }
  73. else {
  74. $option_name = 'optionsframework';
  75. }
  76. // If the option has no saved data, load the defaults
  77. if ( ! get_option($option_name) ) {
  78. optionsframework_setdefaults();
  79. }
  80. // Registers the settings fields and callback
  81. register_setting( 'optionsframework', $option_name, 'optionsframework_validate' );
  82. }
  83. /*
  84. * Adds default options to the database if they aren't already present.
  85. * May update this later to load only on plugin activation, or theme
  86. * activation since most people won't be editing the options.php
  87. * on a regular basis.
  88. *
  89. * http://codex.wordpress.org/Function_Reference/add_option
  90. *
  91. */
  92. function optionsframework_setdefaults() {
  93. $optionsframework_settings = get_option('optionsframework');
  94. // Gets the unique option id
  95. $option_name = $optionsframework_settings['id'];
  96. /*
  97. * Each theme will hopefully have a unique id, and all of its options saved
  98. * as a separate option set. We need to track all of these option sets so
  99. * it can be easily deleted if someone wishes to remove the plugin and
  100. * its associated data. No need to clutter the database.
  101. *
  102. */
  103. if ( isset($optionsframework_settings['knownoptions']) ) {
  104. $knownoptions = $optionsframework_settings['knownoptions'];
  105. if ( !in_array($option_name, $knownoptions) ) {
  106. array_push( $knownoptions, $option_name );
  107. $optionsframework_settings['knownoptions'] = $knownoptions;
  108. update_option('optionsframework', $optionsframework_settings);
  109. }
  110. } else {
  111. $newoptionname = array($option_name);
  112. $optionsframework_settings['knownoptions'] = $newoptionname;
  113. update_option('optionsframework', $optionsframework_settings);
  114. }
  115. // Gets the default options data from the array in options.php
  116. $options = optionsframework_options();
  117. // If the options haven't been added to the database yet, they are added now
  118. $values = of_get_default_values();
  119. if ( isset($values) ) {
  120. add_option( $option_name, $values ); // Add option with default settings
  121. }
  122. }
  123. /* Add a subpage called "Theme Options" to the appearance menu. */
  124. if ( !function_exists( 'optionsframework_add_page' ) ) {
  125. function optionsframework_add_page() {
  126. $of_page = add_theme_page('Theme Options', 'Theme Options', 'edit_theme_options', 'options-framework','optionsframework_page');
  127. // Adds actions to hook in the required css and javascript
  128. add_action("admin_print_styles-$of_page",'optionsframework_load_styles');
  129. add_action("admin_print_scripts-$of_page", 'optionsframework_load_scripts');
  130. }
  131. }
  132. /* Loads the CSS */
  133. function optionsframework_load_styles() {
  134. wp_enqueue_style('admin-style', OPTIONS_FRAMEWORK_DIRECTORY.'css/admin-style.css');
  135. wp_enqueue_style('color-picker', OPTIONS_FRAMEWORK_DIRECTORY.'css/colorpicker.css');
  136. }
  137. /* Loads the javascript */
  138. function optionsframework_load_scripts() {
  139. // Inline scripts from options-interface.php
  140. add_action('admin_head', 'of_admin_head');
  141. // Enqueued scripts
  142. wp_enqueue_script('jquery-ui-core');
  143. wp_enqueue_script('color-picker', OPTIONS_FRAMEWORK_DIRECTORY.'js/colorpicker.js', array('jquery'));
  144. wp_enqueue_script('options-custom', OPTIONS_FRAMEWORK_DIRECTORY.'js/options-custom.js', array('jquery'));
  145. }
  146. function of_admin_head() {
  147. // Hook to add custom scripts
  148. do_action( 'optionsframework_custom_scripts' );
  149. }
  150. /*
  151. * Builds out the options panel.
  152. *
  153. * If we were using the Settings API as it was likely intended we would use
  154. * do_settings_sections here. But as we don't want the settings wrapped in a table,
  155. * we'll call our own custom optionsframework_fields. See options-interface.php
  156. * for specifics on how each individual field is generated.
  157. *
  158. * Nonces are provided using the settings_fields()
  159. *
  160. */
  161. if ( !function_exists( 'optionsframework_page' ) ) {
  162. function optionsframework_page() {
  163. $return = optionsframework_fields();
  164. settings_errors();
  165. ?>
  166. <div class="wrap">
  167. <div class="icon32" id="options_ico"><br /></div>
  168. <?php //screen_icon( 'themes' );
  169. ?>
  170. <h2 class="nav-tab-wrapper">
  171. <?php echo $return[1]; ?>
  172. </h2>
  173. <div class="metabox-holder">
  174. <div id="optionsframework" class="postbox">
  175. <form action="options.php" method="post">
  176. <?php settings_fields('optionsframework'); ?>
  177. <?php echo $return[0]; /* Settings */ ?>
  178. <div id="optionsframework-submit">
  179. <input type="submit" class="button-primary" name="update" value="<?php esc_attr_e( 'Save Options' ); ?>" />
  180. <input type="submit" class="reset-button button-secondary" name="reset" value="<?php esc_attr_e( 'Restore Defaults' ); ?>" onclick="return confirm( '<?php print esc_js( __( 'Click OK to reset. Any theme settings will be lost!' ) ); ?>' );" />
  181. <div class="clear"></div>
  182. </div>
  183. </form>
  184. </div> <!-- / #container -->
  185. <p>Brought to you by <a href="http://320press.com" target="_blank">320press</a>.</p>
  186. </div>
  187. </div> <!-- / .wrap -->
  188. <?php
  189. }
  190. }
  191. /**
  192. * Validate Options.
  193. *
  194. * This runs after the submit/reset button has been clicked and
  195. * validates the inputs.
  196. *
  197. * @uses $_POST['reset']
  198. * @uses $_POST['update']
  199. */
  200. function optionsframework_validate( $input ) {
  201. /*
  202. * Restore Defaults.
  203. *
  204. * In the event that the user clicked the "Restore Defaults"
  205. * button, the options defined in the theme's options.php
  206. * file will be added to the option for the active theme.
  207. */
  208. if ( isset( $_POST['reset'] ) ) {
  209. add_settings_error( 'options-framework', 'restore_defaults', __( 'Default options restored.', 'optionsframework' ), 'updated fade' );
  210. return of_get_default_values();
  211. }
  212. /*
  213. * Udpdate Settings.
  214. */
  215. if ( isset( $_POST['update'] ) ) {
  216. $clean = array();
  217. $options = optionsframework_options();
  218. foreach ( $options as $option ) {
  219. if ( ! isset( $option['id'] ) ) {
  220. continue;
  221. }
  222. if ( ! isset( $option['type'] ) ) {
  223. continue;
  224. }
  225. $id = preg_replace( '/[^a-zA-Z0-9._\-]/', '', strtolower( $option['id'] ) );
  226. // Set checkbox to false if it wasn't sent in the $_POST
  227. if ( 'checkbox' == $option['type'] && ! isset( $input[$id] ) ) {
  228. $input[$id] = '0';
  229. }
  230. // Set each item in the multicheck to false if it wasn't sent in the $_POST
  231. if ( 'multicheck' == $option['type'] && ! isset( $input[$id] ) ) {
  232. foreach ( $option['options'] as $key => $value ) {
  233. $input[$id][$key] = '0';
  234. }
  235. }
  236. // For a value to be submitted to database it must pass through a sanitization filter
  237. if ( has_filter( 'of_sanitize_' . $option['type'] ) ) {
  238. $clean[$id] = apply_filters( 'of_sanitize_' . $option['type'], $input[$id], $option );
  239. }
  240. }
  241. add_settings_error( 'options-framework', 'save_options', __( 'Options saved.', 'optionsframework' ), 'updated fade' );
  242. return $clean;
  243. }
  244. /*
  245. * Request Not Recognized.
  246. */
  247. return of_get_default_values();
  248. }
  249. /**
  250. * Format Configuration Array.
  251. *
  252. * Get an array of all default values as set in
  253. * options.php. The 'id','std' and 'type' keys need
  254. * to be defined in the configuration array. In the
  255. * event that these keys are not present the option
  256. * will not be included in this function's output.
  257. *
  258. * @return array Rey-keyed options configuration array.
  259. *
  260. * @access private
  261. */
  262. function of_get_default_values() {
  263. $output = array();
  264. $config = optionsframework_options();
  265. foreach ( (array) $config as $option ) {
  266. if ( ! isset( $option['id'] ) ) {
  267. continue;
  268. }
  269. if ( ! isset( $option['std'] ) ) {
  270. continue;
  271. }
  272. if ( ! isset( $option['type'] ) ) {
  273. continue;
  274. }
  275. if ( has_filter( 'of_sanitize_' . $option['type'] ) ) {
  276. $output[$option['id']] = apply_filters( 'of_sanitize_' . $option['type'], $option['std'], $option );
  277. }
  278. }
  279. return $output;
  280. }
  281. /**
  282. * Add Theme Options menu item to Admin Bar.
  283. */
  284. add_action( 'wp_before_admin_bar_render', 'optionsframework_adminbar' );
  285. function optionsframework_adminbar() {
  286. global $wp_admin_bar;
  287. $wp_admin_bar->add_menu( array(
  288. 'parent' => 'appearance',
  289. 'id' => 'of_theme_options',
  290. 'title' => __( 'Theme Options' ),
  291. 'href' => admin_url( 'themes.php?page=options-framework' )
  292. ));
  293. }
  294. if ( ! function_exists( 'of_get_option' ) ) {
  295. /**
  296. * Get Option.
  297. *
  298. * Helper function to return the theme option value.
  299. * If no value has been saved, it returns $default.
  300. * Needed because options are saved as serialized strings.
  301. */
  302. function of_get_option( $name, $default = false ) {
  303. $config = get_option( 'optionsframework' );
  304. if ( ! isset( $config['id'] ) ) {
  305. return $default;
  306. }
  307. $options = get_option( $config['id'] );
  308. if ( isset( $options[$name] ) ) {
  309. return $options[$name];
  310. }
  311. return $default;
  312. }
  313. }