AdditionalPayments.php
1 year ago
Appearance.php
2 years ago
CustomizeStore.php
5 months ago
ExperimentalShippingRecommendation.php
5 months ago
ExtendStore.php
1 year ago
GetMobileApp.php
3 years ago
LaunchYourStore.php
2 years ago
Marketing.php
7 months ago
Payments.php
10 months ago
Products.php
1 month ago
ReviewShippingOptions.php
3 years ago
Shipping.php
3 months ago
StoreCreation.php
4 years ago
StoreDetails.php
3 years ago
Tax.php
1 year ago
TourInAppMarketplace.php
2 years ago
WooCommercePayments.php
5 months ago
Products.php
354 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks; |
| 4 | |
| 5 | use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task; |
| 6 | use Automattic\WooCommerce\Enums\ProductStatus; |
| 7 | use Automattic\WooCommerce\Internal\Admin\WCAdminAssets; |
| 8 | use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile; |
| 9 | |
| 10 | /** |
| 11 | * Products Task |
| 12 | */ |
| 13 | class Products extends Task { |
| 14 | const HAS_PRODUCT_TRANSIENT = 'woocommerce_product_task_has_product_transient'; |
| 15 | |
| 16 | /** |
| 17 | * Whether a deferred revert check has already been scheduled for this request. |
| 18 | * |
| 19 | * @var bool |
| 20 | */ |
| 21 | private static $revert_scheduled = false; |
| 22 | |
| 23 | /** |
| 24 | * Constructor |
| 25 | * |
| 26 | * @param TaskList $task_list Parent task list. |
| 27 | */ |
| 28 | public function __construct( $task_list ) { |
| 29 | parent::__construct( $task_list ); |
| 30 | add_action( 'admin_enqueue_scripts', array( $this, 'possibly_add_import_return_notice_script' ) ); |
| 31 | add_action( 'admin_enqueue_scripts', array( $this, 'possibly_add_load_sample_return_notice_script' ) ); |
| 32 | |
| 33 | add_action( 'woocommerce_update_product', array( $this, 'maybe_set_has_product_transient' ), 10, 2 ); |
| 34 | add_action( 'woocommerce_new_product', array( $this, 'maybe_set_has_product_transient' ), 10, 2 ); |
| 35 | add_action( 'untrashed_post', array( $this, 'maybe_set_has_product_transient_on_untrashed_post' ) ); |
| 36 | add_action( 'current_screen', array( $this, 'maybe_redirect_to_add_product_tasklist' ), 30, 0 ); |
| 37 | |
| 38 | add_action( 'trashed_post', array( $this, 'on_product_trashed' ) ); |
| 39 | add_action( 'deleted_post_product', array( $this, 'on_product_deleted' ) ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * ID. |
| 44 | * |
| 45 | * @return string |
| 46 | */ |
| 47 | public function get_id() { |
| 48 | return 'products'; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Title. |
| 53 | * |
| 54 | * @return string |
| 55 | */ |
| 56 | public function get_title() { |
| 57 | $onboarding_profile = get_option( OnboardingProfile::DATA_OPTION, array() ); |
| 58 | |
| 59 | if ( isset( $onboarding_profile['business_choice'] ) && 'im_already_selling' === $onboarding_profile['business_choice'] ) { |
| 60 | return __( 'Import your products', 'woocommerce' ); |
| 61 | } |
| 62 | |
| 63 | return __( 'Add your products', 'woocommerce' ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Content. |
| 68 | * |
| 69 | * @return string |
| 70 | */ |
| 71 | public function get_content() { |
| 72 | return __( |
| 73 | 'Start by adding the first product to your store. You can add your products manually, via CSV, or import them from another service.', |
| 74 | 'woocommerce' |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Time. |
| 80 | * |
| 81 | * @return string |
| 82 | */ |
| 83 | public function get_time() { |
| 84 | return __( '1 minute per product', 'woocommerce' ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Task completion. |
| 89 | * |
| 90 | * @return bool |
| 91 | */ |
| 92 | public function is_complete() { |
| 93 | if ( $this->has_previously_completed() ) { |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | return self::has_products(); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Additional data. |
| 102 | * |
| 103 | * @return array |
| 104 | */ |
| 105 | public function get_additional_data() { |
| 106 | return array( |
| 107 | 'has_products' => self::has_products(), |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * If a task is always accessible, relevant for when a task list is hidden but a task can still be viewed. |
| 113 | * |
| 114 | * @return bool |
| 115 | */ |
| 116 | public function is_always_accessible() { |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Adds a return to task list notice when completing the import product task. |
| 122 | * |
| 123 | * @param string $hook Page hook. |
| 124 | */ |
| 125 | public function possibly_add_import_return_notice_script( $hook ) { |
| 126 | $step = isset( $_GET['step'] ) ? $_GET['step'] : ''; // phpcs:ignore csrf ok, sanitization ok. |
| 127 | |
| 128 | if ( $hook !== 'product_page_product_importer' || $step !== 'done' ) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | if ( ! $this->is_active() || $this->is_complete() ) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | WCAdminAssets::register_script( 'wp-admin-scripts', 'onboarding-product-import-notice', true ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Adds a return to task list notice when completing the loading sample products action. |
| 141 | * |
| 142 | * @param string $hook Page hook. |
| 143 | */ |
| 144 | public function possibly_add_load_sample_return_notice_script( $hook ) { |
| 145 | if ( $hook !== 'edit.php' || get_query_var( 'post_type' ) !== 'product' ) { |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | $referer = wp_get_referer(); |
| 150 | if ( ! $referer || strpos( $referer, wc_admin_url() ) !== 0 ) { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | if ( ! isset( $_GET[ Task::ACTIVE_TASK_TRANSIENT ] ) ) { |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | $task_id = sanitize_title_with_dashes( wp_unslash( $_GET[ Task::ACTIVE_TASK_TRANSIENT ] ) ); |
| 159 | if ( $task_id !== $this->get_id() || ! $this->is_complete() ) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | WCAdminAssets::register_script( 'wp-admin-scripts', 'onboarding-load-sample-products-notice', true ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Set the has products transient if the post qualifies as a user created product. |
| 168 | * |
| 169 | * @param int $post_id Post ID. |
| 170 | */ |
| 171 | public function maybe_set_has_product_transient_on_untrashed_post( $post_id ) { |
| 172 | if ( get_post_type( $post_id ) !== 'product' ) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | $this->maybe_set_has_product_transient( $post_id, wc_get_product( $post_id ) ); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Set the has products transient if the product qualifies as a user created product. |
| 181 | * |
| 182 | * @param int $product_id Product ID. |
| 183 | * @param WC_Product $product Product object. |
| 184 | */ |
| 185 | public function maybe_set_has_product_transient( $product_id, $product ) { |
| 186 | if ( ! $this->has_previously_completed() && $this->is_valid_product( $product ) ) { |
| 187 | set_transient( self::HAS_PRODUCT_TRANSIENT, 'yes' ); |
| 188 | $this->possibly_track_completion(); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Handle product trashing via the trashed_post hook. |
| 194 | * |
| 195 | * @param int $post_id Post ID. |
| 196 | * @return void |
| 197 | */ |
| 198 | public function on_product_trashed( $post_id ) { |
| 199 | if ( get_post_type( $post_id ) !== 'product' ) { |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | $this->revert_task_completion(); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Handle permanent product deletion via the deleted_post_product hook. |
| 208 | * |
| 209 | * @return void |
| 210 | */ |
| 211 | public function on_product_deleted() { |
| 212 | $this->revert_task_completion(); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Schedule a deferred check to revert task completion if no products remain. |
| 217 | * |
| 218 | * Uses the shutdown hook so that bulk operations (e.g. trashing many products |
| 219 | * at once) only trigger a single has_products() query instead of one per product. |
| 220 | * |
| 221 | * @return void |
| 222 | */ |
| 223 | private function revert_task_completion(): void { |
| 224 | delete_transient( self::HAS_PRODUCT_TRANSIENT ); |
| 225 | |
| 226 | if ( self::$revert_scheduled ) { |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | self::$revert_scheduled = true; |
| 231 | add_action( 'shutdown', array( $this, 'maybe_revert_on_shutdown' ) ); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Re-check whether valid products still exist and revert task completion if none remain. |
| 236 | * |
| 237 | * Runs once at the end of the request via the shutdown hook. |
| 238 | * |
| 239 | * @return void |
| 240 | */ |
| 241 | public function maybe_revert_on_shutdown(): void { |
| 242 | self::$revert_scheduled = false; |
| 243 | |
| 244 | if ( self::has_products() ) { |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | $completed_tasks = get_option( self::COMPLETED_OPTION, array() ); |
| 249 | $task_id = $this->get_id(); |
| 250 | |
| 251 | if ( in_array( $task_id, $completed_tasks, true ) ) { |
| 252 | $completed_tasks = array_values( array_diff( $completed_tasks, array( $task_id ) ) ); |
| 253 | update_option( self::COMPLETED_OPTION, $completed_tasks ); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Check if the product qualifies as a user created product. |
| 259 | * |
| 260 | * @param WC_Product $product Product object. |
| 261 | * @return bool |
| 262 | */ |
| 263 | private function is_valid_product( $product ) { |
| 264 | return ProductStatus::PUBLISH === $product->get_status() && |
| 265 | ( ! $product->get_meta( '_headstart_post' ) || |
| 266 | get_post_meta( $product->get_id(), '_edit_last', true ) ); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Check if the store has any user created published products. |
| 271 | * |
| 272 | * @return bool |
| 273 | */ |
| 274 | public static function has_products() { |
| 275 | $product_exists = get_transient( self::HAS_PRODUCT_TRANSIENT ); |
| 276 | if ( $product_exists ) { |
| 277 | return 'yes' === $product_exists; |
| 278 | } |
| 279 | |
| 280 | global $wpdb; |
| 281 | |
| 282 | /* |
| 283 | * Check if any valid products exist and return 'yes' or 'no' |
| 284 | * A valid product must: |
| 285 | * 1. Be a published product post type |
| 286 | * 2. Meet one of these conditions: |
| 287 | * - Have been edited by a user (_edit_last meta exists), OR |
| 288 | * - Not have _headstart_post meta, OR |
| 289 | * - Have _headstart_post meta but it's NULL |
| 290 | */ |
| 291 | $value = $wpdb->get_var( |
| 292 | $wpdb->prepare( |
| 293 | "SELECT IF( |
| 294 | EXISTS ( |
| 295 | SELECT 1 FROM {$wpdb->posts} p |
| 296 | WHERE p.post_type = %s |
| 297 | AND p.post_status = %s |
| 298 | AND ( |
| 299 | EXISTS ( |
| 300 | SELECT 1 FROM {$wpdb->postmeta} pm |
| 301 | WHERE pm.post_id = p.ID |
| 302 | AND pm.meta_key = %s |
| 303 | ) |
| 304 | OR |
| 305 | NOT EXISTS ( |
| 306 | SELECT 1 FROM {$wpdb->postmeta} pm |
| 307 | WHERE pm.post_id = p.ID |
| 308 | AND pm.meta_key = %s |
| 309 | ) |
| 310 | OR |
| 311 | EXISTS ( |
| 312 | SELECT 1 FROM {$wpdb->postmeta} pm |
| 313 | WHERE pm.post_id = p.ID |
| 314 | AND pm.meta_key = %s |
| 315 | AND pm.meta_value = '' |
| 316 | ) |
| 317 | ) |
| 318 | LIMIT 1 |
| 319 | ), |
| 320 | 'yes', 'no' |
| 321 | )", |
| 322 | 'product', |
| 323 | ProductStatus::PUBLISH, |
| 324 | '_edit_last', |
| 325 | '_headstart_post', |
| 326 | '_headstart_post' |
| 327 | ) |
| 328 | ); |
| 329 | |
| 330 | set_transient( self::HAS_PRODUCT_TRANSIENT, $value ); |
| 331 | return 'yes' === $value; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Redirect to the add product tasklist if there are no products. |
| 336 | * |
| 337 | * @return void |
| 338 | */ |
| 339 | public function maybe_redirect_to_add_product_tasklist() { |
| 340 | $screen = get_current_screen(); |
| 341 | if ( $screen && 'edit' === $screen->base && 'product' === $screen->post_type ) { |
| 342 | // wp_count_posts is cached. |
| 343 | $counts = (array) wp_count_posts( $screen->post_type ); |
| 344 | unset( $counts['auto-draft'] ); |
| 345 | $count = array_sum( $counts ); |
| 346 | if ( $count > 0 ) { |
| 347 | return; |
| 348 | } |
| 349 | wp_safe_redirect( admin_url( 'admin.php?page=wc-admin&task=products' ) ); |
| 350 | exit; |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 |