-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli-command-loader.php
54 lines (35 loc) · 1.75 KB
/
cli-command-loader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace WooSubscriptions;
use WP_CLI, WP_CLI_Command;
if( class_exists( 'WP_CLI_Command' ) ) {
class Commands extends WP_CLI_Command {
public function delete( $args, $assoc_args ) {
$product_id = isset( $assoc_args['id'] ) ? $assoc_args['id'] : false;
// $deleted_subscriptions = array();
// $upload_dir = wp_upload_dir();
if( ! $product_id ) {
WP_CLI::error( 'Invalid arguments. Please provide product ID, ex. --id={id}' );
}
$args = array(
'limit' => isset( $assoc_args['limit'] ) ? $assoc_args['limit'] : '10'
);
// create progress bar
$progress = \WP_CLI\Utils\make_progress_bar( 'Deleting subscriptions for product ID ' . $product_id, 0 );
$subscriptions = wcs_get_subscriptions_for_product( $product_id, 'ids', $args );
// $handle = fopen( $upload_dir['basedir'] . "/deleted_subscriptions.txt","wb" );
foreach( $subscriptions as $subscription_id ) {
// Run post delete command to delete subscription. --force is used as this post type does not support being sent to trash.
WP_CLI::runcommand( 'post delete ' . $subscription_id . ' --force' );
// fwrite( $handle, 'Deleted subscriptions with ID ' . $subscription_id . PHP_EOL );
// +1 for progress
$progress->tick();
}
// fclose( $handle );
// progress finished
$progress->finish();
// success message
WP_CLI::success( $args['limit'] . ' Subscriptions deleted!' );
}
}
WP_CLI::add_command( 'woo-subscription', 'WooSubscriptions\Commands' );
}