PluginProbe
SQLite Database Integration / trunk
SQLite Database Integration vtrunk
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 trunk, at activate.php

131 lines 3.4 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
6 /**
7 * Redirect to the plugin's admin screen on activation.
8 *
9 * @param string $plugin The plugin basename.
10 */
11 function sqlite_plugin_activation_redirect( $plugin ) {
12 if (
13 plugin_basename( SQLITE_MAIN_FILE ) !== $plugin
14 || ! current_user_can( sqlite_plugin_get_manage_capability() )
15 ) {
16 return;
17 }
18
19 if ( wp_safe_redirect( sqlite_plugin_get_admin_page_url() ) ) {
20 exit;
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 * the user has permission to manage the database drop-in, 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 function sqlite_activation() {
33 if (
34 ! isset( $_GET['page'], $_GET['confirm-install'] )
35 || 'sqlite-integration' !== $_GET['page']
36 ) {
37 return;
38 }
39
40 if ( ! current_user_can( sqlite_plugin_get_manage_capability() ) ) {
41 wp_die(
42 esc_html__( 'Sorry, you are not allowed to install the SQLite database drop-in.', 'sqlite-database-integration' ),
43 403
44 );
45 }
46
47 check_admin_referer( 'sqlite-install' );
48
49 sqlite_plugin_copy_db_file();
50
51 // WordPress will automatically redirect to the install screen here.
52 wp_redirect( admin_url() );
53 exit;
54 }
55 add_action( 'admin_init', 'sqlite_activation' );
56
57 // Flush the cache at the last moment before the redirect.
58 add_filter(
59 'x_redirect_by',
60 function ( $result ) {
61 wp_cache_flush();
62 return $result;
63 },
64 PHP_INT_MAX,
65 1
66 );
67
68 /**
69 * Add the db.php file in wp-content.
70 *
71 * When the plugin gets merged in wp-core, this is not to be ported.
72 */
73 function sqlite_plugin_copy_db_file() {
74 // Bail early if the pdo_sqlite extension is not loaded.
75 if ( ! extension_loaded( 'pdo_sqlite' ) ) {
76 return;
77 }
78
79 $destination = WP_CONTENT_DIR . '/db.php';
80
81 /*
82 * When an existing "db.php" drop-in is detected, let's check if it's a known
83 * plugin that we can continue supporting even when we override the drop-in.
84 */
85 $override_db_dropin = false;
86 if ( file_exists( $destination ) ) {
87 // Check for the Query Monitor plugin.
88 // When "QM_DB" exists, it must have been loaded via the "db.php" file.
89 if ( class_exists( 'QM_DB', false ) ) {
90 $override_db_dropin = true;
91 }
92
93 if ( $override_db_dropin ) {
94 require_once ABSPATH . '/wp-admin/includes/file.php';
95 global $wp_filesystem;
96 if ( ! $wp_filesystem ) {
97 WP_Filesystem();
98 }
99 $wp_filesystem->delete( $destination );
100 }
101 }
102
103 // Place database drop-in if not present yet, except in case there is
104 // another database drop-in present already.
105 if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) && ! file_exists( $destination ) ) {
106 // Init the filesystem to allow copying the file.
107 global $wp_filesystem;
108
109 require_once ABSPATH . '/wp-admin/includes/file.php';
110
111 // Init the filesystem if needed, then copy the file, replacing contents as needed.
112 if ( ( $wp_filesystem || WP_Filesystem() ) && $wp_filesystem->touch( $destination ) ) {
113
114 // Get the db.copy.php file contents, replace placeholders and write it to the destination.
115 $file_contents = str_replace(
116 array(
117 '{SQLITE_IMPLEMENTATION_FOLDER_PATH}',
118 '{SQLITE_PLUGIN}',
119 ),
120 array(
121 __DIR__,
122 str_replace( WP_PLUGIN_DIR . '/', '', SQLITE_MAIN_FILE ),
123 ),
124 file_get_contents( __DIR__ . '/db.copy' )
125 );
126
127 $wp_filesystem->put_contents( $destination, $file_contents );
128 }
129 }
130 }
131