From 7c1aac48bc6263734cbf1c59cfe6c5416bdd1fcf Mon Sep 17 00:00:00 2001 From: Renan Date: Thu, 5 Sep 2024 21:01:55 +0200 Subject: [PATCH 1/3] Check if draft post exist and return --- .../launchpad/launchpad-task-definitions.php | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/projects/packages/jetpack-mu-wpcom/src/features/launchpad/launchpad-task-definitions.php b/projects/packages/jetpack-mu-wpcom/src/features/launchpad/launchpad-task-definitions.php index eae27e22c0867..3b64c896a9dbe 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/launchpad/launchpad-task-definitions.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/launchpad/launchpad-task-definitions.php @@ -105,7 +105,28 @@ function wpcom_launchpad_get_task_definitions() { add_action( 'publish_post', 'wpcom_launchpad_track_publish_first_post_task' ); }, 'get_calypso_path' => function ( $task, $default, $data ) { - $base_path = wpcom_launchpad_should_use_wp_admin_link() + $use_wp_admin_link = wpcom_launchpad_should_use_wp_admin_link(); + // Query for the latest draft post + $args = array( + 'posts_per_page' => 1, + 'post_status' => 'draft', + 'post_type' => 'post', + 'orderby' => 'date', + 'order' => 'DESC', + 'fields' => 'ids', + ); + // Fetch the latest draft post ID + // if there is a draft post, redirect the user to the draft otherwise to a new post. + $latest_draft_id = get_posts( $args ); + if ( ! empty( $latest_draft_id ) ) { + $draft_post_id = reset( $latest_draft_id ); + if ( $use_wp_admin_link && is_int( $draft_post_id ) ) { + return admin_url( 'post.php?action=edit&post=' . rawurlencode( $draft_post_id ) ); + } + return '/post/' . $data['site_slug_encoded'] . '/' . rawurlencode( $draft_post_id ); + } + + $base_path = $use_wp_admin_link ? admin_url( 'post-new.php' ) : '/post/' . $data['site_slug_encoded']; From ddb039b1d9d14b8b0f97444b06c9cc3264b48ac7 Mon Sep 17 00:00:00 2001 From: Renan Date: Thu, 5 Sep 2024 21:03:54 +0200 Subject: [PATCH 2/3] changelog --- .../jetpack-mu-wpcom/changelog/feat-launchpad_draft_post | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 projects/packages/jetpack-mu-wpcom/changelog/feat-launchpad_draft_post diff --git a/projects/packages/jetpack-mu-wpcom/changelog/feat-launchpad_draft_post b/projects/packages/jetpack-mu-wpcom/changelog/feat-launchpad_draft_post new file mode 100644 index 0000000000000..555d4ff0be6aa --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/changelog/feat-launchpad_draft_post @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Check if user has draft post on Launchpad From 787f3837ac92faddd62de63454cdb01af84e7d44 Mon Sep 17 00:00:00 2001 From: Philip Jackson Date: Tue, 10 Sep 2024 15:53:01 +1200 Subject: [PATCH 3/3] first_post_published copy depends on existence of existing draft The logic for looking up existing draft posts is shifted to wpcom_launchpad_get_latest_draft_id(). This allows the get_title() function to also use it to determine which button label to present to the user. --- .../changelog/feat-launchpad_draft_post | 2 +- .../launchpad/launchpad-task-definitions.php | 67 +++++++++++++------ 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/projects/packages/jetpack-mu-wpcom/changelog/feat-launchpad_draft_post b/projects/packages/jetpack-mu-wpcom/changelog/feat-launchpad_draft_post index 555d4ff0be6aa..bc79a3faa41e1 100644 --- a/projects/packages/jetpack-mu-wpcom/changelog/feat-launchpad_draft_post +++ b/projects/packages/jetpack-mu-wpcom/changelog/feat-launchpad_draft_post @@ -1,4 +1,4 @@ Significance: patch Type: fixed -Check if user has draft post on Launchpad +Launchpad first_post_published task reuses existing draft if there is one diff --git a/projects/packages/jetpack-mu-wpcom/src/features/launchpad/launchpad-task-definitions.php b/projects/packages/jetpack-mu-wpcom/src/features/launchpad/launchpad-task-definitions.php index 3b64c896a9dbe..5f2203aae55d6 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/launchpad/launchpad-task-definitions.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/launchpad/launchpad-task-definitions.php @@ -99,31 +99,25 @@ function wpcom_launchpad_get_task_definitions() { ), 'first_post_published' => array( 'get_title' => function () { - return __( 'Write your first post', 'jetpack-mu-wpcom' ); + $latest_draft_id = wpcom_launchpad_get_latest_draft_id(); + return $latest_draft_id === false + ? __( 'Write your first post', 'jetpack-mu-wpcom' ) + : __( 'Continue to write your first post', 'jetpack-mu-wpcom' ); }, 'add_listener_callback' => function () { add_action( 'publish_post', 'wpcom_launchpad_track_publish_first_post_task' ); }, 'get_calypso_path' => function ( $task, $default, $data ) { - $use_wp_admin_link = wpcom_launchpad_should_use_wp_admin_link(); - // Query for the latest draft post - $args = array( - 'posts_per_page' => 1, - 'post_status' => 'draft', - 'post_type' => 'post', - 'orderby' => 'date', - 'order' => 'DESC', - 'fields' => 'ids', - ); - // Fetch the latest draft post ID - // if there is a draft post, redirect the user to the draft otherwise to a new post. - $latest_draft_id = get_posts( $args ); - if ( ! empty( $latest_draft_id ) ) { - $draft_post_id = reset( $latest_draft_id ); - if ( $use_wp_admin_link && is_int( $draft_post_id ) ) { - return admin_url( 'post.php?action=edit&post=' . rawurlencode( $draft_post_id ) ); + $is_blog_onboarding_flow = in_array( get_option( 'site_intent' ), array( 'start-writing', 'design-first' ), true ); + $use_wp_admin_link = wpcom_launchpad_should_use_wp_admin_link() || $is_blog_onboarding_flow; + $latest_draft_id = wpcom_launchpad_get_latest_draft_id(); + + if ( is_int( $latest_draft_id ) ) { + // There is a draft post, redirect the user to the draft instead of making a fresh post. + if ( $use_wp_admin_link ) { + return admin_url( 'post.php?action=edit&post=' . rawurlencode( $latest_draft_id ) ); } - return '/post/' . $data['site_slug_encoded'] . '/' . rawurlencode( $draft_post_id ); + return '/post/' . $data['site_slug_encoded'] . '/' . rawurlencode( $latest_draft_id ); } $base_path = $use_wp_admin_link @@ -2741,3 +2735,38 @@ function wpcom_launchpad_mark_theme_selected_complete( $new_theme, $old_theme ) wpcom_mark_launchpad_task_complete( 'site_theme_selected' ); } add_action( 'jetpack_sync_current_theme_support', 'wpcom_launchpad_mark_theme_selected_complete', 10, 2 ); + +/** + * Returns the latest draft post ID, otherwise false. + * Similar to wp_get_recent_posts, except we only need the ID. + * + * The response is cached for the lifetime of the current request. + * + * @return int | boolean + */ +function wpcom_launchpad_get_latest_draft_id() { + // The result is cached for the current request + static $cached_blog_id = null; + static $cached_draft_id = null; + + if ( $cached_blog_id === get_current_blog_id() && $cached_draft_id !== null ) { + return $cached_draft_id; + } + + // Query for the latest draft post + $args = array( + 'posts_per_page' => 1, + 'post_status' => 'draft', + 'post_type' => 'post', + 'orderby' => 'date', + 'order' => 'DESC', + 'fields' => 'ids', + ); + + $latest_draft_id = get_posts( $args ); + + $cached_blog_id = get_current_blog_id(); + $cached_draft_id = reset( $latest_draft_id ); + + return $cached_draft_id; +}