PluginProbe
Stream – Activity Log & Audit Trail / 3.6.1
Stream – Activity Log & Audit Trail v3.6.1
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
← All changes | classes/class-install.php +75 -132 3.0.13.6.1 View file →
@@ -1,10 +1,20 @@
1 1 <?php
2 +/**
3 + * Handles database migrations
4 + *
5 + * @package WP_Stream
6 + */
7 +
2 8 namespace WP_Stream;
3 9
10 +/**
11 + * Class - Install
12 + */
4 13 class Install {
5 14 /**
6 - * Hold Plugin class
15 + * Holds Instance of plugin object
16 + *
7 17 * @var Plugin
8 18 */
9 19 public $plugin;
10 20
@@ -15,19 +25,12 @@
15 25 */
16 26 public $option_key = 'wp_stream_db';
17 27
18 28 /**
19 - * Holds the database table prefix
29 + * Holds version of database at last update
20 30 *
21 31 * @var string
22 32 */
23 - public $table_prefix;
24 -
25 - /**
26 - * Holds version of database at last update
27 -] *
28 - * @var string
29 - */
30 33 public $db_version;
31 34
32 35 /**
33 36 * URL to the Stream Admin settings page.
@@ -58,8 +61,10 @@
58 61 public $success_db;
59 62
60 63 /**
61 64 * Class constructor
65 + *
66 + * @param Plugin $plugin Instance of plugin object.
62 67 */
63 68 public function __construct( $plugin ) {
64 69 $this->plugin = $plugin;
65 70
@@ -65,14 +70,8 @@
65 70
66 71 $this->db_version = $this->get_db_version();
67 72 $this->stream_url = self_admin_url( $this->plugin->admin->admin_parent_page . '&page=' . $this->plugin->admin->settings_page_slug );
68 73
69 - // Check DB and display an admin notice if there are tables missing
70 - add_action( 'init', array( $this, 'verify_db' ) );
71 -
72 - // Install the plugin
73 - add_action( 'wp_stream_before_db_notices', array( $this, 'check' ) );
74 -
75 74 register_activation_hook( $this->plugin->locations['plugin'], array( $this, 'check' ) );
76 75 }
77 76
78 77 /**
@@ -82,26 +81,14 @@
82 81 *
83 82 * @return void
84 83 */
85 84 public function check() {
86 - global $wpdb;
87 -
88 85 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
89 86 return;
90 87 }
91 88
92 - /**
93 - * Allows devs to alter the tables prefix, default to base_prefix
94 - *
95 - * @param string $prefix
96 - *
97 - * @return string
98 - */
99 - $this->table_prefix = apply_filters( 'wp_stream_db_tables_prefix', $wpdb->base_prefix );
100 -
101 89 if ( empty( $this->db_version ) ) {
102 90 $this->install( $this->plugin->get_version() );
103 -
104 91 return;
105 92 }
106 93
107 94 if ( $this->plugin->get_version() === $this->db_version ) {
@@ -107,26 +94,38 @@
107 94 if ( $this->plugin->get_version() === $this->db_version ) {
108 95 return;
109 96 }
110 97
111 - $update = isset( $_REQUEST['wp_stream_update'] ) ? $_REQUEST['wp_stream_update'] : null;
98 + $update = null;
99 + if ( isset( $_REQUEST['wp_stream_update'] ) && wp_verify_nonce( 'wp_stream_update_db' ) ) {
100 + $update = esc_attr( $_REQUEST['wp_stream_update'] );
101 + }
112 102
113 103 if ( ! $update ) {
114 104 $this->update_required = true;
115 - $this->success_db = $this->update( $this->db_version, $this->plugin->get_version(), array( 'type' => 'auto' ) );
116 -
117 - return;
105 + $this->success_db = $this->update(
106 + $this->db_version,
107 + $this->plugin->get_version(),
108 + array(
109 + 'type' => 'auto',
110 + )
111 + );
118 112 }
119 113
120 114 if ( 'update_and_continue' === $update ) {
121 - $this->success_db = $this->update( $this->db_version, $this->plugin->get_version(), array( 'type' => 'user' ) );
115 + $this->success_db = $this->update(
116 + $this->db_version,
117 + $this->plugin->get_version(),
118 + array(
119 + 'type' => 'user',
120 + )
121 + );
122 122 }
123 123
124 124 $versions = $this->db_update_versions();
125 125
126 - if ( version_compare( end( $versions ), $this->db_version, '>' ) ) {
126 + if ( ! $this->success_db && version_compare( end( $versions ), $this->db_version, '>' ) ) {
127 127 add_action( 'all_admin_notices', array( $this, 'update_notice_hook' ) );
128 -
129 128 return;
130 129 }
131 130
132 131 $this->update_db_option();
@@ -132,80 +131,12 @@
132 131 $this->update_db_option();
133 132 }
134 133
135 134 /**
136 - * Verify that the required DB tables exists
137 - *
138 - * @return void
139 - */
140 - public function verify_db() {
141 - /**
142 - * Filter will halt install() if set to true
143 - *
144 - * @param bool
145 - *
146 - * @return bool
147 - */
148 - if ( apply_filters( 'wp_stream_no_tables', false ) ) {
149 - return;
150 - }
151 -
152 - if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
153 - require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
154 - }
155 -
156 - /**
157 - * Fires before admin notices are triggered for missing database tables.
158 - */
159 - do_action( 'wp_stream_before_db_notices' );
160 -
161 - global $wpdb;
162 -
163 - $database_message = '';
164 - $uninstall_message = '';
165 -
166 - // Check if all needed DB is present
167 - $missing_tables = array();
168 -
169 - foreach ( $this->plugin->db->get_table_names() as $table_name ) {
170 - if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) !== $table_name ) {
171 - $missing_tables[] = $table_name;
172 - }
173 - }
174 -
175 - if ( $missing_tables ) {
176 - $database_message .= sprintf(
177 - '%s <strong>%s</strong>',
178 - _n(
179 - 'The following table is not present in the WordPress database:',
180 - 'The following tables are not present in the WordPress database:',
181 - count( $missing_tables ),
182 - 'stream'
183 - ),
184 - esc_html( implode( ', ', $missing_tables ) )
185 - );
186 - }
187 -
188 - if ( is_plugin_active_for_network( $this->plugin->locations['plugin'] ) && current_user_can( 'manage_network_plugins' ) ) {
189 - $uninstall_message = sprintf( __( 'Please <a href="%s">uninstall</a> the Stream plugin and activate it again.', 'stream' ), network_admin_url( 'plugins.php#stream' ) );
190 - } elseif ( current_user_can( 'activate_plugins' ) ) {
191 - $uninstall_message = sprintf( __( 'Please <a href="%s">uninstall</a> the Stream plugin and activate it again.', 'stream' ), admin_url( 'plugins.php#stream' ) );
192 - }
193 -
194 - if ( ! empty( $database_message ) ) {
195 - $this->plugin->admin->notice( $database_message );
196 -
197 - if ( ! empty( $uninstall_message ) ) {
198 - $this->plugin->admin->notice( $uninstall_message );
199 - }
200 - }
201 - }
202 -
203 - /**
204 135 * Register a routine to be called when stream or a stream connector has been updated
205 136 * It works by comparing the current version with the version previously stored in the database.
206 137 *
207 - * @param string $file A reference to the main plugin file
138 + * @param string $file A reference to the main plugin file.
208 139 * @param string $callback The function to run when the hook is called.
209 140 * @param string $version The version to which the plugin is updating.
210 141 *
211 142 * @return void
@@ -216,9 +147,9 @@
216 147 }
217 148
218 149 $plugin = plugin_basename( $file );
219 150
220 - if ( is_plugin_active_for_network( $plugin ) ) {
151 + if ( $this->plugin->is_network_activated() ) {
221 152 $current_versions = get_site_option( $this->option_key . '_connectors', array() );
222 153 $network = true;
223 154 } elseif ( is_plugin_active( $plugin ) ) {
224 155 $current_versions = get_option( $this->option_key . '_connectors', array() );
@@ -237,13 +168,13 @@
237 168 update_site_option( $this->option_key . '_registered_connectors', $current_versions );
238 169 } else {
239 170 update_option( $this->option_key . '_registered_connectors', $current_versions );
240 171 }
241 -
242 - return;
243 172 }
244 173
245 174 /**
175 + * Returns the database version.
176 + *
246 177 * @return string
247 178 */
248 179 public function get_db_version() {
249 180 return get_site_option( $this->option_key );
@@ -249,9 +180,9 @@
249 180 return get_site_option( $this->option_key );
250 181 }
251 182
252 183 /**
253 - * @return void
184 + * Checks if migration was successful.
254 185 */
255 186 public function update_db_option() {
256 187 if ( $this->success_db ) {
257 188 $success_op = update_site_option( $this->option_key, $this->plugin->get_version() );
@@ -256,9 +187,9 @@
256 187 if ( $this->success_db ) {
257 188 $success_op = update_site_option( $this->option_key, $this->plugin->get_version() );
258 189 }
259 190
260 - if ( ! empty( $this->success_db ) && ! empty( $success_op ) ) {
191 + if ( ! empty( $this->success_db ) ) {
261 192 return;
262 193 }
263 194
264 195 wp_die(
@@ -282,9 +213,12 @@
282 213 if ( ! current_user_can( $this->plugin->admin->view_cap ) ) {
283 214 return;
284 215 }
285 216
286 - $update = isset( $_REQUEST['wp_stream_update'] ) ? $_REQUEST['wp_stream_update'] : null;
217 + $update = null;
218 + if ( isset( $_REQUEST['wp_stream_update'] ) && wp_verify_nonce( 'wp_stream_update_db' ) ) {
219 + $update = esc_attr( $_REQUEST['wp_stream_update'] );
220 + }
287 221
288 222 if ( ! $update ) {
289 223 $this->prompt_update();
290 224
@@ -306,15 +240,15 @@
306 240 */
307 241 public function prompt_update() {
308 242 ?>
309 243 <div class="error">
310 - <form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ) ?>">
311 - <?php wp_nonce_field( 'wp_stream_update_db' ) ?>
244 + <form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ); ?>">
245 + <?php wp_nonce_field( 'wp_stream_update_db' ); ?>
312 246 <input type="hidden" name="wp_stream_update" value="update_and_continue"/>
313 - <p><strong><?php esc_html_e( 'Stream Database Update Required', 'stream' ) ?></strong></p>
314 - <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>
315 - <p><?php esc_html_e( 'This process could take a little while, so please be patient.', 'stream' ) ?></p>
316 - <?php submit_button( esc_html__( 'Update Database', 'stream' ), 'primary', 'stream-update-db-submit' ) ?>
247 + <p><strong><?php esc_html_e( 'Stream Database Update Required', 'stream' ); ?></strong></p>
248 + <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>
249 + <p><?php esc_html_e( 'This process could take a little while, so please be patient.', 'stream' ); ?></p>
250 + <?php submit_button( esc_html__( 'Update Database', 'stream' ), 'primary', 'stream-update-db-submit' ); ?>
317 251 </form>
318 252 </div>
319 253 <?php
320 254 }
@@ -330,12 +264,21 @@
330 264
331 265 $this->update_db_option();
332 266 ?>
333 267 <div class="updated">
334 - <form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ) ?>" style="display:inline;">
335 - <p><strong><?php esc_html_e( 'Update Complete', 'stream' ) ?></strong></p>
336 - <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>
337 - <?php submit_button( esc_html__( 'Continue', 'stream' ), 'secondary', false ) ?>
268 + <form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ); ?>" style="display:inline;">
269 + <p><strong><?php esc_html_e( 'Update Complete', 'stream' ); ?></strong></p>
270 + <p>
271 + <?php
272 + printf(
273 + /* translators: %1$s: old version, %2$s: new version (e.g. "4.2") */
274 + esc_html__( 'Your Stream database has been successfully updated from %1$s to %2$s!', 'stream' ),
275 + esc_html( $this->db_version ),
276 + esc_html( $this->plugin->get_version() )
277 + );
278 + ?>
279 + </p>
280 + <?php submit_button( esc_html__( 'Continue', 'stream' ), 'secondary', false ); ?>
338 281 </form>
339 282 </div>
340 283 <?php
341 284 }
@@ -351,9 +294,11 @@
351 294 * @return array
352 295 */
353 296 public function db_update_versions() {
354 297 $db_update_versions = array(
355 - '3.0.0' /* @version 3.0.0 Drop the stream_context table, changes to stream table */,
298 + '3.0.0', /* @version 3.0.0 Drop the stream_context table, changes to stream table */
299 + '3.0.2', /* @version 3.0.2 Fix uppercase values in stream table, connector column */
300 + '3.0.8', /* @version 3.0.8 Increase size of user role IDs, user_roll column */
356 301 );
357 302
358 303 /**
359 304 * Filter to alter the DB update versions array
@@ -367,17 +312,17 @@
367 312
368 313 /**
369 314 * Database user controlled update routine
370 315 *
371 - * @param int $db_version
372 - * @param int $current_version
373 - * @param array $update_args
316 + * @param int $db_version Next database version.
317 + * @param int $current_version Current database version.
318 + * @param array $update_args Update options.
374 319 *
375 320 * @return mixed Version number on success, true on no update needed, mysql error message on error
376 321 */
377 322 public function update( $db_version, $current_version, $update_args ) {
378 323 $versions = $this->db_update_versions();
379 - include_once( $this->plugin->locations['inc_dir'] . 'db-updates.php' );
324 + include_once $this->plugin->locations['inc_dir'] . 'db-updates.php';
380 325
381 326 foreach ( $versions as $version ) {
382 327 if ( ! isset( $update_args['type'] ) ) {
383 328 $update_args['type'] = 'user';
@@ -399,9 +344,9 @@
399 344
400 345 /**
401 346 * Initial database install routine
402 347 *
403 - * @param string $current_version
348 + * @param string $current_version Current database version.
404 349 *
405 350 * @return string
406 351 */
407 352 public function install( $current_version ) {
@@ -408,17 +353,15 @@
408 353 global $wpdb;
409 354
410 355 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
411 356
412 - $prefix = $this->table_prefix;
413 -
414 - $sql = "CREATE TABLE {$prefix}stream (
357 + $sql = "CREATE TABLE {$wpdb->base_prefix}stream (
415 358 ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
416 359 site_id bigint(20) unsigned NOT NULL DEFAULT '1',
417 360 blog_id bigint(20) unsigned NOT NULL DEFAULT '1',
418 361 object_id bigint(20) unsigned NULL,
419 362 user_id bigint(20) unsigned NOT NULL DEFAULT '0',
420 - user_role varchar(20) NOT NULL DEFAULT '',
363 + user_role varchar(50) NOT NULL DEFAULT '',
421 364 summary longtext NOT NULL,
422 365 created datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
423 366 connector varchar(100) NOT NULL,
424 367 context varchar(100) NOT NULL,
@@ -458,9 +401,9 @@
458 401 $sql .= ';';
459 402
460 403 \dbDelta( $sql );
461 404
462 - $sql = "CREATE TABLE {$prefix}stream_meta (
405 + $sql = "CREATE TABLE {$wpdb->base_prefix}stream_meta (
463 406 meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
464 407 record_id bigint(20) unsigned NOT NULL,
465 408 meta_key varchar(200) NOT NULL,
466 409 meta_value varchar(200) NOT NULL,
@@ -465,10 +408,10 @@
465 408 meta_key varchar(200) NOT NULL,
466 409 meta_value varchar(200) NOT NULL,
467 410 PRIMARY KEY (meta_id),
468 411 KEY record_id (record_id),
469 - KEY meta_key (meta_key),
470 - KEY meta_value (meta_value)
412 + KEY meta_key (meta_key(191)),
413 + KEY meta_value (meta_value(191))
471 414 )";
472 415
473 416 if ( ! empty( $wpdb->charset ) ) {
474 417 $sql .= " CHARACTER SET $wpdb->charset";