-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigrate-wp-cron-to-action-scheduler.php
More file actions
219 lines (190 loc) · 5.29 KB
/
migrate-wp-cron-to-action-scheduler.php
File metadata and controls
219 lines (190 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* Plugin Name: Advanced Cron Scheduler for WordPress
* Plugin URI: https://wordpress.org/plugins/migrate-wp-cron-to-action-scheduler/
* Description: The Advanced Cron Scheduler plugin helps to easily replace or migrate Native WordPress Cron to the Action Scheduler Library.
* Version: 1.1.4
* Author: Sayan Datta
* Author URI: https://www.sayandatta.co.in
* License: GPLv3
*
* Advanced Cron Scheduler is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Advanced Cron Scheduler is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Advanced Cron Scheduler. If not, see <http://www.gnu.org/licenses/>.
*
* @category Core
* @package Advanced Cron Scheduler
* @author Sayan Datta <iamsayan@protonmail.com>
* @license http://www.gnu.org/licenses/ GNU General Public License
* @link https://wordpress.org/plugins/migrate-wp-cron-to-action-scheduler/
*
*/
// If this file is called firectly, abort!!!
defined( 'ABSPATH' ) || exit;
/**
* ACSWP class.
*
* @class Main class of the plugin.
*/
final class ACSWP {
/**
* Plugin version.
*
* @var string
*/
public $version = '1.1.4';
/**
* Minimum version of WordPress required to run ACSWP.
*
* @var string
*/
private $wordpress_version = '6.5';
/**
* Minimum version of PHP required to run ACSWP.
*
* @var string
*/
private $php_version = '7.1';
/**
* Hold install error messages.
*
* @var array
*/
private $messages = [];
/**
* The single instance of the class.
*
* @var ACSWP
*/
protected static $instance = null;
/**
* Retrieve main ACSWP instance.
*
* Ensure only one instance is loaded or can be loaded.
*
* @see acswp()
* @return ACSWP
*/
public static function get() {
if ( is_null( self::$instance ) && ! ( self::$instance instanceof ACSWP ) ) {
self::$instance = new ACSWP();
self::$instance->setup();
}
return self::$instance;
}
/**
* Instantiate the plugin.
*/
private function setup() {
// Define plugin constants.
$this->define_constants();
if ( ! $this->is_requirements_meet() ) {
return;
}
// Include required files.
$this->includes();
// Instantiate services.
$this->instantiate();
// Loaded action.
do_action( 'acswp/loaded' );
}
/**
* Check that the WordPress and PHP setup meets the plugin requirements.
*
* @return bool
*/
private function is_requirements_meet() {
// Check WordPress version.
if ( version_compare( get_bloginfo( 'version' ), $this->wordpress_version, '<' ) ) {
/* translators: WordPress Version */
$this->messages[] = sprintf( esc_html__( 'You are using the outdated WordPress, please update it to version %s or higher.', 'migrate-wp-cron-to-action-scheduler' ), $this->wordpress_version );
}
// Check PHP version.
if ( version_compare( phpversion(), $this->php_version, '<' ) ) {
/* translators: PHP Version */
$this->messages[] = sprintf( esc_html__( 'Advanced Cron Scheduler for WordPress requires PHP version %s or above. Please update PHP to run this plugin.', 'migrate-wp-cron-to-action-scheduler' ), $this->php_version );
}
if ( empty( $this->messages ) ) {
return true;
}
// Auto-deactivate plugin.
add_action( 'admin_init', [ $this, 'auto_deactivate' ] );
add_action( 'admin_notices', [ $this, 'activation_error' ] );
return false;
}
/**
* Auto-deactivate plugin if requirements are not met, and display a notice.
*/
public function auto_deactivate() {
deactivate_plugins( ACSWP_BASENAME );
if ( isset( $_GET['activate'] ) ) { // phpcs:ignore
unset( $_GET['activate'] ); // phpcs:ignore
}
}
/**
* Error notice on plugin activation.
*/
public function activation_error() {
?>
<div class="notice acswp-notice notice-error">
<p>
<?php echo join( '<br>', $this->messages ); // phpcs:ignore ?>
</p>
</div>
<?php
}
/**
* Define the plugin constants.
*/
private function define_constants() {
define( 'ACSWP_VERSION', $this->version );
define( 'ACSWP_FILE', __FILE__ );
define( 'ACSWP_PATH', dirname( ACSWP_FILE ) . '/' );
define( 'ACSWP_URL', plugins_url( '', ACSWP_FILE ) . '/' );
define( 'ACSWP_BASENAME', plugin_basename( ACSWP_FILE ) );
}
/**
* Include the required files.
*/
private function includes() {
include __DIR__ . '/vendor/autoload.php';
}
/**
* Instantiate services.
*/
private function instantiate() {
// Activation hook.
register_activation_hook( ACSWP_FILE,
function () {
ACSWP\Plugin\Base\Activate::activate();
}
);
// Deactivation hook.
register_deactivation_hook( ACSWP_FILE,
function () {
ACSWP\Plugin\Base\Deactivate::deactivate();
}
);
// Init ACSWP Classes.
ACSWP\Plugin\Loader::register_services();
}
}
/**
* Returns the main instance of ACSWP to prevent the need to use globals.
*
* @return ACSWP
*/
function acswp() { // phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed
return ACSWP::get();
}
// Start it.
acswp();