| @@ -1,32 +1,37 @@ | ||
| 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 | + | |
| 29 | 34 | $db_dropin_path = WP_CONTENT_DIR . '/db.php'; |
| 30 | 35 | |
| 31 | 36 | /* |
| 32 | 37 | * When an existing "db.php" drop-in is detected, let's check if it's a known |
| @@ -55,9 +60,9 @@ | ||
| 55 | 60 | <?php |
| 56 | 61 | printf( |
| 57 | 62 | /* translators: 1: Admin URL to deactivate the module, 2: db.php drop-in path, */ |
| 58 | 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' ), |
| 59 | - esc_url( admin_url( 'plugins.php' ) ), | |
| 64 | + esc_url( self_admin_url( 'plugins.php' ) ), | |
| 60 | 65 | '<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>' |
| 61 | 66 | ); |
| 62 | 67 | ?> |
| 63 | 68 | </p> |
| @@ -65,42 +70,19 @@ | ||
| 65 | 70 | <div class="notice notice-error"> |
| 66 | 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> |
| 67 | 72 | </div> |
| 68 | 73 | <?php elseif ( file_exists( $db_dropin_path ) && ! defined( 'SQLITE_DB_DROPIN_VERSION' ) && ! $override_db_dropin ) : ?> |
| 69 | - <?php if ( defined( 'PERFLAB_SQLITE_DB_DROPIN_VERSION' ) ) : ?> | |
| 70 | - <div class="notice notice-warning"> | |
| 71 | - <p> | |
| 72 | - <?php | |
| 73 | - printf( | |
| 74 | - /* translators: %s: db.php drop-in path */ | |
| 75 | - esc_html__( 'An older %s file was detected. Please click the button below to update the file.', 'sqlite-database-integration' ), | |
| 76 | - '<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>' | |
| 77 | - ); | |
| 78 | - ?> | |
| 79 | - </p> | |
| 80 | - </div> | |
| 81 | - <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' ) ); ?>"> | |
| 74 | + <div class="notice notice-error"> | |
| 75 | + <p> | |
| 82 | 76 | <?php |
| 83 | 77 | printf( |
| 84 | 78 | /* translators: %s: db.php drop-in path */ |
| 85 | - esc_html__( 'Update %s file', 'sqlite-database-integration' ), | |
| 79 | + esc_html__( 'The SQLite plugin cannot be activated because a different %s drop-in already exists.', 'sqlite-database-integration' ), | |
| 86 | 80 | '<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>' |
| 87 | 81 | ); |
| 88 | 82 | ?> |
| 89 | - </a> | |
| 90 | - <?php else : ?> | |
| 91 | - <div class="notice notice-error"> | |
| 92 | - <p> | |
| 93 | - <?php | |
| 94 | - printf( | |
| 95 | - /* translators: %s: db.php drop-in path */ | |
| 96 | - esc_html__( 'The SQLite plugin cannot be activated because a different %s drop-in already exists.', 'sqlite-database-integration' ), | |
| 97 | - '<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>' | |
| 98 | - ); | |
| 99 | - ?> | |
| 100 | - </p> | |
| 101 | - </div> | |
| 102 | - <?php endif; ?> | |
| 83 | + </p> | |
| 84 | + </div> | |
| 103 | 85 | <?php elseif ( ! is_writable( WP_CONTENT_DIR ) ) : ?> |
| 104 | 86 | <div class="notice notice-error"> |
| 105 | 87 | <p> |
| 106 | 88 | <?php |
| @@ -135,9 +117,9 @@ | ||
| 135 | 117 | <?php endif; ?> |
| 136 | 118 | |
| 137 | 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> |
| 138 | 120 | |
| 139 | - <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> | |
| 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> | |
| 140 | 122 | <?php endif; ?> |
| 141 | 123 | </div> |
| 142 | 124 | <?php |
| 143 | 125 | } |
| @@ -144,20 +126,21 @@ | ||
| 144 | 126 | |
| 145 | 127 | /** |
| 146 | 128 | * Adds a link to the admin bar. |
| 147 | 129 | * |
| 148 | - * @since n.e.x.t | |
| 149 | - * | |
| 150 | 130 | * @global wpdb $wpdb WordPress database abstraction object. |
| 151 | 131 | * |
| 152 | 132 | * @param WP_Admin_Bar $admin_bar The admin bar object. |
| 153 | 133 | */ |
| 154 | 134 | function sqlite_plugin_adminbar_item( $admin_bar ) { |
| 135 | + if ( ! current_user_can( sqlite_plugin_get_manage_capability() ) ) { | |
| 136 | + return; | |
| 137 | + } | |
| 138 | + | |
| 155 | 139 | global $wpdb; |
| 156 | 140 | |
| 157 | 141 | if ( defined( 'SQLITE_DB_DROPIN_VERSION' ) && defined( 'DB_ENGINE' ) && 'sqlite' === DB_ENGINE ) { |
| 158 | - $suffix = defined( 'WP_SQLITE_AST_DRIVER' ) && WP_SQLITE_AST_DRIVER ? ' (AST)' : ''; | |
| 159 | - $title = '<span style="color:#46B450;">' . __( 'Database: SQLite', 'sqlite-database-integration' ) . $suffix . '</span>'; | |
| 142 | + $title = '<span style="color:#46B450;">' . __( 'Database: SQLite', 'sqlite-database-integration' ) . '</span>'; | |
| 160 | 143 | } elseif ( stripos( $wpdb->db_server_info(), 'maria' ) !== false ) { |
| 161 | 144 | $title = '<span style="color:#DC3232;">' . __( 'Database: MariaDB', 'sqlite-database-integration' ) . '</span>'; |
| 162 | 145 | } else { |
| 163 | 146 | $title = '<span style="color:#DC3232;">' . __( 'Database: MySQL', 'sqlite-database-integration' ) . '</span>'; |
| @@ -166,10 +149,24 @@ | ||
| 166 | 149 | $args = array( |
| 167 | 150 | 'id' => 'sqlite-db-integration', |
| 168 | 151 | 'parent' => 'top-secondary', |
| 169 | 152 | 'title' => $title, |
| 170 | - 'href' => esc_url( admin_url( 'options-general.php?page=sqlite-integration' ) ), | |
| 153 | + 'href' => esc_url( sqlite_plugin_get_admin_page_url() ), | |
| 171 | 154 | 'meta' => false, |
| 172 | 155 | ); |
| 173 | 156 | $admin_bar->add_node( $args ); |
| 174 | 157 | } |
| 175 | 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 | +} | |