Skip to content

Commit 02641db

Browse files
committed
fix for Windows servers
1 parent e3a36ec commit 02641db

11 files changed

+45
-13
lines changed

includes/API/Customers_Controller.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public function wcpos_get_all_posts( $request ) {
336336
// Collect execution time and server load.
337337
$execution_time = microtime( true ) - $start_time;
338338
$execution_time_ms = number_format( $execution_time * 1000, 2 );
339-
$server_load = sys_getloadavg();
339+
$server_load = $this->get_server_load();
340340

341341
$response = rest_ensure_response( $formatted_results );
342342
$response->header( 'X-WP-Total', (int) $total );

includes/API/Orders_Controller.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ function ( $status ) {
734734
// Collect execution time and server load.
735735
$execution_time = microtime( true ) - $start_time;
736736
$execution_time_ms = number_format( $execution_time * 1000, 2 );
737-
$server_load = sys_getloadavg();
737+
$server_load = $this->get_server_load();
738738

739739
$response = rest_ensure_response( $formatted_results );
740740
$response->header( 'X-WP-Total', (int) $total );

includes/API/Product_Categories_Controller.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ function ( $id ) {
178178
// Collect execution time and server load.
179179
$execution_time = microtime( true ) - $start_time;
180180
$execution_time_ms = number_format( $execution_time * 1000, 2 );
181-
$server_load = sys_getloadavg();
181+
$server_load = $this->get_server_load();
182182

183183
$response = rest_ensure_response( $formatted_results );
184184
$response->header( 'X-WP-Total', (int) $total );

includes/API/Product_Tags_Controller.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ function ( $id ) {
178178
// Collect execution time and server load.
179179
$execution_time = microtime( true ) - $start_time;
180180
$execution_time_ms = number_format( $execution_time * 1000, 2 );
181-
$server_load = sys_getloadavg();
181+
$server_load = $this->get_server_load();
182182

183183
$response = rest_ensure_response( $formatted_results );
184184
$response->header( 'X-WP-Total', (int) $total );

includes/API/Product_Variations_Controller.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ public function wcpos_get_all_posts( $request ) {
453453
// Collect execution time and server load.
454454
$execution_time = microtime( true ) - $start_time;
455455
$execution_time_ms = number_format( $execution_time * 1000, 2 );
456-
$server_load = sys_getloadavg();
456+
$server_load = $this->get_server_load();
457457

458458
$response = rest_ensure_response( $formatted_results );
459459
$response->header( 'X-WP-Total', (int) $total );

includes/API/Products_Controller.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ public function wcpos_get_all_posts( $request ) {
509509
// Collect execution time and server load.
510510
$execution_time = microtime( true ) - $start_time;
511511
$execution_time_ms = number_format( $execution_time * 1000, 2 );
512-
$server_load = sys_getloadavg();
512+
$server_load = $this->get_server_load();
513513

514514
$response = rest_ensure_response( $formatted_results );
515515
$response->header( 'X-WP-Total', (int) $total );

includes/API/Taxes_Controller.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ function ( $item ) {
247247
// Collect execution time and server load.
248248
$execution_time = microtime( true ) - $start_time;
249249
$execution_time_ms = number_format( $execution_time * 1000, 2 );
250-
$server_load = sys_getloadavg();
250+
$server_load = $this->get_server_load();
251251

252252
$response = rest_ensure_response( $formatted_results );
253253
$response->header( 'X-WP-Total', (int) $total );

includes/API/Traits/WCPOS_REST_API.php

+29
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,33 @@ public function wcpos_allow_decimal_quantities() {
9797
// make sure it's true, just in case there's a corrupt setting
9898
return true === $allow_decimal_quantities;
9999
}
100+
101+
/**
102+
* Get server load average.
103+
*
104+
* @return array The load average.
105+
*/
106+
public function get_server_load() {
107+
try {
108+
if ( stristr( PHP_OS, 'win' ) ) {
109+
// Use WMIC to get load percentage from Windows.
110+
$load = @shell_exec( 'wmic cpu get loadpercentage /all' );
111+
if ( $load ) {
112+
$load = explode( "\n", $load );
113+
if ( isset( $load[1] ) ) {
114+
$load = intval( $load[1] );
115+
return array( $load, $load, $load ); // Mimic the array structure of sys_getloadavg().
116+
}
117+
}
118+
} elseif ( function_exists( 'sys_getloadavg' ) ) {
119+
return sys_getloadavg();
120+
}
121+
} catch ( Exception $e ) {
122+
// Log the error for debugging purposes.
123+
Logger::log( 'Error getting server load: ' . $e->getMessage() );
124+
}
125+
126+
// Fallback if no method is available or an error occurs.
127+
return array( 0, 0, 0 );
128+
}
100129
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@wcpos/woocommerce-pos",
3-
"version": "1.6.1",
3+
"version": "1.6.2",
44
"description": "A simple front-end for taking WooCommerce orders at the Point of Sale.",
55
"main": "index.js",
66
"workspaces": {

readme.txt

+3
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ There is more information on our website at [https://wcpos.com](https://wcpos.co
8080

8181
== Changelog ==
8282

83+
= 1.6.2 - 2024/06/20 =
84+
- Fix: Error preventing resources (products, orders, customers, etc) from loading on Windows servers
85+
8386
= 1.6.1 - 2024/06/18 =
8487
- Enhancement: Changed the way POS Only and Online Only products are managed
8588
- Moved from using postmeta (`_pos_visibility`) to using centralized settings in the options table (`woocommerce_pos_settings_visibility`)

woocommerce-pos.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: WooCommerce POS
44
* Plugin URI: https://wordpress.org/plugins/woocommerce-pos/
55
* Description: A simple front-end for taking WooCommerce orders at the Point of Sale. Requires <a href="http://wordpress.org/plugins/woocommerce/">WooCommerce</a>.
6-
* Version: 1.6.1
6+
* Version: 1.6.2
77
* Author: kilbot
88
* Author URI: http://wcpos.com
99
* Text Domain: woocommerce-pos
@@ -14,7 +14,7 @@
1414
* Tested up to: 6.5
1515
* Requires PHP: 7.4
1616
* Requires Plugins: woocommerce
17-
* WC tested up to: 8.9
17+
* WC tested up to: 9.0
1818
* WC requires at least: 5.3
1919
*
2020
* @see http://wcpos.com
@@ -24,7 +24,7 @@
2424
namespace WCPOS\WooCommercePOS;
2525

2626
// Define plugin constants.
27-
const VERSION = '1.6.1';
27+
const VERSION = '1.6.2';
2828
const PLUGIN_NAME = 'woocommerce-pos';
2929
const SHORT_NAME = 'wcpos';
3030
\define( __NAMESPACE__ . '\PLUGIN_FILE', plugin_basename( __FILE__ ) ); // 'woocommerce-pos/woocommerce-pos.php'
@@ -37,7 +37,7 @@
3737
const MIN_PRO_VERSION = '1.5.0';
3838

3939
// Load .env flags (for development).
40-
function load_env( $file ) {
40+
function wcpos_load_env( $file ) {
4141
if ( ! file_exists( $file ) ) {
4242
return;
4343
}
@@ -75,7 +75,7 @@ function wcpos_load_autoloaders() {
7575
wcpos_load_autoloaders();
7676

7777
// Environment variables.
78-
load_env( __DIR__ . '/.env' );
78+
wcpos_load_env( __DIR__ . '/.env' );
7979

8080
// Error handling for autoload failure.
8181
if ( ! class_exists( \WCPOS\WooCommercePOS\Activator::class ) || ! class_exists( \WCPOS\WooCommercePOS\Deactivator::class ) ) {

0 commit comments

Comments
 (0)