Passed
Push — main ( aaef5c...e4c121 )
by TARIQ
71:39
created
packages/action-scheduler/classes/ActionScheduler_Compatibility.php 2 patches
Indentation   +87 added lines, -87 removed lines patch added patch discarded remove patch
@@ -5,105 +5,105 @@
 block discarded – undo
5 5
  */
6 6
 class ActionScheduler_Compatibility {
7 7
 
8
-	/**
9
-	 * Converts a shorthand byte value to an integer byte value.
10
-	 *
11
-	 * Wrapper for wp_convert_hr_to_bytes(), moved to load.php in WordPress 4.6 from media.php
12
-	 *
13
-	 * @link https://secure.php.net/manual/en/function.ini-get.php
14
-	 * @link https://secure.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
15
-	 *
16
-	 * @param string $value A (PHP ini) byte value, either shorthand or ordinary.
17
-	 * @return int An integer byte value.
18
-	 */
19
-	public static function convert_hr_to_bytes( $value ) {
20
-		if ( function_exists( 'wp_convert_hr_to_bytes' ) ) {
21
-			return wp_convert_hr_to_bytes( $value );
22
-		}
8
+    /**
9
+     * Converts a shorthand byte value to an integer byte value.
10
+     *
11
+     * Wrapper for wp_convert_hr_to_bytes(), moved to load.php in WordPress 4.6 from media.php
12
+     *
13
+     * @link https://secure.php.net/manual/en/function.ini-get.php
14
+     * @link https://secure.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
15
+     *
16
+     * @param string $value A (PHP ini) byte value, either shorthand or ordinary.
17
+     * @return int An integer byte value.
18
+     */
19
+    public static function convert_hr_to_bytes( $value ) {
20
+        if ( function_exists( 'wp_convert_hr_to_bytes' ) ) {
21
+            return wp_convert_hr_to_bytes( $value );
22
+        }
23 23
 
24
-		$value = strtolower( trim( $value ) );
25
-		$bytes = (int) $value;
24
+        $value = strtolower( trim( $value ) );
25
+        $bytes = (int) $value;
26 26
 
27
-		if ( false !== strpos( $value, 'g' ) ) {
28
-			$bytes *= GB_IN_BYTES;
29
-		} elseif ( false !== strpos( $value, 'm' ) ) {
30
-			$bytes *= MB_IN_BYTES;
31
-		} elseif ( false !== strpos( $value, 'k' ) ) {
32
-			$bytes *= KB_IN_BYTES;
33
-		}
27
+        if ( false !== strpos( $value, 'g' ) ) {
28
+            $bytes *= GB_IN_BYTES;
29
+        } elseif ( false !== strpos( $value, 'm' ) ) {
30
+            $bytes *= MB_IN_BYTES;
31
+        } elseif ( false !== strpos( $value, 'k' ) ) {
32
+            $bytes *= KB_IN_BYTES;
33
+        }
34 34
 
35
-		// Deal with large (float) values which run into the maximum integer size.
36
-		return min( $bytes, PHP_INT_MAX );
37
-	}
35
+        // Deal with large (float) values which run into the maximum integer size.
36
+        return min( $bytes, PHP_INT_MAX );
37
+    }
38 38
 
39
-	/**
40
-	 * Attempts to raise the PHP memory limit for memory intensive processes.
41
-	 *
42
-	 * Only allows raising the existing limit and prevents lowering it.
43
-	 *
44
-	 * Wrapper for wp_raise_memory_limit(), added in WordPress v4.6.0
45
-	 *
46
-	 * @return bool|int|string The limit that was set or false on failure.
47
-	 */
48
-	public static function raise_memory_limit() {
49
-		if ( function_exists( 'wp_raise_memory_limit' ) ) {
50
-			return wp_raise_memory_limit( 'admin' );
51
-		}
39
+    /**
40
+     * Attempts to raise the PHP memory limit for memory intensive processes.
41
+     *
42
+     * Only allows raising the existing limit and prevents lowering it.
43
+     *
44
+     * Wrapper for wp_raise_memory_limit(), added in WordPress v4.6.0
45
+     *
46
+     * @return bool|int|string The limit that was set or false on failure.
47
+     */
48
+    public static function raise_memory_limit() {
49
+        if ( function_exists( 'wp_raise_memory_limit' ) ) {
50
+            return wp_raise_memory_limit( 'admin' );
51
+        }
52 52
 
53
-		$current_limit     = @ini_get( 'memory_limit' );
54
-		$current_limit_int = self::convert_hr_to_bytes( $current_limit );
53
+        $current_limit     = @ini_get( 'memory_limit' );
54
+        $current_limit_int = self::convert_hr_to_bytes( $current_limit );
55 55
 
56
-		if ( -1 === $current_limit_int ) {
57
-			return false;
58
-		}
56
+        if ( -1 === $current_limit_int ) {
57
+            return false;
58
+        }
59 59
 
60
-		$wp_max_limit       = WP_MAX_MEMORY_LIMIT;
61
-		$wp_max_limit_int   = self::convert_hr_to_bytes( $wp_max_limit );
62
-		$filtered_limit     = apply_filters( 'admin_memory_limit', $wp_max_limit );
63
-		$filtered_limit_int = self::convert_hr_to_bytes( $filtered_limit );
60
+        $wp_max_limit       = WP_MAX_MEMORY_LIMIT;
61
+        $wp_max_limit_int   = self::convert_hr_to_bytes( $wp_max_limit );
62
+        $filtered_limit     = apply_filters( 'admin_memory_limit', $wp_max_limit );
63
+        $filtered_limit_int = self::convert_hr_to_bytes( $filtered_limit );
64 64
 
65
-		if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) {
66
-			if ( false !== @ini_set( 'memory_limit', $filtered_limit ) ) {
67
-				return $filtered_limit;
68
-			} else {
69
-				return false;
70
-			}
71
-		} elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) {
72
-			if ( false !== @ini_set( 'memory_limit', $wp_max_limit ) ) {
73
-				return $wp_max_limit;
74
-			} else {
75
-				return false;
76
-			}
77
-		}
78
-		return false;
79
-	}
65
+        if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) {
66
+            if ( false !== @ini_set( 'memory_limit', $filtered_limit ) ) {
67
+                return $filtered_limit;
68
+            } else {
69
+                return false;
70
+            }
71
+        } elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) {
72
+            if ( false !== @ini_set( 'memory_limit', $wp_max_limit ) ) {
73
+                return $wp_max_limit;
74
+            } else {
75
+                return false;
76
+            }
77
+        }
78
+        return false;
79
+    }
80 80
 
81
-	/**
82
-	 * Attempts to raise the PHP timeout for time intensive processes.
83
-	 *
84
-	 * Only allows raising the existing limit and prevents lowering it. Wrapper for wc_set_time_limit(), when available.
85
-	 *
86
-	 * @param int $limit The time limit in seconds.
87
-	 */
88
-	public static function raise_time_limit( $limit = 0 ) {
89
-		$limit = (int) $limit;
90
-		$max_execution_time = (int) ini_get( 'max_execution_time' );
81
+    /**
82
+     * Attempts to raise the PHP timeout for time intensive processes.
83
+     *
84
+     * Only allows raising the existing limit and prevents lowering it. Wrapper for wc_set_time_limit(), when available.
85
+     *
86
+     * @param int $limit The time limit in seconds.
87
+     */
88
+    public static function raise_time_limit( $limit = 0 ) {
89
+        $limit = (int) $limit;
90
+        $max_execution_time = (int) ini_get( 'max_execution_time' );
91 91
 
92
-		/*
92
+        /*
93 93
 		 * If the max execution time is already unlimited (zero), or if it exceeds or is equal to the proposed
94 94
 		 * limit, there is no reason for us to make further changes (we never want to lower it).
95 95
 		 */
96
-		if (
97
-			0 === $max_execution_time
98
-			|| ( $max_execution_time >= $limit && $limit !== 0 )
99
-		) {
100
-			return;
101
-		}
96
+        if (
97
+            0 === $max_execution_time
98
+            || ( $max_execution_time >= $limit && $limit !== 0 )
99
+        ) {
100
+            return;
101
+        }
102 102
 
103
-		if ( function_exists( 'wc_set_time_limit' ) ) {
104
-			wc_set_time_limit( $limit );
105
-		} elseif ( function_exists( 'set_time_limit' ) && false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) { // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_modeDeprecatedRemoved
106
-			@set_time_limit( $limit ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
107
-		}
108
-	}
103
+        if ( function_exists( 'wc_set_time_limit' ) ) {
104
+            wc_set_time_limit( $limit );
105
+        } elseif ( function_exists( 'set_time_limit' ) && false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) { // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_modeDeprecatedRemoved
106
+            @set_time_limit( $limit ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
107
+        }
108
+    }
109 109
 }
Please login to merge, or discard this patch.
Spacing   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -16,24 +16,24 @@  discard block
 block discarded – undo
16 16
 	 * @param string $value A (PHP ini) byte value, either shorthand or ordinary.
17 17
 	 * @return int An integer byte value.
18 18
 	 */
19
-	public static function convert_hr_to_bytes( $value ) {
20
-		if ( function_exists( 'wp_convert_hr_to_bytes' ) ) {
21
-			return wp_convert_hr_to_bytes( $value );
19
+	public static function convert_hr_to_bytes($value) {
20
+		if (function_exists('wp_convert_hr_to_bytes')) {
21
+			return wp_convert_hr_to_bytes($value);
22 22
 		}
23 23
 
24
-		$value = strtolower( trim( $value ) );
24
+		$value = strtolower(trim($value));
25 25
 		$bytes = (int) $value;
26 26
 
27
-		if ( false !== strpos( $value, 'g' ) ) {
27
+		if (false !== strpos($value, 'g')) {
28 28
 			$bytes *= GB_IN_BYTES;
29
-		} elseif ( false !== strpos( $value, 'm' ) ) {
29
+		} elseif (false !== strpos($value, 'm')) {
30 30
 			$bytes *= MB_IN_BYTES;
31
-		} elseif ( false !== strpos( $value, 'k' ) ) {
31
+		} elseif (false !== strpos($value, 'k')) {
32 32
 			$bytes *= KB_IN_BYTES;
33 33
 		}
34 34
 
35 35
 		// Deal with large (float) values which run into the maximum integer size.
36
-		return min( $bytes, PHP_INT_MAX );
36
+		return min($bytes, PHP_INT_MAX);
37 37
 	}
38 38
 
39 39
 	/**
@@ -46,30 +46,30 @@  discard block
 block discarded – undo
46 46
 	 * @return bool|int|string The limit that was set or false on failure.
47 47
 	 */
48 48
 	public static function raise_memory_limit() {
49
-		if ( function_exists( 'wp_raise_memory_limit' ) ) {
50
-			return wp_raise_memory_limit( 'admin' );
49
+		if (function_exists('wp_raise_memory_limit')) {
50
+			return wp_raise_memory_limit('admin');
51 51
 		}
52 52
 
53
-		$current_limit     = @ini_get( 'memory_limit' );
54
-		$current_limit_int = self::convert_hr_to_bytes( $current_limit );
53
+		$current_limit     = @ini_get('memory_limit');
54
+		$current_limit_int = self::convert_hr_to_bytes($current_limit);
55 55
 
56 56
 		if ( -1 === $current_limit_int ) {
57 57
 			return false;
58 58
 		}
59 59
 
60 60
 		$wp_max_limit       = WP_MAX_MEMORY_LIMIT;
61
-		$wp_max_limit_int   = self::convert_hr_to_bytes( $wp_max_limit );
62
-		$filtered_limit     = apply_filters( 'admin_memory_limit', $wp_max_limit );
63
-		$filtered_limit_int = self::convert_hr_to_bytes( $filtered_limit );
61
+		$wp_max_limit_int   = self::convert_hr_to_bytes($wp_max_limit);
62
+		$filtered_limit     = apply_filters('admin_memory_limit', $wp_max_limit);
63
+		$filtered_limit_int = self::convert_hr_to_bytes($filtered_limit);
64 64
 
65
-		if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) {
66
-			if ( false !== @ini_set( 'memory_limit', $filtered_limit ) ) {
65
+		if ( -1 === $filtered_limit_int || ($filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int) ) {
66
+			if (false !== @ini_set('memory_limit', $filtered_limit)) {
67 67
 				return $filtered_limit;
68 68
 			} else {
69 69
 				return false;
70 70
 			}
71 71
 		} elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) {
72
-			if ( false !== @ini_set( 'memory_limit', $wp_max_limit ) ) {
72
+			if (false !== @ini_set('memory_limit', $wp_max_limit)) {
73 73
 				return $wp_max_limit;
74 74
 			} else {
75 75
 				return false;
@@ -85,9 +85,9 @@  discard block
 block discarded – undo
85 85
 	 *
86 86
 	 * @param int $limit The time limit in seconds.
87 87
 	 */
88
-	public static function raise_time_limit( $limit = 0 ) {
88
+	public static function raise_time_limit($limit = 0) {
89 89
 		$limit = (int) $limit;
90
-		$max_execution_time = (int) ini_get( 'max_execution_time' );
90
+		$max_execution_time = (int) ini_get('max_execution_time');
91 91
 
92 92
 		/*
93 93
 		 * If the max execution time is already unlimited (zero), or if it exceeds or is equal to the proposed
@@ -95,15 +95,15 @@  discard block
 block discarded – undo
95 95
 		 */
96 96
 		if (
97 97
 			0 === $max_execution_time
98
-			|| ( $max_execution_time >= $limit && $limit !== 0 )
98
+			|| ($max_execution_time >= $limit && $limit !== 0)
99 99
 		) {
100 100
 			return;
101 101
 		}
102 102
 
103
-		if ( function_exists( 'wc_set_time_limit' ) ) {
104
-			wc_set_time_limit( $limit );
105
-		} elseif ( function_exists( 'set_time_limit' ) && false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) { // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_modeDeprecatedRemoved
106
-			@set_time_limit( $limit ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
103
+		if (function_exists('wc_set_time_limit')) {
104
+			wc_set_time_limit($limit);
105
+		} elseif (function_exists('set_time_limit') && false === strpos(ini_get('disable_functions'), 'set_time_limit') && !ini_get('safe_mode')) { // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_modeDeprecatedRemoved
106
+			@set_time_limit($limit); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
107 107
 		}
108 108
 	}
109 109
 }
Please login to merge, or discard this patch.
action-scheduler/classes/WP_CLI/ActionScheduler_WPCLI_QueueRunner.php 2 patches
Indentation   +185 added lines, -185 removed lines patch added patch discarded remove patch
@@ -9,189 +9,189 @@
 block discarded – undo
9 9
  */
10 10
 class ActionScheduler_WPCLI_QueueRunner extends ActionScheduler_Abstract_QueueRunner {
11 11
 
12
-	/** @var array */
13
-	protected $actions;
14
-
15
-	/** @var  ActionScheduler_ActionClaim */
16
-	protected $claim;
17
-
18
-	/** @var \cli\progress\Bar */
19
-	protected $progress_bar;
20
-
21
-	/**
22
-	 * ActionScheduler_WPCLI_QueueRunner constructor.
23
-	 *
24
-	 * @param ActionScheduler_Store             $store
25
-	 * @param ActionScheduler_FatalErrorMonitor $monitor
26
-	 * @param ActionScheduler_QueueCleaner      $cleaner
27
-	 *
28
-	 * @throws Exception When this is not run within WP CLI
29
-	 */
30
-	public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null ) {
31
-		if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) {
32
-			/* translators: %s php class name */
33
-			throw new Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'woocommerce' ), __CLASS__ ) );
34
-		}
35
-
36
-		parent::__construct( $store, $monitor, $cleaner );
37
-	}
38
-
39
-	/**
40
-	 * Set up the Queue before processing.
41
-	 *
42
-	 * @author Jeremy Pry
43
-	 *
44
-	 * @param int    $batch_size The batch size to process.
45
-	 * @param array  $hooks      The hooks being used to filter the actions claimed in this batch.
46
-	 * @param string $group      The group of actions to claim with this batch.
47
-	 * @param bool   $force      Whether to force running even with too many concurrent processes.
48
-	 *
49
-	 * @return int The number of actions that will be run.
50
-	 * @throws \WP_CLI\ExitException When there are too many concurrent batches.
51
-	 */
52
-	public function setup( $batch_size, $hooks = array(), $group = '', $force = false ) {
53
-		$this->run_cleanup();
54
-		$this->add_hooks();
55
-
56
-		// Check to make sure there aren't too many concurrent processes running.
57
-		if ( $this->has_maximum_concurrent_batches() ) {
58
-			if ( $force ) {
59
-				WP_CLI::warning( __( 'There are too many concurrent batches, but the run is forced to continue.', 'woocommerce' ) );
60
-			} else {
61
-				WP_CLI::error( __( 'There are too many concurrent batches.', 'woocommerce' ) );
62
-			}
63
-		}
64
-
65
-		// Stake a claim and store it.
66
-		$this->claim = $this->store->stake_claim( $batch_size, null, $hooks, $group );
67
-		$this->monitor->attach( $this->claim );
68
-		$this->actions = $this->claim->get_actions();
69
-
70
-		return count( $this->actions );
71
-	}
72
-
73
-	/**
74
-	 * Add our hooks to the appropriate actions.
75
-	 *
76
-	 * @author Jeremy Pry
77
-	 */
78
-	protected function add_hooks() {
79
-		add_action( 'action_scheduler_before_execute', array( $this, 'before_execute' ) );
80
-		add_action( 'action_scheduler_after_execute', array( $this, 'after_execute' ), 10, 2 );
81
-		add_action( 'action_scheduler_failed_execution', array( $this, 'action_failed' ), 10, 2 );
82
-	}
83
-
84
-	/**
85
-	 * Set up the WP CLI progress bar.
86
-	 *
87
-	 * @author Jeremy Pry
88
-	 */
89
-	protected function setup_progress_bar() {
90
-		$count              = count( $this->actions );
91
-		$this->progress_bar = new ProgressBar(
92
-			/* translators: %d: amount of actions */
93
-			sprintf( _n( 'Running %d action', 'Running %d actions', $count, 'woocommerce' ), number_format_i18n( $count ) ),
94
-			$count
95
-		);
96
-	}
97
-
98
-	/**
99
-	 * Process actions in the queue.
100
-	 *
101
-	 * @author Jeremy Pry
102
-	 *
103
-	 * @param string $context Optional runner context. Default 'WP CLI'.
104
-	 *
105
-	 * @return int The number of actions processed.
106
-	 */
107
-	public function run( $context = 'WP CLI' ) {
108
-		do_action( 'action_scheduler_before_process_queue' );
109
-		$this->setup_progress_bar();
110
-		foreach ( $this->actions as $action_id ) {
111
-			// Error if we lost the claim.
112
-			if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $this->claim->get_id() ) ) ) {
113
-				WP_CLI::warning( __( 'The claim has been lost. Aborting current batch.', 'woocommerce' ) );
114
-				break;
115
-			}
116
-
117
-			$this->process_action( $action_id, $context );
118
-			$this->progress_bar->tick();
119
-		}
120
-
121
-		$completed = $this->progress_bar->current();
122
-		$this->progress_bar->finish();
123
-		$this->store->release_claim( $this->claim );
124
-		do_action( 'action_scheduler_after_process_queue' );
125
-
126
-		return $completed;
127
-	}
128
-
129
-	/**
130
-	 * Handle WP CLI message when the action is starting.
131
-	 *
132
-	 * @author Jeremy Pry
133
-	 *
134
-	 * @param $action_id
135
-	 */
136
-	public function before_execute( $action_id ) {
137
-		/* translators: %s refers to the action ID */
138
-		WP_CLI::log( sprintf( __( 'Started processing action %s', 'woocommerce' ), $action_id ) );
139
-	}
140
-
141
-	/**
142
-	 * Handle WP CLI message when the action has completed.
143
-	 *
144
-	 * @author Jeremy Pry
145
-	 *
146
-	 * @param int $action_id
147
-	 * @param null|ActionScheduler_Action $action The instance of the action. Default to null for backward compatibility.
148
-	 */
149
-	public function after_execute( $action_id, $action = null ) {
150
-		// backward compatibility
151
-		if ( null === $action ) {
152
-			$action = $this->store->fetch_action( $action_id );
153
-		}
154
-		/* translators: 1: action ID 2: hook name */
155
-		WP_CLI::log( sprintf( __( 'Completed processing action %1$s with hook: %2$s', 'woocommerce' ), $action_id, $action->get_hook() ) );
156
-	}
157
-
158
-	/**
159
-	 * Handle WP CLI message when the action has failed.
160
-	 *
161
-	 * @author Jeremy Pry
162
-	 *
163
-	 * @param int       $action_id
164
-	 * @param Exception $exception
165
-	 * @throws \WP_CLI\ExitException With failure message.
166
-	 */
167
-	public function action_failed( $action_id, $exception ) {
168
-		WP_CLI::error(
169
-			/* translators: 1: action ID 2: exception message */
170
-			sprintf( __( 'Error processing action %1$s: %2$s', 'woocommerce' ), $action_id, $exception->getMessage() ),
171
-			false
172
-		);
173
-	}
174
-
175
-	/**
176
-	 * Sleep and help avoid hitting memory limit
177
-	 *
178
-	 * @param int $sleep_time Amount of seconds to sleep
179
-	 * @deprecated 3.0.0
180
-	 */
181
-	protected function stop_the_insanity( $sleep_time = 0 ) {
182
-		_deprecated_function( 'ActionScheduler_WPCLI_QueueRunner::stop_the_insanity', '3.0.0', 'ActionScheduler_DataController::free_memory' );
183
-
184
-		ActionScheduler_DataController::free_memory();
185
-	}
186
-
187
-	/**
188
-	 * Maybe trigger the stop_the_insanity() method to free up memory.
189
-	 */
190
-	protected function maybe_stop_the_insanity() {
191
-		// The value returned by progress_bar->current() might be padded. Remove padding, and convert to int.
192
-		$current_iteration = intval( trim( $this->progress_bar->current() ) );
193
-		if ( 0 === $current_iteration % 50 ) {
194
-			$this->stop_the_insanity();
195
-		}
196
-	}
12
+    /** @var array */
13
+    protected $actions;
14
+
15
+    /** @var  ActionScheduler_ActionClaim */
16
+    protected $claim;
17
+
18
+    /** @var \cli\progress\Bar */
19
+    protected $progress_bar;
20
+
21
+    /**
22
+     * ActionScheduler_WPCLI_QueueRunner constructor.
23
+     *
24
+     * @param ActionScheduler_Store             $store
25
+     * @param ActionScheduler_FatalErrorMonitor $monitor
26
+     * @param ActionScheduler_QueueCleaner      $cleaner
27
+     *
28
+     * @throws Exception When this is not run within WP CLI
29
+     */
30
+    public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null ) {
31
+        if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) {
32
+            /* translators: %s php class name */
33
+            throw new Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'woocommerce' ), __CLASS__ ) );
34
+        }
35
+
36
+        parent::__construct( $store, $monitor, $cleaner );
37
+    }
38
+
39
+    /**
40
+     * Set up the Queue before processing.
41
+     *
42
+     * @author Jeremy Pry
43
+     *
44
+     * @param int    $batch_size The batch size to process.
45
+     * @param array  $hooks      The hooks being used to filter the actions claimed in this batch.
46
+     * @param string $group      The group of actions to claim with this batch.
47
+     * @param bool   $force      Whether to force running even with too many concurrent processes.
48
+     *
49
+     * @return int The number of actions that will be run.
50
+     * @throws \WP_CLI\ExitException When there are too many concurrent batches.
51
+     */
52
+    public function setup( $batch_size, $hooks = array(), $group = '', $force = false ) {
53
+        $this->run_cleanup();
54
+        $this->add_hooks();
55
+
56
+        // Check to make sure there aren't too many concurrent processes running.
57
+        if ( $this->has_maximum_concurrent_batches() ) {
58
+            if ( $force ) {
59
+                WP_CLI::warning( __( 'There are too many concurrent batches, but the run is forced to continue.', 'woocommerce' ) );
60
+            } else {
61
+                WP_CLI::error( __( 'There are too many concurrent batches.', 'woocommerce' ) );
62
+            }
63
+        }
64
+
65
+        // Stake a claim and store it.
66
+        $this->claim = $this->store->stake_claim( $batch_size, null, $hooks, $group );
67
+        $this->monitor->attach( $this->claim );
68
+        $this->actions = $this->claim->get_actions();
69
+
70
+        return count( $this->actions );
71
+    }
72
+
73
+    /**
74
+     * Add our hooks to the appropriate actions.
75
+     *
76
+     * @author Jeremy Pry
77
+     */
78
+    protected function add_hooks() {
79
+        add_action( 'action_scheduler_before_execute', array( $this, 'before_execute' ) );
80
+        add_action( 'action_scheduler_after_execute', array( $this, 'after_execute' ), 10, 2 );
81
+        add_action( 'action_scheduler_failed_execution', array( $this, 'action_failed' ), 10, 2 );
82
+    }
83
+
84
+    /**
85
+     * Set up the WP CLI progress bar.
86
+     *
87
+     * @author Jeremy Pry
88
+     */
89
+    protected function setup_progress_bar() {
90
+        $count              = count( $this->actions );
91
+        $this->progress_bar = new ProgressBar(
92
+            /* translators: %d: amount of actions */
93
+            sprintf( _n( 'Running %d action', 'Running %d actions', $count, 'woocommerce' ), number_format_i18n( $count ) ),
94
+            $count
95
+        );
96
+    }
97
+
98
+    /**
99
+     * Process actions in the queue.
100
+     *
101
+     * @author Jeremy Pry
102
+     *
103
+     * @param string $context Optional runner context. Default 'WP CLI'.
104
+     *
105
+     * @return int The number of actions processed.
106
+     */
107
+    public function run( $context = 'WP CLI' ) {
108
+        do_action( 'action_scheduler_before_process_queue' );
109
+        $this->setup_progress_bar();
110
+        foreach ( $this->actions as $action_id ) {
111
+            // Error if we lost the claim.
112
+            if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $this->claim->get_id() ) ) ) {
113
+                WP_CLI::warning( __( 'The claim has been lost. Aborting current batch.', 'woocommerce' ) );
114
+                break;
115
+            }
116
+
117
+            $this->process_action( $action_id, $context );
118
+            $this->progress_bar->tick();
119
+        }
120
+
121
+        $completed = $this->progress_bar->current();
122
+        $this->progress_bar->finish();
123
+        $this->store->release_claim( $this->claim );
124
+        do_action( 'action_scheduler_after_process_queue' );
125
+
126
+        return $completed;
127
+    }
128
+
129
+    /**
130
+     * Handle WP CLI message when the action is starting.
131
+     *
132
+     * @author Jeremy Pry
133
+     *
134
+     * @param $action_id
135
+     */
136
+    public function before_execute( $action_id ) {
137
+        /* translators: %s refers to the action ID */
138
+        WP_CLI::log( sprintf( __( 'Started processing action %s', 'woocommerce' ), $action_id ) );
139
+    }
140
+
141
+    /**
142
+     * Handle WP CLI message when the action has completed.
143
+     *
144
+     * @author Jeremy Pry
145
+     *
146
+     * @param int $action_id
147
+     * @param null|ActionScheduler_Action $action The instance of the action. Default to null for backward compatibility.
148
+     */
149
+    public function after_execute( $action_id, $action = null ) {
150
+        // backward compatibility
151
+        if ( null === $action ) {
152
+            $action = $this->store->fetch_action( $action_id );
153
+        }
154
+        /* translators: 1: action ID 2: hook name */
155
+        WP_CLI::log( sprintf( __( 'Completed processing action %1$s with hook: %2$s', 'woocommerce' ), $action_id, $action->get_hook() ) );
156
+    }
157
+
158
+    /**
159
+     * Handle WP CLI message when the action has failed.
160
+     *
161
+     * @author Jeremy Pry
162
+     *
163
+     * @param int       $action_id
164
+     * @param Exception $exception
165
+     * @throws \WP_CLI\ExitException With failure message.
166
+     */
167
+    public function action_failed( $action_id, $exception ) {
168
+        WP_CLI::error(
169
+            /* translators: 1: action ID 2: exception message */
170
+            sprintf( __( 'Error processing action %1$s: %2$s', 'woocommerce' ), $action_id, $exception->getMessage() ),
171
+            false
172
+        );
173
+    }
174
+
175
+    /**
176
+     * Sleep and help avoid hitting memory limit
177
+     *
178
+     * @param int $sleep_time Amount of seconds to sleep
179
+     * @deprecated 3.0.0
180
+     */
181
+    protected function stop_the_insanity( $sleep_time = 0 ) {
182
+        _deprecated_function( 'ActionScheduler_WPCLI_QueueRunner::stop_the_insanity', '3.0.0', 'ActionScheduler_DataController::free_memory' );
183
+
184
+        ActionScheduler_DataController::free_memory();
185
+    }
186
+
187
+    /**
188
+     * Maybe trigger the stop_the_insanity() method to free up memory.
189
+     */
190
+    protected function maybe_stop_the_insanity() {
191
+        // The value returned by progress_bar->current() might be padded. Remove padding, and convert to int.
192
+        $current_iteration = intval( trim( $this->progress_bar->current() ) );
193
+        if ( 0 === $current_iteration % 50 ) {
194
+            $this->stop_the_insanity();
195
+        }
196
+    }
197 197
 }
