PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.1
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.1
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 13.7.2 13.8.3 13.9.2 14.0.1 All 504 releases
← All changes | _inc/lib/core-api/wpcom-endpoints/memberships.php +171 -129 13.7.2 → 16.3-a.1 View file →
@@ -5,21 +5,31 @@
5 5 * @package Jetpack
6 6 * @since 7.3.0
7 7 */
8 8
9 -use Automattic\Jetpack\Connection\Client;
9 +use Automattic\Jetpack\Connection\Traits\WPCOM_REST_API_Proxy_Request;
10 10
11 +if ( ! defined( 'ABSPATH' ) ) {
12 + exit( 0 );
13 +}
14 +
11 15 /**
12 16 * Class WPCOM_REST_API_V2_Endpoint_Memberships
13 17 * This introduces V2 endpoints.
18 + *
19 + * @phan-constructor-used-for-side-effects
14 20 */
15 21 class WPCOM_REST_API_V2_Endpoint_Memberships extends WP_REST_Controller {
16 22
23 + use WPCOM_REST_API_Proxy_Request;
24 +
17 25 /**
18 26 * WPCOM_REST_API_V2_Endpoint_Memberships constructor.
19 27 */
20 28 public function __construct() {
21 - $this->namespace = 'wpcom/v2';
29 + $this->base_api_path = 'wpcom';
30 + $this->version = 'v2';
31 + $this->namespace = $this->base_api_path . '/' . $this->version;
22 32 $this->rest_base = 'memberships';
23 33 $this->wpcom_is_wpcom_only_endpoint = true;
24 34 $this->wpcom_is_site_specific_endpoint = true;
25 35 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
@@ -85,9 +95,9 @@
85 95 'type' => 'string',
86 96 'required' => true,
87 97 ),
88 98 'price' => array(
89 - 'type' => 'float',
99 + 'type' => 'number',
90 100 'required' => true,
91 101 ),
92 102 'currency' => array(
93 103 'type' => 'string',
@@ -107,8 +117,12 @@
107 117 'tier' => array(
108 118 'type' => 'integer',
109 119 'required' => false,
110 120 ),
121 + 'description' => array(
122 + 'type' => 'string',
123 + 'required' => false,
124 + ),
111 125 ),
112 126 ),
113 127 )
114 128 );
@@ -119,8 +133,22 @@
119 133 array(
120 134 'methods' => WP_REST_Server::CREATABLE,
121 135 'callback' => array( $this, 'create_products' ),
122 136 'permission_callback' => array( $this, 'can_modify_products_permission_check' ),
137 + 'args' => array(
138 + 'currency' => array(
139 + 'type' => 'string',
140 + 'required' => true,
141 + ),
142 + 'type' => array(
143 + 'type' => 'string',
144 + 'required' => true,
145 + ),
146 + 'is_editable' => array(
147 + 'type' => 'boolean',
148 + 'required' => false,
149 + ),
150 + ),
123 151 ),
124 152 array(
125 153 'methods' => WP_REST_Server::READABLE,
126 154 'callback' => array( $this, 'list_products' ),
@@ -141,9 +169,9 @@
141 169 'type' => 'string',
142 170 'required' => true,
143 171 ),
144 172 'price' => array(
145 - 'type' => 'float',
173 + 'type' => 'number',
146 174 'required' => true,
147 175 ),
148 176 'currency' => array(
149 177 'type' => 'string',
@@ -163,8 +191,12 @@
163 191 'tier' => array(
164 192 'type' => 'integer',
165 193 'required' => false,
166 194 ),
195 + 'description' => array(
196 + 'type' => 'string',
197 + 'required' => false,
198 + ),
167 199 ),
168 200 ),
169 201 array(
170 202 'methods' => WP_REST_Server::DELETABLE,
@@ -220,39 +252,9 @@
220 252 return new WP_Error( $result->get_error_code(), $result->get_error_message(), array( 'status' => $status ) );
221 253 }
222 254 return $result;
223 255 } else {
224 - $payload = array(
225 - 'type' => $request['type'],
226 - 'currency' => $request['currency'],
227 - );
228 -
229 - // If we pass directly is_editable as null, it would break API argument validation.
230 - if ( null !== $is_editable ) {
231 - $payload['is_editable'] = $is_editable;
232 - }
233 -
234 - $blog_id = Jetpack_Options::get_option( 'id' );
235 - $response = Client::wpcom_json_api_request_as_user(
236 - "/sites/$blog_id/{$this->rest_base}/products",
237 - 'v2',
238 - array(
239 - 'method' => 'POST',
240 - ),
241 - $payload
242 - );
243 - if ( is_wp_error( $response ) ) {
244 - if ( $response->get_error_code() === 'missing_token' ) {
245 - return new WP_Error( 'missing_token', __( 'Please connect your user account to WordPress.com', 'jetpack' ), 404 );
246 - }
247 - return new WP_Error( 'wpcom_connection_error', __( 'Could not connect to WordPress.com', 'jetpack' ), 404 );
248 - }
249 - $data = isset( $response['body'] ) ? json_decode( $response['body'], true ) : null;
250 - // If endpoint returned error, we have to detect it.
251 - if ( 200 !== $response['response']['code'] && $data['code'] ) {
252 - return new WP_Error( $data['code'], $data['message'] ? $data['message'] : '', 401 );
253 - }
254 - return $data;
256 + return $this->proxy_request_to_wpcom_as_user( $request, 'products' );
255 257 }
256 258
257 259 return $request;
258 260 }
@@ -264,11 +266,10 @@
264 266 *
265 267 * @return WP_Error|array ['products']
266 268 */
267 269 public function list_products( WP_REST_Request $request ) {
268 - $query = null;
269 270 $is_editable = isset( $request['is_editable'] ) ? (bool) $request['is_editable'] : null;
270 - $type = isset( $request['type'] ) ? $request['type'] : null;
271 + $type = $request['type'] ?? null;
271 272
272 273 if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
273 274 require_lib( 'memberships' );
274 275 require_once JETPACK__PLUGIN_DIR . '/modules/memberships/class-jetpack-memberships.php';
@@ -277,19 +278,10 @@
277 278 } catch ( \Exception $e ) {
278 279 return array( 'error' => $e->getMessage() );
279 280 }
280 281 } else {
281 - $query_parts = array();
282 - if ( $type !== null ) {
283 - $query_parts[] = 'type=' . $type;
284 - }
285 - if ( $is_editable !== null ) {
286 - $query_parts[] = 'is_editable=' . $is_editable;
287 - }
288 - if ( ! empty( $query_parts ) ) {
289 - $query = '?' . implode( '&', $query_parts );
290 - }
291 - return $this->proxy_request_to_wpcom( "products$query", 'GET' );
282 +
283 + return $this->proxy_request_to_wpcom_as_user( $request, 'products' );
292 284 }
293 285 }
294 286
295 287 /**
@@ -301,8 +293,12 @@
301 293 */
302 294 public function create_product( WP_REST_Request $request ) {
303 295 $payload = $this->get_payload_for_product( $request );
304 296
297 + if ( is_wp_error( $payload ) ) {
298 + return $payload;
299 + }
300 +
305 301 if ( $this->is_wpcom() ) {
306 302 require_lib( 'memberships' );
307 303 try {
308 304 return $this->create_product_from_wpcom( $payload );
@@ -309,9 +305,9 @@
309 305 } catch ( \Exception $e ) {
310 306 return array( 'error' => $e->getMessage() );
311 307 }
312 308 } else {
313 - return $this->proxy_request_to_wpcom( 'product', 'POST', $payload );
309 + return $this->proxy_request_to_wpcom_as_user( $request, 'product' );
314 310 }
315 311 }
316 312
317 313 /**
@@ -324,8 +320,12 @@
324 320 public function update_product( \WP_REST_Request $request ) {
325 321 $product_id = $request->get_param( 'product_id' );
326 322 $payload = $this->get_payload_for_product( $request );
327 323
324 + if ( is_wp_error( $payload ) ) {
325 + return $payload;
326 + }
327 +
328 328 if ( $this->is_wpcom() ) {
329 329 require_lib( 'memberships' );
330 330 try {
331 331 return array( 'product' => $this->update_product_from_wpcom( $product_id, $payload ) );
@@ -332,9 +332,9 @@
332 332 } catch ( \Exception $e ) {
333 333 return array( 'error' => $e->getMessage() );
334 334 }
335 335 } else {
336 - return $this->proxy_request_to_wpcom( "product/$product_id", 'POST', $payload );
336 + return $this->proxy_request_to_wpcom_as_user( $request, "product/$product_id" );
337 337 }
338 338 }
339 339
340 340 /**
@@ -355,13 +355,9 @@
355 355 } catch ( \Exception $e ) {
356 356 return array( 'error' => $e->getMessage() );
357 357 }
358 358 } else {
359 - return $this->proxy_request_to_wpcom(
360 - "product/$product_id",
361 - 'DELETE',
362 - array( 'cancel_subscriptions' => $cancel_subscriptions )
363 - );
359 + return $this->proxy_request_to_wpcom_as_user( $request, "product/$product_id" );
364 360 }
365 361 }
366 362
367 363 /**
@@ -410,82 +406,13 @@
410 406 }
411 407
412 408 return (array) $membership_settings;
413 409 } else {
414 - $payload = array(
415 - 'type' => $request['type'],
416 - 'source' => $source,
417 - );
418 -
419 - // If we pass directly is_editable as null, it would break API argument validation.
420 - // This also needs to be converted to int because boolean false is ignored by add_query_arg.
421 - if ( null !== $is_editable ) {
422 - $payload['is_editable'] = (int) $is_editable;
423 - }
424 -
425 - $blog_id = Jetpack_Options::get_option( 'id' );
426 - $path = "/sites/$blog_id/{$this->rest_base}/status";
427 - if ( $product_type ) {
428 - $path = add_query_arg(
429 - $payload,
430 - $path
431 - );
432 - }
433 - $response = Client::wpcom_json_api_request_as_user( $path, 'v2' );
434 - if ( is_wp_error( $response ) ) {
435 - if ( $response->get_error_code() === 'missing_token' ) {
436 - return new WP_Error( 'missing_token', __( 'Please connect your user account to WordPress.com', 'jetpack' ), 404 );
437 - }
438 - return new WP_Error( 'wpcom_connection_error', __( 'Could not connect to WordPress.com', 'jetpack' ), 404 );
439 - }
440 - $data = isset( $response['body'] ) ? json_decode( $response['body'], true ) : null;
441 - if ( 200 !== $response['response']['code'] && $data['code'] && $data['message'] ) {
442 - return new WP_Error( $data['code'], $data['message'], 401 );
443 - }
444 - return $data;
410 + return $this->proxy_request_to_wpcom_as_user( $request, 'status' );
445 411 }
446 412 }
447 413
448 414 /**
449 - * Proxy a request to WPCOM, look for errors and return a response or a WP_Error.
450 - *
451 - * @param string $uri Whatever would go at the end of the url after /sites/$blog_id/$this->rest_base/. This is usually `product`, `products`, or `product/$product_id`.
452 - * @param string $method The HTTP method being used.
453 - * @param array|null $payload An optional payload to be sent with the request.
454 - * @return string The response from WPCOM
455 - */
456 - private function proxy_request_to_wpcom( $uri, $method, $payload = null ) {
457 - // get blog id
458 - $blog_id = Jetpack_Options::get_option( 'id' );
459 -
460 - // proxy request to wpcom
461 - $response = Client::wpcom_json_api_request_as_user(
462 - "/sites/$blog_id/{$this->rest_base}/$uri",
463 - 'v2',
464 - array(
465 - 'method' => strtoupper( $method ),
466 - ),
467 - $payload
468 - );
469 - if ( is_wp_error( $response ) ) {
470 - if ( $response->get_error_code() === 'missing_token' ) {
471 - return new WP_Error( 'missing_token', __( 'Please connect your user account to WordPress.com', 'jetpack' ), 404 );
472 - }
473 - return new WP_Error( 'wpcom_connection_error', __( 'Could not connect to WordPress.com', 'jetpack' ), 404 );
474 - }
475 -
476 - // decode response
477 - $data = isset( $response['body'] ) ? json_decode( $response['body'], true ) : null;
478 - // If endpoint returned error, we have to detect it.
479 - if ( 200 !== $response['response']['code'] && $data['code'] && $data['message'] ) {
480 - return new WP_Error( $data['code'], $data['message'], 401 );
481 - }
482 -
483 - // return response
484 - return $data;
485 - }
486 -
487 - /**
488 415 * This function throws an exception if it is run outside of wpcom.
489 416 *
490 417 * @return void
491 418 * @throws \Exception If the function is run outside of WPCOM.
@@ -594,22 +521,29 @@
594 521 /**
595 522 * Get a payload for creating or updating products by parsing the request.
596 523 *
597 524 * @param WP_REST_Request $request The request for this endpoint, containing the details needed to build the payload.
598 - * @return array The built payload.
525 + * @return array|WP_Error The built payload or WP_Error on validation failure.
599 526 */
600 527 private function get_payload_for_product( WP_REST_Request $request ) {
601 528 $is_editable = isset( $request['is_editable'] ) ? (bool) $request['is_editable'] : null;
602 - $type = isset( $request['type'] ) ? $request['type'] : null;
603 - $tier = isset( $request['tier'] ) ? $request['tier'] : null;
529 + $type = $request['type'] ?? null;
530 + $tier = $request['tier'] ?? null;
604 531 $buyer_can_change_amount = isset( $request['buyer_can_change_amount'] ) && (bool) $request['buyer_can_change_amount'];
532 + $interval = $request['interval'];
605 533
534 + // Validate tier field usage.
535 + $tier_validation = $this->validate_tier_field( $request, $tier, $type, $interval );
536 + if ( is_wp_error( $tier_validation ) ) {
537 + return $tier_validation;
538 + }
539 +
606 540 $payload = array(
607 541 'title' => $request['title'],
608 542 'price' => $request['price'],
609 543 'currency' => $request['currency'],
610 544 'buyer_can_change_amount' => $buyer_can_change_amount,
611 - 'interval' => $request['interval'],
545 + 'interval' => $interval,
612 546 'type' => $type,
613 547 'welcome_email_content' => $request['welcome_email_content'],
614 548 'subscribe_as_site_subscriber' => $request['subscribe_as_site_subscriber'],
615 549 'multiple_per_user' => $request['multiple_per_user'],
@@ -622,9 +556,117 @@
622 556 // If we pass directly the value "null", it will break the argument validation.
623 557 if ( null !== $is_editable ) {
624 558 $payload['is_editable'] = $is_editable;
625 559 }
560 +
561 + if ( isset( $request['description'] ) ) {
562 + $payload['description'] = $request['description'];
563 + }
564 +
626 565 return $payload;
566 + }
567 +
568 + /**
569 + * Validate tier field usage for newsletter plans.
570 + *
571 + * @param WP_REST_Request $request The request object.
572 + * @param string|null $tier The tier value to validate.
573 + * @param string|null $type The product type.
574 + * @param string $interval The product interval.
575 + * @return WP_Error|null Error object if validation fails, null if successful.
576 + */
577 + private function validate_tier_field( WP_REST_Request $request, $tier, $type, $interval ) {
578 + // Only apply tier validation for newsletter plans with type 'tier'.
579 + if ( null === $tier || 'tier' !== $type ) {
580 + return null;
581 + }
582 +
583 + // Monthly plans should not have a tier field.
584 + if ( '1 month' === $interval ) {
585 + return new WP_Error( 'invalid_tier_usage', __( 'Monthly plans should not have a tier field. The tier field is only used to link yearly plans to their corresponding monthly plans.', 'jetpack' ), array( 'status' => 400 ) );
586 + }
587 +
588 + // Yearly plans must have a valid tier that points to a monthly plan.
589 + if ( '1 year' === $interval ) {
590 + return $this->validate_yearly_tier( $request, $tier );
591 + }
592 +
593 + return null;
594 + }
595 +
596 + /**
597 + * Validate yearly tier requirements.
598 + *
599 + * @param WP_REST_Request $request The request object.
600 + * @param string|int $tier The tier value to validate.
601 + * @return WP_Error|null Error object if validation fails, null if successful.
602 + */
603 + private function validate_yearly_tier( WP_REST_Request $request, $tier ) {
604 + if ( ! is_numeric( $tier ) || $tier <= 0 ) {
605 + return new WP_Error( 'invalid_tier_id', __( 'Yearly plans must have a valid tier ID that points to an existing monthly plan.', 'jetpack' ), array( 'status' => 400 ) );
606 + }
607 +
608 + if ( ! $this->is_wpcom() ) {
609 + return null; // Validation will happen on WPCOM side.
610 + }
611 +
612 + return $this->validate_tier_references( $request, $tier );
613 + }
614 +
615 + /**
616 + * Validate that the tier references a valid monthly plan and check for duplicates.
617 + *
618 + * @param WP_REST_Request $request The request object.
619 + * @param string|int $tier The tier value to validate.
620 + * @return WP_Error|null Error object if validation fails, null if successful.
621 + */
622 + private function validate_tier_references( WP_REST_Request $request, $tier ) {
623 + require_lib( 'memberships' );
624 + Memberships_Store_Sandbox::get_instance()->init( true );
625 +
626 + // Check if the referenced monthly plan exists and is actually a monthly plan.
627 + $monthly_plan = Memberships_Product::get_from_post( get_current_blog_id(), $tier );
628 + if ( is_wp_error( $monthly_plan ) || ! $monthly_plan ) {
629 + return new WP_Error( 'tier_not_found', __( 'The specified tier ID does not correspond to an existing monthly plan.', 'jetpack' ), array( 'status' => 400 ) );
630 + }
631 +
632 + $monthly_plan_data = $monthly_plan->to_array();
633 + if ( '1 month' !== $monthly_plan_data['interval'] ) {
634 + return new WP_Error( 'invalid_tier_interval', __( 'The specified tier ID must point to a monthly plan (1 month interval).', 'jetpack' ), array( 'status' => 400 ) );
635 + }
636 +
637 + return $this->check_duplicate_tier_references( $request, $tier );
638 + }
639 +
640 + /**
641 + * Check for duplicate tier references.
642 + *
643 + * @param WP_REST_Request $request The request object.
644 + * @param string|int $tier The tier value to check.
645 + * @return WP_Error|null Error object if duplicate found, null if successful.
646 + */
647 + private function check_duplicate_tier_references( WP_REST_Request $request, $tier ) {
648 + $existing_yearly_plans = Memberships_Product::get_product_list( get_current_blog_id(), 'tier', null, false );
649 + if ( is_wp_error( $existing_yearly_plans ) ) {
650 + return new WP_Error( 'product_list_error', __( 'Could not retrieve existing products to check for duplicate tier references.', 'jetpack' ), array( 'status' => 500 ) );
651 + }
652 +
653 + // Ensure the result is iterable before foreach.
654 + if ( ! is_array( $existing_yearly_plans ) && ! $existing_yearly_plans instanceof Traversable ) {
655 + return new WP_Error( 'invalid_product_list', __( 'Unexpected error: product list is not iterable.', 'jetpack' ), array( 'status' => 500 ) );
656 + }
657 +
658 + foreach ( $existing_yearly_plans as $existing_plan ) {
659 + if ( isset( $existing_plan['tier'] ) && (string) $existing_plan['tier'] === (string) $tier && '1 year' === $existing_plan['interval'] ) {
660 + // If this is an update, allow it to reference itself.
661 + $product_id = $request->get_param( 'product_id' );
662 + if ( ! $product_id || (string) $existing_plan['id'] !== (string) $product_id ) {
663 + return new WP_Error( 'duplicate_tier_reference', __( 'Another yearly plan already references this monthly plan. Each monthly plan can only have one corresponding yearly plan.', 'jetpack' ), array( 'status' => 400 ) );
664 + }
665 + }
666 + }
667 +
668 + return null;
627 669 }
628 670
629 671 /**
630 672 * Returns true if run from WPCOM.