123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- <?php
- final class WP_Customize_Custom_CSS_Setting extends WP_Customize_Setting {
-
- public $type = 'custom_css';
-
- public $transport = 'postMessage';
-
- public $capability = 'edit_css';
-
- public $stylesheet = '';
-
- public function __construct( $manager, $id, $args = array() ) {
- parent::__construct( $manager, $id, $args );
- if ( 'custom_css' !== $this->id_data['base'] ) {
- throw new Exception( 'Expected custom_css id_base.' );
- }
- if ( 1 !== count( $this->id_data['keys'] ) || empty( $this->id_data['keys'][0] ) ) {
- throw new Exception( 'Expected single stylesheet key.' );
- }
- $this->stylesheet = $this->id_data['keys'][0];
- }
-
- public function preview() {
- if ( $this->is_previewed ) {
- return false;
- }
- $this->is_previewed = true;
- add_filter( 'wp_get_custom_css', array( $this, 'filter_previewed_wp_get_custom_css' ), 9, 2 );
- return true;
- }
-
- public function filter_previewed_wp_get_custom_css( $css, $stylesheet ) {
- if ( $stylesheet === $this->stylesheet ) {
- $customized_value = $this->post_value( null );
- if ( ! is_null( $customized_value ) ) {
- $css = $customized_value;
- }
- }
- return $css;
- }
-
- public function value() {
- if ( $this->is_previewed ) {
- $post_value = $this->post_value( null );
- if ( null !== $post_value ) {
- return $post_value;
- }
- }
- $id_base = $this->id_data['base'];
- $value = '';
- $post = wp_get_custom_css_post( $this->stylesheet );
- if ( $post ) {
- $value = $post->post_content;
- }
- if ( empty( $value ) ) {
- $value = $this->default;
- }
-
- $value = apply_filters( "customize_value_{$id_base}", $value, $this );
- return $value;
- }
-
- public function validate( $css ) {
- $validity = new WP_Error();
- if ( preg_match( '#</?\w+#', $css ) ) {
- $validity->add( 'illegal_markup', __( 'Markup is not allowed in CSS.' ) );
- }
- $imbalanced = false;
-
- if ( ! $this->validate_balanced_characters( '{', '}', $css ) ) {
- $validity->add( 'imbalanced_curly_brackets', sprintf(
-
- __( 'Your curly brackets %1$s are imbalanced. Make sure there is a closing %2$s for every opening %3$s.' ),
- '<code>{}</code>',
- '<code>}</code>',
- '<code>{</code>'
- ) );
- $imbalanced = true;
- }
-
- if ( ! $this->validate_balanced_characters( '[', ']', $css ) ) {
- $validity->add( 'imbalanced_braces', sprintf(
-
- __( 'Your brackets %1$s are imbalanced. Make sure there is a closing %2$s for every opening %3$s.' ),
- '<code>[]</code>',
- '<code>]</code>',
- '<code>[</code>'
- ) );
- $imbalanced = true;
- }
-
- if ( ! $this->validate_balanced_characters( '(', ')', $css ) ) {
- $validity->add( 'imbalanced_parentheses', sprintf(
-
- __( 'Your parentheses %1$s are imbalanced. Make sure there is a closing %2$s for every opening %3$s.' ),
- '<code>()</code>',
- '<code>)</code>',
- '<code>(</code>'
- ) );
- $imbalanced = true;
- }
-
- if ( ! $this->validate_equal_characters( '"', $css ) ) {
- $validity->add( 'unequal_double_quotes', sprintf(
-
- __( 'Your double quotes %1$s are uneven. Make sure there is a closing %1$s for every opening %1$s.' ),
- '<code>"</code>'
- ) );
- $imbalanced = true;
- }
-
- $unclosed_comment_count = $this->validate_count_unclosed_comments( $css );
- if ( 0 < $unclosed_comment_count ) {
- $validity->add( 'unclosed_comment', sprintf(
-
- _n(
- 'There is %1$s unclosed code comment. Close each comment with %2$s.',
- 'There are %1$s unclosed code comments. Close each comment with %2$s.',
- $unclosed_comment_count
- ),
- $unclosed_comment_count,
- '<code>*/</code>'
- ) );
- $imbalanced = true;
- } elseif ( ! $this->validate_balanced_characters( '/*', '*/', $css ) ) {
- $validity->add( 'imbalanced_comments', sprintf(
-
- __( 'There is an extra %1$s, indicating an end to a comment. Be sure that there is an opening %2$s for every closing %1$s.' ),
- '<code>*/</code>',
- '<code>/*</code>'
- ) );
- $imbalanced = true;
- }
- if ( $imbalanced && $this->is_possible_content_error( $css ) ) {
- $validity->add( 'possible_false_positive', sprintf(
-
- __( 'Imbalanced/unclosed character errors can be caused by %s declarations. You may need to remove this or add it to a custom CSS file.' ),
- '<code>content: "";</code>'
- ) );
- }
- if ( empty( $validity->errors ) ) {
- $validity = parent::validate( $css );
- }
- return $validity;
- }
-
- public function update( $css ) {
- if ( empty( $css ) ) {
- $css = '';
- }
- $r = wp_update_custom_css_post( $css, array(
- 'stylesheet' => $this->stylesheet,
- ) );
- if ( $r instanceof WP_Error ) {
- return false;
- }
- $post_id = $r->ID;
-
- if ( $this->manager->get_stylesheet() === $this->stylesheet ) {
- set_theme_mod( 'custom_css_post_id', $post_id );
- }
- return $post_id;
- }
-
- private function validate_balanced_characters( $opening_char, $closing_char, $css ) {
- return substr_count( $css, $opening_char ) === substr_count( $css, $closing_char );
- }
-
- private function validate_equal_characters( $char, $css ) {
- $char_count = substr_count( $css, $char );
- return ( 0 === $char_count % 2 );
- }
-
- private function validate_count_unclosed_comments( $css ) {
- $count = 0;
- $comments = explode( '/*', $css );
- if ( ! is_array( $comments ) || ( 1 >= count( $comments ) ) ) {
- return $count;
- }
- unset( $comments[0] );
- foreach ( $comments as $comment ) {
- if ( false === strpos( $comment, '*/' ) ) {
- $count++;
- }
- }
- return $count;
- }
-
- private function is_possible_content_error( $css ) {
- $found = preg_match( '/\bcontent\s*:/', $css );
- if ( ! empty( $found ) ) {
- return true;
- }
- return false;
- }
- }
|