AsyncProductEditorCategoryField
3 years ago
Blueprint
11 months ago
Fulfillments
4 weeks ago
MarketingRecommendations
3 months ago
Navigation
1 year ago
OnboardingTasks
4 weeks ago
PaymentGatewaySuggestions
1 year ago
ProductBlockEditor
4 weeks ago
ProductDataViews
4 weeks ago
ShippingPartnerSuggestions
1 month ago
Features.php
4 weeks ago
LaunchYourStore.php
4 weeks ago
Onboarding.php
1 year ago
ProductVariationsClassicRedesign.php
4 weeks ago
TransientNotices.php
3 years ago
LaunchYourStore.php
358 lines
| 1 | <?php declare( strict_types = 1 ); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Admin\Features; |
| 4 | |
| 5 | use Automattic\WooCommerce\Admin\PluginsHelper; |
| 6 | use Automattic\WooCommerce\Admin\WCAdminHelper; |
| 7 | use Automattic\WooCommerce\Internal\Admin\WCAdminUser; |
| 8 | |
| 9 | |
| 10 | /** |
| 11 | * Takes care of Launch Your Store related actions. |
| 12 | */ |
| 13 | class LaunchYourStore { |
| 14 | const BANNER_DISMISS_USER_META_KEY = 'coming_soon_banner_dismissed'; |
| 15 | /** |
| 16 | * Constructor. |
| 17 | */ |
| 18 | public function __construct() { |
| 19 | add_action( 'woocommerce_update_options_site-visibility', array( $this, 'save_site_visibility_options' ) ); |
| 20 | add_filter( 'woocommerce_admin_shared_settings', array( $this, 'preload_settings' ) ); |
| 21 | add_action( 'wp_footer', array( $this, 'maybe_add_coming_soon_banner_on_frontend' ) ); |
| 22 | add_action( 'init', array( $this, 'register_launch_your_store_user_meta_fields' ) ); |
| 23 | add_filter( 'woocommerce_tracks_event_properties', array( $this, 'append_coming_soon_global_tracks' ), 10, 2 ); |
| 24 | add_action( 'wp_login', array( $this, 'reset_woocommerce_coming_soon_banner_dismissed' ), 10, 2 ); |
| 25 | add_filter( 'woocommerce_admin_get_user_data_fields', array( $this, 'add_user_data_fields' ) ); |
| 26 | if ( Features::is_enabled( 'coming-soon-newsletter-template' ) ) { |
| 27 | add_action( 'admin_enqueue_scripts', array( $this, 'load_newsletter_scripts' ) ); |
| 28 | add_action( 'save_post_wp_template', array( $this, 'maybe_track_template_change' ), 10, 3 ); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Save values submitted from WooCommerce -> Settings -> General. |
| 34 | * |
| 35 | * @return void |
| 36 | */ |
| 37 | public function save_site_visibility_options() { |
| 38 | $nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : ''; |
| 39 | if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'woocommerce-settings' ) ) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | // options to allowed update and their allowed values. |
| 44 | $options = array( |
| 45 | 'woocommerce_coming_soon' => array( 'yes', 'no' ), |
| 46 | 'woocommerce_store_pages_only' => array( 'yes', 'no' ), |
| 47 | 'woocommerce_private_link' => array( 'yes', 'no' ), |
| 48 | ); |
| 49 | |
| 50 | $event_data = array(); |
| 51 | |
| 52 | foreach ( $options as $name => $allowed_values ) { |
| 53 | $current_value = get_option( $name, 'not set' ); |
| 54 | $new_value = $current_value; |
| 55 | |
| 56 | if ( isset( $_POST[ $name ] ) ) { |
| 57 | $input_value = sanitize_text_field( wp_unslash( $_POST[ $name ] ) ); |
| 58 | |
| 59 | // no-op if input value is invalid. |
| 60 | if ( in_array( $input_value, $allowed_values, true ) ) { |
| 61 | update_option( $name, $input_value ); |
| 62 | $new_value = $input_value; |
| 63 | |
| 64 | // log the transition if there is one. |
| 65 | if ( $current_value !== $new_value ) { |
| 66 | $enabled_or_disabled = 'yes' === $new_value ? 'enabled' : 'disabled'; |
| 67 | $event_data[ $name . '_toggled' ] = $enabled_or_disabled; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | $event_data[ $name ] = $new_value; |
| 72 | } |
| 73 | wc_admin_record_tracks_event( 'site_visibility_saved', $event_data ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Append coming soon prop tracks globally. |
| 78 | * |
| 79 | * @param array $event_properties Event properties array. |
| 80 | * |
| 81 | * @return array |
| 82 | */ |
| 83 | public function append_coming_soon_global_tracks( $event_properties ) { |
| 84 | if ( is_array( $event_properties ) ) { |
| 85 | $coming_soon = 'no'; |
| 86 | if ( 'yes' === get_option( 'woocommerce_coming_soon', 'no' ) ) { |
| 87 | if ( 'yes' === get_option( 'woocommerce_store_pages_only', 'no' ) ) { |
| 88 | $coming_soon = 'store'; |
| 89 | } else { |
| 90 | $coming_soon = 'site'; |
| 91 | } |
| 92 | } |
| 93 | $event_properties['coming_soon'] = $coming_soon; |
| 94 | } |
| 95 | return $event_properties; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /** |
| 100 | * Preload settings for Site Visibility. |
| 101 | * |
| 102 | * @param array $settings settings array. |
| 103 | * |
| 104 | * @return mixed |
| 105 | */ |
| 106 | public function preload_settings( $settings ) { |
| 107 | if ( ! is_admin() ) { |
| 108 | return $settings; |
| 109 | } |
| 110 | |
| 111 | $current_screen = get_current_screen(); |
| 112 | $is_setting_page = $current_screen && 'woocommerce_page_wc-settings' === $current_screen->id; |
| 113 | |
| 114 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 115 | $is_woopayments_connect = isset( $_GET['path'] ) && |
| 116 | isset( $_GET['page'] ) && |
| 117 | ( '/payments/connect' === sanitize_text_field( wp_unslash( $_GET['path'] ) ) || '/payments/onboarding' === sanitize_text_field( wp_unslash( $_GET['path'] ) ) ) && |
| 118 | 'wc-admin' === $_GET['page']; |
| 119 | // phpcs:enable |
| 120 | |
| 121 | if ( $is_setting_page || $is_woopayments_connect ) { |
| 122 | // Regnerate the share key if it's not set. |
| 123 | add_option( 'woocommerce_share_key', wp_generate_password( 32, false ) ); |
| 124 | |
| 125 | $settings['siteVisibilitySettings'] = array( |
| 126 | 'shop_permalink' => get_permalink( wc_get_page_id( 'shop' ) ), |
| 127 | 'woocommerce_coming_soon' => get_option( 'woocommerce_coming_soon' ), |
| 128 | 'woocommerce_store_pages_only' => get_option( 'woocommerce_store_pages_only' ), |
| 129 | 'woocommerce_private_link' => get_option( 'woocommerce_private_link' ), |
| 130 | 'woocommerce_share_key' => get_option( 'woocommerce_share_key' ), |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | return $settings; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * User must be an admin or editor. |
| 139 | * |
| 140 | * @return bool |
| 141 | */ |
| 142 | private function is_manager_or_admin() { |
| 143 | // phpcs:ignore |
| 144 | if ( ! current_user_can( 'shop_manager' ) && ! current_user_can( 'administrator' ) ) { |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Add 'coming soon' banner on the frontend when the following conditions met. |
| 153 | * |
| 154 | * - User must be either an admin or store editor (must be logged in). |
| 155 | * - 'woocommerce_coming_soon' option value must be 'yes' |
| 156 | * - The page must not be the Coming soon page itself. |
| 157 | */ |
| 158 | public function maybe_add_coming_soon_banner_on_frontend() { |
| 159 | // Do not show the banner if the site is being previewed. |
| 160 | if ( isset( $_GET['site-preview'] ) ) { // @phpcs:ignore |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | $current_user_id = get_current_user_id(); |
| 165 | if ( ! $current_user_id ) { |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | $has_dismissed_banner = WCAdminUser::get_user_data_field( $current_user_id, self::BANNER_DISMISS_USER_META_KEY ) |
| 170 | // Remove this check in WC 9.4. |
| 171 | || get_user_meta( $current_user_id, 'woocommerce_' . self::BANNER_DISMISS_USER_META_KEY, true ) === 'yes'; |
| 172 | if ( $has_dismissed_banner ) { |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | if ( ! $this->is_manager_or_admin() ) { |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | // 'woocommerce_coming_soon' must be 'yes' |
| 181 | if ( get_option( 'woocommerce_coming_soon', 'no' ) !== 'yes' ) { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | $store_pages_only = get_option( 'woocommerce_store_pages_only' ) === 'yes'; |
| 186 | if ( $store_pages_only && ! WCAdminHelper::is_current_page_store_page() ) { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | $link = admin_url( 'admin.php?page=wc-settings&tab=site-visibility' ); |
| 191 | $rest_url = rest_url( 'wp/v2/users/' . $current_user_id ); |
| 192 | $rest_nonce = wp_create_nonce( 'wp_rest' ); |
| 193 | |
| 194 | $text = sprintf( |
| 195 | // translators: no need to translate it. It's a link. |
| 196 | __( |
| 197 | " |
| 198 | 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>. |
| 199 | ", |
| 200 | 'woocommerce' |
| 201 | ), |
| 202 | $link |
| 203 | ); |
| 204 | // phpcs:ignore |
| 205 | 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>"; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Register user meta fields for Launch Your Store. |
| 210 | * |
| 211 | * This should be removed in WC 9.4. |
| 212 | */ |
| 213 | public function register_launch_your_store_user_meta_fields() { |
| 214 | if ( ! $this->is_manager_or_admin() ) { |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | register_meta( |
| 219 | 'user', |
| 220 | 'woocommerce_launch_your_store_tour_hidden', |
| 221 | array( |
| 222 | 'type' => 'string', |
| 223 | 'description' => 'Indicate whether the user has dismissed the site visibility tour on the home screen.', |
| 224 | 'single' => true, |
| 225 | 'show_in_rest' => true, |
| 226 | ) |
| 227 | ); |
| 228 | |
| 229 | register_meta( |
| 230 | 'user', |
| 231 | 'woocommerce_coming_soon_banner_dismissed', |
| 232 | array( |
| 233 | 'type' => 'string', |
| 234 | 'description' => 'Indicate whether the user has dismissed the coming soon notice or not.', |
| 235 | 'single' => true, |
| 236 | 'show_in_rest' => true, |
| 237 | ) |
| 238 | ); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Register user meta fields for Launch Your Store. |
| 243 | * |
| 244 | * @param array $user_data_fields user data fields. |
| 245 | * @return array |
| 246 | */ |
| 247 | public function add_user_data_fields( $user_data_fields ) { |
| 248 | return array_merge( |
| 249 | $user_data_fields, |
| 250 | array( |
| 251 | 'launch_your_store_tour_hidden', |
| 252 | self::BANNER_DISMISS_USER_META_KEY, |
| 253 | ) |
| 254 | ); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Reset 'woocommerce_coming_soon_banner_dismissed' user meta to 'no'. |
| 259 | * |
| 260 | * Runs when a user logs-in successfully. |
| 261 | * |
| 262 | * @param string $user_login user login. |
| 263 | * @param object $user user object. |
| 264 | */ |
| 265 | public function reset_woocommerce_coming_soon_banner_dismissed( $user_login, $user ) { |
| 266 | $existing_meta = WCAdminUser::get_user_data_field( $user->ID, self::BANNER_DISMISS_USER_META_KEY ); |
| 267 | if ( 'yes' === $existing_meta ) { |
| 268 | WCAdminUser::update_user_data_field( $user->ID, self::BANNER_DISMISS_USER_META_KEY, 'no' ); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Check if the Mailpoet is connected. |
| 274 | * |
| 275 | * @return bool true if Mailpoet is fully connected, meaning the API key is valid and approved. |
| 276 | */ |
| 277 | private function is_mailpoet_connected() { |
| 278 | if ( ! class_exists( '\MailPoet\DI\ContainerWrapper' ) || ! class_exists( '\MailPoet\Settings\SettingsController' ) ) { |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | $container = \MailPoet\DI\ContainerWrapper::getInstance( WP_DEBUG ); |
| 283 | |
| 284 | // SettingController retrieves data from wp_mailpoet_settings table. |
| 285 | $settings = $container->get( \MailPoet\Settings\SettingsController::class ); |
| 286 | |
| 287 | if ( false === $settings instanceof \MailPoet\Settings\SettingsController ) { |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | $mta = $settings->get( 'mta' ); |
| 292 | $api_state = $mta['mailpoet_api_key_state'] ?? null; |
| 293 | |
| 294 | if ( ! $api_state || ! isset( $api_state['state'], $api_state['code'] ) ) { |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | return 'valid' === $api_state['state'] && 200 === $api_state['code']; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Track when coming soon template is changed. |
| 303 | * |
| 304 | * @param int $post_id The post ID. |
| 305 | * @param WP_Post $post The post object. |
| 306 | * @param bool $update Whether the post is being updated. |
| 307 | */ |
| 308 | public function maybe_track_template_change( $post_id, $post, $update ) { |
| 309 | if ( ! $post instanceof \WP_Post || ! isset( $post->post_name, $post->post_title ) ) { |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | // Check multiple fields to avoid false matches with non-WooCommerce templates. |
| 314 | if ( 'coming-soon' === $post->post_name && 'Page: Coming soon' === $post->post_title ) { |
| 315 | $matches = array(); |
| 316 | $content = $post->post_content; |
| 317 | preg_match( '/"comingSoonPatternId":"([^"]+)"/', $content, $matches ); |
| 318 | |
| 319 | if ( isset( $matches[1] ) ) { |
| 320 | wc_admin_record_tracks_event( |
| 321 | 'coming_soon_template_saved', |
| 322 | array( |
| 323 | 'pattern_id' => $matches[1], |
| 324 | 'is_update' => $update, |
| 325 | ) |
| 326 | ); |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Load slotfill script and JS variables for the newsletter. |
| 333 | * The comingSoonNewsletter is used in client/wp-admin-scripts/coming-soon-newsletter-panel |
| 334 | * |
| 335 | * @return void |
| 336 | */ |
| 337 | public function load_newsletter_scripts() { |
| 338 | $screen = get_current_screen(); |
| 339 | if ( ! $screen instanceof \WP_Screen ) { |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | if ( 'site-editor' !== $screen->id ) { |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | $mailpoet = array( |
| 348 | 'mailpoet_installed' => PluginsHelper::is_plugin_installed( 'mailpoet' ), |
| 349 | 'mailpoet_connected' => $this->is_mailpoet_connected(), |
| 350 | ); |
| 351 | |
| 352 | // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion, WordPress.WP.EnqueuedResourceParameters.NotInFooter |
| 353 | wp_register_script( 'coming-soon-newsletter-mailpoet', '' ); |
| 354 | wp_enqueue_script( 'coming-soon-newsletter-mailpoet' ); |
| 355 | wp_add_inline_script( 'coming-soon-newsletter-mailpoet', 'var comingSoonNewsletter = ' . wp_json_encode( $mailpoet, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) . ';' ); |
| 356 | } |
| 357 | } |
| 358 |