post_value() public method

The name "post_value" is a carry-over from when the customized state was exclusively sourced from $_POST['customized']. Nevertheless, the value returned will come from the current changeset post and from the incoming post data.
See also: WP_REST_Server::dispatch()
See also: WP_Rest_Request::sanitize_params()
See also: WP_Rest_Request::has_valid_params()
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.
public post_value ( WP_Customize_Setting $setting, mixed $default = null ) : string | mixed
$setting WP_Customize_Setting A WP_Customize_Setting derived object.
$default mixed 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.
 /**
  * Test the WP_Customize_Manager::post_value() method to make sure that the validation and sanitization are done in the right order.
  *
  * @ticket 37247
  */
 function test_post_value_validation_sanitization_order()
 {
     $default_value = '0';
     $setting = $this->manager->add_setting('numeric', array('validate_callback' => array($this, 'filter_customize_validate_numeric'), 'sanitize_callback' => array($this, 'filter_customize_sanitize_numeric')));
     $this->assertEquals($default_value, $this->manager->post_value($setting, $default_value));
     $this->assertEquals($default_value, $setting->post_value($default_value));
     $post_value = '42';
     $this->manager->set_post_value('numeric', $post_value);
     $this->assertEquals($post_value, $this->manager->post_value($setting, $default_value));
     $this->assertEquals($post_value, $setting->post_value($default_value));
 }
 /**
  * Fetch and sanitize the $_POST value for the setting.
  *
  * @since 3.4.0
  *
  * @param mixed $default A default value which is used as a fallback. Default is null.
  * @return mixed The default value on failure, otherwise the sanitized value.
  */
 public final function post_value($default = null)
 {
     // Check for a cached value
     if (isset($this->_post_value)) {
         return $this->_post_value;
     }
     // Call the manager for the post value
     $result = $this->manager->post_value($this);
     if (isset($result)) {
         return $this->_post_value = $result;
     } else {
         return $default;
     }
 }
Esempio n. 3
0
 /**
  * Test the WP_Customize_Manager::post_value() method for a setting value that fails validation.
  *
  * @ticket 34893
  */
 function test_invalid_post_value()
 {
     $default_value = 'foo_default';
     $setting = $this->manager->add_setting('foo', array('validate_callback' => array($this, 'filter_customize_validate_foo'), 'sanitize_callback' => array($this, 'filter_customize_sanitize_foo')));
     $this->assertEquals($default_value, $this->manager->post_value($setting, $default_value));
     $this->assertEquals($default_value, $setting->post_value($default_value));
     $post_value = 'bar';
     $this->manager->set_post_value('foo', $post_value);
     $this->assertEquals(strtoupper($post_value), $this->manager->post_value($setting, $default_value));
     $this->assertEquals(strtoupper($post_value), $setting->post_value($default_value));
     $this->manager->set_post_value('foo', 'return_wp_error_in_sanitize');
     $this->assertEquals($default_value, $this->manager->post_value($setting, $default_value));
     $this->assertEquals($default_value, $setting->post_value($default_value));
     $this->manager->set_post_value('foo', 'return_null_in_sanitize');
     $this->assertEquals($default_value, $this->manager->post_value($setting, $default_value));
     $this->assertEquals($default_value, $setting->post_value($default_value));
     $post_value = '<script>evil</script>';
     $this->manager->set_post_value('foo', $post_value);
     $this->assertEquals($default_value, $this->manager->post_value($setting, $default_value));
     $this->assertEquals($default_value, $setting->post_value($default_value));
 }
 /**
  * Fetch and sanitize the $_POST value for the setting.
  *
  * @since 3.4.0
  *
  * @param mixed $default A default value which is used as a fallback. Default is null.
  * @return mixed The default value on failure, otherwise the sanitized value.
  */
 public final function post_value($default = null)
 {
     return $this->manager->post_value($this, $default);
 }