Blueprint
4 days ago
Fulfillments
1 month ago
MarketingRecommendations
3 months ago
Navigation
1 year ago
OnboardingTasks
4 days ago
PaymentGatewaySuggestions
1 year ago
ProductBlockEditor
4 days ago
ProductDataViews
4 days ago
ShippingPartnerSuggestions
2 months ago
Features.php
1 month ago
LaunchYourStore.php
4 days ago
Onboarding.php
1 year ago
ProductVariationsClassicRedesign.php
1 month ago
TransientNotices.php
3 years ago
LaunchYourStore.php
269 lines
| 1 | <?php declare( strict_types = 1 ); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Admin\Features; |
| 4 | |
| 5 | use Automattic\WooCommerce\Admin\WCAdminHelper; |
| 6 | use Automattic\WooCommerce\Internal\Admin\WCAdminUser; |
| 7 | |
| 8 | |
| 9 | /** |
| 10 | * Takes care of Launch Your Store related actions. |
| 11 | */ |
| 12 | class LaunchYourStore { |
| 13 | const BANNER_DISMISS_USER_META_KEY = 'coming_soon_banner_dismissed'; |
| 14 | /** |
| 15 | * Constructor. |
| 16 | */ |
| 17 | public function __construct() { |
| 18 | add_action( 'woocommerce_update_options_site-visibility', array( $this, 'save_site_visibility_options' ) ); |
| 19 | add_filter( 'woocommerce_admin_shared_settings', array( $this, 'preload_settings' ) ); |
| 20 | add_action( 'wp_footer', array( $this, 'maybe_add_coming_soon_banner_on_frontend' ) ); |
| 21 | add_action( 'init', array( $this, 'register_launch_your_store_user_meta_fields' ) ); |
| 22 | add_filter( 'woocommerce_tracks_event_properties', array( $this, 'append_coming_soon_global_tracks' ), 10, 2 ); |
| 23 | add_action( 'wp_login', array( $this, 'reset_woocommerce_coming_soon_banner_dismissed' ), 10, 2 ); |
| 24 | add_filter( 'woocommerce_admin_get_user_data_fields', array( $this, 'add_user_data_fields' ) ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Save values submitted from WooCommerce -> Settings -> General. |
| 29 | * |
| 30 | * @return void |
| 31 | */ |
| 32 | public function save_site_visibility_options() { |
| 33 | $nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : ''; |
| 34 | if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'woocommerce-settings' ) ) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | // options to allowed update and their allowed values. |
| 39 | $options = array( |
| 40 | 'woocommerce_coming_soon' => array( 'yes', 'no' ), |
| 41 | 'woocommerce_store_pages_only' => array( 'yes', 'no' ), |
| 42 | 'woocommerce_private_link' => array( 'yes', 'no' ), |
| 43 | 'woocommerce_feature_site_visibility_badge_enabled' => array( 'yes', 'no' ), |
| 44 | ); |
| 45 | |
| 46 | $event_data = array(); |
| 47 | |
| 48 | foreach ( $options as $name => $allowed_values ) { |
| 49 | $current_value = get_option( $name, 'not set' ); |
| 50 | $new_value = $current_value; |
| 51 | |
| 52 | if ( isset( $_POST[ $name ] ) ) { |
| 53 | $input_value = sanitize_text_field( wp_unslash( $_POST[ $name ] ) ); |
| 54 | |
| 55 | // no-op if input value is invalid. |
| 56 | if ( in_array( $input_value, $allowed_values, true ) ) { |
| 57 | update_option( $name, $input_value ); |
| 58 | $new_value = $input_value; |
| 59 | |
| 60 | // log the transition if there is one. |
| 61 | if ( $current_value !== $new_value ) { |
| 62 | $enabled_or_disabled = 'yes' === $new_value ? 'enabled' : 'disabled'; |
| 63 | $event_data[ $name . '_toggled' ] = $enabled_or_disabled; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | $event_data[ $name ] = $new_value; |
| 68 | } |
| 69 | wc_admin_record_tracks_event( 'site_visibility_saved', $event_data ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Append coming soon prop tracks globally. |
| 74 | * |
| 75 | * @param array $event_properties Event properties array. |
| 76 | * |
| 77 | * @return array |
| 78 | */ |
| 79 | public function append_coming_soon_global_tracks( $event_properties ) { |
| 80 | if ( is_array( $event_properties ) ) { |
| 81 | $coming_soon = 'no'; |
| 82 | if ( 'yes' === get_option( 'woocommerce_coming_soon', 'no' ) ) { |
| 83 | if ( 'yes' === get_option( 'woocommerce_store_pages_only', 'no' ) ) { |
| 84 | $coming_soon = 'store'; |
| 85 | } else { |
| 86 | $coming_soon = 'site'; |
| 87 | } |
| 88 | } |
| 89 | $event_properties['coming_soon'] = $coming_soon; |
| 90 | } |
| 91 | return $event_properties; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /** |
| 96 | * Preload settings for Site Visibility. |
| 97 | * |
| 98 | * @param array $settings settings array. |
| 99 | * |
| 100 | * @return mixed |
| 101 | */ |
| 102 | public function preload_settings( $settings ) { |
| 103 | if ( ! is_admin() ) { |
| 104 | return $settings; |
| 105 | } |
| 106 | |
| 107 | $current_screen = get_current_screen(); |
| 108 | $is_setting_page = $current_screen && 'woocommerce_page_wc-settings' === $current_screen->id; |
| 109 | |
| 110 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 111 | $is_woopayments_connect = isset( $_GET['path'] ) && |
| 112 | isset( $_GET['page'] ) && |
| 113 | ( '/payments/connect' === sanitize_text_field( wp_unslash( $_GET['path'] ) ) || '/payments/onboarding' === sanitize_text_field( wp_unslash( $_GET['path'] ) ) ) && |
| 114 | 'wc-admin' === $_GET['page']; |
| 115 | // phpcs:enable |
| 116 | |
| 117 | if ( $is_setting_page || $is_woopayments_connect ) { |
| 118 | // Regnerate the share key if it's not set. |
| 119 | add_option( 'woocommerce_share_key', wp_generate_password( 32, false ) ); |
| 120 | |
| 121 | $settings['siteVisibilitySettings'] = array( |
| 122 | 'shop_permalink' => get_permalink( wc_get_page_id( 'shop' ) ), |
| 123 | 'woocommerce_coming_soon' => get_option( 'woocommerce_coming_soon' ), |
| 124 | 'woocommerce_store_pages_only' => get_option( 'woocommerce_store_pages_only' ), |
| 125 | 'woocommerce_private_link' => get_option( 'woocommerce_private_link' ), |
| 126 | 'woocommerce_share_key' => get_option( 'woocommerce_share_key' ), |
| 127 | 'woocommerce_feature_site_visibility_badge_enabled' => get_option( 'woocommerce_feature_site_visibility_badge_enabled', 'yes' ), |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | return $settings; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * User must be an admin or editor. |
| 136 | * |
| 137 | * @return bool |
| 138 | */ |
| 139 | private function is_manager_or_admin() { |
| 140 | // phpcs:ignore |
| 141 | if ( ! current_user_can( 'shop_manager' ) && ! current_user_can( 'administrator' ) ) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Add 'coming soon' banner on the frontend when the following conditions met. |
| 150 | * |
| 151 | * - User must be either an admin or store editor (must be logged in). |
| 152 | * - 'woocommerce_coming_soon' option value must be 'yes' |
| 153 | * - The page must not be the Coming soon page itself. |
| 154 | */ |
| 155 | public function maybe_add_coming_soon_banner_on_frontend() { |
| 156 | // Do not show the banner if the site is being previewed. |
| 157 | if ( isset( $_GET['site-preview'] ) ) { // @phpcs:ignore |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | $current_user_id = get_current_user_id(); |
| 162 | if ( ! $current_user_id ) { |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | $has_dismissed_banner = WCAdminUser::get_user_data_field( $current_user_id, self::BANNER_DISMISS_USER_META_KEY ) |
| 167 | // Remove this check in WC 9.4. |
| 168 | || get_user_meta( $current_user_id, 'woocommerce_' . self::BANNER_DISMISS_USER_META_KEY, true ) === 'yes'; |
| 169 | if ( $has_dismissed_banner ) { |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | if ( ! $this->is_manager_or_admin() ) { |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | // 'woocommerce_coming_soon' must be 'yes' |
| 178 | if ( get_option( 'woocommerce_coming_soon', 'no' ) !== 'yes' ) { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | $store_pages_only = get_option( 'woocommerce_store_pages_only' ) === 'yes'; |
| 183 | if ( $store_pages_only && ! WCAdminHelper::is_current_page_store_page() ) { |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | $link = admin_url( 'admin.php?page=wc-settings&tab=site-visibility' ); |
| 188 | $rest_url = rest_url( 'wp/v2/users/' . $current_user_id ); |
| 189 | $rest_nonce = wp_create_nonce( 'wp_rest' ); |
| 190 | |
| 191 | $text = sprintf( |
| 192 | // translators: no need to translate it. It's a link. |
| 193 | __( |
| 194 | " |
| 195 | This page is in \"Coming soon\" mode and is only visible to you and those who have permission. To make it public to everyone, <a href='%s'>change visibility settings</a>. |
| 196 | ", |
| 197 | 'woocommerce' |
| 198 | ), |
| 199 | $link |
| 200 | ); |
| 201 | // phpcs:ignore |
| 202 | echo "<div id='coming-soon-footer-banner'><div class='coming-soon-footer-banner__content'>$text</div><a class='coming-soon-footer-banner-dismiss' data-rest-url='$rest_url' data-rest-nonce='$rest_nonce'></a></div>"; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Register user meta fields for Launch Your Store. |
| 207 | * |
| 208 | * This should be removed in WC 9.4. |
| 209 | */ |
| 210 | public function register_launch_your_store_user_meta_fields() { |
| 211 | if ( ! $this->is_manager_or_admin() ) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | register_meta( |
| 216 | 'user', |
| 217 | 'woocommerce_launch_your_store_tour_hidden', |
| 218 | array( |
| 219 | 'type' => 'string', |
| 220 | 'description' => 'Indicate whether the user has dismissed the site visibility tour on the home screen.', |
| 221 | 'single' => true, |
| 222 | 'show_in_rest' => true, |
| 223 | ) |
| 224 | ); |
| 225 | |
| 226 | register_meta( |
| 227 | 'user', |
| 228 | 'woocommerce_coming_soon_banner_dismissed', |
| 229 | array( |
| 230 | 'type' => 'string', |
| 231 | 'description' => 'Indicate whether the user has dismissed the coming soon notice or not.', |
| 232 | 'single' => true, |
| 233 | 'show_in_rest' => true, |
| 234 | ) |
| 235 | ); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Register user meta fields for Launch Your Store. |
| 240 | * |
| 241 | * @param array $user_data_fields user data fields. |
| 242 | * @return array |
| 243 | */ |
| 244 | public function add_user_data_fields( $user_data_fields ) { |
| 245 | return array_merge( |
| 246 | $user_data_fields, |
| 247 | array( |
| 248 | 'launch_your_store_tour_hidden', |
| 249 | self::BANNER_DISMISS_USER_META_KEY, |
| 250 | ) |
| 251 | ); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Reset 'woocommerce_coming_soon_banner_dismissed' user meta to 'no'. |
| 256 | * |
| 257 | * Runs when a user logs-in successfully. |
| 258 | * |
| 259 | * @param string $user_login user login. |
| 260 | * @param object $user user object. |
| 261 | */ |
| 262 | public function reset_woocommerce_coming_soon_banner_dismissed( $user_login, $user ) { |
| 263 | $existing_meta = WCAdminUser::get_user_data_field( $user->ID, self::BANNER_DISMISS_USER_META_KEY ); |
| 264 | if ( 'yes' === $existing_meta ) { |
| 265 | WCAdminUser::update_user_data_field( $user->ID, self::BANNER_DISMISS_USER_META_KEY, 'no' ); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 |