| @@ -1,19 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Handles database migrations | |
| 4 | - * | |
| 5 | - * @package WP_Stream | |
| 6 | - */ | |
| 7 | - | |
| 8 | 2 | namespace WP_Stream; |
| 9 | 3 | |
| 10 | -/** | |
| 11 | - * Class - Install | |
| 12 | - */ | |
| 13 | 4 | class Install { |
| 14 | 5 | /** |
| 15 | - * Holds Instance of plugin object | |
| 6 | + * Hold Plugin class | |
| 16 | 7 | * |
| 17 | 8 | * @var Plugin |
| 18 | 9 | */ |
| 19 | 10 | public $plugin; |
| @@ -61,10 +52,8 @@ | ||
| 61 | 52 | public $success_db; |
| 62 | 53 | |
| 63 | 54 | /** |
| 64 | 55 | * Class constructor |
| 65 | - * | |
| 66 | - * @param Plugin $plugin Instance of plugin object. | |
| 67 | 56 | */ |
| 68 | 57 | public function __construct( $plugin ) { |
| 69 | 58 | $this->plugin = $plugin; |
| 70 | 59 | |
| @@ -70,14 +59,14 @@ | ||
| 70 | 59 | |
| 71 | 60 | $this->db_version = $this->get_db_version(); |
| 72 | 61 | $this->stream_url = self_admin_url( $this->plugin->admin->admin_parent_page . '&page=' . $this->plugin->admin->settings_page_slug ); |
| 73 | 62 | |
| 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 | - } | |
| 63 | + // Check DB and display an admin notice if there are tables missing | |
| 64 | + add_action( 'init', array( $this, 'verify_db' ) ); | |
| 79 | 65 | |
| 66 | + // Install the plugin | |
| 67 | + add_action( 'wp_stream_before_db_notices', array( $this, 'check' ) ); | |
| 68 | + | |
| 80 | 69 | register_activation_hook( $this->plugin->locations['plugin'], array( $this, 'check' ) ); |
| 81 | 70 | } |
| 82 | 71 | |
| 83 | 72 | /** |
| @@ -101,14 +90,10 @@ | ||
| 101 | 90 | return; |
| 102 | 91 | } |
| 103 | 92 | |
| 104 | 93 | $update = null; |
| 105 | - if ( | |
| 106 | - isset( $_REQUEST['wp_stream_update'], $_REQUEST['_wpnonce'] ) | |
| 107 | - && | |
| 108 | - wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'wp_stream_update_db' ) | |
| 109 | - ) { | |
| 110 | - $update = sanitize_key( wp_unslash( $_REQUEST['wp_stream_update'] ) ); | |
| 94 | + if ( isset( $_REQUEST['wp_stream_update'] ) && wp_verify_nonce( 'wp_stream_update_db' ) ) { | |
| 95 | + $update = esc_attr( $_REQUEST['wp_stream_update'] ); | |
| 111 | 96 | } |
| 112 | 97 | |
| 113 | 98 | if ( ! $update ) { |
| 114 | 99 | $this->update_required = true; |
| @@ -141,12 +126,93 @@ | ||
| 141 | 126 | $this->update_db_option(); |
| 142 | 127 | } |
| 143 | 128 | |
| 144 | 129 | /** |
| 130 | + * Verify that the required DB tables exists | |
| 131 | + * | |
| 132 | + * @return void | |
| 133 | + */ | |
| 134 | + public function verify_db() { | |
| 135 | + /** | |
| 136 | + * Filter will halt install() if set to true | |
| 137 | + * | |
| 138 | + * @param bool | |
| 139 | + * | |
| 140 | + * @return bool | |
| 141 | + */ | |
| 142 | + if ( apply_filters( 'wp_stream_no_tables', false ) ) { | |
| 143 | + return; | |
| 144 | + } | |
| 145 | + | |
| 146 | + if ( ! function_exists( 'is_plugin_active_for_network' ) ) { | |
| 147 | + require_once ABSPATH . '/wp-admin/includes/plugin.php'; | |
| 148 | + } | |
| 149 | + | |
| 150 | + /** | |
| 151 | + * Fires before admin notices are triggered for missing database tables. | |
| 152 | + */ | |
| 153 | + do_action( 'wp_stream_before_db_notices' ); | |
| 154 | + | |
| 155 | + global $wpdb; | |
| 156 | + | |
| 157 | + $database_message = ''; | |
| 158 | + $uninstall_message = ''; | |
| 159 | + | |
| 160 | + // Check if all needed DB is present | |
| 161 | + $missing_tables = array(); | |
| 162 | + | |
| 163 | + foreach ( $this->plugin->db->get_table_names() as $table_name ) { | |
| 164 | + $table_search = $wpdb->get_var( | |
| 165 | + $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) | |
| 166 | + ); | |
| 167 | + if ( $table_search !== $table_name ) { | |
| 168 | + $missing_tables[] = $table_name; | |
| 169 | + } | |
| 170 | + } | |
| 171 | + | |
| 172 | + if ( $missing_tables ) { | |
| 173 | + $database_message .= sprintf( | |
| 174 | + '%s <strong>%s</strong>', | |
| 175 | + _n( | |
| 176 | + 'The following table is not present in the WordPress database:', | |
| 177 | + 'The following tables are not present in the WordPress database:', | |
| 178 | + count( $missing_tables ), | |
| 179 | + 'stream' | |
| 180 | + ), | |
| 181 | + esc_html( implode( ', ', $missing_tables ) ) | |
| 182 | + ); | |
| 183 | + } | |
| 184 | + | |
| 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 | + ); | |
| 192 | + } elseif ( current_user_can( 'activate_plugins' ) ) { | |
| 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 | + ); | |
| 199 | + } | |
| 200 | + | |
| 201 | + if ( ! empty( $database_message ) ) { | |
| 202 | + $this->plugin->admin->notice( $database_message ); | |
| 203 | + | |
| 204 | + if ( ! empty( $uninstall_message ) ) { | |
| 205 | + $this->plugin->admin->notice( $uninstall_message ); | |
| 206 | + } | |
| 207 | + } | |
| 208 | + } | |
| 209 | + | |
| 210 | + /** | |
| 145 | 211 | * Register a routine to be called when stream or a stream connector has been updated |
| 146 | 212 | * It works by comparing the current version with the version previously stored in the database. |
| 147 | 213 | * |
| 148 | - * @param string $file A reference to the main plugin file. | |
| 214 | + * @param string $file A reference to the main plugin file | |
| 149 | 215 | * @param string $callback The function to run when the hook is called. |
| 150 | 216 | * @param string $version The version to which the plugin is updating. |
| 151 | 217 | * |
| 152 | 218 | * @return void |
| @@ -181,10 +247,8 @@ | ||
| 181 | 247 | } |
| 182 | 248 | } |
| 183 | 249 | |
| 184 | 250 | /** |
| 185 | - * Returns the database version. | |
| 186 | - * | |
| 187 | 251 | * @return string |
| 188 | 252 | */ |
| 189 | 253 | public function get_db_version() { |
| 190 | 254 | return get_site_option( $this->option_key ); |
| @@ -190,9 +254,9 @@ | ||
| 190 | 254 | return get_site_option( $this->option_key ); |
| 191 | 255 | } |
| 192 | 256 | |
| 193 | 257 | /** |
| 194 | - * Checks if migration was successful. | |
| 258 | + * @return void | |
| 195 | 259 | */ |
| 196 | 260 | public function update_db_option() { |
| 197 | 261 | if ( $this->success_db ) { |
| 198 | 262 | $success_op = update_site_option( $this->option_key, $this->plugin->get_version() ); |
| @@ -224,14 +288,10 @@ | ||
| 224 | 288 | return; |
| 225 | 289 | } |
| 226 | 290 | |
| 227 | 291 | $update = null; |
| 228 | - if ( | |
| 229 | - isset( $_REQUEST['wp_stream_update'], $_REQUEST['_wpnonce'] ) | |
| 230 | - && | |
| 231 | - wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'wp_stream_update_db' ) | |
| 232 | - ) { | |
| 233 | - $update = sanitize_key( wp_unslash( $_REQUEST['wp_stream_update'] ) ); | |
| 292 | + if ( isset( $_REQUEST['wp_stream_update'] ) && wp_verify_nonce( 'wp_stream_update_db' ) ) { | |
| 293 | + $update = esc_attr( $_REQUEST['wp_stream_update'] ); | |
| 234 | 294 | } |
| 235 | 295 | |
| 236 | 296 | if ( ! $update ) { |
| 237 | 297 | $this->prompt_update(); |
| @@ -283,9 +343,9 @@ | ||
| 283 | 343 | <p><strong><?php esc_html_e( 'Update Complete', 'stream' ); ?></strong></p> |
| 284 | 344 | <p> |
| 285 | 345 | <?php |
| 286 | 346 | printf( |
| 287 | - /* translators: %1$s: old version, %2$s: new version (e.g. "4.2") */ | |
| 347 | + // translators: Placeholders refer to version numbers (e.g. "4.2") | |
| 288 | 348 | esc_html__( 'Your Stream database has been successfully updated from %1$s to %2$s!', 'stream' ), |
| 289 | 349 | esc_html( $this->db_version ), |
| 290 | 350 | esc_html( $this->plugin->get_version() ) |
| 291 | 351 | ); |
| @@ -326,11 +386,11 @@ | ||
| 326 | 386 | |
| 327 | 387 | /** |
| 328 | 388 | * Database user controlled update routine |
| 329 | 389 | * |
| 330 | - * @param int $db_version Next database version. | |
| 331 | - * @param int $current_version Current database version. | |
| 332 | - * @param array $update_args Update options. | |
| 390 | + * @param int $db_version | |
| 391 | + * @param int $current_version | |
| 392 | + * @param array $update_args | |
| 333 | 393 | * |
| 334 | 394 | * @return mixed Version number on success, true on no update needed, mysql error message on error |
| 335 | 395 | */ |
| 336 | 396 | public function update( $db_version, $current_version, $update_args ) { |
| @@ -358,9 +418,9 @@ | ||
| 358 | 418 | |
| 359 | 419 | /** |
| 360 | 420 | * Initial database install routine |
| 361 | 421 | * |
| 362 | - * @param string $current_version Current database version. | |
| 422 | + * @param string $current_version | |
| 363 | 423 | * |
| 364 | 424 | * @return string |
| 365 | 425 | */ |
| 366 | 426 | public function install( $current_version ) { |