Skip to content

Commit 7d64ecf

Browse files
committed
Editor: Skip auto-attaching media when attachment pages are enabled.
The block editor attaches unattached images in Image and Gallery blocks to the post when the post is saved. When attachment pages are enabled, attaching an image moves its attachment page to a URL under that post, and the old attachment page URL then returns a 404. This adds an `autoAttachMediaEnabled` setting to `get_default_block_editor_settings()`. It is `false` when the `wp_attachment_pages_enabled` option is on, and `true` otherwise. Sites can still change it with the `block_editor_settings_all` filter. Developed in: #14308 Props faisalahammad, rejaulalomkhan, andrewserong, ramonopoly, padams. Fixes #66321. git-svn-id: https://develop.svn.wordpress.org/trunk@64385 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 699681e commit 7d64ecf

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎src/wp-includes/block-editor.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ function get_default_block_editor_settings() {
214214
'alignWide' => get_theme_support( 'align-wide' ),
215215
'allowedBlockTypes' => true,
216216
'allowedMimeTypes' => get_allowed_mime_types(),
217+
/*
218+
* Attaching media to a post moves its attachment page URL, and the old URL then returns a 404.
219+
* Only attach on save when attachment pages are disabled, since they then redirect to the file.
220+
*/
221+
'autoAttachMediaEnabled' => ! get_option( 'wp_attachment_pages_enabled' ),
217222
'defaultEditorStyles' => $default_editor_styles,
218223
'blockCategories' => get_default_block_categories(),
219224
'isRTL' => is_rtl(),

‎tests/phpunit/tests/blocks/editor.php‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,10 @@ public function test_get_allowed_block_types_deprecated_filter_post_editor() {
251251
public function test_get_default_block_editor_settings() {
252252
$settings = get_default_block_editor_settings();
253253

254-
$this->assertCount( 20, $settings );
254+
$this->assertCount( 21, $settings );
255255
$this->assertFalse( $settings['alignWide'] );
256256
$this->assertIsArray( $settings['allowedMimeTypes'] );
257+
$this->assertTrue( $settings['autoAttachMediaEnabled'] );
257258
$this->assertTrue( $settings['allowedBlockTypes'] );
258259
$this->assertSameSets(
259260
array(
@@ -351,6 +352,36 @@ public function test_get_default_block_editor_settings() {
351352
$this->assertTrue( $settings['__unstableGalleryWithImageBlocks'] );
352353
}
353354

355+
/**
356+
* Tests that media is not attached to the post on save when attachment pages are enabled.
357+
*
358+
* @ticket 66321
359+
*
360+
* @dataProvider data_get_default_block_editor_settings_auto_attach_media_enabled
361+
*
362+
* @param int $attachment_pages_enabled Value of the `wp_attachment_pages_enabled` option.
363+
* @param bool $expected Expected value of the `autoAttachMediaEnabled` setting.
364+
*/
365+
public function test_get_default_block_editor_settings_auto_attach_media_enabled( $attachment_pages_enabled, $expected ) {
366+
update_option( 'wp_attachment_pages_enabled', $attachment_pages_enabled );
367+
368+
$settings = get_default_block_editor_settings();
369+
370+
$this->assertSame( $expected, $settings['autoAttachMediaEnabled'] );
371+
}
372+
373+
/**
374+
* Data provider.
375+
*
376+
* @return array[]
377+
*/
378+
public function data_get_default_block_editor_settings_auto_attach_media_enabled() {
379+
return array(
380+
'attachment pages disabled' => array( 0, true ),
381+
'attachment pages enabled' => array( 1, false ),
382+
);
383+
}
384+
354385
/**
355386
* @ticket 56815
356387
*/

0 commit comments

Comments
 (0)