Repository navigation
Expand file tree
/
Copy pathclass-install.php
More file actions
440 lines (375 loc) · 11.6 KB
/
Copy pathclass-install.php
File metadata and controls
440 lines (375 loc) · 11.6 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
/**
* Handles database migrations
*
* @package WP_Stream
*/
namespace WP_Stream;
/**
* Class - Install
*/
class Install {
/**
* Holds Instance of plugin object
*
* @var Plugin
*/
public $plugin;
/**
* Option key to store database version
*/
public string $option_key = 'wp_stream_db';
/**
* Holds version of database at last update
*
* @var string
*/
public $db_version;
/**
* URL to the Stream Admin settings page.
*
* @var string
*/
public $stream_url;
/**
* Array of version numbers that require database update
*
* @var array
*/
public $update_versions;
/**
* Holds status of whether it's safe to run Stream or not
*/
public bool $update_required = false;
/**
* Holds status of whether the database update worked
*
* @var bool
*/
public $success_db;
/**
* Class constructor
*
* @param Plugin $plugin Instance of plugin object.
*/
public function __construct( $plugin ) {
$this->plugin = $plugin;
$this->db_version = $this->get_db_version();
$this->stream_url = self_admin_url( $this->plugin->admin->admin_parent_page . '&page=' . $this->plugin->admin->settings_page_slug );
// Ensure the tables are created even when the plugin activation hook does not run,
// and run the check in WP Admin or on WP CLI to avoid extra frontend load.
if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
$this->check();
}
register_activation_hook( $this->plugin->locations['plugin'], array( $this, 'check' ) );
}
/**
* Check db version, create/update table schema accordingly
* If database update required admin notice will be given
* on the plugin update screen
*
* @return void
*/
public function check() {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
if ( empty( $this->db_version ) ) {
$this->install( $this->plugin->get_version() );
return;
}
if ( $this->plugin->get_version() === $this->db_version ) {
return;
}
$update = null;
if (
isset( $_REQUEST['wp_stream_update'], $_REQUEST['_wpnonce'] )
&&
wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'wp_stream_update_db' )
) {
$update = sanitize_key( wp_unslash( $_REQUEST['wp_stream_update'] ) );
}
if ( ! $update ) {
$this->update_required = true;
$this->success_db = $this->update(
$this->db_version,
$this->plugin->get_version(),
array(
'type' => 'auto',
)
);
}
if ( 'update_and_continue' === $update ) {
$this->success_db = $this->update(
$this->db_version,
$this->plugin->get_version(),
array(
'type' => 'user',
)
);
}
$versions = $this->db_update_versions();
if ( ! $this->success_db && version_compare( end( $versions ), $this->db_version, '>' ) ) {
add_action( 'all_admin_notices', array( $this, 'update_notice_hook' ) );
return;
}
$this->update_db_option();
}
/**
* Register a routine to be called when stream or a stream connector has been updated
* It works by comparing the current version with the version previously stored in the database.
*
* @param string $file A reference to the main plugin file.
* @param string $callback The function to run when the hook is called.
* @param string $version The version to which the plugin is updating.
*
* @return void
*/
public function register_update_hook( $file, $callback, $version ) {
if ( ! is_admin() ) {
return;
}
$plugin = plugin_basename( $file );
if ( $this->plugin->is_network_activated() ) {
$current_versions = get_site_option( $this->option_key . '_connectors', array() );
$network = true;
} elseif ( is_plugin_active( $plugin ) ) {
$current_versions = get_option( $this->option_key . '_connectors', array() );
$network = false;
} else {
return;
}
if ( version_compare( $version, $current_versions[ $plugin ], '>' ) ) {
call_user_func( $callback, $current_versions[ $plugin ], $network );
$current_versions[ $plugin ] = $version;
}
if ( $network ) {
update_site_option( $this->option_key . '_registered_connectors', $current_versions );
} else {
update_option( $this->option_key . '_registered_connectors', $current_versions );
}
}
/**
* Returns the database version.
*
* @return string
*/
public function get_db_version() {
return get_site_option( $this->option_key );
}
/**
* Checks if migration was successful.
*/
public function update_db_option() {
if ( $this->success_db ) {
$success_op = update_site_option( $this->option_key, $this->plugin->get_version() );
}
if ( ! empty( $this->success_db ) ) {
return;
}
wp_die(
esc_html__( 'There was an error updating the Stream database. Please try again.', 'stream' ),
esc_html__( 'Database Update Error', 'stream' ),
array(
'response' => 200,
'back_link' => 1,
)
);
}
/**
* Added to the admin_notices hook when file plugin version is higher than database plugin version
*
* @action admin_notices
*
* @return void
*/
public function update_notice_hook() {
if ( ! current_user_can( $this->plugin->admin->view_cap ) ) {
return;
}
$update = null;
if (
isset( $_REQUEST['wp_stream_update'], $_REQUEST['_wpnonce'] )
&&
wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'wp_stream_update_db' )
) {
$update = sanitize_key( wp_unslash( $_REQUEST['wp_stream_update'] ) );
}
if ( ! $update ) {
$this->prompt_update();
return;
}
if ( 'update_and_continue' === $update ) {
$this->prompt_update_status();
}
}
/**
* Action hook callback function
*
* Adds the user controlled database upgrade routine to the plugins updated page.
* When database update is complete page will refresh with dismissible message to user.
*
* @return void
*/
public function prompt_update() {
?>
<div class="error">
<form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ); ?>">
<?php wp_nonce_field( 'wp_stream_update_db' ); ?>
<input type="hidden" name="wp_stream_update" value="update_and_continue"/>
<p><strong><?php esc_html_e( 'Stream Database Update Required', 'stream' ); ?></strong></p>
<p><?php esc_html_e( 'Stream has updated! Before we send you on your way, we need to update your database to the newest version.', 'stream' ); ?></p>
<p><?php esc_html_e( 'This process could take a little while, so please be patient.', 'stream' ); ?></p>
<?php submit_button( esc_html__( 'Update Database', 'stream' ), 'primary', 'stream-update-db-submit' ); ?>
</form>
</div>
<?php
}
/**
* When user initiates a database update this function calls the update methods, checks for success
* updates the stream_db version number in the database and outputs a success and continue message
*
* @return void
*/
public function prompt_update_status() {
check_admin_referer( 'wp_stream_update_db' );
$this->update_db_option();
?>
<div class="updated">
<form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ); ?>" style="display:inline;">
<p><strong><?php esc_html_e( 'Update Complete', 'stream' ); ?></strong></p>
<p>
<?php
printf(
/* translators: %1$s: old version, %2$s: new version (e.g. "4.2") */
esc_html__( 'Your Stream database has been successfully updated from %1$s to %2$s!', 'stream' ),
esc_html( $this->db_version ),
esc_html( $this->plugin->get_version() )
);
?>
</p>
<?php submit_button( esc_html__( 'Continue', 'stream' ), 'secondary', false ); ?>
</form>
</div>
<?php
}
/**
* Array of database versions that require and updates
*
* To add your own stream extension database update routine
* use the filter and return the version that requires an update
* You must also make the callback function available in the global namespace on plugins loaded
* use the wp_stream_update_{version_number} version number must be a string of characters that represent the version with no periods
*
* @return array
*/
public function db_update_versions() {
$db_update_versions = array(
'3.0.0', /* @version 3.0.0 Drop the stream_context table, changes to stream table */
'3.0.2', /* @version 3.0.2 Fix uppercase values in stream table, connector column */
'3.0.8', /* @version 3.0.8 Increase size of user role IDs, user_roll column */
);
/**
* Filter to alter the DB update versions array
*
* @param array $db_update_versions
*/
return apply_filters( 'wp_stream_db_update_versions', $db_update_versions );
}
/**
* Database user controlled update routine
*
* @param int $db_version Next database version.
* @param int $current_version Current database version.
* @param array $update_args Update options.
*
* @return mixed Version number on success, true on no update needed, mysql error message on error
*/
public function update( $db_version, $current_version, $update_args ) {
$versions = $this->db_update_versions();
include_once $this->plugin->locations['inc_dir'] . 'db-updates.php';
foreach ( $versions as $version ) {
if ( ! isset( $update_args['type'] ) ) {
$update_args['type'] = 'user';
}
$function = 'wp_stream_update_' . ( 'user' === $update_args['type'] ? '' : $update_args['type'] . '_' ) . str_ireplace( '.', '', $version );
if ( version_compare( $db_version, $version, '<' ) ) {
$result = function_exists( $function ) ? call_user_func( $function, $db_version, $current_version, $this ) : $current_version;
if ( $current_version !== $result ) {
return false;
}
}
}
return $current_version;
}
/**
* Initial database install routine
*
* @param string $current_version Current database version.
*
* @return string
*/
public function install( $current_version ) {
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$sql = "CREATE TABLE {$wpdb->base_prefix}stream (
ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
site_id bigint(20) unsigned NOT NULL DEFAULT '1',
blog_id bigint(20) unsigned NOT NULL DEFAULT '1',
object_id bigint(20) unsigned NULL,
user_id bigint(20) unsigned NOT NULL DEFAULT '0',
user_role varchar(50) NOT NULL DEFAULT '',
summary longtext NOT NULL,
created datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
connector varchar(100) NOT NULL,
context varchar(100) NOT NULL,
action varchar(100) NOT NULL,
ip varchar(39) NULL,
PRIMARY KEY (ID),
KEY site_id (site_id),
KEY blog_id (blog_id),
KEY object_id (object_id),
KEY user_id (user_id),
KEY created (created),
KEY connector (connector),
KEY context (context),
KEY action (action)
)";
if ( ! empty( $wpdb->charset ) ) {
$sql .= " CHARACTER SET $wpdb->charset";
}
if ( ! empty( $wpdb->collate ) ) {
$sql .= " COLLATE $wpdb->collate";
}
$sql .= ';';
\dbDelta( $sql );
if ( ! empty( $wpdb->charset ) ) {
$sql .= " CHARACTER SET $wpdb->charset";
}
if ( ! empty( $wpdb->collate ) ) {
$sql .= " COLLATE $wpdb->collate";
}
$sql .= ';';
\dbDelta( $sql );
$sql = "CREATE TABLE {$wpdb->base_prefix}stream_meta (
meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
record_id bigint(20) unsigned NOT NULL,
meta_key varchar(200) NOT NULL,
meta_value varchar(200) NOT NULL,
PRIMARY KEY (meta_id),
KEY record_id (record_id),
KEY meta_key (meta_key(191)),
KEY meta_value (meta_value(191))
)";
if ( ! empty( $wpdb->charset ) ) {
$sql .= " CHARACTER SET $wpdb->charset";
}
if ( ! empty( $wpdb->collate ) ) {
$sql .= " COLLATE $wpdb->collate";
}
$sql .= ';';
\dbDelta( $sql );
update_site_option( $this->option_key, $this->plugin->get_version() );
return $current_version;
}
}