Skip to content

Commit

Permalink
Add sync coming soon status from LYS to WPCOM (#1503)
Browse files Browse the repository at this point in the history
* Add atomic API helper, add sync from lys to wpcom

* Changelog

* Update includes/class-wc-calypso-bridge-coming-soon.php

Co-authored-by: Chi-Hsuan Huang <[email protected]>

---------

Co-authored-by: Chi-Hsuan Huang <[email protected]>
  • Loading branch information
ilyasfoo and chihsuan authored Aug 26, 2024
1 parent 12d014b commit 1c78762
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 4 deletions.
2 changes: 2 additions & 0 deletions class-wc-calypso-bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ public function initialize() {
* Include files and controllers.
*/
public function includes() {
// Helpers.
require_once WC_CALYPSO_BRIDGE_PLUGIN_PATH . '/includes/atomic-api/class-wc-calypso-bridge-atomic-launch-api.php';

/**
* Hint:
Expand Down
57 changes: 57 additions & 0 deletions includes/atomic-api/class-wc-calypso-bridge-atomic-launch-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

use Automattic\Jetpack\Connection\Client;

/**
* Class WC_Calypso_Bridge_Atomic_Launch_API.
*
* @since x.x.x
* @version x.x.x
*
* API for launch on Atomic.
*/
class WC_Calypso_Bridge_Atomic_Launch_API {
/**
* Launch Atomic.
*/
public static function launch_site() {
if ( ! class_exists( '\Jetpack_Options' ) ) {
return;
}

$blog_id = \Jetpack_Options::get_option( 'id' );
return Client::wpcom_json_api_request_as_user(
sprintf( '/sites/%d/launch', $blog_id ),
'2',
array(
'method' => 'POST',
),
json_encode( array(
'site' => $blog_id
) ),
'wpcom'
);
}

/**
* Update coming soon.
*/
public static function update_coming_soon( $is_coming_soon ) {
if ( ! class_exists( '\Jetpack_Options' ) ) {
return;
}

$blog_id = \Jetpack_Options::get_option( 'id' );
return Client::wpcom_json_api_request_as_user(
sprintf( '/sites/%d/coming-soon', $blog_id ),
'2',
array(
'method' => 'POST',
),
array(
'is_coming_soon' => $is_coming_soon ? 1 : 0
),
'wpcom'
);
}
}
92 changes: 88 additions & 4 deletions includes/class-wc-calypso-bridge-coming-soon.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,22 @@ final public static function get_instance() {
public function __construct() {
add_filter( 'a8c_show_coming_soon_page', array( $this, 'should_show_a8c_coming_soon_page' ), PHP_INT_MAX, 1 );
add_filter( 'woocommerce_coming_soon_exclude', array( $this, 'should_exclude_lys_coming_soon' ) );
add_action( 'update_option_wpcom_public_coming_soon', array( $this, 'sync_coming_soon_option' ), 10, 3 );
add_action( 'update_option_wpcom_public_coming_soon', array( $this, 'sync_coming_soon_wpcom_to_lys' ), 10, 3 );
$this->hook_update_option_woocommerce_coming_soon();
}

/**
* Hook on woocommerce_coming_soon option update for ease of use.
*/
public function hook_update_option_woocommerce_coming_soon() {
add_action( 'update_option_woocommerce_coming_soon' , array( $this, 'sync_coming_soon_lys_to_wpcom' ), 10, 3 );
}

/**
* Unhook on woocommerce_coming_soon option update for ease of use.
*/
public function unhook_update_option_woocommerce_coming_soon() {
remove_action( 'update_option_woocommerce_coming_soon' , array( $this, 'sync_coming_soon_lys_to_wpcom' ), 10, 3 );
}

/**
Expand Down Expand Up @@ -80,19 +95,88 @@ public function should_exclude_lys_coming_soon( $exclude ) {
*
* @param int $old_value
* @param int $new_value
* @param string $option
* @return void
*/
public function sync_coming_soon_option( $old_value, $new_value, $option ) {
public function sync_coming_soon_wpcom_to_lys( $old_value, $new_value ) {
if ( ! class_exists( '\Automattic\WooCommerce\Admin\Features\Features' ) || ! \Automattic\WooCommerce\Admin\Features\Features::is_enabled( 'launch-your-store' ) ) {
return;
}

if ( 1 === (int) $new_value ) {
$woocommerce_coming_soon = get_option( 'woocommerce_coming_soon', false );
$is_coming_soon_wpcom = 1 === (int) $new_value;

// Value is already set, we don't need to update option.
if ( ( 'yes' === $woocommerce_coming_soon && $is_coming_soon_wpcom ) ||
( 'no' === $woocommerce_coming_soon && ! $is_coming_soon_wpcom ) ) {
return;
}

// Unhook listener to prevent a loop of updating option between WPCOM <-> LYS.
$this->unhook_update_option_woocommerce_coming_soon();

if ( $is_coming_soon_wpcom ) {
update_option( 'woocommerce_coming_soon', 'yes' );
} else {
update_option( 'woocommerce_coming_soon', 'no' );
}

$this->hook_update_option_woocommerce_coming_soon();
}

/**
* Sync the coming soon option from to woocommerce_coming_soon wpcom.
* Does not include a check of existing wpcom option values since
* there could be multiple options that are affected.
*
* @param int $old_value
* @param int $new_value
* @return void
*/
public function sync_coming_soon_lys_to_wpcom( $old_value, $new_value ) {
$is_atomic_launched = 'launched' === get_option( 'launch-status' );
$response = false;

if ( 'no' === $new_value ) {
if ( ! $is_atomic_launched ) {
$response = WC_Calypso_Bridge_Atomic_Launch_API::launch_site();
} else {
$response = WC_Calypso_Bridge_Atomic_Launch_API::update_coming_soon( false );
}
} elseif ( $is_atomic_launched && 'yes' === $new_value ) {
$response = WC_Calypso_Bridge_Atomic_Launch_API::update_coming_soon( true );
}

if ( $response && 200 !== wp_remote_retrieve_response_code( $response ) ) {
$body = wp_remote_retrieve_body( $response );
$error = json_decode( $body, true );

if ( isset( $error['message'] ) ) {
$this->add_admin_notice( $error[ 'message' ], 'error' );
} else {
$this->add_admin_notice( __( 'There was a problem trying to update site visibility.', 'wc-calypso-bridge' ), 'error' );
}
}
}

public function add_admin_notice( $message, $notice_type = 'info' ) {
add_action( 'admin_notices', function () use ( $message, $notice_type ) {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : '';

if ( 'woocommerce_page_wc-settings' !== $screen_id ) {
return;
}

if ( ! isset( $_GET['tab'] ) || 'site-visibility' !== $_GET['tab'] ) {
return;
}

?>
<div class="notice notice-<?php echo $notice_type ?>">
<p><?php echo $message; ?></p>
</div>
<?php
} );
}
}

Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This section describes how to install the plugin and get it working.
* Hide WPCOM's coming soon page when the launch-your-store feature flag is enabled #1500
* Exclude LYS coming soon page for WPCOM share link #1501
* Sync WPCOM coming soon status to LYS #1502
* Add sync coming soon status from LYS to WPCOM #1503

= 2.5.5 =
* Add a new class to customize for Stripe from Partner Aware Onboarding #1492
Expand Down

0 comments on commit 1c78762

Please sign in to comment.