Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: removed

Removed the deprecated option jetpack_blogging_prompts_enabled from defaults
1 change: 0 additions & 1 deletion projects/packages/sync/src/class-defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ class Defaults {
'jetpack_testimonial',
'jetpack_testimonial_posts_per_page',
'jetpack_wga',
'jetpack_blogging_prompts_enabled',
'large_size_h',
'large_size_w',
'launch-status',
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/sync/src/class-package-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class Package_Version {

const PACKAGE_VERSION = '1.46.1';
const PACKAGE_VERSION = '1.47.0-alpha';

const PACKAGE_SLUG = 'sync';

Expand Down
115 changes: 59 additions & 56 deletions projects/plugins/jetpack/_inc/blogging-prompts.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ function jetpack_setup_blogging_prompt_response( $post_id ) {
return;
}

if ( jetpack_is_valid_blogging_prompt( $prompt_id ) ) {
$prompt = jetpack_get_blogging_prompt_by_id( $prompt_id );

if ( $prompt ) {
update_post_meta( $post_id, '_jetpack_blogging_prompt_key', $prompt_id );
wp_add_post_tags( $post_id, 'dailyprompt' );
wp_add_post_tags( $post_id, array( 'dailyprompt', "dailyprompt-$prompt_id" ) );
}
}

Expand All @@ -51,29 +53,11 @@ function jetpack_setup_blogging_prompt_response( $post_id ) {
* Utility functions.
*/

/**
* Get the 'jetpack_blogging_prompts_enabled' and default to true if the site appears to be a blog.
*
* @return boolean
*/
function jetpack_are_blogging_prompts_enabled() {
$prompts_enabled = (bool) get_option( 'jetpack_blogging_prompts_enabled', false );

/**
* Filters whether blogging prompts are enabled.
*
* @since 11.7
*
* @param bool $prompts_enabled Whether blogging prompts are enabled.
*/
return apply_filters( 'jetpack_are_blogging_prompts_enabled', $prompts_enabled );
}

/**
* Retrieve daily blogging prompts from the wpcom API and cache them.
*
* @param int $time Unix timestamp representing the day for which to get blogging prompts.
* @return stdClass[] Array of blogging prompt objects.
* @return stdClass[]|null Array of blogging prompt objects or null.
*/
function jetpack_get_daily_blogging_prompts( $time = 0 ) {
$timestamp = $time ? $time : time();
Expand All @@ -89,8 +73,59 @@ function jetpack_get_daily_blogging_prompts( $time = 0 ) {
return $daily_prompts;
}

$path_suffix = '?from=' . rawurldecode( $day_before ) . '&number=10&_locale=' . rawurldecode( $locale );
$body = jetpack_fetch_blogging_prompts( $path_suffix );

if ( ! $body || ! isset( $body->prompts ) ) {
return null;
}

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

return $prompts;
}

/**
* Retieve a blogging prompt by prompt id.
*
* @param int $prompt_id ID of the prompt to return.
* @return stdClass|null Prompt object or null if not found.
*/
function jetpack_get_blogging_prompt_by_id( $prompt_id ) {
$locale = get_locale();
$transient_key = 'jetpack_blogging_prompt_' . $prompt_id . '_' . $locale;
$prompt = get_transient( $transient_key );

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

$path_suffix = '/' . rawurldecode( $prompt_id ) . '?_locale=' . rawurldecode( $locale );
$prompt = jetpack_fetch_blogging_prompts( $path_suffix );

if ( ! $prompt ) {
return null;
}

// Set a long cache timeout: in practice prompts should rarely, if ever, be edited or changed.
set_transient( $transient_key, $prompt, MONTH_IN_SECONDS );

return $prompt;
}

/**
* Fetches blogging prompts from the wpcom API.
*
* When run on wpcom, uses WPCOM_API_Direct::do_request to avoid an unnecessary http request for Simple sites.
*
* @param string $path_suffix Path suffix for API request.
* @return mixed|null Response body or null if not found.
*/
function jetpack_fetch_blogging_prompts( $path_suffix ) {
$blog_id = \Jetpack_Options::get_option( 'id' );
$path = '/sites/' . rawurldecode( $blog_id ) . '/blogging-prompts?from=' . rawurldecode( $day_before ) . '&number=10&_locale=' . rawurldecode( $locale );
$path = '/sites/' . rawurldecode( $blog_id ) . '/blogging-prompts' . $path_suffix;
$args = array(
'headers' => array(
'Content-Type' => 'application/json',
Expand All @@ -106,7 +141,7 @@ function jetpack_get_daily_blogging_prompts( $time = 0 ) {
// This will load the library, but it may be too late to automatically load any endpoints using WPCOM_API_Direct::register_endpoints.
// In that case, call `wpcom_rest_api_v2_load_plugin_files( 'wp-content/rest-api-plugins/endpoints/blogging-prompts.php' )`
// on the `init` hook to load the blogging-prompts endpoint before calling this function.
require_lib( 'wpcom-api-direct' );
require_once WP_CONTENT_DIR . '/lib/wpcom-api-direct/wpcom-api-direct.php';
$response = \WPCOM_API_Direct::do_request( $args );
} else {
$response = \Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user( $path, 'v2', $args, null, 'wpcom' );
Expand All @@ -117,14 +152,7 @@ function jetpack_get_daily_blogging_prompts( $time = 0 ) {
return null;
}

$body = json_decode( wp_remote_retrieve_body( $response ) );

if ( ! $body ) {
return null;
}

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

return $prompts;
}
Expand Down Expand Up @@ -200,28 +228,3 @@ function jetpack_is_new_post_screen() {
function jetpack_is_potential_blogging_site() {
return jetpack_has_write_intent() || jetpack_has_posts_page() || jetpack_has_or_will_publish_posts();
}

/**
* Checks if the given prompt id is included in today's blogging prompts.
*
* Would be best to use the API to check if the prompt id is valid for any day,
* but for now we're only using one prompt per day.
*
* @param int $prompt_id id of blogging prompt.
* @return bool
*/
function jetpack_is_valid_blogging_prompt( $prompt_id ) {
$daily_prompts = jetpack_get_daily_blogging_prompts();

if ( ! $daily_prompts ) {
return false;
}

foreach ( $daily_prompts as $prompt ) {
if ( $prompt->id === $prompt_id ) {
return true;
}
}

return false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2890,16 +2890,6 @@ public static function get_updateable_data_list( $selector = '' ) {
'validate_callback' => __CLASS__ . '::validate_boolean',
'jp_group' => 'videopress',
),

// Writing Prompts.
'jetpack_blogging_prompts_enabled' => array(
'description' => esc_html__( 'Displays a writing prompt when starting a new post.', 'jetpack' ),
'type' => 'boolean',
'default' => false,
'validate_callback' => __CLASS__ . '::validate_boolean',
'jp_group' => 'writing-prompts',
),

);

// Add modules to list so they can be toggled.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: enhancement

Writing prompts: remove placeholder prompt and fetch embedded prompt by id for better compatibility
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/class.jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -6013,6 +6013,10 @@ public function deprecated_hooks() {
'replacement' => null,
'version' => 'jetpack-11.0.0',
),
'jetpack_are_blogging_prompts_enabled' => array(
'replacement' => null,
'version' => 'jetpack-$$next-version$$',
),
);

foreach ( $filter_deprecated_list as $tag => $args ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
use Automattic\Jetpack\Blocks;
use Automattic\Jetpack\Device_Detection\User_Agent_Info;

require_once __DIR__ . '/settings.php';

const FEATURE_NAME = 'blogging-prompts';
const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;

Expand All @@ -25,7 +23,7 @@ function register_extension() {

// Load the blogging-prompts endpoint here on init so its route will be registered.
// We can use it with `WPCOM_API_Direct::do_request` to avoid a network request on Simple Sites.
if ( defined( 'IS_WPCOM' ) && IS_WPCOM && should_load_blogging_prompts() ) {
if ( defined( 'IS_WPCOM' ) && IS_WPCOM && get_answer_prompt_id() ) {
wpcom_rest_api_v2_load_plugin_files( 'wp-content/rest-api-plugins/endpoints/blogging-prompts.php' );
}
}
Expand All @@ -49,14 +47,16 @@ function inject_blogging_prompts() {
return;
}

$prompt_id = get_answer_prompt_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 ) {
$daily_prompt = jetpack_get_blogging_prompt_by_id( $prompt_id );

if ( $daily_prompts ) {
if ( $daily_prompt ) {
wp_add_inline_script(
'jetpack-blocks-editor',
'var Jetpack_BloggingPrompts = ' . wp_json_encode( $daily_prompts, JSON_HEX_TAG | JSON_HEX_AMP ) . ';',
'var Jetpack_BloggingPrompts = { "prompt": ' . wp_json_encode( $daily_prompt, JSON_HEX_TAG | JSON_HEX_AMP ) . ' };',
'before'
);
}
Expand All @@ -68,9 +68,9 @@ function inject_blogging_prompts() {
*
* @return bool
*/
function should_load_blogging_prompts() {
function get_answer_prompt_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 jetpack_are_blogging_prompts_enabled() || ( 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
@@ -1,43 +1,26 @@
import { createBlock } from '@wordpress/blocks';
import { dispatch } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
import { waitForEditor } from '../../shared/wait-for-editor';

async function insertTemplate( prompt, embedPrompt = false ) {
async function insertTemplate( prompt ) {
await waitForEditor();

const { insertBlocks } = dispatch( 'core/block-editor' );
const bloggingPromptBlocks = embedPrompt
? [ createBlock( 'core/pullquote', { value: prompt.text } ), createBlock( 'core/paragraph' ) ]
: createBlock(
'core/paragraph',
{
/* translators: %s is the daily blogging prompt. */
placeholder: sprintf( __( 'Daily Prompt: %s', 'jetpack' ), prompt.text ),
},
[]
);
const bloggingPromptBlocks = [
createBlock( 'core/pullquote', { value: prompt.text } ),
createBlock( 'core/paragraph' ),
];

insertBlocks( bloggingPromptBlocks, 0, undefined, false );
}

function initBloggingPrompts() {
const prompts = window.Jetpack_BloggingPrompts;
if ( ! Array.isArray( prompts ) || ! prompts[ 0 ] ) {
const data = window.Jetpack_BloggingPrompts;
if ( ! data || ! data.prompt ) {
return;
}

const urlQuery = new URLSearchParams( document.location.search );
const answerPrompt = urlQuery.get( 'answer_prompt' ) ?? '0';
const answerPromptId = parseInt( answerPrompt );

// 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 ];

if ( prompt ) {
insertTemplate( prompt, !! answerPromptId );
}
insertTemplate( data.prompt );
}

initBloggingPrompts();

This file was deleted.

Loading