| @@ -1,7 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Handle the SQLite deactivation. |
| 4 | + * | |
| 5 | + * @since 1.0.0 | |
| 6 | + * @package wp-sqlite-integration | |
| 4 | 7 | */ |
| 5 | 8 | |
| 6 | 9 | /** |
| 7 | 10 | * Delete the db.php file in wp-content. |
| @@ -6,22 +9,14 @@ | ||
| 6 | 9 | /** |
| 7 | 10 | * Delete the db.php file in wp-content. |
| 8 | 11 | * |
| 9 | 12 | * 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 | */ |
| 13 | -function sqlite_plugin_remove_db_file( $network_deactivating = false ) { | |
| 14 | - if ( ! sqlite_plugin_has_active_dropin() ) { | |
| 14 | +function sqlite_plugin_remove_db_file() { | |
| 15 | + if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) || ! file_exists( WP_CONTENT_DIR . '/db.php' ) ) { | |
| 15 | 16 | return; |
| 16 | 17 | } |
| 17 | 18 | |
| 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 | 19 | global $wp_filesystem; |
| 25 | 20 | |
| 26 | 21 | require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 27 | 22 | |
| @@ -37,9 +32,9 @@ | ||
| 37 | 32 | |
| 38 | 33 | // Run an action on `shutdown`, to deactivate the option in the MySQL database. |
| 39 | 34 | add_action( |
| 40 | 35 | 'shutdown', |
| 41 | - function () use ( $network_deactivating ) { | |
| 36 | + function () { | |
| 42 | 37 | global $table_prefix; |
| 43 | 38 | |
| 44 | 39 | // Get credentials for the MySQL database. |
| 45 | 40 | $dbuser = defined( 'DB_USER' ) ? DB_USER : ''; |
| @@ -50,9 +45,21 @@ | ||
| 50 | 45 | // Init a connection to the MySQL database. |
| 51 | 46 | $wpdb_mysql = new wpdb( $dbuser, $dbpassword, $dbname, $dbhost ); |
| 52 | 47 | $wpdb_mysql->set_prefix( $table_prefix ); |
| 53 | 48 | |
| 54 | - sqlite_plugin_deactivate_in_mysql( $wpdb_mysql, $network_deactivating ); | |
| 49 | + // Get the perflab options, remove the database/sqlite module and update the option. | |
| 50 | + $row = $wpdb_mysql->get_row( $wpdb_mysql->prepare( "SELECT option_value FROM $wpdb_mysql->options WHERE option_name = %s LIMIT 1", 'active_plugins' ) ); | |
| 51 | + if ( is_object( $row ) ) { | |
| 52 | + $value = maybe_unserialize( $row->option_value ); | |
| 53 | + if ( is_array( $value ) ) { | |
| 54 | + $value_flipped = array_flip( $value ); | |
| 55 | + $items = array_reverse( explode( DIRECTORY_SEPARATOR, SQLITE_MAIN_FILE ) ); | |
| 56 | + $item = $items[1] . DIRECTORY_SEPARATOR . $items[0]; | |
| 57 | + unset( $value_flipped[ $item ] ); | |
| 58 | + $value = array_flip( $value_flipped ); | |
| 59 | + $wpdb_mysql->update( $wpdb_mysql->options, array( 'option_value' => maybe_serialize( $value ) ), array( 'option_name' => 'active_plugins' ) ); | |
| 60 | + } | |
| 61 | + } | |
| 55 | 62 | }, |
| 56 | 63 | PHP_INT_MAX |
| 57 | 64 | ); |
| 58 | 65 | // Flush any persistent cache. |
| @@ -58,53 +65,4 @@ | ||
| 58 | 65 | // Flush any persistent cache. |
| 59 | 66 | wp_cache_flush(); |
| 60 | 67 | } |
| 61 | 68 | 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 | -} | |