Make WordPress Core

Changeset 40071


Ignore:
Timestamp:
02/17/2017 06:46:45 AM (8 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.
Merges [40070] to the 4.7 branch.
Fixes #39277, #39304.

Location:
branches/4.7
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.7

  • branches/4.7/src/wp-includes/media.php

    r39654 r40071  
    36803680                $srcs = array();
    36813681
     3682                $shortcode_attrs = shortcode_parse_atts( $shortcode[3] );
     3683                if ( ! is_array( $shortcode_attrs ) ) {
     3684                    $shortcode_attrs = array();
     3685                }
     3686
     3687                // Specify the post id of the gallery we're viewing if the shortcode doesn't reference another post already.
     3688                if ( ! isset( $shortcode_attrs['id'] ) ) {
     3689                    $shortcode[3] .= ' id="' . intval( $post->ID ) . '"';
     3690                }
     3691
    36823692                $gallery = do_shortcode_tag( $shortcode );
    36833693                if ( $html ) {
     
    36863696                    preg_match_all( '#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER );
    36873697                    if ( ! empty( $src ) ) {
    3688                         foreach ( $src as $s )
     3698                        foreach ( $src as $s ) {
    36893699                            $srcs[] = $s[2];
     3700                        }
    36903701                    }
    36913702
    3692                     $data = shortcode_parse_atts( $shortcode[3] );
    3693                     $data['src'] = array_values( array_unique( $srcs ) );
    3694                     $galleries[] = $data;
     3703                    $galleries[] = array_merge(
     3704                        $shortcode_attrs,
     3705                        array(
     3706                            'src' => array_values( array_unique( $srcs ) )
     3707                        )
     3708                    );
    36953709                }
    36963710            }
  • branches/4.7/tests/phpunit/tests/media.php

    r39399 r40071  
    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.