/** * Test validate. * * @see WP_Customize_Setting::validate() */ public function test_validate() { $setting = new WP_Customize_Setting($this->manager, 'name', array('type' => 'key', 'validate_callback' => array($this, 'filter_validate_for_test_validate'))); $validity = $setting->validate('BAD!'); $this->assertInstanceOf('WP_Error', $validity); $this->assertEquals('invalid_key', $validity->get_error_code()); }
/** * Returns the sanitized value for a given setting from the request's POST data. * * @since 3.4.0 * @since 4.1.1 Introduced the `$default` parameter. * @since 4.6.0 `$default` is now returned early when the setting post value is invalid. * @access public * * @see WP_REST_Server::dispatch() * @see WP_Rest_Request::sanitize_params() * @see WP_Rest_Request::has_valid_params() * * @param WP_Customize_Setting $setting A WP_Customize_Setting derived object. * @param mixed $default Value returned $setting has no post value (added in 4.2.0) * or the post value is invalid (added in 4.6.0). * @return string|mixed $post_value Sanitized value or the $default provided. */ public function post_value($setting, $default = null) { $post_values = $this->unsanitized_post_values(); if (!array_key_exists($setting->id, $post_values)) { return $default; } $value = $post_values[$setting->id]; $valid = $setting->validate($value); if (is_wp_error($valid)) { return $default; } $value = $setting->sanitize($value); if (is_null($value) || is_wp_error($value)) { return $default; } return $value; }
/** * Validate CSS. * * Checks for imbalanced braces, brackets, and comments. * Notifications are rendered when the customizer state is saved. * * @todo There are cases where valid CSS can be incorrectly marked as invalid when strings or comments include balancing characters. To fix, CSS tokenization needs to be used. * * @since 4.7.0 * @access public * * @param string $css The input string. * @return true|WP_Error True if the input was validated, otherwise WP_Error. */ 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; // Make sure that there is a closing brace for each opening brace. if (!$this->validate_balanced_characters('{', '}', $css)) { $validity->add('imbalanced_curly_brackets', __('Your curly brackets <code>{}</code> are imbalanced. Make sure there is a closing <code>}</code> for every opening <code>{</code>.')); $imbalanced = true; } // Ensure brackets are balanced. if (!$this->validate_balanced_characters('[', ']', $css)) { $validity->add('imbalanced_braces', __('Your brackets <code>[]</code> are imbalanced. Make sure there is a closing <code>]</code> for every opening <code>[</code>.')); $imbalanced = true; } // Ensure parentheses are balanced. if (!$this->validate_balanced_characters('(', ')', $css)) { $validity->add('imbalanced_parentheses', __('Your parentheses <code>()</code> are imbalanced. Make sure there is a closing <code>)</code> for every opening <code>(</code>.')); $imbalanced = true; } // Ensure single quotes are equal. if (!$this->validate_equal_characters('\'', $css)) { $validity->add('unequal_single_quotes', __('Your single quotes <code>\'</code> are uneven. Make sure there is a closing <code>\'</code> for every opening <code>\'</code>.')); $imbalanced = true; } // Ensure single quotes are equal. if (!$this->validate_equal_characters('"', $css)) { $validity->add('unequal_double_quotes', __('Your double quotes <code>"</code> are uneven. Make sure there is a closing <code>"</code> for every opening <code>"</code>.')); $imbalanced = true; } /* * Make sure any code comments are closed properly. * * The first check could miss stray an unpaired comment closing figure, so if * The number appears to be balanced, then check for equal numbers * of opening/closing comment figures. * * Although it may initially appear redundant, we use the first method * to give more specific feedback to the user. */ $unclosed_comment_count = $this->validate_count_unclosed_comments($css); if (0 < $unclosed_comment_count) { $validity->add('unclosed_comment', sprintf(_n('There is %s unclosed code comment. Close each comment with <code>*/</code>.', 'There are %s unclosed code comments. Close each comment with <code>*/</code>.', $unclosed_comment_count), $unclosed_comment_count)); $imbalanced = true; } elseif (!$this->validate_balanced_characters('/*', '*/', $css)) { $validity->add('imbalanced_comments', __('There is an extra <code>*/</code>, indicating an end to a comment. Be sure that there is an opening <code>/*</code> for every closing <code>*/</code>.')); $imbalanced = true; } if ($imbalanced && $this->is_possible_content_error($css)) { $validity->add('possible_false_positive', __('Imbalanced/unclosed character errors can be caused by <code>content: "";</code> declarations. You may need to remove this or add it to a custom CSS file.')); } if (empty($validity->errors)) { $validity = parent::validate($css); } return $validity; }