Plugin Directory

source: sqlite-database-integration/trunk/admin-page.php

Last change on this file was 3334261, checked in by janjakes, 8 months ago

v2.2.4

  • Property svn:eol-style set to native
File size: 7.1 KB
Line 
1<?php
2/**
3 * Functions for the admin page of the plugin.
4 *
5 * @since 1.0.0
6 * @package wp-sqlite-integration
7 */
8
9/**
10 * Add an admin menu page.
11 *
12 * @since 1.0.0
13 */
14function sqlite_add_admin_menu() {
15        add_options_page(
16                __( 'SQLite integration', 'sqlite-database-integration' ),
17                __( 'SQLite integration', 'sqlite-database-integration' ),
18                'manage_options',
19                'sqlite-integration',
20                'sqlite_integration_admin_screen'
21        );
22}
23add_action( 'admin_menu', 'sqlite_add_admin_menu' );
24
25/**
26 * The admin page contents.
27 */
28function sqlite_integration_admin_screen() {
29        $db_dropin_path = WP_CONTENT_DIR . '/db.php';
30
31        /*
32         * When an existing "db.php" drop-in is detected, let's check if it's a known
33         * plugin that we can continue supporting even when we override the drop-in.
34         */
35        $override_db_dropin = false;
36        if ( file_exists( $db_dropin_path ) && ! defined( 'SQLITE_DB_DROPIN_VERSION' ) ) {
37                // Check for the Query Monitor plugin.
38                // When "QM_DB" exists, it must have been loaded via the "db.php" file.
39                if ( class_exists( 'QM_DB', false ) ) {
40                        $override_db_dropin = true;
41                }
42        }
43
44        ?>
45        <div class="wrap">
46                <h1><?php esc_html_e( 'SQLite integration.', 'sqlite-database-integration' ); ?></h1>
47        </div>
48        <!-- Set the wrapper width to 50em, to improve readability. -->
49        <div style="max-width:50em;">
50                <?php if ( defined( 'SQLITE_DB_DROPIN_VERSION' ) ) : ?>
51                        <div class="notice notice-success">
52                                <p><?php esc_html_e( 'SQLite is enabled.', 'sqlite-database-integration' ); ?></p>
53                        </div>
54                        <p>
55                                <?php
56                                        printf(
57                                                /* translators: 1: Admin URL to deactivate the module, 2: db.php drop-in path, */
58                                                __( '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' ) ),
60                                                '<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>'
61                                        );
62                                ?>
63                        </p>
64                <?php elseif ( ! extension_loaded( 'pdo_sqlite' ) ) : ?>
65                        <div class="notice notice-error">
66                                <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                        </div>
68                <?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' ) ); ?>">
82                                        <?php
83                                        printf(
84                                                /* translators: %s: db.php drop-in path */
85                                                esc_html__( 'Update %s file', 'sqlite-database-integration' ),
86                                                '<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>'
87                                        );
88                                        ?>
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; ?>
103                <?php elseif ( ! is_writable( WP_CONTENT_DIR ) ) : ?>
104                        <div class="notice notice-error">
105                                <p>
106                                        <?php
107                                        printf(
108                                                /* translators: %s: db.php drop-in path */
109                                                esc_html__( 'The SQLite plugin cannot be activated because the %s directory is not writable.', 'sqlite-database-integration' ),
110                                                '<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '</code>'
111                                        );
112                                        ?>
113                                </p>
114                        </div>
115                <?php else : ?>
116                        <div class="notice notice-success">
117                                <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>
118                        </div>
119                        <h2><?php esc_html_e( 'Important note', 'sqlite-database-integration' ); ?></h2>
120                        <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>
121
122                        <?php if ( $override_db_dropin ) : ?>
123                                <p>
124                                        <strong>NOTE:</strong>
125                                        <?php
126                                        printf(
127                                                /* translators: %s: db.php drop-in path */
128                                                esc_html__( 'We’ve detected an existing database drop-in file at %s, created by the Query Monitor plugin.', 'sqlite-database-integration' ),
129                                                '<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>'
130                                        );
131                                        ?>
132                                        <?php esc_html_e( 'To enable SQLite support, this file will need to be replaced.', 'sqlite-database-integration' ); ?>
133                                        <?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' ); ?>
134                                </p>
135                        <?php endif; ?>
136
137                        <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
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>
140                <?php endif; ?>
141        </div>
142        <?php
143}
144
145/**
146 * Adds a link to the admin bar.
147 *
148 * @since n.e.x.t
149 *
150 * @global wpdb $wpdb WordPress database abstraction object.
151 *
152 * @param WP_Admin_Bar $admin_bar The admin bar object.
153 */
154function sqlite_plugin_adminbar_item( $admin_bar ) {
155        global $wpdb;
156
157        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>';
160        } elseif ( stripos( $wpdb->db_server_info(), 'maria' ) !== false ) {
161                $title = '<span style="color:#DC3232;">' . __( 'Database: MariaDB', 'sqlite-database-integration' ) . '</span>';
162        } else {
163                $title = '<span style="color:#DC3232;">' . __( 'Database: MySQL', 'sqlite-database-integration' ) . '</span>';
164        }
165
166        $args = array(
167                'id'     => 'sqlite-db-integration',
168                'parent' => 'top-secondary',
169                'title'  => $title,
170                'href'   => esc_url( admin_url( 'options-general.php?page=sqlite-integration' ) ),
171                'meta'   => false,
172        );
173        $admin_bar->add_node( $args );
174}
175add_action( 'admin_bar_menu', 'sqlite_plugin_adminbar_item', 999 );
Note: See TracBrowser for help on using the repository browser.