| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle the SQLite deactivation. |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Delete the db.php file in wp-content. |
| 8 |
* |
| 9 |
* When the plugin gets merged in wp-core, this is not to be ported. |
| 10 |
* |
| 11 |
* @param bool $network_deactivating Whether the plugin is being deactivated network-wide. |
| 12 |
*/ |
| 13 |
function sqlite_plugin_remove_db_file( $network_deactivating = false ) { |
| 14 |
if ( ! sqlite_plugin_has_active_dropin() ) { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
// WP-CLI and other programmatic callers may deactivate without a current user. |
| 19 |
// In those cases, the caller is responsible for authorization. |
| 20 |
if ( is_user_logged_in() && ! current_user_can( sqlite_plugin_get_manage_capability() ) ) { |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
global $wp_filesystem; |
| 25 |
|
| 26 |
require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 27 |
|
| 28 |
// Init the filesystem if needed, then delete custom drop-in. |
| 29 |
if ( $wp_filesystem || WP_Filesystem() ) { |
| 30 |
// Flush any persistent cache. |
| 31 |
wp_cache_flush(); |
| 32 |
// Delete the drop-in. |
| 33 |
$wp_filesystem->delete( WP_CONTENT_DIR . '/db.php' ); |
| 34 |
// Flush the cache again to mitigate a possible race condition. |
| 35 |
wp_cache_flush(); |
| 36 |
} |
| 37 |
|
| 38 |
// Run an action on `shutdown`, to deactivate the option in the MySQL database. |
| 39 |
add_action( |
| 40 |
'shutdown', |
| 41 |
function () use ( $network_deactivating ) { |
| 42 |
global $table_prefix; |
| 43 |
|
| 44 |
// Get credentials for the MySQL database. |
| 45 |
$dbuser = defined( 'DB_USER' ) ? DB_USER : ''; |
| 46 |
$dbpassword = defined( 'DB_PASSWORD' ) ? DB_PASSWORD : ''; |
| 47 |
$dbname = defined( 'DB_NAME' ) ? DB_NAME : ''; |
| 48 |
$dbhost = defined( 'DB_HOST' ) ? DB_HOST : ''; |
| 49 |
|
| 50 |
// Init a connection to the MySQL database. |
| 51 |
$wpdb_mysql = new wpdb( $dbuser, $dbpassword, $dbname, $dbhost ); |
| 52 |
$wpdb_mysql->set_prefix( $table_prefix ); |
| 53 |
|
| 54 |
sqlite_plugin_deactivate_in_mysql( $wpdb_mysql, $network_deactivating ); |
| 55 |
}, |
| 56 |
PHP_INT_MAX |
| 57 |
); |
| 58 |
// Flush any persistent cache. |
| 59 |
wp_cache_flush(); |
| 60 |
} |
| 61 |
register_deactivation_hook( SQLITE_MAIN_FILE, 'sqlite_plugin_remove_db_file' ); // Remove db.php file on plugin deactivation. |
| 62 |
|
| 63 |
/** |
| 64 |
* Deactivate the plugin in the original MySQL database. |
| 65 |
* |
| 66 |
* @access private |
| 67 |
* |
| 68 |
* @param wpdb $wpdb_mysql MySQL database connection. |
| 69 |
* @param bool $network_deactivating Whether the plugin is being deactivated network-wide. |
| 70 |
*/ |
| 71 |
function sqlite_plugin_deactivate_in_mysql( $wpdb_mysql, $network_deactivating ) { |
| 72 |
if ( $network_deactivating ) { |
| 73 |
$network_id = get_current_network_id(); |
| 74 |
$row = $wpdb_mysql->get_row( $wpdb_mysql->prepare( "SELECT meta_value AS active_plugins FROM $wpdb_mysql->sitemeta WHERE site_id = %d AND meta_key = %s LIMIT 1", $network_id, 'active_sitewide_plugins' ) ); |
| 75 |
$table = $wpdb_mysql->sitemeta; |
| 76 |
$value_column = 'meta_value'; |
| 77 |
$where = array( |
| 78 |
'site_id' => $network_id, |
| 79 |
'meta_key' => 'active_sitewide_plugins', |
| 80 |
); |
| 81 |
} else { |
| 82 |
$row = $wpdb_mysql->get_row( $wpdb_mysql->prepare( "SELECT option_value AS active_plugins FROM $wpdb_mysql->options WHERE option_name = %s LIMIT 1", 'active_plugins' ) ); |
| 83 |
$table = $wpdb_mysql->options; |
| 84 |
$value_column = 'option_value'; |
| 85 |
$where = array( 'option_name' => 'active_plugins' ); |
| 86 |
} |
| 87 |
|
| 88 |
if ( ! is_object( $row ) ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
$active_plugins = maybe_unserialize( $row->active_plugins ); |
| 93 |
if ( ! is_array( $active_plugins ) ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
$item = plugin_basename( SQLITE_MAIN_FILE ); |
| 98 |
if ( $network_deactivating ) { |
| 99 |
$key = $item; |
| 100 |
} else { |
| 101 |
$key = array_search( $item, $active_plugins, true ); |
| 102 |
} |
| 103 |
|
| 104 |
if ( false === $key || ! array_key_exists( $key, $active_plugins ) ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
unset( $active_plugins[ $key ] ); |
| 109 |
$wpdb_mysql->update( $table, array( $value_column => maybe_serialize( $active_plugins ) ), $where ); |
| 110 |
} |
| 111 |
|