Make WordPress Core

Ticket #32145: wp-get-attachment-url.diff

File wp-get-attachment-url.diff, 3.3 KB (added by pareshradadiya, 10 years ago)
  • src/wp-includes/post.php

     
    49594959 * @return string|bool Attachment URL, otherwise false.
    49604960 */
    49614961function wp_get_attachment_url( $post_id = 0 ) {
     4962        global $blog_id;
     4963
    49624964        $post_id = (int) $post_id;
    49634965        if ( !$post = get_post( $post_id ) )
    49644966                return false;
     
    49764978                                // Replace file location with url location.
    49774979                                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
    49784980                        } elseif ( false !== strpos($file, 'wp-content/uploads') ) {
    4979                                 $url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 );
     4981
     4982                                if ( is_main_site() ) {
     4983                                        $url = $uploads['baseurl'] . substr( $file, strpos( $file, 'wp-content/uploads' ) + 18 );
     4984                                } else {
     4985                                        $url = $uploads['baseurl'] . substr( $file, strpos( $file, 'wp-content/uploads/sites/' . $blog_id ) + 25 + strlen( $blog_id ) );
     4986                                }
     4987
    49804988                        } else {
    49814989                                // It's a newly-uploaded file, therefore $file is relative to the basedir.
    49824990                                $url = $uploads['baseurl'] . "/$file";
  • tests/phpunit/tests/post/attachments.php

     
    439439                $_SERVER['HTTP_HOST'] = $http_host;
    440440        }
    441441
     442        /**
     443         *  wp_get_attachment_url append /sites/$blog_id/ twice when attach same image to multiple posts in mu site
     444         */
     445        public function test_wp_get_attachment_url_not_working_in_mu_site_when_attach_same_image_to_multiple_posts() {
     446                $blog_id = $this->factory->blog->create();
     447
     448                switch_to_blog( $blog_id );
     449
     450                $siteurl = get_option( 'siteurl' );
     451                update_option( 'siteurl', set_url_scheme( $siteurl, 'https' ) );
     452
     453                $filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
     454                $contents = file_get_contents( $filename );
     455
     456                $upload = wp_upload_bits( basename( $filename ), null, $contents );
     457                $this->assertTrue( empty( $upload['error'] ) );
     458
     459                // Set attachment ID
     460                $attachment_id1 = $this->_make_attachment( $upload );
     461
     462                $file = get_post( $attachment_id1 );
     463                $args = array(
     464                        'post_mime_type' => $file->post_mime_type,
     465                        'guid'           => $file->guid,
     466                        'post_title'     => $file->post_title,
     467                        'post_content'   => $file->post_content,
     468                        'post_parent'    => 0,
     469                );
     470
     471                $attachment_id2 = wp_insert_attachment( $args, wp_get_attachment_url( $attachment_id1 ), '0' );
     472
     473                // Save server data for cleanup.
     474                $is_ssl    = is_ssl();
     475                $http_host = $_SERVER['HTTP_HOST'];
     476
     477                $_SERVER['HTTPS'] = 'on';
     478
     479                // Set server host to something random.
     480                $_SERVER['HTTP_HOST'] = 'some.otherhostname.com';
     481
     482                $url1 = wp_get_attachment_url( $attachment_id1 );
     483                $this->assertSame( set_url_scheme( $url1, 'http' ), $url1 );
     484
     485                $url2 = wp_get_attachment_url( $attachment_id2 );
     486                $this->assertSame( set_url_scheme( $url2, 'http' ), $url2 );
     487
     488                //Both attachments url should be same
     489                $this->assertSame( $url1, $url2 );
     490
     491                // Cleanup.
     492                $_SERVER['HTTPS']     = $is_ssl ? 'on' : 'off';
     493                $_SERVER['HTTP_HOST'] = $http_host;
     494
     495                restore_current_blog();
     496        }
     497
    442498        public function test_wp_attachment_is() {
    443499                $filename = DIR_TESTDATA . '/images/test-image.jpg';
    444500                $contents = file_get_contents( $filename );