Make WordPress Core

Changeset 40070 for trunk


Ignore:
Timestamp:
02/17/2017 06:45:31 AM (9 years ago)
Author:
dd32
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r39638 r40070  
    36823682                                $srcs = array();
    36833683
     3684                                $shortcode_attrs = shortcode_parse_atts( $shortcode[3] );
     3685                                if ( ! is_array( $shortcode_attrs ) ) {
     3686                                        $shortcode_attrs = array();
     3687                                }
     3688
     3689                                // Specify the post id of the gallery we're viewing if the shortcode doesn't reference another post already.
     3690                                if ( ! isset( $shortcode_attrs['id'] ) ) {
     3691                                        $shortcode[3] .= ' id="' . intval( $post->ID ) . '"';
     3692                                }
     3693
    36843694                                $gallery = do_shortcode_tag( $shortcode );
    36853695                                if ( $html ) {
     
    36883698                                        preg_match_all( '#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER );
    36893699                                        if ( ! empty( $src ) ) {
    3690                                                 foreach ( $src as $s )
     3700                                                foreach ( $src as $s ) {
    36913701                                                        $srcs[] = $s[2];
     3702                                                }
    36923703                                        }
    36933704
    3694                                         $data = shortcode_parse_atts( $shortcode[3] );
    3695                                         $data['src'] = array_values( array_unique( $srcs ) );
    3696                                         $galleries[] = $data;
     3705                                        $galleries[] = array_merge(
     3706                                                $shortcode_attrs,
     3707                                                array(
     3708                                                        'src' => array_values( array_unique( $srcs ) )
     3709                                                )
     3710                                        );
    36973711                                }
    36983712                        }
  • trunk/tests/phpunit/tests/media.php

    r39919 r40070  
    392392
    393393        /**
     394         * @ticket 39304
     395         */
     396        function test_post_galleries_images_without_global_post() {
     397                // Set up an unattached image.
     398                $this->factory->attachment->create_object( array(
     399                        'file' => 'test.jpg',
     400                        'post_parent' => 0,
     401                        'post_mime_type' => 'image/jpeg',
     402                        'post_type' => 'attachment'
     403                ) );
     404
     405                $post_id = $this->factory->post->create( array(
     406                        'post_content' => '[gallery]',
     407                ) );
     408
     409                $galleries = get_post_galleries( $post_id, false );
     410
     411                $this->assertEmpty( $galleries[0]['src'] );
     412        }
     413
     414        /**
     415         * @ticket 39304
     416         */
     417        function test_post_galleries_ignores_global_post() {
     418                $global_post_id = $this->factory->post->create( array(
     419                        'post_content' => 'Global Post',
     420                ) );
     421                $post_id = $this->factory->post->create( array(
     422                        'post_content' => '[gallery]',
     423                ) );
     424                $this->factory->attachment->create_object( array(
     425                        'file' => 'test.jpg',
     426                        'post_parent' => $post_id,
     427                        'post_mime_type' => 'image/jpeg',
     428                        'post_type' => 'attachment'
     429                ) );
     430                $expected_srcs = array(
     431                        'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg'
     432                );
     433
     434                // Set the global $post context to the other post.
     435                $GLOBALS['post'] = get_post( $global_post_id );
     436
     437                $galleries = get_post_galleries( $post_id, false );
     438
     439                $this->assertNotEmpty( $galleries[0]['src'] );
     440                $this->assertSame( $galleries[0]['src'], $expected_srcs );
     441        }
     442
     443        /**
     444         * @ticket 39304
     445         */
     446        function test_post_galleries_respects_id_attrs() {
     447                $post_id = $this->factory->post->create( array(
     448                        'post_content' => 'No gallery defined',
     449                ) );
     450                $post_id_two = $this->factory->post->create( array(
     451                        'post_content' => "[gallery id='$post_id']",
     452                ) );
     453                $this->factory->attachment->create_object( array(
     454                        'file' => 'test.jpg',
     455                        'post_parent' => $post_id,
     456                        'post_mime_type' => 'image/jpeg',
     457                        'post_type' => 'attachment'
     458                ) );
     459                $expected_srcs = array(
     460                        'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg'
     461                );
     462
     463                $galleries = get_post_galleries( $post_id_two, false );
     464
     465                // Set the global $post context
     466                $GLOBALS['post'] = get_post( $post_id_two );
     467                $galleries_with_global_context = get_post_galleries( $post_id_two, false );
     468
     469                // Check that the global post state doesn't affect the results
     470                $this->assertSame( $galleries, $galleries_with_global_context );
     471
     472                $this->assertNotEmpty( $galleries[0]['src'] );
     473                $this->assertSame( $galleries[0]['src'], $expected_srcs );
     474        }
     475
     476        /**
    394477         * @ticket 22960
    395478         */
Note: See TracChangeset for help on using the changeset viewer.