/**
  * @see Widget_Posts::filter_dynamic_setting_class()
  */
 function test_filter_dynamic_setting_class()
 {
     $setting_class = apply_filters('customize_dynamic_setting_class', 'WP_Customize_Setting', 'widget_text[2]', array());
     $this->assertEquals('\\' . __NAMESPACE__ . '\\WP_Customize_Widget_Setting', $setting_class);
     $this->assertEquals($setting_class, $this->widget_posts->filter_dynamic_setting_class('WP_Customize_Setting', 'widget_text[2]'));
     $this->assertEquals('WP_Customize_Setting', $this->widget_posts->filter_dynamic_setting_class('WP_Customize_Setting', 'foo'));
 }
 /**
  * @param array $config
  */
 public function __construct($config = array())
 {
     // @todo If running unit tests, we can just skip adding this action
     $priority = 9;
     // because WP_Customize_Widgets::register_settings() happens at after_setup_theme priority 10
     add_action('after_setup_theme', array($this, 'init'), $priority);
     parent::__construct();
     // autoload classes and set $slug, $dir_path, and $dir_url vars
     $default_config = array('disable_widgets_init' => false, 'disable_widgets_factory' => false, 'active_modules' => array('non_autoloaded_widget_options' => !$this->is_wpcom_vip_prod(), 'widget_number_incrementing' => true, 'https_resource_proxy' => true, 'widget_posts' => true, 'optimized_widget_registration' => false, 'deferred_customize_widgets' => true), 'https_resource_proxy' => HTTPS_Resource_Proxy::default_config(), 'widget_posts' => Widget_Posts::default_config(), 'memory_limit' => '256M', 'max_memory_usage_percentage' => 0.75);
     $this->config = array_merge($default_config, $config);
 }
 /**
  * Set up.
  */
 function setUp()
 {
     global $wp_customize;
     require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
     parent::setUp();
     $this->widget_posts = new Widget_Posts($this->plugin);
     $this->plugin->widget_number_incrementing = new Widget_Number_Incrementing($this->plugin);
     $this->widget_posts->init();
     wp_widgets_init();
     $this->widget_posts->register_instance_post_type();
     $this->widget_posts->migrate_widgets_from_options();
     wp_set_current_user($this->factory->user->create(array('role' => 'administrator')));
     $wp_customize = $this->customize_manager = new \WP_Customize_Manager();
     $this->widget_posts->add_customize_hooks();
     // This did nothing in init since Customizer was not loaded.
     // Make sure the widgets get re-registered now using the new settings source.
     global $wp_registered_widgets;
     $wp_registered_widgets = array();
     wp_widgets_init();
     do_action('wp_loaded');
 }
 function test_delete_post_id_lookup_cache_on_pre_post_update()
 {
     $this->init_and_migrate();
     $post = $this->module->insert_widget('search', array('title' => 'Hi'));
     $this->assertEquals($post->ID, wp_cache_get($post->post_name, Widget_Posts::POST_NAME_TO_POST_ID_CACHE_GROUP));
     $old_post_name = $post->post_name;
     $new_post_name = $post->post_name . '123';
     $post_data = $post->to_array();
     $post_data['post_name'] = $new_post_name;
     wp_insert_post($post_data);
     $this->assertEmpty(wp_cache_get($old_post_name, Widget_Posts::POST_NAME_TO_POST_ID_CACHE_GROUP));
     $this->assertEquals($post->ID, wp_cache_get($new_post_name, Widget_Posts::POST_NAME_TO_POST_ID_CACHE_GROUP));
 }
 /**
  * \WP_Widget::update_callback(): $old_instance = isset($all_instances[$number]) ? $all_instances[$number] : array();
  * \WP_Widget::display_callback(): $instance = $instance[$this->number];
  * \WP_Widget::form_callback(): $instance = $all_instances[ $widget_args['number'] ];
  *
  * @param int|string $key Array key.
  * @return array|int|null
  */
 public function offsetGet($key)
 {
     if ('_multiwidget' === $key) {
         return 1;
     }
     if (!$this->offsetExists($key)) {
         return null;
     }
     $value = parent::offsetGet($key);
     if (is_int($value)) {
         // Fetch the widget post_content_filtered and store it in the array.
         $post = get_post($value);
         $value = Widget_Posts::get_post_content_filtered($post);
         $this->offsetSet($key, $value);
     }
     return $value;
 }
 /**
  * @see Widget_Settings::current()
  */
 function test_current()
 {
     $settings = $this->create_widget_settings('meta', 3);
     $shallow_settings = $settings->getArrayCopy();
     foreach ($settings as $widget_number => $instance) {
         $this->assertInternalType('array', $instance);
         $this->assertContains('widget_posts', $instance['title']);
         $this->assertEquals($instance, Widget_Posts::get_post_content_filtered(get_post($shallow_settings[$widget_number])));
     }
 }