From 888df716c43c0191c32e2b8bfc11d7ea0a9ed055 Mon Sep 17 00:00:00 2001 From: Marian Kadanka Date: Fri, 20 Apr 2018 16:28:36 +0200 Subject: [PATCH] Fix Ajax endpoint URL again, fixes #319 Another change in WC_AJAX->get_endpoint() implementation - see woocommerce/woocommerce#19139 - this time it returns relative URLs. This breaks Polylangs home_url() filter, which can't handle them. We have to use woocommerce_ajax_get_endpoint filter to alter the AJAX endpoint URL according to the current language - replace the path part of the URL with the correct relative home URL and append the query string. --- src/Hyyan/WPI/Ajax.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Hyyan/WPI/Ajax.php b/src/Hyyan/WPI/Ajax.php index 0b1e5a0..52574d8 100644 --- a/src/Hyyan/WPI/Ajax.php +++ b/src/Hyyan/WPI/Ajax.php @@ -25,19 +25,22 @@ class Ajax */ public function __construct() { - add_filter('pll_home_url_white_list', array( $this, 'pll_home_url_white_list' )); + add_filter('woocommerce_ajax_get_endpoint', array($this, 'filter_woocommerce_ajax_get_endpoint'), 10, 2); } /** - * Add WooCommerce class-wc-ajax.php to the Polylang home_url white list + * Filter woocommerce_ajax_get_endpoint URL - replace the path part + * with the correct relative home URL according to the current language + * and append the query string * - * @param array $white_list Polylang home_url white list + * @param string $url WC AJAX endpoint URL to filter + * @param string $request * - * @return array filtered white list + * @return string filtered WC AJAX endpoint URL */ - public function pll_home_url_white_list($white_list) + public function filter_woocommerce_ajax_get_endpoint($url, $request) { - $white_list[] = array( 'file' => 'class-wc-ajax.php' ); - return $white_list; + global $polylang; + return parse_url($polylang->filters_links->links->get_home_url($polylang->curlang), PHP_URL_PATH) . '?' . parse_url($url, PHP_URL_QUERY); } }