WC Beta Tester - Don't run WC code if WC is not installed (#35742)

* Always check if wc_get_logger is available before calling it.
* Guard against other WooCommerce specific code.
This commit is contained in:
Sam Seay 2022-12-07 07:18:52 +13:00 committed by GitHub
parent d45d7dd64b
commit ef746eae1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 14 deletions

View File

@ -23,18 +23,32 @@ class Tracks_Debug_Log {
*/
private $source = 'tracks';
/**
* Get the logger instance.
*
* @return WC_Logger_Interface|null
*/
private function get_logger() {
// Sometimes between installations of WC versions the logger will not be available.
if ( ! $this->logger && function_exists( 'wc_get_logger' ) ) {
$this->logger = wc_get_logger();
}
return $this->logger;
}
/**
* Initialize hooks.
*/
public function __construct() {
include_once WC_ABSPATH . 'includes/tracks/class-wc-tracks-client.php';
include_once WC_ABSPATH . 'includes/tracks/class-wc-tracks-footer-pixel.php';
// WooCommerce might not be installed/activated between installs of WC versions.
if ( class_exists( 'WooCommerce' ) ) {
include_once WC_ABSPATH . 'includes/tracks/class-wc-tracks-client.php';
include_once WC_ABSPATH . 'includes/tracks/class-wc-tracks-footer-pixel.php';
$logger = wc_get_logger();
$this->logger = $logger;
add_action( 'admin_footer', array( $this, 'log_footer_pixels' ), 5 );
add_action( 'pre_http_request', array( $this, 'log_remote_pixels' ), 10, 3 );
add_action( 'admin_footer', array( $this, 'log_footer_pixels' ), 5 );
add_action( 'pre_http_request', array( $this, 'log_remote_pixels' ), 10, 3 );
}
}
/**
@ -44,16 +58,23 @@ class Tracks_Debug_Log {
* @param array $properties Event properties.
*/
public function log_event( $event_name, $properties ) {
$this->logger->debug(
$event_name,
array( 'source' => $this->source )
);
foreach ( $properties as $key => $property ) {
$this->logger->debug(
" - {$key}: {$property}",
$logger = $this->get_logger();
if ( $logger ) {
$logger->debug(
$event_name,
array( 'source' => $this->source )
);
}
foreach ( $properties as $key => $property ) {
if ( $logger ) {
$logger->debug(
" - {$key}: {$property}",
array( 'source' => $this->source )
);
}
}
}
/**

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Fix a bug where between WC installs the beta tester would crash trying to log.