Skip to content

Commit

Permalink
Plugin: Populate demo content by default content filters (#13553)
Browse files Browse the repository at this point in the history
* Plugin: Populate demo content by default content filters

* Plugin: Avoid checking post status on default content

Assumed to be called only in the process of generating a new post for edit (the same assumption checked by testing 'auto-draft' status)

* Plugin: Remove unreachable initial_edits code
  • Loading branch information
aduth committed Jan 29, 2019
1 parent bf1c941 commit f84ceca
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 27 deletions.
13 changes: 0 additions & 13 deletions gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,6 @@ function gutenberg_init( $return, $post ) {
return true;
}

/**
* Redirects the demo page to edit a new post.
*/
function gutenberg_redirect_demo() {
global $pagenow;

if ( 'admin.php' === $pagenow && isset( $_GET['page'] ) && 'gutenberg' === $_GET['page'] ) {
wp_safe_redirect( admin_url( 'post-new.php?gutenberg-demo' ) );
exit;
}
}
add_action( 'admin_init', 'gutenberg_redirect_demo' );

/**
* Adds the filters to register additional links for the Gutenberg editor in
* the post/page screens.
Expand Down
14 changes: 1 addition & 13 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -865,8 +865,6 @@ function gutenberg_get_available_image_sizes() {
* @param string $hook Screen name.
*/
function gutenberg_editor_scripts_and_styles( $hook ) {
$is_demo = isset( $_GET['gutenberg-demo'] );

global $wp_scripts, $wp_meta_boxes;

// Add "wp-hooks" as dependency of "heartbeat".
Expand Down Expand Up @@ -983,17 +981,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' => __( 'Welcome to the Gutenberg Editor', 'gutenberg' ),
'content' => $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(
Expand Down
64 changes: 64 additions & 0 deletions lib/demo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Supports for populating the Gutenberg demo content new post.
*
* @package gutenberg
*/

if ( ! defined( 'ABSPATH' ) ) {
die( 'Silence is golden.' );
}

/**
* Redirects the demo page to edit a new post.
*/
function gutenberg_redirect_demo() {
global $pagenow;

if ( 'admin.php' === $pagenow && isset( $_GET['page'] ) && 'gutenberg' === $_GET['page'] ) {
wp_safe_redirect( admin_url( 'post-new.php?gutenberg-demo' ) );
exit;
}
}
add_action( 'admin_init', 'gutenberg_redirect_demo' );

/**
* Assigns the default content for the Gutenberg demo post.
*
* @param string $content Default post content.
*
* @return string Demo content if creating a new Gutenberg demo post, or the
* default content otherwise.
*/
function gutenberg_default_demo_content( $content ) {
$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';
return ob_get_clean();
}

return $content;
}
add_filter( 'default_content', 'gutenberg_default_demo_content' );

/**
* Assigns the default title for the Gutenberg demo post.
*
* @param string $title Default post title.
*
* @return string Demo title if creating a new Gutenberg demo post, or the
* default title otherwise.
*/
function gutenberg_default_demo_title( $title ) {
$is_demo = isset( $_GET['gutenberg-demo'] );

if ( $is_demo ) {
return __( 'Welcome to the Gutenberg Editor', 'gutenberg' );
}

return $title;
}
add_filter( 'default_title', 'gutenberg_default_demo_title' );
2 changes: 1 addition & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
require dirname( __FILE__ ) . '/plugin-compat.php';
require dirname( __FILE__ ) . '/i18n.php';
require dirname( __FILE__ ) . '/register.php';

require dirname( __FILE__ ) . '/demo.php';

// Register server-side code for individual blocks.
if ( ! function_exists( 'render_block_core_archives' ) ) {
Expand Down

0 comments on commit f84ceca

Please sign in to comment.