| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions for the admin page of the plugin. |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Add an admin menu page. |
| 8 |
*/ |
| 9 |
function sqlite_add_admin_menu() { |
| 10 |
$parent_slug = is_multisite() ? 'settings.php' : 'options-general.php'; |
| 11 |
|
| 12 |
add_submenu_page( |
| 13 |
$parent_slug, |
| 14 |
__( 'SQLite integration', 'sqlite-database-integration' ), |
| 15 |
__( 'SQLite integration', 'sqlite-database-integration' ), |
| 16 |
sqlite_plugin_get_manage_capability(), |
| 17 |
'sqlite-integration', |
| 18 |
'sqlite_integration_admin_screen' |
| 19 |
); |
| 20 |
} |
| 21 |
add_action( is_multisite() ? 'network_admin_menu' : 'admin_menu', 'sqlite_add_admin_menu' ); |
| 22 |
|
| 23 |
/** |
| 24 |
* The admin page contents. |
| 25 |
*/ |
| 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 |
|
| 49 |
?> |
| 50 |
<div class="wrap"> |
| 51 |
<h1><?php esc_html_e( 'SQLite integration.', 'sqlite-database-integration' ); ?></h1> |
| 52 |
</div> |
| 53 |
<!-- Set the wrapper width to 50em, to improve readability. --> |
| 54 |
<div style="max-width:50em;"> |
| 55 |
<?php if ( defined( 'SQLITE_DB_DROPIN_VERSION' ) ) : ?> |
| 56 |
<div class="notice notice-success"> |
| 57 |
<p><?php esc_html_e( 'SQLite is enabled.', 'sqlite-database-integration' ); ?></p> |
| 58 |
</div> |
| 59 |
<p> |
| 60 |
<?php |
| 61 |
printf( |
| 62 |
/* translators: 1: Admin URL to deactivate the module, 2: db.php drop-in path, */ |
| 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' ), |
| 64 |
esc_url( self_admin_url( 'plugins.php' ) ), |
| 65 |
'<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>' |
| 66 |
); |
| 67 |
?> |
| 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> |
| 97 |
<?php else : ?> |
| 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> |
| 103 |
|
| 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> |
| 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> |
| 122 |
<?php endif; ?> |
| 123 |
</div> |
| 124 |
<?php |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Adds a link to the admin bar. |
| 129 |
* |
| 130 |
* @global wpdb $wpdb WordPress database abstraction object. |
| 131 |
* |
| 132 |
* @param WP_Admin_Bar $admin_bar The admin bar object. |
| 133 |
*/ |
| 134 |
function sqlite_plugin_adminbar_item( $admin_bar ) { |
| 135 |
if ( ! current_user_can( sqlite_plugin_get_manage_capability() ) ) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
global $wpdb; |
| 140 |
|
| 141 |
if ( defined( 'SQLITE_DB_DROPIN_VERSION' ) && defined( 'DB_ENGINE' ) && 'sqlite' === DB_ENGINE ) { |
| 142 |
$title = '<span style="color:#46B450;">' . __( 'Database: SQLite', 'sqlite-database-integration' ) . '</span>'; |
| 143 |
} elseif ( stripos( $wpdb->db_server_info(), 'maria' ) !== false ) { |
| 144 |
$title = '<span style="color:#DC3232;">' . __( 'Database: MariaDB', 'sqlite-database-integration' ) . '</span>'; |
| 145 |
} else { |
| 146 |
$title = '<span style="color:#DC3232;">' . __( 'Database: MySQL', 'sqlite-database-integration' ) . '</span>'; |
| 147 |
} |
| 148 |
|
| 149 |
$args = array( |
| 150 |
'id' => 'sqlite-db-integration', |
| 151 |
'parent' => 'top-secondary', |
| 152 |
'title' => $title, |
| 153 |
'href' => esc_url( sqlite_plugin_get_admin_page_url() ), |
| 154 |
'meta' => false, |
| 155 |
); |
| 156 |
$admin_bar->add_node( $args ); |
| 157 |
} |
| 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 |
} |
| 173 |
|