Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing prompts: get prompt by id when answering a specific prompt #28856

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 60 additions & 8 deletions projects/plugins/jetpack/_inc/blogging-prompts.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,63 @@ function jetpack_get_daily_blogging_prompts( $time = 0 ) {
return $daily_prompts;
}

$prompts = jetpack_fetch_blogging_prompts(
array(
'from' => $day_before,
'number' => 10,
'_locale' => $locale,
)
);

if ( $prompts ) {
set_transient( $transient_key, $prompts, DAY_IN_SECONDS );
}

return $prompts;
}

/**
* Retrieve blogging prompts from the wpcom API and cache them, starting the list
* with the given prompt ID.
*
* @param int $prompt_id ID of the prompt to start with.
* @param int $count Number of prompts to retrieve.
* @return stdClass[]|null Array of blogging prompt objects or null.
*/
function jetpack_get_blogging_prompts_by_id( $prompt_id, $count = 10 ) {
$locale = get_locale();
$transient_key = "jetpack_blogging_prompt_{$prompt_id}_{$count}_{$locale}";
$prompts = get_transient( $transient_key );

// Return the cached prompts, if we have them. Otherwise fetch from the API.
if ( false !== $prompts ) {
return $prompts;
}

$prompts = jetpack_fetch_blogging_prompts(
array(
'from_id' => $prompt_id,
'number' => $count,
'_locale' => $locale,
)
);

if ( $prompts ) {
set_transient( $transient_key, $prompts, HOUR_IN_SECONDS );
}

return $prompts;
}

/**
* Retrieve blogging prompts from the wpcom API.
*
* @param array $query_params Query parameters to pass to the API.
* @return stdClass[]|null Array of blogging prompt objects or null.
*/
function jetpack_fetch_blogging_prompts( $query_params ) {
$blog_id = \Jetpack_Options::get_option( 'id' );
$path = '/sites/' . rawurldecode( $blog_id ) . '/blogging-prompts?from=' . rawurldecode( $day_before ) . '&number=10&_locale=' . rawurldecode( $locale );
$path = add_query_arg( $query_params, '/sites/' . rawurldecode( $blog_id ) . '/blogging-prompts' );

$args = array(
'headers' => array(
Expand Down Expand Up @@ -106,10 +161,7 @@ function jetpack_get_daily_blogging_prompts( $time = 0 ) {
return null;
}

$prompts = $body->prompts;
set_transient( $transient_key, $prompts, DAY_IN_SECONDS );

return $prompts;
return $body->prompts;
}

/**
Expand Down Expand Up @@ -194,13 +246,13 @@ function jetpack_is_potential_blogging_site() {
* @return bool
*/
function jetpack_is_valid_blogging_prompt( $prompt_id ) {
$daily_prompts = jetpack_get_daily_blogging_prompts();
$prompts = jetpack_get_blogging_prompts_by_id( $prompt_id );

if ( ! $daily_prompts ) {
if ( ! $prompts ) {
return false;
}

foreach ( $daily_prompts as $prompt ) {
foreach ( $prompts as $prompt ) {
if ( $prompt->id === $prompt_id ) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Writing prompts: fetch prompts by id, rather than date
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,30 @@ function inject_blogging_prompts() {
return;
}

$prompt_id = get_blogging_prompt_answer_id();

// And only for blogging sites or those explicitly responding to the prompt.
if ( should_load_blogging_prompts() ) {
$daily_prompts = jetpack_get_daily_blogging_prompts();
if ( $prompt_id ) {
$prompts = jetpack_get_blogging_prompts_by_id( $prompt_id );

if ( $daily_prompts ) {
if ( $prompts ) {
wp_add_inline_script(
'jetpack-blocks-editor',
'var Jetpack_BloggingPrompts = ' . wp_json_encode( $daily_prompts, JSON_HEX_TAG | JSON_HEX_AMP ) . ';',
'var Jetpack_BloggingPrompts = ' . wp_json_encode( $prompts, JSON_HEX_TAG | JSON_HEX_AMP ) . ';',
'before'
);
}
}
}

/**
* Determines if the blogging prompts extension should be loaded in the editor.
* Get the blogging prompt id to answer, if there is one.
*
* @return bool
* @return int
*/
function should_load_blogging_prompts() {
function get_blogging_prompt_answer_id() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Clicking a prompt response link can happen from notifications, Calypso, wp-admin, email, etc and only sets up a response post (tag, meta, prompt text); the user must take action to actually publish the post.
return isset( $_GET['answer_prompt'] ) && absint( $_GET['answer_prompt'] );
return isset( $_GET['answer_prompt'] ) && absint( $_GET['answer_prompt'] ) ? absint( $_GET['answer_prompt'] ) : 0;
}

add_action( 'init', __NAMESPACE__ . '\register_extension' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function initBloggingPrompts() {

// Try to find the prompt by id, otherwise just default to the first prompt for today.
// The current list of prompts starts from yesteday, so today's is the second prompt.
const prompt = prompts.find( p => p.id === answerPromptId ) ?? prompts[ 1 ];
const prompt = prompts.find( p => p.id === answerPromptId ) ?? prompts[ 0 ];

if ( prompt ) {
insertTemplate( prompt );
Expand Down