Skip to content

Commit

Permalink
Launchpad: "first_post_published" task links to first post if you hav…
Browse files Browse the repository at this point in the history
…e one (#39259)

* Check if draft post exist and return

* changelog

* 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.

---------

Co-authored-by: Philip Jackson <philip@Philips-MacBook-Pro.local>

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/10804877103

Upstream-Ref: Automattic/jetpack@1ea6041
  • Loading branch information
p-jackson authored and matticbot committed Sep 11, 2024
1 parent 37f4380 commit 8738521
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 87 deletions.
58 changes: 29 additions & 29 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/automattic/jetpack-mu-wpcom/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This is an alpha version! The changes listed here are not final.

### Fixed
- Admin bar: fix paddings around wpcom and reader logos
- Launchpad first_post_published task reuses existing draft if there is one

## [5.62.0] - 2024-09-10
### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,28 @@ 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 ) {
$base_path = wpcom_launchpad_should_use_wp_admin_link()
$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( $latest_draft_id );
}

$base_path = $use_wp_admin_link
? admin_url( 'post-new.php' )
: '/post/' . $data['site_slug_encoded'];

Expand Down Expand Up @@ -2720,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;
}
Loading

0 comments on commit 8738521

Please sign in to comment.