Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Framework: Pass editor initial settings as direct argument #9921

Merged
merged 1 commit into from
Sep 17, 2018
Merged
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
56 changes: 26 additions & 30 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -1316,41 +1316,32 @@ function gutenberg_editor_scripts_and_styles( $hook ) {
'after'
);

// Prepopulate with some test content in demo.
// Assign initial edits, if applicable. These are not initially assigned
// to the persisted post, but should be included in its save payload.
if ( $is_new_post && $is_demo ) {
// Prepopulate with some test content in demo.
ob_start();
include gutenberg_dir_path() . 'post-content.php';
$demo_content = ob_get_clean();

wp_add_inline_script(
'wp-edit-post',
sprintf(
'window._wpGutenbergDefaultPost = { title: %s, content: %s };',
wp_json_encode(
array(
'raw' => __( 'Welcome to the Gutenberg Editor', 'gutenberg' ),
)
),
wp_json_encode(
array(
'raw' => $demo_content,
)
)
)
$initial_edits = array(
'title' => array(
'raw' => __( 'Welcome to the Gutenberg Editor', 'gutenberg' ),
),
'content' => array(
'raw' => $demo_content,
),
);
} elseif ( $is_new_post ) {
wp_add_inline_script(
'wp-edit-post',
sprintf(
'window._wpGutenbergDefaultPost = { title: %s };',
wp_json_encode(
array(
'raw' => '',
'rendered' => apply_filters( 'the_title', '', $post->ID ),
)
)
)
// Override "(Auto Draft)" new post default title with empty string,
// or filtered value.
$initial_edits = array(
'title' => array(
'raw' => apply_filters( 'the_title', '', $post->ID ),
),
);
} else {
$initial_edits = null;
}

// Prepare Jed locale data.
Expand Down Expand Up @@ -1488,10 +1479,9 @@ function gutenberg_editor_scripts_and_styles( $hook ) {

$init_script = <<<JS
( function() {
var editorSettings = %s;
window._wpLoadGutenbergEditor = new Promise( function( resolve ) {
wp.domReady( function() {
resolve( wp.editPost.initializeEditor( 'editor', "%s", %d, editorSettings, window._wpGutenbergDefaultPost ) );
resolve( wp.editPost.initializeEditor( 'editor', "%s", %d, %s, %s ) );
} );
} );
} )();
Expand All @@ -1507,7 +1497,13 @@ function gutenberg_editor_scripts_and_styles( $hook ) {
*/
$editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post );

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

/**
Expand Down