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 +49 -53 13.6.2 → 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,9 +135,9 @@
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
131 142 if ( $request->get_param( 'force_year' ) ) {
132 143 $this->force_year = $request->get_param( 'force_year' );
@@ -144,14 +155,22 @@
144 155 *
145 156 * @param WP_Query $wp_query The WP_Query instance (passed by reference).
146 157 */
147 158 public function modify_query( &$wp_query ) {
148 - if ( is_array( $wp_query->query_vars['date_query'] ) ) {
149 - $wp_query->query_vars['date_query'] = array_map(
150 - array( $this, 'map_date_query' ),
151 - $wp_query->query_vars['date_query']
152 - );
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;
153 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 + }
154 173 }
155 174
156 175 /**
157 176 * Modify date_query items when querying prompts.
@@ -186,13 +205,17 @@
186 205
187 206 /**
188 207 * Modify post sql for custom date ordering using the {@see 'posts_clauses'} hook.
189 208 *
190 - * @param array $clauses SQL clauses for the current query.
191 - * @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.
192 212 */
193 - public function filter_sql( $clauses ) {
213 + public function filter_sql( $clauses, $query = null ) {
194 214 global $wpdb;
215 + if ( ! $query instanceof WP_Query || ! $query->get( 'jetpack_blogging_prompts' ) ) {
216 + return $clauses;
217 + }
195 218 if ( $this->day_of_year_query > 0 ) {
196 219 $day = $this->day_of_year_query;
197 220 $year = $this->force_year ? $this->force_year : wp_date( 'Y' );
198 221
@@ -329,14 +352,20 @@
329 352
330 353 /**
331 354 * Return true if the post is in "Bloganuary"
332 355 *
333 - * @param string $post_date_gmt Post date in GMT.
334 - * @return bool True if the post is in "Bloganuary".
356 + * @param string $post_date_gmt Unused - Post date in GMT.
357 + * @return bool Always returns false as Bloganuary is disabled.
335 358 */
336 - protected function is_in_bloganuary( $post_date_gmt ) {
337 - $post_month = gmdate( 'm', strtotime( $post_date_gmt ) );
338 - return $post_month === '01';
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;
339 368 }
340 369
341 370 /**
342 371 * Return the bloganuary id of the form `bloganuary-yyyy-dd`
@@ -363,9 +392,9 @@
363 392 $post_date = $date ? $date : $date_gmt;
364 393 $date_obj = date_create( $post_date );
365 394
366 395 if ( $this->force_year ) {
367 - $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' ) );
368 397
369 398 // If ascending by day of year, go to the next year when we pass the last day of the year.
370 399 if ( $date_obj->format( 'm-d' ) === '12-31' ) {
371 400 $this->force_year += 1;
@@ -521,41 +550,8 @@
521 550 'rest_cannot_read_prompts',
522 551 __( 'Sorry, you are not allowed to access blogging prompts on this site.', 'jetpack' ),
523 552 array( 'status' => rest_authorization_required_code() )
524 553 );
525 - }
526 -
527 - /**
528 - * Proxy request to wpcom servers for the site and user.
529 - *
530 - * @param WP_Rest_Request $request Request to proxy.
531 - * @param string $path Path to append to the rest base.
532 - * @return mixed|WP_Error Response from wpcom servers or an error.
533 - */
534 - public function proxy_request_to_wpcom( $request, $path = '' ) {
535 - $blog_id = \Jetpack_Options::get_option( 'id' );
536 - $path = '/sites/' . rawurldecode( $blog_id ) . '/' . rawurldecode( $this->rest_base ) . ( $path ? '/' . rawurldecode( $path ) : '' );
537 - $api_url = add_query_arg( $request->get_query_params(), $path );
538 -
539 - // Prefer request as user, if possible. Fall back to blog request to show prompt data for unconnected users.
540 - $response = ( new Manager() )->is_user_connected()
541 - ? Client::wpcom_json_api_request_as_user( $api_url, '3', array(), null, 'wpcom' )
542 - : Client::wpcom_json_api_request_as_blog( $api_url, 'v3', array(), null, 'wpcom' );
543 -
544 - if ( is_wp_error( $response ) ) {
545 - return $response;
546 - }
547 -
548 - $response_status = wp_remote_retrieve_response_code( $response );
549 - $response_body = json_decode( wp_remote_retrieve_body( $response ), true );
550 -
551 - if ( $response_status >= 400 ) {
552 - $code = isset( $response_body['code'] ) ? $response_body['code'] : 'unknown_error';
553 - $message = isset( $response_body['message'] ) ? $response_body['message'] : __( 'An unknown error occurred.', 'jetpack' );
554 - return new WP_Error( $code, $message, array( 'status' => $response_status ) );
555 - }
556 -
557 - return $response_body;
558 554 }
559 555
560 556 /**
561 557 * Creates a sample of users who have answered a blogging prompt.