Add dismiss button to coming soon banner (#46149)

* Add coming_soon page as a store page

* Implement coming soon banner dismiss

* Added a new REST for Launch Your Store with a dismiss endpoint

* Updated coming soon banner logic to check for woocommerce_coming_soon_banner_dismissed

* Lint fixes

* Add changefile(s) from automation for the following project(s): woocommerce

* Update plugins/woocommerce/src/Admin/API/LaunchYourStore.php

Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com>

* Update plugins/woocommerce/src/Admin/API/LaunchYourStore.php

Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com>

* Update plugins/woocommerce/src/Admin/API/LaunchYourStore.php

Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com>

* Use user meta to save woocommerce_coming_soon_banner_dismissed

* Lint fixes

* Use the built-in rest api to manage woocommerce_coming_soon_banner_dismissed user meta

* Reset woocommerce_coming_soon_banner_dismissed on login

* Fix lint issues and add back wp_login hook to reset banner dismiss user meta

* Use constant for user meta field

* Update woocommerce_coming_soon_banner_dismissed user meta when X button is clicked

* Restrict user meta to shop manager or admin

* Fix lint issue

* Fix lint issue

---------

Co-authored-by: github-actions <github-actions@github.com>
Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com>
This commit is contained in:
Moon 2024-04-17 12:26:42 +12:00 committed by GitHub
parent ec80c8d641
commit 87868097d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 108 additions and 17 deletions

View File

@ -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.

View File

@ -2457,4 +2457,13 @@ body:not(.search-results) .twentysixteen .entry-summary {
color: #3858E9; color: #3858E9;
text-decoration: none; text-decoration: none;
} }
a.coming-soon-footer-banner-dismiss {
background-image: url('data:image/svg+xml,<svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M12.4995 13.0602L16.2118 16.7725L17.2725 15.7118L13.5602 11.9995L17.2725 8.28723L16.2119 7.22657L12.4995 10.9389L8.78722 7.22656L7.72656 8.28722L11.4389 11.9995L7.72657 15.7119L8.78723 16.7725L12.4995 13.0602Z" fill="%23757575"/></svg>');
width: 24px;
height: 24px;
cursor: pointer;
position: absolute;
right: 20px;
}
} }

View File

@ -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();
}
} );
} );
}); });

View File

@ -51,7 +51,6 @@ class Init {
add_filter( 'woocommerce_rest_prepare_shop_order_object', array( __CLASS__, 'add_currency_symbol_to_order_response' ) ); 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'; 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. // The performance indicators controller must be registered last, after other /stats endpoints have been registered.
$analytics_controllers[] = 'Automattic\WooCommerce\Admin\API\Reports\PerformanceIndicators\Controller'; $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 );
} }
/** /**

View File

@ -4,21 +4,22 @@ namespace Automattic\WooCommerce\Admin\Features;
use Automattic\WooCommerce\Admin\PageController; use Automattic\WooCommerce\Admin\PageController;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils; use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
use Automattic\WooCommerce\Admin\WCAdminHelper;
/** /**
* Takes care of Launch Your Store related actions. * Takes care of Launch Your Store related actions.
*/ */
class LaunchYourStore { class LaunchYourStore {
const BANNER_DISMISS_USER_META_KEY = 'woocommerce_coming_soon_banner_dismissed';
/** /**
* Constructor. * Constructor.
*/ */
public function __construct() { public function __construct() {
add_action( 'woocommerce_update_options_site-visibility', array( $this, 'save_site_visibility_options' ) ); 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( '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( '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; 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. * Add 'coming soon' banner on the frontend when the following conditions met.
* *
@ -94,9 +109,16 @@ class LaunchYourStore {
return false; return false;
} }
// User must be an admin or editor. $current_user_id = get_current_user_id();
// phpcs:ignore if ( ! $current_user_id ) {
if ( ! current_user_can( 'shop_manager' ) && ! current_user_can( 'administrator' ) ) { 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; return false;
} }
@ -105,26 +127,37 @@ class LaunchYourStore {
return false; 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( $text = sprintf(
// translators: no need to translate it. It's a link. // 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,&nbsp;<a href='%s'>change visibility settings</a>. This page is in \"Coming soon\" mode and is only visible to you and those who have permission. To make it public to everyone,&nbsp;<a href='%s'>change visibility settings</a>
", ",
'woocommerce' 'woocommerce'
), ),
$link $link
); );
// phpcs:ignore // phpcs:ignore
echo "<div id='coming-soon-footer-banner'>$text</div>"; echo "<div id='coming-soon-footer-banner'>$text<a class='coming-soon-footer-banner-dismiss' data-rest-url='$rest_url' data-rest-nonce='$rest_nonce'></a></div>";
} }
/** /**
* Register user meta fields for Launch Your Store. * Register user meta fields for Launch Your Store.
*/ */
public function register_launch_your_store_user_meta_fields() { public function register_launch_your_store_user_meta_fields() {
if ( ! $this->is_manager_or_admin() ) {
return;
}
register_meta( register_meta(
'user', 'user',
'woocommerce_launch_your_store_tour_hidden', 'woocommerce_launch_your_store_tour_hidden',
@ -135,5 +168,31 @@ class LaunchYourStore {
'show_in_rest' => true, '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' );
}
} }
} }

View File

@ -152,11 +152,12 @@ class WCAdminHelper {
// WC store pages. // WC store pages.
$store_pages = array( $store_pages = array(
'shop' => wc_get_page_id( 'shop' ), 'shop' => wc_get_page_id( 'shop' ),
'cart' => wc_get_page_id( 'cart' ), 'cart' => wc_get_page_id( 'cart' ),
'checkout' => wc_get_page_id( 'checkout' ), 'checkout' => wc_get_page_id( 'checkout' ),
'privacy' => wc_privacy_policy_page_id(), 'privacy' => wc_privacy_policy_page_id(),
'terms' => wc_terms_and_conditions_page_id(), 'terms' => wc_terms_and_conditions_page_id(),
'coming_soon' => wc_get_page_id( 'coming_soon' ),
); );
/** /**