PluginProbe
SQLite Database Integration / 2.2.7
SQLite Database Integration v2.2.7
3.0.1 trunk 2.1.13 2.1.14 2.1.15 2.1.16 2.2.0 2.2.1 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.2.2 2.2.20 2.2.21 2.2.22 2.2.23 2.2.3 2.2.4 All 31 releases
sqlite-database-integration / activate.php

activate.php in SQLite Database Integration 2.2.7, at activate.php

135 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /*
86 * When an existing "db.php" drop-in is detected, let's check if it's a known
87 * plugin that we can continue supporting even when we override the drop-in.
88 */
89 $override_db_dropin = false;
90 if ( file_exists( $destination ) ) {
91 // Check for the Query Monitor plugin.
92 // When "QM_DB" exists, it must have been loaded via the "db.php" file.
93 if ( class_exists( 'QM_DB', false ) ) {
94 $override_db_dropin = true;
95 }
96
97 if ( $override_db_dropin ) {
98 require_once ABSPATH . '/wp-admin/includes/file.php';
99 global $wp_filesystem;
100 if ( ! $wp_filesystem ) {
101 WP_Filesystem();
102 }
103 $wp_filesystem->delete( $destination );
104 }
105 }
106
107 // Place database drop-in if not present yet, except in case there is
108 // another database drop-in present already.
109 if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) && ! file_exists( $destination ) ) {
110 // Init the filesystem to allow copying the file.
111 global $wp_filesystem;
112
113 require_once ABSPATH . '/wp-admin/includes/file.php';
114
115 // Init the filesystem if needed, then copy the file, replacing contents as needed.
116 if ( ( $wp_filesystem || WP_Filesystem() ) && $wp_filesystem->touch( $destination ) ) {
117
118 // Get the db.copy.php file contents, replace placeholders and write it to the destination.
119 $file_contents = str_replace(
120 array(
121 '{SQLITE_IMPLEMENTATION_FOLDER_PATH}',
122 '{SQLITE_PLUGIN}',
123 ),
124 array(
125 __DIR__,
126 str_replace( WP_PLUGIN_DIR . '/', '', SQLITE_MAIN_FILE ),
127 ),
128 file_get_contents( __DIR__ . '/db.copy' )
129 );
130
131 $wp_filesystem->put_contents( $destination, $file_contents );
132 }
133 }
134 }
135