-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mechanism to refresh feature status.
- Loading branch information
1 parent
be28f81
commit d2abeb5
Showing
8 changed files
with
178 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
modules/ppcp-wc-gateway/src/Endpoint/RefreshFeatureStatusEndpoint.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
/** | ||
* Controls the endpoint for refreshing feature status. | ||
* | ||
* @package WooCommerce\PayPalCommerce\WcGateway\Endpoint | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WooCommerce\PayPalCommerce\WcGateway\Endpoint; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; | ||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; | ||
|
||
/** | ||
* Class RefreshFeatureStatusEndpoint | ||
*/ | ||
class RefreshFeatureStatusEndpoint { | ||
|
||
const ENDPOINT = 'ppc-refresh-feature-status'; | ||
|
||
const CACHE_KEY = 'refresh_feature_status_timeout'; | ||
const TIMEOUT = 60; | ||
|
||
/** | ||
* The settings. | ||
* | ||
* @var ContainerInterface | ||
*/ | ||
protected $settings; | ||
|
||
/** | ||
* The cache. | ||
* | ||
* @var Cache | ||
*/ | ||
protected $cache; | ||
|
||
/** | ||
* The logger. | ||
* | ||
* @var LoggerInterface | ||
*/ | ||
protected $logger; | ||
|
||
/** | ||
* RefreshFeatureStatusEndpoint constructor. | ||
* | ||
* @param ContainerInterface $settings The settings. | ||
* @param Cache $cache The cache. | ||
* @param LoggerInterface $logger The logger. | ||
*/ | ||
public function __construct( | ||
ContainerInterface $settings, | ||
Cache $cache, | ||
LoggerInterface $logger | ||
) { | ||
$this->settings = $settings; | ||
$this->cache = $cache; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Returns the nonce. | ||
* | ||
* @return string | ||
*/ | ||
public static function nonce(): string { | ||
return self::ENDPOINT; | ||
} | ||
|
||
/** | ||
* Handles the incoming request. | ||
*/ | ||
public function handle_request(): void { | ||
$now = time(); | ||
$last_request_time = $this->cache->get( self::CACHE_KEY ) ?: 0; | ||
$seconds_missing = $last_request_time + self::TIMEOUT - $now; | ||
|
||
if ( $seconds_missing > 0 ) { | ||
$response = array( | ||
'message' => sprintf( | ||
// translators: %1$s is the number of seconds remaining. | ||
__( 'Wait %1$s seconds before trying again.', 'woocommerce-paypal-payments' ), | ||
$seconds_missing | ||
), | ||
); | ||
wp_send_json_error( $response ); | ||
} | ||
|
||
$this->cache->set( self::CACHE_KEY, $now, self::TIMEOUT ); | ||
do_action( 'woocommerce_paypal_payments_clear_apm_product_status', $this->settings ); | ||
wp_send_json_success(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters