Refactor germanized plugin asset loaded

This commit is contained in:
Emili Castells Guasch 2023-07-31 16:17:17 +02:00
parent e8118381fa
commit 66f29789fb
3 changed files with 67 additions and 35 deletions

View file

@ -0,0 +1,39 @@
<?php
/**
* The compatibility module.
*
* @package WooCommerce\PayPalCommerce\Compat
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Compat;
use WC_Order;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
trait AdminContextTrait {
/**
* Checks if current post id is from a PayPal order.
*
* @return bool
*/
private function is_paypal_order_edit_page(): bool {
$post_id = isset( $_GET['post'] ) ? (int) $_GET['post'] : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! $post_id ) {
return false;
}
$order = wc_get_order( $post_id );
if ( ! is_a( $order, WC_Order::class ) ) {
return false;
}
if ( ! $order->get_meta( PayPalGateway::ORDER_ID_META_KEY ) ) {
return false;
}
return true;
}
}

View file

@ -20,6 +20,7 @@ use WC_Order;
use WooCommerce\PayPalCommerce\Compat\Assets\CompatAssets;
use WooCommerce\PayPalCommerce\OrderTracking\Endpoint\OrderTrackingEndpoint;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WP_Theme;
@ -28,6 +29,8 @@ use WP_Theme;
*/
class CompatModule implements ModuleInterface {
use AdminContextTrait;
/**
* Setup the compatibility module.
*
@ -48,14 +51,9 @@ class CompatModule implements ModuleInterface {
public function run( ContainerInterface $c ): void {
$this->initialize_ppec_compat_layer( $c );
$this->fix_site_ground_optimizer_compatibility( $c );
$this->initialize_gzd_compat_layer( $c );
$asset_loader = $c->get( 'compat.assets' );
assert( $asset_loader instanceof CompatAssets );
add_action( 'init', array( $asset_loader, 'register' ) );
add_action( 'admin_enqueue_scripts', array( $asset_loader, 'enqueue' ) );
$this->migrate_pay_later_settings( $c );
$this->migrate_smart_button_settings( $c );
@ -128,6 +126,26 @@ class CompatModule implements ModuleInterface {
return;
}
add_action(
'admin_enqueue_scripts',
/**
* Param types removed to avoid third-party issues.
*
* @psalm-suppress MissingClosureParamType
*/
function( $hook ) use ( $c ): void {
if ( $hook !== 'post.php' || ! $this->is_paypal_order_edit_page() ) {
return;
}
$asset_loader = $c->get( 'compat.assets' );
assert( $asset_loader instanceof CompatAssets );
$asset_loader->register();
$asset_loader->enqueue();
}
);
$endpoint = $c->get( 'order-tracking.endpoint.controller' );
assert( $endpoint instanceof OrderTrackingEndpoint );