PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/news/library/functions/customize.php

https://bitbucket.org/lgorence/quickpress
PHP | 181 lines | 87 code | 26 blank | 68 comment | 8 complexity | d7e9db88a09f32a1036508e512ce20ee MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Functions for registering and setting theme settings that tie into the WordPress theme customizer.
  4. * This file loads additional classes and adds settings to the customizer for the built-in Hybrid Core
  5. * settings.
  6. *
  7. * @package HybridCore
  8. * @subpackage Functions
  9. * @author Justin Tadlock <justin@justintadlock.com>
  10. * @copyright Copyright (c) 2008 - 2012, Justin Tadlock
  11. * @link http://themehybrid.com/hybrid-core
  12. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  13. */
  14. /* Load custom control classes. */
  15. add_action( 'customize_register', 'hybrid_load_customize_controls', 1 );
  16. /* Register custom sections, settings, and controls. */
  17. add_action( 'customize_register', 'hybrid_customize_register' );
  18. /* Add the footer content Ajax to the correct hooks. */
  19. add_action( 'wp_ajax_hybrid_customize_footer_content', 'hybrid_customize_footer_content_ajax' );
  20. add_action( 'wp_ajax_nopriv_hybrid_customize_footer_content', 'hybrid_customize_footer_content_ajax' );
  21. /**
  22. * Loads framework-specific customize control classes. Customize control classes extend the WordPress
  23. * WP_Customize_Control class to create unique classes that can be used within the framework.
  24. *
  25. * @since 1.4.0
  26. * @access private
  27. */
  28. function hybrid_load_customize_controls() {
  29. /* Loads the textarea customize control class. */
  30. require_once( trailingslashit( HYBRID_CLASSES ) . 'customize-control-textarea.php' );
  31. }
  32. /**
  33. * Registers custom sections, settings, and controls for the $wp_customize instance.
  34. *
  35. * @since 1.4.0
  36. * @access private
  37. * @param object $wp_customize
  38. */
  39. function hybrid_customize_register( $wp_customize ) {
  40. /* Get supported theme settings. */
  41. $supports = get_theme_support( 'hybrid-core-theme-settings' );
  42. /* Get the theme prefix. */
  43. $prefix = hybrid_get_prefix();
  44. /* Get the default theme settings. */
  45. $default_settings = hybrid_get_default_theme_settings();
  46. /* Add the footer section, setting, and control if theme supports the 'footer' setting. */
  47. if ( is_array( $supports[0] ) && in_array( 'footer', $supports[0] ) ) {
  48. /* Add the footer section. */
  49. $wp_customize->add_section(
  50. 'hybrid-core-footer',
  51. array(
  52. 'title' => esc_html__( 'Footer', 'hybrid-core' ),
  53. 'priority' => 200,
  54. 'capability' => 'edit_theme_options'
  55. )
  56. );
  57. /* Add the 'footer_insert' setting. */
  58. $wp_customize->add_setting(
  59. "{$prefix}_theme_settings[footer_insert]",
  60. array(
  61. 'default' => $default_settings['footer_insert'],
  62. 'type' => 'option',
  63. 'capability' => 'edit_theme_options',
  64. 'sanitize_callback' => 'hybrid_customize_sanitize',
  65. 'sanitize_js_callback' => 'hybrid_customize_sanitize',
  66. 'transport' => 'postMessage',
  67. )
  68. );
  69. /* Add the textarea control for the 'footer_insert' setting. */
  70. $wp_customize->add_control(
  71. new Hybrid_Customize_Control_Textarea(
  72. $wp_customize,
  73. 'hybrid-core-footer',
  74. array(
  75. 'label' => esc_html__( 'Footer', 'hybrid-core' ),
  76. 'section' => 'hybrid-core-footer',
  77. 'settings' => "{$prefix}_theme_settings[footer_insert]",
  78. )
  79. )
  80. );
  81. /* If viewing the customize preview screen, add a script to show a live preview. */
  82. if ( $wp_customize->is_preview() && !is_admin() )
  83. add_action( 'wp_footer', 'hybrid_customize_preview_script', 21 );
  84. }
  85. }
  86. /**
  87. * Sanitizes the footer content on the customize screen. Users with the 'unfiltered_html' cap can post
  88. * anything. For other users, wp_filter_post_kses() is ran over the setting.
  89. *
  90. * @since 1.4.0
  91. * @access public
  92. * @param mixed $setting The current setting passed to sanitize.
  93. * @param object $object The setting object passed via WP_Customize_Setting.
  94. * @return mixed $setting
  95. */
  96. function hybrid_customize_sanitize( $setting, $object ) {
  97. /* Get the theme prefix. */
  98. $prefix = hybrid_get_prefix();
  99. /* Make sure we kill evil scripts from users without the 'unfiltered_html' cap. */
  100. if ( "{$prefix}_theme_settings[footer_insert]" == $object->id && !current_user_can( 'unfiltered_html' ) )
  101. $setting = stripslashes( wp_filter_post_kses( addslashes( $setting ) ) );
  102. /* Return the sanitized setting and apply filters. */
  103. return apply_filters( "{$prefix}_customize_sanitize", $setting, $object );
  104. }
  105. /**
  106. * Runs the footer content posted via Ajax through the do_shortcode() function. This makes sure the
  107. * shortcodes are output correctly in the live preview.
  108. *
  109. * @since 1.4.0
  110. * @access private
  111. */
  112. function hybrid_customize_footer_content_ajax() {
  113. /* Check the AJAX nonce to make sure this is a valid request. */
  114. check_ajax_referer( 'hybrid_customize_footer_content_nonce' );
  115. /* If footer content has been posted, run it through the do_shortcode() function. */
  116. if ( isset( $_POST['footer_content'] ) )
  117. echo do_shortcode( wp_kses_stripslashes( $_POST['footer_content'] ) );
  118. /* Always die() when handling Ajax. */
  119. die();
  120. }
  121. /**
  122. * Handles changing settings for the live preview of the theme.
  123. *
  124. * @since 1.4.0
  125. * @access private
  126. */
  127. function hybrid_customize_preview_script() {
  128. /* Create a nonce for the Ajax. */
  129. $nonce = wp_create_nonce( 'hybrid_customize_footer_content_nonce' );
  130. ?>
  131. <script type="text/javascript">
  132. wp.customize(
  133. '<?php echo hybrid_get_prefix(); ?>_theme_settings[footer_insert]',
  134. function( value ) {
  135. value.bind(
  136. function( to ) {
  137. jQuery.post(
  138. '<?php echo admin_url( 'admin-ajax.php' ); ?>',
  139. {
  140. action: 'hybrid_customize_footer_content',
  141. _ajax_nonce: '<?php echo $nonce; ?>',
  142. footer_content: to
  143. },
  144. function( response ) {
  145. jQuery( '.footer-content' ).html( response );
  146. }
  147. );
  148. }
  149. );
  150. }
  151. );
  152. </script>
  153. <?php
  154. }
  155. ?>