PluginProbe
Stream – Activity Log & Audit Trail / 4.0.2
Stream – Activity Log & Audit Trail v4.0.2
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 +80 -113 3.0.54.0.2 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
@@ -51,8 +61,10 @@
51 61 public $success_db;
52 62
53 63 /**
54 64 * Class constructor
65 + *
66 + * @param Plugin $plugin Instance of plugin object.
55 67 */
56 68 public function __construct( $plugin ) {
57 69 $this->plugin = $plugin;
58 70
@@ -58,14 +70,14 @@
58 70
59 71 $this->db_version = $this->get_db_version();
60 72 $this->stream_url = self_admin_url( $this->plugin->admin->admin_parent_page . '&page=' . $this->plugin->admin->settings_page_slug );
61 73
62 - // Check DB and display an admin notice if there are tables missing
63 - add_action( 'init', array( $this, 'verify_db' ) );
74 + // Ensure the tables are created even when the plugin activation hook does not run,
75 + // and run the check in WP Admin or on WP CLI to avoid extra frontend load.
76 + if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
77 + $this->check();
78 + }
64 79
65 - // Install the plugin
66 - add_action( 'wp_stream_before_db_notices', array( $this, 'check' ) );
67 -
68 80 register_activation_hook( $this->plugin->locations['plugin'], array( $this, 'check' ) );
69 81 }
70 82
71 83 /**
@@ -75,10 +87,8 @@
75 87 *
76 88 * @return void
77 89 */
78 90 public function check() {
79 - global $wpdb;
80 -
81 91 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
82 92 return;
83 93 }
84 94
@@ -83,9 +93,8 @@
83 93 }
84 94
85 95 if ( empty( $this->db_version ) ) {
86 96 $this->install( $this->plugin->get_version() );
87 -
88 97 return;
89 98 }
90 99
91 100 if ( $this->plugin->get_version() === $this->db_version ) {
@@ -91,22 +100,38 @@
91 100 if ( $this->plugin->get_version() === $this->db_version ) {
92 101 return;
93 102 }
94 103
95 - $update = isset( $_REQUEST['wp_stream_update'] ) ? $_REQUEST['wp_stream_update'] : null;
104 + $update = null;
105 + if ( isset( $_REQUEST['wp_stream_update'] ) && wp_verify_nonce( 'wp_stream_update_db' ) ) {
106 + $update = esc_attr( $_REQUEST['wp_stream_update'] );
107 + }
96 108
97 109 if ( ! $update ) {
98 110 $this->update_required = true;
99 - $this->success_db = $this->update( $this->db_version, $this->plugin->get_version(), array( 'type' => 'auto' ) );
100 - } elseif ( 'update_and_continue' === $update ) {
101 - $this->success_db = $this->update( $this->db_version, $this->plugin->get_version(), array( 'type' => 'user' ) );
111 + $this->success_db = $this->update(
112 + $this->db_version,
113 + $this->plugin->get_version(),
114 + array(
115 + 'type' => 'auto',
116 + )
117 + );
102 118 }
103 119
120 + if ( 'update_and_continue' === $update ) {
121 + $this->success_db = $this->update(
122 + $this->db_version,
123 + $this->plugin->get_version(),
124 + array(
125 + 'type' => 'user',
126 + )
127 + );
128 + }
129 +
104 130 $versions = $this->db_update_versions();
105 131
106 - if ( version_compare( end( $versions ), $this->db_version, '>' ) ) {
132 + if ( ! $this->success_db && version_compare( end( $versions ), $this->db_version, '>' ) ) {
107 133 add_action( 'all_admin_notices', array( $this, 'update_notice_hook' ) );
108 -
109 134 return;
110 135 }
111 136
112 137 $this->update_db_option();
@@ -112,83 +137,12 @@
112 137 $this->update_db_option();
113 138 }
114 139
115 140 /**
116 - * Verify that the required DB tables exists
117 - *
118 - * @return void
119 - */
120 - public function verify_db() {
121 - /**
122 - * Filter will halt install() if set to true
123 - *
124 - * @param bool
125 - *
126 - * @return bool
127 - */
128 - if ( apply_filters( 'wp_stream_no_tables', false ) ) {
129 - return;
130 - }
131 -
132 - if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
133 - require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
134 - }
135 -
136 - /**
137 - * Fires before admin notices are triggered for missing database tables.
138 - */
139 - do_action( 'wp_stream_before_db_notices' );
140 -
141 - global $wpdb;
142 -
143 - $database_message = '';
144 - $uninstall_message = '';
145 -
146 - // Check if all needed DB is present
147 - $missing_tables = array();
148 -
149 - foreach ( $this->plugin->db->get_table_names() as $table_name ) {
150 - $table_search = $wpdb->get_var(
151 - $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name )
152 - );
153 - if ( $table_search !== $table_name ) {
154 - $missing_tables[] = $table_name;
155 - }
156 - }
157 -
158 - if ( $missing_tables ) {
159 - $database_message .= sprintf(
160 - '%s <strong>%s</strong>',
161 - _n(
162 - 'The following table is not present in the WordPress database:',
163 - 'The following tables are not present in the WordPress database:',
164 - count( $missing_tables ),
165 - 'stream'
166 - ),
167 - esc_html( implode( ', ', $missing_tables ) )
168 - );
169 - }
170 -
171 - if ( is_plugin_active_for_network( $this->plugin->locations['plugin'] ) && current_user_can( 'manage_network_plugins' ) ) {
172 - $uninstall_message = sprintf( __( 'Please <a href="%s">uninstall</a> the Stream plugin and activate it again.', 'stream' ), network_admin_url( 'plugins.php#stream' ) );
173 - } elseif ( current_user_can( 'activate_plugins' ) ) {
174 - $uninstall_message = sprintf( __( 'Please <a href="%s">uninstall</a> the Stream plugin and activate it again.', 'stream' ), admin_url( 'plugins.php#stream' ) );
175 - }
176 -
177 - if ( ! empty( $database_message ) ) {
178 - $this->plugin->admin->notice( $database_message );
179 -
180 - if ( ! empty( $uninstall_message ) ) {
181 - $this->plugin->admin->notice( $uninstall_message );
182 - }
183 - }
184 - }
185 -
186 - /**
187 141 * Register a routine to be called when stream or a stream connector has been updated
188 142 * It works by comparing the current version with the version previously stored in the database.
189 143 *
190 - * @param string $file A reference to the main plugin file
144 + * @param string $file A reference to the main plugin file.
191 145 * @param string $callback The function to run when the hook is called.
192 146 * @param string $version The version to which the plugin is updating.
193 147 *
194 148 * @return void
@@ -199,9 +153,9 @@
199 153 }
200 154
201 155 $plugin = plugin_basename( $file );
202 156
203 - if ( is_plugin_active_for_network( $plugin ) ) {
157 + if ( $this->plugin->is_network_activated() ) {
204 158 $current_versions = get_site_option( $this->option_key . '_connectors', array() );
205 159 $network = true;
206 160 } elseif ( is_plugin_active( $plugin ) ) {
207 161 $current_versions = get_option( $this->option_key . '_connectors', array() );
@@ -220,13 +174,13 @@
220 174 update_site_option( $this->option_key . '_registered_connectors', $current_versions );
221 175 } else {
222 176 update_option( $this->option_key . '_registered_connectors', $current_versions );
223 177 }
224 -
225 - return;
226 178 }
227 179
228 180 /**
181 + * Returns the database version.
182 + *
229 183 * @return string
230 184 */
231 185 public function get_db_version() {
232 186 return get_site_option( $this->option_key );
@@ -232,9 +186,9 @@
232 186 return get_site_option( $this->option_key );
233 187 }
234 188
235 189 /**
236 - * @return void
190 + * Checks if migration was successful.
237 191 */
238 192 public function update_db_option() {
239 193 if ( $this->success_db ) {
240 194 $success_op = update_site_option( $this->option_key, $this->plugin->get_version() );
@@ -239,9 +193,9 @@
239 193 if ( $this->success_db ) {
240 194 $success_op = update_site_option( $this->option_key, $this->plugin->get_version() );
241 195 }
242 196
243 - if ( ! empty( $this->success_db ) && ! empty( $success_op ) ) {
197 + if ( ! empty( $this->success_db ) ) {
244 198 return;
245 199 }
246 200
247 201 wp_die(
@@ -265,9 +219,12 @@
265 219 if ( ! current_user_can( $this->plugin->admin->view_cap ) ) {
266 220 return;
267 221 }
268 222
269 - $update = isset( $_REQUEST['wp_stream_update'] ) ? $_REQUEST['wp_stream_update'] : null;
223 + $update = null;
224 + if ( isset( $_REQUEST['wp_stream_update'] ) && wp_verify_nonce( 'wp_stream_update_db' ) ) {
225 + $update = esc_attr( $_REQUEST['wp_stream_update'] );
226 + }
270 227
271 228 if ( ! $update ) {
272 229 $this->prompt_update();
273 230
@@ -289,15 +246,15 @@
289 246 */
290 247 public function prompt_update() {
291 248 ?>
292 249 <div class="error">
293 - <form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ) ?>">
294 - <?php wp_nonce_field( 'wp_stream_update_db' ) ?>
250 + <form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ); ?>">
251 + <?php wp_nonce_field( 'wp_stream_update_db' ); ?>
295 252 <input type="hidden" name="wp_stream_update" value="update_and_continue"/>
296 - <p><strong><?php esc_html_e( 'Stream Database Update Required', 'stream' ) ?></strong></p>
297 - <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>
298 - <p><?php esc_html_e( 'This process could take a little while, so please be patient.', 'stream' ) ?></p>
299 - <?php submit_button( esc_html__( 'Update Database', 'stream' ), 'primary', 'stream-update-db-submit' ) ?>
253 + <p><strong><?php esc_html_e( 'Stream Database Update Required', 'stream' ); ?></strong></p>
254 + <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>
255 + <p><?php esc_html_e( 'This process could take a little while, so please be patient.', 'stream' ); ?></p>
256 + <?php submit_button( esc_html__( 'Update Database', 'stream' ), 'primary', 'stream-update-db-submit' ); ?>
300 257 </form>
301 258 </div>
302 259 <?php
303 260 }
@@ -313,12 +270,21 @@
313 270
314 271 $this->update_db_option();
315 272 ?>
316 273 <div class="updated">
317 - <form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ) ?>" style="display:inline;">
318 - <p><strong><?php esc_html_e( 'Update Complete', 'stream' ) ?></strong></p>
319 - <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( $this->plugin->get_version() ) ), 'stream' ) ?></p>
320 - <?php submit_button( esc_html__( 'Continue', 'stream' ), 'secondary', false ) ?>
274 + <form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ); ?>" style="display:inline;">
275 + <p><strong><?php esc_html_e( 'Update Complete', 'stream' ); ?></strong></p>
276 + <p>
277 + <?php
278 + printf(
279 + /* translators: %1$s: old version, %2$s: new version (e.g. "4.2") */
280 + esc_html__( 'Your Stream database has been successfully updated from %1$s to %2$s!', 'stream' ),
281 + esc_html( $this->db_version ),
282 + esc_html( $this->plugin->get_version() )
283 + );
284 + ?>
285 + </p>
286 + <?php submit_button( esc_html__( 'Continue', 'stream' ), 'secondary', false ); ?>
321 287 </form>
322 288 </div>
323 289 <?php
324 290 }
@@ -334,10 +300,11 @@
334 300 * @return array
335 301 */
336 302 public function db_update_versions() {
337 303 $db_update_versions = array(
338 - '3.0.0' /* @version 3.0.0 Drop the stream_context table, changes to stream table */,
339 - '3.0.2' /* @version 3.0.2 Fix uppercase values in stream table, connector column */,
304 + '3.0.0', /* @version 3.0.0 Drop the stream_context table, changes to stream table */
305 + '3.0.2', /* @version 3.0.2 Fix uppercase values in stream table, connector column */
306 + '3.0.8', /* @version 3.0.8 Increase size of user role IDs, user_roll column */
340 307 );
341 308
342 309 /**
343 310 * Filter to alter the DB update versions array
@@ -351,17 +318,17 @@
351 318
352 319 /**
353 320 * Database user controlled update routine
354 321 *
355 - * @param int $db_version
356 - * @param int $current_version
357 - * @param array $update_args
322 + * @param int $db_version Next database version.
323 + * @param int $current_version Current database version.
324 + * @param array $update_args Update options.
358 325 *
359 326 * @return mixed Version number on success, true on no update needed, mysql error message on error
360 327 */
361 328 public function update( $db_version, $current_version, $update_args ) {
362 329 $versions = $this->db_update_versions();
363 - include_once( $this->plugin->locations['inc_dir'] . 'db-updates.php' );
330 + include_once $this->plugin->locations['inc_dir'] . 'db-updates.php';
364 331
365 332 foreach ( $versions as $version ) {
366 333 if ( ! isset( $update_args['type'] ) ) {
367 334 $update_args['type'] = 'user';
@@ -383,9 +350,9 @@
383 350
384 351 /**
385 352 * Initial database install routine
386 353 *
387 - * @param string $current_version
354 + * @param string $current_version Current database version.
388 355 *
389 356 * @return string
390 357 */
391 358 public function install( $current_version ) {
@@ -398,9 +365,9 @@
398 365 site_id bigint(20) unsigned NOT NULL DEFAULT '1',
399 366 blog_id bigint(20) unsigned NOT NULL DEFAULT '1',
400 367 object_id bigint(20) unsigned NULL,
401 368 user_id bigint(20) unsigned NOT NULL DEFAULT '0',
402 - user_role varchar(20) NOT NULL DEFAULT '',
369 + user_role varchar(50) NOT NULL DEFAULT '',
403 370 summary longtext NOT NULL,
404 371 created datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
405 372 connector varchar(100) NOT NULL,
406 373 context varchar(100) NOT NULL,
@@ -447,10 +414,10 @@
447 414 meta_key varchar(200) NOT NULL,
448 415 meta_value varchar(200) NOT NULL,
449 416 PRIMARY KEY (meta_id),
450 417 KEY record_id (record_id),
451 - KEY meta_key (meta_key),
452 - KEY meta_value (meta_value)
418 + KEY meta_key (meta_key(191)),
419 + KEY meta_value (meta_value(191))
453 420 )";
454 421
455 422 if ( ! empty( $wpdb->charset ) ) {
456 423 $sql .= " CHARACTER SET $wpdb->charset";