diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index deb18a40b6..3411b84c80 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -3679,19 +3679,33 @@ function get_post_galleries( $post, $html = true ) { if ( 'gallery' === $shortcode[2] ) { $srcs = array(); + $shortcode_attrs = shortcode_parse_atts( $shortcode[3] ); + if ( ! is_array( $shortcode_attrs ) ) { + $shortcode_attrs = array(); + } + + // Specify the post id of the gallery we're viewing if the shortcode doesn't reference another post already. + if ( ! isset( $shortcode_attrs['id'] ) ) { + $shortcode[3] .= ' id="' . intval( $post->ID ) . '"'; + } + $gallery = do_shortcode_tag( $shortcode ); if ( $html ) { $galleries[] = $gallery; } else { preg_match_all( '#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER ); if ( ! empty( $src ) ) { - foreach ( $src as $s ) + foreach ( $src as $s ) { $srcs[] = $s[2]; + } } - $data = shortcode_parse_atts( $shortcode[3] ); - $data['src'] = array_values( array_unique( $srcs ) ); - $galleries[] = $data; + $galleries[] = array_merge( + $shortcode_attrs, + array( + 'src' => array_values( array_unique( $srcs ) ) + ) + ); } } } diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index d8c28c2191..af8c2f1428 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -390,6 +390,89 @@ BLOB; $this->assertEquals( $srcs, array( $ids1_srcs, $ids2_srcs ) ); } + /** + * @ticket 39304 + */ + function test_post_galleries_images_without_global_post() { + // Set up an unattached image. + $this->factory->attachment->create_object( array( + 'file' => 'test.jpg', + 'post_parent' => 0, + 'post_mime_type' => 'image/jpeg', + 'post_type' => 'attachment' + ) ); + + $post_id = $this->factory->post->create( array( + 'post_content' => '[gallery]', + ) ); + + $galleries = get_post_galleries( $post_id, false ); + + $this->assertEmpty( $galleries[0]['src'] ); + } + + /** + * @ticket 39304 + */ + function test_post_galleries_ignores_global_post() { + $global_post_id = $this->factory->post->create( array( + 'post_content' => 'Global Post', + ) ); + $post_id = $this->factory->post->create( array( + 'post_content' => '[gallery]', + ) ); + $this->factory->attachment->create_object( array( + 'file' => 'test.jpg', + 'post_parent' => $post_id, + 'post_mime_type' => 'image/jpeg', + 'post_type' => 'attachment' + ) ); + $expected_srcs = array( + 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg' + ); + + // Set the global $post context to the other post. + $GLOBALS['post'] = get_post( $global_post_id ); + + $galleries = get_post_galleries( $post_id, false ); + + $this->assertNotEmpty( $galleries[0]['src'] ); + $this->assertSame( $galleries[0]['src'], $expected_srcs ); + } + + /** + * @ticket 39304 + */ + function test_post_galleries_respects_id_attrs() { + $post_id = $this->factory->post->create( array( + 'post_content' => 'No gallery defined', + ) ); + $post_id_two = $this->factory->post->create( array( + 'post_content' => "[gallery id='$post_id']", + ) ); + $this->factory->attachment->create_object( array( + 'file' => 'test.jpg', + 'post_parent' => $post_id, + 'post_mime_type' => 'image/jpeg', + 'post_type' => 'attachment' + ) ); + $expected_srcs = array( + 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg' + ); + + $galleries = get_post_galleries( $post_id_two, false ); + + // Set the global $post context + $GLOBALS['post'] = get_post( $post_id_two ); + $galleries_with_global_context = get_post_galleries( $post_id_two, false ); + + // Check that the global post state doesn't affect the results + $this->assertSame( $galleries, $galleries_with_global_context ); + + $this->assertNotEmpty( $galleries[0]['src'] ); + $this->assertSame( $galleries[0]['src'], $expected_srcs ); + } + /** * @ticket 22960 */