WC_CLI_Coupon::delete()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
eloc 11
nc 6
nop 2
1
<?php
2
3
/**
4
 * Manage Coupons.
5
 *
6
 * @since    2.5.0
7
 * @package  WooCommerce/CLI
8
 * @category CLI
9
 * @author   WooThemes
10
 */
11
class WC_CLI_Coupon extends WC_CLI_Command {
12
13
	/**
14
	 * Create a coupon.
15
	 *
16
	 * ## OPTIONS
17
	 *
18
	 * [--<field>=<value>]
19
	 * : Associative args for the new coupon.
20
	 *
21
	 * [--porcelain]
22
	 * : Outputs just the new coupon id.
23
	 *
24
	 * ## AVAILABLE FIELDS
25
	 *
26
	 * These fields are available for create command:
27
	 *
28
	 * * code
29
	 * * type
30
	 * * amount
31
	 * * description
32
	 * * expiry_date
33
	 * * individual_use
34
	 * * product_ids
35
	 * * exclude_product_ids
36
	 * * usage_limit
37
	 * * usage_limit_per_user
38
	 * * limit_usage_to_x_items
39
	 * * usage_count
40
	 * * enable_free_shipping
41
	 * * product_category_ids
42
	 * * exclude_product_category_ids
43
	 * * minimum_amount
44
	 * * maximum_amount
45
	 * * customer_emails
46
	 *
47
	 * ## EXAMPLES
48
	 *
49
	 *     wp wc coupon create --code=new-coupon --type=percent
50
	 *
51
	 */
52
	public function create( $__, $assoc_args ) {
53
		global $wpdb;
54
55
		try {
56
			$porcelain = isset( $assoc_args['porcelain'] );
57
			unset( $assoc_args['porcelain'] );
58
59
			$assoc_args = apply_filters( 'woocommerce_cli_create_coupon_data', $assoc_args );
60
61
			// Check if coupon code is specified.
62 View Code Duplication
			if ( ! isset( $assoc_args['code'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
				throw new WC_CLI_Exception( 'woocommerce_cli_missing_coupon_code', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'code' ) );
64
			}
65
66
			$coupon_code = apply_filters( 'woocommerce_coupon_code', $assoc_args['code'] );
67
68
			// Check for duplicate coupon codes.
69
			$coupon_found = $wpdb->get_var( $wpdb->prepare( "
70
				SELECT $wpdb->posts.ID
71
				FROM $wpdb->posts
72
				WHERE $wpdb->posts.post_type = 'shop_coupon'
73
				AND $wpdb->posts.post_status = 'publish'
74
				AND $wpdb->posts.post_title = '%s'
75
			 ", $coupon_code ) );
76
77
			if ( $coupon_found ) {
78
				throw new WC_CLI_Exception( 'woocommerce_cli_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ) );
79
			}
80
81
			$defaults = array(
82
				'type'                         => 'fixed_cart',
83
				'amount'                       => 0,
84
				'individual_use'               => false,
85
				'product_ids'                  => array(),
86
				'exclude_product_ids'          => array(),
87
				'usage_limit'                  => '',
88
				'usage_limit_per_user'         => '',
89
				'limit_usage_to_x_items'       => '',
90
				'usage_count'                  => '',
91
				'expiry_date'                  => '',
92
				'enable_free_shipping'         => false,
93
				'product_category_ids'         => array(),
94
				'exclude_product_category_ids' => array(),
95
				'exclude_sale_items'           => false,
96
				'minimum_amount'               => '',
97
				'maximum_amount'               => '',
98
				'customer_emails'              => array(),
99
				'description'                  => ''
100
			);
101
102
			$coupon_data = wp_parse_args( $assoc_args, $defaults );
103
104
			// Validate coupon types
105
			if ( ! in_array( wc_clean( $coupon_data['type'] ), array_keys( wc_get_coupon_types() ) ) ) {
106
				throw new WC_CLI_Exception( 'woocommerce_cli_invalid_coupon_type', sprintf( __( 'Invalid coupon type - the coupon type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_coupon_types() ) ) ) );
107
			}
108
109
			$new_coupon = array(
110
				'post_title'   => $coupon_code,
111
				'post_content' => '',
112
				'post_status'  => 'publish',
113
				'post_author'  => get_current_user_id(),
114
				'post_type'    => 'shop_coupon',
115
				'post_excerpt' => $coupon_data['description']
116
	 		);
117
118
			$id = wp_insert_post( $new_coupon, $wp_error = false );
119
120
			if ( is_wp_error( $id ) ) {
121
				throw new WC_CLI_Exception( 'woocommerce_cli_cannot_create_coupon', $id->get_error_message() );
122
			}
123
124
			// Set coupon meta
125
			update_post_meta( $id, 'discount_type', $coupon_data['type'] );
126
			update_post_meta( $id, 'coupon_amount', wc_format_decimal( $coupon_data['amount'] ) );
127
			update_post_meta( $id, 'individual_use', ( $this->is_true( $coupon_data['individual_use'] ) ) ? 'yes' : 'no' );
128
			update_post_meta( $id, 'product_ids', implode( ',', array_filter( array_map( 'intval', $coupon_data['product_ids'] ) ) ) );
129
			update_post_meta( $id, 'exclude_product_ids', implode( ',', array_filter( array_map( 'intval', $coupon_data['exclude_product_ids'] ) ) ) );
130
			update_post_meta( $id, 'usage_limit', absint( $coupon_data['usage_limit'] ) );
131
			update_post_meta( $id, 'usage_limit_per_user', absint( $coupon_data['usage_limit_per_user'] ) );
132
			update_post_meta( $id, 'limit_usage_to_x_items', absint( $coupon_data['limit_usage_to_x_items'] ) );
133
			update_post_meta( $id, 'usage_count', absint( $coupon_data['usage_count'] ) );
134
			update_post_meta( $id, 'expiry_date', $this->get_coupon_expiry_date( wc_clean( $coupon_data['expiry_date'] ) ) );
135
			update_post_meta( $id, 'free_shipping', ( $this->is_true( $coupon_data['enable_free_shipping'] ) ) ? 'yes' : 'no' );
136
			update_post_meta( $id, 'product_categories', array_filter( array_map( 'intval', $coupon_data['product_category_ids'] ) ) );
137
			update_post_meta( $id, 'exclude_product_categories', array_filter( array_map( 'intval', $coupon_data['exclude_product_category_ids'] ) ) );
138
			update_post_meta( $id, 'exclude_sale_items', ( $this->is_true( $coupon_data['exclude_sale_items'] ) ) ? 'yes' : 'no' );
139
			update_post_meta( $id, 'minimum_amount', wc_format_decimal( $coupon_data['minimum_amount'] ) );
140
			update_post_meta( $id, 'maximum_amount', wc_format_decimal( $coupon_data['maximum_amount'] ) );
141
			update_post_meta( $id, 'customer_email', array_filter( array_map( 'sanitize_email', $coupon_data['customer_emails'] ) ) );
142
143
			do_action( 'woocommerce_cli_create_coupon', $id, $coupon_data );
144
145
			if ( $porcelain ) {
146
				WP_CLI::line( $id );
147
			} else {
148
				WP_CLI::success( "Created coupon $id." );
149
			}
150
		} catch ( WC_CLI_Exception $e ) {
151
			WP_CLI::error( $e->getMessage() );
152
		}
153
	}
154
155
	/**
156
	 * Delete one or more coupons.
157
	 *
158
	 * ## OPTIONS
159
	 *
160
	 * <id>...
161
	 * : The coupon ID to delete.
162
	 *
163
	 * ## EXAMPLES
164
	 *
165
	 *     wp wc coupon delete 123
166
	 *
167
	 *     wp wc coupon delete $(wp wc coupon list --format=ids)
168
	 *
169
	 */
170
	public function delete( $args, $assoc_args ) {
171
		$exit_code = 0;
172
		foreach ( $this->get_many_coupons_from_ids_or_codes( $args, true ) as $coupon ) {
173
			do_action( 'woocommerce_cli_delete_coupon', $coupon->id );
174
			$r = wp_delete_post( $coupon->id, true );
175
176
			if ( $r ) {
177
				WP_CLI::success( "Deleted coupon {$coupon->id}." );
178
			} else {
179
				$exit_code += 1;
180
				WP_CLI::warning( "Failed deleting coupon {$coupon->id}." );
181
			}
182
		}
183
		exit( $exit_code ? 1 : 0 );
184
	}
185
186
	/**
187
	 * Get a coupon.
188
	 *
189
	 * ## OPTIONS
190
	 *
191
	 * <coupon>
192
	 * : Coupon ID or code
193
	 *
194
	 * [--field=<field>]
195
	 * : Instead of returning the whole coupon fields, returns the value of a single fields.
196
	 *
197
	 * [--fields=<fields>]
198
	 * : Get a specific subset of the coupon's fields.
199
	 *
200
	 * [--format=<format>]
201
	 * : Accepted values: table, json, csv. Default: table.
202
	 *
203
	 * ## AVAILABLE FIELDS
204
	 *
205
	 * These fields are available for get command:
206
	 *
207
	 * * id
208
	 * * code
209
	 * * type
210
	 * * amount
211
	 * * description
212
	 * * expiry_date
213
	 * * individual_use
214
	 * * product_ids
215
	 * * exclude_product_ids
216
	 * * usage_limit
217
	 * * usage_limit_per_user
218
	 * * limit_usage_to_x_items
219
	 * * usage_count
220
	 * * enable_free_shipping
221
	 * * product_category_ids
222
	 * * exclude_product_category_ids
223
	 * * minimum_amount
224
	 * * maximum_amount
225
	 * * customer_emails
226
	 *
227
	 * ## EXAMPLES
228
	 *
229
	 *     wp wc coupon get 123 --field=discount_type
230
	 *
231
	 *     wp wc coupon get disc50 --format=json > disc50.json
232
	 *
233
	 * @since 2.5.0
234
	 */
235
	public function get( $args, $assoc_args ) {
236
		global $wpdb;
237
238
		try {
239
			$coupon = $this->get_coupon_from_id_or_code( $args[0] );
240 View Code Duplication
			if ( ! $coupon ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
241
				throw new WC_CLI_Exception( 'woocommerce_cli_invalid_coupon', sprintf( __( 'Invalid coupon ID or code: %s', 'woocommerce' ), $args[0] ) );
242
			}
243
244
			$coupon_post = get_post( $coupon->id );
245
			$coupon_data = array(
246
				'id'                           => $coupon->id,
247
				'code'                         => $coupon->code,
248
				'type'                         => $coupon->type,
249
				'created_at'                   => $this->format_datetime( $coupon_post->post_date_gmt ),
250
				'updated_at'                   => $this->format_datetime( $coupon_post->post_modified_gmt ),
251
				'amount'                       => wc_format_decimal( $coupon->coupon_amount, 2 ),
252
				'individual_use'               => $coupon->individual_use,
253
				'product_ids'                  => $coupon_post->product_ids,
254
				'exclude_product_ids'          => $coupon_post->exclude_product_ids,
255
				'usage_limit'                  => ( ! empty( $coupon->usage_limit ) ) ? $coupon->usage_limit : null,
256
				'usage_limit_per_user'         => ( ! empty( $coupon->usage_limit_per_user ) ) ? $coupon->usage_limit_per_user : null,
257
				'limit_usage_to_x_items'       => (int) $coupon->limit_usage_to_x_items,
258
				'usage_count'                  => (int) $coupon->usage_count,
259
				'expiry_date'                  => ( ! empty( $coupon->expiry_date ) ) ? $this->format_datetime( $coupon->expiry_date ) : null,
260
				'enable_free_shipping'         => $coupon->free_shipping,
261
				'product_category_ids'         => implode( ', ', $coupon->product_categories ),
262
				'exclude_product_category_ids' => implode( ', ', $coupon->exclude_product_categories ),
263
				'exclude_sale_items'           => $coupon->exclude_sale_items,
264
				'minimum_amount'               => wc_format_decimal( $coupon->minimum_amount, 2 ),
265
				'maximum_amount'               => wc_format_decimal( $coupon->maximum_amount, 2 ),
266
				'customer_emails'              => implode( ', ', $coupon->customer_email ),
267
				'description'                  => $coupon_post->post_excerpt,
268
			);
269
270
			$coupon_data = apply_filters( 'woocommerce_cli_get_coupon', $coupon_data );
271
272
			if ( empty( $assoc_args['fields'] ) ) {
273
				$assoc_args['fields'] = array_keys( $coupon_data );
274
			}
275
276
			$formatter = $this->get_formatter( $assoc_args );
277
			$formatter->display_item( $coupon_data );
278
		} catch ( WC_CLI_Exception $e ) {
279
			WP_CLI::error( $e->getMessage() );
280
		}
281
	}
282
283
	/**
284
	 * List coupons.
285
	 *
286
	 * ## OPTIONS
287
	 *
288
	 * [--<field>=<value>]
289
	 * : Filter coupon based on coupon property.
290
	 *
291
	 * [--field=<field>]
292
	 * : Prints the value of a single field for each coupon.
293
	 *
294
	 * [--fields=<fields>]
295
	 * : Limit the output to specific coupon fields.
296
	 *
297
	 * [--format=<format>]
298
	 * : Acceptec values: table, csv, json, count, ids. Default: table.
299
	 *
300
	 * ## AVAILABLE FIELDS
301
	 *
302
	 * These fields will be displayed by default for each coupon:
303
	 *
304
	 * * id
305
	 * * code
306
	 * * type
307
	 * * amount
308
	 * * description
309
	 * * expiry_date
310
	 *
311
	 * These fields are optionally available:
312
	 *
313
	 * * individual_use
314
	 * * product_ids
315
	 * * exclude_product_ids
316
	 * * usage_limit
317
	 * * usage_limit_per_user
318
	 * * limit_usage_to_x_items
319
	 * * usage_count
320
	 * * free_shipping
321
	 * * product_category_ids
322
	 * * exclude_product_category_ids
323
	 * * exclude_sale_items
324
	 * * minimum_amount
325
	 * * maximum_amount
326
	 * * customer_emails
327
	 *
328
	 * Fields for filtering query result also available:
329
	 *
330
	 * * q              Filter coupons with search query.
331
	 * * in             Specify coupon IDs to retrieve.
332
	 * * not_in         Specify coupon IDs NOT to retrieve.
333
	 * * created_at_min Filter coupons created after this date.
334
	 * * created_at_max Filter coupons created before this date.
335
	 * * updated_at_min Filter coupons updated after this date.
336
	 * * updated_at_max Filter coupons updated before this date.
337
	 * * page           Page number.
338
	 * * offset         Number of coupon to displace or pass over.
339
	 * * order          Accepted values: ASC and DESC. Default: DESC.
340
	 * * orderby        Sort retrieved coupons by parameter. One or more options can be passed.
341
	 *
342
	 * ## EXAMPLES
343
	 *
344
	 *     wp wc coupon list
345
	 *
346
	 *     wp wc coupon list --field=id
347
	 *
348
	 *     wp wc coupon list --fields=id,code,type --format=json
349
	 *
350
	 * @since      2.5.0
351
	 * @subcommand list
352
	 */
353 View Code Duplication
	public function list_( $__, $assoc_args ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
354
		$query_args = $this->merge_wp_query_args( $this->get_list_query_args(), $assoc_args );
355
		$formatter  = $this->get_formatter( $assoc_args );
356
357
		if ( 'ids' === $formatter->format ) {
358
			$query_args['fields'] = 'ids';
359
			$query = new WP_Query( $query_args );
360
			echo implode( ' ', $query->posts );
361
		} else {
362
			$query = new WP_Query( $query_args );
363
			$items = $this->format_posts_to_items( $query->posts );
364
			$formatter->display_items( $items );
365
		}
366
	}
367
368
	/**
369
	 * Get coupon types.
370
	 *
371
	 * ## EXAMPLES
372
	 *
373
	 *     wp wc coupon types
374
	 *
375
	 * @since 2.5.0
376
	 */
377
	public function types( $__, $___ ) {
378
		$coupon_types = wc_get_coupon_types();
379
		foreach ( $coupon_types as $type => $label ) {
380
			WP_CLI::line( sprintf( '%s: %s', $label, $type ) );
381
		}
382
	}
383
384
	/**
385
	 * Update one or more coupons.
386
	 *
387
	 * ## OPTIONS
388
	 *
389
	 * <coupon>
390
	 * : The ID or code of the coupon to update.
391
	 *
392
	 * [--<field>=<value>]
393
	 * : One or more fields to update.
394
	 *
395
	 * ## AVAILABLE FIELDS
396
	 *
397
	 * These fields are available for update command:
398
	 *
399
	 * * code
400
	 * * type
401
	 * * amount
402
	 * * description
403
	 * * expiry_date
404
	 * * individual_use
405
	 * * product_ids
406
	 * * exclude_product_ids
407
	 * * usage_limit
408
	 * * usage_limit_per_user
409
	 * * limit_usage_to_x_items
410
	 * * usage_count
411
	 * * enable_free_shipping
412
	 * * product_category_ids
413
	 * * exclude_product_categories
414
	 * * exclude_product_category_ids
415
	 * * minimum_amount
416
	 * * maximum_amount
417
	 * * customer_emails
418
	 *
419
	 * ## EXAMPLES
420
	 *
421
	 *     wp wc coupon update 123 --amount=5
422
	 *
423
	 *     wp wc coupon update coupon-code --code=new-coupon-code
424
	 *
425
	 * @since 2.5.0
426
	 */
427
	public function update( $args, $assoc_args ) {
428
		try {
429
			$coupon = $this->get_coupon_from_id_or_code( $args[0] );
430 View Code Duplication
			if ( ! $coupon ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
431
				throw new WC_CLI_Exception( 'woocommerce_cli_invalid_coupon', sprintf( __( 'Invalid coupon ID or code: %s', 'woocommerce' ), $args[0] ) );
432
			}
433
434
			$id          = $coupon->id;
435
			$coupon_code = $coupon->code;
436
			$data        = apply_filters( 'woocommerce_cli_update_coupon_data', $assoc_args, $id );
437
			if ( isset( $data['code'] ) ) {
438
				global $wpdb;
439
440
				$coupon_code = apply_filters( 'woocommerce_coupon_code', $data['code'] );
441
442
				// Check for duplicate coupon codes
443
				$coupon_found = $wpdb->get_var( $wpdb->prepare( "
444
					SELECT $wpdb->posts.ID
445
					FROM $wpdb->posts
446
					WHERE $wpdb->posts.post_type = 'shop_coupon'
447
					AND $wpdb->posts.post_status = 'publish'
448
					AND $wpdb->posts.post_title = '%s'
449
					AND $wpdb->posts.ID != %s
450
				 ", $coupon_code, $id ) );
451
452
				if ( $coupon_found ) {
453
					throw new WC_CLI_Exception( 'woocommerce_cli_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ) );
454
				}
455
			}
456
457
			$id = wp_update_post( array( 'ID' => intval( $id ), 'post_title' => $coupon_code, 'post_excerpt' => isset( $data['description'] ) ? $data['description'] : '' ) );
458
			if ( 0 === $id ) {
459
				throw new WC_CLI_Exception( 'woocommerce_cli_cannot_update_coupon', __( 'Failed to update coupon', 'woocommerce' ) );
460
			}
461
462
			if ( isset( $data['type'] ) ) {
463
				// Validate coupon types.
464
				if ( ! in_array( wc_clean( $data['type'] ), array_keys( wc_get_coupon_types() ) ) ) {
465
					throw new WC_CLI_Exception( 'woocommerce_cli_invalid_coupon_type', sprintf( __( 'Invalid coupon type - the coupon type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_coupon_types() ) ) ) );
466
				}
467
				update_post_meta( $id, 'discount_type', $data['type'] );
468
			}
469
470
			if ( isset( $data['amount'] ) ) {
471
				update_post_meta( $id, 'coupon_amount', wc_format_decimal( $data['amount'] ) );
472
			}
473
474
			if ( isset( $data['individual_use'] ) ) {
475
				update_post_meta( $id, 'individual_use', ( $this->is_true( $data['individual_use'] ) ) ? 'yes' : 'no' );
476
			}
477
478 View Code Duplication
			if ( isset( $data['product_ids'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
479
				update_post_meta( $id, 'product_ids', implode( ',', array_filter( array_map( 'intval', $data['product_ids'] ) ) ) );
480
			}
481
482 View Code Duplication
			if ( isset( $data['exclude_product_ids'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
483
				update_post_meta( $id, 'exclude_product_ids', implode( ',', array_filter( array_map( 'intval', $data['exclude_product_ids'] ) ) ) );
484
			}
485
486
			if ( isset( $data['usage_limit'] ) ) {
487
				update_post_meta( $id, 'usage_limit', absint( $data['usage_limit'] ) );
488
			}
489
490
			if ( isset( $data['usage_limit_per_user'] ) ) {
491
				update_post_meta( $id, 'usage_limit_per_user', absint( $data['usage_limit_per_user'] ) );
492
			}
493
494
			if ( isset( $data['limit_usage_to_x_items'] ) ) {
495
				update_post_meta( $id, 'limit_usage_to_x_items', absint( $data['limit_usage_to_x_items'] ) );
496
			}
497
498
			if ( isset( $data['usage_count'] ) ) {
499
				update_post_meta( $id, 'usage_count', absint( $data['usage_count'] ) );
500
			}
501
502
			if ( isset( $data['expiry_date'] ) ) {
503
				update_post_meta( $id, 'expiry_date', $this->get_coupon_expiry_date( wc_clean( $data['expiry_date'] ) ) );
504
			}
505
506
			if ( isset( $data['enable_free_shipping'] ) ) {
507
				update_post_meta( $id, 'free_shipping', ( $this->is_true( $data['enable_free_shipping'] ) ) ? 'yes' : 'no' );
508
			}
509
510 View Code Duplication
			if ( isset( $data['product_category_ids'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
511
				update_post_meta( $id, 'product_categories', array_filter( array_map( 'intval', $data['product_category_ids'] ) ) );
512
			}
513
514 View Code Duplication
			if ( isset( $data['exclude_product_category_ids'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
515
				update_post_meta( $id, 'exclude_product_categories', array_filter( array_map( 'intval', $data['exclude_product_category_ids'] ) ) );
516
			}
517
518
			if ( isset( $data['exclude_sale_items'] ) ) {
519
				update_post_meta( $id, 'exclude_sale_items', ( $this->is_true( $data['exclude_sale_items'] ) ) ? 'yes' : 'no' );
520
			}
521
522
			if ( isset( $data['minimum_amount'] ) ) {
523
				update_post_meta( $id, 'minimum_amount', wc_format_decimal( $data['minimum_amount'] ) );
524
			}
525
526
			if ( isset( $data['maximum_amount'] ) ) {
527
				update_post_meta( $id, 'maximum_amount', wc_format_decimal( $data['maximum_amount'] ) );
528
			}
529
530 View Code Duplication
			if ( isset( $data['customer_emails'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
531
				update_post_meta( $id, 'customer_email', array_filter( array_map( 'sanitize_email', $data['customer_emails'] ) ) );
532
			}
533
534
			do_action( 'woocommerce_cli_update_coupon', $id, $data );
535
536
			WP_CLI::success( "Updated coupon $id." );
537
		} catch ( WC_CLI_Exception $e ) {
538
			WP_CLI::error( $e->getMessage() );
539
		}
540
	}
541
542
	/**
543
	 * Get query args for list subcommand.
544
	 *
545
	 * @since  2.5.0
546
	 * @return array
547
	 */
548
	protected function get_list_query_args() {
549
		return array(
550
			'post_type'      => 'shop_coupon',
551
			'post_status'    => 'publish',
552
			'posts_per_page' => -1,
553
			'order'          => 'DESC',
554
		);
555
	}
556
557
	/**
558
	 * Get default format fields that will be used in `list` and `get` subcommands.
559
	 *
560
	 * @since  2.5.0
561
	 * @return string
562
	 */
563
	protected function get_default_format_fields() {
564
		return 'id,code,type,amount,description,expiry_date';
565
	}
566
567
	/**
568
	 * Format posts from WP_Query result to items in which each item contain
569
	 * common properties of item, for instance `post_title` will be `code`.
570
	 *
571
	 * @since  2.5.0
572
	 * @param  array $posts Array of post
573
	 * @return array Items
574
	 */
575
	protected function format_posts_to_items( $posts ) {
576
		$items = array();
577
		foreach ( $posts as $post ) {
578
			$items[] = array(
579
				'id'                           => $post->ID,
580
				'code'                         => $post->post_title,
581
				'type'                         => $post->discount_type,
582
				'created_at'                   => $this->format_datetime( $post->post_date_gmt ),
583
				'updated_at'                   => $this->format_datetime( $post->post_modified_gmt ),
584
				'amount'                       => wc_format_decimal( $post->coupon_amount, 2 ),
585
				'individual_use'               => $post->individual_use,
586
				'product_ids'                  => $post->product_ids,
587
				'exclude_product_ids'          => $post->exclude_product_ids,
588
				'usage_limit'                  => ( ! empty( $post->usage_limit ) ) ? $post->usage_limit : null,
589
				'usage_limit_per_user'         => ( ! empty( $post->usage_limit_per_user ) ) ? $post->usage_limit_per_user : null,
590
				'limit_usage_to_x_items'       => (int) $post->limit_usage_to_x_items,
591
				'usage_count'                  => (int) $post->usage_count,
592
				'expiry_date'                  => ( ! empty( $post->expiry_date ) ) ? $this->format_datetime( $post->expiry_date ) : null,
593
				'free_shipping'                => $post->free_shipping,
594
				'product_category_ids'         => implode( ', ', is_array( $post->product_categories ) ? $post->product_categories : array() ),
595
				'exclude_product_category_ids' => implode( ', ', is_array( $post->exclude_product_categories ) ? $post->exclude_product_categories : array() ),
596
				'exclude_sale_items'           => $post->exclude_sale_items,
597
				'minimum_amount'               => wc_format_decimal( $post->minimum_amount, 2 ),
598
				'maximum_amount'               => wc_format_decimal( $post->maximum_amount, 2 ),
599
				'customer_emails'              => implode( ', ', is_array( $post->customer_email ) ? $post->customer_email : array() ),
600
				'description'                  => $post->post_excerpt,
601
			);
602
		}
603
604
		return $items;
605
	}
606
607
	/**
608
	 * Get expiry_date format before saved into DB.
609
	 *
610
	 * @since  2.5.0
611
	 * @param  string $expiry_date
612
	 * @return string
613
	 */
614
	protected function get_coupon_expiry_date( $expiry_date ) {
615
		if ( '' !== $expiry_date ) {
616
			return date( 'Y-m-d', strtotime( $expiry_date ) );
617
		}
618
619
		return '';
620
	}
621
622
	/**
623
	 * Get coupon from coupon's ID or code.
624
	 *
625
	 * @since  2.5.0
626
	 * @param  int|string $coupon_id_or_code Coupon's ID or code
627
	 * @param  bool       $display_warning   Display warning if ID or code is invalid. Default false.
628
	 * @return WC_Coupon
629
	 */
630
	protected function get_coupon_from_id_or_code( $coupon_id_or_code, $display_warning = false ) {
631
		global $wpdb;
632
633
		$code = $wpdb->get_var( $wpdb->prepare( "SELECT post_title FROM $wpdb->posts WHERE (id = %s OR post_title = %s) AND post_type = 'shop_coupon' AND post_status = 'publish' LIMIT 1", $coupon_id_or_code, $coupon_id_or_code ) );
634
		if ( ! $code ) {
635
			if ( $display_warning ) {
636
				WP_CLI::warning( "Invalid coupon ID or code $coupon_id_or_code" );
637
			}
638
			return null;
639
		}
640
641
		return new WC_Coupon( $code );
642
	}
643
644
	/**
645
	 * Get coupon from coupon's ID or code.
646
	 *
647
	 * @since  2.5.0
648
	 * @param  array     $args            Coupon's IDs or codes
649
	 * @param  bool      $display_warning Display warning if ID or code is invalid. Default false.
650
	 * @return WC_Coupon
651
	 */
652
	protected function get_many_coupons_from_ids_or_codes( $args, $display_warning = false ) {
653
		$coupons = array();
654
655
		foreach ( $args as $arg ) {
656
			$code = $this->get_coupon_from_id_or_code( $arg, $display_warning );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $code is correct as $this->get_coupon_from_i...$arg, $display_warning) (which targets WC_CLI_Coupon::get_coupon_from_id_or_code()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
657
			if ( $code ) {
658
				$coupons[] = $code;
659
			}
660
		}
661
662
		return $coupons;
663
	}
664
}
665