← All changes
|
_inc/lib/core-api/wpcom-endpoints/memberships.php
+181
-131
13.3.3
→
16.3-a.7
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' ) ); |
| @@ -57,8 +67,9 @@ | ||
| 57 | 67 | 'earn-newsletter', |
| 58 | 68 | 'gutenberg', |
| 59 | 69 | 'gutenberg-wpcom', |
| 60 | 70 | 'launchpad', |
| 71 | + 'import-paid-subscribers', | |
| 61 | 72 | ), |
| 62 | 73 | true |
| 63 | 74 | ); |
| 64 | 75 | }, |
| @@ -84,9 +95,9 @@ | ||
| 84 | 95 | 'type' => 'string', |
| 85 | 96 | 'required' => true, |
| 86 | 97 | ), |
| 87 | 98 | 'price' => array( |
| 88 | - 'type' => 'float', | |
| 99 | + 'type' => 'number', | |
| 89 | 100 | 'required' => true, |
| 90 | 101 | ), |
| 91 | 102 | 'currency' => array( |
| 92 | 103 | 'type' => 'string', |
| @@ -106,8 +117,12 @@ | ||
| 106 | 117 | 'tier' => array( |
| 107 | 118 | 'type' => 'integer', |
| 108 | 119 | 'required' => false, |
| 109 | 120 | ), |
| 121 | + 'description' => array( | |
| 122 | + 'type' => 'string', | |
| 123 | + 'required' => false, | |
| 124 | + ), | |
| 110 | 125 | ), |
| 111 | 126 | ), |
| 112 | 127 | ) |
| 113 | 128 | ); |
| @@ -118,8 +133,22 @@ | ||
| 118 | 133 | array( |
| 119 | 134 | 'methods' => WP_REST_Server::CREATABLE, |
| 120 | 135 | 'callback' => array( $this, 'create_products' ), |
| 121 | 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 | + ), | |
| 122 | 151 | ), |
| 123 | 152 | array( |
| 124 | 153 | 'methods' => WP_REST_Server::READABLE, |
| 125 | 154 | 'callback' => array( $this, 'list_products' ), |
| @@ -140,9 +169,9 @@ | ||
| 140 | 169 | 'type' => 'string', |
| 141 | 170 | 'required' => true, |
| 142 | 171 | ), |
| 143 | 172 | 'price' => array( |
| 144 | - 'type' => 'float', | |
| 173 | + 'type' => 'number', | |
| 145 | 174 | 'required' => true, |
| 146 | 175 | ), |
| 147 | 176 | 'currency' => array( |
| 148 | 177 | 'type' => 'string', |
| @@ -162,8 +191,12 @@ | ||
| 162 | 191 | 'tier' => array( |
| 163 | 192 | 'type' => 'integer', |
| 164 | 193 | 'required' => false, |
| 165 | 194 | ), |
| 195 | + 'description' => array( | |
| 196 | + 'type' => 'string', | |
| 197 | + 'required' => false, | |
| 198 | + ), | |
| 166 | 199 | ), |
| 167 | 200 | ), |
| 168 | 201 | array( |
| 169 | 202 | 'methods' => WP_REST_Server::DELETABLE, |
| @@ -209,8 +242,9 @@ | ||
| 209 | 242 | $is_editable = isset( $request['is_editable'] ) ? (bool) $request['is_editable'] : null; |
| 210 | 243 | |
| 211 | 244 | if ( $this->is_wpcom() ) { |
| 212 | 245 | require_lib( 'memberships' ); |
| 246 | + Memberships_Store_Sandbox::get_instance()->init( true ); | |
| 213 | 247 | |
| 214 | 248 | $result = Memberships_Product::generate_default_products( get_current_blog_id(), $request['type'], $request['currency'], $is_editable ); |
| 215 | 249 | |
| 216 | 250 | if ( is_wp_error( $result ) ) { |
| @@ -218,39 +252,9 @@ | ||
| 218 | 252 | return new WP_Error( $result->get_error_code(), $result->get_error_message(), array( 'status' => $status ) ); |
| 219 | 253 | } |
| 220 | 254 | return $result; |
| 221 | 255 | } else { |
| 222 | - $payload = array( | |
| 223 | - 'type' => $request['type'], | |
| 224 | - 'currency' => $request['currency'], | |
| 225 | - ); | |
| 226 | - | |
| 227 | - // If we pass directly is_editable as null, it would break API argument validation. | |
| 228 | - if ( null !== $is_editable ) { | |
| 229 | - $payload['is_editable'] = $is_editable; | |
| 230 | - } | |
| 231 | - | |
| 232 | - $blog_id = Jetpack_Options::get_option( 'id' ); | |
| 233 | - $response = Client::wpcom_json_api_request_as_user( | |
| 234 | - "/sites/$blog_id/{$this->rest_base}/products", | |
| 235 | - 'v2', | |
| 236 | - array( | |
| 237 | - 'method' => 'POST', | |
| 238 | - ), | |
| 239 | - $payload | |
| 240 | - ); | |
| 241 | - if ( is_wp_error( $response ) ) { | |
| 242 | - if ( $response->get_error_code() === 'missing_token' ) { | |
| 243 | - return new WP_Error( 'missing_token', __( 'Please connect your user account to WordPress.com', 'jetpack' ), 404 ); | |
| 244 | - } | |
| 245 | - return new WP_Error( 'wpcom_connection_error', __( 'Could not connect to WordPress.com', 'jetpack' ), 404 ); | |
| 246 | - } | |
| 247 | - $data = isset( $response['body'] ) ? json_decode( $response['body'], true ) : null; | |
| 248 | - // If endpoint returned error, we have to detect it. | |
| 249 | - if ( 200 !== $response['response']['code'] && $data['code'] ) { | |
| 250 | - return new WP_Error( $data['code'], $data['message'] ? $data['message'] : '', 401 ); | |
| 251 | - } | |
| 252 | - return $data; | |
| 256 | + return $this->proxy_request_to_wpcom_as_user( $request, 'products' ); | |
| 253 | 257 | } |
| 254 | 258 | |
| 255 | 259 | return $request; |
| 256 | 260 | } |
| @@ -262,11 +266,10 @@ | ||
| 262 | 266 | * |
| 263 | 267 | * @return WP_Error|array ['products'] |
| 264 | 268 | */ |
| 265 | 269 | public function list_products( WP_REST_Request $request ) { |
| 266 | - $query = null; | |
| 267 | 270 | $is_editable = isset( $request['is_editable'] ) ? (bool) $request['is_editable'] : null; |
| 268 | - $type = isset( $request['type'] ) ? $request['type'] : null; | |
| 271 | + $type = $request['type'] ?? null; | |
| 269 | 272 | |
| 270 | 273 | if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { |
| 271 | 274 | require_lib( 'memberships' ); |
| 272 | 275 | require_once JETPACK__PLUGIN_DIR . '/modules/memberships/class-jetpack-memberships.php'; |
| @@ -275,19 +278,10 @@ | ||
| 275 | 278 | } catch ( \Exception $e ) { |
| 276 | 279 | return array( 'error' => $e->getMessage() ); |
| 277 | 280 | } |
| 278 | 281 | } else { |
| 279 | - $query_parts = array(); | |
| 280 | - if ( $type !== null ) { | |
| 281 | - $query_parts[] = 'type=' . $type; | |
| 282 | - } | |
| 283 | - if ( $is_editable !== null ) { | |
| 284 | - $query_parts[] = 'is_editable=' . $is_editable; | |
| 285 | - } | |
| 286 | - if ( ! empty( $query_parts ) ) { | |
| 287 | - $query = '?' . implode( '&', $query_parts ); | |
| 288 | - } | |
| 289 | - return $this->proxy_request_to_wpcom( "products$query", 'GET' ); | |
| 282 | + | |
| 283 | + return $this->proxy_request_to_wpcom_as_user( $request, 'products' ); | |
| 290 | 284 | } |
| 291 | 285 | } |
| 292 | 286 | |
| 293 | 287 | /** |
| @@ -299,8 +293,12 @@ | ||
| 299 | 293 | */ |
| 300 | 294 | public function create_product( WP_REST_Request $request ) { |
| 301 | 295 | $payload = $this->get_payload_for_product( $request ); |
| 302 | 296 | |
| 297 | + if ( is_wp_error( $payload ) ) { | |
| 298 | + return $payload; | |
| 299 | + } | |
| 300 | + | |
| 303 | 301 | if ( $this->is_wpcom() ) { |
| 304 | 302 | require_lib( 'memberships' ); |
| 305 | 303 | try { |
| 306 | 304 | return $this->create_product_from_wpcom( $payload ); |
| @@ -307,9 +305,9 @@ | ||
| 307 | 305 | } catch ( \Exception $e ) { |
| 308 | 306 | return array( 'error' => $e->getMessage() ); |
| 309 | 307 | } |
| 310 | 308 | } else { |
| 311 | - return $this->proxy_request_to_wpcom( 'product', 'POST', $payload ); | |
| 309 | + return $this->proxy_request_to_wpcom_as_user( $request, 'product' ); | |
| 312 | 310 | } |
| 313 | 311 | } |
| 314 | 312 | |
| 315 | 313 | /** |
| @@ -322,8 +320,12 @@ | ||
| 322 | 320 | public function update_product( \WP_REST_Request $request ) { |
| 323 | 321 | $product_id = $request->get_param( 'product_id' ); |
| 324 | 322 | $payload = $this->get_payload_for_product( $request ); |
| 325 | 323 | |
| 324 | + if ( is_wp_error( $payload ) ) { | |
| 325 | + return $payload; | |
| 326 | + } | |
| 327 | + | |
| 326 | 328 | if ( $this->is_wpcom() ) { |
| 327 | 329 | require_lib( 'memberships' ); |
| 328 | 330 | try { |
| 329 | 331 | return array( 'product' => $this->update_product_from_wpcom( $product_id, $payload ) ); |
| @@ -330,9 +332,9 @@ | ||
| 330 | 332 | } catch ( \Exception $e ) { |
| 331 | 333 | return array( 'error' => $e->getMessage() ); |
| 332 | 334 | } |
| 333 | 335 | } else { |
| 334 | - return $this->proxy_request_to_wpcom( "product/$product_id", 'POST', $payload ); | |
| 336 | + return $this->proxy_request_to_wpcom_as_user( $request, "product/$product_id" ); | |
| 335 | 337 | } |
| 336 | 338 | } |
| 337 | 339 | |
| 338 | 340 | /** |
| @@ -353,13 +355,9 @@ | ||
| 353 | 355 | } catch ( \Exception $e ) { |
| 354 | 356 | return array( 'error' => $e->getMessage() ); |
| 355 | 357 | } |
| 356 | 358 | } else { |
| 357 | - return $this->proxy_request_to_wpcom( | |
| 358 | - "product/$product_id", | |
| 359 | - 'DELETE', | |
| 360 | - array( 'cancel_subscriptions' => $cancel_subscriptions ) | |
| 361 | - ); | |
| 359 | + return $this->proxy_request_to_wpcom_as_user( $request, "product/$product_id" ); | |
| 362 | 360 | } |
| 363 | 361 | } |
| 364 | 362 | |
| 365 | 363 | /** |
| @@ -381,8 +379,9 @@ | ||
| 381 | 379 | $is_editable = ! isset( $request['is_editable'] ) ? null : (bool) $request['is_editable']; |
| 382 | 380 | |
| 383 | 381 | if ( $this->is_wpcom() ) { |
| 384 | 382 | require_lib( 'memberships' ); |
| 383 | + Memberships_Store_Sandbox::get_instance()->init( true ); | |
| 385 | 384 | $blog_id = get_current_blog_id(); |
| 386 | 385 | $membership_settings = get_memberships_settings_for_site( $blog_id, $product_type, $is_editable, $source ); |
| 387 | 386 | |
| 388 | 387 | if ( is_wp_error( $membership_settings ) ) { |
| @@ -407,82 +406,13 @@ | ||
| 407 | 406 | } |
| 408 | 407 | |
| 409 | 408 | return (array) $membership_settings; |
| 410 | 409 | } else { |
| 411 | - $payload = array( | |
| 412 | - 'type' => $request['type'], | |
| 413 | - 'source' => $source, | |
| 414 | - ); | |
| 415 | - | |
| 416 | - // If we pass directly is_editable as null, it would break API argument validation. | |
| 417 | - // This also needs to be converted to int because boolean false is ignored by add_query_arg. | |
| 418 | - if ( null !== $is_editable ) { | |
| 419 | - $payload['is_editable'] = (int) $is_editable; | |
| 420 | - } | |
| 421 | - | |
| 422 | - $blog_id = Jetpack_Options::get_option( 'id' ); | |
| 423 | - $path = "/sites/$blog_id/{$this->rest_base}/status"; | |
| 424 | - if ( $product_type ) { | |
| 425 | - $path = add_query_arg( | |
| 426 | - $payload, | |
| 427 | - $path | |
| 428 | - ); | |
| 429 | - } | |
| 430 | - $response = Client::wpcom_json_api_request_as_user( $path, 'v2' ); | |
| 431 | - if ( is_wp_error( $response ) ) { | |
| 432 | - if ( $response->get_error_code() === 'missing_token' ) { | |
| 433 | - return new WP_Error( 'missing_token', __( 'Please connect your user account to WordPress.com', 'jetpack' ), 404 ); | |
| 434 | - } | |
| 435 | - return new WP_Error( 'wpcom_connection_error', __( 'Could not connect to WordPress.com', 'jetpack' ), 404 ); | |
| 436 | - } | |
| 437 | - $data = isset( $response['body'] ) ? json_decode( $response['body'], true ) : null; | |
| 438 | - if ( 200 !== $response['response']['code'] && $data['code'] && $data['message'] ) { | |
| 439 | - return new WP_Error( $data['code'], $data['message'], 401 ); | |
| 440 | - } | |
| 441 | - return $data; | |
| 410 | + return $this->proxy_request_to_wpcom_as_user( $request, 'status' ); | |
| 442 | 411 | } |
| 443 | 412 | } |
| 444 | 413 | |
| 445 | 414 | /** |
| 446 | - * Proxy a request to WPCOM, look for errors and return a response or a WP_Error. | |
| 447 | - * | |
| 448 | - * @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`. | |
| 449 | - * @param string $method The HTTP method being used. | |
| 450 | - * @param array|null $payload An optional payload to be sent with the request. | |
| 451 | - * @return string The response from WPCOM | |
| 452 | - */ | |
| 453 | - private function proxy_request_to_wpcom( $uri, $method, $payload = null ) { | |
| 454 | - // get blog id | |
| 455 | - $blog_id = Jetpack_Options::get_option( 'id' ); | |
| 456 | - | |
| 457 | - // proxy request to wpcom | |
| 458 | - $response = Client::wpcom_json_api_request_as_user( | |
| 459 | - "/sites/$blog_id/{$this->rest_base}/$uri", | |
| 460 | - 'v2', | |
| 461 | - array( | |
| 462 | - 'method' => strtoupper( $method ), | |
| 463 | - ), | |
| 464 | - $payload | |
| 465 | - ); | |
| 466 | - if ( is_wp_error( $response ) ) { | |
| 467 | - if ( $response->get_error_code() === 'missing_token' ) { | |
| 468 | - return new WP_Error( 'missing_token', __( 'Please connect your user account to WordPress.com', 'jetpack' ), 404 ); | |
| 469 | - } | |
| 470 | - return new WP_Error( 'wpcom_connection_error', __( 'Could not connect to WordPress.com', 'jetpack' ), 404 ); | |
| 471 | - } | |
| 472 | - | |
| 473 | - // decode response | |
| 474 | - $data = isset( $response['body'] ) ? json_decode( $response['body'], true ) : null; | |
| 475 | - // If endpoint returned error, we have to detect it. | |
| 476 | - if ( 200 !== $response['response']['code'] && $data['code'] && $data['message'] ) { | |
| 477 | - return new WP_Error( $data['code'], $data['message'], 401 ); | |
| 478 | - } | |
| 479 | - | |
| 480 | - // return response | |
| 481 | - return $data; | |
| 482 | - } | |
| 483 | - | |
| 484 | - /** | |
| 485 | 415 | * This function throws an exception if it is run outside of wpcom. |
| 486 | 416 | * |
| 487 | 417 | * @return void |
| 488 | 418 | * @throws \Exception If the function is run outside of WPCOM. |
| @@ -497,14 +427,15 @@ | ||
| 497 | 427 | * List products via the WPCOM-specific Memberships_Product class. |
| 498 | 428 | * |
| 499 | 429 | * @param WP_REST_Request $request The request for this endpoint. |
| 500 | 430 | * @param ?string $type The type of the products to list. |
| 501 | - * @param ?string $is_editable This string will be interpreted as a bool to determine if we are looking for editable or non-editable products. | |
| 431 | + * @param ?bool $is_editable If we are looking for editable or non-editable products. | |
| 502 | 432 | * @throws \Exception If blog is not known or if there is an error getting products. |
| 503 | 433 | * @return array List of products. |
| 504 | 434 | */ |
| 505 | 435 | private function list_products_from_wpcom( WP_REST_Request $request, $type, $is_editable ) { |
| 506 | 436 | $this->prevent_running_outside_of_wpcom(); |
| 437 | + Memberships_Store_Sandbox::get_instance()->init( true ); | |
| 507 | 438 | $blog_id = $request->get_param( 'blog_id' ); |
| 508 | 439 | if ( is_wp_error( $blog_id ) ) { |
| 509 | 440 | throw new \Exception( 'Unknown blog' ); |
| 510 | 441 | } |
| @@ -523,8 +454,9 @@ | ||
| 523 | 454 | * @return object The found product. |
| 524 | 455 | */ |
| 525 | 456 | private function find_product_from_wpcom( $product_id ) { |
| 526 | 457 | $this->prevent_running_outside_of_wpcom(); |
| 458 | + Memberships_Store_Sandbox::get_instance()->init( true ); | |
| 527 | 459 | $product = Memberships_Product::get_from_post( get_current_blog_id(), $product_id ); |
| 528 | 460 | if ( is_wp_error( $product ) ) { |
| 529 | 461 | throw new \Exception( $product->get_error_message() ); |
| 530 | 462 | } |
| @@ -538,12 +470,13 @@ | ||
| 538 | 470 | * Create a product via the WPCOM-specific Memberships_Product class. |
| 539 | 471 | * |
| 540 | 472 | * @param array $payload The request payload which contains details about the product. |
| 541 | 473 | * @throws \Exception When the product failed to be created. |
| 542 | - * @return object The newly created product. | |
| 474 | + * @return array The newly created product. | |
| 543 | 475 | */ |
| 544 | 476 | private function create_product_from_wpcom( $payload ) { |
| 545 | 477 | $this->prevent_running_outside_of_wpcom(); |
| 478 | + Memberships_Store_Sandbox::get_instance()->init( true ); | |
| 546 | 479 | $product = Memberships_Product::create( get_current_blog_id(), $payload ); |
| 547 | 480 | if ( is_wp_error( $product ) ) { |
| 548 | 481 | throw new \Exception( __( 'Creating product has failed.', 'jetpack' ) ); |
| 549 | 482 | } |
| @@ -558,8 +491,9 @@ | ||
| 558 | 491 | * @throws \Exception When there is a problem updating the product. |
| 559 | 492 | * @return object The newly updated product. |
| 560 | 493 | */ |
| 561 | 494 | private function update_product_from_wpcom( $product_id, $payload ) { |
| 495 | + Memberships_Store_Sandbox::get_instance()->init( true ); | |
| 562 | 496 | $product = $this->find_product_from_wpcom( $product_id ); // prevents running outside of wpcom |
| 563 | 497 | $updated_product = $product->update( $payload ); |
| 564 | 498 | if ( is_wp_error( $updated_product ) ) { |
| 565 | 499 | throw new \Exception( $updated_product->get_error_message() ); |
| @@ -575,8 +509,9 @@ | ||
| 575 | 509 | * @throws \Exception When there is a problem deleting the product. |
| 576 | 510 | * @return void |
| 577 | 511 | */ |
| 578 | 512 | private function delete_product_from_wpcom( $product_id, $cancel_subscriptions = false ) { |
| 513 | + Memberships_Store_Sandbox::get_instance()->init( true ); | |
| 579 | 514 | $product = $this->find_product_from_wpcom( $product_id ); // prevents running outside of wpcom |
| 580 | 515 | $result = $product->delete( $cancel_subscriptions ? Memberships_Product::CANCEL_SUBSCRIPTIONS : Memberships_Product::KEEP_SUBSCRIPTIONS ); |
| 581 | 516 | if ( is_wp_error( $result ) ) { |
| 582 | 517 | throw new \Exception( $result->get_error_message() ); |
| @@ -586,22 +521,29 @@ | ||
| 586 | 521 | /** |
| 587 | 522 | * Get a payload for creating or updating products by parsing the request. |
| 588 | 523 | * |
| 589 | 524 | * @param WP_REST_Request $request The request for this endpoint, containing the details needed to build the payload. |
| 590 | - * @return array The built payload. | |
| 525 | + * @return array|WP_Error The built payload or WP_Error on validation failure. | |
| 591 | 526 | */ |
| 592 | 527 | private function get_payload_for_product( WP_REST_Request $request ) { |
| 593 | 528 | $is_editable = isset( $request['is_editable'] ) ? (bool) $request['is_editable'] : null; |
| 594 | - $type = isset( $request['type'] ) ? $request['type'] : null; | |
| 595 | - $tier = isset( $request['tier'] ) ? $request['tier'] : null; | |
| 529 | + $type = $request['type'] ?? null; | |
| 530 | + $tier = $request['tier'] ?? null; | |
| 596 | 531 | $buyer_can_change_amount = isset( $request['buyer_can_change_amount'] ) && (bool) $request['buyer_can_change_amount']; |
| 532 | + $interval = $request['interval']; | |
| 597 | 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 | + | |
| 598 | 540 | $payload = array( |
| 599 | 541 | 'title' => $request['title'], |
| 600 | 542 | 'price' => $request['price'], |
| 601 | 543 | 'currency' => $request['currency'], |
| 602 | 544 | 'buyer_can_change_amount' => $buyer_can_change_amount, |
| 603 | - 'interval' => $request['interval'], | |
| 545 | + 'interval' => $interval, | |
| 604 | 546 | 'type' => $type, |
| 605 | 547 | 'welcome_email_content' => $request['welcome_email_content'], |
| 606 | 548 | 'subscribe_as_site_subscriber' => $request['subscribe_as_site_subscriber'], |
| 607 | 549 | 'multiple_per_user' => $request['multiple_per_user'], |
| @@ -614,9 +556,117 @@ | ||
| 614 | 556 | // If we pass directly the value "null", it will break the argument validation. |
| 615 | 557 | if ( null !== $is_editable ) { |
| 616 | 558 | $payload['is_editable'] = $is_editable; |
| 617 | 559 | } |
| 560 | + | |
| 561 | + if ( isset( $request['description'] ) ) { | |
| 562 | + $payload['description'] = $request['description']; | |
| 563 | + } | |
| 564 | + | |
| 618 | 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; | |
| 619 | 669 | } |
| 620 | 670 | |
| 621 | 671 | /** |
| 622 | 672 | * Returns true if run from WPCOM. |