PluginProbe
WooCommerce / 11.1.0-beta.2
WooCommerce v11.1.0-beta.2
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / Admin / Features / OnboardingTasks / Tasks / Products.php
Products.php
316 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Contextual image URL.
58 *
59 * @return string
60 */
61 public function get_image_url() {
62 return WC()->plugin_url() . '/assets/images/task_list/sales-section-illustration.svg';
63 }
64
65 /**
66 * Alt text for the contextual image.
67 *
68 * @return string
69 */
70 public function get_image_alt() {
71 return __( 'Products illustration', 'woocommerce' );
72 }
73
74 /**
75 * Title.
76 *
77 * @return string
78 */
79 public function get_title() {
80 $onboarding_profile = get_option( OnboardingProfile::DATA_OPTION, array() );
81
82 if ( isset( $onboarding_profile['business_choice'] ) && 'im_already_selling' === $onboarding_profile['business_choice'] ) {
83 return __( 'Import your products', 'woocommerce' );
84 }
85
86 return __( 'Add your products', 'woocommerce' );
87 }
88
89 /**
90 * Content.
91 *
92 * @return string
93 */
94 public function get_content() {
95 return __(
96 'Start by adding the first product to your store. You can add your products manually, via CSV, or import them from another service.',
97 'woocommerce'
98 );
99 }
100
101 /**
102 * Time.
103 *
104 * @return string
105 */
106 public function get_time() {
107 return __( '1 minute per product', 'woocommerce' );
108 }
109
110 /**
111 * Task completion.
112 *
113 * @return bool
114 */
115 public function is_complete() {
116 if ( $this->has_previously_completed() ) {
117 return true;
118 }
119
120 return self::has_products();
121 }
122
123 /**
124 * Additional data.
125 *
126 * @return array
127 */
128 public function get_additional_data() {
129 return array(
130 'has_products' => self::has_products(),
131 );
132 }
133
134 /**
135 * If a task is always accessible, relevant for when a task list is hidden but a task can still be viewed.
136 *
137 * @return bool
138 */
139 public function is_always_accessible() {
140 return true;
141 }
142
143 /**
144 * Adds a return to task list notice when completing the import product task.
145 *
146 * @param string $hook Page hook.
147 */
148 public function possibly_add_import_return_notice_script( $hook ) {
149 $step = isset( $_GET['step'] ) ? $_GET['step'] : ''; // phpcs:ignore csrf ok, sanitization ok.
150
151 if ( $hook !== 'product_page_product_importer' || $step !== 'done' ) {
152 return;
153 }
154
155 if ( ! $this->is_active() || $this->is_complete() ) {
156 return;
157 }
158
159 WCAdminAssets::register_script( 'wp-admin-scripts', 'onboarding-product-import-notice', true );
160 }
161
162 /**
163 * Adds a return to task list notice when completing the loading sample products action.
164 *
165 * @param string $hook Page hook.
166 */
167 public function possibly_add_load_sample_return_notice_script( $hook ) {
168 if ( $hook !== 'edit.php' || get_query_var( 'post_type' ) !== 'product' ) {
169 return;
170 }
171
172 $referer = wp_get_referer();
173 if ( ! $referer || strpos( $referer, wc_admin_url() ) !== 0 ) {
174 return;
175 }
176
177 if ( ! isset( $_GET[ Task::ACTIVE_TASK_TRANSIENT ] ) ) {
178 return;
179 }
180
181 $task_id = sanitize_title_with_dashes( wp_unslash( $_GET[ Task::ACTIVE_TASK_TRANSIENT ] ) );
182 if ( $task_id !== $this->get_id() || ! $this->is_complete() ) {
183 return;
184 }
185
186 WCAdminAssets::register_script( 'wp-admin-scripts', 'onboarding-load-sample-products-notice', true );
187 }
188
189 /**
190 * Set the has products transient if the post qualifies as a user created product.
191 *
192 * @param int $post_id Post ID.
193 */
194 public function maybe_set_has_product_transient_on_untrashed_post( $post_id ) {
195 if ( get_post_type( $post_id ) !== 'product' ) {
196 return;
197 }
198
199 $this->maybe_set_has_product_transient( $post_id, wc_get_product( $post_id ) );
200 }
201
202 /**
203 * Set the has products transient if the product qualifies as a user created product.
204 *
205 * @param int $product_id Product ID.
206 * @param WC_Product $product Product object.
207 */
208 public function maybe_set_has_product_transient( $product_id, $product ) {
209 if ( ! $this->has_previously_completed() && ProductStatus::PUBLISH === $product->get_status() ) {
210 set_transient( self::HAS_PRODUCT_TRANSIENT, 'yes' );
211 $this->possibly_track_completion();
212 }
213 }
214
215 /**
216 * Handle product trashing via the trashed_post hook.
217 *
218 * @param int $post_id Post ID.
219 * @return void
220 */
221 public function on_product_trashed( $post_id ) {
222 if ( get_post_type( $post_id ) !== 'product' ) {
223 return;
224 }
225
226 $this->revert_task_completion();
227 }
228
229 /**
230 * Handle permanent product deletion via the deleted_post_product hook.
231 *
232 * @return void
233 */
234 public function on_product_deleted() {
235 $this->revert_task_completion();
236 }
237
238 /**
239 * Schedule a deferred check to revert task completion if no products remain.
240 *
241 * Uses the shutdown hook so that bulk operations (e.g. trashing many products
242 * at once) only trigger a single has_products() query instead of one per product.
243 *
244 * @return void
245 */
246 private function revert_task_completion(): void {
247 delete_transient( self::HAS_PRODUCT_TRANSIENT );
248
249 if ( self::$revert_scheduled ) {
250 return;
251 }
252
253 self::$revert_scheduled = true;
254 add_action( 'shutdown', array( $this, 'maybe_revert_on_shutdown' ) );
255 }
256
257 /**
258 * Re-check whether valid products still exist and revert task completion if none remain.
259 *
260 * Runs once at the end of the request via the shutdown hook.
261 *
262 * @return void
263 */
264 public function maybe_revert_on_shutdown(): void {
265 self::$revert_scheduled = false;
266
267 if ( self::has_products() ) {
268 return;
269 }
270
271 $completed_tasks = get_option( self::COMPLETED_OPTION, array() );
272 $task_id = $this->get_id();
273
274 if ( in_array( $task_id, $completed_tasks, true ) ) {
275 $completed_tasks = array_values( array_diff( $completed_tasks, array( $task_id ) ) );
276 update_option( self::COMPLETED_OPTION, $completed_tasks );
277 }
278 }
279
280 /**
281 * Check if the store has any user created published products.
282 *
283 * @return bool
284 */
285 public static function has_products() {
286 $product_exists = get_transient( self::HAS_PRODUCT_TRANSIENT );
287 if ( $product_exists ) {
288 return 'yes' === $product_exists;
289 }
290
291 $counts = wp_count_posts( 'product' );
292 $value = isset( $counts->publish ) && $counts->publish > 0 ? 'yes' : 'no';
293 set_transient( self::HAS_PRODUCT_TRANSIENT, $value );
294 return 'yes' === $value;
295 }
296
297 /**
298 * Redirect to the add product tasklist if there are no products.
299 *
300 * @return void
301 */
302 public function maybe_redirect_to_add_product_tasklist() {
303 $screen = get_current_screen();
304 if ( $screen && 'edit' === $screen->base && 'product' === $screen->post_type ) {
305 $counts = wc_get_container()->get( ProductUtil::class )->get_counts_for_type( 'product' );
306 $count = array_sum( $counts ) - ( $counts[ ProductStatus::AUTO_DRAFT ] ?? 0 );
307 if ( $count > 0 ) {
308 return;
309 }
310
311 wp_safe_redirect( admin_url( 'admin.php?page=wc-admin&task=products' ) );
312 exit;
313 }
314 }
315 }
316