PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/news/library/admin/theme-settings.php

https://bitbucket.org/lgorence/quickpress
PHP | 319 lines | 128 code | 55 blank | 136 comment | 12 complexity | c4bac3ff009570ec792d759e9347dd6b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Handles the display and functionality of the theme settings page. This provides the needed hooks and
  4. * meta box calls for developers to create any number of theme settings needed. This file is only loaded if
  5. * the theme supports the 'hybrid-core-theme-settings' feature.
  6. *
  7. * Provides the ability for developers to add custom meta boxes to the theme settings page by using the
  8. * add_meta_box() function. Developers should register their meta boxes on the 'add_meta_boxes' hook
  9. * and register the meta box for 'appearance_page_theme-settings'. To validate/sanitize data from
  10. * custom settings, devs should use the 'sanitize_option_{$prefix}_theme_settings' filter hook.
  11. *
  12. * @package HybridCore
  13. * @subpackage Admin
  14. * @author Justin Tadlock <justin@justintadlock.com>
  15. * @copyright Copyright (c) 2008 - 2012, Justin Tadlock
  16. * @link http://themehybrid.com/hybrid-core
  17. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  18. */
  19. /* Hook the settings page function to 'admin_menu'. */
  20. add_action( 'admin_menu', 'hybrid_settings_page_init' );
  21. /**
  22. * Initializes all the theme settings page functionality. This function is used to create the theme settings
  23. * page, then use that as a launchpad for specific actions that need to be tied to the settings page.
  24. *
  25. * @since 0.7.0
  26. * @global string $hybrid The global theme object.
  27. * @return void
  28. */
  29. function hybrid_settings_page_init() {
  30. global $hybrid;
  31. /* Get theme information. */
  32. $theme = wp_get_theme( get_template(), get_theme_root( get_template_directory() ) );
  33. $prefix = hybrid_get_prefix();
  34. /* Register theme settings. */
  35. register_setting(
  36. "{$prefix}_theme_settings", // Options group.
  37. "{$prefix}_theme_settings", // Database option.
  38. 'hybrid_save_theme_settings' // Validation callback function.
  39. );
  40. /* Create the theme settings page. */
  41. $hybrid->settings_page = add_theme_page(
  42. sprintf( esc_html__( '%s Theme Settings', 'hybrid-core' ), $theme->get( 'Name' ) ), // Settings page name.
  43. esc_html__( 'Theme Settings', 'hybrid-core' ), // Menu item name.
  44. hybrid_settings_page_capability(), // Required capability.
  45. 'theme-settings', // Screen name.
  46. 'hybrid_settings_page' // Callback function.
  47. );
  48. /* Check if the settings page is being shown before running any functions for it. */
  49. if ( !empty( $hybrid->settings_page ) ) {
  50. /* Filter the settings page capability so that it recognizes the 'edit_theme_options' cap. */
  51. add_filter( "option_page_capability_{$prefix}_theme_settings", 'hybrid_settings_page_capability' );
  52. /* Add help tabs to the theme settings page. */
  53. add_action( "load-{$hybrid->settings_page}", 'hybrid_settings_page_help' );
  54. /* Load the theme settings meta boxes. */
  55. add_action( "load-{$hybrid->settings_page}", 'hybrid_load_settings_page_meta_boxes' );
  56. /* Create a hook for adding meta boxes. */
  57. add_action( "load-{$hybrid->settings_page}", 'hybrid_settings_page_add_meta_boxes' );
  58. /* Load the JavaScript and stylesheets needed for the theme settings screen. */
  59. add_action( 'admin_enqueue_scripts', 'hybrid_settings_page_enqueue_scripts' );
  60. add_action( 'admin_enqueue_scripts', 'hybrid_settings_page_enqueue_styles' );
  61. add_action( "admin_footer-{$hybrid->settings_page}", 'hybrid_settings_page_load_scripts' );
  62. }
  63. }
  64. /**
  65. * Returns the required capability for viewing and saving theme settings.
  66. *
  67. * @since 1.2.0
  68. * @return string
  69. */
  70. function hybrid_settings_page_capability() {
  71. return apply_filters( hybrid_get_prefix() . '_settings_capability', 'edit_theme_options' );
  72. }
  73. /**
  74. * Returns the theme settings page name/hook as a string.
  75. *
  76. * @since 1.2.0
  77. * @return string
  78. */
  79. function hybrid_get_settings_page_name() {
  80. global $hybrid;
  81. return ( isset( $hybrid->settings_page ) ? $hybrid->settings_page : 'appearance_page_theme-settings' );
  82. }
  83. /**
  84. * Provides a hook for adding meta boxes as seen on the post screen in the WordPress admin. This addition
  85. * is needed because normal plugin/theme pages don't have this hook by default. The other goal of this
  86. * function is to provide a way for themes to load and execute meta box code only on the theme settings
  87. * page in the admin. This way, they're not needlessly loading extra files.
  88. *
  89. * @since 1.2.0
  90. * @return void
  91. */
  92. function hybrid_settings_page_add_meta_boxes() {
  93. do_action( 'add_meta_boxes', hybrid_get_settings_page_name() );
  94. }
  95. /**
  96. * Loads the meta boxes packaged with the framework on the theme settings page. These meta boxes are
  97. * merely loaded with this function. Meta boxes are only loaded if the feature is supported by the theme.
  98. *
  99. * @since 1.2.0
  100. * @return void
  101. */
  102. function hybrid_load_settings_page_meta_boxes() {
  103. /* Get theme-supported meta boxes for the settings page. */
  104. $supports = get_theme_support( 'hybrid-core-theme-settings' );
  105. /* If there are any supported meta boxes, load them. */
  106. if ( is_array( $supports[0] ) ) {
  107. /* Load the 'About' meta box if it is supported. */
  108. if ( in_array( 'about', $supports[0] ) )
  109. require_once( trailingslashit( HYBRID_ADMIN ) . 'meta-box-theme-about.php' );
  110. /* Load the 'Footer' meta box if it is supported. */
  111. if ( in_array( 'footer', $supports[0] ) )
  112. require_once( trailingslashit( HYBRID_ADMIN ) . 'meta-box-theme-footer.php' );
  113. }
  114. }
  115. /**
  116. * Validation/Sanitization callback function for theme settings. This just returns the data passed to it. Theme
  117. * developers should validate/sanitize their theme settings on the "sanitize_option_{$prefix}_theme_settings"
  118. * hook. This function merely exists for backwards compatibility.
  119. *
  120. * @since 0.7.0
  121. * @param array $settings An array of the theme settings passed by the Settings API for validation.
  122. * @return array $settings The array of theme settings.
  123. */
  124. function hybrid_save_theme_settings( $settings ) {
  125. /* @deprecated 1.0.0. Developers should filter "sanitize_option_{$prefix}_theme_settings" instead. */
  126. return apply_filters( hybrid_get_prefix() . '_validate_theme_settings', $settings );
  127. }
  128. /**
  129. * Displays the theme settings page and calls do_meta_boxes() to allow additional settings
  130. * meta boxes to be added to the page.
  131. *
  132. * @since 0.7.0
  133. * @return void
  134. */
  135. function hybrid_settings_page() {
  136. /* Get the theme information. */
  137. $prefix = hybrid_get_prefix();
  138. $theme = wp_get_theme( get_template(), get_theme_root( get_template_directory() ) );
  139. do_action( "{$prefix}_before_settings_page" ); ?>
  140. <div class="wrap">
  141. <?php screen_icon(); ?>
  142. <h2>
  143. <?php printf( __( '%s Theme Settings', 'hybrid-core' ), $theme->get( 'Name' ) ); ?>
  144. <a href="<?php echo admin_url( 'customize.php' ); ?>" class="add-new-h2"><?php esc_html_e( 'Customize', 'hybrid-core' ); ?></a>
  145. </h2>
  146. <?php settings_errors(); ?>
  147. <?php do_action( "{$prefix}_open_settings_page" ); ?>
  148. <div class="hybrid-core-settings-wrap">
  149. <form method="post" action="options.php">
  150. <?php settings_fields( "{$prefix}_theme_settings" ); ?>
  151. <?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
  152. <?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?>
  153. <div class="metabox-holder">
  154. <div class="post-box-container column-1 normal">
  155. <?php do_meta_boxes( hybrid_get_settings_page_name(), 'normal', null ); ?>
  156. </div>
  157. <div class="post-box-container column-2 side">
  158. <?php do_meta_boxes( hybrid_get_settings_page_name(), 'side', null ); ?>
  159. </div>
  160. <div class="post-box-container column-3 advanced">
  161. <?php do_meta_boxes( hybrid_get_settings_page_name(), 'advanced', null ); ?>
  162. </div>
  163. </div>
  164. <?php submit_button( esc_attr__( 'Update Settings', 'hybrid-core' ) ); ?>
  165. </form>
  166. </div><!-- .hybrid-core-settings-wrap -->
  167. <?php do_action( "{$prefix}_close_settings_page" ); ?>
  168. </div><!-- .wrap --><?php
  169. do_action( "{$prefix}_after_settings_page" );
  170. }
  171. /**
  172. * Creates a settings field id attribute for use on the theme settings page. This is a helper function for use
  173. * with the WordPress settings API.
  174. *
  175. * @since 1.0.0
  176. * @return string
  177. */
  178. function hybrid_settings_field_id( $setting ) {
  179. return hybrid_get_prefix() . '_theme_settings-' . sanitize_html_class( $setting );
  180. }
  181. /**
  182. * Creates a settings field name attribute for use on the theme settings page. This is a helper function for
  183. * use with the WordPress settings API.
  184. *
  185. * @since 1.0.0
  186. * @return string
  187. */
  188. function hybrid_settings_field_name( $setting ) {
  189. return hybrid_get_prefix() . "_theme_settings[{$setting}]";
  190. }
  191. /**
  192. * Adds a help tab to the theme settings screen if the theme has provided a 'Documentation URI' and/or
  193. * 'Support URI'. Theme developers can add custom help tabs using get_current_screen()->add_help_tab().
  194. *
  195. * @since 1.3.0
  196. * @return void
  197. */
  198. function hybrid_settings_page_help() {
  199. /* Get the parent theme data. */
  200. $theme = wp_get_theme( get_template(), get_theme_root( get_template_directory() ) );
  201. $doc_uri = $theme->get( 'Documentation URI' );
  202. $support_uri = $theme->get( 'Support URI' );
  203. /* If the theme has provided a documentation or support URI, add them to the help text. */
  204. if ( !empty( $doc_uri ) || !empty( $support_uri ) ) {
  205. /* Open an unordered list for the help text. */
  206. $help = '<ul>';
  207. /* Add the Documentation URI. */
  208. if ( !empty( $doc_uri ) )
  209. $help .= '<li><a href="' . esc_url( $doc_uri ) . '">' . __( 'Documentation', 'hybrid-core' ) . '</a></li>';
  210. /* Add the Support URI. */
  211. if ( !empty( $support_uri ) )
  212. $help .= '<li><a href="' . esc_url( $support_uri ) . '">' . __( 'Support', 'hybrid-core' ) . '</a></li>';
  213. /* Close the unordered list for the help text. */
  214. $help .= '</ul>';
  215. /* Add a help tab with links for documentation and support. */
  216. get_current_screen()->add_help_tab(
  217. array(
  218. 'id' => 'default',
  219. 'title' => esc_attr( $theme->get( 'Name' ) ),
  220. 'content' => $help
  221. )
  222. );
  223. }
  224. }
  225. /**
  226. * Loads the required stylesheets for displaying the theme settings page in the WordPress admin.
  227. *
  228. * @since 1.2.0
  229. * @return void
  230. */
  231. function hybrid_settings_page_enqueue_styles( $hook_suffix ) {
  232. /* Load admin stylesheet if on the theme settings screen. */
  233. if ( $hook_suffix == hybrid_get_settings_page_name() )
  234. wp_enqueue_style( 'hybrid-core-admin' );
  235. }
  236. /**
  237. * Loads the JavaScript files required for managing the meta boxes on the theme settings
  238. * page, which allows users to arrange the boxes to their liking.
  239. *
  240. * @since 1.2.0
  241. * @param string $hook_suffix The current page being viewed.
  242. * @return void
  243. */
  244. function hybrid_settings_page_enqueue_scripts( $hook_suffix ) {
  245. if ( $hook_suffix == hybrid_get_settings_page_name() )
  246. wp_enqueue_script( 'postbox' );
  247. }
  248. /**
  249. * Loads the JavaScript required for toggling the meta boxes on the theme settings page.
  250. *
  251. * @since 0.7.0
  252. * @return void
  253. */
  254. function hybrid_settings_page_load_scripts() { ?>
  255. <script type="text/javascript">
  256. //<![CDATA[
  257. jQuery(document).ready( function($) {
  258. $('.if-js-closed').removeClass('if-js-closed').addClass('closed');
  259. postboxes.add_postbox_toggles( '<?php echo hybrid_get_settings_page_name(); ?>' );
  260. });
  261. //]]>
  262. </script><?php
  263. }
  264. ?>