Skip to content

Commit

Permalink
fix for 502 server errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed May 5, 2023
1 parent 4890c28 commit b77e342
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 20 additions & 0 deletions includes/API/Product_Variations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
20 changes: 20 additions & 0 deletions includes/API/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
16 changes: 16 additions & 0 deletions includes/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

}

/**
Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
11 changes: 9 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit b77e342

Please sign in to comment.