/**
  * \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_Posts::migrate_widgets_from_options()
  */
 function test_migrate_widgets_from_options()
 {
     $id_base = 'meta';
     $original_instances = get_option("widget_{$id_base}");
     unset($original_instances['_multiwidget']);
     $instance = new Widget_Posts($this->plugin);
     $instance->init();
     wp_widgets_init();
     $instance->register_instance_post_type();
     $this->assertEmpty($instance->get_widget_instance_numbers($id_base));
     $instance->migrate_widgets_from_options();
     // @todo this should be getting called! wp_cache_delete( $id_base, 'widget_instance_numbers' );
     $this->assertGreaterThan(0, did_action('widget_posts_import_success'));
     $shallow_instances = $instance->get_widget_instance_numbers($id_base);
     $this->assertNotEmpty($shallow_instances);
     $this->assertEqualSets(array_keys($original_instances), array_keys($shallow_instances));
     foreach ($shallow_instances as $widget_number => $post_id) {
         $this->assertInternalType('int', $widget_number);
         $this->assertInternalType('int', $post_id);
         $post = get_post($post_id);
         $this->assertEquals(Widget_Posts::INSTANCE_POST_TYPE, $post->post_type);
         $this->assertEquals("{$id_base}-{$widget_number}", $post->post_name);
         $this->assertEquals($original_instances[$widget_number], Widget_Posts::get_post_content_filtered($post));
     }
     // Before any Widget_Settings::offsetGet() gets called, try iterating
     $settings = new Widget_Settings($shallow_instances);
     foreach ($settings as $widget_number => $hydrated_instance) {
         $this->assertEquals($original_instances[$widget_number], $hydrated_instance);
     }
     // Now make sure that offsetGet() also does the right thing.
     $settings = new Widget_Settings($shallow_instances);
     foreach ($original_instances as $widget_number => $original_instance) {
         $this->assertArrayHasKey($widget_number, $settings);
         $this->assertEquals($original_instance, $settings[$widget_number]);
     }
     $this->assertEquals(0, did_action('update_option_widget_meta'), 'Expected update_option( "widget_meta" ) to short-circuit.');
 }
 /**
  * @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])));
     }
 }