*/ private static $running = array(); /** * Whether init() ran. * * @var bool */ private static $started = false; /** * Shim classes, checked in this order. * * @return string[] */ public static function classes(): array { /** * Filters the compatibility shim classes (each extends AiolosCheckout\Compat\Plugins\Shim). * * @since 1.6.0 * * @param string[] $classes Class names. */ $classes = (array) apply_filters( 'aiolos_compat_shims', array( Plugins\Elementor::class, Plugins\Subscriptions::class, Plugins\GiftCards::class, Plugins\CouponPlugins::class, ) ); return array_values( array_filter( $classes, static fn( $class_name ) => is_string( $class_name ) && is_subclass_of( $class_name, Shim::class ) ) ); } /** * Starts the generic repairs (for any theme or plugin) and the shims whose plugin is active. */ public static function init(): void { if ( self::$started ) { return; } self::$started = true; Hooks::init(); Page::init(); Express::init(); CartHooks::init(); foreach ( self::classes() as $class_name ) { if ( ! $class_name::available() ) { continue; } $shim = new $class_name(); $shim->run(); self::$running[ $class_name ] = $shim; } add_action( 'wp_enqueue_scripts', array( self::class, 'enqueue' ), 25 ); } /** * The running shims. * * @return array */ public static function running(): array { return self::$running; } /** * Whether a shim runs. * * @param string $class_name Shim class name. */ public static function is_running( string $class_name ): bool { return isset( self::$running[ $class_name ] ); } /** * On the checkout: the generic compatibility script, the running shims' parameters and CSS. */ public static function enqueue(): void { if ( ! Checkout::active() ) { return; } $params = array( 'enterApply' => array(), 'refreshOnAjax' => array(), 'couponProxy' => false, ); // A rate whose pickup point is missing (compat.js marks it). $css = ':root .acw-shipping-methods li.acw-pickup-missing [data-acw-extra],:root .acw-relocated.acw-pickup-missing{outline:2px solid #dc2626;outline-offset:2px;border-radius:var(--acw-radius,8px)}' // Pickup and delivery UIs moved under the shipping methods (Hooks.php). . ':root .acw-relocated{margin-top:16px}:root .acw-relocated:empty{display:none}'; foreach ( self::$running as $shim ) { foreach ( $shim->script_params() as $key => $value ) { if ( is_array( $value ) && isset( $params[ $key ] ) && is_array( $params[ $key ] ) ) { $params[ $key ] = array_merge( $params[ $key ], $value ); } else { $params[ $key ] = $value; } } $css .= $shim->checkout_css(); } /** * Filters what a pickup-point field looks like by its name or id (a JavaScript regular * expression, case-insensitive): when the chosen rate has such a field and every one of * them is empty, the customer is asked to choose a point before going on. * * @since 1.6.0 * * @param string $pattern Pattern source. */ $params['pickupPattern'] = (string) apply_filters( 'aiolos_pickup_field_pattern', 'locker|point|pickup|station|terminal|parcel_?shop|box_?now|(?:^|[_-])acs(?:[_-]|$)|smartpoint|paketomat|automat' ); $params['i18n'] = array( 'pickup' => __( 'Choose a pickup point or locker for this shipping method to continue.', 'aiolos-checkout-for-woocommerce' ), ); /** * Filters the parameters of the checkout's compatibility script. * * @since 1.6.0 * * @param array $params Parameters. */ $params = (array) apply_filters( 'aiolos_compat_script_params', $params ); $deps = array( 'jquery', 'wc-checkout' ); if ( wp_script_is( 'aiolos-checkout', 'registered' ) ) { $deps[] = 'aiolos-checkout'; } wp_enqueue_script( 'aiolos-compat', Plugin::asset( 'js/compat.js' ), $deps, Plugin::asset_version( 'js/compat.js' ), true ); wp_localize_script( 'aiolos-compat', 'aiolosCompat', $params ); if ( wp_style_is( 'aiolos-checkout', 'registered' ) ) { wp_add_inline_style( 'aiolos-checkout', wp_strip_all_tags( $css ) ); } } /** * Finds a class method hooked on an action or filter (an object's method or a static one). * * @param string $hook Hook name. * @param string $class_name Class name (subclasses match too). * @param string $method Method name. * @return array{callback:callable,priority:int,accepted_args:int}|null */ public static function find_callback( string $hook, string $class_name, string $method ): ?array { global $wp_filter; if ( empty( $wp_filter[ $hook ] ) || ! $wp_filter[ $hook ] instanceof \WP_Hook ) { return null; } $class_name = ltrim( $class_name, '\\' ); foreach ( $wp_filter[ $hook ]->callbacks as $priority => $callbacks ) { foreach ( $callbacks as $entry ) { $function = $entry['function']; if ( is_string( $function ) && false !== strpos( $function, '::' ) ) { $function = explode( '::', $function, 2 ); } if ( ! is_array( $function ) || 2 !== count( $function ) || ! is_string( $function[1] ) || 0 !== strcasecmp( $function[1], $method ) ) { continue; } $owner = $function[0]; $match = is_object( $owner ) ? ( 0 === strcasecmp( get_class( $owner ), $class_name ) || is_a( $owner, $class_name ) ) : ( is_string( $owner ) && 0 === strcasecmp( ltrim( $owner, '\\' ), $class_name ) ); if ( $match ) { return array( 'callback' => $entry['function'], 'priority' => (int) $priority, 'accepted_args' => (int) $entry['accepted_args'], ); } } } return null; } /** * Removes a class method from a hook. * * @param string $hook Hook name. * @param string $class_name Class name. * @param string $method Method name. * @return array{callback:callable,priority:int,accepted_args:int}|null What was removed. */ public static function remove_callback( string $hook, string $class_name, string $method ): ?array { $found = self::find_callback( $hook, $class_name, $method ); if ( $found ) { remove_filter( $hook, $found['callback'], $found['priority'] ); } return $found; } /** * Moves a class method from one hook to another. * * @param string $hook Hook it is on. * @param string $class_name Class name. * @param string $method Method name. * @param string $new_hook Hook it moves to. * @param int|null $new_priority Priority there (null: the same). * @return bool Whether it was found and moved. */ public static function move_callback( string $hook, string $class_name, string $method, string $new_hook, ?int $new_priority = null ): bool { $found = self::remove_callback( $hook, $class_name, $method ); if ( ! $found ) { return false; } add_filter( $new_hook, $found['callback'], null === $new_priority ? $found['priority'] : $new_priority, $found['accepted_args'] ); return true; } }