Skip to content

Commit

Permalink
PLUGINS-6911
Browse files Browse the repository at this point in the history
  • Loading branch information
meteor-ec committed Jul 9, 2024
1 parent 6b54ea4 commit b73d407
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 93 deletions.
5 changes: 2 additions & 3 deletions ecwid-shopping-cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) {
Expand Down Expand Up @@ -2447,8 +2448,6 @@ function ecwid_get_categories_for_selector() {
}
}
}

$parents = array();

$result = array();
foreach ( $all_categories as $category ) {
Expand Down
10 changes: 7 additions & 3 deletions includes/class-ecwid-static-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -219,9 +219,13 @@ protected static function get_static_snapshot( $url, $dynamic_css = '' ) {
$pattern = '/<img(.*?)>/is';

$fetched_data->htmlCode = preg_replace( $pattern, '<img $1 decoding="async" loading="lazy">', $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 {
Expand All @@ -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
Expand Down
58 changes: 47 additions & 11 deletions lib/ecwid_api_v3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
Expand All @@ -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'] );

Expand Down Expand Up @@ -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'] );

Expand Down Expand Up @@ -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 ) ) {
Expand Down
133 changes: 63 additions & 70 deletions lib/ecwid_catalog.php
Original file line number Diff line number Diff line change
@@ -1,144 +1,137 @@
<?php

class EcwidCatalog
{
var $store_id = 0;
class EcwidCatalog {

var $store_id = 0;
var $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 __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();

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],
);
}
}
Expand Down
Loading

0 comments on commit b73d407

Please sign in to comment.