|
61 | 61 | * that are present in the GitHub repository |
62 | 62 | * but not the plugin published on WordPress.org. |
63 | 63 | */ |
| 64 | +global $wpdb; |
64 | 65 | $crosscheck_tests_file_path = dirname( __DIR__, 2 ) . '/tests/class-wp-sqlite-crosscheck-db.php'; |
65 | 66 | if ( defined( 'SQLITE_DEBUG_CROSSCHECK' ) && SQLITE_DEBUG_CROSSCHECK && file_exists( $crosscheck_tests_file_path ) ) { |
66 | 67 | require_once $crosscheck_tests_file_path; |
67 | | - $GLOBALS['wpdb'] = new WP_SQLite_Crosscheck_DB( DB_NAME ); |
| 68 | + $wpdb = new WP_SQLite_Crosscheck_DB( DB_NAME ); |
68 | 69 | } else { |
69 | | - $GLOBALS['wpdb'] = new WP_SQLite_DB( defined( 'DB_NAME' ) ? DB_NAME : '' ); |
| 70 | + // Allow plugins to wrap the wpdb object. |
| 71 | + $wpdb = new WP_SQLite_DB( defined( 'DB_NAME' ) ? DB_NAME : '' ); |
| 72 | + |
| 73 | + /* |
| 74 | + * Query Monitor integration: |
| 75 | + * |
| 76 | + * When the Query Monitor plugin exists in its standard location, let's check |
| 77 | + * if it is active, so we can load it eagerly. This is a workaround to avoid |
| 78 | + * SQLite and Query Monitor competing for the "wp-content/db.php" file. |
| 79 | + */ |
| 80 | + global $table_prefix; |
| 81 | + $plugins_dir = defined( 'WP_PLUGIN_DIR' ) ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins'; |
| 82 | + $qm_db_file = $plugins_dir . '/query-monitor/wp-content/db.php'; |
| 83 | + if ( isset( $table_prefix ) && file_exists( $qm_db_file ) ) { |
| 84 | + // Check if Query Monitor is active. |
| 85 | + $wpdb->set_prefix( $table_prefix ); |
| 86 | + $value = $wpdb->get_row( |
| 87 | + $wpdb->prepare( |
| 88 | + "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", |
| 89 | + 'active_plugins' |
| 90 | + ) |
| 91 | + ); |
| 92 | + |
| 93 | + $query_monitor_active = in_array( |
| 94 | + 'query-monitor/query-monitor.php', |
| 95 | + unserialize( $value->option_value ), |
| 96 | + true |
| 97 | + ); |
| 98 | + |
| 99 | + if ( $query_monitor_active ) { |
| 100 | + // Load Query Monitor's "db.php" file. This overrides the global $wpdb |
| 101 | + // object, so we need to back it up before and restore after loading. |
| 102 | + $_wpdb = $wpdb; |
| 103 | + require_once $qm_db_file; |
| 104 | + $wpdb = $_wpdb; |
| 105 | + |
| 106 | + // Remove Query Monitor's deactivation hook so that it doesn't delete |
| 107 | + // the "db.php" file. Currently, it attempts so when QM_DB is defined. |
| 108 | + add_action( |
| 109 | + 'deactivate_query-monitor/query-monitor.php', |
| 110 | + function () { |
| 111 | + remove_all_actions( 'deactivate_query-monitor/query-monitor.php' ); |
| 112 | + }, |
| 113 | + 0 |
| 114 | + ); |
| 115 | + } |
| 116 | + } |
70 | 117 | } |
0 commit comments