Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/wp-admin/includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,11 @@ function get_sample_permalink_html( $post, $new_title = null, $new_slug = null )
$preview_target = " target='wp-preview-{$post->ID}'";
} else {
if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) {
$view_link = get_permalink( $post );
if ( 'attachment' === $post->post_type && ! get_option( 'wp_attachment_pages_enabled' ) ) {
$view_link = wp_get_attachment_url( $post->ID );
} else {
$view_link = get_permalink( $post );
}
} else {
// Allow non-published (private, future) to be viewed at a pretty permalink, in case $post->post_name is set.
$view_link = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, $permalink );
Expand Down
17 changes: 17 additions & 0 deletions src/wp-admin/options-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@
<?php do_settings_fields( 'media', 'default' ); ?>
</table>

<h2 class="title"><?php _e( 'Attachment pages' ); ?></h2>
<table class="form-table" role="presentation">
<tr>
<th scope="row"><?php _e( 'Attachment pages' ); ?></th>
<td>
<fieldset>
<legend class="screen-reader-text"><span><?php _e( 'Attachment pages' ); ?></span></legend>
<label for="wp_attachment_pages_enabled">
<input name="wp_attachment_pages_enabled" type="checkbox" id="wp_attachment_pages_enabled" value="1" <?php checked( '1', (string) get_option( 'wp_attachment_pages_enabled' ) ); ?> />
<?php _e( 'Enable attachment pages for uploaded media files' ); ?>
</label>
<p class="description"><?php _e( 'When disabled, attachment page URLs redirect to the media file URL.' ); ?></p>
</fieldset>
</td>
</tr>
</table>

<?php
/**
* @global array $wp_settings
Expand Down
1 change: 1 addition & 0 deletions src/wp-admin/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
'image_default_size',
'image_default_align',
'image_default_link_type',
'wp_attachment_pages_enabled',
),
'reading' => array(
'posts_per_page',
Expand Down
9 changes: 9 additions & 0 deletions src/wp-includes/media-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ function wp_print_media_templates() {
</script>

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

Expand Down Expand Up @@ -881,13 +882,15 @@ function wp_print_media_templates() {
<?php esc_html_e( 'Media File' ); ?>
<# } #>
</option>
<?php if ( $wp_attachment_pages_enabled ) : ?>
<option value="post">
<# if ( data.model.canEmbed ) { #>
<?php esc_html_e( 'Link to Attachment Page' ); ?>
<# } else { #>
<?php esc_html_e( 'Attachment Page' ); ?>
<# } #>
</option>
<?php endif; ?>
<# if ( 'image' === data.type ) { #>
<option value="custom">
<?php esc_html_e( 'Custom URL' ); ?>
Expand Down Expand Up @@ -947,12 +950,16 @@ function wp_print_media_templates() {
data-user-setting="urlbutton"
<# } #>>

<?php if ( $wp_attachment_pages_enabled ) : ?>
<option value="post" <# if ( ! wp.media.galleryDefaults.link || 'post' === wp.media.galleryDefaults.link ) {
#>selected="selected"<# }
#>>
<?php esc_html_e( 'Attachment Page' ); ?>
</option>
<option value="file" <# if ( 'file' === wp.media.galleryDefaults.link ) { #>selected="selected"<# } #>>
<?php else : ?>
<option value="file" <# if ( ! wp.media.galleryDefaults.link || 'file' === wp.media.galleryDefaults.link || 'post' === wp.media.galleryDefaults.link ) { #>selected="selected"<# } #>>
<?php endif; ?>
<?php esc_html_e( 'Media File' ); ?>
</option>
<option value="none" <# if ( 'none' === wp.media.galleryDefaults.link ) { #>selected="selected"<# } #>>
Expand Down Expand Up @@ -1228,9 +1235,11 @@ function wp_print_media_templates() {
<option value="file">
<?php esc_html_e( 'Media File' ); ?>
</option>
<?php if ( $wp_attachment_pages_enabled ) : ?>
<option value="post">
<?php esc_html_e( 'Attachment Page' ); ?>
</option>
<?php endif; ?>
<# } else { #>
<option value="file">
<?php esc_html_e( 'Image URL' ); ?>
Expand Down
17 changes: 17 additions & 0 deletions tests/phpunit/tests/admin/includesPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,23 @@ public function test_get_sample_permalink_html_should_use_pretty_permalink_for_v
$this->assertStringContainsString( '>' . urldecode( get_permalink( $post ) ) . '<', $found );
}

/**
* @ticket 65169
*/
public function test_get_sample_permalink_html_should_use_attachment_file_url_when_attachment_pages_are_disabled() {
$this->set_permalink_structure( '/%postname%/' );

wp_set_current_user( self::$admin_id );
update_option( 'wp_attachment_pages_enabled', 0 );

$attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/images/canola.jpg' );

$found = get_sample_permalink_html( $attachment_id );

$this->assertStringContainsString( 'href="' . wp_get_attachment_url( $attachment_id ) . '"', $found );
$this->assertStringNotContainsString( 'href="' . get_permalink( $attachment_id ) . '"', $found );
}

/**
* @ticket 32954
* @ticket 18306
Expand Down
28 changes: 28 additions & 0 deletions tests/phpunit/tests/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,34 @@ public function test_wp_prepare_attachment_for_js_without_image_sizes() {
$this->assertArrayHasKey( 'sizes', $prepped );
}

/**
* @ticket 65169
*/
public function test_wp_print_media_templates_hides_attachment_page_link_options_when_disabled() {
update_option( 'wp_attachment_pages_enabled', 0 );

ob_start();
wp_print_media_templates();
$output = ob_get_clean();

$this->assertStringNotContainsString( '<option value="post">', $output );
$this->assertStringContainsString( 'Media File', $output );
}

/**
* @ticket 65169
*/
public function test_wp_print_media_templates_shows_attachment_page_link_options_when_enabled() {
update_option( 'wp_attachment_pages_enabled', 1 );

ob_start();
wp_print_media_templates();
$output = ob_get_clean();

$this->assertStringContainsString( '<option value="post">', $output );
$this->assertStringContainsString( 'Attachment Page', $output );
}

/**
* @ticket 19067
* @expectedDeprecated wp_convert_bytes_to_hr
Expand Down
Loading