Skip to content

Commit

Permalink
fix for Windows servers
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Jun 20, 2024
1 parent e3a36ec commit 02641db
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 13 deletions.
2 changes: 1 addition & 1 deletion includes/API/Customers_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion includes/API/Orders_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion includes/API/Product_Categories_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion includes/API/Product_Tags_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion includes/API/Product_Variations_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion includes/API/Products_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion includes/API/Taxes_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
29 changes: 29 additions & 0 deletions includes/API/Traits/WCPOS_REST_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
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.6.1",
"version": "1.6.2",
"description": "A simple front-end for taking WooCommerce orders at the Point of Sale.",
"main": "index.js",
"workspaces": {
Expand Down
3 changes: 3 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
10 changes: 5 additions & 5 deletions woocommerce-pos.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="http://wordpress.org/plugins/woocommerce/">WooCommerce</a>.
* Version: 1.6.1
* Version: 1.6.2
* Author: kilbot
* Author URI: http://wcpos.com
* Text Domain: woocommerce-pos
Expand All @@ -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
Expand All @@ -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'
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 ) ) {
Expand Down

0 comments on commit 02641db

Please sign in to comment.