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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
<?php namespace AutomateWoo;
/** * Addon class. * * This class must remain named as 'includes/abstracts/addon.php' because it's what AW add-ons expect. */ abstract class Addon {
/** @var Addon - must declare in child */ protected static $_instance; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
/** @var string */ public $id;
/** @var string */ public $name;
/** @var string */ public $version;
/** @var string */ public $plugin_basename;
/** @var string */ public $plugin_path;
/** @var string */ public $file;
/** @var string */ public $min_php_version;
/** @var string */ public $min_automatewoo_version;
/** @var string */ public $min_woocommerce_version;
/** @var array */ public $db_updates = [];
/** * Method to init the add on */ abstract public function init();
/** * Required method to return options class * * @return Options_API */ abstract public function options();
/** * Optional installer method */ public function install() {}
/** * Constructor for add-on * * @param Plugin_Data|object $plugin_data */ public function __construct( $plugin_data ) {
$this->id = $plugin_data->id; $this->name = $plugin_data->name; $this->version = $plugin_data->version; $this->file = $plugin_data->file; $this->min_automatewoo_version = $plugin_data->min_automatewoo_version;
$this->plugin_basename = plugin_basename( $plugin_data->file ); $this->plugin_path = dirname( $plugin_data->file );
add_action( 'automatewoo_init_addons', [ $this, 'register' ] ); add_action( 'automatewoo_init_addons', [ $this, 'init' ] ); }
/** * @param string $end * @return string */ public function url( $end = '' ) { return untrailingslashit( plugin_dir_url( $this->plugin_basename ) ) . $end; }
/** * @param string $end * @return string */ public function path( $end = '' ) { return untrailingslashit( $this->plugin_path ) . $end; }
/** * Check the version stored in the database and determine if an upgrade needs to occur */ public function check_version() {
if ( version_compare( $this->version, $this->options()->version, '=' ) ) { return; }
$this->install();
if ( $this->is_database_upgrade_available() ) { if ( apply_filters( 'woocommerce_enable_auto_update_db', false ) ) { $this->do_database_update(); return; } else { add_action( 'admin_notices', [ $this, 'data_upgrade_prompt' ] ); } } else { $this->update_database_version(); } }
/** * @return bool */ public function is_database_upgrade_available() {
if ( version_compare( $this->version, $this->options()->version, '=' ) || empty( $this->db_updates ) ) { return false; }
return $this->options()->version && version_compare( $this->options()->version, max( $this->db_updates ), '<' ); }
/** * Handle updates */ public function do_database_update() { wp_raise_memory_limit( 'admin' );
foreach ( $this->db_updates as $update ) { if ( version_compare( $this->options()->version, $update, '<' ) ) { include $this->path( "/includes/updates/$update.php" ); } }
$this->update_database_version(); }
/** * Update version to current */ public function update_database_version() { update_option( $this->options()->prefix . 'version', $this->version, true ); do_action( 'automatewoo_addon_updated' ); }
/** * Renders prompt notice for user to update */ public function data_upgrade_prompt() { AW()->admin->get_view( 'data-upgrade-prompt', [ 'plugin_name' => $this->name, 'plugin_slug' => $this->id, ] ); }
/** * Registers the add-on. * * @since 4.6.0 */ public function register() { Addons::register( $this ); }
/** * Runs when the add-on plugin is activated. */ public function activate() { flush_rewrite_rules(); AdminNotices::add_notice( 'addon_welcome_' . $this->id ); }
/** * @return string */ public function get_getting_started_url() { return ''; }
/** * @param Plugin_Data|mixed $data * @return Addon|mixed */ public static function instance( $data ) { if ( is_null( static::$_instance ) ) { static::$_instance = new static( $data ); } return static::$_instance; } }
/** * @class Plugin_Data * * phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound */ class Plugin_Data {
/** * Slug * * @var string */ public $id;
/** * Name * * @var string */ public $name;
/** * Version * * @var string */ public $version;
/** * Main plugin file * * @var string */ public $file;
/** * Minimum PHP version * * @var string */ public $min_php_version;
/** * Minimum AutomateWoo version * * @var string */ public $min_automatewoo_version;
/** * Minimum WooCommerce version * * @var string */ public $min_woocommerce_version; }
|