Skip to content

Commit 00f3ebd

Browse files
shreyasikharCopilot
andcommitted
Enhance attachment handling: Use attachment file URL when pages are disabled and update media templates accordingly.
Co-authored-by: Copilot <copilot@github.com>
1 parent bd4e3c9 commit 00f3ebd

4 files changed

Lines changed: 59 additions & 1 deletion

File tree

src/wp-admin/includes/post.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,11 @@ function get_sample_permalink_html( $post, $new_title = null, $new_slug = null )
15701570
$preview_target = " target='wp-preview-{$post->ID}'";
15711571
} else {
15721572
if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) {
1573-
$view_link = get_permalink( $post );
1573+
if ( 'attachment' === $post->post_type && ! get_option( 'wp_attachment_pages_enabled' ) ) {
1574+
$view_link = wp_get_attachment_url( $post->ID );
1575+
} else {
1576+
$view_link = get_permalink( $post );
1577+
}
15741578
} else {
15751579
// Allow non-published (private, future) to be viewed at a pretty permalink, in case $post->post_name is set.
15761580
$view_link = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, $permalink );

src/wp-includes/media-template.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ function wp_print_media_templates() {
822822
</script>
823823

824824
<?php // Template for the Attachment display settings, used for example in the sidebar. ?>
825+
<?php $wp_attachment_pages_enabled = (bool) get_option( 'wp_attachment_pages_enabled' ); ?>
825826
<script type="text/html" id="tmpl-attachment-display-settings">
826827
<h2><?php _e( 'Attachment Display Settings' ); ?></h2>
827828

@@ -881,13 +882,15 @@ function wp_print_media_templates() {
881882
<?php esc_html_e( 'Media File' ); ?>
882883
<# } #>
883884
</option>
885+
<?php if ( $wp_attachment_pages_enabled ) : ?>
884886
<option value="post">
885887
<# if ( data.model.canEmbed ) { #>
886888
<?php esc_html_e( 'Link to Attachment Page' ); ?>
887889
<# } else { #>
888890
<?php esc_html_e( 'Attachment Page' ); ?>
889891
<# } #>
890892
</option>
893+
<?php endif; ?>
891894
<# if ( 'image' === data.type ) { #>
892895
<option value="custom">
893896
<?php esc_html_e( 'Custom URL' ); ?>
@@ -947,12 +950,16 @@ function wp_print_media_templates() {
947950
data-user-setting="urlbutton"
948951
<# } #>>
949952

953+
<?php if ( $wp_attachment_pages_enabled ) : ?>
950954
<option value="post" <# if ( ! wp.media.galleryDefaults.link || 'post' === wp.media.galleryDefaults.link ) {
951955
#>selected="selected"<# }
952956
#>>
953957
<?php esc_html_e( 'Attachment Page' ); ?>
954958
</option>
955959
<option value="file" <# if ( 'file' === wp.media.galleryDefaults.link ) { #>selected="selected"<# } #>>
960+
<?php else : ?>
961+
<option value="file" <# if ( ! wp.media.galleryDefaults.link || 'file' === wp.media.galleryDefaults.link || 'post' === wp.media.galleryDefaults.link ) { #>selected="selected"<# } #>>
962+
<?php endif; ?>
956963
<?php esc_html_e( 'Media File' ); ?>
957964
</option>
958965
<option value="none" <# if ( 'none' === wp.media.galleryDefaults.link ) { #>selected="selected"<# } #>>
@@ -1228,9 +1235,11 @@ function wp_print_media_templates() {
12281235
<option value="file">
12291236
<?php esc_html_e( 'Media File' ); ?>
12301237
</option>
1238+
<?php if ( $wp_attachment_pages_enabled ) : ?>
12311239
<option value="post">
12321240
<?php esc_html_e( 'Attachment Page' ); ?>
12331241
</option>
1242+
<?php endif; ?>
12341243
<# } else { #>
12351244
<option value="file">
12361245
<?php esc_html_e( 'Image URL' ); ?>

tests/phpunit/tests/admin/includesPost.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,23 @@ public function test_get_sample_permalink_html_should_use_pretty_permalink_for_v
706706
$this->assertStringContainsString( '>' . urldecode( get_permalink( $post ) ) . '<', $found );
707707
}
708708

709+
/**
710+
* @ticket 65169
711+
*/
712+
public function test_get_sample_permalink_html_should_use_attachment_file_url_when_attachment_pages_are_disabled() {
713+
$this->set_permalink_structure( '/%postname%/' );
714+
715+
wp_set_current_user( self::$admin_id );
716+
update_option( 'wp_attachment_pages_enabled', 0 );
717+
718+
$attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/images/canola.jpg' );
719+
720+
$found = get_sample_permalink_html( $attachment_id );
721+
722+
$this->assertStringContainsString( 'href="' . wp_get_attachment_url( $attachment_id ) . '"', $found );
723+
$this->assertStringNotContainsString( 'href="' . get_permalink( $attachment_id ) . '"', $found );
724+
}
725+
709726
/**
710727
* @ticket 32954
711728
* @ticket 18306

tests/phpunit/tests/media.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,34 @@ public function test_wp_prepare_attachment_for_js_without_image_sizes() {
494494
$this->assertArrayHasKey( 'sizes', $prepped );
495495
}
496496

497+
/**
498+
* @ticket 65169
499+
*/
500+
public function test_wp_print_media_templates_hides_attachment_page_link_options_when_disabled() {
501+
update_option( 'wp_attachment_pages_enabled', 0 );
502+
503+
ob_start();
504+
wp_print_media_templates();
505+
$output = ob_get_clean();
506+
507+
$this->assertStringNotContainsString( '<option value="post">', $output );
508+
$this->assertStringContainsString( 'Media File', $output );
509+
}
510+
511+
/**
512+
* @ticket 57913
513+
*/
514+
public function test_wp_print_media_templates_shows_attachment_page_link_options_when_enabled() {
515+
update_option( 'wp_attachment_pages_enabled', 1 );
516+
517+
ob_start();
518+
wp_print_media_templates();
519+
$output = ob_get_clean();
520+
521+
$this->assertStringContainsString( '<option value="post">', $output );
522+
$this->assertStringContainsString( 'Attachment Page', $output );
523+
}
524+
497525
/**
498526
* @ticket 19067
499527
* @expectedDeprecated wp_convert_bytes_to_hr

0 commit comments

Comments
 (0)