Agentic
4 months ago
BlockTemplates
4 weeks ago
EmailImprovements
1 year ago
EmailPreview
4 weeks ago
Emails
1 year ago
ImportExport
1 year ago
Logging
1 year ago
Marketing
2 years ago
Notes
4 weeks ago
Onboarding
5 months ago
Orders
4 weeks ago
ProductForm
2 years ago
ProductReviews
4 weeks ago
RemoteFreeExtensions
4 weeks ago
Schedulers
4 weeks ago
Settings
2 weeks ago
Suggestions
4 weeks ago
WCPayPromotion
10 months ago
ActivityPanels.php
3 years ago
Analytics.php
4 weeks ago
CategoryLookup.php
4 years ago
Coupons.php
1 year ago
CouponsMovedTrait.php
5 months ago
CustomerEffortScoreTracks.php
7 months ago
Events.php
2 months ago
FeaturePlugin.php
4 months ago
Homescreen.php
1 month ago
Loader.php
4 weeks ago
Marketing.php
1 year ago
Marketplace.php
5 months ago
MobileAppBanner.php
4 years ago
OrderMilestoneEasterEgg.php
4 weeks ago
RemoteInboxNotifications.php
3 years ago
Settings.php
2 weeks ago
ShippingLabelBanner.php
1 year ago
ShippingLabelBannerDisplayRules.php
1 year ago
SiteHealth.php
3 years ago
Survey.php
4 years ago
SystemStatusReport.php
1 year ago
Translations.php
1 year ago
WCAdminAssets.php
2 weeks ago
WCAdminSharedSettings.php
1 year ago
WCAdminUser.php
9 months ago
WcPayWelcomePage.php
1 year ago
Homescreen.php
283 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Homescreen. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types=1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\Admin; |
| 9 | |
| 10 | use Automattic\WooCommerce\Admin\Features\Features; |
| 11 | use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks\Shipping; |
| 12 | |
| 13 | /** |
| 14 | * Contains backend logic for the homescreen feature. |
| 15 | */ |
| 16 | class Homescreen { |
| 17 | /** |
| 18 | * Menu slug. |
| 19 | */ |
| 20 | const MENU_SLUG = 'wc-admin'; |
| 21 | |
| 22 | /** |
| 23 | * Class instance. |
| 24 | * |
| 25 | * @var Homescreen instance |
| 26 | */ |
| 27 | protected static $instance = null; |
| 28 | |
| 29 | /** |
| 30 | * Get class instance. |
| 31 | */ |
| 32 | public static function get_instance() { |
| 33 | if ( ! self::$instance ) { |
| 34 | self::$instance = new self(); |
| 35 | } |
| 36 | return self::$instance; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Hook into WooCommerce. |
| 41 | */ |
| 42 | public function __construct() { |
| 43 | add_filter( 'woocommerce_admin_get_user_data_fields', array( $this, 'add_user_data_fields' ) ); |
| 44 | add_action( 'admin_menu', array( $this, 'register_page' ) ); |
| 45 | // In WC Core 5.1 $submenu manipulation occurs in admin_menu, not admin_head. See https://github.com/woocommerce/woocommerce/pull/29088. |
| 46 | if ( version_compare( WC_VERSION, '5.1', '>=' ) ) { |
| 47 | // priority is 20 to run after admin_menu hook for woocommerce runs, so that submenu is populated. |
| 48 | add_action( 'admin_menu', array( $this, 'possibly_remove_woocommerce_menu' ) ); |
| 49 | add_action( 'admin_menu', array( $this, 'update_link_structure' ), 20 ); |
| 50 | } else { |
| 51 | // priority is 20 to run after https://github.com/woocommerce/woocommerce/blob/a55ae325306fc2179149ba9b97e66f32f84fdd9c/includes/admin/class-wc-admin-menus.php#L165. |
| 52 | add_action( 'admin_head', array( $this, 'update_link_structure' ), 20 ); |
| 53 | } |
| 54 | |
| 55 | add_filter( 'woocommerce_admin_preload_options', array( $this, 'preload_options' ) ); |
| 56 | |
| 57 | if ( Features::is_enabled( 'shipping-smart-defaults' ) ) { |
| 58 | add_filter( |
| 59 | 'woocommerce_admin_shared_settings', |
| 60 | array( $this, 'maybe_set_default_shipping_options_on_home' ), |
| 61 | 9999 |
| 62 | ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Set free shipping in the same country as the store default |
| 68 | * Flag rate in all other countries when any of the following conditions are true |
| 69 | * |
| 70 | * - The store sells physical products, has JP and WCS installed and connected, and is located in the US. |
| 71 | * - The store sells physical products, and is not located in US/Canada/Australia/UK (irrelevant if JP is installed or not). |
| 72 | * - The store sells physical products and is located in US, but JP and WCS are not installed. |
| 73 | * |
| 74 | * @param array $settings shared admin settings. |
| 75 | * @return array |
| 76 | */ |
| 77 | public function maybe_set_default_shipping_options_on_home( $settings ) { |
| 78 | if ( ! function_exists( 'get_current_screen' ) ) { |
| 79 | return $settings; |
| 80 | } |
| 81 | |
| 82 | $current_screen = get_current_screen(); |
| 83 | |
| 84 | // Abort if it's not the homescreen. |
| 85 | if ( ! isset( $current_screen->id ) || 'woocommerce_page_wc-admin' !== $current_screen->id ) { |
| 86 | return $settings; |
| 87 | } |
| 88 | |
| 89 | // Abort if we already created the shipping options. |
| 90 | $already_created = get_option( 'woocommerce_admin_created_default_shipping_zones' ); |
| 91 | if ( 'yes' === $already_created ) { |
| 92 | return $settings; |
| 93 | } |
| 94 | |
| 95 | $zone_count = count( \WC_Data_Store::load( 'shipping-zone' )->get_zones() ); |
| 96 | if ( $zone_count ) { |
| 97 | update_option( 'woocommerce_admin_created_default_shipping_zones', 'yes' ); |
| 98 | update_option( 'woocommerce_admin_reviewed_default_shipping_zones', 'yes' ); |
| 99 | return $settings; |
| 100 | } |
| 101 | |
| 102 | $user_skipped_obw = $settings['onboarding']['profile']['skipped'] ?? false; |
| 103 | $store_address = $settings['preloadSettings']['general']['woocommerce_store_address'] ?? ''; |
| 104 | $product_types = $settings['onboarding']['profile']['product_types'] ?? array(); |
| 105 | $user_has_set_store_country = $settings['onboarding']['profile']['is_store_country_set'] ?? false; |
| 106 | |
| 107 | // Do not proceed if user has not filled out their country in the onboarding profiler. |
| 108 | if ( ! $user_has_set_store_country ) { |
| 109 | return $settings; |
| 110 | } |
| 111 | |
| 112 | // If user skipped the obw or has not completed the store_details |
| 113 | // then we assume the user is going to sell physical products. |
| 114 | if ( $user_skipped_obw || '' === $store_address ) { |
| 115 | $product_types[] = 'physical'; |
| 116 | } |
| 117 | |
| 118 | if ( false === in_array( 'physical', $product_types, true ) ) { |
| 119 | return $settings; |
| 120 | } |
| 121 | |
| 122 | $country_code = wc_format_country_state_string( $settings['preloadSettings']['general']['woocommerce_default_country'] )['country']; |
| 123 | $country_name = WC()->countries->get_countries()[ $country_code ] ?? null; |
| 124 | |
| 125 | $is_jetpack_installed = in_array( 'jetpack', $settings['plugins']['installedPlugins'] ?? array(), true ); |
| 126 | $is_wcs_installed = in_array( 'woocommerce-services', $settings['plugins']['installedPlugins'] ?? array(), true ); |
| 127 | |
| 128 | if ( |
| 129 | ( 'US' === $country_code && $is_jetpack_installed ) |
| 130 | || |
| 131 | ( ! in_array( $country_code, array( 'US', 'CA', 'AU', 'NZ', 'SG', 'HK', 'GB', 'ES', 'IT', 'DE', 'FR', 'MX', 'CO', 'CL', 'AR', 'PE', 'BR', 'UY', 'GT', 'NL', 'AT', 'BE' ), true ) ) |
| 132 | || |
| 133 | ( 'US' === $country_code && false === $is_jetpack_installed && false === $is_wcs_installed ) |
| 134 | ) { |
| 135 | $zone = new \WC_Shipping_Zone(); |
| 136 | $zone->set_zone_name( $country_name ); |
| 137 | $zone->add_location( $country_code, 'country' ); |
| 138 | |
| 139 | // Method creation has no default title, use the REST API to add a title. |
| 140 | $instance_id = $zone->add_shipping_method( 'free_shipping' ); |
| 141 | $request = new \WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); |
| 142 | $request->set_body_params( |
| 143 | array( |
| 144 | 'settings' => array( |
| 145 | 'title' => 'Free shipping', |
| 146 | ), |
| 147 | ) |
| 148 | ); |
| 149 | rest_do_request( $request ); |
| 150 | |
| 151 | update_option( 'woocommerce_admin_created_default_shipping_zones', 'yes' ); |
| 152 | Shipping::delete_zone_count_transient(); |
| 153 | } |
| 154 | |
| 155 | return $settings; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Adds fields so that we can store performance indicators, row settings, and chart type settings for users. |
| 160 | * |
| 161 | * @param array $user_data_fields User data fields. |
| 162 | * @return array |
| 163 | */ |
| 164 | public function add_user_data_fields( $user_data_fields ) { |
| 165 | return array_merge( |
| 166 | $user_data_fields, |
| 167 | array( |
| 168 | 'homepage_layout', |
| 169 | 'homepage_stats', |
| 170 | 'task_list_tracked_started_tasks', |
| 171 | ) |
| 172 | ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Registers home page. |
| 177 | */ |
| 178 | public function register_page() { |
| 179 | // Register a top-level item for users who cannot view the core WooCommerce menu. |
| 180 | if ( ! self::is_admin_user() ) { |
| 181 | wc_admin_register_page( |
| 182 | array( |
| 183 | 'id' => 'woocommerce-home', |
| 184 | 'title' => __( 'WooCommerce', 'woocommerce' ), |
| 185 | 'path' => self::MENU_SLUG, |
| 186 | 'capability' => 'read', |
| 187 | ) |
| 188 | ); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | wc_admin_register_page( |
| 193 | array( |
| 194 | 'id' => 'woocommerce-home', |
| 195 | 'title' => __( 'Home', 'woocommerce' ), |
| 196 | 'parent' => 'woocommerce', |
| 197 | 'path' => self::MENU_SLUG, |
| 198 | 'order' => 0, |
| 199 | 'capability' => 'read', |
| 200 | ) |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Check if the user can access the top-level WooCommerce item. |
| 206 | * |
| 207 | * @return bool |
| 208 | */ |
| 209 | public static function is_admin_user() { |
| 210 | if ( ! class_exists( 'WC_Admin_Menus', false ) ) { |
| 211 | include_once WC_ABSPATH . 'includes/admin/class-wc-admin-menus.php'; |
| 212 | } |
| 213 | if ( method_exists( 'WC_Admin_Menus', 'can_view_woocommerce_menu_item' ) ) { |
| 214 | return \WC_Admin_Menus::can_view_woocommerce_menu_item() || current_user_can( 'manage_woocommerce' ); |
| 215 | } else { |
| 216 | // We leave this line for WC versions <= 6.2. |
| 217 | return current_user_can( 'edit_others_shop_orders' ) || current_user_can( 'manage_woocommerce' ); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Possibly remove the WooCommerce menu item if it was purely used to access wc-admin pages. |
| 223 | */ |
| 224 | public function possibly_remove_woocommerce_menu() { |
| 225 | global $menu; |
| 226 | |
| 227 | if ( self::is_admin_user() ) { |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | foreach ( $menu as $key => $menu_item ) { |
| 232 | if ( self::MENU_SLUG !== $menu_item[2] || 'read' !== $menu_item[1] ) { |
| 233 | continue; |
| 234 | } |
| 235 | |
| 236 | unset( $menu[ $key ] ); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Update the WooCommerce menu structure to make our main dashboard/handler |
| 242 | * the top level link for 'WooCommerce'. |
| 243 | */ |
| 244 | public function update_link_structure() { |
| 245 | global $submenu; |
| 246 | // User does not have capabilities to see the submenu. |
| 247 | if ( ! current_user_can( 'manage_woocommerce' ) || empty( $submenu['woocommerce'] ) ) { |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | $wc_admin_key = null; |
| 252 | foreach ( $submenu['woocommerce'] as $submenu_key => $submenu_item ) { |
| 253 | if ( self::MENU_SLUG === $submenu_item[2] ) { |
| 254 | $wc_admin_key = $submenu_key; |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | if ( ! $wc_admin_key ) { |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | $menu = $submenu['woocommerce'][ $wc_admin_key ]; |
| 264 | |
| 265 | // Move menu item to top of array. |
| 266 | unset( $submenu['woocommerce'][ $wc_admin_key ] ); |
| 267 | array_unshift( $submenu['woocommerce'], $menu ); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Preload options to prime state of the application. |
| 272 | * |
| 273 | * @param array $options Array of options to preload. |
| 274 | * @return array |
| 275 | */ |
| 276 | public function preload_options( $options ) { |
| 277 | $options[] = 'woocommerce_default_homepage_layout'; |
| 278 | $options[] = 'woocommerce_admin_install_timestamp'; |
| 279 | |
| 280 | return $options; |
| 281 | } |
| 282 | } |
| 283 |