| @@ -3,8 +3,9 @@ | ||
| 3 | 3 | |
| 4 | 4 | class Install { |
| 5 | 5 | /** |
| 6 | 6 | * Hold Plugin class |
| 7 | + * | |
| 7 | 8 | * @var Plugin |
| 8 | 9 | */ |
| 9 | 10 | public $plugin; |
| 10 | 11 | |
| @@ -15,15 +16,8 @@ | ||
| 15 | 16 | */ |
| 16 | 17 | public $option_key = 'wp_stream_db'; |
| 17 | 18 | |
| 18 | 19 | /** |
| 19 | - * Holds the database table prefix | |
| 20 | - * | |
| 21 | - * @var string | |
| 22 | - */ | |
| 23 | - public $table_prefix; | |
| 24 | - | |
| 25 | - /** | |
| 26 | 20 | * Holds version of database at last update |
| 27 | 21 | * |
| 28 | 22 | * @var string |
| 29 | 23 | */ |
| @@ -82,26 +76,14 @@ | ||
| 82 | 76 | * |
| 83 | 77 | * @return void |
| 84 | 78 | */ |
| 85 | 79 | public function check() { |
| 86 | - global $wpdb; | |
| 87 | - | |
| 88 | 80 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 89 | 81 | return; |
| 90 | 82 | } |
| 91 | 83 | |
| 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 | 84 | if ( empty( $this->db_version ) ) { |
| 102 | 85 | $this->install( $this->plugin->get_version() ); |
| 103 | - | |
| 104 | 86 | return; |
| 105 | 87 | } |
| 106 | 88 | |
| 107 | 89 | if ( $this->plugin->get_version() === $this->db_version ) { |
| @@ -107,22 +89,38 @@ | ||
| 107 | 89 | if ( $this->plugin->get_version() === $this->db_version ) { |
| 108 | 90 | return; |
| 109 | 91 | } |
| 110 | 92 | |
| 111 | - $update = isset( $_REQUEST['wp_stream_update'] ) ? $_REQUEST['wp_stream_update'] : null; | |
| 93 | + $update = null; | |
| 94 | + if ( isset( $_REQUEST['wp_stream_update'] ) && wp_verify_nonce( 'wp_stream_update_db' ) ) { | |
| 95 | + $update = esc_attr( $_REQUEST['wp_stream_update'] ); | |
| 96 | + } | |
| 112 | 97 | |
| 113 | 98 | if ( ! $update ) { |
| 114 | 99 | $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' ) ); | |
| 100 | + $this->success_db = $this->update( | |
| 101 | + $this->db_version, | |
| 102 | + $this->plugin->get_version(), | |
| 103 | + array( | |
| 104 | + 'type' => 'auto', | |
| 105 | + ) | |
| 106 | + ); | |
| 118 | 107 | } |
| 119 | 108 | |
| 109 | + if ( 'update_and_continue' === $update ) { | |
| 110 | + $this->success_db = $this->update( | |
| 111 | + $this->db_version, | |
| 112 | + $this->plugin->get_version(), | |
| 113 | + array( | |
| 114 | + 'type' => 'user', | |
| 115 | + ) | |
| 116 | + ); | |
| 117 | + } | |
| 118 | + | |
| 120 | 119 | $versions = $this->db_update_versions(); |
| 121 | 120 | |
| 122 | - if ( version_compare( end( $versions ), $this->db_version, '>' ) ) { | |
| 121 | + if ( ! $this->success_db && version_compare( end( $versions ), $this->db_version, '>' ) ) { | |
| 123 | 122 | add_action( 'all_admin_notices', array( $this, 'update_notice_hook' ) ); |
| 124 | - | |
| 125 | 123 | return; |
| 126 | 124 | } |
| 127 | 125 | |
| 128 | 126 | $this->update_db_option(); |
| @@ -145,9 +143,9 @@ | ||
| 145 | 143 | return; |
| 146 | 144 | } |
| 147 | 145 | |
| 148 | 146 | if ( ! function_exists( 'is_plugin_active_for_network' ) ) { |
| 149 | - require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); | |
| 147 | + require_once ABSPATH . '/wp-admin/includes/plugin.php'; | |
| 150 | 148 | } |
| 151 | 149 | |
| 152 | 150 | /** |
| 153 | 151 | * Fires before admin notices are triggered for missing database tables. |
| @@ -162,9 +160,12 @@ | ||
| 162 | 160 | // Check if all needed DB is present |
| 163 | 161 | $missing_tables = array(); |
| 164 | 162 | |
| 165 | 163 | foreach ( $this->plugin->db->get_table_names() as $table_name ) { |
| 166 | - if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) !== $table_name ) { | |
| 164 | + $table_search = $wpdb->get_var( | |
| 165 | + $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) | |
| 166 | + ); | |
| 167 | + if ( $table_search !== $table_name ) { | |
| 167 | 168 | $missing_tables[] = $table_name; |
| 168 | 169 | } |
| 169 | 170 | } |
| 170 | 171 | |
| @@ -180,12 +181,22 @@ | ||
| 180 | 181 | esc_html( implode( ', ', $missing_tables ) ) |
| 181 | 182 | ); |
| 182 | 183 | } |
| 183 | 184 | |
| 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' ) ); | |
| 185 | + if ( $this->plugin->is_network_activated() && current_user_can( 'manage_network_plugins' ) ) { | |
| 186 | + $uninstall_message = sprintf( | |
| 187 | + // translators: Placeholders refer to HTML Link tags (e.g. "<a href="https://foo.com/wp-admin/">") | |
| 188 | + __( 'Please %1$suninstall%2$s the Stream plugin and activate it again.', 'stream' ), | |
| 189 | + '<a href="' . network_admin_url( 'plugins.php#stream' ) . '">', | |
| 190 | + '</a>' | |
| 191 | + ); | |
| 186 | 192 | } 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' ) ); | |
| 193 | + $uninstall_message = sprintf( | |
| 194 | + // translators: Placeholders refer to HTML Link tags (e.g. "<a href="https://foo.com/wp-admin/">") | |
| 195 | + __( 'Please %1$suninstall%2$s the Stream plugin and activate it again.', 'stream' ), | |
| 196 | + '<a href="' . admin_url( 'plugins.php#stream' ) . '">', | |
| 197 | + '</a>' | |
| 198 | + ); | |
| 188 | 199 | } |
| 189 | 200 | |
| 190 | 201 | if ( ! empty( $database_message ) ) { |
| 191 | 202 | $this->plugin->admin->notice( $database_message ); |
| @@ -212,9 +223,9 @@ | ||
| 212 | 223 | } |
| 213 | 224 | |
| 214 | 225 | $plugin = plugin_basename( $file ); |
| 215 | 226 | |
| 216 | - if ( is_plugin_active_for_network( $plugin ) ) { | |
| 227 | + if ( $this->plugin->is_network_activated() ) { | |
| 217 | 228 | $current_versions = get_site_option( $this->option_key . '_connectors', array() ); |
| 218 | 229 | $network = true; |
| 219 | 230 | } elseif ( is_plugin_active( $plugin ) ) { |
| 220 | 231 | $current_versions = get_option( $this->option_key . '_connectors', array() ); |
| @@ -233,10 +244,8 @@ | ||
| 233 | 244 | update_site_option( $this->option_key . '_registered_connectors', $current_versions ); |
| 234 | 245 | } else { |
| 235 | 246 | update_option( $this->option_key . '_registered_connectors', $current_versions ); |
| 236 | 247 | } |
| 237 | - | |
| 238 | - return; | |
| 239 | 248 | } |
| 240 | 249 | |
| 241 | 250 | /** |
| 242 | 251 | * @return string |
| @@ -252,9 +261,9 @@ | ||
| 252 | 261 | if ( $this->success_db ) { |
| 253 | 262 | $success_op = update_site_option( $this->option_key, $this->plugin->get_version() ); |
| 254 | 263 | } |
| 255 | 264 | |
| 256 | - if ( ! empty( $this->success_db ) && ! empty( $success_op ) ) { | |
| 265 | + if ( ! empty( $this->success_db ) ) { | |
| 257 | 266 | return; |
| 258 | 267 | } |
| 259 | 268 | |
| 260 | 269 | wp_die( |
| @@ -278,9 +287,12 @@ | ||
| 278 | 287 | if ( ! current_user_can( $this->plugin->admin->view_cap ) ) { |
| 279 | 288 | return; |
| 280 | 289 | } |
| 281 | 290 | |
| 282 | - $update = isset( $_REQUEST['wp_stream_update'] ) ? $_REQUEST['wp_stream_update'] : null; | |
| 291 | + $update = null; | |
| 292 | + if ( isset( $_REQUEST['wp_stream_update'] ) && wp_verify_nonce( 'wp_stream_update_db' ) ) { | |
| 293 | + $update = esc_attr( $_REQUEST['wp_stream_update'] ); | |
| 294 | + } | |
| 283 | 295 | |
| 284 | 296 | if ( ! $update ) { |
| 285 | 297 | $this->prompt_update(); |
| 286 | 298 | |
| @@ -302,15 +314,15 @@ | ||
| 302 | 314 | */ |
| 303 | 315 | public function prompt_update() { |
| 304 | 316 | ?> |
| 305 | 317 | <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' ) ?> | |
| 318 | + <form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ); ?>"> | |
| 319 | + <?php wp_nonce_field( 'wp_stream_update_db' ); ?> | |
| 308 | 320 | <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' ) ?> | |
| 321 | + <p><strong><?php esc_html_e( 'Stream Database Update Required', 'stream' ); ?></strong></p> | |
| 322 | + <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> | |
| 323 | + <p><?php esc_html_e( 'This process could take a little while, so please be patient.', 'stream' ); ?></p> | |
| 324 | + <?php submit_button( esc_html__( 'Update Database', 'stream' ), 'primary', 'stream-update-db-submit' ); ?> | |
| 313 | 325 | </form> |
| 314 | 326 | </div> |
| 315 | 327 | <?php |
| 316 | 328 | } |
| @@ -326,12 +338,21 @@ | ||
| 326 | 338 | |
| 327 | 339 | $this->update_db_option(); |
| 328 | 340 | ?> |
| 329 | 341 | <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 ) ?> | |
| 342 | + <form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ); ?>" style="display:inline;"> | |
| 343 | + <p><strong><?php esc_html_e( 'Update Complete', 'stream' ); ?></strong></p> | |
| 344 | + <p> | |
| 345 | + <?php | |
| 346 | + printf( | |
| 347 | + // translators: Placeholders refer to version numbers (e.g. "4.2") | |
| 348 | + esc_html__( 'Your Stream database has been successfully updated from %1$s to %2$s!', 'stream' ), | |
| 349 | + esc_html( $this->db_version ), | |
| 350 | + esc_html( $this->plugin->get_version() ) | |
| 351 | + ); | |
| 352 | + ?> | |
| 353 | + </p> | |
| 354 | + <?php submit_button( esc_html__( 'Continue', 'stream' ), 'secondary', false ); ?> | |
| 334 | 355 | </form> |
| 335 | 356 | </div> |
| 336 | 357 | <?php |
| 337 | 358 | } |
| @@ -347,10 +368,11 @@ | ||
| 347 | 368 | * @return array |
| 348 | 369 | */ |
| 349 | 370 | public function db_update_versions() { |
| 350 | 371 | $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 */, | |
| 372 | + '3.0.0', /* @version 3.0.0 Drop the stream_context table, changes to stream table */ | |
| 373 | + '3.0.2', /* @version 3.0.2 Fix uppercase values in stream table, connector column */ | |
| 374 | + '3.0.8', /* @version 3.0.8 Increase size of user role IDs, user_roll column */ | |
| 353 | 375 | ); |
| 354 | 376 | |
| 355 | 377 | /** |
| 356 | 378 | * Filter to alter the DB update versions array |
| @@ -372,9 +394,9 @@ | ||
| 372 | 394 | * @return mixed Version number on success, true on no update needed, mysql error message on error |
| 373 | 395 | */ |
| 374 | 396 | public function update( $db_version, $current_version, $update_args ) { |
| 375 | 397 | $versions = $this->db_update_versions(); |
| 376 | - include_once( $this->plugin->locations['inc_dir'] . 'db-updates.php' ); | |
| 398 | + include_once $this->plugin->locations['inc_dir'] . 'db-updates.php'; | |
| 377 | 399 | |
| 378 | 400 | foreach ( $versions as $version ) { |
| 379 | 401 | if ( ! isset( $update_args['type'] ) ) { |
| 380 | 402 | $update_args['type'] = 'user'; |
| @@ -405,17 +427,15 @@ | ||
| 405 | 427 | global $wpdb; |
| 406 | 428 | |
| 407 | 429 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 408 | 430 | |
| 409 | - $prefix = $this->table_prefix; | |
| 410 | - | |
| 411 | - $sql = "CREATE TABLE {$prefix}stream ( | |
| 431 | + $sql = "CREATE TABLE {$wpdb->base_prefix}stream ( | |
| 412 | 432 | ID bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 413 | 433 | site_id bigint(20) unsigned NOT NULL DEFAULT '1', |
| 414 | 434 | blog_id bigint(20) unsigned NOT NULL DEFAULT '1', |
| 415 | 435 | object_id bigint(20) unsigned NULL, |
| 416 | 436 | user_id bigint(20) unsigned NOT NULL DEFAULT '0', |
| 417 | - user_role varchar(20) NOT NULL DEFAULT '', | |
| 437 | + user_role varchar(50) NOT NULL DEFAULT '', | |
| 418 | 438 | summary longtext NOT NULL, |
| 419 | 439 | created datetime NOT NULL DEFAULT '0000-00-00 00:00:00', |
| 420 | 440 | connector varchar(100) NOT NULL, |
| 421 | 441 | context varchar(100) NOT NULL, |
| @@ -455,9 +475,9 @@ | ||
| 455 | 475 | $sql .= ';'; |
| 456 | 476 | |
| 457 | 477 | \dbDelta( $sql ); |
| 458 | 478 | |
| 459 | - $sql = "CREATE TABLE {$prefix}stream_meta ( | |
| 479 | + $sql = "CREATE TABLE {$wpdb->base_prefix}stream_meta ( | |
| 460 | 480 | meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 461 | 481 | record_id bigint(20) unsigned NOT NULL, |
| 462 | 482 | meta_key varchar(200) NOT NULL, |
| 463 | 483 | meta_value varchar(200) NOT NULL, |
| @@ -462,10 +482,10 @@ | ||
| 462 | 482 | meta_key varchar(200) NOT NULL, |
| 463 | 483 | meta_value varchar(200) NOT NULL, |
| 464 | 484 | PRIMARY KEY (meta_id), |
| 465 | 485 | KEY record_id (record_id), |
| 466 | - KEY meta_key (meta_key), | |
| 467 | - KEY meta_value (meta_value) | |
| 486 | + KEY meta_key (meta_key(191)), | |
| 487 | + KEY meta_value (meta_value(191)) | |
| 468 | 488 | )"; |
| 469 | 489 | |
| 470 | 490 | if ( ! empty( $wpdb->charset ) ) { |
| 471 | 491 | $sql .= " CHARACTER SET $wpdb->charset"; |