Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor how transients are deleted by option name #3548

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions tests/php/TestUninstall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Test the uninstall class/process
*
* @since 4.7.0
* @package elasticpress
*/

namespace ElasticPressTest;

/**
* TestUninstall test class
*/
class TestUninstall extends BaseTestCase {
/**
* Holds the EP_Uninstaller class instance.
*
* @var EP_Uninstaller
*/
protected $uninstaller;

/**
* Setup each test.
*/
public function set_up() {
require_once __DIR__ . '/../../uninstall.php';

$this->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;
}
}
56 changes: 31 additions & 25 deletions uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()' );
}

/**
Expand All @@ -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();
}
}

Expand Down