| @@ -1,25 +1,25 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Handle the SQLite activation. |
| 4 | + * | |
| 5 | + * @since 1.0.0 | |
| 6 | + * @package wp-sqlite-integration | |
| 4 | 7 | */ |
| 5 | 8 | |
| 6 | 9 | /** |
| 7 | 10 | * Redirect to the plugin's admin screen on activation. |
| 8 | 11 | * |
| 12 | + * @since 1.0.0 | |
| 13 | + * | |
| 9 | 14 | * @param string $plugin The plugin basename. |
| 10 | 15 | */ |
| 11 | 16 | function sqlite_plugin_activation_redirect( $plugin ) { |
| 12 | - if ( | |
| 13 | - plugin_basename( SQLITE_MAIN_FILE ) !== $plugin | |
| 14 | - || ! current_user_can( sqlite_plugin_get_manage_capability() ) | |
| 15 | - ) { | |
| 16 | - return; | |
| 17 | + if ( plugin_basename( SQLITE_MAIN_FILE ) === $plugin ) { | |
| 18 | + if ( wp_safe_redirect( admin_url( 'options-general.php?page=sqlite-integration' ) ) ) { | |
| 19 | + exit; | |
| 20 | + } | |
| 17 | 21 | } |
| 18 | - | |
| 19 | - if ( wp_safe_redirect( sqlite_plugin_get_admin_page_url() ) ) { | |
| 20 | - exit; | |
| 21 | - } | |
| 22 | 22 | } |
| 23 | 23 | add_action( 'activated_plugin', 'sqlite_plugin_activation_redirect' ); |
| 24 | 24 | |
| 25 | 25 | /** |
| @@ -24,34 +24,38 @@ | ||
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | 26 | * Check the URL to ensure we're on the plugin page, |
| 27 | 27 | * the user has clicked the button to install SQLite, |
| 28 | - * the user has permission to manage the database drop-in, and the nonce is valid. | |
| 28 | + * and the nonce is valid. | |
| 29 | 29 | * If the above conditions are met, run the sqlite_plugin_copy_db_file() function, |
| 30 | 30 | * and redirect to the install screen. |
| 31 | + * | |
| 32 | + * @since 1.0.0 | |
| 31 | 33 | */ |
| 32 | 34 | function sqlite_activation() { |
| 33 | - if ( | |
| 34 | - ! isset( $_GET['page'], $_GET['confirm-install'] ) | |
| 35 | - || 'sqlite-integration' !== $_GET['page'] | |
| 36 | - ) { | |
| 35 | + global $current_screen; | |
| 36 | + if ( isset( $current_screen->base ) && 'settings_page_sqlite-integration' === $current_screen->base ) { | |
| 37 | 37 | return; |
| 38 | 38 | } |
| 39 | + if ( isset( $_GET['confirm-install'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'sqlite-install' ) ) { | |
| 39 | 40 | |
| 40 | - if ( ! current_user_can( sqlite_plugin_get_manage_capability() ) ) { | |
| 41 | - wp_die( | |
| 42 | - esc_html__( 'Sorry, you are not allowed to install the SQLite database drop-in.', 'sqlite-database-integration' ), | |
| 43 | - 403 | |
| 44 | - ); | |
| 41 | + // Handle upgrading from the performance-lab plugin. | |
| 42 | + if ( isset( $_GET['upgrade-from-pl'] ) ) { | |
| 43 | + global $wp_filesystem; | |
| 44 | + require_once ABSPATH . '/wp-admin/includes/file.php'; | |
| 45 | + // Delete the previous db.php file. | |
| 46 | + $wp_filesystem->delete( WP_CONTENT_DIR . '/db.php' ); | |
| 47 | + // Deactivate the performance-lab SQLite module. | |
| 48 | + $pl_option_name = defined( 'PERFLAB_MODULES_SETTING' ) ? PERFLAB_MODULES_SETTING : 'perflab_modules_settings'; | |
| 49 | + $pl_option = get_option( $pl_option_name, array() ); | |
| 50 | + unset( $pl_option['database/sqlite'] ); | |
| 51 | + update_option( $pl_option_name, $pl_option ); | |
| 52 | + } | |
| 53 | + sqlite_plugin_copy_db_file(); | |
| 54 | + // WordPress will automatically redirect to the install screen here. | |
| 55 | + wp_redirect( admin_url() ); | |
| 56 | + exit; | |
| 45 | 57 | } |
| 46 | - | |
| 47 | - check_admin_referer( 'sqlite-install' ); | |
| 48 | - | |
| 49 | - sqlite_plugin_copy_db_file(); | |
| 50 | - | |
| 51 | - // WordPress will automatically redirect to the install screen here. | |
| 52 | - wp_redirect( admin_url() ); | |
| 53 | - exit; | |
| 54 | 58 | } |
| 55 | 59 | add_action( 'admin_init', 'sqlite_activation' ); |
| 56 | 60 | |
| 57 | 61 | // Flush the cache at the last moment before the redirect. |
| @@ -70,9 +74,9 @@ | ||
| 70 | 74 | * |
| 71 | 75 | * When the plugin gets merged in wp-core, this is not to be ported. |
| 72 | 76 | */ |
| 73 | 77 | function sqlite_plugin_copy_db_file() { |
| 74 | - // Bail early if the pdo_sqlite extension is not loaded. | |
| 78 | + // Bail early if the PDO SQLite extension is not loaded. | |
| 75 | 79 | if ( ! extension_loaded( 'pdo_sqlite' ) ) { |
| 76 | 80 | return; |
| 77 | 81 | } |
| 78 | 82 | |