From b73d4077ebc233578a34423352450fda7afa25ec Mon Sep 17 00:00:00 2001 From: meteor Date: Tue, 9 Jul 2024 18:54:14 +0400 Subject: [PATCH] PLUGINS-6911 --- ecwid-shopping-cart.php | 5 +- includes/class-ecwid-static-page.php | 10 +- lib/ecwid_api_v3.php | 58 +++++++++--- lib/ecwid_catalog.php | 133 +++++++++++++-------------- lib/ecwid_platform.php | 36 ++++++-- 5 files changed, 149 insertions(+), 93 deletions(-) diff --git a/ecwid-shopping-cart.php b/ecwid-shopping-cart.php index b3b792d1..bdca8f43 100644 --- a/ecwid-shopping-cart.php +++ b/ecwid-shopping-cart.php @@ -2412,7 +2412,8 @@ function ecwid_get_categories_for_selector() { } $query_params = array( - 'hidden_categories' => true + 'hidden_categories' => true, + 'responseFields' => 'total,count,items(id,name,url,enabled,parentId)' ); if( ecwid_is_demo_store() ) { @@ -2447,8 +2448,6 @@ function ecwid_get_categories_for_selector() { } } } - - $parents = array(); $result = array(); foreach ( $all_categories as $category ) { diff --git a/includes/class-ecwid-static-page.php b/includes/class-ecwid-static-page.php index 82a8920e..e8601a89 100644 --- a/includes/class-ecwid-static-page.php +++ b/includes/class-ecwid-static-page.php @@ -180,7 +180,7 @@ protected static function maybe_fetch_data() { if ( $is_home_page && $is_css_defined && ! $is_css_already_set ) { $cached_data->cssFiles = array( $dynamic_css ); //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase - EcwidPlatform::store_in_static_pages_cache( $url, $cached_data ); + EcwidPlatform::save_in_static_pages_cache( $url, $cached_data ); } return $cached_data; @@ -219,9 +219,13 @@ protected static function get_static_snapshot( $url, $dynamic_css = '' ) { $pattern = '//is'; $fetched_data->htmlCode = preg_replace( $pattern, '', $fetched_data->htmlCode ); - $fetched_data->htmlCode = wp_encode_emoji( $fetched_data->htmlCode ); } + EcwidPlatform::encode_fields_with_emoji( + $fetched_data, + array( 'htmlCode', 'metaDescriptionHtml', 'ogTagsHtml', 'jsonLDHtml' ) + ); + if ( isset( $fetched_data->lastUpdated ) ) { $last_update = substr( $fetched_data->lastUpdated, 0, -3 ); } else { @@ -230,7 +234,7 @@ protected static function get_static_snapshot( $url, $dynamic_css = '' ) { //phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase EcwidPlatform::invalidate_static_pages_cache_from( $last_update ); - EcwidPlatform::store_in_static_pages_cache( $url, $fetched_data ); + EcwidPlatform::save_in_static_pages_cache( $url, $fetched_data ); return $fetched_data; }//end if diff --git a/lib/ecwid_api_v3.php b/lib/ecwid_api_v3.php index 674ec874..b5b58d63 100644 --- a/lib/ecwid_api_v3.php +++ b/lib/ecwid_api_v3.php @@ -163,7 +163,7 @@ public function get_categories( $input_params ) { $params['parent'] = $input_params['parent']; } - $passthru = array( 'offset', 'limit', 'parent', 'baseUrl', 'cleanUrls', 'hidden_categories' ); + $passthru = array( 'offset', 'limit', 'parent', 'baseUrl', 'cleanUrls', 'hidden_categories', 'responseFields' ); foreach ( $passthru as $name ) { if ( array_key_exists( $name, $input_params ) ) { $params[ $name ] = $input_params[ $name ]; @@ -189,13 +189,28 @@ public function get_categories( $input_params ) { if ( ! $result ) { $result = EcwidPlatform::fetch_url( $url, $options ); - } - if ( $result['code'] != '200' ) { - return false; - } + if ( $result['code'] != '200' ) { + return false; + } - EcwidPlatform::store_in_categories_cache( $url, $result ); + // PLUGINS-6870 there's some cases when data are not cached because emojis, need to encode + $data = json_decode( $result['data'] ); + + if ( ! empty( $data->items ) ) { + foreach ( $data->items as &$item ) { + $item = EcwidPlatform::encode_fields_with_emoji( + $item, + array( 'name', 'nameTranslated', 'description', 'descriptionTranslated', 'seoTitle', 'seoTitleTranslated', 'seoDescription', 'seoDescriptionTranslated', 'alt' ) + ); + } + } + + $result['data'] = wp_json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); + // end PLUGINS-6870 there's some cases when data are not cached because emojis, need to encode + + EcwidPlatform::store_in_categories_cache( $url, $result ); + }//end if $result = json_decode( $result['data'] ); @@ -248,13 +263,25 @@ public function get_category( $category_id ) { if ( ! $result ) { $result = EcwidPlatform::fetch_url( $url, $options ); - } - if ( $result['code'] != '200' ) { - return false; - } + if ( $result['code'] != '200' ) { + return false; + } - EcwidPlatform::store_in_categories_cache( $url, $result ); + // PLUGINS-6870 there's some cases when data are not cached because emojis, need to encode + $data = json_decode( $result['data'] ); + if ( ! empty( $data ) ) { + $data = EcwidPlatform::encode_fields_with_emoji( + $data, + array( 'name', 'nameTranslated', 'description', 'descriptionTranslated', 'seoTitle', 'seoTitleTranslated', 'seoDescription', 'seoDescriptionTranslated', 'alt' ) + ); + } + // end PLUGINS-6870 there's some cases when data are not cached because emojis, need to encode + + $result['data'] = wp_json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); + + EcwidPlatform::store_in_categories_cache( $url, $result ); + }//end if $result = json_decode( $result['data'] ); @@ -601,6 +628,15 @@ public function get_store_profile( $disable_cache = false ) { $profile = json_decode( $result['data'] ); + // PLUGINS-6870 there's some cases when data are not cached because emojis, need to encode + if ( ! empty( $profile ) ) { + $profile->settings = EcwidPlatform::encode_fields_with_emoji( + $profile->settings, + array( 'storeName', 'storeDescription', 'storeDescriptionTranslated', 'rootCategorySeoTitleTranslated', 'rootCategorySeoDescription', 'rootCategorySeoDescriptionTranslated' ) + ); + } + // end PLUGINS-6870 there's some cases when data are not cached because emojis, need to encode + EcwidPlatform::cache_set( self::PROFILE_CACHE_NAME, $profile, 10 * MINUTE_IN_SECONDS ); if ( $profile && isset( $profile->settings ) && isset( $profile->settings->hideOutOfStockProductsInStorefront ) ) { diff --git a/lib/ecwid_catalog.php b/lib/ecwid_catalog.php index 69d18f8e..e50e0405 100755 --- a/lib/ecwid_catalog.php +++ b/lib/ecwid_catalog.php @@ -1,59 +1,55 @@ store_id = intval($store_id); - $this->store_base_url = $store_base_url; + public function __construct( $store_id, $store_base_url ) { + $this->store_id = intval( $store_id ); + $this->store_base_url = $store_base_url; } - public function warmup_store_page( $category_id ) - { + public function warmup_store_page( $category_id ) { $this->_get_data_for_category( $category_id, null ); } - - public function get_product($id) - { - $result = $this->_get_data_for_product($id); - if ( !$result ) { + public function get_product( $id ) { + $result = $this->_get_data_for_product( $id ); + + if ( ! $result ) { return ''; } - + ob_start(); $product = $result->product; $formats = $result->formats; - require dirname(__FILE__) . '/html-catalog-templates/product.php'; + require __DIR__ . '/html-catalog-templates/product.php'; $return = ob_get_contents(); ob_end_clean(); - + return $return; } - public function get_category($id) - { - $offset = ( isset($_GET['offset']) ) ? intval($_GET['offset']) : 0; - $data = $this->_get_data_for_category( $id, $offset ); - - if ( !$data ) { + public function get_category( $id ) { + $offset = ( isset( $_GET['offset'] ) ) ? intval( $_GET['offset'] ) : 0; + $data = $this->_get_data_for_category( $id, $offset ); + + if ( ! $data ) { return ''; } - + $main_category = null; - if ($id > 0) { + if ( $id > 0 ) { $main_category = $data->main_category; } $categories = $data->categories; - $products = $data->products; - $formats = $data->formats; - + $products = $data->products; + $formats = $data->formats; + ob_start(); - require dirname(__FILE__) . '/html-catalog-templates/category.php'; + require __DIR__ . '/html-catalog-templates/category.php'; $return = ob_get_contents(); ob_end_clean(); @@ -61,84 +57,81 @@ public function get_category($id) return $return; } - protected function _get_data_for_product( $id ) - { + protected function _get_data_for_product( $id ) { if ( Ecwid_Api_V3::is_available() ) { $api = new Ecwid_Api_V3(); - + $product = Ecwid_Product::get_by_id( $id ); - + $profile = $api->get_store_profile(); - - if (!$profile) { + + if ( ! $profile ) { return null; } - + return (object) array( 'product' => $product, - 'formats' => @$profile->formatsAndUnits + 'formats' => @$profile->formatsAndUnits, ); - } - + } + return null; } - - protected function _get_data_for_category( $id, $offset = 0 ) - { + + protected function _get_data_for_category( $id, $offset = 0 ) { if ( Ecwid_Api_V3::is_available() ) { $api = new Ecwid_Api_V3(); - + $main_category = null; - if ($id > 0) { + if ( $id > 0 ) { $main_category = $api->get_category( $id ); } $get_categories_params = array( - 'parent' => $id + 'parent' => $id, ); - if ($offset && $offset > 0) { + if ( $offset && $offset > 0 ) { $get_categories_params['offset'] = $offset; } $categories = $api->get_categories( $get_categories_params ); - + $get_products_params = array( - 'category' => $id + 'category' => $id, ); - if ($offset) { + if ( $offset ) { $get_products_params['offset'] = $offset; } $products = $api->search_products( $get_products_params ); - + $profile = $api->get_store_profile(); - - if ( is_null( $profile ) || !isset( $categories->items ) || !isset( $products->items ) ) { + + if ( is_null( $profile ) || ! isset( $categories->items ) || ! isset( $products->items ) ) { return null; } - + return (object) array( 'main_category' => $main_category, - 'categories' => $categories->items, - 'products' => $products->items, - 'formats' => @$profile->formatsAndUnits + 'categories' => $categories->items, + 'products' => $products->items, + 'formats' => @$profile->formatsAndUnits, ); - } - + }//end if + return null; } - - public function parse_escaped_fragment($escaped_fragment) - { - $fragment = urldecode($escaped_fragment); - $return = array(); - - if (preg_match('/^(\/~\/)([a-z]+)\/(.*)$/', $fragment, $matches)) { - parse_str($matches[3], $return); + + public function parse_escaped_fragment( $escaped_fragment ) { + $fragment = urldecode( $escaped_fragment ); + $return = array(); + + if ( preg_match( '/^(\/~\/)([a-z]+)\/(.*)$/', $fragment, $matches ) ) { + parse_str( $matches[3], $return ); $return['mode'] = $matches[2]; - } elseif (preg_match('!.*/(p|c)/([0-9]*)!', $fragment, $matches)) { - if (count($matches) == 3 && in_array($matches[1], array('p', 'c'))) { - $return = array( + } elseif ( preg_match( '!.*/(p|c)/([0-9]*)!', $fragment, $matches ) ) { + if ( count( $matches ) == 3 && in_array( $matches[1], array( 'p', 'c' ) ) ) { + $return = array( 'mode' => 'p' == $matches[1] ? 'product' : 'category', - 'id' => $matches[2] + 'id' => $matches[2], ); } } diff --git a/lib/ecwid_platform.php b/lib/ecwid_platform.php index 5bc55274..114bbdbd 100644 --- a/lib/ecwid_platform.php +++ b/lib/ecwid_platform.php @@ -225,12 +225,36 @@ public static function cache_set( $name, $value, $expires_after = 0 ) { 'expires' => $expires_after, ) ); - // self::encode_emoji( $value ); set_transient( 'ecwid_' . $name, $value, $expires_after ); } + public static function encode_fields_with_emoji( $data, $properties = array() ) { + if ( ! is_object( $data ) ) { + return $data; + } + + if ( ! is_array( $properties ) ) { + return $data; + } + + foreach ( $properties as $property ) { + if ( ! empty( $data->$property ) ) { + if ( is_string( $data->$property ) ) { + $data->$property = wp_encode_emoji( $data->$property ); + } else { + self::encode_emoji( $data->$property ); + } + } + } + return $data; + } + public static function encode_emoji( &$item, $key = false ) { //phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed + if ( empty( $item ) ) { + return; + } + if ( is_object( $item ) || is_array( $item ) ) { array_walk_recursive( $item, 'self::encode_emoji' ); } elseif ( is_string( $item ) ) { @@ -376,18 +400,18 @@ public static function reset( $name ) { } public static function store_in_products_cache( $url, $data ) { - self::store_in_cache( $url, 'products', $data, WEEK_IN_SECONDS ); + self::save_in_cache( $url, 'products', $data, WEEK_IN_SECONDS ); } public static function store_in_categories_cache( $url, $data ) { - self::store_in_cache( $url, 'categories', $data, WEEK_IN_SECONDS ); + self::save_in_cache( $url, 'categories', $data, WEEK_IN_SECONDS ); } - public static function store_in_static_pages_cache( $url, $data ) { - self::store_in_cache( $url, 'catalog', $data, WEEK_IN_SECONDS ); + public static function save_in_static_pages_cache( $url, $data ) { + self::save_in_cache( $url, 'catalog', $data, WEEK_IN_SECONDS ); } - protected static function store_in_cache( $url, $type, $data, $expires_after ) { + protected static function save_in_cache( $url, $type, $data, $expires_after ) { $name = self::_build_cache_name( $url, $type ); $to_store = array(