| @@ -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,15 +25,8 @@ | ||
| 15 | 25 | */ |
| 16 | 26 | public $option_key = 'wp_stream_db'; |
| 17 | 27 | |
| 18 | 28 | /** |
| 19 | - * Holds the database table prefix | |
| 20 | - * | |
| 21 | - * @var string | |
| 22 | - */ | |
| 23 | - public $table_prefix; | |
| 24 | - | |
| 25 | - /** | |
| 26 | 29 | * Holds version of database at last update |
| 27 | 30 | * |
| 28 | 31 | * @var string |
| 29 | 32 | */ |
| @@ -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,14 @@ | ||
| 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' ) ); | |
| 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 | + } | |
| 71 | 79 | |
| 72 | - // Install the plugin | |
| 73 | - add_action( 'wp_stream_before_db_notices', array( $this, 'check' ) ); | |
| 74 | - | |
| 75 | 80 | register_activation_hook( $this->plugin->locations['plugin'], array( $this, 'check' ) ); |
| 76 | 81 | } |
| 77 | 82 | |
| 78 | 83 | /** |
| @@ -82,26 +87,14 @@ | ||
| 82 | 87 | * |
| 83 | 88 | * @return void |
| 84 | 89 | */ |
| 85 | 90 | public function check() { |
| 86 | - global $wpdb; | |
| 87 | - | |
| 88 | 91 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 89 | 92 | return; |
| 90 | 93 | } |
| 91 | 94 | |
| 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 | 95 | if ( empty( $this->db_version ) ) { |
| 102 | 96 | $this->install( $this->plugin->get_version() ); |
| 103 | - | |
| 104 | 97 | return; |
| 105 | 98 | } |
| 106 | 99 | |
| 107 | 100 | if ( $this->plugin->get_version() === $this->db_version ) { |
| @@ -107,22 +100,38 @@ | ||
| 107 | 100 | if ( $this->plugin->get_version() === $this->db_version ) { |
| 108 | 101 | return; |
| 109 | 102 | } |
| 110 | 103 | |
| 111 | - $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 | + } | |
| 112 | 108 | |
| 113 | 109 | if ( ! $update ) { |
| 114 | 110 | $this->update_required = true; |
| 115 | - $this->success_db = $this->update( $this->db_version, $this->plugin->get_version(), array( 'type' => 'auto' ) ); | |
| 116 | - } elseif ( 'update_and_continue' === $update ) { | |
| 117 | - $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 | + ); | |
| 118 | 118 | } |
| 119 | 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 | + | |
| 120 | 130 | $versions = $this->db_update_versions(); |
| 121 | 131 | |
| 122 | - if ( version_compare( end( $versions ), $this->db_version, '>' ) ) { | |
| 132 | + if ( ! $this->success_db && version_compare( end( $versions ), $this->db_version, '>' ) ) { | |
| 123 | 133 | add_action( 'all_admin_notices', array( $this, 'update_notice_hook' ) ); |
| 124 | - | |
| 125 | 134 | return; |
| 126 | 135 | } |
| 127 | 136 | |
| 128 | 137 | $this->update_db_option(); |
| @@ -128,80 +137,12 @@ | ||
| 128 | 137 | $this->update_db_option(); |
| 129 | 138 | } |
| 130 | 139 | |
| 131 | 140 | /** |
| 132 | - * Verify that the required DB tables exists | |
| 133 | - * | |
| 134 | - * @return void | |
| 135 | - */ | |
| 136 | - public function verify_db() { | |
| 137 | - /** | |
| 138 | - * Filter will halt install() if set to true | |
| 139 | - * | |
| 140 | - * @param bool | |
| 141 | - * | |
| 142 | - * @return bool | |
| 143 | - */ | |
| 144 | - if ( apply_filters( 'wp_stream_no_tables', false ) ) { | |
| 145 | - return; | |
| 146 | - } | |
| 147 | - | |
| 148 | - if ( ! function_exists( 'is_plugin_active_for_network' ) ) { | |
| 149 | - require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); | |
| 150 | - } | |
| 151 | - | |
| 152 | - /** | |
| 153 | - * Fires before admin notices are triggered for missing database tables. | |
| 154 | - */ | |
| 155 | - do_action( 'wp_stream_before_db_notices' ); | |
| 156 | - | |
| 157 | - global $wpdb; | |
| 158 | - | |
| 159 | - $database_message = ''; | |
| 160 | - $uninstall_message = ''; | |
| 161 | - | |
| 162 | - // Check if all needed DB is present | |
| 163 | - $missing_tables = array(); | |
| 164 | - | |
| 165 | - foreach ( $this->plugin->db->get_table_names() as $table_name ) { | |
| 166 | - if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) !== $table_name ) { | |
| 167 | - $missing_tables[] = $table_name; | |
| 168 | - } | |
| 169 | - } | |
| 170 | - | |
| 171 | - if ( $missing_tables ) { | |
| 172 | - $database_message .= sprintf( | |
| 173 | - '%s <strong>%s</strong>', | |
| 174 | - _n( | |
| 175 | - 'The following table is not present in the WordPress database:', | |
| 176 | - 'The following tables are not present in the WordPress database:', | |
| 177 | - count( $missing_tables ), | |
| 178 | - 'stream' | |
| 179 | - ), | |
| 180 | - esc_html( implode( ', ', $missing_tables ) ) | |
| 181 | - ); | |
| 182 | - } | |
| 183 | - | |
| 184 | - if ( is_plugin_active_for_network( $this->plugin->locations['plugin'] ) && current_user_can( 'manage_network_plugins' ) ) { | |
| 185 | - $uninstall_message = sprintf( __( 'Please <a href="%s">uninstall</a> the Stream plugin and activate it again.', 'stream' ), network_admin_url( 'plugins.php#stream' ) ); | |
| 186 | - } elseif ( current_user_can( 'activate_plugins' ) ) { | |
| 187 | - $uninstall_message = sprintf( __( 'Please <a href="%s">uninstall</a> the Stream plugin and activate it again.', 'stream' ), admin_url( 'plugins.php#stream' ) ); | |
| 188 | - } | |
| 189 | - | |
| 190 | - if ( ! empty( $database_message ) ) { | |
| 191 | - $this->plugin->admin->notice( $database_message ); | |
| 192 | - | |
| 193 | - if ( ! empty( $uninstall_message ) ) { | |
| 194 | - $this->plugin->admin->notice( $uninstall_message ); | |
| 195 | - } | |
| 196 | - } | |
| 197 | - } | |
| 198 | - | |
| 199 | - /** | |
| 200 | 141 | * Register a routine to be called when stream or a stream connector has been updated |
| 201 | 142 | * It works by comparing the current version with the version previously stored in the database. |
| 202 | 143 | * |
| 203 | - * @param string $file A reference to the main plugin file | |
| 144 | + * @param string $file A reference to the main plugin file. | |
| 204 | 145 | * @param string $callback The function to run when the hook is called. |
| 205 | 146 | * @param string $version The version to which the plugin is updating. |
| 206 | 147 | * |
| 207 | 148 | * @return void |
| @@ -212,9 +153,9 @@ | ||
| 212 | 153 | } |
| 213 | 154 | |
| 214 | 155 | $plugin = plugin_basename( $file ); |
| 215 | 156 | |
| 216 | - if ( is_plugin_active_for_network( $plugin ) ) { | |
| 157 | + if ( $this->plugin->is_network_activated() ) { | |
| 217 | 158 | $current_versions = get_site_option( $this->option_key . '_connectors', array() ); |
| 218 | 159 | $network = true; |
| 219 | 160 | } elseif ( is_plugin_active( $plugin ) ) { |
| 220 | 161 | $current_versions = get_option( $this->option_key . '_connectors', array() ); |
| @@ -233,13 +174,13 @@ | ||
| 233 | 174 | update_site_option( $this->option_key . '_registered_connectors', $current_versions ); |
| 234 | 175 | } else { |
| 235 | 176 | update_option( $this->option_key . '_registered_connectors', $current_versions ); |
| 236 | 177 | } |
| 237 | - | |
| 238 | - return; | |
| 239 | 178 | } |
| 240 | 179 | |
| 241 | 180 | /** |
| 181 | + * Returns the database version. | |
| 182 | + * | |
| 242 | 183 | * @return string |
| 243 | 184 | */ |
| 244 | 185 | public function get_db_version() { |
| 245 | 186 | return get_site_option( $this->option_key ); |
| @@ -245,9 +186,9 @@ | ||
| 245 | 186 | return get_site_option( $this->option_key ); |
| 246 | 187 | } |
| 247 | 188 | |
| 248 | 189 | /** |
| 249 | - * @return void | |
| 190 | + * Checks if migration was successful. | |
| 250 | 191 | */ |
| 251 | 192 | public function update_db_option() { |
| 252 | 193 | if ( $this->success_db ) { |
| 253 | 194 | $success_op = update_site_option( $this->option_key, $this->plugin->get_version() ); |
| @@ -252,9 +193,9 @@ | ||
| 252 | 193 | if ( $this->success_db ) { |
| 253 | 194 | $success_op = update_site_option( $this->option_key, $this->plugin->get_version() ); |
| 254 | 195 | } |
| 255 | 196 | |
| 256 | - if ( ! empty( $this->success_db ) && ! empty( $success_op ) ) { | |
| 197 | + if ( ! empty( $this->success_db ) ) { | |
| 257 | 198 | return; |
| 258 | 199 | } |
| 259 | 200 | |
| 260 | 201 | wp_die( |
| @@ -278,9 +219,12 @@ | ||
| 278 | 219 | if ( ! current_user_can( $this->plugin->admin->view_cap ) ) { |
| 279 | 220 | return; |
| 280 | 221 | } |
| 281 | 222 | |
| 282 | - $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 | + } | |
| 283 | 227 | |
| 284 | 228 | if ( ! $update ) { |
| 285 | 229 | $this->prompt_update(); |
| 286 | 230 | |
| @@ -302,15 +246,15 @@ | ||
| 302 | 246 | */ |
| 303 | 247 | public function prompt_update() { |
| 304 | 248 | ?> |
| 305 | 249 | <div class="error"> |
| 306 | - <form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ) ?>"> | |
| 307 | - <?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' ); ?> | |
| 308 | 252 | <input type="hidden" name="wp_stream_update" value="update_and_continue"/> |
| 309 | - <p><strong><?php esc_html_e( 'Stream Database Update Required', 'stream' ) ?></strong></p> | |
| 310 | - <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> | |
| 311 | - <p><?php esc_html_e( 'This process could take a little while, so please be patient.', 'stream' ) ?></p> | |
| 312 | - <?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' ); ?> | |
| 313 | 257 | </form> |
| 314 | 258 | </div> |
| 315 | 259 | <?php |
| 316 | 260 | } |
| @@ -326,12 +270,21 @@ | ||
| 326 | 270 | |
| 327 | 271 | $this->update_db_option(); |
| 328 | 272 | ?> |
| 329 | 273 | <div class="updated"> |
| 330 | - <form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ) ?>" style="display:inline;"> | |
| 331 | - <p><strong><?php esc_html_e( 'Update Complete', 'stream' ) ?></strong></p> | |
| 332 | - <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> | |
| 333 | - <?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 ); ?> | |
| 334 | 287 | </form> |
| 335 | 288 | </div> |
| 336 | 289 | <?php |
| 337 | 290 | } |
| @@ -347,10 +300,11 @@ | ||
| 347 | 300 | * @return array |
| 348 | 301 | */ |
| 349 | 302 | public function db_update_versions() { |
| 350 | 303 | $db_update_versions = array( |
| 351 | - '3.0.0' /* @version 3.0.0 Drop the stream_context table, changes to stream table */, | |
| 352 | - '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 */ | |
| 353 | 307 | ); |
| 354 | 308 | |
| 355 | 309 | /** |
| 356 | 310 | * Filter to alter the DB update versions array |
| @@ -364,17 +318,17 @@ | ||
| 364 | 318 | |
| 365 | 319 | /** |
| 366 | 320 | * Database user controlled update routine |
| 367 | 321 | * |
| 368 | - * @param int $db_version | |
| 369 | - * @param int $current_version | |
| 370 | - * @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. | |
| 371 | 325 | * |
| 372 | 326 | * @return mixed Version number on success, true on no update needed, mysql error message on error |
| 373 | 327 | */ |
| 374 | 328 | public function update( $db_version, $current_version, $update_args ) { |
| 375 | 329 | $versions = $this->db_update_versions(); |
| 376 | - include_once( $this->plugin->locations['inc_dir'] . 'db-updates.php' ); | |
| 330 | + include_once $this->plugin->locations['inc_dir'] . 'db-updates.php'; | |
| 377 | 331 | |
| 378 | 332 | foreach ( $versions as $version ) { |
| 379 | 333 | if ( ! isset( $update_args['type'] ) ) { |
| 380 | 334 | $update_args['type'] = 'user'; |
| @@ -396,9 +350,9 @@ | ||
| 396 | 350 | |
| 397 | 351 | /** |
| 398 | 352 | * Initial database install routine |
| 399 | 353 | * |
| 400 | - * @param string $current_version | |
| 354 | + * @param string $current_version Current database version. | |
| 401 | 355 | * |
| 402 | 356 | * @return string |
| 403 | 357 | */ |
| 404 | 358 | public function install( $current_version ) { |
| @@ -405,17 +359,15 @@ | ||
| 405 | 359 | global $wpdb; |
| 406 | 360 | |
| 407 | 361 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 408 | 362 | |
| 409 | - $prefix = $this->table_prefix; | |
| 410 | - | |
| 411 | - $sql = "CREATE TABLE {$prefix}stream ( | |
| 363 | + $sql = "CREATE TABLE {$wpdb->base_prefix}stream ( | |
| 412 | 364 | ID bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 413 | 365 | site_id bigint(20) unsigned NOT NULL DEFAULT '1', |
| 414 | 366 | blog_id bigint(20) unsigned NOT NULL DEFAULT '1', |
| 415 | 367 | object_id bigint(20) unsigned NULL, |
| 416 | 368 | user_id bigint(20) unsigned NOT NULL DEFAULT '0', |
| 417 | - user_role varchar(20) NOT NULL DEFAULT '', | |
| 369 | + user_role varchar(50) NOT NULL DEFAULT '', | |
| 418 | 370 | summary longtext NOT NULL, |
| 419 | 371 | created datetime NOT NULL DEFAULT '0000-00-00 00:00:00', |
| 420 | 372 | connector varchar(100) NOT NULL, |
| 421 | 373 | context varchar(100) NOT NULL, |
| @@ -455,9 +407,9 @@ | ||
| 455 | 407 | $sql .= ';'; |
| 456 | 408 | |
| 457 | 409 | \dbDelta( $sql ); |
| 458 | 410 | |
| 459 | - $sql = "CREATE TABLE {$prefix}stream_meta ( | |
| 411 | + $sql = "CREATE TABLE {$wpdb->base_prefix}stream_meta ( | |
| 460 | 412 | meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 461 | 413 | record_id bigint(20) unsigned NOT NULL, |
| 462 | 414 | meta_key varchar(200) NOT NULL, |
| 463 | 415 | meta_value varchar(200) NOT NULL, |
| @@ -462,10 +414,10 @@ | ||
| 462 | 414 | meta_key varchar(200) NOT NULL, |
| 463 | 415 | meta_value varchar(200) NOT NULL, |
| 464 | 416 | PRIMARY KEY (meta_id), |
| 465 | 417 | KEY record_id (record_id), |
| 466 | - KEY meta_key (meta_key), | |
| 467 | - KEY meta_value (meta_value) | |
| 418 | + KEY meta_key (meta_key(191)), | |
| 419 | + KEY meta_value (meta_value(191)) | |
| 468 | 420 | )"; |
| 469 | 421 | |
| 470 | 422 | if ( ! empty( $wpdb->charset ) ) { |
| 471 | 423 | $sql .= " CHARACTER SET $wpdb->charset"; |