Cyr-To-Lat - Version 3.6.5

Version Description

(11.02.2019) = * Added queues for background slug conversion process

Download this release

Release Info

Developer mihdan
Plugin Icon 128x128 Cyr-To-Lat
Version 3.6.5
Comparing to
See all releases

Code changes from version 3.6.4 to 3.6.5

cyr-to-lat.php CHANGED
@@ -9,8 +9,8 @@
9
  * Author URI: https://profiles.wordpress.org/sergeybiryukov/
10
  * Requires at least: 2.3
11
  * Tested up to: 5.1
12
- * Version: 3.6.4
13
- * Stable tag: 3.6.4
14
  *
15
  * Text Domain: cyr-to-lat
16
  * Domain Path: /languages/
@@ -23,6 +23,11 @@ if ( ! defined( 'ABSPATH' ) ) {
23
  exit; // Exit if accessed directly.
24
  }
25
 
 
 
 
 
 
26
  /**
27
  * Path to the plugin dir.
28
  */
@@ -39,9 +44,19 @@ define( 'CYR_TO_LAT_URL', untrailingslashit( plugin_dir_url( __FILE__ ) ) );
39
  define( 'CYR_TO_LAT_FILE', __FILE__ );
40
 
41
  /**
42
- * Plugin version.
 
 
 
 
 
 
 
 
 
 
43
  */
44
- define( 'CYR_TO_LAT_VERSION', '3.6.4' );
45
 
46
  /**
47
  * Init plugin class on plugin load.
9
  * Author URI: https://profiles.wordpress.org/sergeybiryukov/
10
  * Requires at least: 2.3
11
  * Tested up to: 5.1
12
+ * Version: 3.6.5
13
+ * Stable tag: 3.6.5
14
  *
15
  * Text Domain: cyr-to-lat
16
  * Domain Path: /languages/
23
  exit; // Exit if accessed directly.
24
  }
25
 
26
+ /**
27
+ * Plugin version.
28
+ */
29
+ define( 'CYR_TO_LAT_VERSION', '3.6.5' );
30
+
31
  /**
32
  * Path to the plugin dir.
33
  */
44
  define( 'CYR_TO_LAT_FILE', __FILE__ );
45
 
46
  /**
47
+ * Plugin prefix.
48
+ */
49
+ define( 'CYR_TO_LAT_PREFIX', 'cyr_to_lat' );
50
+
51
+ /**
52
+ * Post conversion action.
53
+ */
54
+ define( 'CYR_TO_LAT_POST_CONVERSION_ACTION', 'post_conversion_action' );
55
+
56
+ /**
57
+ * Term conversion action.
58
  */
59
+ define( 'CYR_TO_LAT_TERM_CONVERSION_ACTION', 'term_conversion_action' );
60
 
61
  /**
62
  * Init plugin class on plugin load.
includes/background-processes/class-cyr-to-lat-post-conversion-process.php ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Background old post slugs converting process.
4
+ *
5
+ * @package cyr-to-lat
6
+ */
7
+
8
+ /**
9
+ * Class Cyr_To_Lat_Post_Conversion_Process
10
+ */
11
+ class Cyr_To_Lat_Post_Conversion_Process extends WP_Background_Process {
12
+
13
+ /**
14
+ * Prefix.
15
+ *
16
+ * @var string
17
+ */
18
+ protected $prefix = CYR_TO_LAT_PREFIX;
19
+
20
+ /**
21
+ * Process action name.
22
+ *
23
+ * @var string
24
+ */
25
+ protected $action = CYR_TO_LAT_POST_CONVERSION_ACTION;
26
+
27
+ /**
28
+ * Plugin main class.
29
+ *
30
+ * @var Cyr_To_Lat_Main
31
+ */
32
+ private $main;
33
+
34
+ /**
35
+ * Cyr_To_Lat_Post_Conversion_Process constructor.
36
+ *
37
+ * @param Cyr_To_Lat_Main $main Plugin main class.
38
+ */
39
+ public function __construct( $main ) {
40
+ $this->main = $main;
41
+
42
+ parent::__construct();
43
+ }
44
+
45
+ /**
46
+ * Task. Updates single post.
47
+ *
48
+ * @param stdClass $post Queue item to iterate over.
49
+ *
50
+ * @return mixed
51
+ */
52
+ protected function task( $post ) {
53
+ global $wpdb;
54
+
55
+ $sanitized_name = $this->main->ctl_sanitize_title( $post->post_name );
56
+
57
+ if ( $sanitized_name !== $post->post_name ) {
58
+ add_post_meta( $post->ID, '_wp_old_slug', $post->post_name );
59
+ // phpcs:disable WordPress.DB.DirectDatabaseQuery
60
+ $wpdb->update( $wpdb->posts, array( 'post_name' => $sanitized_name ), array( 'ID' => $post->ID ) );
61
+ // phpcs:enable
62
+ }
63
+
64
+ $this->log( $post->post_name . ' => ' . $sanitized_name );
65
+
66
+ return false;
67
+ }
68
+
69
+ /**
70
+ * Complete
71
+ */
72
+ protected function complete() {
73
+ parent::complete();
74
+
75
+ $this->log( 'Post slugs conversion completed.' );
76
+ }
77
+
78
+ /**
79
+ * Log.
80
+ *
81
+ * @param string $message Message to log.
82
+ */
83
+ public function log( $message ) {
84
+ if ( WP_DEBUG_LOG ) {
85
+ error_log( $message );
86
+ }
87
+ }
88
+ }
includes/background-processes/class-cyr-to-lat-term-conversion-process.php ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Background old term slugs converting process.
4
+ *
5
+ * @package cyr-to-lat
6
+ */
7
+
8
+ /**
9
+ * Class Cyr_To_Lat_Term_Conversion_Process
10
+ */
11
+ class Cyr_To_Lat_Term_Conversion_Process extends WP_Background_Process {
12
+
13
+ /**
14
+ * Prefix.
15
+ *
16
+ * @var string
17
+ */
18
+ protected $prefix = CYR_TO_LAT_PREFIX;
19
+
20
+ /**
21
+ * Process action name.
22
+ *
23
+ * @var string
24
+ */
25
+ protected $action = CYR_TO_LAT_TERM_CONVERSION_ACTION;
26
+
27
+ /**
28
+ * Plugin main class.
29
+ *
30
+ * @var Cyr_To_Lat_Main
31
+ */
32
+ private $main;
33
+
34
+ /**
35
+ * Cyr_To_Lat_Post_Conversion_Process constructor.
36
+ *
37
+ * @param Cyr_To_Lat_Main $main Plugin main class.
38
+ */
39
+ public function __construct( $main ) {
40
+ $this->main = $main;
41
+
42
+ parent::__construct();
43
+ }
44
+
45
+ /**
46
+ * Task. Updates single term.
47
+ *
48
+ * @param stdClass $term Queue item to iterate over.
49
+ *
50
+ * @return mixed
51
+ */
52
+ protected function task( $term ) {
53
+ global $wpdb;
54
+
55
+ $sanitized_slug = $this->main->ctl_sanitize_title( $term->slug );
56
+
57
+ if ( $sanitized_slug !== $term->slug ) {
58
+ // phpcs:disable WordPress.DB.DirectDatabaseQuery
59
+ $wpdb->update( $wpdb->terms, array( 'slug' => $sanitized_slug ), array( 'term_id' => $term->term_id ) );
60
+ // phpcs:enable
61
+ }
62
+
63
+ $this->log( $term->slug . ' => ' . $sanitized_slug );
64
+
65
+ return false;
66
+ }
67
+
68
+ /**
69
+ * Complete
70
+ */
71
+ protected function complete() {
72
+ parent::complete();
73
+
74
+ $this->log( 'Term slugs conversion completed.' );
75
+ }
76
+
77
+ /**
78
+ * Log.
79
+ *
80
+ * @param string $message Message to log.
81
+ */
82
+ public function log( $message ) {
83
+ if ( WP_DEBUG_LOG ) {
84
+ error_log( $message );
85
+ }
86
+ }
87
+ }
includes/class-cyr-to-lat-converter.php CHANGED
@@ -12,6 +12,11 @@
12
  */
