| @@ -1,32 +1,52 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Functions for the admin page of the plugin. |
| 4 | - * | |
| 5 | - * @since 1.0.0 | |
| 6 | - * @package wp-sqlite-integration | |
| 7 | 4 | */ |
| 8 | 5 | |
| 9 | 6 | /** |
| 10 | 7 | * Add an admin menu page. |
| 11 | - * | |
| 12 | - * @since 1.0.0 | |
| 13 | 8 | */ |
| 14 | 9 | function sqlite_add_admin_menu() { |
| 15 | - add_options_page( | |
| 10 | + $parent_slug = is_multisite() ? 'settings.php' : 'options-general.php'; | |
| 11 | + | |
| 12 | + add_submenu_page( | |
| 13 | + $parent_slug, | |
| 16 | 14 | __( 'SQLite integration', 'sqlite-database-integration' ), |
| 17 | 15 | __( 'SQLite integration', 'sqlite-database-integration' ), |
| 18 | - 'manage_options', | |
| 16 | + sqlite_plugin_get_manage_capability(), | |
| 19 | 17 | 'sqlite-integration', |
| 20 | 18 | 'sqlite_integration_admin_screen' |
| 21 | 19 | ); |
| 22 | 20 | } |
| 23 | -add_action( 'admin_menu', 'sqlite_add_admin_menu' ); | |
| 21 | +add_action( is_multisite() ? 'network_admin_menu' : 'admin_menu', 'sqlite_add_admin_menu' ); | |
| 24 | 22 | |
| 25 | 23 | /** |
| 26 | 24 | * The admin page contents. |
| 27 | 25 | */ |
| 28 | 26 | function sqlite_integration_admin_screen() { |
| 27 | + if ( ! current_user_can( sqlite_plugin_get_manage_capability() ) ) { | |
| 28 | + wp_die( | |
| 29 | + esc_html__( 'Sorry, you are not allowed to access the SQLite integration settings.', 'sqlite-database-integration' ), | |
| 30 | + 403 | |
| 31 | + ); | |
| 32 | + } | |
| 33 | + | |
| 34 | + $db_dropin_path = WP_CONTENT_DIR . '/db.php'; | |
| 35 | + | |
| 36 | + /* | |
| 37 | + * When an existing "db.php" drop-in is detected, let's check if it's a known | |
| 38 | + * plugin that we can continue supporting even when we override the drop-in. | |
| 39 | + */ | |
| 40 | + $override_db_dropin = false; | |
| 41 | + if ( file_exists( $db_dropin_path ) && ! defined( 'SQLITE_DB_DROPIN_VERSION' ) ) { | |
| 42 | + // Check for the Query Monitor plugin. | |
| 43 | + // When "QM_DB" exists, it must have been loaded via the "db.php" file. | |
| 44 | + if ( class_exists( 'QM_DB', false ) ) { | |
| 45 | + $override_db_dropin = true; | |
| 46 | + } | |
| 47 | + } | |
| 48 | + | |
| 29 | 49 | ?> |
| 30 | 50 | <div class="wrap"> |
| 31 | 51 | <h1><?php esc_html_e( 'SQLite integration.', 'sqlite-database-integration' ); ?></h1> |
| 32 | 52 | </div> |
| @@ -40,79 +60,66 @@ | ||
| 40 | 60 | <?php |
| 41 | 61 | printf( |
| 42 | 62 | /* translators: 1: Admin URL to deactivate the module, 2: db.php drop-in path, */ |
| 43 | 63 | __( 'The SQLite drop-in is enabled. To disable it and get back to your previous, MySQL database, you can <a href="%1$s">deactivate the plugin</a>. Alternatively, you can manually delete the %2$s file from your server.', 'sqlite-database-integration' ), |
| 44 | - esc_url( admin_url( 'plugins.php' ) ), | |
| 64 | + esc_url( self_admin_url( 'plugins.php' ) ), | |
| 45 | 65 | '<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>' |
| 46 | 66 | ); |
| 47 | 67 | ?> |
| 48 | 68 | </p> |
| 69 | + <?php elseif ( ! extension_loaded( 'pdo_sqlite' ) ) : ?> | |
| 70 | + <div class="notice notice-error"> | |
| 71 | + <p><?php esc_html_e( 'We detected that the PDO SQLite driver is missing from your server (the pdo_sqlite extension is not loaded). Please make sure that SQLite is enabled in your PHP installation before proceeding.', 'sqlite-database-integration' ); ?></p> | |
| 72 | + </div> | |
| 73 | + <?php elseif ( file_exists( $db_dropin_path ) && ! defined( 'SQLITE_DB_DROPIN_VERSION' ) && ! $override_db_dropin ) : ?> | |
| 74 | + <div class="notice notice-error"> | |
| 75 | + <p> | |
| 76 | + <?php | |
| 77 | + printf( | |
| 78 | + /* translators: %s: db.php drop-in path */ | |
| 79 | + esc_html__( 'The SQLite plugin cannot be activated because a different %s drop-in already exists.', 'sqlite-database-integration' ), | |
| 80 | + '<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>' | |
| 81 | + ); | |
| 82 | + ?> | |
| 83 | + </p> | |
| 84 | + </div> | |
| 85 | + <?php elseif ( ! is_writable( WP_CONTENT_DIR ) ) : ?> | |
| 86 | + <div class="notice notice-error"> | |
| 87 | + <p> | |
| 88 | + <?php | |
| 89 | + printf( | |
| 90 | + /* translators: %s: db.php drop-in path */ | |
| 91 | + esc_html__( 'The SQLite plugin cannot be activated because the %s directory is not writable.', 'sqlite-database-integration' ), | |
| 92 | + '<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '</code>' | |
| 93 | + ); | |
| 94 | + ?> | |
| 95 | + </p> | |
| 96 | + </div> | |
| 49 | 97 | <?php else : ?> |
| 50 | - <?php if ( ! class_exists( 'SQLite3' ) ) : ?> | |
| 51 | - <div class="notice notice-error"> | |
| 52 | - <p><?php esc_html_e( 'We detected that the SQLite3 class is missing from your server. Please make sure that SQLite is enabled in your PHP installation before proceeding.', 'sqlite-database-integration' ); ?></p> | |
| 53 | - </div> | |
| 54 | - <?php elseif ( ! extension_loaded( 'pdo_sqlite' ) ) : ?> | |
| 55 | - <div class="notice notice-error"> | |
| 56 | - <p><?php esc_html_e( 'We detected that the PDO SQLite driver is missing from your server (the pdo_sqlite extension is not loaded). Please make sure that SQLite is enabled in your PHP installation before proceeding.', 'sqlite-database-integration' ); ?></p> | |
| 57 | - </div> | |
| 58 | - <?php elseif ( file_exists( WP_CONTENT_DIR . '/db.php' ) && ! defined( 'SQLITE_DB_DROPIN_VERSION' ) ) : ?> | |
| 59 | - <?php if ( defined( 'PERFLAB_SQLITE_DB_DROPIN_VERSION' ) ) : ?> | |
| 60 | - <div class="notice notice-warning"> | |
| 61 | - <p> | |
| 62 | - <?php | |
| 63 | - printf( | |
| 64 | - /* translators: %s: db.php drop-in path */ | |
| 65 | - esc_html__( 'An older %s file was detected. Please click the button below to update the file.', 'sqlite-database-integration' ), | |
| 66 | - '<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>' | |
| 67 | - ); | |
| 68 | - ?> | |
| 69 | - </p> | |
| 70 | - </div> | |
| 71 | - <a class="button button-primary" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=sqlite-integration&confirm-install&upgrade-from-pl' ), 'sqlite-install' ) ); ?>"> | |
| 72 | - <?php | |
| 73 | - printf( | |
| 74 | - /* translators: %s: db.php drop-in path */ | |
| 75 | - esc_html__( 'Update %s file', 'sqlite-database-integration' ), | |
| 76 | - '<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>' | |
| 77 | - ); | |
| 78 | - ?> | |
| 79 | - </a> | |
| 80 | - <?php else : ?> | |
| 81 | - <div class="notice notice-error"> | |
| 82 | - <p> | |
| 83 | - <?php | |
| 84 | - printf( | |
| 85 | - /* translators: %s: db.php drop-in path */ | |
| 86 | - esc_html__( 'The SQLite plugin cannot be activated because a different %s drop-in already exists.', 'sqlite-database-integration' ), | |
| 87 | - '<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>' | |
| 88 | - ); | |
| 89 | - ?> | |
| 90 | - </p> | |
| 91 | - </div> | |
| 92 | - <?php endif; ?> | |
| 93 | - <?php elseif ( ! is_writable( WP_CONTENT_DIR ) ) : ?> | |
| 94 | - <div class="notice notice-error"> | |
| 95 | - <p> | |
| 96 | - <?php | |
| 97 | - printf( | |
| 98 | - /* translators: %s: db.php drop-in path */ | |
| 99 | - esc_html__( 'The SQLite plugin cannot be activated because the %s directory is not writable.', 'sqlite-database-integration' ), | |
| 100 | - '<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '</code>' | |
| 101 | - ); | |
| 102 | - ?> | |
| 103 | - </p> | |
| 104 | - </div> | |
| 105 | - <?php else : ?> | |
| 106 | - <div class="notice notice-success"> | |
| 107 | - <p><?php esc_html_e( 'All checks completed successfully, your site can use an SQLite database. You can proceed with the installation.', 'sqlite-database-integration' ); ?></p> | |
| 108 | - </div> | |
| 109 | - <h2><?php esc_html_e( 'Important note', 'sqlite-database-integration' ); ?></h2> | |
| 110 | - <p><?php esc_html_e( 'This plugin will switch to a separate database and install WordPress in it. You will need to reconfigure your site, and start with a fresh site. Disabling the plugin you will get back to your previous MySQL database, with all your previous data intact.', 'sqlite-database-integration' ); ?></p> | |
| 111 | - <p><?php esc_html_e( 'By clicking the button below, you will be redirected to the WordPress installation screen to setup your new database', 'sqlite-database-integration' ); ?></p> | |
| 98 | + <div class="notice notice-success"> | |
| 99 | + <p><?php esc_html_e( 'All checks completed successfully, your site can use an SQLite database. You can proceed with the installation.', 'sqlite-database-integration' ); ?></p> | |
| 100 | + </div> | |
| 101 | + <h2><?php esc_html_e( 'Important note', 'sqlite-database-integration' ); ?></h2> | |
| 102 | + <p><?php esc_html_e( 'This plugin will switch to a separate database and install WordPress in it. You will need to reconfigure your site, and start with a fresh site. Disabling the plugin you will get back to your previous MySQL database, with all your previous data intact.', 'sqlite-database-integration' ); ?></p> | |
| 112 | 103 | |
| 113 | - <a class="button button-primary" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=sqlite-integration&confirm-install' ), 'sqlite-install' ) ); ?>"><?php esc_html_e( 'Install SQLite database', 'sqlite-database-integration' ); ?></a> | |
| 104 | + <?php if ( $override_db_dropin ) : ?> | |
| 105 | + <p> | |
| 106 | + <strong>NOTE:</strong> | |
| 107 | + <?php | |
| 108 | + printf( | |
| 109 | + /* translators: %s: db.php drop-in path */ | |
| 110 | + esc_html__( 'We’ve detected an existing database drop-in file at %s, created by the Query Monitor plugin.', 'sqlite-database-integration' ), | |
| 111 | + '<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>' | |
| 112 | + ); | |
| 113 | + ?> | |
| 114 | + <?php esc_html_e( 'To enable SQLite support, this file will need to be replaced.', 'sqlite-database-integration' ); ?> | |
| 115 | + <?php esc_html_e( 'The Query Monitor plugin will continue to function correctly after the change. You can safely proceed with the installation.', 'sqlite-database-integration' ); ?> | |
| 116 | + </p> | |
| 114 | 117 | <?php endif; ?> |
| 118 | + | |
| 119 | + <p><?php esc_html_e( 'By clicking the button below, you will be redirected to the WordPress installation screen to setup your new database', 'sqlite-database-integration' ); ?></p> | |
| 120 | + | |
| 121 | + <a class="button button-primary" href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'confirm-install', '1', sqlite_plugin_get_admin_page_url() ), 'sqlite-install' ) ); ?>"><?php esc_html_e( 'Install SQLite database', 'sqlite-database-integration' ); ?></a> | |
| 115 | 122 | <?php endif; ?> |
| 116 | 123 | </div> |
| 117 | 124 | <?php |
| 118 | 125 | } |
| @@ -119,15 +126,17 @@ | ||
| 119 | 126 | |
| 120 | 127 | /** |
| 121 | 128 | * Adds a link to the admin bar. |
| 122 | 129 | * |
| 123 | - * @since n.e.x.t | |
| 124 | - * | |
| 125 | 130 | * @global wpdb $wpdb WordPress database abstraction object. |
| 126 | 131 | * |
| 127 | 132 | * @param WP_Admin_Bar $admin_bar The admin bar object. |
| 128 | 133 | */ |
| 129 | 134 | function sqlite_plugin_adminbar_item( $admin_bar ) { |
| 135 | + if ( ! current_user_can( sqlite_plugin_get_manage_capability() ) ) { | |
| 136 | + return; | |
| 137 | + } | |
| 138 | + | |
| 130 | 139 | global $wpdb; |
| 131 | 140 | |
| 132 | 141 | if ( defined( 'SQLITE_DB_DROPIN_VERSION' ) && defined( 'DB_ENGINE' ) && 'sqlite' === DB_ENGINE ) { |
| 133 | 142 | $title = '<span style="color:#46B450;">' . __( 'Database: SQLite', 'sqlite-database-integration' ) . '</span>'; |
| @@ -140,10 +149,24 @@ | ||
| 140 | 149 | $args = array( |
| 141 | 150 | 'id' => 'sqlite-db-integration', |
| 142 | 151 | 'parent' => 'top-secondary', |
| 143 | 152 | 'title' => $title, |
| 144 | - 'href' => esc_url( admin_url( 'options-general.php?page=sqlite-integration' ) ), | |
| 153 | + 'href' => esc_url( sqlite_plugin_get_admin_page_url() ), | |
| 145 | 154 | 'meta' => false, |
| 146 | 155 | ); |
| 147 | 156 | $admin_bar->add_node( $args ); |
| 148 | 157 | } |
| 149 | 158 | add_action( 'admin_bar_menu', 'sqlite_plugin_adminbar_item', 999 ); |
| 159 | + | |
| 160 | +/** | |
| 161 | + * Get the SQLite integration admin page URL. | |
| 162 | + * | |
| 163 | + * @access private | |
| 164 | + * | |
| 165 | + * @return string Admin page URL. | |
| 166 | + */ | |
| 167 | +function sqlite_plugin_get_admin_page_url() { | |
| 168 | + if ( is_multisite() ) { | |
| 169 | + return network_admin_url( 'settings.php?page=sqlite-integration' ); | |
| 170 | + } | |
| 171 | + return admin_url( 'options-general.php?page=sqlite-integration' ); | |
| 172 | +} | |