*/ private static $relocated = array(); /** * Whether the repairs ran for this request. * * @var bool */ private static $done = false; /** * Hooks. */ public static function init(): void { // Before Checkout::maybe_take_over() (template_redirect 100) renders the page. add_action( 'template_redirect', array( self::class, 'on_page' ), 99 ); // update_order_review renders the payment box and the summary again. add_action( 'woocommerce_checkout_update_order_review', array( self::class, 'on_ajax' ), 1 ); add_filter( 'woocommerce_update_order_review_fragments', array( self::class, 'fragment' ) ); } /** * On the checkout page. */ public static function on_page(): void { if ( Checkout::active() ) { self::repair(); add_action( 'aiolos_after_shipping_methods', array( self::class, 'render_relocated' ), 5 ); } } /** * In our form's AJAX updates. */ public static function on_ajax(): void { if ( Checkout::from_our_form() ) { self::repair(); } } /** * Runs both repairs once. */ private static function repair(): void { if ( self::$done ) { return; } self::$done = true; self::core_placements(); if ( function_exists( 'WC' ) && WC()->cart && WC()->cart->needs_shipping() ) { self::relocate_shipping_ui(); } } /** * The hooks fired on our checkout page and in its updates: where a moved core function or a * shipping UI could print. * * @return string[] */ private static function checkout_hooks(): array { global $wp_filter; $hooks = array(); foreach ( array_keys( (array) $wp_filter ) as $hook ) { $hook = (string) $hook; if ( preg_match( '/^(woocommerce_(checkout_|review_order_|before_checkout|after_checkout|before_order_notes|after_order_notes)|aiolos_)/', $hook ) ) { $hooks[] = $hook; } } return $hooks; } /** * WooCommerce functions printed once, at their default hook and priority. * * @return array */ private static function core_functions(): array { $checkout = WC()->checkout(); $list = array( array( 'callback' => 'woocommerce_checkout_payment', 'hook' => 'woocommerce_checkout_order_review', 'priority' => 20, ), array( 'callback' => 'woocommerce_order_review', 'hook' => 'woocommerce_checkout_order_review', 'priority' => 10, ), array( 'callback' => 'woocommerce_checkout_login_form', 'hook' => 'woocommerce_before_checkout_form', 'priority' => 10, ), array( 'callback' => 'woocommerce_checkout_coupon_form', 'hook' => 'woocommerce_before_checkout_form', 'priority' => 10, ), array( 'callback' => 'wc_checkout_privacy_policy_text', 'hook' => 'woocommerce_checkout_terms_and_conditions', 'priority' => 20, ), array( 'callback' => 'wc_terms_and_conditions_page_content', 'hook' => 'woocommerce_checkout_terms_and_conditions', 'priority' => 30, ), array( 'callback' => array( $checkout, 'checkout_form_billing' ), 'hook' => 'woocommerce_checkout_billing', 'priority' => 10, ), array( 'callback' => array( $checkout, 'checkout_form_shipping' ), 'hook' => 'woocommerce_checkout_shipping', 'priority' => 10, ), ); /** * Filters the WooCommerce functions kept once, at their default place, on Aiolos Checkout's * page (themes that move them to another hook would print them twice). * * @since 1.6.0 * * @param array $list Each: callback, hook, priority. */ return (array) apply_filters( 'aiolos_core_placements', $list ); } /** * Whether a hooked callback is the given one (functions by name, methods by class and name). * * @param mixed $hooked Hooked callback. * @param mixed $wanted Wanted callback. */ private static function same_callback( $hooked, $wanted ): bool { if ( is_string( $wanted ) ) { return is_string( $hooked ) && 0 === strcasecmp( ltrim( $hooked, '\\' ), $wanted ); } if ( ! is_array( $wanted ) || ! is_array( $hooked ) || 2 !== count( $wanted ) || 2 !== count( $hooked ) || ! is_string( $wanted[1] ) || ! is_string( $hooked[1] ) ) { return false; } if ( 0 !== strcasecmp( $hooked[1], $wanted[1] ) ) { return false; } $class = is_object( $wanted[0] ) ? get_class( $wanted[0] ) : (string) $wanted[0]; return is_object( $hooked[0] ) ? is_a( $hooked[0], $class ) : ( is_string( $hooked[0] ) && 0 === strcasecmp( ltrim( $hooked[0], '\\' ), ltrim( $class, '\\' ) ) ); } /** * Keeps each core function at its default place only. One moved elsewhere (and not left at its * default) goes back to the default; one a plugin removed on purpose (Germanized replaces the * privacy text) stays removed. */ private static function core_placements(): void { global $wp_filter; $hooks = self::checkout_hooks(); foreach ( self::core_functions() as $core ) { if ( ! is_array( $core ) || empty( $core['callback'] ) || empty( $core['hook'] ) || ! is_string( $core['hook'] ) ) { continue; } $default_hook = $core['hook']; $default_priority = (int) ( $core['priority'] ?? 10 ); $found = array(); foreach ( $hooks as $hook ) { if ( empty( $wp_filter[ $hook ] ) || ! $wp_filter[ $hook ] instanceof \WP_Hook ) { continue; } foreach ( $wp_filter[ $hook ]->callbacks as $priority => $callbacks ) { foreach ( $callbacks as $entry ) { if ( self::same_callback( $entry['function'], $core['callback'] ) ) { $found[] = array( $hook, (int) $priority, $entry['function'] ); } } } } if ( ! $found ) { continue; } $at_default = false; foreach ( $found as $placement ) { if ( $placement[0] === $default_hook && $placement[1] === $default_priority ) { $at_default = true; continue; } remove_action( $placement[0], $placement[2], $placement[1] ); } if ( ! $at_default ) { add_action( $default_hook, $found[0][2], $default_priority ); } } } /** * Name of a callback for matching: class::method or function, plus the folder of the plugin * it comes from. Null for WooCommerce's and this plugin's own callbacks. * * @param mixed $callback Callback. */ private static function describe( $callback ): ?string { $name = ''; try { if ( is_string( $callback ) && false !== strpos( $callback, '::' ) ) { $callback = explode( '::', $callback, 2 ); } if ( is_array( $callback ) && 2 === count( $callback ) && is_string( $callback[1] ) ) { $class = is_object( $callback[0] ) ? get_class( $callback[0] ) : (string) $callback[0]; $name = $class . '::' . $callback[1]; $reflection = new \ReflectionMethod( $class, $callback[1] ); } elseif ( is_string( $callback ) ) { $name = $callback; $reflection = new \ReflectionFunction( $callback ); } elseif ( $callback instanceof \Closure ) { $reflection = new \ReflectionFunction( $callback ); } else { return null; } } catch ( \ReflectionException $e ) { return null; } $file = wp_normalize_path( (string) $reflection->getFileName() ); if ( '' === $file ) { return null; } $own = array( wp_normalize_path( WC()->plugin_path() ) . '/', wp_normalize_path( AIOLOS_CHECKOUT_PATH ) ); if ( defined( 'ACWP_PATH' ) ) { $own[] = wp_normalize_path( (string) ACWP_PATH ); } foreach ( $own as $dir ) { if ( 0 === strpos( $file, trailingslashit( $dir ) ) ) { return null; } } $plugins = trailingslashit( wp_normalize_path( WP_PLUGIN_DIR ) ); if ( 0 === strpos( $file, $plugins ) ) { $name .= ' ' . strtok( substr( $file, strlen( $plugins ) ), '/' ); } return '' === trim( $name ) ? null : $name; } /** * Moves callbacks that look like a shipping UI to under the shipping methods. */ private static function relocate_shipping_ui(): void { global $wp_filter; /** * Filters the hooks whose shipping UIs (pickup points, delivery dates) move under the shipping * methods on Aiolos Checkout's page. * * @since 1.6.0 * * @param string[] $hooks Hook names. */ $hooks = (array) apply_filters( 'aiolos_shipping_ui_hooks', array( 'woocommerce_review_order_before_payment', 'woocommerce_review_order_after_payment', 'woocommerce_before_checkout_billing_form', 'woocommerce_after_checkout_billing_form', 'woocommerce_checkout_before_order_review_heading', 'woocommerce_checkout_before_order_review', 'woocommerce_checkout_after_order_review', 'woocommerce_review_order_before_order_total', 'woocommerce_review_order_after_order_total', ) ); /** * Filters what a shipping UI callback's name (class::method or function, and plugin folder) * looks like: a PCRE pattern. * * @since 1.6.0 * * @param string $pattern Pattern. */ $pattern = (string) apply_filters( 'aiolos_shipping_ui_pattern', '/pickup|locker|(?:^|[^a-z])points?(?:[^a-z]|$)|parcel|terminal|delivery_?(?:date|time)|timeslot|(?:^|[^a-z])slots?(?:[^a-z]|$)/i' ); if ( false === @preg_match( $pattern, '' ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- checks a filtered pattern without a warning. return; } $with_checkout = array( 'woocommerce_before_checkout_billing_form', 'woocommerce_after_checkout_billing_form' ); foreach ( $hooks as $hook ) { if ( ! is_string( $hook ) || empty( $wp_filter[ $hook ] ) || ! $wp_filter[ $hook ] instanceof \WP_Hook ) { continue; } foreach ( $wp_filter[ $hook ]->callbacks as $priority => $callbacks ) { foreach ( $callbacks as $entry ) { $name = self::describe( $entry['function'] ); if ( null === $name || ! preg_match( $pattern, $name ) ) { continue; } /** * Filters whether a callback moves under the shipping methods. * * @since 1.6.0 * * @param bool $move Whether to move it. * @param callable $callback Callback. * @param string $hook Hook it is on. * @param string $name Its name as matched. */ if ( ! apply_filters( 'aiolos_relocate_shipping_ui', true, $entry['function'], $hook, $name ) ) { continue; } remove_action( $hook, $entry['function'], $priority ); self::$relocated[] = array( $entry['function'], in_array( $hook, $with_checkout, true ) ? 'checkout' : '', (int) $entry['accepted_args'] ); } } } } /** * Prints the moved shipping UIs (also the fragment of AJAX updates). */ public static function render_relocated(): void { if ( ! self::$relocated ) { return; } echo '
'; foreach ( self::$relocated as $moved ) { $args = 'checkout' === $moved[1] ? array( WC()->checkout() ) : array(); call_user_func_array( $moved[0], array_slice( $args, 0, max( 0, $moved[2] ) ) ); } echo '
'; } /** * The moved shipping UIs, refreshed with the totals. * * @param mixed $fragments Fragments. * @return mixed */ public static function fragment( $fragments ) { if ( ! is_array( $fragments ) || ! self::$relocated || ! Checkout::from_our_form() ) { return $fragments; } ob_start(); self::render_relocated(); $fragments['.acw-relocated'] = (string) ob_get_clean(); return $fragments; } }