| @@ -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 | /** |
| @@ -96,9 +108,11 @@ | ||
| 96 | 108 | |
| 97 | 109 | if ( ! $update ) { |
| 98 | 110 | $this->update_required = true; |
| 99 | 111 | $this->success_db = $this->update( |
| 100 | - $this->db_version, $this->plugin->get_version(), array( | |
| 112 | + $this->db_version, | |
| 113 | + $this->plugin->get_version(), | |
| 114 | + array( | |
| 101 | 115 | 'type' => 'auto', |
| 102 | 116 | ) |
| 103 | 117 | ); |
| 104 | 118 | } |
| @@ -104,9 +118,11 @@ | ||
| 104 | 118 | } |
| 105 | 119 | |
| 106 | 120 | if ( 'update_and_continue' === $update ) { |
| 107 | 121 | $this->success_db = $this->update( |
| 108 | - $this->db_version, $this->plugin->get_version(), array( | |
| 122 | + $this->db_version, | |
| 123 | + $this->plugin->get_version(), | |
| 124 | + array( | |
| 109 | 125 | 'type' => 'user', |
| 110 | 126 | ) |
| 111 | 127 | ); |
| 112 | 128 | } |
| @@ -121,93 +137,12 @@ | ||
| 121 | 137 | $this->update_db_option(); |
| 122 | 138 | } |
| 123 | 139 | |
| 124 | 140 | /** |
| 125 | - * Verify that the required DB tables exists | |
| 126 | - * | |
| 127 | - * @return void | |
| 128 | - */ | |
| 129 | - public function verify_db() { | |
| 130 | - /** | |
| 131 | - * Filter will halt install() if set to true | |
| 132 | - * | |
| 133 | - * @param bool | |
| 134 | - * | |
| 135 | - * @return bool | |
| 136 | - */ | |
| 137 | - if ( apply_filters( 'wp_stream_no_tables', false ) ) { | |
| 138 | - return; | |
| 139 | - } | |
| 140 | - | |
| 141 | - if ( ! function_exists( 'is_plugin_active_for_network' ) ) { | |
| 142 | - require_once ABSPATH . '/wp-admin/includes/plugin.php'; | |
| 143 | - } | |
| 144 | - | |
| 145 | - /** | |
| 146 | - * Fires before admin notices are triggered for missing database tables. | |
| 147 | - */ | |
| 148 | - do_action( 'wp_stream_before_db_notices' ); | |
| 149 | - | |
| 150 | - global $wpdb; | |
| 151 | - | |
| 152 | - $database_message = ''; | |
| 153 | - $uninstall_message = ''; | |
| 154 | - | |
| 155 | - // Check if all needed DB is present | |
| 156 | - $missing_tables = array(); | |
| 157 | - | |
| 158 | - foreach ( $this->plugin->db->get_table_names() as $table_name ) { | |
| 159 | - $table_search = $wpdb->get_var( | |
| 160 | - $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) | |
| 161 | - ); | |
| 162 | - if ( $table_search !== $table_name ) { | |
| 163 | - $missing_tables[] = $table_name; | |
| 164 | - } | |
| 165 | - } | |
| 166 | - | |
| 167 | - if ( $missing_tables ) { | |
| 168 | - $database_message .= sprintf( | |
| 169 | - '%s <strong>%s</strong>', | |
| 170 | - _n( | |
| 171 | - 'The following table is not present in the WordPress database:', | |
| 172 | - 'The following tables are not present in the WordPress database:', | |
| 173 | - count( $missing_tables ), | |
| 174 | - 'stream' | |
| 175 | - ), | |
| 176 | - esc_html( implode( ', ', $missing_tables ) ) | |
| 177 | - ); | |
| 178 | - } | |
| 179 | - | |
| 180 | - if ( is_plugin_active_for_network( $this->plugin->locations['plugin'] ) && current_user_can( 'manage_network_plugins' ) ) { | |
| 181 | - $uninstall_message = sprintf( | |
| 182 | - // translators: Placeholders refer to HTML Link tags (e.g. "<a href="https://foo.com/wp-admin/">") | |
| 183 | - __( 'Please %1$suninstall%2$s the Stream plugin and activate it again.', 'stream' ), | |
| 184 | - '<a href="' . network_admin_url( 'plugins.php#stream' ) . '">', | |
| 185 | - '</a>' | |
| 186 | - ); | |
| 187 | - } elseif ( current_user_can( 'activate_plugins' ) ) { | |
| 188 | - $uninstall_message = sprintf( | |
| 189 | - // translators: Placeholders refer to HTML Link tags (e.g. "<a href="https://foo.com/wp-admin/">") | |
| 190 | - __( 'Please %1$suninstall%2$s the Stream plugin and activate it again.', 'stream' ), | |
| 191 | - '<a href="' . admin_url( 'plugins.php#stream' ) . '">', | |
| 192 | - '</a>' | |
| 193 | - ); | |
| 194 | - } | |
| 195 | - | |
| 196 | - if ( ! empty( $database_message ) ) { | |
| 197 | - $this->plugin->admin->notice( $database_message ); | |
| 198 | - | |
| 199 | - if ( ! empty( $uninstall_message ) ) { | |
| 200 | - $this->plugin->admin->notice( $uninstall_message ); | |
| 201 | - } | |
| 202 | - } | |
| 203 | - } | |
| 204 | - | |
| 205 | - /** | |
| 206 | 141 | * Register a routine to be called when stream or a stream connector has been updated |
| 207 | 142 | * It works by comparing the current version with the version previously stored in the database. |
| 208 | 143 | * |
| 209 | - * @param string $file A reference to the main plugin file | |
| 144 | + * @param string $file A reference to the main plugin file. | |
| 210 | 145 | * @param string $callback The function to run when the hook is called. |
| 211 | 146 | * @param string $version The version to which the plugin is updating. |
| 212 | 147 | * |
| 213 | 148 | * @return void |
| @@ -218,9 +153,9 @@ | ||
| 218 | 153 | } |
| 219 | 154 | |
| 220 | 155 | $plugin = plugin_basename( $file ); |
| 221 | 156 | |
| 222 | - if ( is_plugin_active_for_network( $plugin ) ) { | |
| 157 | + if ( $this->plugin->is_network_activated() ) { | |
| 223 | 158 | $current_versions = get_site_option( $this->option_key . '_connectors', array() ); |
| 224 | 159 | $network = true; |
| 225 | 160 | } elseif ( is_plugin_active( $plugin ) ) { |
| 226 | 161 | $current_versions = get_option( $this->option_key . '_connectors', array() ); |
| @@ -242,8 +177,10 @@ | ||
| 242 | 177 | } |
| 243 | 178 | } |
| 244 | 179 | |
| 245 | 180 | /** |
| 181 | + * Returns the database version. | |
| 182 | + * | |
| 246 | 183 | * @return string |
| 247 | 184 | */ |
| 248 | 185 | public function get_db_version() { |
| 249 | 186 | return get_site_option( $this->option_key ); |
| @@ -249,9 +186,9 @@ | ||
| 249 | 186 | return get_site_option( $this->option_key ); |
| 250 | 187 | } |
| 251 | 188 | |
| 252 | 189 | /** |
| 253 | - * @return void | |
| 190 | + * Checks if migration was successful. | |
| 254 | 191 | */ |
| 255 | 192 | public function update_db_option() { |
| 256 | 193 | if ( $this->success_db ) { |
| 257 | 194 | $success_op = update_site_option( $this->option_key, $this->plugin->get_version() ); |
| @@ -338,9 +275,9 @@ | ||
| 338 | 275 | <p><strong><?php esc_html_e( 'Update Complete', 'stream' ); ?></strong></p> |
| 339 | 276 | <p> |
| 340 | 277 | <?php |
| 341 | 278 | printf( |
| 342 | - // translators: Placeholders refer to version numbers (e.g. "4.2") | |
| 279 | + /* translators: %1$s: old version, %2$s: new version (e.g. "4.2") */ | |
| 343 | 280 | esc_html__( 'Your Stream database has been successfully updated from %1$s to %2$s!', 'stream' ), |
| 344 | 281 | esc_html( $this->db_version ), |
| 345 | 282 | esc_html( $this->plugin->get_version() ) |
| 346 | 283 | ); |
| @@ -381,11 +318,11 @@ | ||
| 381 | 318 | |
| 382 | 319 | /** |
| 383 | 320 | * Database user controlled update routine |
| 384 | 321 | * |
| 385 | - * @param int $db_version | |
| 386 | - * @param int $current_version | |
| 387 | - * @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. | |
| 388 | 325 | * |
| 389 | 326 | * @return mixed Version number on success, true on no update needed, mysql error message on error |
| 390 | 327 | */ |
| 391 | 328 | public function update( $db_version, $current_version, $update_args ) { |
| @@ -413,9 +350,9 @@ | ||
| 413 | 350 | |
| 414 | 351 | /** |
| 415 | 352 | * Initial database install routine |
| 416 | 353 | * |
| 417 | - * @param string $current_version | |
| 354 | + * @param string $current_version Current database version. | |
| 418 | 355 | * |
| 419 | 356 | * @return string |
| 420 | 357 | */ |
| 421 | 358 | public function install( $current_version ) { |