Skip to content

Commit

Permalink
Framework: Pass editor initial settings as direct argument (#9921)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth authored Sep 17, 2018
1 parent 2b42db5 commit cd6ae73
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -1317,41 +1317,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 @@ -1489,10 +1480,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 @@ -1508,7 +1498,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

0 comments on commit cd6ae73

Please sign in to comment.