Media: Avoid PHP Warnings in `get_post_galleries()` when processing empty `[gallery]` shortcodes and avoid returning the incorrect results when the global `$post` does not match the provided post ID.
Props dd32, joemcgill, seanchayes. Fixes #39277, #39304. git-svn-id: https://develop.svn.wordpress.org/trunk@40070 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
bfeda161b7
commit
82f99ca927
|
@ -3681,19 +3681,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 ) )
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue