Skip to content

Commit

Permalink
Add/memberships store (#37097)
Browse files Browse the repository at this point in the history
* Update memberships.php

* Create add-memberships-store

* Added stub for Memberships_Store_Sandbox

* Revert "Added stub for Memberships_Store_Sandbox"

This reverts commit ad83004.

* Hardcode init() stub

* Update wpcom-stubs.php

---------

Co-authored-by: Fabien MILLERAND <fab.millerand@automattic.com>
  • Loading branch information
millerf and Fabien MILLERAND committed May 10, 2024
1 parent 90f0812 commit d447771
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
9 changes: 6 additions & 3 deletions .phan/stubs/wpcom-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static function get_current_plan($blog_id = 0)
class WPCOM_Store
{
/**
* @deprecated
* @deprecated
* @param int $user_id
* @param int $blog_id
* @param false $ids_only
Expand Down Expand Up @@ -343,6 +343,9 @@ public function init($force = \false)
public static function get_instance(): \Memberships_Store_Sandbox
{
}
public static function init(bool $force = false)
{
}
}
/**
* @param int $user_id
Expand Down Expand Up @@ -838,7 +841,7 @@ function get_active_blogs_for_user($args = array())
/**
* @param string $url
* @return string
* @internal
* @internal
*/
function staticize_subdomain($url)
{
Expand Down Expand Up @@ -908,7 +911,7 @@ function wpcom_should_load_theme_files_on_rest_api()
{
}
/**
* @deprecated
* @deprecated
* @return bool
*/
function wpcom_is_jetpack_search_supported($blog_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public function create_products( $request ) {

if ( $this->is_wpcom() ) {
require_lib( 'memberships' );
Memberships_Store_Sandbox::get_instance()->init( true );

$result = Memberships_Product::generate_default_products( get_current_blog_id(), $request['type'], $request['currency'], $is_editable );

Expand Down Expand Up @@ -382,6 +383,7 @@ public function get_status( \WP_REST_Request $request ) {

if ( $this->is_wpcom() ) {
require_lib( 'memberships' );
Memberships_Store_Sandbox::get_instance()->init( true );
$blog_id = get_current_blog_id();
$membership_settings = get_memberships_settings_for_site( $blog_id, $product_type, $is_editable, $source );

Expand Down Expand Up @@ -504,6 +506,7 @@ private function prevent_running_outside_of_wpcom() {
*/
private function list_products_from_wpcom( WP_REST_Request $request, $type, $is_editable ) {
$this->prevent_running_outside_of_wpcom();
Memberships_Store_Sandbox::get_instance()->init( true );
$blog_id = $request->get_param( 'blog_id' );
if ( is_wp_error( $blog_id ) ) {
throw new \Exception( 'Unknown blog' );
Expand All @@ -524,6 +527,7 @@ private function list_products_from_wpcom( WP_REST_Request $request, $type, $is_
*/
private function find_product_from_wpcom( $product_id ) {
$this->prevent_running_outside_of_wpcom();
Memberships_Store_Sandbox::get_instance()->init( true );
$product = Memberships_Product::get_from_post( get_current_blog_id(), $product_id );
if ( is_wp_error( $product ) ) {
throw new \Exception( $product->get_error_message() );
Expand All @@ -543,6 +547,7 @@ private function find_product_from_wpcom( $product_id ) {
*/
private function create_product_from_wpcom( $payload ) {
$this->prevent_running_outside_of_wpcom();
Memberships_Store_Sandbox::get_instance()->init( true );
$product = Memberships_Product::create( get_current_blog_id(), $payload );
if ( is_wp_error( $product ) ) {
throw new \Exception( __( 'Creating product has failed.', 'jetpack' ) );
Expand All @@ -559,6 +564,7 @@ private function create_product_from_wpcom( $payload ) {
* @return object The newly updated product.
*/
private function update_product_from_wpcom( $product_id, $payload ) {
Memberships_Store_Sandbox::get_instance()->init( true );
$product = $this->find_product_from_wpcom( $product_id ); // prevents running outside of wpcom
$updated_product = $product->update( $payload );
if ( is_wp_error( $updated_product ) ) {
Expand All @@ -576,6 +582,7 @@ private function update_product_from_wpcom( $product_id, $payload ) {
* @return void
*/
private function delete_product_from_wpcom( $product_id, $cancel_subscriptions = false ) {
Memberships_Store_Sandbox::get_instance()->init( true );
$product = $this->find_product_from_wpcom( $product_id ); // prevents running outside of wpcom
$result = $product->delete( $cancel_subscriptions ? Memberships_Product::CANCEL_SUBSCRIPTIONS : Memberships_Product::KEEP_SUBSCRIPTIONS );
if ( is_wp_error( $result ) ) {
Expand Down
5 changes: 5 additions & 0 deletions projects/plugins/jetpack/changelog/add-memberships-store
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Support for WPCOM endpoints


0 comments on commit d447771

Please sign in to comment.