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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 29 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 0 deletions
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`)

0 commit comments

Comments
 (0)