[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/customize/ -> class-wp-customize-custom-css-setting.php (source)

   1  <?php
   2  /**
   3   * Customize API: WP_Customize_Custom_CSS_Setting class
   4   *
   5   * This handles validation, sanitization and saving of the value.
   6   *
   7   * @package WordPress
   8   * @subpackage Customize
   9   * @since 4.7.0
  10   */
  11  
  12  /**
  13   * Custom Setting to handle WP Custom CSS.
  14   *
  15   * @since 4.7.0
  16   *
  17   * @see WP_Customize_Setting
  18   */
  19  final class WP_Customize_Custom_CSS_Setting extends WP_Customize_Setting {
  20  
  21      /**
  22       * The setting type.
  23       *
  24       * @since 4.7.0
  25       * @var string
  26       */
  27      public $type = 'custom_css';
  28  
  29      /**
  30       * Setting Transport
  31       *
  32       * @since 4.7.0
  33       * @var string
  34       */
  35      public $transport = 'postMessage';
  36  
  37      /**
  38       * Capability required to edit this setting.
  39       *
  40       * @since 4.7.0
  41       * @var string
  42       */
  43      public $capability = 'edit_css';
  44  
  45      /**
  46       * Stylesheet
  47       *
  48       * @since 4.7.0
  49       * @var string
  50       */
  51      public $stylesheet = '';
  52  
  53      /**
  54       * WP_Customize_Custom_CSS_Setting constructor.
  55       *
  56       * @since 4.7.0
  57       *
  58       * @throws Exception If the setting ID does not match the pattern `custom_css[$stylesheet]`.
  59       *
  60       * @param WP_Customize_Manager $manager Customizer bootstrap instance.
  61       * @param string               $id      A specific ID of the setting.
  62       *                                      Can be a theme mod or option name.
  63       * @param array                $args    Setting arguments.
  64       */
  65  	public function __construct( $manager, $id, $args = array() ) {
  66          parent::__construct( $manager, $id, $args );
  67          if ( 'custom_css' !== $this->id_data['base'] ) {
  68              throw new Exception( 'Expected custom_css id_base.' );
  69          }
  70          if ( 1 !== count( $this->id_data['keys'] ) || empty( $this->id_data['keys'][0] ) ) {
  71              throw new Exception( 'Expected single stylesheet key.' );
  72          }
  73          $this->stylesheet = $this->id_data['keys'][0];
  74      }
  75  
  76      /**
  77       * Add filter to preview post value.
  78       *
  79       * @since 4.7.9
  80       *
  81       * @return bool False when preview short-circuits due no change needing to be previewed.
  82       */
  83  	public function preview() {
  84          if ( $this->is_previewed ) {
  85              return false;
  86          }
  87          $this->is_previewed = true;
  88          add_filter( 'wp_get_custom_css', array( $this, 'filter_previewed_wp_get_custom_css' ), 9, 2 );
  89          return true;
  90      }
  91  
  92      /**
  93       * Filters `wp_get_custom_css` for applying the customized value.
  94       *
  95       * This is used in the preview when `wp_get_custom_css()` is called for rendering the styles.
  96       *
  97       * @since 4.7.0
  98       *
  99       * @see wp_get_custom_css()
 100       *
 101       * @param string $css        Original CSS.
 102       * @param string $stylesheet Current stylesheet.
 103       * @return string CSS.
 104       */
 105  	public function filter_previewed_wp_get_custom_css( $css, $stylesheet ) {
 106          if ( $stylesheet === $this->stylesheet ) {
 107              $customized_value = $this->post_value( null );
 108              if ( ! is_null( $customized_value ) ) {
 109                  $css = $customized_value;
 110              }
 111          }
 112          return $css;
 113      }
 114  
 115      /**
 116       * Fetch the value of the setting. Will return the previewed value when `preview()` is called.
 117       *
 118       * @since 4.7.0
 119       *
 120       * @see WP_Customize_Setting::value()
 121       *
 122       * @return string
 123       */
 124  	public function value() {
 125          if ( $this->is_previewed ) {
 126              $post_value = $this->post_value( null );
 127              if ( null !== $post_value ) {
 128                  return $post_value;
 129              }
 130          }
 131          $id_base = $this->id_data['base'];
 132          $value   = '';
 133          $post    = wp_get_custom_css_post( $this->stylesheet );
 134          if ( $post ) {
 135              $value = $post->post_content;
 136          }
 137          if ( empty( $value ) ) {
 138              $value = $this->default;
 139          }
 140  
 141          /** This filter is documented in wp-includes/class-wp-customize-setting.php */
 142          $value = apply_filters( "customize_value_{$id_base}", $value, $this );
 143  
 144          return $value;
 145      }
 146  
 147      /**
 148       * Validate CSS.
 149       *
 150       * Checks for imbalanced braces, brackets, and comments.
 151       * Notifications are rendered when the customizer state is saved.
 152       *
 153       * @since 4.7.0
 154       * @since 4.9.0 Checking for balanced characters has been moved client-side via linting in code editor.
 155       *
 156       * @param string $css The input string.
 157       * @return true|WP_Error True if the input was validated, otherwise WP_Error.
 158       */
 159  	public function validate( $css ) {
 160          $validity = new WP_Error();
 161  
 162          if ( preg_match( '#</?\w+#', $css ) ) {
 163              $validity->add( 'illegal_markup', __( 'Markup is not allowed in CSS.' ) );
 164          }
 165  
 166          if ( ! $validity->has_errors() ) {
 167              $validity = parent::validate( $css );
 168          }
 169          return $validity;
 170      }
 171  
 172      /**
 173       * Store the CSS setting value in the custom_css custom post type for the stylesheet.
 174       *
 175       * @since 4.7.0
 176       *
 177       * @param string $css The input value.
 178       * @return int|false The post ID or false if the value could not be saved.
 179       */
 180  	public function update( $css ) {
 181          if ( empty( $css ) ) {
 182              $css = '';
 183          }
 184  
 185          $r = wp_update_custom_css_post(
 186              $css,
 187              array(
 188                  'stylesheet' => $this->stylesheet,
 189              )
 190          );
 191  
 192          if ( $r instanceof WP_Error ) {
 193              return false;
 194          }
 195          $post_id = $r->ID;
 196  
 197          // Cache post ID in theme mod for performance to avoid additional DB query.
 198          if ( $this->manager->get_stylesheet() === $this->stylesheet ) {
 199              set_theme_mod( 'custom_css_post_id', $post_id );
 200          }
 201  
 202          return $post_id;
 203      }
 204  }


Generated: Wed Oct 28 01:00:03 2020 Cross-referenced by PHPXref 0.7.1