| @@ -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 | |
| 7 | 4 | */ |
| 8 | 5 | |
| 9 | 6 | /** |
| 10 | 7 | * Redirect to the plugin's admin screen on activation. |
| 11 | 8 | * |
| 12 | - * @since 1.0.0 | |
| 13 | - * | |
| 14 | 9 | * @param string $plugin The plugin basename. |
| 15 | 10 | */ |
| 16 | 11 | function sqlite_plugin_activation_redirect( $plugin ) { |
| 17 | - if ( plugin_basename( SQLITE_MAIN_FILE ) === $plugin ) { | |
| 18 | - if ( wp_safe_redirect( admin_url( 'options-general.php?page=sqlite-integration' ) ) ) { | |
| 19 | - exit; | |
| 20 | - } | |
| 12 | + if ( | |
| 13 | + plugin_basename( SQLITE_MAIN_FILE ) !== $plugin | |
| 14 | + || ! current_user_can( sqlite_plugin_get_manage_capability() ) | |
| 15 | + ) { | |
| 16 | + return; | |
| 21 | 17 | } |
| 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,38 +24,34 @@ | ||
| 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 | - * and the nonce is valid. | |
| 28 | + * the user has permission to manage the database drop-in, 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 | |
| 33 | 31 | */ |
| 34 | 32 | function sqlite_activation() { |
| 35 | - global $current_screen; | |
| 36 | - if ( isset( $current_screen->base ) && 'settings_page_sqlite-integration' === $current_screen->base ) { | |
| 33 | + if ( | |
| 34 | + ! isset( $_GET['page'], $_GET['confirm-install'] ) | |
| 35 | + || 'sqlite-integration' !== $_GET['page'] | |
| 36 | + ) { | |
| 37 | 37 | return; |
| 38 | 38 | } |
| 39 | - if ( isset( $_GET['confirm-install'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'sqlite-install' ) ) { | |
| 40 | 39 | |
| 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; | |
| 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 | + ); | |
| 57 | 45 | } |
| 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; | |
| 58 | 54 | } |
| 59 | 55 | add_action( 'admin_init', 'sqlite_activation' ); |
| 60 | 56 | |
| 61 | 57 | // Flush the cache at the last moment before the redirect. |
| @@ -74,14 +70,36 @@ | ||
| 74 | 70 | * |
| 75 | 71 | * When the plugin gets merged in wp-core, this is not to be ported. |
| 76 | 72 | */ |
| 77 | 73 | function sqlite_plugin_copy_db_file() { |
| 78 | - // Bail early if the SQLite3 class does not exist. | |
| 79 | - if ( ! class_exists( 'SQLite3' ) ) { | |
| 74 | + // Bail early if the pdo_sqlite extension is not loaded. | |
| 75 | + if ( ! extension_loaded( 'pdo_sqlite' ) ) { | |
| 80 | 76 | return; |
| 81 | 77 | } |
| 82 | 78 | |
| 83 | 79 | $destination = WP_CONTENT_DIR . '/db.php'; |
| 80 | + | |
| 81 | + /* | |
| 82 | + * When an existing "db.php" drop-in is detected, let's check if it's a known | |
| 83 | + * plugin that we can continue supporting even when we override the drop-in. | |
| 84 | + */ | |
| 85 | + $override_db_dropin = false; | |
| 86 | + if ( file_exists( $destination ) ) { | |
| 87 | + // Check for the Query Monitor plugin. | |
| 88 | + // When "QM_DB" exists, it must have been loaded via the "db.php" file. | |
| 89 | + if ( class_exists( 'QM_DB', false ) ) { | |
| 90 | + $override_db_dropin = true; | |
| 91 | + } | |
| 92 | + | |
| 93 | + if ( $override_db_dropin ) { | |
| 94 | + require_once ABSPATH . '/wp-admin/includes/file.php'; | |
| 95 | + global $wp_filesystem; | |
| 96 | + if ( ! $wp_filesystem ) { | |
| 97 | + WP_Filesystem(); | |
| 98 | + } | |
| 99 | + $wp_filesystem->delete( $destination ); | |
| 100 | + } | |
| 101 | + } | |
| 84 | 102 | |
| 85 | 103 | // Place database drop-in if not present yet, except in case there is |
| 86 | 104 | // another database drop-in present already. |
| 87 | 105 | if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) && ! file_exists( $destination ) ) { |