diff --git a/plugins/woocommerce/changelog/46149-update-add-dismiss-button-for-coming-soon-banner b/plugins/woocommerce/changelog/46149-update-add-dismiss-button-for-coming-soon-banner new file mode 100644 index 00000000000..c70af96f352 --- /dev/null +++ b/plugins/woocommerce/changelog/46149-update-add-dismiss-button-for-coming-soon-banner @@ -0,0 +1,4 @@ +Significance: patch +Type: update + +Implement dismiss button for the coming soon banner and restrict the banner on the store pages only when 'Restrict to store pages only' option is selected. \ No newline at end of file diff --git a/plugins/woocommerce/client/legacy/css/woocommerce.scss b/plugins/woocommerce/client/legacy/css/woocommerce.scss index ca5bcc51375..082932acad5 100644 --- a/plugins/woocommerce/client/legacy/css/woocommerce.scss +++ b/plugins/woocommerce/client/legacy/css/woocommerce.scss @@ -2457,4 +2457,13 @@ body:not(.search-results) .twentysixteen .entry-summary { color: #3858E9; text-decoration: none; } + + a.coming-soon-footer-banner-dismiss { + background-image: url('data:image/svg+xml,'); + width: 24px; + height: 24px; + cursor: pointer; + position: absolute; + right: 20px; + } } diff --git a/plugins/woocommerce/client/legacy/js/frontend/woocommerce.js b/plugins/woocommerce/client/legacy/js/frontend/woocommerce.js index 734d73cced2..c3190ac687b 100644 --- a/plugins/woocommerce/client/legacy/js/frontend/woocommerce.js +++ b/plugins/woocommerce/client/legacy/js/frontend/woocommerce.js @@ -99,4 +99,24 @@ jQuery( function( $ ) { } } ); + + + $( 'a.coming-soon-footer-banner-dismiss' ).on( 'click', function( e ) { + var target = $( e.target ); + $.ajax( { + type: 'post', + url: target.data( 'rest-url' ), + data: { + meta: { + 'woocommerce_coming_soon_banner_dismissed': 'yes' + } + }, + beforeSend: function ( xhr ) { + xhr.setRequestHeader( 'X-WP-Nonce', target.data( 'rest-nonce' ) ); + }, + complete: function () { + $('#coming-soon-footer-banner').hide(); + } + } ); + } ); }); diff --git a/plugins/woocommerce/src/Admin/API/Init.php b/plugins/woocommerce/src/Admin/API/Init.php index 4464b44722a..407522e19bf 100644 --- a/plugins/woocommerce/src/Admin/API/Init.php +++ b/plugins/woocommerce/src/Admin/API/Init.php @@ -51,7 +51,6 @@ class Init { add_filter( 'woocommerce_rest_prepare_shop_order_object', array( __CLASS__, 'add_currency_symbol_to_order_response' ) ); include_once WC_ABSPATH . 'includes/admin/class-wc-admin-upload-downloadable-product.php'; - } /** @@ -134,8 +133,7 @@ class Init { // The performance indicators controller must be registered last, after other /stats endpoints have been registered. $analytics_controllers[] = 'Automattic\WooCommerce\Admin\API\Reports\PerformanceIndicators\Controller'; - - $controllers = array_merge( $controllers, $analytics_controllers, $product_form_controllers ); + $controllers = array_merge( $controllers, $analytics_controllers, $product_form_controllers ); } /** diff --git a/plugins/woocommerce/src/Admin/Features/LaunchYourStore.php b/plugins/woocommerce/src/Admin/Features/LaunchYourStore.php index 0fcc361c71e..5d3bb3d356a 100644 --- a/plugins/woocommerce/src/Admin/Features/LaunchYourStore.php +++ b/plugins/woocommerce/src/Admin/Features/LaunchYourStore.php @@ -4,21 +4,22 @@ namespace Automattic\WooCommerce\Admin\Features; use Automattic\WooCommerce\Admin\PageController; use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils; +use Automattic\WooCommerce\Admin\WCAdminHelper; /** * Takes care of Launch Your Store related actions. */ class LaunchYourStore { + const BANNER_DISMISS_USER_META_KEY = 'woocommerce_coming_soon_banner_dismissed'; /** * Constructor. */ public function __construct() { add_action( 'woocommerce_update_options_site-visibility', array( $this, 'save_site_visibility_options' ) ); - if ( is_admin() ) { - add_filter( 'woocommerce_admin_shared_settings', array( $this, 'preload_settings' ) ); - } + add_filter( 'woocommerce_admin_shared_settings', array( $this, 'preload_settings' ) ); add_action( 'wp_footer', array( $this, 'maybe_add_coming_soon_banner_on_frontend' ) ); add_action( 'init', array( $this, 'register_launch_your_store_user_meta_fields' ) ); + add_action( 'wp_login', array( $this, 'reset_woocommerce_coming_soon_banner_dismissed' ), 10, 2 ); } /** @@ -81,6 +82,20 @@ class LaunchYourStore { return $settings; } + /** + * User must be an admin or editor. + * + * @return bool + */ + private function is_manager_or_admin() { + // phpcs:ignore + if ( ! current_user_can( 'shop_manager' ) && ! current_user_can( 'administrator' ) ) { + return false; + } + + return true; + } + /** * Add 'coming soon' banner on the frontend when the following conditions met. * @@ -94,9 +109,16 @@ class LaunchYourStore { return false; } - // User must be an admin or editor. - // phpcs:ignore - if ( ! current_user_can( 'shop_manager' ) && ! current_user_can( 'administrator' ) ) { + $current_user_id = get_current_user_id(); + if ( ! $current_user_id ) { + return false; + } + + if ( get_user_meta( $current_user_id, self::BANNER_DISMISS_USER_META_KEY, true ) === 'yes' ) { + return false; + } + + if ( ! $this->is_manager_or_admin() ) { return false; } @@ -105,26 +127,37 @@ class LaunchYourStore { return false; } - $link = admin_url( 'admin.php?page=wc-settings#wc_settings_general_site_visibility_slotfill' ); + $store_pages_only = get_option( 'woocommerce_store_pages_only' ) === 'yes'; + if ( $store_pages_only && ! WCAdminHelper::is_store_page() ) { + return false; + } + + $link = admin_url( 'admin.php?page=wc-settings&tab=site-visibility' ); + $rest_url = rest_url( 'wp/v2/users/' . $current_user_id ); + $rest_nonce = wp_create_nonce( 'wp_rest' ); $text = sprintf( // translators: no need to translate it. It's a link. __( " - This page is in \"Coming soon\" mode and is only visible to you and those who have permission. To make it public to everyone, change visibility settings. + This page is in \"Coming soon\" mode and is only visible to you and those who have permission. To make it public to everyone, change visibility settings ", 'woocommerce' ), $link ); // phpcs:ignore - echo "
"; + echo " "; } /** * Register user meta fields for Launch Your Store. */ public function register_launch_your_store_user_meta_fields() { + if ( ! $this->is_manager_or_admin() ) { + return; + } + register_meta( 'user', 'woocommerce_launch_your_store_tour_hidden', @@ -135,5 +168,31 @@ class LaunchYourStore { 'show_in_rest' => true, ) ); + + register_meta( + 'user', + self::BANNER_DISMISS_USER_META_KEY, + array( + 'type' => 'string', + 'description' => 'Indicate wheter user has dismissed coming soon notice or not', + 'single' => true, + 'show_in_rest' => true, + ) + ); + } + + /** + * Reset 'woocommerce_coming_soon_banner_dismissed' user meta to 'no'. + * + * Runs when a user logs-in successfully. + * + * @param string $user_login user login. + * @param object $user user object. + */ + public function reset_woocommerce_coming_soon_banner_dismissed( $user_login, $user ) { + $existing_meta = get_user_meta( $user->id, self::BANNER_DISMISS_USER_META_KEY, true ); + if ( 'yes' === $existing_meta ) { + update_user_meta( $user->id, self::BANNER_DISMISS_USER_META_KEY, 'no' ); + } } } diff --git a/plugins/woocommerce/src/Admin/WCAdminHelper.php b/plugins/woocommerce/src/Admin/WCAdminHelper.php index 8eff443f0c6..1ca45db2055 100644 --- a/plugins/woocommerce/src/Admin/WCAdminHelper.php +++ b/plugins/woocommerce/src/Admin/WCAdminHelper.php @@ -152,11 +152,12 @@ class WCAdminHelper { // WC store pages. $store_pages = array( - 'shop' => wc_get_page_id( 'shop' ), - 'cart' => wc_get_page_id( 'cart' ), - 'checkout' => wc_get_page_id( 'checkout' ), - 'privacy' => wc_privacy_policy_page_id(), - 'terms' => wc_terms_and_conditions_page_id(), + 'shop' => wc_get_page_id( 'shop' ), + 'cart' => wc_get_page_id( 'cart' ), + 'checkout' => wc_get_page_id( 'checkout' ), + 'privacy' => wc_privacy_policy_page_id(), + 'terms' => wc_terms_and_conditions_page_id(), + 'coming_soon' => wc_get_page_id( 'coming_soon' ), ); /**