13
  class Cyr_To_Lat_Converter {
14
 
 
 
 
 
 
15
  /**
16
  * Plugin main class.
17
  *
@@ -26,6 +31,20 @@ class Cyr_To_Lat_Converter {
26
  */
27
  private $settings;
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  /**
30
  * Cyr_To_Lat_Converter constructor.
31
  *
@@ -33,8 +52,10 @@ class Cyr_To_Lat_Converter {
33
  * @param Cyr_To_Lat_Settings $settings Plugin settings.
34
  */
35
  public function __construct( Cyr_To_Lat_Main $main, Cyr_To_Lat_Settings $settings ) {
36
- $this->main = $main;
37
- $this->settings = $settings;
 
 
38
  $this->init_hooks();
39
  }
40
 
@@ -42,45 +63,166 @@ class Cyr_To_Lat_Converter {
42
  * Init class hooks.
43
  */
44
  public function init_hooks() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  if ( 'yes' === $this->settings->get_option( 'convert_existing_slugs' ) ) {
46
  $this->settings->set_option( 'convert_existing_slugs', 'no' );
47
- add_action( 'shutdown', array( $this, 'convert_existing_slugs' ) );
48
  }
 
49
  }
50
 
51
  /**
52
- * Convert Existing Slugs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  */
54
  public function convert_existing_slugs() {
55
  global $wpdb;
56
 
 
 
57
  // phpcs:disable WordPress.DB.DirectDatabaseQuery
58
- $posts = $wpdb->get_results( "SELECT ID, post_name FROM $wpdb->posts WHERE post_name REGEXP('[^A-Za-z0-9\-]+') AND post_status IN ('publish', 'future', 'private')" );
 
 
 
 
 
59
  // phpcs:enable
60
 
61
  foreach ( (array) $posts as $post ) {
62
- $sanitized_name = $this->main->ctl_sanitize_title( urldecode( $post->post_name ) );
63
-
64
- if ( $post->post_name !== $sanitized_name ) {
65
- add_post_meta( $post->ID, '_wp_old_slug', $post->post_name );
66
- // phpcs:disable WordPress.DB.DirectDatabaseQuery
67
- $wpdb->update( $wpdb->posts, array( 'post_name' => $sanitized_name ), array( 'ID' => $post->ID ) );
68
- // phpcs:enable
69
- }
70
  }
71
 
 
 
72
  // phpcs:disable WordPress.DB.DirectDatabaseQuery
73
- $terms = $wpdb->get_results( "SELECT term_id, slug FROM $wpdb->terms WHERE slug REGEXP('[^A-Za-z0-9\-]+') " );
 
 
 
 
 
74
  // phpcs:enable
75
 
76
  foreach ( (array) $terms as $term ) {
77
- $sanitized_slug = $this->main->ctl_sanitize_title( urldecode( $term->slug ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
- if ( $term->slug !== $sanitized_slug ) {
80
- // phpcs:disable WordPress.DB.DirectDatabaseQuery
81
- $wpdb->update( $wpdb->terms, array( 'slug' => $sanitized_slug ), array( 'term_id' => $term->term_id ) );
82
- // phpcs:enable
83
- }
 
 
 
 
 
 
 
 
 
 
 
84
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  }
86
  }
12
  */
13
  class Cyr_To_Lat_Converter {
14
 
15
+ /**
16
+ * Query arg in url to start conversion.
17
+ */
18
+ const QUERY_ARG = 'cyr-to-lat-convert';
19
+
20
  /**
21
  * Plugin main class.
22
  *
31
  */
32
  private $settings;
33
 
34
+ /**
35
+ * Background process to convert posts.
36
+ *
37
+ * @var Cyr_To_Lat_Post_Conversion_Process
38
+ */
39
+ private $process_all_posts;
40
+
41
+ /**
42
+ * Background process to convert terms.
43
+ *
44
+ * @var Cyr_To_Lat_Term_Conversion_Process
45
+ */
46
+ private $process_all_terms;
47
+
48
  /**
49
  * Cyr_To_Lat_Converter constructor.
50
  *
52
  * @param Cyr_To_Lat_Settings $settings Plugin settings.
53
  */
54
  public function __construct( Cyr_To_Lat_Main $main, Cyr_To_Lat_Settings $settings ) {
55
+ $this->main = $main;
56
+ $this->settings = $settings;
57
+ $this->process_all_posts = new Cyr_To_Lat_Post_Conversion_Process( $main );
58
+ $this->process_all_terms = new Cyr_To_Lat_Term_Conversion_Process( $main );
59
  $this->init_hooks();
60
  }
61
 
63
  * Init class hooks.
64
  */
65
  public function init_hooks() {
66
+ add_action( 'init', array( $this, 'process_handler' ) );
67
+
68
+ /**
69
+ * Fix bug in WP_Background_Process::memory_exceeded() function.
70
+ * See hook.
71
+ */
72
+ add_filter(
73
+ CYR_TO_LAT_PREFIX . '_' . CYR_TO_LAT_POST_CONVERSION_ACTION . '_memory_exceeded',
74
+ array( $this, 'memory_exceeded_filter' )
75
+ );
76
+ add_filter(
77
+ CYR_TO_LAT_PREFIX . '_' . CYR_TO_LAT_TERM_CONVERSION_ACTION . '_memory_exceeded',
78
+ array( $this, 'memory_exceeded_filter' )
79
+ );
80
+
81
+ // Do not limit execution time with WP_CLI.
82
+ add_filter(
83
+ CYR_TO_LAT_PREFIX . '_' . CYR_TO_LAT_POST_CONVERSION_ACTION . '_time_exceeded',
84
+ array( $this, 'time_exceeded_filter' )
85
+ );
86
+ add_filter(
87
+ CYR_TO_LAT_PREFIX . '_' . CYR_TO_LAT_TERM_CONVERSION_ACTION . '_time_exceeded',
88
+ array( $this, 'time_exceeded_filter' )
89
+ );
90
+
91
  if ( 'yes' === $this->settings->get_option( 'convert_existing_slugs' ) ) {
92
  $this->settings->set_option( 'convert_existing_slugs', 'no' );
93
+ add_action( 'init', array( $this, 'convert_existing_slugs' ) );
94
  }
95
+
96
  }
97
 
98
  /**
99
+ * Process handler.
100
+ */
101
+ public function process_handler() {
102
+ if ( ! isset( $_GET[ self::QUERY_ARG ] ) || ! isset( $_GET['_wpnonce'] ) ) {
103
+ return;
104
+ }
105
+
106
+ if ( ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), self::QUERY_ARG ) ) {
107
+ return;
108
+ }
109
+
110
+ $this->convert_existing_slugs();
111
+ }
112
+
113
+ /**
114
+ * Convert Existing Slugs.
115
  */
116
  public function convert_existing_slugs() {
117
  global $wpdb;
118
 
119
+ $regexp = '[^A-Za-z0-9[.hyphen.][.underscore.][.period.][.apostrophe.]]+';
120
+
121
  // phpcs:disable WordPress.DB.DirectDatabaseQuery
122
+ $posts = $wpdb->get_results(
123
+ $wpdb->prepare(
124
+ "SELECT ID, post_name FROM $wpdb->posts WHERE post_name REGEXP(%s) AND post_status IN ('publish', 'future', 'private')",
125
+ $regexp
126
+ )
127
+ );
128
  // phpcs:enable
129
 
130
  foreach ( (array) $posts as $post ) {
131
+ $this->process_all_posts->push_to_queue( $post );
 
 
 
 
 
 
 
132
  }
133
 
134
+ $this->process_all_posts->save()->dispatch();
135
+
136
  // phpcs:disable WordPress.DB.DirectDatabaseQuery
137
+ $terms = $wpdb->get_results(
138
+ $wpdb->prepare(
139
+ "SELECT term_id, slug FROM $wpdb->terms WHERE slug REGEXP(%s)",
140
+ $regexp
141
+ )
142
+ );
143
  // phpcs:enable
144
 
145
  foreach ( (array) $terms as $term ) {
146
+ $this->process_all_terms->push_to_queue( $term );
147
+ }
148
+
149
+ $this->process_all_terms->save()->dispatch();
150
+ }
151
+
152
+ /**
153
+ * Filter WP_Background_Process::memory_exceeded() result.
154
+ * This function expects memory limit coded in php.ini only in megabytes ( as '128M', for instance).
155
+ * Thus, it returns wrong result when memory limit is encoded in other way.
156
+ *
157
+ * Filter does this job again.
158
+ *
159
+ * @param bool $return If memory is exceeded.
160
+ *
161
+ * @return mixed
162
+ */
163
+ public function memory_exceeded_filter( $return ) {
164
+ $memory_limit = $this->get_memory_limit() * 0.9; // 90% of max memory
165
+ $current_memory = memory_get_usage( true );
166
+
167
+ return $current_memory >= $memory_limit;
168
+ }
169
 
170
+ /**
171
+ * Get memory limit in bytes.
172
+ *
173
+ * @return int
174
+ */
175
+ protected function get_memory_limit() {
176
+ if ( function_exists( 'ini_get' ) ) {
177
+ $memory_limit = ini_get( 'memory_limit' );
178
+ } else {
179
+ // Sensible default.
180
+ $memory_limit = '128M';
181
+ }
182
+
183
+ if ( ! $memory_limit || - 1 === intval( $memory_limit ) ) {
184
+ // Unlimited, set to 32GB.
185
+ $memory_limit = '32000M';
186
  }
187
+
188
+ return $this->convert_shorthand_to_bytes( $memory_limit );
189
+ }
190
+
191
+ /**
192
+ * Converts a shorthand byte value to an integer byte value.
193
+ *
194
+ * @param string $value A (PHP ini) byte value, either shorthand or ordinary.
195
+ * @return int An integer byte value.
196
+ */
197
+ protected function convert_shorthand_to_bytes( $value ) {
198
+ $value = strtolower( trim( $value ) );
199
+ $bytes = (int) $value;
200
+
201
+ if ( false !== strpos( $value, 'g' ) ) {
202
+ $bytes *= 1024 * 1024 * 1024;
203
+ } elseif ( false !== strpos( $value, 'm' ) ) {
204
+ $bytes *= 1024 * 1024;
205
+ } elseif ( false !== strpos( $value, 'k' ) ) {
206
+ $bytes *= 1024;
207
+ }
208
+
209
+ // Deal with large (float) values which run into the maximum integer size.
210
+ return min( $bytes, PHP_INT_MAX );
211
+ }
212
+
213
+ /**
214
+ * Filter WP_Background_Process::time_exceeded() result.
215
+ * Return false with WP_CLI.
216
+ *
217
+ * @param bool $return If memory is exceeded.
218
+ *
219
+ * @return mixed
220
+ */
221
+ protected function time_exceeded_filter( $return ) {
222
+ if ( defined( 'WP_CLI' ) && WP_CLI ) {
223
+ return false;
224
+ }
225
+
226
+ return $return;
227
  }
228
  }
includes/class-cyr-to-lat-main.php CHANGED
@@ -37,9 +37,9 @@ class Cyr_To_Lat_Main {
37
  */
38
  public function init() {
39
  $this->settings = new Cyr_To_Lat_Settings();
 
40
 
41
  if ( defined( 'WP_CLI' ) && WP_CLI ) {
42
- $converter = new Cyr_To_Lat_Converter( $this, $this->settings );
43
  $this->cli = new Cyr_To_Lat_WP_CLI( $converter );
44
  try {
45
  /**
@@ -116,7 +116,7 @@ class Cyr_To_Lat_Main {
116
  $title = iconv( 'UTF-8', 'UTF-8//TRANSLIT//IGNORE', $title );
117
  }
118
 
119
- $title = preg_replace( "/[^A-Za-z0-9'_\-\.%]/", '-', $title );
120
  $title = preg_replace( '/\-+/', '-', $title );
121
  $title = trim( $title, '-' );
122
  }
37
  */
38
  public function init() {
39
  $this->settings = new Cyr_To_Lat_Settings();
40
+ $converter = new Cyr_To_Lat_Converter( $this, $this->settings );
41
 
42
  if ( defined( 'WP_CLI' ) && WP_CLI ) {
 
43
  $this->cli = new Cyr_To_Lat_WP_CLI( $converter );
44
  try {
45
  /**
116
  $title = iconv( 'UTF-8', 'UTF-8//TRANSLIT//IGNORE', $title );
117
  }
118
 
119
+ $title = preg_replace( "/[^A-Za-z0-9'_\-\.]/", '-', $title );
120
  $title = preg_replace( '/\-+/', '-', $title );
121
  $title = trim( $title, '-' );
122
  }
includes/class-cyr-to-lat-wp-cli.php CHANGED
@@ -52,12 +52,6 @@ class Cyr_To_Lat_WP_CLI extends WP_CLI_Command {
52
  */
53
  $notify = \WP_CLI\Utils\make_progress_bar( 'Regenerate old slugs', 1 );
54
 
55
- //for ( $i = 0; $i < 5; $i ++ ) {
56
- /// sleep( wp_rand( 1, 2 ) );
57
- // $notify->tick();
58
- //}
59
- //$notify->finish();
60
-
61
  $this->converter->convert_existing_slugs();
62
  $notify->tick();
63
  $notify->finish();
52
  */
53
  $notify = \WP_CLI\Utils\make_progress_bar( 'Regenerate old slugs', 1 );
54
 
 
 
 
 
 
 
55
  $this->converter->convert_existing_slugs();
56
  $notify->tick();
57
  $notify->finish();
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: SergeyBiryukov, mihdan, karevn, webvitaly, kaggdesign
3
  Tags: cyrillic, georgian, latin, l10n, russian, rustolat, slugs, translations, transliteration
4
  Requires at least: 2.3
5
  Tested up to: 5.1
6
- Stable tag: 3.6.4
7
  Requires PHP: 5.2
8
 
9
  Converts Cyrillic characters in post, page and term slugs to Latin characters.
@@ -46,6 +46,9 @@ Yes you can! Join in on our [GitHub repository](https://github.com/mihdan/cyr2la
46
 
47
  == Changelog ==
48
 
 
 
 
49
  = 3.6.4 (06.02.2019) =
50
  * Fixed bug with `_wp_old_slug` redirect.
51
  * Fixed bug with `urldecode` in posts.
3
  Tags: cyrillic, georgian, latin, l10n, russian, rustolat, slugs, translations, transliteration
4
  Requires at least: 2.3
5
  Tested up to: 5.1
6
+ Stable tag: 3.6.5
7
  Requires PHP: 5.2
8
 
9
  Converts Cyrillic characters in post, page and term slugs to Latin characters.
46
 
47
  == Changelog ==
48
 
49
+ = 3.6.5 (11.02.2019) =
50
+ * Added queues for background slug conversion process
51
+
52
  = 3.6.4 (06.02.2019) =
53
  * Fixed bug with `_wp_old_slug` redirect.
54
  * Fixed bug with `urldecode` in posts.
vendor/a5hleyrich/wp-background-processing/.gitignore ADDED
@@ -0,0 +1 @@
 
1
+ /vendor/
vendor/a5hleyrich/wp-background-processing/README.md ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # WP Background Processing
2
+
3
+ WP Background Processing can be used to fire off non-blocking asynchronous requests or as a background processing tool, allowing you to queue tasks. Check out the [example plugin](https://github.com/A5hleyRich/wp-background-processing-example) or read the [accompanying article](https://deliciousbrains.com/background-processing-wordpress/).
4
+
5
+ Inspired by [TechCrunch WP Asynchronous Tasks](https://github.com/techcrunch/wp-async-task).
6
+
7
+ __Requires PHP 5.2+__
8
+
9
+ ### Async Request
10
+
11
+ Async requests are useful for pushing slow one-off tasks such as sending emails to a background process. Once the request has been dispatched it will process in the background instantly.
12
+
13
+ Extend the `WP_Async_Request` class:
14
+
15
+ ```php
16
+ class WP_Example_Request extends WP_Async_Request {
17
+
18
+ /**
19
+ * @var string
20
+ */
21
+ protected $action = 'example_request';
22
+
23
+ /**
24
+ * Handle
25
+ *
26
+ * Override this method to perform any actions required
27
+ * during the async request.
28
+ */
29
+ protected function handle() {
30
+ // Actions to perform
31
+ }
32
+
33
+ }
34
+ ```
35
+
36
+ ##### `protected $action`
37
+
38
+ Should be set to a unique name.
39
+
40
+ ##### `protected function handle()`
41
+
42
+ Should contain any logic to perform during the non-blocking request. The data passed to the request will be accessible via `$_POST`.
43
+
44
+ ##### Dispatching Requests
45
+
46
+ Instantiate your request:
47
+
48
+ `$this->example_request = new WP_Example_Request();`
49
+
50
+ Add data to the request if required:
51
+
52
+ `$this->example_request->data( array( 'value1' => $value1, 'value2' => $value2 ) );`
53
+
54
+ Fire off the request:
55
+
56
+ `$this->example_request->dispatch();`
57
+
58
+ Chaining is also supported:
59
+
60
+ `$this->example_request->data( array( 'data' => $data ) )->dispatch();`
61
+
62
+ ### Background Process
63
+
64
+ Background processes work in a similar fashion to async requests but they allow you to queue tasks. Items pushed onto the queue will be processed in the background once the queue has been dispatched. Queues will also scale based on available server resources, so higher end servers will process more items per batch. Once a batch has completed the next batch will start instantly.
65
+
66
+ Health checks run by default every 5 minutes to ensure the queue is running when queued items exist. If the queue has failed it will be restarted.
67
+
68
+ Queues work on a first in first out basis, which allows additional items to be pushed to the queue even if it’s already processing.
69
+
70
+ Extend the `WP_Background_Process` class:
71
+
72
+ ```php
73
+ class WP_Example_Process extends WP_Background_Process {
74
+
75
+ /**
76
+ * @var string
77
+ */
78
+ protected $action = 'example_process';
79
+
80
+ /**
81
+ * Task
82
+ *
83
+ * Override this method to perform any actions required on each
84
+ * queue item. Return the modified item for further processing
85
+ * in the next pass through. Or, return false to remove the
86
+ * item from the queue.
87
+ *
88
+ * @param mixed $item Queue item to iterate over
89
+ *
90
+ * @return mixed
91
+ */
92
+ protected function task( $item ) {
93
+ // Actions to perform
94
+
95
+ return false;
96
+ }
97
+
98
+ /**
99
+ * Complete
100
+ *
101
+ * Override if applicable, but ensure that the below actions are
102
+ * performed, or, call parent::complete().
103
+ */
104
+ protected function complete() {
105
+ parent::complete();
106
+
107
+ // Show notice to user or perform some other arbitrary task...
108
+ }
109
+
110
+ }
111
+ ```
112
+
113
+ ##### `protected $action`
114
+
115
+ Should be set to a unique name.
116
+
117
+ ##### `protected function task( $item )`
118
+
119
+ Should contain any logic to perform on the queued item. Return `false` to remove the item from the queue or return `$item` to push it back onto the queue for further processing. If the item has been modified and is pushed back onto the queue the current state will be saved before the batch is exited.
120
+
121
+ ##### `protected function complete()`
122
+
123
+ Optionally contain any logic to perform once the queue has completed.
124
+
125
+ ##### Dispatching Processes
126
+
127
+ Instantiate your process:
128
+
129
+ `$this->example_process = new WP_Example_Process();`
130
+
131
+ Push items to the queue:
132
+
133
+ ```php
134
+ foreach ( $items as $item ) {
135
+ $this->example_process->push_to_queue( $item );
136
+ }
137
+ ```
138
+
139
+ Save and dispatch the queue:
140
+
141
+ `$this->example_process->save()->dispatch();`
142
+
143
+ ### BasicAuth
144
+
145
+ If your site is behind BasicAuth, both async requests and background processes will fail to complete. This is because WP Background Processing relies on the [WordPress HTTP API](http://codex.wordpress.org/HTTP_API), which requires you to attach your BasicAuth credentials to requests. The easiest way to do this is using the following filter:
146
+
147
+ ```php
148
+ function wpbp_http_request_args( $r, $url ) {
149
+ $r['headers']['Authorization'] = 'Basic ' . base64_encode( USERNAME . ':' . PASSWORD );
150
+
151
+ return $r;
152
+ }
153
+ add_filter( 'http_request_args', 'wpbp_http_request_args', 10, 2);
154
+ ```
155
+
156
+ ## License
157
+
158
+ [GPLv2+](http://www.gnu.org/licenses/gpl-2.0.html)
vendor/a5hleyrich/wp-background-processing/classes/wp-async-request.php ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * WP Async Request
4
+ *
5
+ * @package WP-Background-Processing
6
+ */
7
+
8
+ if ( ! class_exists( 'WP_Async_Request' ) ) {
9
+
10
+ /**
11
+ * Abstract WP_Async_Request class.
12
+ *
13
+ * @abstract
14
+ */
15
+ abstract class WP_Async_Request {
16
+
17
+ /**
18
+ * Prefix
19
+ *
20
+ * (default value: 'wp')
21
+ *
22
+ * @var string
23
+ * @access protected
24
+ */
25
+ protected $prefix = 'wp';
26
+
27
+ /**
28
+ * Action
29
+ *
30
+ * (default value: 'async_request')
31
+ *
32
+ * @var string
33
+ * @access protected
34
+ */
35
+ protected $action = 'async_request';
36
+
37
+ /**
38
+ * Identifier
39
+ *
40
+ * @var mixed
41
+ * @access protected
42
+ */
43
+ protected $identifier;
44
+
45
+ /**
46
+ * Data
47
+ *
48
+ * (default value: array())
49
+ *
50
+ * @var array
51
+ * @access protected
52
+ */
53
+ protected $data = array();
54
+
55
+ /**
56
+ * Initiate new async request
57
+ */
58
+ public function __construct() {
59
+ $this->identifier = $this->prefix . '_' . $this->action;
60
+
61
+ add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) );
62
+ add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) );
63
+ }
64
+
65
+ /**
66
+ * Set data used during the request
67
+ *
68
+ * @param array $data Data.
69
+ *
70
+ * @return $this
71
+ */
72
+ public function data( $data ) {
73
+ $this->data = $data;
74
+
75
+ return $this;
76
+ }
77
+
78
+ /**
79
+ * Dispatch the async request
80
+ *
81
+ * @return array|WP_Error
82
+ */
83
+ public function dispatch() {
84
+ $url = add_query_arg( $this->get_query_args(), $this->get_query_url() );
85
+ $args = $this->get_post_args();
86
+
87
+ return wp_remote_post( esc_url_raw( $url ), $args );
88
+ }
89
+
90
+ /**
91
+ * Get query args
92
+ *
93
+ * @return array
94
+ */
95
+ protected function get_query_args() {
96
+ if ( property_exists( $this, 'query_args' ) ) {
97
+ return $this->query_args;
98
+ }
99
+
100
+ return array(
101
+ 'action' => $this->identifier,
102
+ 'nonce' => wp_create_nonce( $this->identifier ),
103
+ );
104
+ }
105
+
106
+ /**
107
+ * Get query URL
108
+ *
109
+ * @return string
110
+ */
111
+ protected function get_query_url() {
112
+ if ( property_exists( $this, 'query_url' ) ) {
113
+ return $this->query_url;
114
+ }
115
+
116
+ return admin_url( 'admin-ajax.php' );
117
+ }
118
+
119
+ /**
120
+ * Get post args
121
+ *
122
+ * @return array
123
+ */
124
+ protected function get_post_args() {
125
+ if ( property_exists( $this, 'post_args' ) ) {
126
+ return $this->post_args;
127
+ }
128
+
129
+ return array(
130
+ 'timeout' => 0.01,
131
+ 'blocking' => false,
132
+ 'body' => $this->data,
133
+ 'cookies' => $_COOKIE,
134
+ 'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
135
+ );
136
+ }
137
+
138
+ /**
139
+ * Maybe handle
140
+ *
141
+ * Check for correct nonce and pass to handler.
142
+ */
143
+ public function maybe_handle() {
144
+ // Don't lock up other requests while processing
145
+ session_write_close();
146
+
147
+ check_ajax_referer( $this->identifier, 'nonce' );
148
+
149
+ $this->handle();
150
+
151
+ wp_die();
152
+ }
153
+
154
+ /**
155
+ * Handle
156
+ *
157
+ * Override this method to perform any actions required
158
+ * during the async request.
159
+ */
160
+ abstract protected function handle();
161
+
162
+ }
163
+ }
vendor/a5hleyrich/wp-background-processing/classes/wp-background-process.php ADDED
@@ -0,0 +1,506 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * WP Background Process
4
+ *
5
+ * @package WP-Background-Processing
6
+ */
7
+
8
+ if ( ! class_exists( 'WP_Background_Process' ) ) {
9
+
10
+ /**
11
+ * Abstract WP_Background_Process class.
12
+ *
13
+ * @abstract
14
+ * @extends WP_Async_Request
15
+ */
16
+ abstract class WP_Background_Process extends WP_Async_Request {
17
+
18
+ /**
19
+ * Action
20
+ *
21
+ * (default value: 'background_process')
22
+ *
23
+ * @var string
24
+ * @access protected
25
+ */
26
+ protected $action = 'background_process';
27
+
28
+ /**
29
+ * Start time of current process.
30
+ *
31
+ * (default value: 0)
32
+ *
33
+ * @var int
34
+ * @access protected
35
+ */
36
+ protected $start_time = 0;
37
+
38
+ /**
39
+ * Cron_hook_identifier
40
+ *
41
+ * @var mixed
42
+ * @access protected
43
+ */
44
+ protected $cron_hook_identifier;
45
+
46
+ /**
47
+ * Cron_interval_identifier
48
+ *
49
+ * @var mixed
50
+ * @access protected
51
+ */
52
+ protected $cron_interval_identifier;
53
+
54
+ /**
55
+ * Initiate new background process
56
+ */
57
+ public function __construct() {
58
+ parent::__construct();
59
+
60
+ $this->cron_hook_identifier = $this->identifier . '_cron';
61
+ $this->cron_interval_identifier = $this->identifier . '_cron_interval';
62
+
63
+ add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) );
64
+ add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) );
65
+ }
66
+
67
+ /**
68
+ * Dispatch
69
+ *
70
+ * @access public
71
+ * @return void
72
+ */
73
+ public function dispatch() {
74
+ // Schedule the cron healthcheck.
75
+ $this->schedule_event();
76
+
77
+ // Perform remote post.
78
+ return parent::dispatch();
79
+ }
80
+
81
+ /**
82
+ * Push to queue
83
+ *
84
+ * @param mixed $data Data.
85
+ *
86
+ * @return $this
87
+ */
88
+ public function push_to_queue( $data ) {
89
+ $this->data[] = $data;
90
+
91
+ return $this;
92
+ }
93
+
94
+ /**
95
+ * Save queue
96
+ *
97
+ * @return $this
98
+ */
99
+ public function save() {
100
+ $key = $this->generate_key();
101
+
102
+ if ( ! empty( $this->data ) ) {
103
+ update_site_option( $key, $this->data );
104
+ }
105
+
106
+ return $this;
107
+ }
108
+
109
+ /**
110
+ * Update queue
111
+ *
112
+ * @param string $key Key.
113
+ * @param array $data Data.
114
+ *
115
+ * @return $this
116
+ */
117
+ public function update( $key, $data ) {
118
+ if ( ! empty( $data ) ) {
119
+ update_site_option( $key, $data );
120
+ }
121
+
122
+ return $this;
123
+ }
124
+
125
+ /**
126
+ * Delete queue
127
+ *
128
+ * @param string $key Key.
129
+ *
130
+ * @return $this
131
+ */
132
+ public function delete( $key ) {
133
+ delete_site_option( $key );
134
+
135
+ return $this;
136
+ }
137
+
138
+ /**
139
+ * Generate key
140
+ *
141
+ * Generates a unique key based on microtime. Queue items are
142
+ * given a unique key so that they can be merged upon save.
143
+ *
144
+ * @param int $length Length.
145
+ *
146
+ * @return string
147
+ */
148
+ protected function generate_key( $length = 64 ) {
149
+ $unique = md5( microtime() . rand() );
150
+ $prepend = $this->identifier . '_batch_';
151
+
152
+ return substr( $prepend . $unique, 0, $length );
153
+ }
154
+
155
+ /**
156
+ * Maybe process queue
157
+ *
158
+ * Checks whether data exists within the queue and that
159
+ * the process is not already running.
160
+ */
161
+ public function maybe_handle() {
162
+ // Don't lock up other requests while processing
163
+ session_write_close();
164
+
165
+ if ( $this->is_process_running() ) {
166
+ // Background process already running.
167
+ wp_die();
168
+ }
169
+
170
+ if ( $this->is_queue_empty() ) {
171
+ // No data to process.
172
+ wp_die();
173
+ }
174
+
175
+ check_ajax_referer( $this->identifier, 'nonce' );
176
+
177
+ $this->handle();
178
+
179
+ wp_die();
180
+ }
181
+
182
+ /**
183
+ * Is queue empty
184
+ *
185
+ * @return bool
186
+ */
187
+ protected function is_queue_empty() {
188
+ global $wpdb;
189
+
190
+ $table = $wpdb->options;
191
+ $column = 'option_name';
192
+
193
+ if ( is_multisite() ) {
194
+ $table = $wpdb->sitemeta;
195
+ $column = 'meta_key';
196
+ }
197
+
198
+ $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
199
+
200
+ $count = $wpdb->get_var( $wpdb->prepare( "
201
+ SELECT COUNT(*)
202
+ FROM {$table}
203
+ WHERE {$column} LIKE %s
204
+ ", $key ) );
205
+
206
+ return ( $count > 0 ) ? false : true;
207
+ }
208
+
209
+ /**
210
+ * Is process running
211
+ *
212
+ * Check whether the current process is already running
213
+ * in a background process.
214
+ */
215
+ protected function is_process_running() {
216
+ if ( get_site_transient( $this->identifier . '_process_lock' ) ) {
217
+ // Process already running.
218
+ return true;
219
+ }
220
+
221
+ return false;
222
+ }
223
+
224
+ /**
225
+ * Lock process
226
+ *
227
+ * Lock the process so that multiple instances can't run simultaneously.
228
+ * Override if applicable, but the duration should be greater than that
229
+ * defined in the time_exceeded() method.
230
+ */
231
+ protected function lock_process() {
232
+ $this->start_time = time(); // Set start time of current process.
233
+
234
+ $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute
235
+ $lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration );
236
+
237
+ set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration );
238
+ }
239
+
240
+ /**
241
+ * Unlock process
242
+ *
243
+ * Unlock the process so that other instances can spawn.
244
+ *
245
+ * @return $this
246
+ */
247
+ protected function unlock_process() {
248
+ delete_site_transient( $this->identifier . '_process_lock' );
249
+
250
+ return $this;
251
+ }
252
+
253
+ /**
254
+ * Get batch
255
+ *
256
+ * @return stdClass Return the first batch from the queue
257
+ */
258
+ protected function get_batch() {
259
+ global $wpdb;
260
+
261
+ $table = $wpdb->options;
262
+ $column = 'option_name';
263
+ $key_column = 'option_id';
264
+ $value_column = 'option_value';
265
+
266
+ if ( is_multisite() ) {
267
+ $table = $wpdb->sitemeta;
268
+ $column = 'meta_key';
269
+ $key_column = 'meta_id';
270
+ $value_column = 'meta_value';
271
+ }
272
+
273
+ $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
274
+
275
+ $query = $wpdb->get_row( $wpdb->prepare( "
276
+ SELECT *
277
+ FROM {$table}
278
+ WHERE {$column} LIKE %s
279
+ ORDER BY {$key_column} ASC
280
+ LIMIT 1
281
+ ", $key ) );
282
+
283
+ $batch = new stdClass();
284
+ $batch->key = $query->$column;
285
+ $batch->data = maybe_unserialize( $query->$value_column );
286
+
287
+ return $batch;
288
+ }
289
+
290
+ /**
291
+ * Handle
292
+ *
293
+ * Pass each queue item to the task handler, while remaining
294
+ * within server memory and time limit constraints.
295
+ */
296
+ protected function handle() {
297
+ $this->lock_process();
298
+
299
+ do {
300
+ $batch = $this->get_batch();
301
+
302
+ foreach ( $batch->data as $key => $value ) {
303
+ $task = $this->task( $value );
304
+
305
+ if ( false !== $task ) {
306
+ $batch->data[ $key ] = $task;
307
+ } else {
308
+ unset( $batch->data[ $key ] );
309
+ }
310
+
311
+ if ( $this->time_exceeded() || $this->memory_exceeded() ) {
312
+ // Batch limits reached.
313
+ break;
314
+ }
315
+ }
316
+
317
+ // Update or delete current batch.
318
+ if ( ! empty( $batch->data ) ) {
319
+ $this->update( $batch->key, $batch->data );
320
+ } else {
321
+ $this->delete( $batch->key );
322
+ }
323
+ } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() );
324
+
325
+ $this->unlock_process();
326
+
327
+ // Start next batch or complete process.
328
+ if ( ! $this->is_queue_empty() ) {
329
+ $this->dispatch();
330
+ } else {
331
+ $this->complete();
332
+ }
333
+
334
+ wp_die();
335
+ }
336
+
337
+ /**
338
+ * Memory exceeded
339
+ *
340
+ * Ensures the batch process never exceeds 90%
341
+ * of the maximum WordPress memory.
342
+ *
343
+ * @return bool
344
+ */
345
+ protected function memory_exceeded() {
346
+ $memory_limit = $this->get_memory_limit() * 0.9; // 90% of max memory
347
+ $current_memory = memory_get_usage( true );
348
+ $return = false;
349
+
350
+ if ( $current_memory >= $memory_limit ) {
351
+ $return = true;
352
+ }
353
+
354
+ return apply_filters( $this->identifier . '_memory_exceeded', $return );
355
+ }
356
+
357
+ /**
358
+ * Get memory limit
359
+ *
360
+ * @return int
361
+ */
362
+ protected function get_memory_limit() {
363
+ if ( function_exists( 'ini_get' ) ) {
364
+ $memory_limit = ini_get( 'memory_limit' );
365
+ } else {
366
+ // Sensible default.
367
+ $memory_limit = '128M';
368
+ }
369
+
370
+ if ( ! $memory_limit || -1 === intval( $memory_limit ) ) {
371
+ // Unlimited, set to 32GB.
372
+ $memory_limit = '32000M';
373
+ }
374
+
375
+ return intval( $memory_limit ) * 1024 * 1024;
376
+ }
377
+
378
+ /**
379
+ * Time exceeded.
380
+ *
381
+ * Ensures the batch never exceeds a sensible time limit.
382
+ * A timeout limit of 30s is common on shared hosting.
383
+ *
384
+ * @return bool
385
+ */
386
+ protected function time_exceeded() {
387
+ $finish = $this->start_time + apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds
388
+ $return = false;
389
+
390
+ if ( time() >= $finish ) {
391
+ $return = true;
392
+ }
393
+
394
+ return apply_filters( $this->identifier . '_time_exceeded', $return );
395
+ }
396
+
397
+ /**
398
+ * Complete.
399
+ *
400
+ * Override if applicable, but ensure that the below actions are
401
+ * performed, or, call parent::complete().
402
+ */
403
+ protected function complete() {
404
+ // Unschedule the cron healthcheck.
405
+ $this->clear_scheduled_event();
406
+ }
407
+
408
+ /**
409
+ * Schedule cron healthcheck
410
+ *
411
+ * @access public
412
+ * @param mixed $schedules Schedules.
413
+ * @return mixed
414
+ */
415
+ public function schedule_cron_healthcheck( $schedules ) {
416
+ $interval = apply_filters( $this->identifier . '_cron_interval', 5 );
417
+
418
+ if ( property_exists( $this, 'cron_interval' ) ) {
419
+ $interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval );
420
+ }
421
+
422
+ // Adds every 5 minutes to the existing schedules.
423
+ $schedules[ $this->identifier . '_cron_interval' ] = array(
424
+ 'interval' => MINUTE_IN_SECONDS * $interval,
425
+ 'display' => sprintf( __( 'Every %d Minutes' ), $interval ),
426
+ );
427
+
428
+ return $schedules;
429
+ }
430
+
431
+ /**
432
+ * Handle cron healthcheck
433
+ *
434
+ * Restart the background process if not already running
435
+ * and data exists in the queue.
436
+ */
437
+ public function handle_cron_healthcheck() {
438
+ if ( $this->is_process_running() ) {
439
+ // Background process already running.
440
+ exit;
441
+ }
442
+
443
+ if ( $this->is_queue_empty() ) {
444
+ // No data to process.
445
+ $this->clear_scheduled_event();
446
+ exit;
447
+ }
448
+
449
+ $this->handle();
450
+
451
+ exit;
452
+ }
453
+
454
+ /**
455
+ * Schedule event
456
+ */
457
+ protected function schedule_event() {
458
+ if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) {
459
+ wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier );
460
+ }
461
+ }
462
+
463
+ /**
464
+ * Clear scheduled event
465
+ */
466
+ protected function clear_scheduled_event() {
467
+ $timestamp = wp_next_scheduled( $this->cron_hook_identifier );
468
+
469
+ if ( $timestamp ) {
470
+ wp_unschedule_event( $timestamp, $this->cron_hook_identifier );
471
+ }
472
+ }
473
+
474
+ /**
475
+ * Cancel Process
476
+ *
477
+ * Stop processing queue items, clear cronjob and delete batch.
478
+ *
479
+ */
480
+ public function cancel_process() {
481
+ if ( ! $this->is_queue_empty() ) {
482
+ $batch = $this->get_batch();
483
+
484
+ $this->delete( $batch->key );
485
+
486
+ wp_clear_scheduled_hook( $this->cron_hook_identifier );
487
+ }
488
+
489
+ }
490
+
491
+ /**
492
+ * Task
493
+ *
494
+ * Override this method to perform any actions required on each
495
+ * queue item. Return the modified item for further processing
496
+ * in the next pass through. Or, return false to remove the
497
+ * item from the queue.
498
+ *
499
+ * @param mixed $item Queue item to iterate over.
500
+ *
501
+ * @return mixed
502
+ */
503
+ abstract protected function task( $item );
504
+
505
+ }
506
+ }
vendor/a5hleyrich/wp-background-processing/composer.json ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "a5hleyrich/wp-background-processing",
3
+ "description": "WP Background Processing can be used to fire off non-blocking asynchronous requests or as a background processing tool, allowing you to queue tasks.",
4
+ "type": "library",
5
+ "require": {
6
+ "php": ">=5.2"
7
+ },
8
+ "license": "GPL-2.0-only",
9
+ "authors": [
10
+ {
11
+ "name": "Ashley Rich",
12
+ "email": "hello@ashleyrich.com"
13
+ }
14
+ ],
15
+ "autoload": {
16
+ "classmap": [ "classes/" ]
17
+ }
18
+ }
vendor/a5hleyrich/wp-background-processing/license.txt ADDED
@@ -0,0 +1,280 @@