Skip to content

Commit

Permalink
Merge pull request #321 from Ecwid/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
meteor-ec authored Feb 11, 2025
2 parents ddc9a94 + 4975afc commit a97957d
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 132 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
== Changelog ==
= 6.12.27 - Feb 12, 2024 =
- Fixed an issue in the admin area where the product popup would not work on pages with a custom type.
- Internal improvements and optimizations.

= 6.12.26 - Jan 30, 2024 =
- Improved compatibility with the WPML plugin. Fixed an issue where translation loading for the `sitepress-multilingual-cs` domain was triggered too early on some ecommerce sites.
- Improved compatibility with the WPML plugin. Fixed an issue where translation loading for the "sitepress-multilingual-cs" domain was triggered too early on some ecommerce sites.
- Fixed an issue where refreshing a product page would sometimes redirect users to the main storefront page.
- Internal improvements and optimizations.

Expand Down
10 changes: 9 additions & 1 deletion ecwid-shopping-cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Description: Ecwid by Lightspeed is a free full-featured shopping cart. It can be easily integrated with any Wordpress blog and takes less than 5 minutes to set up.
Text Domain: ecwid-shopping-cart
Author: Ecwid Ecommerce
Version: 6.12.26
Version: 6.12.27
Author URI: https://ecwid.to/ecwid-site
License: GPLv2 or later
*/
Expand Down Expand Up @@ -1696,6 +1696,14 @@ function ecwid_get_update_params_options() {
)
),

Ecwid_Static_Page::OPTION_NEW_IS_ENABLED => array(
'values' => array(
Ecwid_Static_Page::OPTION_VALUE_AUTO,
Ecwid_Static_Page::OPTION_VALUE_ENABLED,
Ecwid_Static_Page::OPTION_VALUE_DISABLED
)
),

