diff --git a/tests/php/TestUninstall.php b/tests/php/TestUninstall.php new file mode 100644 index 000000000..86258962c --- /dev/null +++ b/tests/php/TestUninstall.php @@ -0,0 +1,68 @@ +uninstaller = new \EP_Uninstaller(); + + parent::set_up(); + } + + /** + * Test the `delete_transients_by_option_name` method + * + * @group uninstall + */ + public function test_delete_transients_by_option_name() { + set_transient( 'ep_total_fields_limit_test', 'test' ); + set_transient( 'ep_total_fields_limit_test_2', 'test' ); + set_transient( 'ep_related_posts_test', 'test' ); + set_transient( 'ep_related_posts_test_2', 'test' ); + + $method = $this->get_protected_method( 'delete_transients_by_option_name' ); + $method->invoke( $this->uninstaller ); + + $this->assertFalse( get_transient( 'ep_total_fields_limit_test' ) ); + $this->assertFalse( get_transient( 'ep_total_fields_limit_test_2' ) ); + $this->assertFalse( get_transient( 'ep_related_posts_test' ) ); + $this->assertFalse( get_transient( 'ep_related_posts_test_2' ) ); + } + + /** + * Return a protected method made public. + * + * This should NOT be copied to any other class. + * + * @param string $method_name The method name + * @return \ReflectionMethod + */ + protected function get_protected_method( string $method_name ) : \ReflectionMethod { + $reflection = new \ReflectionClass( '\EP_Uninstaller' ); + $method = $reflection->getMethod( $method_name ); + $method->setAccessible( true ); + + return $method; + } +} diff --git a/uninstall.php b/uninstall.php index 42fad27a1..e171a8e93 100644 --- a/uninstall.php +++ b/uninstall.php @@ -81,12 +81,16 @@ class EP_Uninstaller { * @since 1.7 */ public function __construct() { - // Exit if accessed directly. if ( ! defined( 'ABSPATH' ) ) { $this->exit_uninstaller(); } + // If testing, do not do anything automatically + if ( defined( 'EP_UNIT_TESTS' ) && EP_UNIT_TESTS ) { + return; + } + // EP_MANUAL_SETTINGS_RESET is used by the `settings-reset` WP-CLI command. if ( ! defined( 'EP_MANUAL_SETTINGS_RESET' ) || ! EP_MANUAL_SETTINGS_RESET ) { // Not uninstalling. @@ -124,37 +128,41 @@ protected function delete_transients() { } /** - * Delete all transients of the Related Posts feature. + * Delete remaining transients by their option names. + * + * @since 4.7.0 */ - protected function delete_related_posts_transients() { + protected function delete_transients_by_option_name() { global $wpdb; - $related_posts_transients = $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery - "SELECT option_name FROM {$wpdb->prefix}options WHERE option_name LIKE '_transient_ep_related_posts_%'" + $transients = $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery + "SELECT option_name + FROM {$wpdb->prefix}options + WHERE + option_name LIKE '_transient_ep_total_fields_limit_%' + OR option_name LIKE '_transient_ep_related_posts_%' + " ); - foreach ( $related_posts_transients as $related_posts_transient ) { - $related_posts_transient = str_replace( '_transient_', '', $related_posts_transient ); - delete_site_transient( $related_posts_transient ); - delete_transient( $related_posts_transient ); + foreach ( $transients as $transient ) { + $transient_name = str_replace( '_transient_', '', $transient ); + delete_site_transient( $transient_name ); + delete_transient( $transient_name ); } } /** - * Delete all transients of the total fields limit. + * DEPRECATED. Delete all transients of the Related Posts feature. */ - protected function delete_total_fields_limit_transients() { - global $wpdb; - - $related_posts_transients = $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery - "SELECT option_name FROM {$wpdb->prefix}options WHERE option_name LIKE '_transient_ep_total_fields_limit_%'" - ); + protected function delete_related_posts_transients() { + _deprecated_function( __METHOD__, '4.7.0', '\EP_Uninstaller::delete_transients_by_name()' ); + } - foreach ( $related_posts_transients as $related_posts_transient ) { - $related_posts_transient = str_replace( '_transient_', '', $related_posts_transient ); - delete_site_transient( $related_posts_transient ); - delete_transient( $related_posts_transient ); - } + /** + * DEPRECATED. Delete all transients of the total fields limit. + */ + protected function delete_total_fields_limit_transients() { + _deprecated_function( __METHOD__, '4.7.0', '\EP_Uninstaller::delete_transients_by_name()' ); } /** @@ -180,16 +188,14 @@ protected function clean_options_and_transients() { $this->delete_options(); $this->delete_transients(); - $this->delete_related_posts_transients(); - $this->delete_total_fields_limit_transients(); + $this->delete_transients_by_option_name(); restore_current_blog(); } } else { $this->delete_options(); $this->delete_transients(); - $this->delete_related_posts_transients(); - $this->delete_total_fields_limit_transients(); + $this->delete_transients_by_option_name(); } }