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 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / admin / class-wc-admin-duplicate-product.php
woocommerce / includes / admin Last commit date
helper 1 month ago importers 1 year ago list-tables 4 months ago marketplace-suggestions 10 months ago meta-boxes 1 month ago notes 1 month ago plugin-updates 2 years ago reports 2 months ago settings 1 week ago views 2 months ago class-wc-admin-addons.php 8 months ago class-wc-admin-api-keys-table-list.php 2 years ago class-wc-admin-api-keys.php 10 months ago class-wc-admin-assets.php 1 month ago class-wc-admin-attributes.php 3 years ago class-wc-admin-brands.php 3 months ago class-wc-admin-customize.php 5 years ago class-wc-admin-dashboard-setup.php 10 months ago class-wc-admin-dashboard.php 3 months ago class-wc-admin-duplicate-product.php 4 months ago class-wc-admin-exporters.php 1 year ago class-wc-admin-help.php 2 years ago class-wc-admin-importers.php 10 months ago class-wc-admin-log-table-list.php 3 months ago class-wc-admin-marketplace-promotions.php 3 months ago class-wc-admin-menus.php 3 months ago class-wc-admin-meta-boxes.php 1 year ago class-wc-admin-notices.php 1 month ago class-wc-admin-permalink-settings.php 5 years ago class-wc-admin-pointers.php 3 years ago class-wc-admin-post-types.php 1 year ago class-wc-admin-profile.php 1 year ago class-wc-admin-reports.php 3 months ago class-wc-admin-settings.php 2 months ago class-wc-admin-setup-wizard.php 3 months ago class-wc-admin-status.php 1 year ago class-wc-admin-taxonomies.php 6 months ago class-wc-admin-upload-downloadable-product.php 2 years ago class-wc-admin-webhooks-table-list.php 1 year ago class-wc-admin-webhooks.php 10 months ago class-wc-admin.php 2 months ago wc-admin-functions.php 6 months ago wc-meta-box-functions.php 1 year ago woocommerce-legacy-reports.php 1 year ago
class-wc-admin-duplicate-product.php
404 lines
1 <?php
2 /**
3 * Duplicate product functionality
4 *
5 * @package WooCommerce\Admin
6 * @version 3.0.0
7 */
8
9 use Automattic\WooCommerce\Enums\ProductStatus;
10 use Automattic\WooCommerce\Enums\ProductType;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 if ( class_exists( 'WC_Admin_Duplicate_Product', false ) ) {
17 return new WC_Admin_Duplicate_Product();
18 }
19
20 /**
21 * WC_Admin_Duplicate_Product Class.
22 */
23 class WC_Admin_Duplicate_Product {
24
25 /**
26 * Constructor.
27 */
28 public function __construct() {
29 add_action( 'admin_action_duplicate_product', array( $this, 'duplicate_product_action' ) );
30 add_filter( 'post_row_actions', array( $this, 'dupe_link' ), 10, 2 );
31 add_action( 'post_submitbox_start', array( $this, 'dupe_button' ) );
32 }
33
34 /**
35 * Show the "Duplicate" link in admin products list.
36 *
37 * @param array $actions Array of actions.
38 * @param WP_Post $post Post object.
39 * @return array
40 */
41 public function dupe_link( $actions, $post ) {
42 global $the_product;
43
44 if ( ! current_user_can( apply_filters( 'woocommerce_duplicate_product_capability', 'manage_woocommerce' ) ) ) {
45 return $actions;
46 }
47
48 if ( 'product' !== $post->post_type ) {
49 return $actions;
50 }
51
52 // Add Class to Delete Permanently link in row actions.
53 if ( empty( $the_product ) || $the_product->get_id() !== $post->ID ) {
54 $the_product = wc_get_product( $post );
55 }
56
57 if ( $the_product && ProductStatus::PUBLISH === $the_product->get_status() && 0 < $the_product->get_total_sales() ) {
58 $actions['trash'] = sprintf(
59 '<a href="%s" class="submitdelete trash-product" aria-label="%s">%s</a>',
60 get_delete_post_link( $the_product->get_id(), '', false ),
61 /* translators: %s: post title */
62 esc_attr( sprintf( __( 'Move &#8220;%s&#8221; to the Trash', 'woocommerce' ), $the_product->get_name() ) ),
63 esc_html__( 'Trash', 'woocommerce' )
64 );
65 }
66
67 $actions['duplicate'] = '<a href="' . wp_nonce_url( admin_url( 'edit.php?post_type=product&action=duplicate_product&amp;post=' . $post->ID ), 'woocommerce-duplicate-product_' . $post->ID ) . '" aria-label="' . esc_attr__( 'Make a duplicate from this product', 'woocommerce' )
68 . '" rel="permalink">' . esc_html__( 'Duplicate', 'woocommerce' ) . '</a>';
69
70 return $actions;
71 }
72
73 /**
74 * Show the dupe product link in admin.
75 */
76 public function dupe_button() {
77 global $post;
78
79 if ( ! current_user_can( apply_filters( 'woocommerce_duplicate_product_capability', 'manage_woocommerce' ) ) ) {
80 return;
81 }
82
83 if ( ! is_object( $post ) ) {
84 return;
85 }
86
87 if ( 'product' !== $post->post_type ) {
88 return;
89 }
90
91 $notify_url = wp_nonce_url( admin_url( 'edit.php?post_type=product&action=duplicate_product&post=' . absint( $post->ID ) ), 'woocommerce-duplicate-product_' . $post->ID );
92 ?>
93 <div id="duplicate-action"><a class="submitduplicate duplication" href="<?php echo esc_url( $notify_url ); ?>"><?php esc_html_e( 'Copy to a new draft', 'woocommerce' ); ?></a></div>
94 <?php
95 }
96
97 /**
98 * Duplicate a product action.
99 */
100 public function duplicate_product_action() {
101 if ( empty( $_REQUEST['post'] ) ) {
102 wp_die( esc_html__( 'No product to duplicate has been supplied!', 'woocommerce' ) );
103 }
104
105 $product_id = isset( $_REQUEST['post'] ) ? absint( $_REQUEST['post'] ) : '';
106
107 check_admin_referer( 'woocommerce-duplicate-product_' . $product_id );
108
109 $product = wc_get_product( $product_id );
110
111 if ( false === $product ) {
112 /* translators: %s: product id */
113 wp_die( sprintf( esc_html__( 'Product creation failed, could not find original product: %s', 'woocommerce' ), esc_html( $product_id ) ) );
114 }
115
116 $duplicate = $this->product_duplicate( $product );
117
118 // Hook rename to match other woocommerce_product_* hooks, and to move away from depending on a response from the wp_posts table.
119 do_action( 'woocommerce_product_duplicate', $duplicate, $product );
120 wc_do_deprecated_action( 'woocommerce_duplicate_product', array( $duplicate->get_id(), $this->get_product_to_duplicate( $product_id ) ), '3.0', 'Use woocommerce_product_duplicate action instead.' );
121
122 // Redirect to the edit screen for the new draft page.
123 wp_redirect( admin_url( 'post.php?action=edit&post=' . $duplicate->get_id() ) );
124 exit;
125 }
126
127 /**
128 * Function to create the duplicate of the product.
129 *
130 * @param WC_Product $product The product to duplicate.
131 * @return WC_Product The duplicate.
132 */
133 public function product_duplicate( $product ) {
134 if ( ! $product instanceof WC_Product ) {
135 wc_doing_it_wrong( __METHOD__, 'product_duplicate() expects a WC_Product instance', '10.5.0' );
136 return new WC_Product();
137 }
138
139 /**
140 * Filter to allow us to exclude meta keys from product duplication..
141 *
142 * @param array $exclude_meta The keys to exclude from the duplicate.
143 * @param array $existing_meta_keys The meta keys that the product already has.
144 * @since 2.6
145 */
146 $meta_to_exclude = array_filter(
147 apply_filters(
148 'woocommerce_duplicate_product_exclude_meta',
149 array(),
150 array_map(
151 function ( $datum ) {
152 return $datum->key;
153 },
154 $product->get_meta_data()
155 )
156 )
157 );
158
159 $duplicate = clone $product;
160 $duplicate->set_id( 0 );
161 /* translators: %s contains the name of the original product. */
162 $duplicate->set_name( sprintf( esc_html__( '%s (Copy)', 'woocommerce' ), $duplicate->get_name() ) );
163 $duplicate->set_total_sales( 0 );
164 if ( '' !== $product->get_sku( 'edit' ) ) {
165 $this->generate_unique_sku( $duplicate );
166 }
167 if ( '' !== $product->get_global_unique_id( 'edit' ) ) {
168 $duplicate->set_global_unique_id( '' );
169 }
170 $duplicate->set_status( ProductStatus::DRAFT );
171 $duplicate->set_date_created( null );
172 $duplicate->set_slug( '' );
173 $duplicate->set_rating_counts( 0 );
174 $duplicate->set_average_rating( 0 );
175 $duplicate->set_review_count( 0 );
176
177 foreach ( $meta_to_exclude as $meta_key ) {
178 $duplicate->delete_meta_data( $meta_key );
179 }
180
181 /**
182 * This action can be used to modify the object further before it is created - it will be passed by reference.
183 *
184 * @since 3.0
185 */
186 do_action( 'woocommerce_product_duplicate_before_save', $duplicate, $product );
187
188 // Save parent product.
189 $duplicate->save();
190
191 /**
192 * Duplicate children of a variable product.
193 *
194 * @since 2.7
195 */
196 if ( ! apply_filters( 'woocommerce_duplicate_product_exclude_children', false, $product ) && $product->is_type( ProductType::VARIABLE ) ) {
197 foreach ( $product->get_children() as $child_id ) {
198 $child = wc_get_product( $child_id );
199
200 if ( ! $child instanceof WC_Product ) {
201 wc_doing_it_wrong( __METHOD__, 'product_duplicate() expects product children to be WC_Product instances', '10.5.0' );
202 continue;
203 }
204
205 $child->read_meta_data();
206 $child_duplicate = clone $child;
207 $child_duplicate->set_parent_id( $duplicate->get_id() );
208 $child_duplicate->set_id( 0 );
209 $child_duplicate->set_date_created( null );
210
211 // If we wait and let the insertion generate the slug, we will see extreme performance degradation
212 // in the case where a product is used as a template. Every time the template is duplicated, each
213 // variation will query every consecutive slug until it finds an empty one. To avoid this, we can
214 // optimize the generation ourselves, avoiding the issue altogether.
215 $this->generate_unique_slug( $child_duplicate );
216
217 if ( '' !== $child->get_sku( 'edit' ) ) {
218 $this->generate_unique_sku( $child_duplicate );
219 }
220 if ( '' !== $child->get_global_unique_id( 'edit' ) ) {
221 $child_duplicate->set_global_unique_id( '' );
222 }
223
224 foreach ( $meta_to_exclude as $meta_key ) {
225 $child_duplicate->delete_meta_data( $meta_key );
226 }
227
228 /**
229 * This action can be used to modify the object further before it is created - it will be passed by reference.
230 *
231 * @since 3.0
232 */
233 do_action( 'woocommerce_product_duplicate_before_save', $child_duplicate, $child );
234
235 $child_duplicate->save();
236 }
237
238 // Get new object to reflect new children.
239 $duplicate = wc_get_product( $duplicate->get_id() );
240 }
241
242 return $duplicate;
243 }
244
245 /**
246 * Get a product from the database to duplicate.
247 *
248 * @deprecated 3.0.0
249 * @param mixed $id The ID of the product to duplicate.
250 * @return object|bool
251 * @see duplicate_product
252 */
253 private function get_product_to_duplicate( $id ) {
254 global $wpdb;
255
256 $id = absint( $id );
257
258 if ( ! $id ) {
259 return false;
260 }
261
262 $post = $wpdb->get_row( $wpdb->prepare( "SELECT {$wpdb->posts}.* FROM {$wpdb->posts} WHERE ID = %d", $id ) );
263
264 if ( isset( $post->post_type ) && 'revision' === $post->post_type ) {
265 $id = $post->post_parent;
266 $post = $wpdb->get_row( $wpdb->prepare( "SELECT {$wpdb->posts}.* FROM {$wpdb->posts} WHERE ID = %d", $id ) );
267 }
268
269 return $post;
270 }
271
272 /**
273 * Generates a unique slug for a given product. We do this so that we can override the
274 * behavior of wp_unique_post_slug(). The normal slug generation will run single
275 * select queries on every non-unique slug, resulting in very bad performance.
276 *
277 * @param WC_Product $product The product to generate a slug for.
278 * @since 3.9.0
279 */
280 private function generate_unique_slug( $product ) {
281 global $wpdb;
282
283 // We want to remove the suffix from the slug so that we can find the maximum suffix using this root slug.
284 // This will allow us to find the next-highest suffix that is unique. While this does not support gap
285 // filling, this shouldn't matter for our use-case.
286 $root_slug = preg_replace( '/-[0-9]+$/', '', $product->get_slug() );
287
288 $results = $wpdb->get_results(
289 $wpdb->prepare( "SELECT post_name FROM $wpdb->posts WHERE post_name LIKE %s AND post_type IN ( 'product', 'product_variation' )", $root_slug . '%' )
290 );
291
292 // The slug is already unique!
293 if ( empty( $results ) ) {
294 return;
295 }
296
297 // Find the maximum suffix so we can ensure uniqueness.
298 $max_suffix = 1;
299 foreach ( $results as $result ) {
300 // Pull a numerical suffix off the slug after the last hyphen.
301 $suffix = intval( substr( $result->post_name, strrpos( $result->post_name, '-' ) + 1 ) );
302 if ( $suffix > $max_suffix ) {
303 $max_suffix = $suffix;
304 }
305 }
306
307 $product->set_slug( $root_slug . '-' . ( $max_suffix + 1 ) );
308 }
309
310 /**
311 * Generates a unique sku for a given product.
312 *
313 * @param WC_Product $product The product to generate a sku for.
314 * @return void
315 * @since 10.5.0
316 *
317 * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
318 */
319 private function generate_unique_sku( $product ) {
320 global $wpdb;
321
322 // We want to remove the suffix from the sku so that we can find the maximum suffix using this root sku.
323 // This will allow us to find the next-highest suffix that is unique. While this does not support gap
324 // filling, this shouldn't matter for our use-case.
325 $root_sku = preg_replace( '/-[0-9]+$/', '', $product->get_sku() );
326
327 // If the parent product has no SKU, don't do anything.
328 if ( ! $root_sku ) {
329 return;
330 }
331
332 $existing_skus = $wpdb->get_col(
333 $wpdb->prepare(
334 "SELECT lookup.sku
335 FROM {$wpdb->posts} as posts
336 INNER JOIN {$wpdb->wc_product_meta_lookup} AS lookup ON posts.ID = lookup.product_id
337 WHERE posts.post_type IN ( 'product', 'product_variation' )
338 AND lookup.sku LIKE %s",
339 $wpdb->esc_like( $root_sku ) . '%'
340 )
341 );
342
343 // The sku is already unique!
344 if ( empty( $existing_skus ) ) {
345 $product->set_sku( $root_sku );
346 return;
347 }
348
349 // Find the maximum suffix so we can ensure uniqueness.
350 $max_suffix = 0;
351 foreach ( $existing_skus as $existing_sku ) {
352 // Pull a numerical suffix off the sku after the last hyphen.
353 $suffix = intval( substr( $existing_sku, strrpos( $existing_sku, '-', -1 ) + 1 ) );
354 if ( $suffix > $max_suffix ) {
355 $max_suffix = $suffix;
356 }
357 }
358
359 // We set a limit of SKUs to try in order to avoid infinite loops.
360 $limit = $max_suffix + 100;
361 $product_id = $product->get_id();
362
363 while ( $max_suffix < $limit ) {
364 $new_sku = $root_sku . '-' . ( $max_suffix + 1 );
365
366 /**
367 * Gives plugins an opportunity to verify SKU uniqueness themselves. Filter added to keep backwards
368 * compatibility with `wc_product_has_unique_sku()`.
369 * See: https://github.com/woocommerce/woocommerce/pull/62628
370 *
371 * @since 10.5.0
372 *
373 * @param bool|null $has_unique_sku Set to a boolean value to short-circuit the default SKU check.
374 * @param int $product_id The ID of the current product.
375 * @param string $sku The SKU to check for uniqueness.
376 */
377 $pre_has_unique_sku = apply_filters( 'wc_product_pre_has_unique_sku', true, $product_id, $new_sku );
378
379 if ( $pre_has_unique_sku ) {
380 /**
381 * Gives plugins an opportunity to verify SKU uniqueness themselves. Filter added to keep backwards
382 * compatibility with `wc_product_has_unique_sku()`.
383 * See: https://github.com/woocommerce/woocommerce/pull/62628
384 *
385 * @since 10.5.0
386 *
387 * @param bool|null $sku_found Set to a boolean value to short-circuit the default SKU check.
388 * @param int $product_id The ID of the current product.
389 * @param string $sku The SKU to check for uniqueness.
390 */
391 $sku_found = apply_filters( 'wc_product_has_unique_sku', false, $product_id, $new_sku );
392
393 if ( ! $sku_found ) {
394 $product->set_sku( $new_sku );
395 return;
396 }
397 }
398 ++$max_suffix;
399 }
400 }
401 }
402
403 return new WC_Admin_Duplicate_Product();
404