| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle the SQLite activation. |
| 4 |
* |
| 5 |
* @since 1.0.0 |
| 6 |
* @package wp-sqlite-integration |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Redirect to the plugin's admin screen on activation. |
| 11 |
* |
| 12 |
* @since 1.0.0 |
| 13 |
* |
| 14 |
* @param string $plugin The plugin basename. |
| 15 |
*/ |
| 16 |
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 |
} |
| 21 |
} |
| 22 |
} |
| 23 |
add_action( 'activated_plugin', 'sqlite_plugin_activation_redirect' ); |
| 24 |
|
| 25 |
/** |
| 26 |
* Check the URL to ensure we're on the plugin page, |
| 27 |
* the user has clicked the button to install SQLite, |
| 28 |
* and the nonce is valid. |
| 29 |
* If the above conditions are met, run the sqlite_plugin_copy_db_file() function, |
| 30 |
* and redirect to the install screen. |
| 31 |
* |
| 32 |
* @since 1.0.0 |
| 33 |
*/ |
| 34 |
function sqlite_activation() { |
| 35 |
global $current_screen; |
| 36 |
if ( isset( $current_screen->base ) && 'settings_page_sqlite-integration' === $current_screen->base ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
if ( isset( $_GET['confirm-install'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'sqlite-install' ) ) { |
| 40 |
|
| 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; |
| 57 |
} |
| 58 |
} |
| 59 |
add_action( 'admin_init', 'sqlite_activation' ); |
| 60 |
|
| 61 |
// Flush the cache at the last moment before the redirect. |
| 62 |
add_filter( |
| 63 |
'x_redirect_by', |
| 64 |
function ( $result ) { |
| 65 |
wp_cache_flush(); |
| 66 |
return $result; |
| 67 |
}, |
| 68 |
PHP_INT_MAX, |
| 69 |
1 |
| 70 |
); |
| 71 |
|
| 72 |
/** |
| 73 |
* Add the db.php file in wp-content. |
| 74 |
* |
| 75 |
* When the plugin gets merged in wp-core, this is not to be ported. |
| 76 |
*/ |
| 77 |
function sqlite_plugin_copy_db_file() { |
| 78 |
// Bail early if the PDO SQLite extension is not loaded. |
| 79 |
if ( ! extension_loaded( 'pdo_sqlite' ) ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
$destination = WP_CONTENT_DIR . '/db.php'; |
| 84 |
|
| 85 |
// Place database drop-in if not present yet, except in case there is |
| 86 |
// another database drop-in present already. |
| 87 |
if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) && ! file_exists( $destination ) ) { |
| 88 |
// Init the filesystem to allow copying the file. |
| 89 |
global $wp_filesystem; |
| 90 |
|
| 91 |
require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 92 |
|
| 93 |
// Init the filesystem if needed, then copy the file, replacing contents as needed. |
| 94 |
if ( ( $wp_filesystem || WP_Filesystem() ) && $wp_filesystem->touch( $destination ) ) { |
| 95 |
|
| 96 |
// Get the db.copy.php file contents, replace placeholders and write it to the destination. |
| 97 |
$file_contents = str_replace( |
| 98 |
array( |
| 99 |
'{SQLITE_IMPLEMENTATION_FOLDER_PATH}', |
| 100 |
'{SQLITE_PLUGIN}', |
| 101 |
), |
| 102 |
array( |
| 103 |
__DIR__, |
| 104 |
str_replace( WP_PLUGIN_DIR . '/', '', SQLITE_MAIN_FILE ), |
| 105 |
), |
| 106 |
file_get_contents( __DIR__ . '/db.copy' ) |
| 107 |
); |
| 108 |
|
| 109 |
$wp_filesystem->put_contents( $destination, $file_contents ); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|