Skip to content

Commit

Permalink
Templates: Assign default post format block as template setting (#9287)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth authored Aug 29, 2018
1 parent 5a630c2 commit 540778b
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 19 deletions.
1 change: 1 addition & 0 deletions docs/reference/deprecated.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Gutenberg's deprecation policy is intended to support backwards-compatibility fo
- `wp.editor.getColorClass` has been renamed. Please use `wp.editor.getColorClassName` instead.
- `value` property in color objects passed by `wp.editor.withColors` has been removed. Please use color property instead.
- The Subheading block has been removed. Please use the Paragraph block instead.
- `wp.blocks.getDefaultBlockForPostFormat` has been removed.

## 3.8.0

Expand Down
50 changes: 50 additions & 0 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,46 @@ function gutenberg_enqueue_registered_block_scripts_and_styles() {
add_action( 'enqueue_block_assets', 'gutenberg_enqueue_registered_block_scripts_and_styles' );
add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_registered_block_scripts_and_styles' );

/**
* Assigns a default editor template with a default block by post format, if
* not otherwise assigned for a new post of type "post".
*
* @param array $settings Default editor settings.
* @param WP_Post $post Post being edited.
*
* @return array Filtered block editor settings.
*/
function gutenberg_default_post_format_template( $settings, $post ) {
// Only assign template for new posts without explicitly assigned template.
$is_new_post = 'auto-draft' === $post->post_status;
if ( $is_new_post && ! isset( $settings['template'] ) && 'post' === $post->post_type ) {
switch ( get_post_format() ) {
case 'audio':
$default_block_name = 'core/audio';
break;
case 'gallery':
$default_block_name = 'core/gallery';
break;
case 'image':
$default_block_name = 'core/image';
break;
case 'quote':
$default_block_name = 'core/quote';
break;
case 'video':
$default_block_name = 'core/video';
break;
}

if ( isset( $default_block_name ) ) {
$settings['template'] = array( array( $default_block_name ) );
}
}

return $settings;
}
add_filter( 'block_editor_settings', 'gutenberg_default_post_format_template', 10, 2 );

/**
* The code editor settings that were last captured by
* gutenberg_capture_code_editor_settings().
Expand Down Expand Up @@ -1405,6 +1445,16 @@ function gutenberg_editor_scripts_and_styles( $hook ) {
} )();
JS;

/**
* Filters the settings to pass to the block editor.
*
* @since 3.7.0
*
* @param array $editor_settings Default editor settings.
* @param WP_Post $post Post being edited.
*/
$editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post );

$script = sprintf( $init_script, wp_json_encode( $editor_settings ), $post->post_type, $post->ID );
wp_add_inline_script( 'wp-edit-post', $script );

Expand Down
4 changes: 4 additions & 0 deletions packages/blocks/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
### Breaking Change

- The `isSharedBlock` function is removed. Use `isReusableBlock` instead.

### Deprecations

- The `getDefaultBlockForPostFormat` function has been deprecated.
6 changes: 6 additions & 0 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { get, isFunction, some } from 'lodash';
*/
import { applyFilters, addFilter } from '@wordpress/hooks';
import { select, dispatch } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';

/**
* Internal dependencies
Expand Down Expand Up @@ -228,6 +229,11 @@ export function getDefaultBlockName() {
* @return {string} Block name.
*/
export function getDefaultBlockForPostFormat( postFormat ) {
deprecated( 'getDefaultBlockForPostFormat', {
plugin: 'Gutenberg',
version: '3.9',
} );

const blockName = POST_FORMAT_BLOCK_MAP[ postFormat ];
if ( blockName && getBlockType( blockName ) ) {
return blockName;
Expand Down
20 changes: 1 addition & 19 deletions packages/editor/src/store/effects.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { get, last } from 'lodash';
import { last } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -10,8 +10,6 @@ import {
parse,
getBlockType,
switchToBlockType,
createBlock,
getDefaultBlockForPostFormat,
doBlocksMatchTemplate,
synchronizeBlocksWithTemplate,
} from '@wordpress/blocks';
Expand All @@ -25,14 +23,12 @@ import {
setupEditorState,
replaceBlocks,
createWarningNotice,
insertBlock,
selectBlock,
resetBlocks,
setTemplateValidity,
} from './actions';
import {
getBlock,
getBlockCount,
getBlockRootClientId,
getBlocks,
getPreviousBlockClientId,
Expand Down Expand Up @@ -138,8 +134,6 @@ export default {
);
} else if ( template ) {
blocks = synchronizeBlocksWithTemplate( [], template );
} else if ( getDefaultBlockForPostFormat( post.format ) ) {
blocks = [ createBlock( getDefaultBlockForPostFormat( post.format ) ) ];
} else {
blocks = [];
}
Expand Down Expand Up @@ -213,18 +207,6 @@ export default {
const message = spokenMessage || content;
speak( message, 'assertive' );
},

EDIT_POST( action, { getState } ) {
const format = get( action, [ 'edits', 'format' ] );
if ( ! format ) {
return;
}
const blockName = getDefaultBlockForPostFormat( format );
if ( blockName && getBlockCount( getState() ) === 0 ) {
return insertBlock( createBlock( blockName ) );
}
},

REMOVE_BLOCKS( action, { getState, dispatch } ) {
// if the action says previous block should not be selected don't do anything.
if ( ! action.selectPrevious ) {
Expand Down
10 changes: 10 additions & 0 deletions test/e2e/specs/__snapshots__/templates.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ exports[`templates Using a CPT with a predefined template Should respect user ed
<!-- /wp:column --></div>
<!-- /wp:columns -->"
`;
exports[`templates With default post format assigned should not populate edited post with default block for format 1`] = `""`;
exports[`templates With default post format assigned should not populate new page with default block for format 1`] = `""`;
exports[`templates With default post format assigned should populate new post with default block for format 1`] = `
"<!-- wp:image -->
<figure class=\\"wp-block-image\\"><img alt=\\"\\"/></figure>
<!-- /wp:image -->"
`;
45 changes: 45 additions & 0 deletions test/e2e/specs/templates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
getEditedPostContent,
saveDraft,
pressWithModifier,
visitAdmin,
clickBlockAppender,
} from '../support/utils';
import { activatePlugin, deactivatePlugin } from '../support/plugins';

Expand Down Expand Up @@ -53,4 +55,47 @@ describe( 'templates', () => {
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
} );

describe( 'With default post format assigned', () => {
const STANDARD_FORMAT_VALUE = '0';

async function setPostFormat( format ) {
await visitAdmin( 'options-writing.php' );
await page.select( '#default_post_format', format );
return Promise.all( [
page.waitForNavigation(),
page.click( '#submit' ),
] );
}

beforeAll( async () => await setPostFormat( 'image' ) );
afterAll( async () => await setPostFormat( STANDARD_FORMAT_VALUE ) );

it( 'should populate new post with default block for format', async () => {
await newPost();

expect( await getEditedPostContent() ).toMatchSnapshot();
} );

it( 'should not populate edited post with default block for format', async () => {
await newPost();

// Remove the default block template to verify that it's not
// re-added after saving and reloading the editor.
await page.type( '.editor-post-title__input', 'My Image Format' );
await clickBlockAppender();
await page.keyboard.press( 'Backspace' );
await page.keyboard.press( 'Backspace' );
await saveDraft();
await page.reload();

expect( await getEditedPostContent() ).toMatchSnapshot();
} );

it( 'should not populate new page with default block for format', async () => {
await newPost( { postType: 'page' } );

expect( await getEditedPostContent() ).toMatchSnapshot();
} );
} );
} );

0 comments on commit 540778b

Please sign in to comment.