| @@ -1,10 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Handle the SQLite deactivation. |
| 4 | - * | |
| 5 | - * @since 1.0.0 | |
| 6 | - * @package wp-sqlite-integration | |
| 7 | 4 | */ |
| 8 | 5 | |
| 9 | 6 | /** |
| 10 | 7 | * Delete the db.php file in wp-content. |
| @@ -9,14 +6,22 @@ | ||
| 9 | 6 | /** |
| 10 | 7 | * Delete the db.php file in wp-content. |
| 11 | 8 | * |
| 12 | 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. | |
| 13 | 12 | */ |
| 14 | -function sqlite_plugin_remove_db_file() { | |
| 15 | - if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) || ! file_exists( WP_CONTENT_DIR . '/db.php' ) ) { | |
| 13 | +function sqlite_plugin_remove_db_file( $network_deactivating = false ) { | |
| 14 | + if ( ! sqlite_plugin_has_active_dropin() ) { | |
| 16 | 15 | return; |
| 17 | 16 | } |
| 18 | 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 | + | |
| 19 | 24 | global $wp_filesystem; |
| 20 | 25 | |
| 21 | 26 | require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 22 | 27 | |
| @@ -32,9 +37,9 @@ | ||
| 32 | 37 | |
| 33 | 38 | // Run an action on `shutdown`, to deactivate the option in the MySQL database. |
| 34 | 39 | add_action( |
| 35 | 40 | 'shutdown', |
| 36 | - function () { | |
| 41 | + function () use ( $network_deactivating ) { | |
| 37 | 42 | global $table_prefix; |
| 38 | 43 | |
| 39 | 44 | // Get credentials for the MySQL database. |
| 40 | 45 | $dbuser = defined( 'DB_USER' ) ? DB_USER : ''; |
| @@ -45,21 +50,9 @@ | ||
| 45 | 50 | // Init a connection to the MySQL database. |
| 46 | 51 | $wpdb_mysql = new wpdb( $dbuser, $dbpassword, $dbname, $dbhost ); |
| 47 | 52 | $wpdb_mysql->set_prefix( $table_prefix ); |
| 48 | 53 | |
| 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 | - } | |
| 54 | + sqlite_plugin_deactivate_in_mysql( $wpdb_mysql, $network_deactivating ); | |
| 62 | 55 | }, |
| 63 | 56 | PHP_INT_MAX |
| 64 | 57 | ); |
| 65 | 58 | // Flush any persistent cache. |
| @@ -65,4 +58,53 @@ | ||
| 65 | 58 | // Flush any persistent cache. |
| 66 | 59 | wp_cache_flush(); |
| 67 | 60 | } |
| 68 | 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 | +} | |