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

Register a "Reset Order Limiting" WooCommerce tool #42

Merged
merged 2 commits into from
Jul 15, 2020
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Configuration for Limit Orders for WooCommerce is available through WooCommerce

For example, if you're using an hourly interval and switch it to daily, the plugin will re-calculate whether or not to disable ordering based on the number of orders received since the start of the current day (midnight, by default).

If you need to clear the cached order count, you may do so via WooCommerce › Status › Tools › Reset Order Limiting within WP Admin.

### General settings

These settings determine how and when order limiting should take effect.
Expand Down
27 changes: 27 additions & 0 deletions src/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public function init() {
add_filter( 'woocommerce_get_settings_pages', [ $this, 'register_settings_page' ] );
add_filter( 'admin_notices', [ $this, 'admin_notice' ] );
add_filter( 'plugin_action_links_' . $basename, [ $this, 'action_links' ] );
add_filter( 'woocommerce_debug_tools', [ $this, 'debug_tools' ] );
add_action( 'woocommerce_delete_shop_order_transients', [ $this, 'reset_limiter' ] );
add_action( 'woocommerce_system_status_report', [ $this, 'system_status_report' ] );
}

Expand Down Expand Up @@ -112,6 +114,31 @@ public function system_status_report() {
] );
}

/**
* Add additional debugging tools.
*
* @param array $tools Currently-registered tools.
*
* @return array The $tools array with our button included.
*/
public function debug_tools( $tools ) {
$tools['limit_orders'] = [
'name' => __( 'Reset order limiting', 'limit-orders' ),
'button' => __( 'Reset limiter', 'limit-orders' ),
'desc' => __( 'Clear the cached order count. This may be needed if you\'ve changed your order limiting settings.', 'limit-orders' ),
'callback' => [ $this, 'reset_limiter' ],
];

return $tools;
}

/**
* Delete the order limiter transient.
*/
public function reset_limiter() {
$this->limiter->reset();
}

/**
* Retrieve a link to the plugin's settings page.
*
Expand Down
13 changes: 10 additions & 3 deletions src/OrderLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct( \DateTimeImmutable $now = null ) {
*/
public function init() {
add_action( 'woocommerce_new_order', [ $this, 'regenerate_transient' ] );
add_action( 'update_option_' . self::OPTION_KEY, [ $this, 'reset_limiter' ], 10, 2 );
add_action( 'update_option_' . self::OPTION_KEY, [ $this, 'reset_limiter_on_update' ], 10, 2 );
}

/**
Expand Down Expand Up @@ -359,15 +359,22 @@ public function regenerate_transient() {
return $count;
}

/**
* Reset the order limiter.
*/
public function reset() {
delete_transient( self::TRANSIENT_NAME );
}

/**
* Reset the limiter when its configuration changes.
*
* @param mixed $previous The previous value of the option.
* @param mixed $new The new option value.
*/
public function reset_limiter( $previous, $new ) {
public function reset_limiter_on_update( $previous, $new ) {
if ( $previous !== $new ) {
delete_transient( self::TRANSIENT_NAME );
$this->reset();
}
}

Expand Down
38 changes: 38 additions & 0 deletions tests/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

use Nexcess\LimitOrders\Admin;
use Nexcess\LimitOrders\OrderLimiter;
use WC_Admin_Status;
use WC_REST_System_Status_Tools_V2_Controller;

/**
* @covers Nexcess\LimitOrders\Admin
Expand Down Expand Up @@ -126,4 +128,40 @@ public function admin_notices_should_use_midnight_instead_of_dates_for_daily_int

$this->assertContains( __( 'midnight' ), $output );
}

/**
* @test
*/
public function the_plugin_should_register_a_debug_tool() {
( new Admin( new OrderLimiter() ) )->init();

$this->assertArrayHasKey( 'limit_orders', WC_Admin_Status::get_tools() );
}

/**
* @test
*/
public function the_debug_tool_should_clear_the_order_count_transient() {
set_transient( OrderLimiter::TRANSIENT_NAME, 5 );

( new Admin( new OrderLimiter() ) )->init();

$controller = new WC_REST_System_Status_Tools_V2_Controller();
$controller->execute_tool( 'limit_orders' );

$this->assertFalse( get_transient( OrderLimiter::TRANSIENT_NAME ) );
}

/**
* @test
*/
public function clearing_order_transients_should_remove_the_order_count() {
set_transient( OrderLimiter::TRANSIENT_NAME, 5 );

( new Admin( new OrderLimiter() ) )->init();

wc_delete_shop_order_transients();

$this->assertFalse( get_transient( OrderLimiter::TRANSIENT_NAME ) );
}
}
11 changes: 11 additions & 0 deletions tests/OrderLimiterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,17 @@ public function regenerate_transient_should_create_the_site_transient() {
$this->assertSame( 3, get_transient( OrderLimiter::TRANSIENT_NAME ) );
}

/**
* @test
*/
public function reset_should_delete_the_transient_cache() {
set_transient( OrderLimiter::TRANSIENT_NAME, 5 );

( new OrderLimiter() )->reset();

$this->assertFalse( get_transient( OrderLimiter::TRANSIENT_NAME ) );
}

/**
* @test
*/
Expand Down