/
class-install.php
489 lines (405 loc) · 12.9 KB
/
class-install.php
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?php
namespace WP_Stream;
class Install {
/**
* Hold Plugin class
* @var Plugin
*/
public $plugin;
/**
* Option key to store database version
*
* @var string
*/
public $option_key = 'wp_stream_db';
/**
* Holds the database table prefix
*
* @var string
*/
public $table_prefix;
/**
* 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
*
* @var bool
*/
public $update_required = false;
/**
* Holds status of whether the database update worked
*
* @var bool
*/
public $success_db;
/**
* Class constructor
*/
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 );
// Check DB and display an admin notice if there are tables missing
add_action( 'init', array( $this, 'verify_db' ) );
// Install the plugin
add_action( 'wp_stream_before_db_notices', array( $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() {
global $wpdb;
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
/**
* Allows devs to alter the tables prefix, default to base_prefix
*
* @param string $prefix
*
* @return string
*/
$this->table_prefix = apply_filters( 'wp_stream_db_tables_prefix', $wpdb->base_prefix );
if ( empty( $this->db_version ) ) {
$this->install( $this->plugin->get_version() );
return;
}
if ( $this->plugin->get_version() === $this->db_version ) {
return;
}
$update = isset( $_REQUEST['wp_stream_update'] ) ? $_REQUEST['wp_stream_update'] : null;
if ( ! $update ) {
$this->update_required = true;
$this->success_db = $this->update( $this->db_version, $this->plugin->get_version(), array( 'type' => 'auto' ) );
return;
}
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 ( version_compare( end( $versions ), $this->db_version, '>' ) ) {
add_action( 'all_admin_notices', array( $this, 'update_notice_hook' ) );
return;
}
$this->update_db_option();
}
/**
* Verify that the required DB tables exists
*
* @return void
*/
public function verify_db() {
/**
* Filter will halt install() if set to true
*
* @param bool
*
* @return bool
*/
if ( apply_filters( 'wp_stream_no_tables', false ) ) {
return;
}
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
}
/**
* Fires before admin notices are triggered for missing database tables.
*/
do_action( 'wp_stream_before_db_notices' );
global $wpdb;
$database_message = '';
$uninstall_message = '';
// Check if all needed DB is present
$missing_tables = array();
foreach ( $this->plugin->db->get_table_names() as $table_name ) {
if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) !== $table_name ) {
$missing_tables[] = $table_name;
}
}
if ( $missing_tables ) {
$database_message .= sprintf(
'%s <strong>%s</strong>',
_n(
'The following table is not present in the WordPress database:',
'The following tables are not present in the WordPress database:',
count( $missing_tables ),
'stream'
),
esc_html( implode( ', ', $missing_tables ) )
);
}
if ( is_plugin_active_for_network( $this->plugin->locations['plugin'] ) && current_user_can( 'manage_network_plugins' ) ) {
$uninstall_message = sprintf( __( 'Please <a href="%s">uninstall</a> the Stream plugin and activate it again.', 'stream' ), network_admin_url( 'plugins.php#stream' ) );
} elseif ( current_user_can( 'activate_plugins' ) ) {
$uninstall_message = sprintf( __( 'Please <a href="%s">uninstall</a> the Stream plugin and activate it again.', 'stream' ), admin_url( 'plugins.php#stream' ) );
}
if ( ! empty( $database_message ) ) {
$this->plugin->admin->notice( $database_message );
if ( ! empty( $uninstall_message ) ) {
$this->plugin->admin->notice( $uninstall_message );
}
}
}
/**
* 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 ( is_plugin_active_for_network( $plugin ) ) {
$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 );
}
return;
}
/**
* @return string
*/
public function get_db_version() {
return get_site_option( $this->option_key );
}
/**
* @return void
*/
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 ) && ! empty( $success_op ) ) {
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 = isset( $_REQUEST['wp_stream_update'] ) ? $_REQUEST['wp_stream_update'] : null;
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 esc_html_e( sprintf( 'Your Stream database has been successfully updated from %1$s to %2$s!', esc_html( $this->db_version ), esc_html( WP_Stream::VERSION ) ), 'stream' ) ?></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 */,
);
/**
* Filter to alter the DB update versions array
*
* @param array $db_update_versions
*
* @return array
*/
return apply_filters( 'wp_stream_db_update_versions', $db_update_versions );
}
/**
* Database user controlled update routine
*
* @param int $db_version
* @param int $current_version
* @param array $update_args
*
* @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 ) : $current_version;
if ( $current_version !== $result ) {
return false;
}
}
}
return $current_version;
}
/**
* Initial database install routine
*
* @param string $current_version
*
* @return string
*/
public function install( $current_version ) {
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$prefix = $this->table_prefix;
$sql = "CREATE TABLE {$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(20) 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 {$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),
KEY meta_value (meta_value)
)";
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;
}
}