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