Please login to merge, or discard this patch.
Spacing   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -27,13 +27,13 @@  discard block
 block discarded – undo
27 27
 	 *
28 28
 	 * @throws Exception When this is not run within WP CLI
29 29
 	 */
30
-	public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null ) {
31
-		if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) {
30
+	public function __construct(ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null) {
31
+		if (!(defined('WP_CLI') && WP_CLI)) {
32 32
 			/* translators: %s php class name */
33
-			throw new Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'woocommerce' ), __CLASS__ ) );
33
+			throw new Exception(sprintf(__('The %s class can only be run within WP CLI.', 'woocommerce'), __CLASS__));
34 34
 		}
35 35
 
36
-		parent::__construct( $store, $monitor, $cleaner );
36
+		parent::__construct($store, $monitor, $cleaner);
37 37
 	}
38 38
 
39 39
 	/**
@@ -49,25 +49,25 @@  discard block
 block discarded – undo
49 49
 	 * @return int The number of actions that will be run.
50 50
 	 * @throws \WP_CLI\ExitException When there are too many concurrent batches.
51 51
 	 */
52
-	public function setup( $batch_size, $hooks = array(), $group = '', $force = false ) {
52
+	public function setup($batch_size, $hooks = array(), $group = '', $force = false) {
53 53
 		$this->run_cleanup();
54 54
 		$this->add_hooks();
55 55
 
56 56
 		// Check to make sure there aren't too many concurrent processes running.
57
-		if ( $this->has_maximum_concurrent_batches() ) {
58
-			if ( $force ) {
59
-				WP_CLI::warning( __( 'There are too many concurrent batches, but the run is forced to continue.', 'woocommerce' ) );
57
+		if ($this->has_maximum_concurrent_batches()) {
58
+			if ($force) {
59
+				WP_CLI::warning(__('There are too many concurrent batches, but the run is forced to continue.', 'woocommerce'));
60 60
 			} else {
61
-				WP_CLI::error( __( 'There are too many concurrent batches.', 'woocommerce' ) );
61
+				WP_CLI::error(__('There are too many concurrent batches.', 'woocommerce'));
62 62
 			}
63 63
 		}
64 64
 
65 65
 		// Stake a claim and store it.
66
-		$this->claim = $this->store->stake_claim( $batch_size, null, $hooks, $group );
67
-		$this->monitor->attach( $this->claim );
66
+		$this->claim = $this->store->stake_claim($batch_size, null, $hooks, $group);
67
+		$this->monitor->attach($this->claim);
68 68
 		$this->actions = $this->claim->get_actions();
69 69
 
70
-		return count( $this->actions );
70
+		return count($this->actions);
71 71
 	}
72 72
 
73 73
 	/**
@@ -76,9 +76,9 @@  discard block
 block discarded – undo
76 76
 	 * @author Jeremy Pry
77 77
 	 */
78 78
 	protected function add_hooks() {
79
-		add_action( 'action_scheduler_before_execute', array( $this, 'before_execute' ) );
80
-		add_action( 'action_scheduler_after_execute', array( $this, 'after_execute' ), 10, 2 );
81
-		add_action( 'action_scheduler_failed_execution', array( $this, 'action_failed' ), 10, 2 );
79
+		add_action('action_scheduler_before_execute', array($this, 'before_execute'));
80
+		add_action('action_scheduler_after_execute', array($this, 'after_execute'), 10, 2);
81
+		add_action('action_scheduler_failed_execution', array($this, 'action_failed'), 10, 2);
82 82
 	}
83 83
 
84 84
 	/**
@@ -87,10 +87,10 @@  discard block
 block discarded – undo
87 87
 	 * @author Jeremy Pry
88 88
 	 */
89 89
 	protected function setup_progress_bar() {
90
-		$count              = count( $this->actions );
90
+		$count              = count($this->actions);
91 91
 		$this->progress_bar = new ProgressBar(
92 92
 			/* translators: %d: amount of actions */
93
-			sprintf( _n( 'Running %d action', 'Running %d actions', $count, 'woocommerce' ), number_format_i18n( $count ) ),
93
+			sprintf(_n('Running %d action', 'Running %d actions', $count, 'woocommerce'), number_format_i18n($count)),
94 94
 			$count
95 95
 		);
96 96
 	}
@@ -104,24 +104,24 @@  discard block
 block discarded – undo
104 104
 	 *
105 105
 	 * @return int The number of actions processed.
106 106
 	 */
107
-	public function run( $context = 'WP CLI' ) {
108
-		do_action( 'action_scheduler_before_process_queue' );
107
+	public function run($context = 'WP CLI') {
108
+		do_action('action_scheduler_before_process_queue');
109 109
 		$this->setup_progress_bar();
110
-		foreach ( $this->actions as $action_id ) {
110
+		foreach ($this->actions as $action_id) {
111 111
 			// Error if we lost the claim.
112
-			if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $this->claim->get_id() ) ) ) {
113
-				WP_CLI::warning( __( 'The claim has been lost. Aborting current batch.', 'woocommerce' ) );
112
+			if (!in_array($action_id, $this->store->find_actions_by_claim_id($this->claim->get_id()))) {
113
+				WP_CLI::warning(__('The claim has been lost. Aborting current batch.', 'woocommerce'));
114 114
 				break;
115 115
 			}
116 116
 
117
-			$this->process_action( $action_id, $context );
117
+			$this->process_action($action_id, $context);
118 118
 			$this->progress_bar->tick();
119 119
 		}
120 120
 
121 121
 		$completed = $this->progress_bar->current();
122 122
 		$this->progress_bar->finish();
123
-		$this->store->release_claim( $this->claim );
124
-		do_action( 'action_scheduler_after_process_queue' );
123
+		$this->store->release_claim($this->claim);
124
+		do_action('action_scheduler_after_process_queue');
125 125
 
126 126
 		return $completed;
127 127
 	}
@@ -133,9 +133,9 @@  discard block
 block discarded – undo
133 133
 	 *
134 134
 	 * @param $action_id
135 135
 	 */
136
-	public function before_execute( $action_id ) {
136
+	public function before_execute($action_id) {
137 137
 		/* translators: %s refers to the action ID */
138
-		WP_CLI::log( sprintf( __( 'Started processing action %s', 'woocommerce' ), $action_id ) );
138
+		WP_CLI::log(sprintf(__('Started processing action %s', 'woocommerce'), $action_id));
139 139
 	}
140 140
 
141 141
 	/**
@@ -146,13 +146,13 @@  discard block
 block discarded – undo
146 146
 	 * @param int $action_id
147 147
 	 * @param null|ActionScheduler_Action $action The instance of the action. Default to null for backward compatibility.
148 148
 	 */
149
-	public function after_execute( $action_id, $action = null ) {
149
+	public function after_execute($action_id, $action = null) {
150 150
 		// backward compatibility
151
-		if ( null === $action ) {
152
-			$action = $this->store->fetch_action( $action_id );
151
+		if (null === $action) {
152
+			$action = $this->store->fetch_action($action_id);
153 153
 		}
154 154
 		/* translators: 1: action ID 2: hook name */
155
-		WP_CLI::log( sprintf( __( 'Completed processing action %1$s with hook: %2$s', 'woocommerce' ), $action_id, $action->get_hook() ) );
155
+		WP_CLI::log(sprintf(__('Completed processing action %1$s with hook: %2$s', 'woocommerce'), $action_id, $action->get_hook()));
156 156
 	}
157 157
 
158 158
 	/**
@@ -164,10 +164,10 @@  discard block
 block discarded – undo
164 164
 	 * @param Exception $exception
165 165
 	 * @throws \WP_CLI\ExitException With failure message.
166 166
 	 */
167
-	public function action_failed( $action_id, $exception ) {
167
+	public function action_failed($action_id, $exception) {
168 168
 		WP_CLI::error(
169 169
 			/* translators: 1: action ID 2: exception message */
170
-			sprintf( __( 'Error processing action %1$s: %2$s', 'woocommerce' ), $action_id, $exception->getMessage() ),
170
+			sprintf(__('Error processing action %1$s: %2$s', 'woocommerce'), $action_id, $exception->getMessage()),
171 171
 			false
172 172
 		);
173 173
 	}
@@ -178,8 +178,8 @@  discard block
 block discarded – undo
178 178
 	 * @param int $sleep_time Amount of seconds to sleep
179 179
 	 * @deprecated 3.0.0
180 180
 	 */
181
-	protected function stop_the_insanity( $sleep_time = 0 ) {
182
-		_deprecated_function( 'ActionScheduler_WPCLI_QueueRunner::stop_the_insanity', '3.0.0', 'ActionScheduler_DataController::free_memory' );
181
+	protected function stop_the_insanity($sleep_time = 0) {
182
+		_deprecated_function('ActionScheduler_WPCLI_QueueRunner::stop_the_insanity', '3.0.0', 'ActionScheduler_DataController::free_memory');
183 183
 
184 184
 		ActionScheduler_DataController::free_memory();
185 185
 	}
@@ -189,8 +189,8 @@  discard block
 block discarded – undo
189 189
 	 */
190 190
 	protected function maybe_stop_the_insanity() {
191 191
 		// The value returned by progress_bar->current() might be padded. Remove padding, and convert to int.
192
-		$current_iteration = intval( trim( $this->progress_bar->current() ) );
193
-		if ( 0 === $current_iteration % 50 ) {
192
+		$current_iteration = intval(trim($this->progress_bar->current()));
193
+		if (0 === $current_iteration % 50) {
194 194
 			$this->stop_the_insanity();
195 195
 		}
196 196
 	}
Please login to merge, or discard this patch.
action-scheduler/classes/WP_CLI/ActionScheduler_WPCLI_Scheduler_command.php 2 patches
Indentation   +180 added lines, -180 removed lines patch added patch discarded remove patch
@@ -5,184 +5,184 @@
 block discarded – undo
5 5
  */
6 6
 class ActionScheduler_WPCLI_Scheduler_command extends WP_CLI_Command {
7 7
 
8
-	/**
9
-	 * Force tables schema creation for Action Scheduler
10
-	 *
11
-	 * ## OPTIONS
12
-	 *
13
-	 * @param array $args Positional arguments.
14
-	 * @param array $assoc_args Keyed arguments.
15
-	 *
16
-	 * @subcommand fix-schema
17
-	 */
18
-	public function fix_schema( $args, $assoc_args ) {
19
-		$schema_classes = array( ActionScheduler_LoggerSchema::class, ActionScheduler_StoreSchema::class );
20
-
21
-		foreach ( $schema_classes as $classname ) {
22
-			if ( is_subclass_of( $classname, ActionScheduler_Abstract_Schema::class ) ) {
23
-				$obj = new $classname();
24
-				$obj->init();
25
-				$obj->register_tables( true );
26
-
27
-				WP_CLI::success(
28
-					sprintf(
29
-						/* translators: %s refers to the schema name*/
30
-						__( 'Registered schema for %s', 'woocommerce' ),
31
-						$classname
32
-					)
33
-				);
34
-			}
35
-		}
36
-	}
37
-
38
-	/**
39
-	 * Run the Action Scheduler
40
-	 *
41
-	 * ## OPTIONS
42
-	 *
43
-	 * [--batch-size=<size>]
44
-	 * : The maximum number of actions to run. Defaults to 100.
45
-	 *
46
-	 * [--batches=<size>]
47
-	 * : Limit execution to a number of batches. Defaults to 0, meaning batches will continue being executed until all actions are complete.
48
-	 *
49
-	 * [--cleanup-batch-size=<size>]
50
-	 * : The maximum number of actions to clean up. Defaults to the value of --batch-size.
51
-	 *
52
-	 * [--hooks=<hooks>]
53
-	 * : Only run actions with the specified hook. Omitting this option runs actions with any hook. Define multiple hooks as a comma separated string (without spaces), e.g. `--hooks=hook_one,hook_two,hook_three`
54
-	 *
55
-	 * [--group=<group>]
56
-	 * : Only run actions from the specified group. Omitting this option runs actions from all groups.
57
-	 *
58
-	 * [--free-memory-on=<count>]
59
-	 * : The number of actions to process between freeing memory. 0 disables freeing memory. Default 50.
60
-	 *
61
-	 * [--pause=<seconds>]
62
-	 * : The number of seconds to pause when freeing memory. Default no pause.
63
-	 *
64
-	 * [--force]
65
-	 * : Whether to force execution despite the maximum number of concurrent processes being exceeded.
66
-	 *
67
-	 * @param array $args Positional arguments.
68
-	 * @param array $assoc_args Keyed arguments.
69
-	 * @throws \WP_CLI\ExitException When an error occurs.
70
-	 *
71
-	 * @subcommand run
72
-	 */
73
-	public function run( $args, $assoc_args ) {
74
-		// Handle passed arguments.
75
-		$batch   = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batch-size', 100 ) );
76
-		$batches = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batches', 0 ) );
77
-		$clean   = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'cleanup-batch-size', $batch ) );
78
-		$hooks   = explode( ',', WP_CLI\Utils\get_flag_value( $assoc_args, 'hooks', '' ) );
79
-		$hooks   = array_filter( array_map( 'trim', $hooks ) );
80
-		$group   = \WP_CLI\Utils\get_flag_value( $assoc_args, 'group', '' );
81
-		$free_on = \WP_CLI\Utils\get_flag_value( $assoc_args, 'free-memory-on', 50 );
82
-		$sleep   = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pause', 0 );
83
-		$force   = \WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false );
84
-
85
-		ActionScheduler_DataController::set_free_ticks( $free_on );
86
-		ActionScheduler_DataController::set_sleep_time( $sleep );
87
-
88
-		$batches_completed = 0;
89
-		$actions_completed = 0;
90
-		$unlimited         = $batches === 0;
91
-
92
-		try {
93
-			// Custom queue cleaner instance.
94
-			$cleaner = new ActionScheduler_QueueCleaner( null, $clean );
95
-
96
-			// Get the queue runner instance
97
-			$runner = new ActionScheduler_WPCLI_QueueRunner( null, null, $cleaner );
98
-
99
-			// Determine how many tasks will be run in the first batch.
100
-			$total = $runner->setup( $batch, $hooks, $group, $force );
101
-
102
-			// Run actions for as long as possible.
103
-			while ( $total > 0 ) {
104
-				$this->print_total_actions( $total );
105
-				$actions_completed += $runner->run();
106
-				$batches_completed++;
107
-
108
-				// Maybe set up tasks for the next batch.
109
-				$total = ( $unlimited || $batches_completed < $batches ) ? $runner->setup( $batch, $hooks, $group, $force ) : 0;
110
-			}
111
-		} catch ( Exception $e ) {
112
-			$this->print_error( $e );
113
-		}
114
-
115
-		$this->print_total_batches( $batches_completed );
116
-		$this->print_success( $actions_completed );
117
-	}
118
-
119
-	/**
120
-	 * Print WP CLI message about how many actions are about to be processed.
121
-	 *
122
-	 * @author Jeremy Pry
123
-	 *
124
-	 * @param int $total
125
-	 */
126
-	protected function print_total_actions( $total ) {
127
-		WP_CLI::log(
128
-			sprintf(
129
-				/* translators: %d refers to how many scheduled taks were found to run */
130
-				_n( 'Found %d scheduled task', 'Found %d scheduled tasks', $total, 'woocommerce' ),
131
-				number_format_i18n( $total )
132
-			)
133
-		);
134
-	}
135
-
136
-	/**
137
-	 * Print WP CLI message about how many batches of actions were processed.
138
-	 *
139
-	 * @author Jeremy Pry
140
-	 *
141
-	 * @param int $batches_completed
142
-	 */
143
-	protected function print_total_batches( $batches_completed ) {
144
-		WP_CLI::log(
145
-			sprintf(
146
-				/* translators: %d refers to the total number of batches executed */
147
-				_n( '%d batch executed.', '%d batches executed.', $batches_completed, 'woocommerce' ),
148
-				number_format_i18n( $batches_completed )
149
-			)
150
-		);
151
-	}
152
-
153
-	/**
154
-	 * Convert an exception into a WP CLI error.
155
-	 *
156
-	 * @author Jeremy Pry
157
-	 *
158
-	 * @param Exception $e The error object.
159
-	 *
160
-	 * @throws \WP_CLI\ExitException
161
-	 */
162
-	protected function print_error( Exception $e ) {
163
-		WP_CLI::error(
164
-			sprintf(
165
-				/* translators: %s refers to the exception error message */
166
-				__( 'There was an error running the action scheduler: %s', 'woocommerce' ),
167
-				$e->getMessage()
168
-			)
169
-		);
170
-	}
171
-
172
-	/**
173
-	 * Print a success message with the number of completed actions.
174
-	 *
175
-	 * @author Jeremy Pry
176
-	 *
177
-	 * @param int $actions_completed
178
-	 */
179
-	protected function print_success( $actions_completed ) {
180
-		WP_CLI::success(
181
-			sprintf(
182
-				/* translators: %d refers to the total number of taskes completed */
183
-				_n( '%d scheduled task completed.', '%d scheduled tasks completed.', $actions_completed, 'woocommerce' ),
184
-				number_format_i18n( $actions_completed )
185
-			)
186
-		);
187
-	}
8
+    /**
9
+     * Force tables schema creation for Action Scheduler
10
+     *
11
+     * ## OPTIONS
12
+     *
13
+     * @param array $args Positional arguments.
14
+     * @param array $assoc_args Keyed arguments.
15
+     *
16
+     * @subcommand fix-schema
17
+     */
18
+    public function fix_schema( $args, $assoc_args ) {
19
+        $schema_classes = array( ActionScheduler_LoggerSchema::class, ActionScheduler_StoreSchema::class );
20
+
21
+        foreach ( $schema_classes as $classname ) {
22
+            if ( is_subclass_of( $classname, ActionScheduler_Abstract_Schema::class ) ) {
23
+                $obj = new $classname();
24
+                $obj->init();
25
+                $obj->register_tables( true );
26
+
27
+                WP_CLI::success(
28
+                    sprintf(
29
+                        /* translators: %s refers to the schema name*/
30
+                        __( 'Registered schema for %s', 'woocommerce' ),
31
+                        $classname
32
+                    )
33
+                );
34
+            }
35
+        }
36
+    }
37
+
38
+    /**
39
+     * Run the Action Scheduler
40
+     *
41
+     * ## OPTIONS
42
+     *
43
+     * [--batch-size=<size>]
44
+     * : The maximum number of actions to run. Defaults to 100.
45
+     *
46
+     * [--batches=<size>]
47
+     * : Limit execution to a number of batches. Defaults to 0, meaning batches will continue being executed until all actions are complete.
48
+     *
49
+     * [--cleanup-batch-size=<size>]
50
+     * : The maximum number of actions to clean up. Defaults to the value of --batch-size.
51
+     *
52
+     * [--hooks=<hooks>]
53
+     * : Only run actions with the specified hook. Omitting this option runs actions with any hook. Define multiple hooks as a comma separated string (without spaces), e.g. `--hooks=hook_one,hook_two,hook_three`
54
+     *
55
+     * [--group=<group>]
56
+     * : Only run actions from the specified group. Omitting this option runs actions from all groups.
57
+     *
58
+     * [--free-memory-on=<count>]
59
+     * : The number of actions to process between freeing memory. 0 disables freeing memory. Default 50.
60
+     *
61
+     * [--pause=<seconds>]
62
+     * : The number of seconds to pause when freeing memory. Default no pause.
63
+     *
64
+     * [--force]
65
+     * : Whether to force execution despite the maximum number of concurrent processes being exceeded.
66
+     *
67
+     * @param array $args Positional arguments.
68
+     * @param array $assoc_args Keyed arguments.
69
+     * @throws \WP_CLI\ExitException When an error occurs.
70
+     *
71
+     * @subcommand run
72
+     */
73
+    public function run( $args, $assoc_args ) {
74
+        // Handle passed arguments.
75
+        $batch   = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batch-size', 100 ) );
76
+        $batches = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batches', 0 ) );
77
+        $clean   = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'cleanup-batch-size', $batch ) );
78
+        $hooks   = explode( ',', WP_CLI\Utils\get_flag_value( $assoc_args, 'hooks', '' ) );
79
+        $hooks   = array_filter( array_map( 'trim', $hooks ) );
80
+        $group   = \WP_CLI\Utils\get_flag_value( $assoc_args, 'group', '' );
81
+        $free_on = \WP_CLI\Utils\get_flag_value( $assoc_args, 'free-memory-on', 50 );
82
+        $sleep   = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pause', 0 );
83
+        $force   = \WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false );
84
+
85
+        ActionScheduler_DataController::set_free_ticks( $free_on );
86
+        ActionScheduler_DataController::set_sleep_time( $sleep );
87
+
88
+        $batches_completed = 0;
89
+        $actions_completed = 0;
90
+        $unlimited         = $batches === 0;
91
+
92
+        try {
93
+            // Custom queue cleaner instance.
94
+            $cleaner = new ActionScheduler_QueueCleaner( null, $clean );
95
+
96
+            // Get the queue runner instance
97
+            $runner = new ActionScheduler_WPCLI_QueueRunner( null, null, $cleaner );
98
+
99
+            // Determine how many tasks will be run in the first batch.
100
+            $total = $runner->setup( $batch, $hooks, $group, $force );
101
+
102
+            // Run actions for as long as possible.
103
+            while ( $total > 0 ) {
104
+                $this->print_total_actions( $total );
105
+                $actions_completed += $runner->run();
106
+                $batches_completed++;
107
+
108
+                // Maybe set up tasks for the next batch.
109
+                $total = ( $unlimited || $batches_completed < $batches ) ? $runner->setup( $batch, $hooks, $group, $force ) : 0;
110
+            }
111
+        } catch ( Exception $e ) {
112
+            $this->print_error( $e );
113
+        }
114
+
115
+        $this->print_total_batches( $batches_completed );
116
+        $this->print_success( $actions_completed );
117
+    }
118
+
119
+    /**
120
+     * Print WP CLI message about how many actions are about to be processed.
121
+     *
122
+     * @author Jeremy Pry
123
+     *
124
+     * @param int $total
125
+     */
126
+    protected function print_total_actions( $total ) {
127
+        WP_CLI::log(
128
+            sprintf(
129
+                /* translators: %d refers to how many scheduled taks were found to run */
130
+                _n( 'Found %d scheduled task', 'Found %d scheduled tasks', $total, 'woocommerce' ),
131
+                number_format_i18n( $total )
132
+            )
133
+        );
134
+    }
135
+
136
+    /**
137
+     * Print WP CLI message about how many batches of actions were processed.
138
+     *
139
+     * @author Jeremy Pry
140
+     *
141
+     * @param int $batches_completed
142
+     */
143
+    protected function print_total_batches( $batches_completed ) {
144
+        WP_CLI::log(
145
+            sprintf(
146
+                /* translators: %d refers to the total number of batches executed */
147
+                _n( '%d batch executed.', '%d batches executed.', $batches_completed, 'woocommerce' ),
148
+                number_format_i18n( $batches_completed )
149
+            )
150
+        );
151
+    }
152
+
153
+    /**
154
+     * Convert an exception into a WP CLI error.
155
+     *
156
+     * @author Jeremy Pry
157
+     *
158
+     * @param Exception $e The error object.
159
+     *
160
+     * @throws \WP_CLI\ExitException
161
+     */
162
+    protected function print_error( Exception $e ) {
163
+        WP_CLI::error(
164
+            sprintf(
165
+                /* translators: %s refers to the exception error message */
166
+                __( 'There was an error running the action scheduler: %s', 'woocommerce' ),
167
+                $e->getMessage()
168
+            )
169
+        );
170
+    }
171
+
172
+    /**
173
+     * Print a success message with the number of completed actions.
174
+     *
175
+     * @author Jeremy Pry
176
+     *
177
+     * @param int $actions_completed
178
+     */
179
+    protected function print_success( $actions_completed ) {
180
+        WP_CLI::success(
181
+            sprintf(
182
+                /* translators: %d refers to the total number of taskes completed */
183
+                _n( '%d scheduled task completed.', '%d scheduled tasks completed.', $actions_completed, 'woocommerce' ),
184
+                number_format_i18n( $actions_completed )
185
+            )
186
+        );
187
+    }
188 188
 }
Please login to merge, or discard this patch.
Spacing   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -15,19 +15,19 @@  discard block
 block discarded – undo
15 15
 	 *
16 16
 	 * @subcommand fix-schema
17 17
 	 */
18
-	public function fix_schema( $args, $assoc_args ) {
19
-		$schema_classes = array( ActionScheduler_LoggerSchema::class, ActionScheduler_StoreSchema::class );
18
+	public function fix_schema($args, $assoc_args) {
19
+		$schema_classes = array(ActionScheduler_LoggerSchema::class, ActionScheduler_StoreSchema::class);
20 20
 
21
-		foreach ( $schema_classes as $classname ) {
22
-			if ( is_subclass_of( $classname, ActionScheduler_Abstract_Schema::class ) ) {
21
+		foreach ($schema_classes as $classname) {
22
+			if (is_subclass_of($classname, ActionScheduler_Abstract_Schema::class)) {
23 23
 				$obj = new $classname();
24 24
 				$obj->init();
25
-				$obj->register_tables( true );
25
+				$obj->register_tables(true);
26 26
 
27 27
 				WP_CLI::success(
28 28
 					sprintf(
29 29
 						/* translators: %s refers to the schema name*/
30
-						__( 'Registered schema for %s', 'woocommerce' ),
30
+						__('Registered schema for %s', 'woocommerce'),
31 31
 						$classname
32 32
 					)
33 33
 				);
@@ -70,20 +70,20 @@  discard block
 block discarded – undo
70 70
 	 *
71 71
 	 * @subcommand run
72 72
 	 */
73
-	public function run( $args, $assoc_args ) {
73
+	public function run($args, $assoc_args) {
74 74
 		// Handle passed arguments.
75
-		$batch   = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batch-size', 100 ) );
76
-		$batches = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batches', 0 ) );
77
-		$clean   = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'cleanup-batch-size', $batch ) );
78
-		$hooks   = explode( ',', WP_CLI\Utils\get_flag_value( $assoc_args, 'hooks', '' ) );
79
-		$hooks   = array_filter( array_map( 'trim', $hooks ) );
80
-		$group   = \WP_CLI\Utils\get_flag_value( $assoc_args, 'group', '' );
81
-		$free_on = \WP_CLI\Utils\get_flag_value( $assoc_args, 'free-memory-on', 50 );
82
-		$sleep   = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pause', 0 );
83
-		$force   = \WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false );
84
-
85
-		ActionScheduler_DataController::set_free_ticks( $free_on );
86
-		ActionScheduler_DataController::set_sleep_time( $sleep );
75
+		$batch   = absint(\WP_CLI\Utils\get_flag_value($assoc_args, 'batch-size', 100));
76
+		$batches = absint(\WP_CLI\Utils\get_flag_value($assoc_args, 'batches', 0));
77
+		$clean   = absint(\WP_CLI\Utils\get_flag_value($assoc_args, 'cleanup-batch-size', $batch));
78
+		$hooks   = explode(',', WP_CLI\Utils\get_flag_value($assoc_args, 'hooks', ''));
79
+		$hooks   = array_filter(array_map('trim', $hooks));
80
+		$group   = \WP_CLI\Utils\get_flag_value($assoc_args, 'group', '');
81
+		$free_on = \WP_CLI\Utils\get_flag_value($assoc_args, 'free-memory-on', 50);
82
+		$sleep   = \WP_CLI\Utils\get_flag_value($assoc_args, 'pause', 0);
83
+		$force   = \WP_CLI\Utils\get_flag_value($assoc_args, 'force', false);
84
+
85
+		ActionScheduler_DataController::set_free_ticks($free_on);
86
+		ActionScheduler_DataController::set_sleep_time($sleep);
87 87
 
88 88
 		$batches_completed = 0;
89 89
 		$actions_completed = 0;
@@ -91,29 +91,29 @@  discard block
 block discarded – undo
91 91
 
92 92
 		try {
93 93
 			// Custom queue cleaner instance.
94
-			$cleaner = new ActionScheduler_QueueCleaner( null, $clean );
94
+			$cleaner = new ActionScheduler_QueueCleaner(null, $clean);
95 95
 
96 96
 			// Get the queue runner instance
97
-			$runner = new ActionScheduler_WPCLI_QueueRunner( null, null, $cleaner );
97
+			$runner = new ActionScheduler_WPCLI_QueueRunner(null, null, $cleaner);
98 98
 
99 99
 			// Determine how many tasks will be run in the first batch.
100
-			$total = $runner->setup( $batch, $hooks, $group, $force );
100
+			$total = $runner->setup($batch, $hooks, $group, $force);
101 101
 
102 102
 			// Run actions for as long as possible.
103
-			while ( $total > 0 ) {
104
-				$this->print_total_actions( $total );
103
+			while ($total > 0) {
104
+				$this->print_total_actions($total);
105 105
 				$actions_completed += $runner->run();
106 106
 				$batches_completed++;
107 107
 
108 108
 				// Maybe set up tasks for the next batch.
109
-				$total = ( $unlimited || $batches_completed < $batches ) ? $runner->setup( $batch, $hooks, $group, $force ) : 0;
109
+				$total = ($unlimited || $batches_completed < $batches) ? $runner->setup($batch, $hooks, $group, $force) : 0;
110 110
 			}
111
-		} catch ( Exception $e ) {
112
-			$this->print_error( $e );
111
+		} catch (Exception $e) {
112
+			$this->print_error($e);
113 113
 		}
114 114
 
115
-		$this->print_total_batches( $batches_completed );
116
-		$this->print_success( $actions_completed );
115
+		$this->print_total_batches($batches_completed);
116
+		$this->print_success($actions_completed);
117 117
 	}
118 118
 
119 119
 	/**
@@ -123,12 +123,12 @@  discard block
 block discarded – undo
123 123
 	 *
124 124
 	 * @param int $total
125 125
 	 */
126
-	protected function print_total_actions( $total ) {
126
+	protected function print_total_actions($total) {
127 127
 		WP_CLI::log(
128 128
 			sprintf(
129 129
 				/* translators: %d refers to how many scheduled taks were found to run */
130
-				_n( 'Found %d scheduled task', 'Found %d scheduled tasks', $total, 'woocommerce' ),
131
-				number_format_i18n( $total )
130
+				_n('Found %d scheduled task', 'Found %d scheduled tasks', $total, 'woocommerce'),
131
+				number_format_i18n($total)
132 132
 			)
133 133
 		);
134 134
 	}
@@ -140,12 +140,12 @@  discard block
 block discarded – undo
140 140
 	 *
141 141
 	 * @param int $batches_completed
142 142
 	 */
143
-	protected function print_total_batches( $batches_completed ) {
143
+	protected function print_total_batches($batches_completed) {
144 144
 		WP_CLI::log(
145 145
 			sprintf(
146 146
 				/* translators: %d refers to the total number of batches executed */
147
-				_n( '%d batch executed.', '%d batches executed.', $batches_completed, 'woocommerce' ),
148
-				number_format_i18n( $batches_completed )
147
+				_n('%d batch executed.', '%d batches executed.', $batches_completed, 'woocommerce'),
148
+				number_format_i18n($batches_completed)
149 149
 			)
150 150
 		);
151 151
 	}
@@ -159,11 +159,11 @@  discard block
 block discarded – undo
159 159
 	 *
160 160
 	 * @throws \WP_CLI\ExitException
161 161
 	 */
162
-	protected function print_error( Exception $e ) {
162
+	protected function print_error(Exception $e) {
163 163
 		WP_CLI::error(
164 164
 			sprintf(
165 165
 				/* translators: %s refers to the exception error message */
166
-				__( 'There was an error running the action scheduler: %s', 'woocommerce' ),
166
+				__('There was an error running the action scheduler: %s', 'woocommerce'),
167 167
 				$e->getMessage()
168 168
 			)
169 169
 		);
@@ -176,12 +176,12 @@  discard block
 block discarded – undo
176 176
 	 *
177 177
 	 * @param int $actions_completed
178 178
 	 */
179
-	protected function print_success( $actions_completed ) {
179
+	protected function print_success($actions_completed) {
180 180
 		WP_CLI::success(
181 181
 			sprintf(
182 182
 				/* translators: %d refers to the total number of taskes completed */
183
-				_n( '%d scheduled task completed.', '%d scheduled tasks completed.', $actions_completed, 'woocommerce' ),
184
-				number_format_i18n( $actions_completed )
183
+				_n('%d scheduled task completed.', '%d scheduled tasks completed.', $actions_completed, 'woocommerce'),
184
+				number_format_i18n($actions_completed)
185 185
 			)
186 186
 		);
187 187
 	}
Please login to merge, or discard this patch.
woocommerce/packages/action-scheduler/classes/WP_CLI/ProgressBar.php 2 patches
Indentation   +99 added lines, -99 removed lines patch added patch discarded remove patch
@@ -17,103 +17,103 @@
 block discarded – undo
17 17
  */
18 18
 class ProgressBar {
19 19
 
20
-	/** @var integer */
21
-	protected $total_ticks;
22
-
23
-	/** @var integer */
24
-	protected $count;
25
-
26
-	/** @var integer */
27
-	protected $interval;
28
-
29
-	/** @var string */
30
-	protected $message;
31
-
32
-	/** @var \cli\progress\Bar */
33
-	protected $progress_bar;
34
-
35
-	/**
36
-	 * ProgressBar constructor.
37
-	 *
38
-	 * @param string  $message    Text to display before the progress bar.
39
-	 * @param integer $count      Total number of ticks to be performed.
40
-	 * @param integer $interval   Optional. The interval in milliseconds between updates. Default 100.
41
- 	 *
42
-	 * @throws Exception When this is not run within WP CLI
43
-	 */
44
-	public function __construct( $message, $count, $interval = 100 ) {
45
-		if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) {
46
-			/* translators: %s php class name */
47
-			throw new \Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'woocommerce' ), __CLASS__ ) );
48
-		}
49
-
50
-		$this->total_ticks = 0;
51
-		$this->message     = $message;
52
-		$this->count       = $count;
53
-		$this->interval    = $interval;
54
-	}
55
-
56
-	/**
57
-	 * Increment the progress bar ticks.
58
-	 */
59
-	public function tick() {
60
-		if ( null === $this->progress_bar ) {
61
-			$this->setup_progress_bar();
62
-		}
63
-
64
-		$this->progress_bar->tick();
65
-		$this->total_ticks++;
66
-
67
-		do_action( 'action_scheduler/progress_tick', $this->total_ticks );
68
-	}
69
-
70
-	/**
71
-	 * Get the progress bar tick count.
72
-	 *
73
-	 * @return int
74
-	 */
75
-	public function current() {
76
-		return $this->progress_bar ? $this->progress_bar->current() : 0;
77
-	}
78
-
79
-	/**
80
-	 * Finish the current progress bar.
81
-	 */
82
-	public function finish() {
83
-		if ( null !== $this->progress_bar ) {
84
-			$this->progress_bar->finish();
85
-		}
86
-
87
-		$this->progress_bar = null;
88
-	}
89
-
90
-	/**
91
-	 * Set the message used when creating the progress bar.
92
-	 *
93
-	 * @param string $message The message to be used when the next progress bar is created.
94
-	 */
95
-	public function set_message( $message ) {
96
-		$this->message = $message;
97
-	}
98
-
99
-	/**
100
-	 * Set the count for a new progress bar.
101
-	 *
102
-	 * @param integer $count The total number of ticks expected to complete.
103
-	 */
104
-	public function set_count( $count ) {
105
-		$this->count = $count;
106
-		$this->finish();
107
-	}
108
-
109
-	/**
110
-	 * Set up the progress bar.
111
-	 */
112
-	protected function setup_progress_bar() {
113
-		$this->progress_bar = \WP_CLI\Utils\make_progress_bar(
114
-			$this->message,
115
-			$this->count,
116
-			$this->interval
117
-		);
118
-	}
20
+    /** @var integer */
21
+    protected $total_ticks;
22
+
23
+    /** @var integer */
24
+    protected $count;
25
+
26
+    /** @var integer */
27
+    protected $interval;
28
+
29
+    /** @var string */
30
+    protected $message;
31
+
32
+    /** @var \cli\progress\Bar */
33
+    protected $progress_bar;
34
+
35
+    /**
36
+     * ProgressBar constructor.
37
+     *
38
+     * @param string  $message    Text to display before the progress bar.
39
+     * @param integer $count      Total number of ticks to be performed.
40
+     * @param integer $interval   Optional. The interval in milliseconds between updates. Default 100.
41
+     *
42
+     * @throws Exception When this is not run within WP CLI
43
+     */
44
+    public function __construct( $message, $count, $interval = 100 ) {
45
+        if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) {
46
+            /* translators: %s php class name */
47
+            throw new \Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'woocommerce' ), __CLASS__ ) );
48
+        }
49
+
50
+        $this->total_ticks = 0;
51
+        $this->message     = $message;
52
+        $this->count       = $count;
53
+        $this->interval    = $interval;
54
+    }
55
+
56
+    /**
57
+     * Increment the progress bar ticks.
58
+     */
59
+    public function tick() {
60
+        if ( null === $this->progress_bar ) {
61
+            $this->setup_progress_bar();
62
+        }
63
+
64
+        $this->progress_bar->tick();
65
+        $this->total_ticks++;
66
+
67
+        do_action( 'action_scheduler/progress_tick', $this->total_ticks );
68
+    }
69
+
70
+    /**
71
+     * Get the progress bar tick count.
72
+     *
73
+     * @return int
74
+     */
75
+    public function current() {
76
+        return $this->progress_bar ? $this->progress_bar->current() : 0;
77
+    }
78
+
79
+    /**
80
+     * Finish the current progress bar.
81
+     */
82
+    public function finish() {
83
+        if ( null !== $this->progress_bar ) {
84
+            $this->progress_bar->finish();
85
+        }
86
+
87
+        $this->progress_bar = null;
88
+    }
89
+
90
+    /**
91
+     * Set the message used when creating the progress bar.
92
+     *
93
+     * @param string $message The message to be used when the next progress bar is created.
94
+     */
95
+    public function set_message( $message ) {
96
+        $this->message = $message;
97
+    }
98
+
99
+    /**
100
+     * Set the count for a new progress bar.
101
+     *
102
+     * @param integer $count The total number of ticks expected to complete.
103
+     */
104
+    public function set_count( $count ) {
105
+        $this->count = $count;
106
+        $this->finish();
107
+    }
108
+
109
+    /**
110
+     * Set up the progress bar.
111
+     */
112
+    protected function setup_progress_bar() {
113
+        $this->progress_bar = \WP_CLI\Utils\make_progress_bar(
114
+            $this->message,
115
+            $this->count,
116
+            $this->interval
117
+        );
118
+    }
119 119
 }
Please login to merge, or discard this patch.
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -41,10 +41,10 @@  discard block
 block discarded – undo
41 41
  	 *
42 42
 	 * @throws Exception When this is not run within WP CLI
43 43
 	 */
44
-	public function __construct( $message, $count, $interval = 100 ) {
45
-		if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) {
44
+	public function __construct($message, $count, $interval = 100) {
45
+		if (!(defined('WP_CLI') && WP_CLI)) {
46 46
 			/* translators: %s php class name */
47
-			throw new \Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'woocommerce' ), __CLASS__ ) );
47
+			throw new \Exception(sprintf(__('The %s class can only be run within WP CLI.', 'woocommerce'), __CLASS__));
48 48
 		}
49 49
 
50 50
 		$this->total_ticks = 0;
@@ -57,14 +57,14 @@  discard block
 block discarded – undo
57 57
 	 * Increment the progress bar ticks.
58 58
 	 */
59 59
 	public function tick() {
60
-		if ( null === $this->progress_bar ) {
60
+		if (null === $this->progress_bar) {
61 61
 			$this->setup_progress_bar();
62 62
 		}
63 63
 
64 64
 		$this->progress_bar->tick();
65 65
 		$this->total_ticks++;
66 66
 
67
-		do_action( 'action_scheduler/progress_tick', $this->total_ticks );
67
+		do_action('action_scheduler/progress_tick', $this->total_ticks);
68 68
 	}
69 69
 
70 70
 	/**
@@ -80,7 +80,7 @@  discard block
 block discarded – undo
80 80
 	 * Finish the current progress bar.
81 81
 	 */
82 82
 	public function finish() {
83
-		if ( null !== $this->progress_bar ) {
83
+		if (null !== $this->progress_bar) {
84 84
 			$this->progress_bar->finish();
85 85
 		}
86 86
 
@@ -92,7 +92,7 @@  discard block
 block discarded – undo
92 92
 	 *
93 93
 	 * @param string $message The message to be used when the next progress bar is created.
94 94
 	 */
95
-	public function set_message( $message ) {
95
+	public function set_message($message) {
96 96
 		$this->message = $message;
97 97
 	}
98 98
 
@@ -101,7 +101,7 @@  discard block
 block discarded – undo
101 101
 	 *
102 102
 	 * @param integer $count The total number of ticks expected to complete.
103 103
 	 */
104
-	public function set_count( $count ) {
104
+	public function set_count($count) {
105 105
 		$this->count = $count;
106 106
 		$this->finish();
107 107
 	}
Please login to merge, or discard this patch.
woocommerce/packages/action-scheduler/classes/WP_CLI/Migration_Command.php 2 patches
Indentation   +124 added lines, -124 removed lines patch added patch discarded remove patch
@@ -21,128 +21,128 @@
 block discarded – undo
21 21
  */
22 22
 class Migration_Command extends WP_CLI_Command {
23 23
 
24
-	/** @var int */
25
-	private $total_processed = 0;
26
-
27
-	/**
28
-	 * Register the command with WP-CLI
29
-	 */
30
-	public function register() {
31
-		if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
32
-			return;
33
-		}
34
-
35
-		WP_CLI::add_command( 'action-scheduler migrate', [ $this, 'migrate' ], [
36
-			'shortdesc' => 'Migrates actions to the DB tables store',
37
-			'synopsis'  => [
38
-				[
39
-					'type'        => 'assoc',
40
-					'name'        => 'batch-size',
41
-					'optional'    => true,
42
-					'default'     => 100,
43
-					'description' => 'The number of actions to process in each batch',
44
-				],
45
-				[
46
-					'type'        => 'assoc',
47
-					'name'        => 'free-memory-on',
48
-					'optional'    => true,
49
-					'default'     => 50,
50
-					'description' => 'The number of actions to process between freeing memory. 0 disables freeing memory',
51
-				],
52
-				[
53
-					'type'        => 'assoc',
54
-					'name'        => 'pause',
55
-					'optional'    => true,
56
-					'default'     => 0,
57
-					'description' => 'The number of seconds to pause when freeing memory',
58
-				],
59
-				[
60
-					'type'        => 'flag',
61
-					'name'        => 'dry-run',
62
-					'optional'    => true,
63
-					'description' => 'Reports on the actions that would have been migrated, but does not change any data',
64
-				],
65
-			],
66
-		] );
67
-	}
68
-
69
-	/**
70
-	 * Process the data migration.
71
-	 *
72
-	 * @param array $positional_args Required for WP CLI. Not used in migration.
73
-	 * @param array $assoc_args Optional arguments.
74
-	 *
75
-	 * @return void
76
-	 */
77
-	public function migrate( $positional_args, $assoc_args ) {
78
-		$this->init_logging();
79
-
80
-		$config = $this->get_migration_config( $assoc_args );
81
-		$runner = new Runner( $config );
82
-		$runner->init_destination();
83
-
84
-		$batch_size = isset( $assoc_args[ 'batch-size' ] ) ? (int) $assoc_args[ 'batch-size' ] : 100;
85
-		$free_on    = isset( $assoc_args[ 'free-memory-on' ] ) ? (int) $assoc_args[ 'free-memory-on' ] : 50;
86
-		$sleep      = isset( $assoc_args[ 'pause' ] ) ? (int) $assoc_args[ 'pause' ] : 0;
87
-		\ActionScheduler_DataController::set_free_ticks( $free_on );
88
-		\ActionScheduler_DataController::set_sleep_time( $sleep );
89
-
90
-		do {
91
-			$actions_processed     = $runner->run( $batch_size );
92
-			$this->total_processed += $actions_processed;
93
-		} while ( $actions_processed > 0 );
94
-
95
-		if ( ! $config->get_dry_run() ) {
96
-			// let the scheduler know that there's nothing left to do
97
-			$scheduler = new Scheduler();
98
-			$scheduler->mark_complete();
99
-		}
100
-
101
-		WP_CLI::success( sprintf( '%s complete. %d actions processed.', $config->get_dry_run() ? 'Dry run' : 'Migration', $this->total_processed ) );
102
-	}
103
-
104
-	/**
105
-	 * Build the config object used to create the Runner
106
-	 *
107
-	 * @param array $args Optional arguments.
108
-	 *
109
-	 * @return ActionScheduler\Migration\Config
110
-	 */
111
-	private function get_migration_config( $args ) {
112
-		$args = wp_parse_args( $args, [
113
-			'dry-run' => false,
114
-		] );
115
-
116
-		$config = Controller::instance()->get_migration_config_object();
117
-		$config->set_dry_run( ! empty( $args[ 'dry-run' ] ) );
118
-
119
-		return $config;
120
-	}
121
-
122
-	/**
123
-	 * Hook command line logging into migration actions.
124
-	 */
125
-	private function init_logging() {
126
-		add_action( 'action_scheduler/migrate_action_dry_run', function ( $action_id ) {
127
-			WP_CLI::debug( sprintf( 'Dry-run: migrated action %d', $action_id ) );
128
-		}, 10, 1 );
129
-		add_action( 'action_scheduler/no_action_to_migrate', function ( $action_id ) {
130
-			WP_CLI::debug( sprintf( 'No action found to migrate for ID %d', $action_id ) );
131
-		}, 10, 1 );
132
-		add_action( 'action_scheduler/migrate_action_failed', function ( $action_id ) {
133
-			WP_CLI::warning( sprintf( 'Failed migrating action with ID %d', $action_id ) );
134
-		}, 10, 1 );
135
-		add_action( 'action_scheduler/migrate_action_incomplete', function ( $source_id, $destination_id ) {
136
-			WP_CLI::warning( sprintf( 'Unable to remove source action with ID %d after migrating to new ID %d', $source_id, $destination_id ) );
137
-		}, 10, 2 );
138
-		add_action( 'action_scheduler/migrated_action', function ( $source_id, $destination_id ) {
139
-			WP_CLI::debug( sprintf( 'Migrated source action with ID %d to new store with ID %d', $source_id, $destination_id ) );
140
-		}, 10, 2 );
141
-		add_action( 'action_scheduler/migration_batch_starting', function ( $batch ) {
142
-			WP_CLI::debug( 'Beginning migration of batch: ' . print_r( $batch, true ) );
143
-		}, 10, 1 );
144
-		add_action( 'action_scheduler/migration_batch_complete', function ( $batch ) {
145
-			WP_CLI::log( sprintf( 'Completed migration of %d actions', count( $batch ) ) );
146
-		}, 10, 1 );
147
-	}
24
+    /** @var int */
25
+    private $total_processed = 0;
26
+
27
+    /**
28
+     * Register the command with WP-CLI
29
+     */
30
+    public function register() {
31
+        if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
32
+            return;
33
+        }
34
+
35
+        WP_CLI::add_command( 'action-scheduler migrate', [ $this, 'migrate' ], [
36
+            'shortdesc' => 'Migrates actions to the DB tables store',
37
+            'synopsis'  => [
38
+                [
39
+                    'type'        => 'assoc',
40
+                    'name'        => 'batch-size',
41
+                    'optional'    => true,
42
+                    'default'     => 100,
43
+                    'description' => 'The number of actions to process in each batch',
44
+                ],
45
+                [
46
+                    'type'        => 'assoc',
47
+                    'name'        => 'free-memory-on',
48
+                    'optional'    => true,
49
+                    'default'     => 50,
50
+                    'description' => 'The number of actions to process between freeing memory. 0 disables freeing memory',
51
+                ],
52
+                [
53
+                    'type'        => 'assoc',
54
+                    'name'        => 'pause',
55
+                    'optional'    => true,
56
+                    'default'     => 0,
57
+                    'description' => 'The number of seconds to pause when freeing memory',
58
+                ],
59
+                [
60
+                    'type'        => 'flag',
61
+                    'name'        => 'dry-run',
62
+                    'optional'    => true,
63
+                    'description' => 'Reports on the actions that would have been migrated, but does not change any data',
64
+                ],
65
+            ],
66
+        ] );
67
+    }
68
+
69
+    /**
70
+     * Process the data migration.
71
+     *
72
+     * @param array $positional_args Required for WP CLI. Not used in migration.
73
+     * @param array $assoc_args Optional arguments.
74
+     *
75
+     * @return void
76
+     */
77
+    public function migrate( $positional_args, $assoc_args ) {
78
+        $this->init_logging();
79
+
80
+        $config = $this->get_migration_config( $assoc_args );
81
+        $runner = new Runner( $config );
82
+        $runner->init_destination();
83
+
84
+        $batch_size = isset( $assoc_args[ 'batch-size' ] ) ? (int) $assoc_args[ 'batch-size' ] : 100;
85
+        $free_on    = isset( $assoc_args[ 'free-memory-on' ] ) ? (int) $assoc_args[ 'free-memory-on' ] : 50;
86
+        $sleep      = isset( $assoc_args[ 'pause' ] ) ? (int) $assoc_args[ 'pause' ] : 0;
87
+        \ActionScheduler_DataController::set_free_ticks( $free_on );
88
+        \ActionScheduler_DataController::set_sleep_time( $sleep );
89
+
90
+        do {
91
+            $actions_processed     = $runner->run( $batch_size );
92
+            $this->total_processed += $actions_processed;
93
+        } while ( $actions_processed > 0 );
94
+
95
+        if ( ! $config->get_dry_run() ) {
96
+            // let the scheduler know that there's nothing left to do
97
+            $scheduler = new Scheduler();
98
+            $scheduler->mark_complete();
99
+        }
100
+
101
+        WP_CLI::success( sprintf( '%s complete. %d actions processed.', $config->get_dry_run() ? 'Dry run' : 'Migration', $this->total_processed ) );
102
+    }
103
+
104
+    /**
105
+     * Build the config object used to create the Runner
106
+     *
107
+     * @param array $args Optional arguments.
108
+     *
109
+     * @return ActionScheduler\Migration\Config
110
+     */
111
+    private function get_migration_config( $args ) {
112
+        $args = wp_parse_args( $args, [
113
+            'dry-run' => false,
114
+        ] );
115
+
116
+        $config = Controller::instance()->get_migration_config_object();
117
+        $config->set_dry_run( ! empty( $args[ 'dry-run' ] ) );
118
+
119
+        return $config;
120
+    }
121
+
122
+    /**
123
+     * Hook command line logging into migration actions.
124
+     */
125
+    private function init_logging() {
126
+        add_action( 'action_scheduler/migrate_action_dry_run', function ( $action_id ) {
127
+            WP_CLI::debug( sprintf( 'Dry-run: migrated action %d', $action_id ) );
128
+        }, 10, 1 );
129
+        add_action( 'action_scheduler/no_action_to_migrate', function ( $action_id ) {
130
+            WP_CLI::debug( sprintf( 'No action found to migrate for ID %d', $action_id ) );
131
+        }, 10, 1 );
132
+        add_action( 'action_scheduler/migrate_action_failed', function ( $action_id ) {
133
+            WP_CLI::warning( sprintf( 'Failed migrating action with ID %d', $action_id ) );
134
+        }, 10, 1 );
135
+        add_action( 'action_scheduler/migrate_action_incomplete', function ( $source_id, $destination_id ) {
136
+            WP_CLI::warning( sprintf( 'Unable to remove source action with ID %d after migrating to new ID %d', $source_id, $destination_id ) );
137
+        }, 10, 2 );
138
+        add_action( 'action_scheduler/migrated_action', function ( $source_id, $destination_id ) {
139
+            WP_CLI::debug( sprintf( 'Migrated source action with ID %d to new store with ID %d', $source_id, $destination_id ) );
140
+        }, 10, 2 );
141
+        add_action( 'action_scheduler/migration_batch_starting', function ( $batch ) {
142
+            WP_CLI::debug( 'Beginning migration of batch: ' . print_r( $batch, true ) );
143
+        }, 10, 1 );
144
+        add_action( 'action_scheduler/migration_batch_complete', function ( $batch ) {
145
+            WP_CLI::log( sprintf( 'Completed migration of %d actions', count( $batch ) ) );
146
+        }, 10, 1 );
147
+    }
148 148
 }
Please login to merge, or discard this patch.
Spacing   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -28,11 +28,11 @@  discard block
 block discarded – undo
28 28
 	 * Register the command with WP-CLI
29 29
 	 */
30 30
 	public function register() {
31
-		if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
31
+		if (!defined('WP_CLI') || !WP_CLI) {
32 32
 			return;
33 33
 		}
34 34
 
35
-		WP_CLI::add_command( 'action-scheduler migrate', [ $this, 'migrate' ], [
35
+		WP_CLI::add_command('action-scheduler migrate', [$this, 'migrate'], [
36 36
 			'shortdesc' => 'Migrates actions to the DB tables store',
37 37
 			'synopsis'  => [
38 38
 				[
@@ -63,7 +63,7 @@  discard block
 block discarded – undo
63 63
 					'description' => 'Reports on the actions that would have been migrated, but does not change any data',
64 64
 				],
65 65
 			],
66
-		] );
66
+		]);
67 67
 	}
68 68
 
69 69
 	/**
@@ -74,31 +74,31 @@  discard block
 block discarded – undo
74 74
 	 *
75 75
 	 * @return void
76 76
 	 */
77
-	public function migrate( $positional_args, $assoc_args ) {
77
+	public function migrate($positional_args, $assoc_args) {
78 78
 		$this->init_logging();
79 79
 
80
-		$config = $this->get_migration_config( $assoc_args );
81
-		$runner = new Runner( $config );
80
+		$config = $this->get_migration_config($assoc_args);
81
+		$runner = new Runner($config);
82 82
 		$runner->init_destination();
83 83
 
84
-		$batch_size = isset( $assoc_args[ 'batch-size' ] ) ? (int) $assoc_args[ 'batch-size' ] : 100;
85
-		$free_on    = isset( $assoc_args[ 'free-memory-on' ] ) ? (int) $assoc_args[ 'free-memory-on' ] : 50;
86
-		$sleep      = isset( $assoc_args[ 'pause' ] ) ? (int) $assoc_args[ 'pause' ] : 0;
87
-		\ActionScheduler_DataController::set_free_ticks( $free_on );
88
-		\ActionScheduler_DataController::set_sleep_time( $sleep );
84
+		$batch_size = isset($assoc_args['batch-size']) ? (int) $assoc_args['batch-size'] : 100;
85
+		$free_on    = isset($assoc_args['free-memory-on']) ? (int) $assoc_args['free-memory-on'] : 50;
86
+		$sleep      = isset($assoc_args['pause']) ? (int) $assoc_args['pause'] : 0;
87
+		\ActionScheduler_DataController::set_free_ticks($free_on);
88
+		\ActionScheduler_DataController::set_sleep_time($sleep);
89 89
 
90 90
 		do {
91
-			$actions_processed     = $runner->run( $batch_size );
91
+			$actions_processed = $runner->run($batch_size);
92 92
 			$this->total_processed += $actions_processed;
93
-		} while ( $actions_processed > 0 );
93
+		} while ($actions_processed > 0);
94 94
 
95
-		if ( ! $config->get_dry_run() ) {
95
+		if (!$config->get_dry_run()) {
96 96
 			// let the scheduler know that there's nothing left to do
97 97
 			$scheduler = new Scheduler();
98 98
 			$scheduler->mark_complete();
99 99
 		}
100 100
 
101
-		WP_CLI::success( sprintf( '%s complete. %d actions processed.', $config->get_dry_run() ? 'Dry run' : 'Migration', $this->total_processed ) );
101
+		WP_CLI::success(sprintf('%s complete. %d actions processed.', $config->get_dry_run() ? 'Dry run' : 'Migration', $this->total_processed));
102 102
 	}
103 103
 
104 104
 	/**
@@ -108,13 +108,13 @@  discard block
 block discarded – undo
108 108
 	 *
109 109
 	 * @return ActionScheduler\Migration\Config
110 110
 	 */
111
-	private function get_migration_config( $args ) {
112
-		$args = wp_parse_args( $args, [
111
+	private function get_migration_config($args) {
112
+		$args = wp_parse_args($args, [
113 113
 			'dry-run' => false,
114
-		] );
114
+		]);
115 115
 
116 116
 		$config = Controller::instance()->get_migration_config_object();
117
-		$config->set_dry_run( ! empty( $args[ 'dry-run' ] ) );
117
+		$config->set_dry_run(!empty($args['dry-run']));
118 118
 
119 119
 		return $config;
120 120
 	}
@@ -123,26 +123,26 @@  discard block
 block discarded – undo
123 123
 	 * Hook command line logging into migration actions.
124 124
 	 */
125 125
 	private function init_logging() {
126
-		add_action( 'action_scheduler/migrate_action_dry_run', function ( $action_id ) {
127
-			WP_CLI::debug( sprintf( 'Dry-run: migrated action %d', $action_id ) );
128
-		}, 10, 1 );
129
-		add_action( 'action_scheduler/no_action_to_migrate', function ( $action_id ) {
130
-			WP_CLI::debug( sprintf( 'No action found to migrate for ID %d', $action_id ) );
131
-		}, 10, 1 );
132
-		add_action( 'action_scheduler/migrate_action_failed', function ( $action_id ) {
133
-			WP_CLI::warning( sprintf( 'Failed migrating action with ID %d', $action_id ) );
134
-		}, 10, 1 );
135
-		add_action( 'action_scheduler/migrate_action_incomplete', function ( $source_id, $destination_id ) {
136
-			WP_CLI::warning( sprintf( 'Unable to remove source action with ID %d after migrating to new ID %d', $source_id, $destination_id ) );
137
-		}, 10, 2 );
138
-		add_action( 'action_scheduler/migrated_action', function ( $source_id, $destination_id ) {
139
-			WP_CLI::debug( sprintf( 'Migrated source action with ID %d to new store with ID %d', $source_id, $destination_id ) );
140
-		}, 10, 2 );
141
-		add_action( 'action_scheduler/migration_batch_starting', function ( $batch ) {
142
-			WP_CLI::debug( 'Beginning migration of batch: ' . print_r( $batch, true ) );
143
-		}, 10, 1 );
144
-		add_action( 'action_scheduler/migration_batch_complete', function ( $batch ) {
145
-			WP_CLI::log( sprintf( 'Completed migration of %d actions', count( $batch ) ) );
146
-		}, 10, 1 );
126
+		add_action('action_scheduler/migrate_action_dry_run', function($action_id) {
127
+			WP_CLI::debug(sprintf('Dry-run: migrated action %d', $action_id));
128
+		}, 10, 1);
129
+		add_action('action_scheduler/no_action_to_migrate', function($action_id) {
130
+			WP_CLI::debug(sprintf('No action found to migrate for ID %d', $action_id));
131
+		}, 10, 1);
132
+		add_action('action_scheduler/migrate_action_failed', function($action_id) {
133
+			WP_CLI::warning(sprintf('Failed migrating action with ID %d', $action_id));
134
+		}, 10, 1);
135
+		add_action('action_scheduler/migrate_action_incomplete', function($source_id, $destination_id) {
136
+			WP_CLI::warning(sprintf('Unable to remove source action with ID %d after migrating to new ID %d', $source_id, $destination_id));
137
+		}, 10, 2);
138
+		add_action('action_scheduler/migrated_action', function($source_id, $destination_id) {
139
+			WP_CLI::debug(sprintf('Migrated source action with ID %d to new store with ID %d', $source_id, $destination_id));
140
+		}, 10, 2);
141
+		add_action('action_scheduler/migration_batch_starting', function($batch) {
142
+			WP_CLI::debug('Beginning migration of batch: ' . print_r($batch, true));
143
+		}, 10, 1);
144
+		add_action('action_scheduler/migration_batch_complete', function($batch) {
145
+			WP_CLI::log(sprintf('Completed migration of %d actions', count($batch)));
146
+		}, 10, 1);
147 147
 	}
148 148
 }
Please login to merge, or discard this patch.
packages/action-scheduler/classes/ActionScheduler_ActionFactory.php 2 patches
Indentation   +159 added lines, -159 removed lines patch added patch discarded remove patch
@@ -5,175 +5,175 @@
 block discarded – undo
5 5
  */
6 6
 class ActionScheduler_ActionFactory {
7 7
 
8
-	/**
9
-	 * @param string $status The action's status in the data store
10
-	 * @param string $hook The hook to trigger when this action runs
11
-	 * @param array $args Args to pass to callbacks when the hook is triggered
12
-	 * @param ActionScheduler_Schedule $schedule The action's schedule
13
-	 * @param string $group A group to put the action in
14
-	 *
15
-	 * @return ActionScheduler_Action An instance of the stored action
16
-	 */
17
-	public function get_stored_action( $status, $hook, array $args = array(), ActionScheduler_Schedule $schedule = null, $group = '' ) {
8
+    /**
9
+     * @param string $status The action's status in the data store
10
+     * @param string $hook The hook to trigger when this action runs
11
+     * @param array $args Args to pass to callbacks when the hook is triggered
12
+     * @param ActionScheduler_Schedule $schedule The action's schedule
13
+     * @param string $group A group to put the action in
14
+     *
15
+     * @return ActionScheduler_Action An instance of the stored action
16
+     */
17
+    public function get_stored_action( $status, $hook, array $args = array(), ActionScheduler_Schedule $schedule = null, $group = '' ) {
18 18
 
19
-		switch ( $status ) {
20
-			case ActionScheduler_Store::STATUS_PENDING :
21
-				$action_class = 'ActionScheduler_Action';
22
-				break;
23
-			case ActionScheduler_Store::STATUS_CANCELED :
24
-				$action_class = 'ActionScheduler_CanceledAction';
25
-				if ( ! is_null( $schedule ) && ! is_a( $schedule, 'ActionScheduler_CanceledSchedule' ) && ! is_a( $schedule, 'ActionScheduler_NullSchedule' ) ) {
26
-					$schedule = new ActionScheduler_CanceledSchedule( $schedule->get_date() );
27
-				}
28
-				break;
29
-			default :
30
-				$action_class = 'ActionScheduler_FinishedAction';
31
-				break;
32
-		}
19
+        switch ( $status ) {
20
+            case ActionScheduler_Store::STATUS_PENDING :
21
+                $action_class = 'ActionScheduler_Action';
22
+                break;
23
+            case ActionScheduler_Store::STATUS_CANCELED :
24
+                $action_class = 'ActionScheduler_CanceledAction';
25
+                if ( ! is_null( $schedule ) && ! is_a( $schedule, 'ActionScheduler_CanceledSchedule' ) && ! is_a( $schedule, 'ActionScheduler_NullSchedule' ) ) {
26
+                    $schedule = new ActionScheduler_CanceledSchedule( $schedule->get_date() );
27
+                }
28
+                break;
29
+            default :
30
+                $action_class = 'ActionScheduler_FinishedAction';
31
+                break;
32
+        }
33 33
 
34
-		$action_class = apply_filters( 'action_scheduler_stored_action_class', $action_class, $status, $hook, $args, $schedule, $group );
34
+        $action_class = apply_filters( 'action_scheduler_stored_action_class', $action_class, $status, $hook, $args, $schedule, $group );
35 35
 
36
-		$action = new $action_class( $hook, $args, $schedule, $group );
36
+        $action = new $action_class( $hook, $args, $schedule, $group );
37 37
 
38
-		/**
39
-		 * Allow 3rd party code to change the instantiated action for a given hook, args, schedule and group.
40
-		 *
41
-		 * @param ActionScheduler_Action $action The instantiated action.
42
-		 * @param string $hook The instantiated action's hook.
43
-		 * @param array $args The instantiated action's args.
44
-		 * @param ActionScheduler_Schedule $schedule The instantiated action's schedule.
45
-		 * @param string $group The instantiated action's group.
46
-		 */
47
-		return apply_filters( 'action_scheduler_stored_action_instance', $action, $hook, $args, $schedule, $group );
48
-	}
38
+        /**
39
+         * Allow 3rd party code to change the instantiated action for a given hook, args, schedule and group.
40
+         *
41
+         * @param ActionScheduler_Action $action The instantiated action.
42
+         * @param string $hook The instantiated action's hook.
43
+         * @param array $args The instantiated action's args.
44
+         * @param ActionScheduler_Schedule $schedule The instantiated action's schedule.
45
+         * @param string $group The instantiated action's group.
46
+         */
47
+        return apply_filters( 'action_scheduler_stored_action_instance', $action, $hook, $args, $schedule, $group );
48
+    }
49 49
 
50
-	/**
51
-	 * Enqueue an action to run one time, as soon as possible (rather a specific scheduled time).
52
-	 *
53
-	 * This method creates a new action with the NULLSchedule. This schedule maps to a MySQL datetime string of
54
-	 * 0000-00-00 00:00:00. This is done to create a psuedo "async action" type that is fully backward compatible.
55
-	 * Existing queries to claim actions claim by date, meaning actions scheduled for 0000-00-00 00:00:00 will
56
-	 * always be claimed prior to actions scheduled for a specific date. This makes sure that any async action is
57
-	 * given priority in queue processing. This has the added advantage of making sure async actions can be
58
-	 * claimed by both the existing WP Cron and WP CLI runners, as well as a new async request runner.
59
-	 *
60
-	 * @param string $hook The hook to trigger when this action runs
61
-	 * @param array $args Args to pass when the hook is triggered
62
-	 * @param string $group A group to put the action in
63
-	 *
64
-	 * @return int The ID of the stored action
65
-	 */
66
-	public function async( $hook, $args = array(), $group = '' ) {
67
-		$schedule = new ActionScheduler_NullSchedule();
68
-		$action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
69
-		return $this->store( $action );
70
-	}
50
+    /**
51
+     * Enqueue an action to run one time, as soon as possible (rather a specific scheduled time).
52
+     *
53
+     * This method creates a new action with the NULLSchedule. This schedule maps to a MySQL datetime string of
54
+     * 0000-00-00 00:00:00. This is done to create a psuedo "async action" type that is fully backward compatible.
55
+     * Existing queries to claim actions claim by date, meaning actions scheduled for 0000-00-00 00:00:00 will
56
+     * always be claimed prior to actions scheduled for a specific date. This makes sure that any async action is
57
+     * given priority in queue processing. This has the added advantage of making sure async actions can be
58
+     * claimed by both the existing WP Cron and WP CLI runners, as well as a new async request runner.
59
+     *
60
+     * @param string $hook The hook to trigger when this action runs
61
+     * @param array $args Args to pass when the hook is triggered
62
+     * @param string $group A group to put the action in
63
+     *
64
+     * @return int The ID of the stored action
65
+     */
66
+    public function async( $hook, $args = array(), $group = '' ) {
67
+        $schedule = new ActionScheduler_NullSchedule();
68
+        $action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
69
+        return $this->store( $action );
70
+    }
71 71
 
72
-	/**
73
-	 * @param string $hook The hook to trigger when this action runs
74
-	 * @param array $args Args to pass when the hook is triggered
75
-	 * @param int $when Unix timestamp when the action will run
76
-	 * @param string $group A group to put the action in
77
-	 *
78
-	 * @return int The ID of the stored action
79
-	 */
80
-	public function single( $hook, $args = array(), $when = null, $group = '' ) {
81
-		$date = as_get_datetime_object( $when );
82
-		$schedule = new ActionScheduler_SimpleSchedule( $date );
83
-		$action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
84
-		return $this->store( $action );
85
-	}
72
+    /**
73
+     * @param string $hook The hook to trigger when this action runs
74
+     * @param array $args Args to pass when the hook is triggered
75
+     * @param int $when Unix timestamp when the action will run
76
+     * @param string $group A group to put the action in
77
+     *
78
+     * @return int The ID of the stored action
79
+     */
80
+    public function single( $hook, $args = array(), $when = null, $group = '' ) {
81
+        $date = as_get_datetime_object( $when );
82
+        $schedule = new ActionScheduler_SimpleSchedule( $date );
83
+        $action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
84
+        return $this->store( $action );
85
+    }
86 86
 
87
-	/**
88
-	 * Create the first instance of an action recurring on a given interval.
89
-	 *
90
-	 * @param string $hook The hook to trigger when this action runs
91
-	 * @param array $args Args to pass when the hook is triggered
92
-	 * @param int $first Unix timestamp for the first run
93
-	 * @param int $interval Seconds between runs
94
-	 * @param string $group A group to put the action in
95
-	 *
96
-	 * @return int The ID of the stored action
97
-	 */
98
-	public function recurring( $hook, $args = array(), $first = null, $interval = null, $group = '' ) {
99
-		if ( empty($interval) ) {
100
-			return $this->single( $hook, $args, $first, $group );
101
-		}
102
-		$date = as_get_datetime_object( $first );
103
-		$schedule = new ActionScheduler_IntervalSchedule( $date, $interval );
104
-		$action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
105
-		return $this->store( $action );
106
-	}
87
+    /**
88
+     * Create the first instance of an action recurring on a given interval.
89
+     *
90
+     * @param string $hook The hook to trigger when this action runs
91
+     * @param array $args Args to pass when the hook is triggered
92
+     * @param int $first Unix timestamp for the first run
93
+     * @param int $interval Seconds between runs
94
+     * @param string $group A group to put the action in
95
+     *
96
+     * @return int The ID of the stored action
97
+     */
98
+    public function recurring( $hook, $args = array(), $first = null, $interval = null, $group = '' ) {
99
+        if ( empty($interval) ) {
100
+            return $this->single( $hook, $args, $first, $group );
101
+        }
102
+        $date = as_get_datetime_object( $first );
103
+        $schedule = new ActionScheduler_IntervalSchedule( $date, $interval );
104
+        $action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
105
+        return $this->store( $action );
106
+    }
107 107
 
108
-	/**
109
-	 * Create the first instance of an action recurring on a Cron schedule.
110
-	 *
111
-	 * @param string $hook The hook to trigger when this action runs
112
-	 * @param array $args Args to pass when the hook is triggered
113
-	 * @param int $base_timestamp The first instance of the action will be scheduled
114
-	 *        to run at a time calculated after this timestamp matching the cron
115
-	 *        expression. This can be used to delay the first instance of the action.
116
-	 * @param int $schedule A cron definition string
117
-	 * @param string $group A group to put the action in
118
-	 *
119
-	 * @return int The ID of the stored action
120
-	 */
121
-	public function cron( $hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '' ) {
122
-		if ( empty($schedule) ) {
123
-			return $this->single( $hook, $args, $base_timestamp, $group );
124
-		}
125
-		$date = as_get_datetime_object( $base_timestamp );
126
-		$cron = CronExpression::factory( $schedule );
127
-		$schedule = new ActionScheduler_CronSchedule( $date, $cron );
128
-		$action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
129
-		return $this->store( $action );
130
-	}
108
+    /**
109
+     * Create the first instance of an action recurring on a Cron schedule.
110
+     *
111
+     * @param string $hook The hook to trigger when this action runs
112
+     * @param array $args Args to pass when the hook is triggered
113
+     * @param int $base_timestamp The first instance of the action will be scheduled
114
+     *        to run at a time calculated after this timestamp matching the cron
115
+     *        expression. This can be used to delay the first instance of the action.
116
+     * @param int $schedule A cron definition string
117
+     * @param string $group A group to put the action in
118
+     *
119
+     * @return int The ID of the stored action
120
+     */
121
+    public function cron( $hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '' ) {
122
+        if ( empty($schedule) ) {
123
+            return $this->single( $hook, $args, $base_timestamp, $group );
124
+        }
125
+        $date = as_get_datetime_object( $base_timestamp );
126
+        $cron = CronExpression::factory( $schedule );
127
+        $schedule = new ActionScheduler_CronSchedule( $date, $cron );
128
+        $action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
129
+        return $this->store( $action );
130
+    }
131 131
 
132
-	/**
133
-	 * Create a successive instance of a recurring or cron action.
134
-	 *
135
-	 * Importantly, the action will be rescheduled to run based on the current date/time.
136
-	 * That means when the action is scheduled to run in the past, the next scheduled date
137
-	 * will be pushed forward. For example, if a recurring action set to run every hour
138
-	 * was scheduled to run 5 seconds ago, it will be next scheduled for 1 hour in the
139
-	 * future, which is 1 hour and 5 seconds from when it was last scheduled to run.
140
-	 *
141
-	 * Alternatively, if the action is scheduled to run in the future, and is run early,
142
-	 * likely via manual intervention, then its schedule will change based on the time now.
143
-	 * For example, if a recurring action set to run every day, and is run 12 hours early,
144
-	 * it will run again in 24 hours, not 36 hours.
145
-	 *
146
-	 * This slippage is less of an issue with Cron actions, as the specific run time can
147
-	 * be set for them to run, e.g. 1am each day. In those cases, and entire period would
148
-	 * need to be missed before there was any change is scheduled, e.g. in the case of an
149
-	 * action scheduled for 1am each day, the action would need to run an entire day late.
150
-	 *
151
-	 * @param ActionScheduler_Action $action The existing action.
152
-	 *
153
-	 * @return string The ID of the stored action
154
-	 * @throws InvalidArgumentException If $action is not a recurring action.
155
-	 */
156
-	public function repeat( $action ) {
157
-		$schedule = $action->get_schedule();
158
-		$next     = $schedule->get_next( as_get_datetime_object() );
132
+    /**
133
+     * Create a successive instance of a recurring or cron action.
134
+     *
135
+     * Importantly, the action will be rescheduled to run based on the current date/time.
136
+     * That means when the action is scheduled to run in the past, the next scheduled date
137
+     * will be pushed forward. For example, if a recurring action set to run every hour
138
+     * was scheduled to run 5 seconds ago, it will be next scheduled for 1 hour in the
139
+     * future, which is 1 hour and 5 seconds from when it was last scheduled to run.
140
+     *
141
+     * Alternatively, if the action is scheduled to run in the future, and is run early,
142
+     * likely via manual intervention, then its schedule will change based on the time now.
143
+     * For example, if a recurring action set to run every day, and is run 12 hours early,
144
+     * it will run again in 24 hours, not 36 hours.
145
+     *
146
+     * This slippage is less of an issue with Cron actions, as the specific run time can
147
+     * be set for them to run, e.g. 1am each day. In those cases, and entire period would
148
+     * need to be missed before there was any change is scheduled, e.g. in the case of an
149
+     * action scheduled for 1am each day, the action would need to run an entire day late.
150
+     *
151
+     * @param ActionScheduler_Action $action The existing action.
152
+     *
153
+     * @return string The ID of the stored action
154
+     * @throws InvalidArgumentException If $action is not a recurring action.
155
+     */
156
+    public function repeat( $action ) {
157
+        $schedule = $action->get_schedule();
158
+        $next     = $schedule->get_next( as_get_datetime_object() );
159 159
 
160
-		if ( is_null( $next ) || ! $schedule->is_recurring() ) {
161
-			throw new InvalidArgumentException( __( 'Invalid action - must be a recurring action.', 'woocommerce' ) );
162
-		}
160
+        if ( is_null( $next ) || ! $schedule->is_recurring() ) {
161
+            throw new InvalidArgumentException( __( 'Invalid action - must be a recurring action.', 'woocommerce' ) );
162
+        }
163 163
 
164
-		$schedule_class = get_class( $schedule );
165
-		$new_schedule = new $schedule( $next, $schedule->get_recurrence(), $schedule->get_first_date() );
166
-		$new_action = new ActionScheduler_Action( $action->get_hook(), $action->get_args(), $new_schedule, $action->get_group() );
167
-		return $this->store( $new_action );
168
-	}
164
+        $schedule_class = get_class( $schedule );
165
+        $new_schedule = new $schedule( $next, $schedule->get_recurrence(), $schedule->get_first_date() );
166
+        $new_action = new ActionScheduler_Action( $action->get_hook(), $action->get_args(), $new_schedule, $action->get_group() );
167
+        return $this->store( $new_action );
168
+    }
169 169
 
170
-	/**
171
-	 * @param ActionScheduler_Action $action
172
-	 *
173
-	 * @return int The ID of the stored action
174
-	 */
175
-	protected function store( ActionScheduler_Action $action ) {
176
-		$store = ActionScheduler_Store::instance();
177
-		return $store->save_action( $action );
178
-	}
170
+    /**
171
+     * @param ActionScheduler_Action $action
172
+     *
173
+     * @return int The ID of the stored action
174
+     */
175
+    protected function store( ActionScheduler_Action $action ) {
176
+        $store = ActionScheduler_Store::instance();
177
+        return $store->save_action( $action );
178
+    }
179 179
 }
Please login to merge, or discard this patch.
Spacing   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -14,16 +14,16 @@  discard block
 block discarded – undo
14 14
 	 *
15 15
 	 * @return ActionScheduler_Action An instance of the stored action
16 16
 	 */
17
-	public function get_stored_action( $status, $hook, array $args = array(), ActionScheduler_Schedule $schedule = null, $group = '' ) {
17
+	public function get_stored_action($status, $hook, array $args = array(), ActionScheduler_Schedule $schedule = null, $group = '') {
18 18
 
19
-		switch ( $status ) {
19
+		switch ($status) {
20 20
 			case ActionScheduler_Store::STATUS_PENDING :
21 21
 				$action_class = 'ActionScheduler_Action';
22 22
 				break;
23 23
 			case ActionScheduler_Store::STATUS_CANCELED :
24 24
 				$action_class = 'ActionScheduler_CanceledAction';
25
-				if ( ! is_null( $schedule ) && ! is_a( $schedule, 'ActionScheduler_CanceledSchedule' ) && ! is_a( $schedule, 'ActionScheduler_NullSchedule' ) ) {
26
-					$schedule = new ActionScheduler_CanceledSchedule( $schedule->get_date() );
25
+				if (!is_null($schedule) && !is_a($schedule, 'ActionScheduler_CanceledSchedule') && !is_a($schedule, 'ActionScheduler_NullSchedule')) {
26
+					$schedule = new ActionScheduler_CanceledSchedule($schedule->get_date());
27 27
 				}
28 28
 				break;
29 29
 			default :
@@ -31,9 +31,9 @@  discard block
 block discarded – undo
31 31
 				break;
32 32
 		}
33 33
 
34
-		$action_class = apply_filters( 'action_scheduler_stored_action_class', $action_class, $status, $hook, $args, $schedule, $group );
34
+		$action_class = apply_filters('action_scheduler_stored_action_class', $action_class, $status, $hook, $args, $schedule, $group);
35 35
 
36
-		$action = new $action_class( $hook, $args, $schedule, $group );
36
+		$action = new $action_class($hook, $args, $schedule, $group);
37 37
 
38 38
 		/**
39 39
 		 * Allow 3rd party code to change the instantiated action for a given hook, args, schedule and group.
@@ -44,7 +44,7 @@  discard block
 block discarded – undo
44 44
 		 * @param ActionScheduler_Schedule $schedule The instantiated action's schedule.
45 45
 		 * @param string $group The instantiated action's group.
46 46
 		 */
47
-		return apply_filters( 'action_scheduler_stored_action_instance', $action, $hook, $args, $schedule, $group );
47
+		return apply_filters('action_scheduler_stored_action_instance', $action, $hook, $args, $schedule, $group);
48 48
 	}
49 49
 
50 50
 	/**
@@ -63,10 +63,10 @@  discard block
 block discarded – undo
63 63
 	 *
64 64
 	 * @return int The ID of the stored action
65 65
 	 */
66
-	public function async( $hook, $args = array(), $group = '' ) {
66
+	public function async($hook, $args = array(), $group = '') {
67 67
 		$schedule = new ActionScheduler_NullSchedule();
68
-		$action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
69
-		return $this->store( $action );
68
+		$action = new ActionScheduler_Action($hook, $args, $schedule, $group);
69
+		return $this->store($action);
70 70
 	}
71 71
 
72 72
 	/**
@@ -77,11 +77,11 @@  discard block
 block discarded – undo
77 77
 	 *
78 78
 	 * @return int The ID of the stored action
79 79
 	 */
80
-	public function single( $hook, $args = array(), $when = null, $group = '' ) {
81
-		$date = as_get_datetime_object( $when );
82
-		$schedule = new ActionScheduler_SimpleSchedule( $date );
83
-		$action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
84
-		return $this->store( $action );
80
+	public function single($hook, $args = array(), $when = null, $group = '') {
81
+		$date = as_get_datetime_object($when);
82
+		$schedule = new ActionScheduler_SimpleSchedule($date);
83
+		$action = new ActionScheduler_Action($hook, $args, $schedule, $group);
84
+		return $this->store($action);
85 85
 	}
86 86
 
87 87
 	/**
@@ -95,14 +95,14 @@  discard block
 block discarded – undo
95 95
 	 *
96 96
 	 * @return int The ID of the stored action
97 97
 	 */
98
-	public function recurring( $hook, $args = array(), $first = null, $interval = null, $group = '' ) {
99
-		if ( empty($interval) ) {
100
-			return $this->single( $hook, $args, $first, $group );
98
+	public function recurring($hook, $args = array(), $first = null, $interval = null, $group = '') {
99
+		if (empty($interval)) {
100
+			return $this->single($hook, $args, $first, $group);
101 101
 		}
102
-		$date = as_get_datetime_object( $first );
103
-		$schedule = new ActionScheduler_IntervalSchedule( $date, $interval );
104
-		$action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
105
-		return $this->store( $action );
102
+		$date = as_get_datetime_object($first);
103
+		$schedule = new ActionScheduler_IntervalSchedule($date, $interval);
104
+		$action = new ActionScheduler_Action($hook, $args, $schedule, $group);
105
+		return $this->store($action);
106 106
 	}
107 107
 
108 108
 	/**
@@ -118,15 +118,15 @@  discard block
 block discarded – undo
118 118
 	 *
119 119
 	 * @return int The ID of the stored action
120 120
 	 */
121
-	public function cron( $hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '' ) {
122
-		if ( empty($schedule) ) {
123
-			return $this->single( $hook, $args, $base_timestamp, $group );
121
+	public function cron($hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '') {
122
+		if (empty($schedule)) {
123
+			return $this->single($hook, $args, $base_timestamp, $group);
124 124
 		}
125
-		$date = as_get_datetime_object( $base_timestamp );
126
-		$cron = CronExpression::factory( $schedule );
127
-		$schedule = new ActionScheduler_CronSchedule( $date, $cron );
128
-		$action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
129
-		return $this->store( $action );
125
+		$date = as_get_datetime_object($base_timestamp);
126
+		$cron = CronExpression::factory($schedule);
127
+		$schedule = new ActionScheduler_CronSchedule($date, $cron);
128
+		$action = new ActionScheduler_Action($hook, $args, $schedule, $group);
129
+		return $this->store($action);
130 130
 	}
131 131
 
132 132
 	/**
@@ -153,18 +153,18 @@  discard block
 block discarded – undo
153 153
 	 * @return string The ID of the stored action
154 154
 	 * @throws InvalidArgumentException If $action is not a recurring action.
155 155
 	 */
156
-	public function repeat( $action ) {
156
+	public function repeat($action) {
157 157
 		$schedule = $action->get_schedule();
158
-		$next     = $schedule->get_next( as_get_datetime_object() );
158
+		$next     = $schedule->get_next(as_get_datetime_object());
159 159
 
160
-		if ( is_null( $next ) || ! $schedule->is_recurring() ) {
161
-			throw new InvalidArgumentException( __( 'Invalid action - must be a recurring action.', 'woocommerce' ) );
160
+		if (is_null($next) || !$schedule->is_recurring()) {
161
+			throw new InvalidArgumentException(__('Invalid action - must be a recurring action.', 'woocommerce'));
162 162
 		}
163 163
 
164
-		$schedule_class = get_class( $schedule );
165
-		$new_schedule = new $schedule( $next, $schedule->get_recurrence(), $schedule->get_first_date() );
166
-		$new_action = new ActionScheduler_Action( $action->get_hook(), $action->get_args(), $new_schedule, $action->get_group() );
167
-		return $this->store( $new_action );
164
+		$schedule_class = get_class($schedule);
165
+		$new_schedule = new $schedule($next, $schedule->get_recurrence(), $schedule->get_first_date());
166
+		$new_action = new ActionScheduler_Action($action->get_hook(), $action->get_args(), $new_schedule, $action->get_group());
167
+		return $this->store($new_action);
168 168
 	}
169 169
 
170 170
 	/**
@@ -172,8 +172,8 @@  discard block
 block discarded – undo
172 172
 	 *
173 173
 	 * @return int The ID of the stored action
174 174
 	 */
175
-	protected function store( ActionScheduler_Action $action ) {
175
+	protected function store(ActionScheduler_Action $action) {
176 176
 		$store = ActionScheduler_Store::instance();
177
-		return $store->save_action( $action );
177
+		return $store->save_action($action);
178 178
 	}
179 179
 }
Please login to merge, or discard this patch.
packages/action-scheduler/classes/ActionScheduler_NullLogEntry.php 2 patches
Indentation   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -4,8 +4,8 @@
 block discarded – undo
4 4
  * Class ActionScheduler_NullLogEntry
5 5
  */
6 6
 class ActionScheduler_NullLogEntry extends ActionScheduler_LogEntry {
7
-	public function __construct( $action_id = '', $message = '' ) {
8
-		// nothing to see here
9
-	}
7
+    public function __construct( $action_id = '', $message = '' ) {
8
+        // nothing to see here
9
+    }
10 10
 }
11
- 
12 11
\ No newline at end of file
12
+    
13 13
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@
 block discarded – undo
4 4
  * Class ActionScheduler_NullLogEntry
5 5
  */
6 6
 class ActionScheduler_NullLogEntry extends ActionScheduler_LogEntry {
7
-	public function __construct( $action_id = '', $message = '' ) {
7
+	public function __construct($action_id = '', $message = '') {
8 8
 		// nothing to see here
9 9
 	}
10 10
 }
Please login to merge, or discard this patch.
packages/action-scheduler/classes/ActionScheduler_wcSystemStatus.php 2 patches
Indentation   +130 added lines, -130 removed lines patch added patch discarded remove patch
@@ -5,105 +5,105 @@  discard block
 block discarded – undo
5 5
  */
6 6
 class ActionScheduler_wcSystemStatus {
7 7
 
8
-	/**
9
-	 * The active data stores
10
-	 *
11
-	 * @var ActionScheduler_Store
12
-	 */
13
-	protected $store;
14
-
15
-	/**
16
-	 * Constructor method for ActionScheduler_wcSystemStatus.
17
-	 *
18
-	 * @param ActionScheduler_Store $store Active store object.
19
-	 *
20
-	 * @return void
21
-	 */
22
-	public function __construct( $store ) {
23
-		$this->store = $store;
24
-	}
25
-
26
-	/**
27
-	 * Display action data, including number of actions grouped by status and the oldest & newest action in each status.
28
-	 *
29
-	 * Helpful to identify issues, like a clogged queue.
30
-	 */
31
-	public function render() {
32
-		$action_counts     = $this->store->action_counts();
33
-		$status_labels     = $this->store->get_status_labels();
34
-		$oldest_and_newest = $this->get_oldest_and_newest( array_keys( $status_labels ) );
35
-
36
-		$this->get_template( $status_labels, $action_counts, $oldest_and_newest );
37
-	}
38
-
39
-	/**
40
-	 * Get oldest and newest scheduled dates for a given set of statuses.
41
-	 *
42
-	 * @param array $status_keys Set of statuses to find oldest & newest action for.
43
-	 * @return array
44
-	 */
45
-	protected function get_oldest_and_newest( $status_keys ) {
46
-
47
-		$oldest_and_newest = array();
48
-
49
-		foreach ( $status_keys as $status ) {
50
-			$oldest_and_newest[ $status ] = array(
51
-				'oldest' => '&ndash;',
52
-				'newest' => '&ndash;',
53
-			);
54
-
55
-			if ( 'in-progress' === $status ) {
56
-				continue;
57
-			}
58
-
59
-			$oldest_and_newest[ $status ]['oldest'] = $this->get_action_status_date( $status, 'oldest' );
60
-			$oldest_and_newest[ $status ]['newest'] = $this->get_action_status_date( $status, 'newest' );
61
-		}
62
-
63
-		return $oldest_and_newest;
64
-	}
65
-
66
-	/**
67
-	 * Get oldest or newest scheduled date for a given status.
68
-	 *
69
-	 * @param string $status Action status label/name string.
70
-	 * @param string $date_type Oldest or Newest.
71
-	 * @return DateTime
72
-	 */
73
-	protected function get_action_status_date( $status, $date_type = 'oldest' ) {
74
-
75
-		$order = 'oldest' === $date_type ? 'ASC' : 'DESC';
76
-
77
-		$action = $this->store->query_actions(
78
-			array(
79
-				'claimed'  => false,
80
-				'status'   => $status,
81
-				'per_page' => 1,
82
-				'order'    => $order,
83
-			)
84
-		);
85
-
86
-		if ( ! empty( $action ) ) {
87
-			$date_object = $this->store->get_date( $action[0] );
88
-			$action_date = $date_object->format( 'Y-m-d H:i:s O' );
89
-		} else {
90
-			$action_date = '&ndash;';
91
-		}
92
-
93
-		return $action_date;
94
-	}
95
-
96
-	/**
97
-	 * Get oldest or newest scheduled date for a given status.
98
-	 *
99
-	 * @param array $status_labels Set of statuses to find oldest & newest action for.
100
-	 * @param array $action_counts Number of actions grouped by status.
101
-	 * @param array $oldest_and_newest Date of the oldest and newest action with each status.
102
-	 */
103
-	protected function get_template( $status_labels, $action_counts, $oldest_and_newest ) {
104
-		$as_version   = ActionScheduler_Versions::instance()->latest_version();
105
-		$as_datastore = get_class( ActionScheduler_Store::instance() );
106
-		?>
8
+    /**
9
+     * The active data stores
10
+     *
11
+     * @var ActionScheduler_Store
12
+     */
13
+    protected $store;
14
+
15
+    /**
16
+     * Constructor method for ActionScheduler_wcSystemStatus.
17
+     *
18
+     * @param ActionScheduler_Store $store Active store object.
19
+     *
20
+     * @return void
21
+     */
22
+    public function __construct( $store ) {
23
+        $this->store = $store;
24
+    }
25
+
26
+    /**
27
+     * Display action data, including number of actions grouped by status and the oldest & newest action in each status.
28
+     *
29
+     * Helpful to identify issues, like a clogged queue.
30
+     */
31
+    public function render() {
32
+        $action_counts     = $this->store->action_counts();
33
+        $status_labels     = $this->store->get_status_labels();
34
+        $oldest_and_newest = $this->get_oldest_and_newest( array_keys( $status_labels ) );
35
+
36
+        $this->get_template( $status_labels, $action_counts, $oldest_and_newest );
37
+    }
38
+
39
+    /**
40
+     * Get oldest and newest scheduled dates for a given set of statuses.
41
+     *
42
+     * @param array $status_keys Set of statuses to find oldest & newest action for.
43
+     * @return array
44
+     */
45
+    protected function get_oldest_and_newest( $status_keys ) {
46
+
47
+        $oldest_and_newest = array();
48
+
49
+        foreach ( $status_keys as $status ) {
50
+            $oldest_and_newest[ $status ] = array(
51
+                'oldest' => '&ndash;',
52
+                'newest' => '&ndash;',
53
+            );
54
+
55
+            if ( 'in-progress' === $status ) {
56
+                continue;
57
+            }
58
+
59
+            $oldest_and_newest[ $status ]['oldest'] = $this->get_action_status_date( $status, 'oldest' );
60
+            $oldest_and_newest[ $status ]['newest'] = $this->get_action_status_date( $status, 'newest' );
61
+        }
62
+
63
+        return $oldest_and_newest;
64
+    }
65
+
66
+    /**
67
+     * Get oldest or newest scheduled date for a given status.
68
+     *
69
+     * @param string $status Action status label/name string.
70
+     * @param string $date_type Oldest or Newest.
71
+     * @return DateTime
72
+     */
73
+    protected function get_action_status_date( $status, $date_type = 'oldest' ) {
74
+
75
+        $order = 'oldest' === $date_type ? 'ASC' : 'DESC';
76
+
77
+        $action = $this->store->query_actions(
78
+            array(
79
+                'claimed'  => false,
80
+                'status'   => $status,
81
+                'per_page' => 1,
82
+                'order'    => $order,
83
+            )
84
+        );
85
+
86
+        if ( ! empty( $action ) ) {
87
+            $date_object = $this->store->get_date( $action[0] );
88
+            $action_date = $date_object->format( 'Y-m-d H:i:s O' );
89
+        } else {
90
+            $action_date = '&ndash;';
91
+        }
92
+
93
+        return $action_date;
94
+    }
95
+
96
+    /**
97
+     * Get oldest or newest scheduled date for a given status.
98
+     *
99
+     * @param array $status_labels Set of statuses to find oldest & newest action for.
100
+     * @param array $action_counts Number of actions grouped by status.
101
+     * @param array $oldest_and_newest Date of the oldest and newest action with each status.
102
+     */
103
+    protected function get_template( $status_labels, $action_counts, $oldest_and_newest ) {
104
+        $as_version   = ActionScheduler_Versions::instance()->latest_version();
105
+        $as_datastore = get_class( ActionScheduler_Store::instance() );
106
+        ?>
107 107
 
108 108
 		<table class="wc_status_table widefat" cellspacing="0">
109 109
 			<thead>
@@ -128,39 +128,39 @@  discard block
 block discarded – undo
128 128
 			</thead>
129 129
 			<tbody>
130 130
 				<?php
131
-				foreach ( $action_counts as $status => $count ) {
132
-					// WC uses the 3rd column for export, so we need to display more data in that (hidden when viewed as part of the table) and add an empty 2nd column.
133
-					printf(
134
-						'<tr><td>%1$s</td><td>&nbsp;</td><td>%2$s<span style="display: none;">, Oldest: %3$s, Newest: %4$s</span></td><td>%3$s</td><td>%4$s</td></tr>',
135
-						esc_html( $status_labels[ $status ] ),
136
-						esc_html( number_format_i18n( $count ) ),
137
-						esc_html( $oldest_and_newest[ $status ]['oldest'] ),
138
-						esc_html( $oldest_and_newest[ $status ]['newest'] )
139
-					);
140
-				}
141
-				?>
131
+                foreach ( $action_counts as $status => $count ) {
132
+                    // WC uses the 3rd column for export, so we need to display more data in that (hidden when viewed as part of the table) and add an empty 2nd column.
133
+                    printf(
134
+                        '<tr><td>%1$s</td><td>&nbsp;</td><td>%2$s<span style="display: none;">, Oldest: %3$s, Newest: %4$s</span></td><td>%3$s</td><td>%4$s</td></tr>',
135
+                        esc_html( $status_labels[ $status ] ),
136
+                        esc_html( number_format_i18n( $count ) ),
137
+                        esc_html( $oldest_and_newest[ $status ]['oldest'] ),
138
+                        esc_html( $oldest_and_newest[ $status ]['newest'] )
139
+                    );
140
+                }
141
+                ?>
142 142
 			</tbody>
143 143
 		</table>
144 144
 
145 145
 		<?php
146
-	}
147
-
148
-	/**
149
-	 * Is triggered when invoking inaccessible methods in an object context.
150
-	 *
151
-	 * @param string $name Name of method called.
152
-	 * @param array  $arguments Parameters to invoke the method with.
153
-	 *
154
-	 * @return mixed
155
-	 * @link https://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods
156
-	 */
157
-	public function __call( $name, $arguments ) {
158
-		switch ( $name ) {
159
-			case 'print':
160
-				_deprecated_function( __CLASS__ . '::print()', '2.2.4', __CLASS__ . '::render()' );
161
-				return call_user_func_array( array( $this, 'render' ), $arguments );
162
-		}
163
-
164
-		return null;
165
-	}
146
+    }
147
+
148
+    /**
149
+     * Is triggered when invoking inaccessible methods in an object context.
150
+     *
151
+     * @param string $name Name of method called.
152
+     * @param array  $arguments Parameters to invoke the method with.
153
+     *
154
+     * @return mixed
155
+     * @link https://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods
156
+     */
157
+    public function __call( $name, $arguments ) {
158
+        switch ( $name ) {
159
+            case 'print':
160
+                _deprecated_function( __CLASS__ . '::print()', '2.2.4', __CLASS__ . '::render()' );
161
+                return call_user_func_array( array( $this, 'render' ), $arguments );
162
+        }
163
+
164
+        return null;
165
+    }
166 166
 }
Please login to merge, or discard this patch.
Spacing   +33 added lines, -33 removed lines patch added patch discarded remove patch
@@ -19,7 +19,7 @@  discard block
 block discarded – undo
19 19
 	 *
20 20
 	 * @return void
21 21
 	 */
22
-	public function __construct( $store ) {
22
+	public function __construct($store) {
23 23
 		$this->store = $store;
24 24
 	}
25 25
 
@@ -31,9 +31,9 @@  discard block
 block discarded – undo
31 31
 	public function render() {
32 32
 		$action_counts     = $this->store->action_counts();
33 33
 		$status_labels     = $this->store->get_status_labels();
34
-		$oldest_and_newest = $this->get_oldest_and_newest( array_keys( $status_labels ) );
34
+		$oldest_and_newest = $this->get_oldest_and_newest(array_keys($status_labels));
35 35
 
36
-		$this->get_template( $status_labels, $action_counts, $oldest_and_newest );
36
+		$this->get_template($status_labels, $action_counts, $oldest_and_newest);
37 37
 	}
38 38
 
39 39
 	/**
@@ -42,22 +42,22 @@  discard block
 block discarded – undo
42 42
 	 * @param array $status_keys Set of statuses to find oldest & newest action for.
43 43
 	 * @return array
44 44
 	 */
45
-	protected function get_oldest_and_newest( $status_keys ) {
45
+	protected function get_oldest_and_newest($status_keys) {
46 46
 
47 47
 		$oldest_and_newest = array();
48 48
 
49
-		foreach ( $status_keys as $status ) {
50
-			$oldest_and_newest[ $status ] = array(
49
+		foreach ($status_keys as $status) {
50
+			$oldest_and_newest[$status] = array(
51 51
 				'oldest' => '&ndash;',
52 52
 				'newest' => '&ndash;',
53 53
 			);
54 54
 
55
-			if ( 'in-progress' === $status ) {
55
+			if ('in-progress' === $status) {
56 56
 				continue;
57 57
 			}
58 58
 
59
-			$oldest_and_newest[ $status ]['oldest'] = $this->get_action_status_date( $status, 'oldest' );
60
-			$oldest_and_newest[ $status ]['newest'] = $this->get_action_status_date( $status, 'newest' );
59
+			$oldest_and_newest[$status]['oldest'] = $this->get_action_status_date($status, 'oldest');
60
+			$oldest_and_newest[$status]['newest'] = $this->get_action_status_date($status, 'newest');
61 61
 		}
62 62
 
63 63
 		return $oldest_and_newest;
@@ -70,7 +70,7 @@  discard block
 block discarded – undo
70 70
 	 * @param string $date_type Oldest or Newest.
71 71
 	 * @return DateTime
72 72
 	 */
73
-	protected function get_action_status_date( $status, $date_type = 'oldest' ) {
73
+	protected function get_action_status_date($status, $date_type = 'oldest') {
74 74
 
75 75
 		$order = 'oldest' === $date_type ? 'ASC' : 'DESC';
76 76
 
@@ -83,9 +83,9 @@  discard block
 block discarded – undo
83 83
 			)
84 84
 		);
85 85
 
86
-		if ( ! empty( $action ) ) {
87
-			$date_object = $this->store->get_date( $action[0] );
88
-			$action_date = $date_object->format( 'Y-m-d H:i:s O' );
86
+		if (!empty($action)) {
87
+			$date_object = $this->store->get_date($action[0]);
88
+			$action_date = $date_object->format('Y-m-d H:i:s O');
89 89
 		} else {
90 90
 			$action_date = '&ndash;';
91 91
 		}
@@ -100,42 +100,42 @@  discard block
 block discarded – undo
100 100
 	 * @param array $action_counts Number of actions grouped by status.
101 101
 	 * @param array $oldest_and_newest Date of the oldest and newest action with each status.
102 102
 	 */
103
-	protected function get_template( $status_labels, $action_counts, $oldest_and_newest ) {
103
+	protected function get_template($status_labels, $action_counts, $oldest_and_newest) {
104 104
 		$as_version   = ActionScheduler_Versions::instance()->latest_version();
105
-		$as_datastore = get_class( ActionScheduler_Store::instance() );
105
+		$as_datastore = get_class(ActionScheduler_Store::instance());
106 106
 		?>
107 107
 
108 108
 		<table class="wc_status_table widefat" cellspacing="0">
109 109
 			<thead>
110 110
 				<tr>
111
-					<th colspan="5" data-export-label="Action Scheduler"><h2><?php esc_html_e( 'Action Scheduler', 'woocommerce' ); ?><?php echo wc_help_tip( esc_html__( 'This section shows details of Action Scheduler.', 'woocommerce' ) ); ?></h2></th>
111
+					<th colspan="5" data-export-label="Action Scheduler"><h2><?php esc_html_e('Action Scheduler', 'woocommerce'); ?><?php echo wc_help_tip(esc_html__('This section shows details of Action Scheduler.', 'woocommerce')); ?></h2></th>
112 112
 				</tr>
113 113
 				<tr>
114
-					<td colspan="2" data-export-label="Version"><?php esc_html_e( 'Version:', 'woocommerce' ); ?></td>
115
-					<td colspan="3"><?php echo esc_html( $as_version ); ?></td>
114
+					<td colspan="2" data-export-label="Version"><?php esc_html_e('Version:', 'woocommerce'); ?></td>
115
+					<td colspan="3"><?php echo esc_html($as_version); ?></td>
116 116
 				</tr>
117 117
 				<tr>
118
-					<td colspan="2" data-export-label="Data store"><?php esc_html_e( 'Data store:', 'woocommerce' ); ?></td>
119
-					<td colspan="3"><?php echo esc_html( $as_datastore ); ?></td>
118
+					<td colspan="2" data-export-label="Data store"><?php esc_html_e('Data store:', 'woocommerce'); ?></td>
119
+					<td colspan="3"><?php echo esc_html($as_datastore); ?></td>
120 120
 				</tr>
121 121
 				<tr>
122
-					<td><strong><?php esc_html_e( 'Action Status', 'woocommerce' ); ?></strong></td>
122
+					<td><strong><?php esc_html_e('Action Status', 'woocommerce'); ?></strong></td>
123 123
 					<td class="help">&nbsp;</td>
124
-					<td><strong><?php esc_html_e( 'Count', 'woocommerce' ); ?></strong></td>
125
-					<td><strong><?php esc_html_e( 'Oldest Scheduled Date', 'woocommerce' ); ?></strong></td>
126
-					<td><strong><?php esc_html_e( 'Newest Scheduled Date', 'woocommerce' ); ?></strong></td>
124
+					<td><strong><?php esc_html_e('Count', 'woocommerce'); ?></strong></td>
125
+					<td><strong><?php esc_html_e('Oldest Scheduled Date', 'woocommerce'); ?></strong></td>
126
+					<td><strong><?php esc_html_e('Newest Scheduled Date', 'woocommerce'); ?></strong></td>
127 127
 				</tr>
128 128
 			</thead>
129 129
 			<tbody>
130 130
 				<?php
131
-				foreach ( $action_counts as $status => $count ) {
131
+				foreach ($action_counts as $status => $count) {
132 132
 					// WC uses the 3rd column for export, so we need to display more data in that (hidden when viewed as part of the table) and add an empty 2nd column.
133 133
 					printf(
134 134
 						'<tr><td>%1$s</td><td>&nbsp;</td><td>%2$s<span style="display: none;">, Oldest: %3$s, Newest: %4$s</span></td><td>%3$s</td><td>%4$s</td></tr>',
135
-						esc_html( $status_labels[ $status ] ),
136
-						esc_html( number_format_i18n( $count ) ),
137
-						esc_html( $oldest_and_newest[ $status ]['oldest'] ),
138
-						esc_html( $oldest_and_newest[ $status ]['newest'] )
135
+						esc_html($status_labels[$status]),
136
+						esc_html(number_format_i18n($count)),
137
+						esc_html($oldest_and_newest[$status]['oldest']),
138
+						esc_html($oldest_and_newest[$status]['newest'])
139 139
 					);
140 140
 				}
141 141
 				?>
@@ -154,11 +154,11 @@  discard block
 block discarded – undo
154 154
 	 * @return mixed
155 155
 	 * @link https://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods
156 156
 	 */
157
-	public function __call( $name, $arguments ) {
158
-		switch ( $name ) {
157
+	public function __call($name, $arguments) {
158
+		switch ($name) {
159 159
 			case 'print':
160
-				_deprecated_function( __CLASS__ . '::print()', '2.2.4', __CLASS__ . '::render()' );
161
-				return call_user_func_array( array( $this, 'render' ), $arguments );
160
+				_deprecated_function(__CLASS__ . '::print()', '2.2.4', __CLASS__ . '::render()');
161
+				return call_user_func_array(array($this, 'render'), $arguments);
162 162
 		}
163 163
 
164 164
 		return null;
Please login to merge, or discard this patch.
plugins/woocommerce/packages/action-scheduler/classes/migration/Runner.php 2 patches
Indentation   +120 added lines, -120 removed lines patch added patch discarded remove patch
@@ -13,124 +13,124 @@
 block discarded – undo
13 13
  * @codeCoverageIgnore
14 14
  */
15 15
 class Runner {
16
-	/** @var ActionScheduler_Store */
17
-	private $source_store;
18
-
19
-	/** @var ActionScheduler_Store */
20
-	private $destination_store;
21
-
22
-	/** @var ActionScheduler_Logger */
23
-	private $source_logger;
24
-
25
-	/** @var ActionScheduler_Logger */
26
-	private $destination_logger;
27
-
28
-	/** @var BatchFetcher */
29
-	private $batch_fetcher;
30
-
31
-	/** @var ActionMigrator */
32
-	private $action_migrator;
33
-
34
-	/** @var LogMigrator */
35
-	private $log_migrator;
36
-
37
-	/** @var ProgressBar */
38
-	private $progress_bar;
39
-
40
-	/**
41
-	 * Runner constructor.
42
-	 *
43
-	 * @param Config $config Migration configuration object.
44
-	 */
45
-	public function __construct( Config $config ) {
46
-		$this->source_store       = $config->get_source_store();
47
-		$this->destination_store  = $config->get_destination_store();
48
-		$this->source_logger      = $config->get_source_logger();
49
-		$this->destination_logger = $config->get_destination_logger();
50
-
51
-		$this->batch_fetcher = new BatchFetcher( $this->source_store );
52
-		if ( $config->get_dry_run() ) {
53
-			$this->log_migrator    = new DryRun_LogMigrator( $this->source_logger, $this->destination_logger );
54
-			$this->action_migrator = new DryRun_ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator );
55
-		} else {
56
-			$this->log_migrator    = new LogMigrator( $this->source_logger, $this->destination_logger );
57
-			$this->action_migrator = new ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator );
58
-		}
59
-
60
-		if ( defined( 'WP_CLI' ) && WP_CLI ) {
61
-			$this->progress_bar = $config->get_progress_bar();
62
-		}
63
-	}
64
-
65
-	/**
66
-	 * Run migration batch.
67
-	 *
68
-	 * @param int $batch_size Optional batch size. Default 10.
69
-	 *
70
-	 * @return int Size of batch processed.
71
-	 */
72
-	public function run( $batch_size = 10 ) {
73
-		$batch = $this->batch_fetcher->fetch( $batch_size );
74
-		$batch_size = count( $batch );
75
-
76
-		if ( ! $batch_size ) {
77
-			return 0;
78
-		}
79
-
80
-		if ( $this->progress_bar ) {
81
-			/* translators: %d: amount of actions */
82
-			$this->progress_bar->set_message( sprintf( _n( 'Migrating %d action', 'Migrating %d actions', $batch_size, 'woocommerce' ), number_format_i18n( $batch_size ) ) );
83
-			$this->progress_bar->set_count( $batch_size );
84
-		}
85
-
86
-		$this->migrate_actions( $batch );
87
-
88
-		return $batch_size;
89
-	}
90
-
91
-	/**
92
-	 * Migration a batch of actions.
93
-	 *
94
-	 * @param array $action_ids List of action IDs to migrate.
95
-	 */
96
-	public function migrate_actions( array $action_ids ) {
97
-		do_action( 'action_scheduler/migration_batch_starting', $action_ids );
98
-
99
-		\ActionScheduler::logger()->unhook_stored_action();
100
-		$this->destination_logger->unhook_stored_action();
101
-
102
-		foreach ( $action_ids as $source_action_id ) {
103
-			$destination_action_id = $this->action_migrator->migrate( $source_action_id );
104
-			if ( $destination_action_id ) {
105
-				$this->destination_logger->log( $destination_action_id, sprintf(
106
-					/* translators: 1: source action ID 2: source store class 3: destination action ID 4: destination store class */
107
-					__( 'Migrated action with ID %1$d in %2$s to ID %3$d in %4$s', 'woocommerce' ),
108
-					$source_action_id,
109
-					get_class( $this->source_store ),
110
-					$destination_action_id,
111
-					get_class( $this->destination_store )
112
-				) );
113
-			}
114
-
115
-			if ( $this->progress_bar ) {
116
-				$this->progress_bar->tick();
117
-			}
118
-		}
119
-
120
-		if ( $this->progress_bar ) {
121
-			$this->progress_bar->finish();
122
-		}
123
-
124
-		\ActionScheduler::logger()->hook_stored_action();
125
-
126
-		do_action( 'action_scheduler/migration_batch_complete', $action_ids );
127
-	}
128
-
129
-	/**
130
-	 * Initialize destination store and logger.
131
-	 */
132
-	public function init_destination() {
133
-		$this->destination_store->init();
134
-		$this->destination_logger->init();
135
-	}
16
+    /** @var ActionScheduler_Store */
17
+    private $source_store;
18
+
19
+    /** @var ActionScheduler_Store */
20
+    private $destination_store;
21
+
22
+    /** @var ActionScheduler_Logger */
23
+    private $source_logger;
24
+
25
+    /** @var ActionScheduler_Logger */
26
+    private $destination_logger;
27
+
28
+    /** @var BatchFetcher */
29
+    private $batch_fetcher;
30
+
31
+    /** @var ActionMigrator */
32
+    private $action_migrator;
33
+
34
+    /** @var LogMigrator */
35
+    private $log_migrator;
36
+
37
+    /** @var ProgressBar */
38
+    private $progress_bar;
39
+
40
+    /**
41
+     * Runner constructor.
42
+     *
43
+     * @param Config $config Migration configuration object.
44
+     */
45
+    public function __construct( Config $config ) {
46
+        $this->source_store       = $config->get_source_store();
47
+        $this->destination_store  = $config->get_destination_store();
48
+        $this->source_logger      = $config->get_source_logger();
49
+        $this->destination_logger = $config->get_destination_logger();
50
+
51
+        $this->batch_fetcher = new BatchFetcher( $this->source_store );
52
+        if ( $config->get_dry_run() ) {
53
+            $this->log_migrator    = new DryRun_LogMigrator( $this->source_logger, $this->destination_logger );
54
+            $this->action_migrator = new DryRun_ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator );
55
+        } else {
56
+            $this->log_migrator    = new LogMigrator( $this->source_logger, $this->destination_logger );
57
+            $this->action_migrator = new ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator );
58
+        }
59
+
60
+        if ( defined( 'WP_CLI' ) && WP_CLI ) {
61
+            $this->progress_bar = $config->get_progress_bar();
62
+        }
63
+    }
64
+
65
+    /**
66
+     * Run migration batch.
67
+     *
68
+     * @param int $batch_size Optional batch size. Default 10.
69
+     *
70
+     * @return int Size of batch processed.
71
+     */
72
+    public function run( $batch_size = 10 ) {
73
+        $batch = $this->batch_fetcher->fetch( $batch_size );
74
+        $batch_size = count( $batch );
75
+
76
+        if ( ! $batch_size ) {
77
+            return 0;
78
+        }
79
+
80
+        if ( $this->progress_bar ) {
81
+            /* translators: %d: amount of actions */
82
+            $this->progress_bar->set_message( sprintf( _n( 'Migrating %d action', 'Migrating %d actions', $batch_size, 'woocommerce' ), number_format_i18n( $batch_size ) ) );
83
+            $this->progress_bar->set_count( $batch_size );
84
+        }
85
+
86
+        $this->migrate_actions( $batch );
87
+
88
+        return $batch_size;
89
+    }
90
+
91
+    /**
92
+     * Migration a batch of actions.
93
+     *
94
+     * @param array $action_ids List of action IDs to migrate.
95
+     */
96
+    public function migrate_actions( array $action_ids ) {
97
+        do_action( 'action_scheduler/migration_batch_starting', $action_ids );
98
+
99
+        \ActionScheduler::logger()->unhook_stored_action();
100
+        $this->destination_logger->unhook_stored_action();
101
+
102
+        foreach ( $action_ids as $source_action_id ) {
103
+            $destination_action_id = $this->action_migrator->migrate( $source_action_id );
104
+            if ( $destination_action_id ) {
105
+                $this->destination_logger->log( $destination_action_id, sprintf(
106
+                    /* translators: 1: source action ID 2: source store class 3: destination action ID 4: destination store class */
107
+                    __( 'Migrated action with ID %1$d in %2$s to ID %3$d in %4$s', 'woocommerce' ),
108
+                    $source_action_id,
109
+                    get_class( $this->source_store ),
110
+                    $destination_action_id,
111
+                    get_class( $this->destination_store )
112
+                ) );
113
+            }
114
+
115
+            if ( $this->progress_bar ) {
116
+                $this->progress_bar->tick();
117
+            }
118
+        }
119
+
120
+        if ( $this->progress_bar ) {
121
+            $this->progress_bar->finish();
122
+        }
123
+
124
+        \ActionScheduler::logger()->hook_stored_action();
125
+
126
+        do_action( 'action_scheduler/migration_batch_complete', $action_ids );
127
+    }
128
+
129
+    /**
130
+     * Initialize destination store and logger.
131
+     */
132
+    public function init_destination() {
133
+        $this->destination_store->init();
134
+        $this->destination_logger->init();
135
+    }
136 136
 }
Please login to merge, or discard this patch.
Spacing   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -42,22 +42,22 @@  discard block
 block discarded – undo
42 42
 	 *
43 43
 	 * @param Config $config Migration configuration object.
44 44
 	 */
45
-	public function __construct( Config $config ) {
45
+	public function __construct(Config $config) {
46 46
 		$this->source_store       = $config->get_source_store();
47 47
 		$this->destination_store  = $config->get_destination_store();
48 48
 		$this->source_logger      = $config->get_source_logger();
49 49
 		$this->destination_logger = $config->get_destination_logger();
50 50
 
51
-		$this->batch_fetcher = new BatchFetcher( $this->source_store );
52
-		if ( $config->get_dry_run() ) {
53
-			$this->log_migrator    = new DryRun_LogMigrator( $this->source_logger, $this->destination_logger );
54
-			$this->action_migrator = new DryRun_ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator );
51
+		$this->batch_fetcher = new BatchFetcher($this->source_store);
52
+		if ($config->get_dry_run()) {
53
+			$this->log_migrator    = new DryRun_LogMigrator($this->source_logger, $this->destination_logger);
54
+			$this->action_migrator = new DryRun_ActionMigrator($this->source_store, $this->destination_store, $this->log_migrator);
55 55
 		} else {
56
-			$this->log_migrator    = new LogMigrator( $this->source_logger, $this->destination_logger );
57
-			$this->action_migrator = new ActionMigrator( $this->source_store, $this->destination_store, $this->log_migrator );
56
+			$this->log_migrator    = new LogMigrator($this->source_logger, $this->destination_logger);
57
+			$this->action_migrator = new ActionMigrator($this->source_store, $this->destination_store, $this->log_migrator);
58 58
 		}
59 59
 
60
-		if ( defined( 'WP_CLI' ) && WP_CLI ) {
60
+		if (defined('WP_CLI') && WP_CLI) {
61 61
 			$this->progress_bar = $config->get_progress_bar();
62 62
 		}
63 63
 	}
@@ -69,21 +69,21 @@  discard block
 block discarded – undo
69 69
 	 *
70 70
 	 * @return int Size of batch processed.
71 71
 	 */
72
-	public function run( $batch_size = 10 ) {
73
-		$batch = $this->batch_fetcher->fetch( $batch_size );
74
-		$batch_size = count( $batch );
72
+	public function run($batch_size = 10) {
73
+		$batch = $this->batch_fetcher->fetch($batch_size);
74
+		$batch_size = count($batch);
75 75
 
76
-		if ( ! $batch_size ) {
76
+		if (!$batch_size) {
77 77
 			return 0;
78 78
 		}
79 79
 
80
-		if ( $this->progress_bar ) {
80
+		if ($this->progress_bar) {
81 81
 			/* translators: %d: amount of actions */
82
-			$this->progress_bar->set_message( sprintf( _n( 'Migrating %d action', 'Migrating %d actions', $batch_size, 'woocommerce' ), number_format_i18n( $batch_size ) ) );
83
-			$this->progress_bar->set_count( $batch_size );
82
+			$this->progress_bar->set_message(sprintf(_n('Migrating %d action', 'Migrating %d actions', $batch_size, 'woocommerce'), number_format_i18n($batch_size)));
83
+			$this->progress_bar->set_count($batch_size);
84 84
 		}
85 85
 
86
-		$this->migrate_actions( $batch );
86
+		$this->migrate_actions($batch);
87 87
 
88 88
 		return $batch_size;
89 89
 	}
@@ -93,37 +93,37 @@  discard block
 block discarded – undo
93 93
 	 *
94 94
 	 * @param array $action_ids List of action IDs to migrate.
95 95
 	 */
96
-	public function migrate_actions( array $action_ids ) {
97
-		do_action( 'action_scheduler/migration_batch_starting', $action_ids );
96
+	public function migrate_actions(array $action_ids) {
97
+		do_action('action_scheduler/migration_batch_starting', $action_ids);
98 98
 
99 99
 		\ActionScheduler::logger()->unhook_stored_action();
100 100
 		$this->destination_logger->unhook_stored_action();
101 101
 
102
-		foreach ( $action_ids as $source_action_id ) {
103
-			$destination_action_id = $this->action_migrator->migrate( $source_action_id );
104
-			if ( $destination_action_id ) {
105
-				$this->destination_logger->log( $destination_action_id, sprintf(
102
+		foreach ($action_ids as $source_action_id) {
103
+			$destination_action_id = $this->action_migrator->migrate($source_action_id);
104
+			if ($destination_action_id) {
105
+				$this->destination_logger->log($destination_action_id, sprintf(
106 106
 					/* translators: 1: source action ID 2: source store class 3: destination action ID 4: destination store class */
107
-					__( 'Migrated action with ID %1$d in %2$s to ID %3$d in %4$s', 'woocommerce' ),
107
+					__('Migrated action with ID %1$d in %2$s to ID %3$d in %4$s', 'woocommerce'),
108 108
 					$source_action_id,
109
-					get_class( $this->source_store ),
109
+					get_class($this->source_store),
110 110
 					$destination_action_id,
111
-					get_class( $this->destination_store )
112
-				) );
111
+					get_class($this->destination_store)
112
+				));
113 113
 			}
114 114
 
115
-			if ( $this->progress_bar ) {
115
+			if ($this->progress_bar) {
116 116
 				$this->progress_bar->tick();
117 117
 			}
118 118
 		}
119 119
 
120
-		if ( $this->progress_bar ) {
120
+		if ($this->progress_bar) {
121 121
 			$this->progress_bar->finish();
122 122
 		}
123 123
 
124 124
 		\ActionScheduler::logger()->hook_stored_action();
125 125
 
126
-		do_action( 'action_scheduler/migration_batch_complete', $action_ids );
126
+		do_action('action_scheduler/migration_batch_complete', $action_ids);
127 127
 	}
128 128
 
129 129
 	/**
Please login to merge, or discard this patch.