From b77e342aa6d6f7e2ab68d6c8afd4b8c8bb1f28eb Mon Sep 17 00:00:00 2001 From: Paul Kilmurray Date: Fri, 5 May 2023 20:52:58 +0100 Subject: [PATCH] fix for 502 server errors --- composer.json | 2 +- includes/API/Product_Variations.php | 20 ++++++++++++++++++++ includes/API/Products.php | 20 ++++++++++++++++++++ includes/Init.php | 16 ++++++++++++++++ package.json | 2 +- readme.txt | 11 +++++++++-- 6 files changed, 67 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index c369c00..dc6f763 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "woocommerce/woocommerce-sniffs": "^0.1.3", "wp-coding-standards/wpcs": "2.3.0", "wp-phpunit/wp-phpunit": "6.1.1", - "yoast/phpunit-polyfills": "^1.0.4" + "yoast/phpunit-polyfills": "^1.0.5" }, "require": { "php": ">=7.2.0", diff --git a/includes/API/Product_Variations.php b/includes/API/Product_Variations.php index e5ea108..4790c6f 100644 --- a/includes/API/Product_Variations.php +++ b/includes/API/Product_Variations.php @@ -8,6 +8,7 @@ use WP_REST_Request; use WP_REST_Response; use WC_Product_Query; +use WCPOS\WooCommercePOS\Logger; class Product_Variations { private $request; @@ -52,6 +53,25 @@ public function product_response( WP_REST_Response $response, WC_Data $product, $barcode_field = woocommerce_pos_get_settings( 'general', 'barcode_field' ); $data['barcode'] = $product->get_meta( $barcode_field ); + /** + * Truncate the product description + */ + $max_length = 100; + $plain_text_description = wp_strip_all_tags( $data['description'], true ); + if ( strlen( $plain_text_description ) > $max_length ) { + $truncated_description = substr( $plain_text_description, 0, $max_length - 3 ) . '...'; + $data['description'] = $truncated_description; + } + + /** + * Check the response size and log a debug message if it is over the maximum size. + */ + $response_size = strlen( serialize( $response->data ) ); + $max_response_size = 10000; + if ( $response_size > $max_response_size ) { + Logger::log( "Variation ID {$product->get_id()} has a response size of {$response_size} bytes, exceeding the limit of {$max_response_size} bytes." ); + } + /** * Reset the new response data */ diff --git a/includes/API/Products.php b/includes/API/Products.php index 5c330d4..7a88521 100644 --- a/includes/API/Products.php +++ b/includes/API/Products.php @@ -4,6 +4,7 @@ use Ramsey\Uuid\Uuid; use WC_Data; +use WCPOS\WooCommercePOS\Logger; use WP_Query; use WP_REST_Request; use WP_REST_Response; @@ -86,6 +87,25 @@ public function product_response( WP_REST_Response $response, WC_Data $product, $barcode_field = woocommerce_pos_get_settings( 'general', 'barcode_field' ); $data['barcode'] = $product->get_meta( $barcode_field ); + /** + * Truncate the product description + */ + $max_length = 100; + $plain_text_description = wp_strip_all_tags( $data['description'], true ); + if ( strlen( $plain_text_description ) > $max_length ) { + $truncated_description = substr( $plain_text_description, 0, $max_length - 3 ) . '...'; + $data['description'] = $truncated_description; + } + + /** + * Check the response size and log a debug message if it is over the maximum size. + */ + $response_size = strlen( serialize( $response->data ) ); + $max_response_size = 10000; + if ( $response_size > $max_response_size ) { + Logger::log( "Product ID {$product->get_id()} has a response size of {$response_size} bytes, exceeding the limit of {$max_response_size} bytes." ); + } + /** * Reset the new response data */ diff --git a/includes/Init.php b/includes/Init.php index f9de27c..032e3f3 100644 --- a/includes/Init.php +++ b/includes/Init.php @@ -36,6 +36,8 @@ public function __construct() { // Hack - remove when possible add_filter( 'woocommerce_rest_shop_order_schema', array( $this, 'shop_order_schema' ), 10, 1 ); + add_filter( 'option_wpseo', array( $this, 'remove_wpseo_rest_api_links' ), 10, 1 ); + } /** @@ -149,4 +151,18 @@ private function integrations(): void { // new Integrations\Bookings(); // } } + + /** + * Yoast SEO adds SEO to the WC REST API by default, this adds to the download weight and can cause problems + * It is programmatically turned off here for POS requests + * This gets loaded and cached before the rest_api init hook, so we can't use the filter + */ + public function remove_wpseo_rest_api_links ( $wpseo_options ) { + if ( woocommerce_pos_request() ) { + $wpseo_options['remove_rest_api_links'] = true; + $wpseo_options['enable_headless_rest_endpoints'] = false; + return $wpseo_options; + } + return $wpseo_options; + } } diff --git a/package.json b/package.json index c57d6bd..d3f66a5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@wcpos/woocommerce-pos", - "version": "1.0.0", + "version": "1.0.1", "description": "A simple front-end for taking WooCommerce orders at the Point of Sale.", "main": "index.js", "workspaces": { diff --git a/readme.txt b/readme.txt index 08f2517..6e0a8a8 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: kilbot Tags: cart, e-commerce, ecommerce, inventory, point-of-sale, pos, sales, sell, shop, shopify, store, vend, woocommerce, wordpress-ecommerce Requires at least: 5.6 & WooCommerce 5.3 Tested up to: 6.2 -Stable tag: 1.0.0 +Stable tag: 1.0.1 License: GPL-3.0 License URI: http://www.gnu.org/licenses/gpl-3.0.html @@ -63,7 +63,14 @@ There is more information on our website at [https://wcpos.com](https://wcpos.co == Changelog == -= 1.0.0 = += 1.0.1 = 2023/05/05 +* Fix: Product and Variations not showing in POS + Description: The WC REST API response can be too large in some cases causing a 502 server error. + - Product and Variation descriptions are not truncated to 100 characters for the POS to reduce response size. + - Yoast SEO is now programmatically disabled for the POS to reduce response size. + - A message is now logged when the WC REST API product or variation response is too large. + += 1.0.0 = 2023/05/03 * Complete rewrite of the plugin with improved functionality and performance. * Although extensive testing has been done, there may still be bugs. * We recommend updating only when you have time to deal with potential issues.