From 6bc2244ec98cd1e183ac9dfa27f18e1c1a1d7ae3 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Tue, 17 Sep 2024 22:31:55 +0800 Subject: [PATCH] Reduce dependency of remote logging on WC_Tracks (#51365) * Simplify WC_Tracks::get_blog_details() to reduce its dependencies * Add changelog * Remove store_id field * Improve the check --- .../enhance-remote-logging-reduce-dependency | 4 ++++ .../src/Internal/Logging/RemoteLogger.php | 21 +++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) create mode 100644 plugins/woocommerce/changelog/enhance-remote-logging-reduce-dependency diff --git a/plugins/woocommerce/changelog/enhance-remote-logging-reduce-dependency b/plugins/woocommerce/changelog/enhance-remote-logging-reduce-dependency new file mode 100644 index 00000000000..2a0eca872f0 --- /dev/null +++ b/plugins/woocommerce/changelog/enhance-remote-logging-reduce-dependency @@ -0,0 +1,4 @@ +Significance: patch +Type: enhancement + +Reduce dependency of remote logging on WC_Tracks diff --git a/plugins/woocommerce/src/Internal/Logging/RemoteLogger.php b/plugins/woocommerce/src/Internal/Logging/RemoteLogger.php index 4e9bd7432b6..2f8e3d5ab8a 100644 --- a/plugins/woocommerce/src/Internal/Logging/RemoteLogger.php +++ b/plugins/woocommerce/src/Internal/Logging/RemoteLogger.php @@ -8,6 +8,7 @@ use Automattic\WooCommerce\Utilities\StringUtil; use Automattic\WooCommerce\Internal\McStats; use WC_Rate_Limiter; use WC_Log_Levels; +use Jetpack_Options; /** * WooCommerce Remote Logger @@ -72,9 +73,16 @@ class RemoteLogger extends \WC_Log_Handler { 'php_version' => phpversion(), 'wp_version' => get_bloginfo( 'version' ), 'request_uri' => $this->sanitize_request_uri( filter_input( INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL ) ), + 'store_id' => get_option( \WC_Install::STORE_ID_OPTION, null ), ), ); + $blog_id = class_exists( 'Jetpack_Options' ) ? Jetpack_Options::get_option( 'id' ) : null; + + if ( ! empty( $blog_id ) && is_int( $blog_id ) ) { + $log_data['blog_id'] = $blog_id; + } + if ( isset( $context['backtrace'] ) ) { if ( is_array( $context['backtrace'] ) || is_string( $context['backtrace'] ) ) { $log_data['trace'] = $this->sanitize_trace( $context['backtrace'] ); @@ -89,19 +97,6 @@ class RemoteLogger extends \WC_Log_Handler { unset( $context['tags'] ); } - if ( class_exists( '\WC_Tracks' ) && function_exists( 'wp_get_current_user' ) ) { - $user = wp_get_current_user(); - $blog_details = \WC_Tracks::get_blog_details( $user->ID ); - - if ( is_numeric( $blog_details['blog_id'] ) && $blog_details['blog_id'] > 0 ) { - $log_data['blog_id'] = $blog_details['blog_id']; - } - - if ( ! empty( $blog_details['store_id'] ) ) { - $log_data['properties']['store_id'] = $blog_details['store_id']; - } - } - if ( isset( $context['error'] ) && is_array( $context['error'] ) && ! empty( $context['error']['file'] ) ) { $context['error']['file'] = $this->sanitize( $context['error']['file'] ); }