WP_Customize_Manager::unsanitized_post_values PHP Method

unsanitized_post_values() public method

The returned array consists of a merge of three sources: 1. If the theme is not currently active, then the base array is any stashed theme mods that were modified previously but never published. 2. The values from the current changeset, if it exists. 3. If the user can customize, the values parsed from the incoming $_POST['customized'] JSON data. 4. Any programmatically-set post values via WP_Customize_Manager::set_post_value(). The name "unsanitized_post_values" 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.
Since: 4.1.1
Since: 4.7.0 Added $args param and merging with changeset values and stashed theme mods.
public unsanitized_post_values ( array $args = [] ) : array
$args array { Args. @type bool $exclude_changeset Whether the changeset values should also be excluded. Defaults to false. @type bool $exclude_post_data Whether the post input values should also be excluded. Defaults to false when lacking the customize capability. }
return array
    public function unsanitized_post_values($args = array())
    {
        $args = array_merge(array('exclude_changeset' => false, 'exclude_post_data' => !current_user_can('customize')), $args);
        $values = array();
        // Let default values be from the stashed theme mods if doing a theme switch and if no changeset is present.
        if (!$this->is_theme_active()) {
            $stashed_theme_mods = get_option('customize_stashed_theme_mods');
            $stylesheet = $this->get_stylesheet();
            if (isset($stashed_theme_mods[$stylesheet])) {
                $values = array_merge($values, wp_list_pluck($stashed_theme_mods[$stylesheet], 'value'));
            }
        }
        if (!$args['exclude_changeset']) {
            foreach ($this->changeset_data() as $setting_id => $setting_params) {
                if (!array_key_exists('value', $setting_params)) {
                    continue;
                }
                if (isset($setting_params['type']) && 'theme_mod' === $setting_params['type']) {
                    // Ensure that theme mods values are only used if they were saved under the current theme.
                    $namespace_pattern = '/^(?P<stylesheet>.+?)::(?P<setting_id>.+)$/';
                    if (preg_match($namespace_pattern, $setting_id, $matches) && $this->get_stylesheet() === $matches['stylesheet']) {
                        $values[$matches['setting_id']] = $setting_params['value'];
                    }
                } else {
                    $values[$setting_id] = $setting_params['value'];
                }
            }
        }
        if (!$args['exclude_post_data']) {
            if (!isset($this->_post_values)) {
                if (isset($_POST['customized'])) {
                    $post_values = json_decode(wp_unslash($_POST['customized']), true);
                } else {
                    $post_values = array();
                }
                if (is_array($post_values)) {
                    $this->_post_values = $post_values;
                } else {
                    $this->_post_values = array();
                }
            }
            $values = array_merge($values, $this->_post_values);
        }
        return $values;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * @ticket 34738
  * @see WP_Customize_Widgets::call_widget_update()
  */
 function test_call_widget_update()
 {
     $widget_number = 2;
     $widget_id = "search-{$widget_number}";
     $setting_id = "widget_search[{$widget_number}]";
     $instance = array('title' => 'Buscar');
     $_POST = wp_slash(array('action' => 'update-widget', 'wp_customize' => 'on', 'nonce' => wp_create_nonce('update-widget'), 'theme' => $this->manager->get_stylesheet(), 'customized' => '{}', 'widget-search' => array(2 => $instance), 'widget-id' => $widget_id, 'id_base' => 'search', 'widget-width' => '250', 'widget-height' => '200', 'widget_number' => strval($widget_number), 'multi_number' => '', 'add_new' => ''));
     $this->do_customize_boot_actions();
     $this->assertArrayNotHasKey($setting_id, $this->manager->unsanitized_post_values());
     $result = $this->manager->widgets->call_widget_update($widget_id);
     $this->assertInternalType('array', $result);
     $this->assertArrayHasKey('instance', $result);
     $this->assertArrayHasKey('form', $result);
     $this->assertEquals($instance, $result['instance']);
     $this->assertContains(sprintf('value="%s"', esc_attr($instance['title'])), $result['form']);
     $post_values = $this->manager->unsanitized_post_values();
     $this->assertArrayHasKey($setting_id, $post_values);
     $post_value = $post_values[$setting_id];
     $this->assertInternalType('array', $post_value);
     $this->assertArrayHasKey('title', $post_value);
     $this->assertArrayHasKey('encoded_serialized_instance', $post_value);
     $this->assertArrayHasKey('instance_hash_key', $post_value);
     $this->assertArrayHasKey('is_widget_customizer_js_value', $post_value);
     $this->assertEquals($post_value, $this->manager->widgets->sanitize_widget_js_instance($instance));
 }
All Usage Examples Of WP_Customize_Manager::unsanitized_post_values
WP_Customize_Manager