Skip to content

Commit feabf3d

Browse files
committed
Media: Hide fallback file URL on attachment edit screen
1 parent d7a80fc commit feabf3d

2 files changed

Lines changed: 95 additions & 2 deletions

File tree

src/wp-admin/includes/media.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3194,7 +3194,11 @@ function edit_form_image_editor( $post ) {
31943194

31953195
wp_maybe_generate_attachment_metadata( $post );
31963196

3197-
echo wp_audio_shortcode( array( 'src' => $att_url ) );
3197+
$preview = wp_audio_shortcode( array( 'src' => $att_url ) );
3198+
3199+
if ( ! str_contains( $preview, 'class="wp-embedded-audio"' ) ) :
3200+
echo $preview;
3201+
endif;
31983202

31993203
elseif ( $attachment_id && wp_attachment_is( 'video', $post ) ) :
32003204

@@ -3221,7 +3225,11 @@ function edit_form_image_editor( $post ) {
32213225
$attr['poster'] = wp_get_attachment_url( $thumb_id );
32223226
}
32233227

3224-
echo wp_video_shortcode( $attr );
3228+
$preview = wp_video_shortcode( $attr );
3229+
3230+
if ( ! str_contains( $preview, 'class="wp-embedded-video"' ) ) :
3231+
echo $preview;
3232+
endif;
32253233

32263234
elseif ( isset( $thumb_url[0] ) ) :
32273235
?>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/**
4+
* @group admin
5+
*/
6+
class Tests_Admin_IncludesMedia extends WP_UnitTestCase {
7+
/**
8+
* The ID of an administrator user.
9+
*
10+
* @var int
11+
*/
12+
protected static $admin_id;
13+
14+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
15+
require_once ABSPATH . 'wp-admin/includes/media.php';
16+
17+
self::$admin_id = $factory->user->create(
18+
array(
19+
'role' => 'administrator',
20+
)
21+
);
22+
}
23+
24+
public function set_up(): void {
25+
parent::set_up();
26+
27+
wp_set_current_user( self::$admin_id );
28+
set_current_screen( 'post.php' );
29+
}
30+
31+
public function tear_down(): void {
32+
unset( $_GET['image-editor'] );
33+
parent::tear_down();
34+
}
35+
36+
/**
37+
* @ticket 64929
38+
*/
39+
public function test_edit_form_image_editor_skips_fallback_link_for_non_previewable_video() {
40+
$attachment_id = $this->create_upload_attachment_from_contents( 'sample.avi', 'RIFF0000AVI LIST' );
41+
$post = get_post( $attachment_id );
42+
43+
ob_start();
44+
edit_form_image_editor( $post );
45+
$markup = ob_get_clean();
46+
47+
$this->assertStringNotContainsString( 'wp-embedded-video', $markup );
48+
$this->assertStringNotContainsString( wp_get_attachment_url( $attachment_id ), $markup );
49+
}
50+
51+
/**
52+
* @ticket 64929
53+
*/
54+
public function test_edit_form_image_editor_keeps_preview_for_supported_video() {
55+
$attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/uploads/small-video.mp4' );
56+
$post = get_post( $attachment_id );
57+
58+
ob_start();
59+
edit_form_image_editor( $post );
60+
$markup = ob_get_clean();
61+
62+
$this->assertStringContainsString( '<video', $markup );
63+
$this->assertStringNotContainsString( 'wp-embedded-video', $markup );
64+
}
65+
66+
/**
67+
* Creates an uploaded attachment from raw file contents.
68+
*
69+
* @param string $filename The file name to use for the upload.
70+
* @param string $contents The file contents.
71+
* @return int Attachment ID.
72+
*/
73+
private function create_upload_attachment_from_contents( $filename, $contents ) {
74+
$temp_file = trailingslashit( get_temp_dir() ) . wp_generate_password( 8, false ) . '-' . $filename;
75+
file_put_contents( $temp_file, $contents );
76+
77+
$attachment_id = self::factory()->attachment->create_upload_object( $temp_file );
78+
79+
unlink( $temp_file );
80+
81+
$this->assertIsInt( $attachment_id );
82+
83+
return $attachment_id;
84+
}
85+
}

0 commit comments

Comments
 (0)