Skip to content

Instantly share code, notes, and snippets.

@aduth
Created September 14, 2018 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aduth/bd19a020a01fd40b58242d45b4bdfd94 to your computer and use it in GitHub Desktop.
Save aduth/bd19a020a01fd40b58242d45b4bdfd94 to your computer and use it in GitHub Desktop.
diff --git a/lib/client-assets.php b/lib/client-assets.php
index b0f3905cc..b0937a910 100644
--- a/lib/client-assets.php
+++ b/lib/client-assets.php
@@ -1131,6 +1131,41 @@ function gutenberg_default_post_format_template( $settings, $post ) {
}
add_filter( 'block_editor_settings', 'gutenberg_default_post_format_template', 10, 2 );
+/**
+ * Assigns initial edit values for the demo post content and title.
+ *
+ * @param array $initial_edits Default initial edits.
+ * @param WP_Post $post Post being edited.
+ *
+ * @return array Filtered initial edits, including demo content and title if
+ * viewing the demo page.
+ */
+function gutenberg_demo_initial_edits( $initial_edits, $post ) {
+ $is_new_post = 'auto-draft' === $post->post_status;
+ $is_demo = isset( $_GET['gutenberg-demo'] );
+ if ( $is_demo ) {
+ // Prepopulate with some test content in demo.
+ ob_start();
+ include gutenberg_dir_path() . 'post-content.php';
+ $demo_content = ob_get_clean();
+
+ $initial_edits = array_merge(
+ $initial_edits,
+ array(
+ 'title' => array(
+ 'raw' => __( 'Welcome to the Gutenberg Editor', 'gutenberg' ),
+ ),
+ 'content' => array(
+ 'raw' => $demo_content,
+ ),
+ )
+ );
+ }
+
+ return $initial_edits;
+}
+add_filter( 'block_editor_initial_edits', 'gutenberg_demo_initial_edits', 10, 2 );
+
/**
* The code editor settings that were last captured by
* gutenberg_capture_code_editor_settings().
@@ -1318,21 +1353,7 @@ function gutenberg_editor_scripts_and_styles( $hook ) {
// 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();
-
- $initial_edits = array(
- 'title' => array(
- 'raw' => __( 'Welcome to the Gutenberg Editor', 'gutenberg' ),
- ),
- 'content' => array(
- 'raw' => $demo_content,
- ),
- );
- } elseif ( $is_new_post ) {
+ if ( $is_new_post ) {
// Override "(Auto Draft)" new post default title with empty string,
// or filtered value.
$initial_edits = array(
@@ -1497,6 +1518,18 @@ JS;
*/
$editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post );
+ /**
+ * Filters the initial edits to pass to the block editor. An initial edit
+ * is one which is considered non-dirtying, but should be included in the
+ * first save payload.
+ *
+ * @since 3.9.0
+ *
+ * @param array $initial_edits Default initial edits.
+ * @param WP_Post $post Post being edited.
+ */
+ $initial_edits = apply_filters( 'block_editor_initial_edits', $initial_edits, $post );
+
$script = sprintf(
$init_script,
$post->post_type,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment