Fix type error for services requiring SmartButton.

This commit is contained in:
Pedro Silva 2024-01-05 09:37:23 +00:00
parent 723e2cf275
commit a23dbb9c8b
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
2 changed files with 23 additions and 11 deletions

View file

@ -13,6 +13,7 @@ use Psr\Log\LoggerInterface;
use Throwable; use Throwable;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money; use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\Button\Assets\SmartButton; use WooCommerce\PayPalCommerce\Button\Assets\SmartButton;
use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface;
/** /**
* Class CartScriptParamsEndpoint. * Class CartScriptParamsEndpoint.
@ -25,7 +26,7 @@ class CartScriptParamsEndpoint implements EndpointInterface {
/** /**
* The SmartButton. * The SmartButton.
* *
* @var SmartButton * @var SmartButtonInterface
*/ */
private $smart_button; private $smart_button;
@ -39,11 +40,11 @@ class CartScriptParamsEndpoint implements EndpointInterface {
/** /**
* CartScriptParamsEndpoint constructor. * CartScriptParamsEndpoint constructor.
* *
* @param SmartButton $smart_button he SmartButton. * @param SmartButtonInterface $smart_button he SmartButton.
* @param LoggerInterface $logger The logger. * @param LoggerInterface $logger The logger.
*/ */
public function __construct( public function __construct(
SmartButton $smart_button, SmartButtonInterface $smart_button,
LoggerInterface $logger LoggerInterface $logger
) { ) {
$this->smart_button = $smart_button; $this->smart_button = $smart_button;
@ -66,6 +67,11 @@ class CartScriptParamsEndpoint implements EndpointInterface {
*/ */
public function handle_request(): bool { public function handle_request(): bool {
try { try {
if ( ! $this->smart_button instanceof SmartButton ) {
wp_send_json_error();
return false;
}
if ( is_callable( 'wc_maybe_define_constant' ) ) { if ( is_callable( 'wc_maybe_define_constant' ) ) {
wc_maybe_define_constant( 'WOOCOMMERCE_CART', true ); wc_maybe_define_constant( 'WOOCOMMERCE_CART', true );
} }

View file

@ -13,6 +13,7 @@ use Exception;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money; use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\Button\Assets\SmartButton; use WooCommerce\PayPalCommerce\Button\Assets\SmartButton;
use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface;
use WooCommerce\PayPalCommerce\Button\Helper\CartProductsHelper; use WooCommerce\PayPalCommerce\Button\Helper\CartProductsHelper;
/** /**
@ -25,7 +26,7 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
/** /**
* The SmartButton. * The SmartButton.
* *
* @var SmartButton * @var SmartButtonInterface
*/ */
private $smart_button; private $smart_button;
@ -39,14 +40,14 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
/** /**
* ChangeCartEndpoint constructor. * ChangeCartEndpoint constructor.
* *
* @param SmartButton $smart_button The SmartButton. * @param SmartButtonInterface $smart_button The SmartButton.
* @param \WC_Cart $cart The current WC cart object. * @param \WC_Cart $cart The current WC cart object.
* @param RequestData $request_data The request data helper. * @param RequestData $request_data The request data helper.
* @param CartProductsHelper $cart_products The cart products helper. * @param CartProductsHelper $cart_products The cart products helper.
* @param LoggerInterface $logger The logger. * @param LoggerInterface $logger The logger.
*/ */
public function __construct( public function __construct(
SmartButton $smart_button, SmartButtonInterface $smart_button,
\WC_Cart $cart, \WC_Cart $cart,
RequestData $request_data, RequestData $request_data,
CartProductsHelper $cart_products, CartProductsHelper $cart_products,
@ -68,6 +69,11 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
* @throws Exception On error. * @throws Exception On error.
*/ */
protected function handle_data(): bool { protected function handle_data(): bool {
if ( ! $this->smart_button instanceof SmartButton ) {
wp_send_json_error();
return false;
}
$products = $this->products_from_request(); $products = $this->products_from_request();
if ( ! $products ) { if ( ! $products ) {