Ec_Store_Defer_Init::OPTION_NAME => array(
'values' => array(
Ec_Store_Defer_Init::OPTION_VALUE_AUTO,
Expand Down
3 changes: 2 additions & 1 deletion includes/class-ecwid-product-popup.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public function init_current_screen() {
return;
}

if ( $is_post_screen && ! in_array( $current_screen->post_type, array( 'page', 'post' ) ) ) { //phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
$post_types = get_post_types( array( 'public' => true ) );
if ( $is_post_screen && ! in_array( $current_screen->post_type, $post_types, true ) ) {
return;
}

Expand Down
209 changes: 137 additions & 72 deletions includes/class-ecwid-static-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

class Ecwid_Static_Page {

const OPTION_IS_ENABLED = 'ecwid_static_home_page_enabled';
const OPTION_IS_ENABLED = 'ecwid_static_home_page_enabled';
const OPTION_NEW_IS_ENABLED = 'ecwid_new_static_home_page_enabled';

const OPTION_VALUE_ENABLED = 'Y';
const OPTION_VALUE_DISABLED = 'N';
Expand All @@ -18,6 +19,8 @@ public function __construct() {
if ( ! is_admin() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
}

add_action( 'update_option_' . self::OPTION_NEW_IS_ENABLED, array( $this, 'clear_cache' ), 10, 3 );
}

public function enqueue_scripts() {
Expand Down Expand Up @@ -53,31 +56,19 @@ public static function get_data_for_current_page() {
return $data;
}

protected static function get_endpoint_url( $params = false ) {
protected static function get_endpoint_params() {

if ( ! $params ) {
if ( ecwid_is_applicable_escaped_fragment() ) {
$params = ecwid_parse_escaped_fragment();
} else {
$params = Ecwid_Seo_Links::maybe_extract_html_catalog_params();
}
}
if ( ecwid_is_applicable_escaped_fragment() ) {
$params = ecwid_parse_escaped_fragment();
} else {
$params = Ecwid_Seo_Links::maybe_extract_html_catalog_params();
}

if ( ! isset( $params['mode'] ) ) {
$params['mode'] = 'home';
}

$url = 'https://' . Ecwid_Config::get_static_pages_api_domain() . '/';
$url .= sprintf( '%s-page/', $params['mode'] );
$url .= sprintf( '%s/', get_ecwid_store_id() );

if ( isset( $params['id'] ) ) {
$url .= sprintf( '%s/', $params['id'] );
}

$url .= 'static-code?';

return $url;
return $params;
}

protected static function maybe_fetch_data() {
Expand All @@ -88,7 +79,8 @@ protected static function maybe_fetch_data() {
}

$store_page_params = Ecwid_Store_Page::get_store_page_params();
$endpoint_params = false;
$endpoint_params = false;
$query_params = array();

// for cases of early access to the page if the cache is empty and need to get store block params
if ( empty( $store_page_params ) ) {
Expand All @@ -98,25 +90,23 @@ protected static function maybe_fetch_data() {
}
}

$params = array();

if ( Ecwid_Seo_Links::is_enabled() || ecwid_is_demo_store() ) {
$params['clean_urls'] = 'true';
$query_params['clean_urls'] = 'true';
} else {
$params['clean_urls'] = 'false';
$query_params['clean_urls'] = 'false';
}

$params['base_url'] = get_permalink();
$query_params['base_url'] = get_permalink();

if ( array_key_exists( 'offset', $_GET ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
$params['offset'] = intval( $_GET['offset'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended
$query_params['offset'] = intval( $_GET['offset'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended
}

if ( ! array_key_exists( 'category', $_GET ) && isset( $store_page_params['default_category_id'] ) && intval( $store_page_params['default_category_id'] ) > 0 ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
$params['default_category_id'] = $store_page_params['default_category_id'];
$query_params['default_category_id'] = $store_page_params['default_category_id'];
}

$params['lang'] = self::get_accept_language();
$query_params['lang'] = self::get_accept_language();

$storefront_view_params = array( 'show_root_categories', 'enable_catalog_on_one_page' );
foreach ( $storefront_view_params as $param ) {
Expand All @@ -141,10 +131,10 @@ protected static function maybe_fetch_data() {
}

if ( strpos( $name, 'chameleon' ) !== false ) {
$name = str_replace( 'chameleon_', '', $name );
$params[ 'tplvar_ec.chameleon.' . $name ] = $value;
$name = str_replace( 'chameleon_', '', $name );
$query_params[ 'tplvar_ec.chameleon.' . $name ] = $value;
} else {
$params[ 'tplvar_ec.storefront.' . $name ] = $value;
$query_params[ 'tplvar_ec.storefront.' . $name ] = $value;
}
}
}//end foreach
Expand All @@ -153,96 +143,149 @@ protected static function maybe_fetch_data() {

if ( ! empty( $hreflang_items ) ) {
foreach ( $hreflang_items as $lang => $link ) {
$params[ 'international_pages[' . $lang . ']' ] = $link;
$query_params[ 'international_pages[' . $lang . ']' ] = $link;
}
}

if ( ! empty( $params['default_category_id'] ) ) {
$endpoint_params = array(
'mode' => 'category',
'id' => $params['default_category_id'],
);
}
if ( self::is_need_to_use_new_endpoint() ) {
$query_params['getStaticContent'] = 'true';
$query_params['slug'] = self::get_current_storefront_page_slug();

$url = self::get_endpoint_url( $endpoint_params );
if ( empty( $query_params['slug'] ) ) {
$query_params['storeRootPage'] = 'true';
} else {
$query_params['storeRootPage'] = 'false';
}
} else {
if ( ! empty( $query_params['default_category_id'] ) ) {
$endpoint_params = array(
'mode' => 'category',
'id' => $query_params['default_category_id'],
);
}

foreach ( $params as $name => $value ) {
$url .= $name . '=' . rawurlencode( $value ) . '&';
if( empty( $endpoint_params ) ) {
$endpoint_params = self::get_endpoint_params();
}
}

$url = substr( $url, 0, -1 );

$dynamic_css = '';
if ( ! empty( $_COOKIE['ec_store_dynamic_css'] ) ) {
$dynamic_css = wp_strip_all_tags( $_COOKIE['ec_store_dynamic_css'] ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash
}

$cached_data = EcwidPlatform::get_from_static_pages_cache( $url );
$cache_key = serialize($query_params);

$cached_data = EcwidPlatform::get_from_static_pages_cache( $cache_key );

if ( $cached_data ) {
if ( isset( $cached_data->staticContent ) ) {
$static_content = $cached_data->staticContent;
} else {
$static_content = $cached_data;
}

$is_css_defined = ! empty( $dynamic_css );
$is_css_already_set = in_array( $dynamic_css, $cached_data->cssFiles, true ); //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$is_css_already_set = in_array( $dynamic_css, $static_content->cssFiles, true ); //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$is_home_page = Ecwid_Store_Page::is_store_home_page();

if ( $is_home_page && $is_css_defined && ! $is_css_already_set ) {
$cached_data->cssFiles = array( $dynamic_css ); //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$static_content->cssFiles = array( $dynamic_css ); //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase

EcwidPlatform::save_in_static_pages_cache( $url, $cached_data );
if ( isset( $cached_data->staticContent ) ) {
$cached_data->staticContent = $static_content;
} else {
$cached_data = $static_content;
}

EcwidPlatform::save_in_static_pages_cache( $cache_key, $cached_data );
}

return $cached_data;
return $static_content;
}

$fetched_data = self::get_static_snapshot( $url, $dynamic_css );
$fetched_data = self::get_static_snapshot( $endpoint_params, $query_params, $dynamic_css );

return $fetched_data;
}

protected static function get_static_snapshot( $url, $dynamic_css = '' ) {
protected static function get_current_storefront_page_slug() {
$slug = '';

$fetched_data = EcwidPlatform::fetch_url(
$url,
array(
'timeout' => 3,
'headers' => array(
'ACCEPT-LANGUAGE' => self::get_accept_language(),
),
)
);
$page_id = get_queried_object_id();
$page_link = get_permalink( $page_id );
$page_permalink = wp_make_link_relative( $page_link );

$current_url = add_query_arg( null, null );

$url = str_replace( $page_permalink, '/', $current_url );

if ( preg_match( '/\/([^\/\?]+)/', $url, $matches ) ) {
$slug = $matches[1];
}

return $slug;
}

if ( $fetched_data && isset( $fetched_data['data'] ) ) {
$fetched_data = json_decode( $fetched_data['data'] );
protected static function get_static_snapshot( $endpoint_params, $query_params, $dynamic_css = '' ) {

if ( empty( $fetched_data ) || ! is_object( $fetched_data ) ) {
if ( self::is_need_to_use_new_endpoint() ) {
$api = new Ecwid_Api_V3();
$data = $api->get_storefront_widget_page( $query_params );

if ( empty( $data->staticContent ) || ! is_object( $data->staticContent ) ) { //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
return null;
}
} else {
$api = new Ecwid_Api_V3();
$data = $api->get_static_page( $endpoint_params, $query_params );

if ( empty( $data ) || ! is_object( $data ) ) {
return null;
}
}//end if

if ( ! empty( $data ) ) {
//phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase

if ( ! empty( $data->staticContent ) ) {
$static_content = $data->staticContent;
} else {
$static_content = $data;
}

if ( ! empty( $dynamic_css ) ) {
$fetched_data->cssFiles = array( $dynamic_css );
$static_content->cssFiles = array( $dynamic_css );
}

if ( ! empty( $fetched_data->htmlCode ) ) {
if ( ! empty( $static_content->htmlCode ) ) {
$pattern = '/<img(.*?)>/is';

$fetched_data->htmlCode = preg_replace( $pattern, '<img $1 decoding="async" loading="lazy">', $fetched_data->htmlCode );
$static_content->htmlCode = preg_replace( $pattern, '<img $1 decoding="async" loading="lazy">', $static_content->htmlCode );
}

EcwidPlatform::encode_fields_with_emoji(
$fetched_data,
$static_content,
array( 'htmlCode', 'metaDescriptionHtml', 'ogTagsHtml', 'jsonLDHtml' )
);

if ( isset( $fetched_data->lastUpdated ) ) {
$last_update = substr( $fetched_data->lastUpdated, 0, -3 );
if ( isset( $static_content->lastUpdated ) ) {
$last_update = substr( $static_content->lastUpdated, 0, -3 );
} else {
$last_update = time();
}
//phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase

if ( ! empty( $data->staticContent ) ) {
$data->staticContent = $static_content;
}

$cache_key = serialize($query_params);

EcwidPlatform::invalidate_static_pages_cache_from( $last_update );
EcwidPlatform::save_in_static_pages_cache( $url, $fetched_data );
EcwidPlatform::save_in_static_pages_cache( $cache_key, $data );

return $fetched_data;
return $static_content;
}//end if

return null;
Expand Down Expand Up @@ -408,12 +451,34 @@ public static function is_enabled_static_home_page() {
return false;
}

if ( get_option( self::OPTION_IS_ENABLED ) === '' ) {
if ( get_option( self::OPTION_IS_ENABLED, self::OPTION_VALUE_AUTO ) === self::OPTION_VALUE_AUTO ) {
return true;
}

return false;
}

public static function is_need_to_use_new_endpoint() {
if ( get_option( self::OPTION_NEW_IS_ENABLED ) === self::OPTION_VALUE_ENABLED ) {
return true;
}

if ( get_option( self::OPTION_NEW_IS_ENABLED ) === self::OPTION_VALUE_DISABLED ) {
return false;
}

if ( get_option( self::OPTION_NEW_IS_ENABLED, self::OPTION_VALUE_AUTO ) === '' && get_ecwid_store_id() % 4 === 0 ) {
return true;
}

return false;
}

public function clear_cache( $old_value, $value, $option ) {
if( $old_value !== $value ) {
EcwidPlatform::clear_all_transients();
}
}
}

$__ecwid_static_page = new Ecwid_Static_Page();
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public function render() {

$code .= '<div id="static-ec-store-container">';
$code .= htmlspecialchars_decode( Ecwid_Static_Page::get_html_code() );
$code .= '</div>';

$js_code = Ecwid_Static_Page::get_js_code();
if ( ! empty( $js_code ) ) {
$code .= sprintf( '<!--noptimize--><script id="ec-static-inline-js" data-cfasync="false" data-no-optimize="1" type="text/javascript">%s</script><!--/noptimize-->', $js_code ) . PHP_EOL;
}
$code .= '</div>';

$force_dynamic_js_code = 'if( typeof window.ec.storefront.staticPages != "undefined" && typeof window.ec.storefront.staticPages.forceDynamicLoadingIfRequired != "undefined" ) {
window.ec.storefront.staticPages.forceDynamicLoadingIfRequired();
Expand Down
Loading

0 comments on commit a97957d

Please sign in to comment.