PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
← All changes | _inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v3-endpoint-blogging-prompts.php +96 -53 12.6.4 → 16.3 View file →
@@ -4,15 +4,21 @@
4 4 *
5 5 * @package automattic/jetpack
6 6 */
7 7
8 -use Automattic\Jetpack\Connection\Client;
9 -use Automattic\Jetpack\Connection\Manager;
8 +use Automattic\Jetpack\Connection\Traits\WPCOM_REST_API_Proxy_Request;
10 9
10 +if ( ! defined( 'ABSPATH' ) ) {
11 + exit( 0 );
12 +}
13 +
11 14 /**
12 15 * REST API endpoint wpcom/v3/sites/%s/blogging-prompts.
13 16 */
14 17 class WPCOM_REST_API_V3_Endpoint_Blogging_Prompts extends WP_REST_Posts_Controller {
18 +
19 + use WPCOM_REST_API_Proxy_Request;
20 +
15 21 const TEMPLATE_BLOG_ID = 205876834;
16 22
17 23 /**
18 24 * Whether the endpoint is running on wpcom, or not.
@@ -41,9 +47,11 @@
41 47 * Constructor.
42 48 */
43 49 public function __construct() {
44 50 $this->post_type = 'post';
45 - $this->namespace = 'wpcom/v3';
51 + $this->base_api_path = 'wpcom';
52 + $this->version = 'v3';
53 + $this->namespace = $this->base_api_path . '/' . $this->version;
46 54 $this->rest_base = 'blogging-prompts';
47 55 $this->wpcom_is_wpcom_only_endpoint = true;
48 56 $this->wpcom_is_site_specific_endpoint = true;
49 57 $this->is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM;
@@ -98,9 +106,9 @@
98 106 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
99 107 */
100 108 public function get_items( $request ) {
101 109 if ( ! $this->is_wpcom ) {
102 - return $this->proxy_request_to_wpcom( $request );
110 + return $this->proxy_request_to_wpcom( $request, '', 'user', true );
103 111 }
104 112
105 113 if ( $request->get_param( 'force_year' ) ) {
106 114 $this->force_year = $request->get_param( 'force_year' );
@@ -107,14 +115,17 @@
107 115 }
108 116
109 117 switch_to_blog( self::TEMPLATE_BLOG_ID );
110 118 add_action( 'pre_get_posts', array( $this, 'modify_query' ) );
111 - add_filter( 'posts_clauses', array( $this, 'filter_sql' ) );
119 + add_filter( 'posts_clauses', array( $this, 'filter_sql' ), 10, 2 );
112 120 $items = parent::get_items( $request );
113 121 remove_filter( 'posts_clauses', array( $this, 'filter_sql' ) );
114 122 remove_action( 'pre_get_posts', array( $this, 'modify_query' ) );
115 123 restore_current_blog();
116 124
125 + // Reset so a later query in the same request can never inherit this state.
126 + $this->day_of_year_query = 0;
127 +
117 128 return $items;
118 129 }
119 130
120 131 /**
@@ -124,11 +135,15 @@
124 135 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
125 136 */
126 137 public function get_item( $request ) {
127 138 if ( ! $this->is_wpcom ) {
128 - return $this->proxy_request_to_wpcom( $request, $request->get_param( 'id' ) );
139 + return $this->proxy_request_to_wpcom( $request, $request->get_param( 'id' ), 'user', true );
129 140 }
130 141
142 + if ( $request->get_param( 'force_year' ) ) {
143 + $this->force_year = $request->get_param( 'force_year' );
144 + }
145 +
131 146 switch_to_blog( self::TEMPLATE_BLOG_ID );
132 147 $item = parent::get_item( $request );
133 148 restore_current_blog();
134 149
@@ -140,14 +155,22 @@
140 155 *
141 156 * @param WP_Query $wp_query The WP_Query instance (passed by reference).
142 157 */
143 158 public function modify_query( &$wp_query ) {
144 - if ( is_array( $wp_query->query_vars['date_query'] ) ) {
145 - $wp_query->query_vars['date_query'] = array_map(
146 - array( $this, 'map_date_query' ),
147 - $wp_query->query_vars['date_query']
148 - );
159 + // parent::get_items() renders each prompt while this hook is still attached,
160 + // and rendering can spawn nested WP_Querys (e.g. Gutenberg's wp_global_styles
161 + // lookup), so only ever touch the prompts query itself.
162 + if ( $this->post_type !== $wp_query->get( 'post_type' ) ) {
163 + return;
149 164 }
165 +
166 + $date_query = $wp_query->get( 'date_query' );
167 +
168 + if ( is_array( $date_query ) ) {
169 + $wp_query->set( 'date_query', array_map( array( $this, 'map_date_query' ), $date_query ) );
170 + // Mark the query so filter_sql() only modifies this one.
171 + $wp_query->set( 'jetpack_blogging_prompts', true );
172 + }
150 173 }
151 174
152 175 /**
153 176 * Modify date_query items when querying prompts.
@@ -163,9 +186,9 @@
163 186 $date_query['inclusive'] = true;
164 187
165 188 // If using a "year-less" date, e.g. `--03-16`, override the date_query, and prepare to modify sql manually.
166 189 // `after` should be a date string when making API requests, rather than an array.
167 - if ( is_string( $date_query['after'] ) && 0 === strpos( $date_query['after'], '-' ) ) {
190 + if ( is_string( $date_query['after'] ) && str_starts_with( $date_query['after'], '-' ) ) {
168 191 $date = date_create_from_format( '--m-d', $date_query['after'] );
169 192
170 193 if ( false !== $date ) {
171 194 // PHP day of the year starts with 0; normalize to match SQL DAYOFTHEYEAR which starts with 1.
@@ -182,19 +205,24 @@
182 205
183 206 /**
184 207 * Modify post sql for custom date ordering using the {@see 'posts_clauses'} hook.
185 208 *
186 - * @param array $clauses SQL clauses for the current query.
187 - * @return array Modified SQL clauses.
209 + * @param array $clauses SQL clauses for the current query.
210 + * @param WP_Query|null $query The WP_Query instance being filtered.
211 + * @return array Modified SQL clauses.
188 212 */
189 - public function filter_sql( $clauses ) {
213 + public function filter_sql( $clauses, $query = null ) {
190 214 global $wpdb;
215 + if ( ! $query instanceof WP_Query || ! $query->get( 'jetpack_blogging_prompts' ) ) {
216 + return $clauses;
217 + }
191 218 if ( $this->day_of_year_query > 0 ) {
192 219 $day = $this->day_of_year_query;
193 220 $year = $this->force_year ? $this->force_year : wp_date( 'Y' );
194 221
195 222 // Grab the current sort order, `ASC` or `DESC`, so we can reuse it.
196 - $order = end( explode( ' ', $clauses['orderby'] ) );
223 + $exploded = explode( ' ', $clauses['orderby'] );
224 + $order = end( $exploded );
197 225
198 226 // Calculate the day of year for each prompt, from 1 to 366, but use the current year so that prompts published
199 227 // during leap years have the correct day for non-leap years.
200 228 $fields = $clauses['fields'] . $wpdb->prepare( ", DAYOFYEAR(CONCAT(%d, DATE_FORMAT({$wpdb->posts}.post_date, '-%%m-%%d'))) AS day_of_year", $year );
@@ -270,9 +298,13 @@
270 298 $data['date'] = $this->prepare_date_response( $prompt->post_date_gmt );
271 299 }
272 300
273 301 if ( rest_is_field_included( 'label', $fields ) ) {
274 - $data['label'] = __( 'Daily writing prompt', 'jetpack' );
302 + if ( $this->is_in_bloganuary( $prompt->post_date_gmt ) ) {
303 + $data['label'] = __( 'Bloganuary writing prompt', 'jetpack' );
304 + } else {
305 + $data['label'] = __( 'Daily writing prompt', 'jetpack' );
306 + }
275 307 }
276 308
277 309 if ( rest_is_field_included( 'text', $fields ) ) {
278 310 $text = \BloggingPrompts\prompt_without_blocks( $prompt->post_content );
@@ -298,9 +330,14 @@
298 330 $data['answered_users_sample'] = $this->build_answering_users_sample( $prompt->ID );
299 331 }
300 332
301 333 if ( rest_is_field_included( 'answered_link', $fields ) ) {
302 - $data['answered_link'] = esc_url( "https://wordpress.com/tag/dailyprompt-{$prompt->ID}" );
334 + if ( $this->is_in_bloganuary( $prompt->post_date_gmt ) ) {
335 + $bloganuary_id = $this->get_bloganuary_id( $prompt->post_date_gmt );
336 + $data['answered_link'] = esc_url( "https://wordpress.com/tag/{$bloganuary_id}" );
337 + } else {
338 + $data['answered_link'] = esc_url( "https://wordpress.com/tag/dailyprompt-{$prompt->ID}" );
339 + }
303 340 }
304 341
305 342 if ( rest_is_field_included( 'answered_link_text', $fields ) ) {
306 343 $data['answered_link_text'] = __( 'View all responses', 'jetpack' );
@@ -305,12 +342,47 @@
305 342 if ( rest_is_field_included( 'answered_link_text', $fields ) ) {
306 343 $data['answered_link_text'] = __( 'View all responses', 'jetpack' );
307 344 }
308 345
346 + if ( $this->is_in_bloganuary( $prompt->post_date_gmt ) && rest_is_field_included( 'bloganuary_id', $fields ) ) {
347 + $data['bloganuary_id'] = $this->get_bloganuary_id( $prompt->post_date_gmt );
348 + }
349 +
309 350 return $data;
310 351 }
311 352
312 353 /**
354 + * Return true if the post is in "Bloganuary"
355 + *
356 + * @param string $post_date_gmt Unused - Post date in GMT.
357 + * @return bool Always returns false as Bloganuary is disabled.
358 + */
359 + protected function is_in_bloganuary( $post_date_gmt ) { //phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
360 +
361 + /*
362 + Disable for January 2025 and beyond (see https://wp.me/p5uIfZ-gxX).
363 + Previously, this method would check if the post was published in January:
364 + - Extract month from post_date_gmt -- $post_month = gmdate( 'm', strtotime( $post_date_gmt ) );
365 + - Return true if month was '01' -- return $post_month === '01';
366 + */
367 + return false;
368 + }
369 +
370 + /**
371 + * Return the bloganuary id of the form `bloganuary-yyyy-dd`
372 + *
373 + * @param string $post_date_gmt Post date in GMT.
374 + * @return string Bloganuary id.
375 + */
376 + protected function get_bloganuary_id( $post_date_gmt ) {
377 + $post_year_day = gmdate( 'Y-d', strtotime( $post_date_gmt ) );
378 + if ( $this->force_year ) {
379 + $post_year_day = $this->force_year . '-' . gmdate( 'd', strtotime( $post_date_gmt ) );
380 + }
381 + return 'bloganuary-' . $post_year_day;
382 + }
383 +
384 + /**
313 385 * Format a date for a blogging prompt, omiting the time.
314 386 *
315 387 * @param string $date_gmt Publish datetime of the prompt in GMT, i.e. 0000-00-00 00:00:00.
316 388 * @param string $date Publish datetime of the prompt, i.e. 0000-00-00 00:00:00.
@@ -320,9 +392,9 @@
320 392 $post_date = $date ? $date : $date_gmt;
321 393 $date_obj = date_create( $post_date );
322 394
323 395 if ( $this->force_year ) {
324 - $date_obj->setDate( $this->force_year, $date_obj->format( 'm' ), $date_obj->format( 'd' ) );
396 + $date_obj->setDate( $this->force_year, (int) $date_obj->format( 'n' ), (int) $date_obj->format( 'j' ) );
325 397
326 398 // If ascending by day of year, go to the next year when we pass the last day of the year.
327 399 if ( $date_obj->format( 'm-d' ) === '12-31' ) {
328 400 $this->force_year += 1;
@@ -346,9 +418,9 @@
346 418 'description' => __( 'Show prompts following a given date.', 'jetpack' ),
347 419 'type' => 'string',
348 420 'validate_callback' => function ( $param ) {
349 421 // Allow month and day without year, e.g. `--02-28`
350 - if ( strpos( $param, '-' ) === 0 ) {
422 + if ( str_starts_with( $param, '-' ) ) {
351 423 return false !== date_create_from_format( '--m-d', $param );
352 424 }
353 425
354 426 return false !== date_create( $param );
@@ -443,8 +515,12 @@
443 515 'answered_link_text' => array(
444 516 'description' => __( 'Text for the link to answers for the prompt.', 'jetpack' ),
445 517 'type' => 'string',
446 518 ),
519 + 'bloganuary_id' => array(
520 + 'description' => __( 'Id used by the bloganuary promotion', 'jetpack' ),
521 + 'type' => 'string',
522 + ),
447 523 ),
448 524 );
449 525 }
450 526
@@ -474,41 +550,8 @@
474 550 'rest_cannot_read_prompts',
475 551 __( 'Sorry, you are not allowed to access blogging prompts on this site.', 'jetpack' ),
476 552 array( 'status' => rest_authorization_required_code() )
477 553 );
478 - }
479 -
480 - /**
481 - * Proxy request to wpcom servers for the site and user.
482 - *
483 - * @param WP_Rest_Request $request Request to proxy.
484 - * @param string $path Path to append to the rest base.
485 - * @return mixed|WP_Error Response from wpcom servers or an error.
486 - */
487 - public function proxy_request_to_wpcom( $request, $path = '' ) {
488 - $blog_id = \Jetpack_Options::get_option( 'id' );
489 - $path = '/sites/' . rawurldecode( $blog_id ) . '/' . rawurldecode( $this->rest_base ) . ( $path ? '/' . rawurldecode( $path ) : '' );
490 - $api_url = add_query_arg( $request->get_query_params(), $path );
491 -
492 - // Prefer request as user, if possible. Fall back to blog request to show prompt data for unconnected users.
493 - $response = ( new Manager() )->is_user_connected()
494 - ? Client::wpcom_json_api_request_as_user( $api_url, '3', array(), null, 'wpcom' )
495 - : Client::wpcom_json_api_request_as_blog( $api_url, 'v3', array(), null, 'wpcom' );
496 -
497 - if ( is_wp_error( $response ) ) {
498 - return $response;
499 - }
500 -
501 - $response_status = wp_remote_retrieve_response_code( $response );
502 - $response_body = json_decode( wp_remote_retrieve_body( $response ) );
503 -
504 - if ( $response_status >= 400 ) {
505 - $code = isset( $response_body->code ) ? $response_body->code : 'unknown_error';
506 - $message = isset( $response_body->message ) ? $response_body->message : __( 'An unknown error occurred.', 'jetpack' );
507 - return new WP_Error( $code, $message, array( 'status' => $response_status ) );
508 - }
509 -
510 - return $response_body;
511 554 }
512 555
513 556 /**
514 557 * Creates a sample of users who have answered a blogging prompt.