From 02641dbdae75b0df95452de501b6d402c988df8a Mon Sep 17 00:00:00 2001 From: Paul Kilmurray Date: Thu, 20 Jun 2024 08:36:46 +0100 Subject: [PATCH] fix for Windows servers --- includes/API/Customers_Controller.php | 2 +- includes/API/Orders_Controller.php | 2 +- .../API/Product_Categories_Controller.php | 2 +- includes/API/Product_Tags_Controller.php | 2 +- .../API/Product_Variations_Controller.php | 2 +- includes/API/Products_Controller.php | 2 +- includes/API/Taxes_Controller.php | 2 +- includes/API/Traits/WCPOS_REST_API.php | 29 +++++++++++++++++++ package.json | 2 +- readme.txt | 3 ++ woocommerce-pos.php | 10 +++---- 11 files changed, 45 insertions(+), 13 deletions(-) diff --git a/includes/API/Customers_Controller.php b/includes/API/Customers_Controller.php index 9c98c07..ae72bd2 100644 --- a/includes/API/Customers_Controller.php +++ b/includes/API/Customers_Controller.php @@ -336,7 +336,7 @@ public function wcpos_get_all_posts( $request ) { // Collect execution time and server load. $execution_time = microtime( true ) - $start_time; $execution_time_ms = number_format( $execution_time * 1000, 2 ); - $server_load = sys_getloadavg(); + $server_load = $this->get_server_load(); $response = rest_ensure_response( $formatted_results ); $response->header( 'X-WP-Total', (int) $total ); diff --git a/includes/API/Orders_Controller.php b/includes/API/Orders_Controller.php index 834b723..550ca20 100644 --- a/includes/API/Orders_Controller.php +++ b/includes/API/Orders_Controller.php @@ -734,7 +734,7 @@ function ( $status ) { // Collect execution time and server load. $execution_time = microtime( true ) - $start_time; $execution_time_ms = number_format( $execution_time * 1000, 2 ); - $server_load = sys_getloadavg(); + $server_load = $this->get_server_load(); $response = rest_ensure_response( $formatted_results ); $response->header( 'X-WP-Total', (int) $total ); diff --git a/includes/API/Product_Categories_Controller.php b/includes/API/Product_Categories_Controller.php index fd11c63..93e5ed8 100644 --- a/includes/API/Product_Categories_Controller.php +++ b/includes/API/Product_Categories_Controller.php @@ -178,7 +178,7 @@ function ( $id ) { // Collect execution time and server load. $execution_time = microtime( true ) - $start_time; $execution_time_ms = number_format( $execution_time * 1000, 2 ); - $server_load = sys_getloadavg(); + $server_load = $this->get_server_load(); $response = rest_ensure_response( $formatted_results ); $response->header( 'X-WP-Total', (int) $total ); diff --git a/includes/API/Product_Tags_Controller.php b/includes/API/Product_Tags_Controller.php index e19983a..fc2b947 100644 --- a/includes/API/Product_Tags_Controller.php +++ b/includes/API/Product_Tags_Controller.php @@ -178,7 +178,7 @@ function ( $id ) { // Collect execution time and server load. $execution_time = microtime( true ) - $start_time; $execution_time_ms = number_format( $execution_time * 1000, 2 ); - $server_load = sys_getloadavg(); + $server_load = $this->get_server_load(); $response = rest_ensure_response( $formatted_results ); $response->header( 'X-WP-Total', (int) $total ); diff --git a/includes/API/Product_Variations_Controller.php b/includes/API/Product_Variations_Controller.php index aae77fb..25874b2 100644 --- a/includes/API/Product_Variations_Controller.php +++ b/includes/API/Product_Variations_Controller.php @@ -453,7 +453,7 @@ public function wcpos_get_all_posts( $request ) { // Collect execution time and server load. $execution_time = microtime( true ) - $start_time; $execution_time_ms = number_format( $execution_time * 1000, 2 ); - $server_load = sys_getloadavg(); + $server_load = $this->get_server_load(); $response = rest_ensure_response( $formatted_results ); $response->header( 'X-WP-Total', (int) $total ); diff --git a/includes/API/Products_Controller.php b/includes/API/Products_Controller.php index cc8fa20..cf33dee 100644 --- a/includes/API/Products_Controller.php +++ b/includes/API/Products_Controller.php @@ -509,7 +509,7 @@ public function wcpos_get_all_posts( $request ) { // Collect execution time and server load. $execution_time = microtime( true ) - $start_time; $execution_time_ms = number_format( $execution_time * 1000, 2 ); - $server_load = sys_getloadavg(); + $server_load = $this->get_server_load(); $response = rest_ensure_response( $formatted_results ); $response->header( 'X-WP-Total', (int) $total ); diff --git a/includes/API/Taxes_Controller.php b/includes/API/Taxes_Controller.php index a8c9e88..4b8c1ca 100644 --- a/includes/API/Taxes_Controller.php +++ b/includes/API/Taxes_Controller.php @@ -247,7 +247,7 @@ function ( $item ) { // Collect execution time and server load. $execution_time = microtime( true ) - $start_time; $execution_time_ms = number_format( $execution_time * 1000, 2 ); - $server_load = sys_getloadavg(); + $server_load = $this->get_server_load(); $response = rest_ensure_response( $formatted_results ); $response->header( 'X-WP-Total', (int) $total ); diff --git a/includes/API/Traits/WCPOS_REST_API.php b/includes/API/Traits/WCPOS_REST_API.php index 0a60935..12bbb5c 100644 --- a/includes/API/Traits/WCPOS_REST_API.php +++ b/includes/API/Traits/WCPOS_REST_API.php @@ -97,4 +97,33 @@ public function wcpos_allow_decimal_quantities() { // make sure it's true, just in case there's a corrupt setting return true === $allow_decimal_quantities; } + + /** + * Get server load average. + * + * @return array The load average. + */ + public function get_server_load() { + try { + if ( stristr( PHP_OS, 'win' ) ) { + // Use WMIC to get load percentage from Windows. + $load = @shell_exec( 'wmic cpu get loadpercentage /all' ); + if ( $load ) { + $load = explode( "\n", $load ); + if ( isset( $load[1] ) ) { + $load = intval( $load[1] ); + return array( $load, $load, $load ); // Mimic the array structure of sys_getloadavg(). + } + } + } elseif ( function_exists( 'sys_getloadavg' ) ) { + return sys_getloadavg(); + } + } catch ( Exception $e ) { + // Log the error for debugging purposes. + Logger::log( 'Error getting server load: ' . $e->getMessage() ); + } + + // Fallback if no method is available or an error occurs. + return array( 0, 0, 0 ); + } } diff --git a/package.json b/package.json index 3eca2f5..1f3b508 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@wcpos/woocommerce-pos", - "version": "1.6.1", + "version": "1.6.2", "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 0bb473e..e73b294 100644 --- a/readme.txt +++ b/readme.txt @@ -80,6 +80,9 @@ There is more information on our website at [https://wcpos.com](https://wcpos.co == Changelog == += 1.6.2 - 2024/06/20 = +- Fix: Error preventing resources (products, orders, customers, etc) from loading on Windows servers + = 1.6.1 - 2024/06/18 = - Enhancement: Changed the way POS Only and Online Only products are managed - Moved from using postmeta (`_pos_visibility`) to using centralized settings in the options table (`woocommerce_pos_settings_visibility`) diff --git a/woocommerce-pos.php b/woocommerce-pos.php index d99a061..100fb52 100644 --- a/woocommerce-pos.php +++ b/woocommerce-pos.php @@ -3,7 +3,7 @@ * Plugin Name: WooCommerce POS * Plugin URI: https://wordpress.org/plugins/woocommerce-pos/ * Description: A simple front-end for taking WooCommerce orders at the Point of Sale. Requires WooCommerce. - * Version: 1.6.1 + * Version: 1.6.2 * Author: kilbot * Author URI: http://wcpos.com * Text Domain: woocommerce-pos @@ -14,7 +14,7 @@ * Tested up to: 6.5 * Requires PHP: 7.4 * Requires Plugins: woocommerce - * WC tested up to: 8.9 + * WC tested up to: 9.0 * WC requires at least: 5.3 * * @see http://wcpos.com @@ -24,7 +24,7 @@ namespace WCPOS\WooCommercePOS; // Define plugin constants. -const VERSION = '1.6.1'; +const VERSION = '1.6.2'; const PLUGIN_NAME = 'woocommerce-pos'; const SHORT_NAME = 'wcpos'; \define( __NAMESPACE__ . '\PLUGIN_FILE', plugin_basename( __FILE__ ) ); // 'woocommerce-pos/woocommerce-pos.php' @@ -37,7 +37,7 @@ const MIN_PRO_VERSION = '1.5.0'; // Load .env flags (for development). -function load_env( $file ) { +function wcpos_load_env( $file ) { if ( ! file_exists( $file ) ) { return; } @@ -75,7 +75,7 @@ function wcpos_load_autoloaders() { wcpos_load_autoloaders(); // Environment variables. -load_env( __DIR__ . '/.env' ); +wcpos_load_env( __DIR__ . '/.env' ); // Error handling for autoload failure. if ( ! class_exists( \WCPOS\WooCommercePOS\Activator::class ) || ! class_exists( \WCPOS\WooCommercePOS\Deactivator::class ) ) {