| | 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 | |