Files
IQ-Dynamic-Google-Pricing/includes/class-informatiq-sp-logger.php
Malin d1f3607895 feat: Migrate to Google Merchant API and remove Composer dependencies
- Fix ISE 500 error on plugin activation:
  - Defer wc_get_logger() call in Logger class (lazy initialization)
  - Move plugin init to plugins_loaded hook with priority 20
  - Remove invalid Google_Service_ShoppingContent_Reports instantiation

- Migrate from deprecated Content API to new Merchant API:
  - Rewrite Google API class with direct REST calls
  - Implement JWT authentication using PHP OpenSSL
  - Use WordPress wp_remote_* functions for HTTP requests
  - Support Price Competitiveness reports for competitor pricing

- Remove all Composer dependencies:
  - Delete vendor/ directory (~50 packages)
  - Delete composer.json and composer.lock
  - Remove setup scripts and related documentation

- Plugin is now fully self-contained with no external dependencies

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-21 08:29:25 +01:00

223 lines
4.7 KiB
PHP

<?php
/**
* Logger class for tracking price updates and errors.
*
* @package InformatiqSmartPricing
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Logger class.
*/
class Informatiq_SP_Logger {
/**
* WC Logger instance.
*
* @var WC_Logger
*/
private $wc_logger;
/**
* Log source.
*
* @var string
*/
private $source = 'informatiq-smart-pricing';
/**
* Database table name.
*
* @var string
*/
private $table_name;
/**
* Constructor.
*/
public function __construct() {
global $wpdb;
$this->table_name = $wpdb->prefix . 'informatiq_sp_logs';
}
/**
* Get WC Logger instance (lazy initialization).
*
* @return WC_Logger|null
*/
private function get_wc_logger() {
if ( null === $this->wc_logger && function_exists( 'wc_get_logger' ) ) {
$this->wc_logger = wc_get_logger();
}
return $this->wc_logger;
}
/**
* Create log table.
*/
public function create_log_table() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS {$this->table_name} (
id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
product_id bigint(20) UNSIGNED NOT NULL,
product_name varchar(255) NOT NULL,
old_price decimal(10,2) DEFAULT NULL,
new_price decimal(10,2) NOT NULL,
competitor_price decimal(10,2) NOT NULL,
message text,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY product_id (product_id),
KEY created_at (created_at)
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}
/**
* Log info message.
*
* @param string $message Message to log.
* @param array $context Additional context.
*/
public function info( $message, $context = array() ) {
$logger = $this->get_wc_logger();
if ( $logger ) {
$logger->info( $message, array_merge( array( 'source' => $this->source ), $context ) );
}
}
/**
* Log error message.
*
* @param string $message Message to log.
* @param array $context Additional context.
*/
public function error( $message, $context = array() ) {
$logger = $this->get_wc_logger();
if ( $logger ) {
$logger->error( $message, array_merge( array( 'source' => $this->source ), $context ) );
}
}
/**
* Log warning message.
*
* @param string $message Message to log.
* @param array $context Additional context.
*/
public function warning( $message, $context = array() ) {
$logger = $this->get_wc_logger();
if ( $logger ) {
$logger->warning( $message, array_merge( array( 'source' => $this->source ), $context ) );
}
}
/**
* Log price update.
*
* @param int $product_id Product ID.
* @param string $product_name Product name.
* @param float $old_price Old price.
* @param float $new_price New price.
* @param float $competitor_price Competitor price.
* @param string $message Optional message.
* @return bool
*/
public function log_price_update( $product_id, $product_name, $old_price, $new_price, $competitor_price, $message = '' ) {
global $wpdb;
$result = $wpdb->insert(
$this->table_name,
array(
'product_id' => $product_id,
'product_name' => $product_name,
'old_price' => $old_price,
'new_price' => $new_price,
'competitor_price' => $competitor_price,
'message' => $message,
'created_at' => current_time( 'mysql' ),
),
array( '%d', '%s', '%f', '%f', '%f', '%s', '%s' )
);
// Also log to WC logger.
$this->info(
sprintf(
'Price updated for product #%d (%s): %s → %s (competitor: %s)',
$product_id,
$product_name,
wc_price( $old_price ),
wc_price( $new_price ),
wc_price( $competitor_price )
)
);
return false !== $result;
}
/**
* Get today's logs.
*
* @return array
*/
public function get_todays_logs() {
global $wpdb;
$today = current_time( 'Y-m-d' );
return $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$this->table_name}
WHERE DATE(created_at) = %s
ORDER BY created_at DESC",
$today
)
);
}
/**
* Get recent logs.
*
* @param int $limit Number of logs to retrieve.
* @return array
*/
public function get_recent_logs( $limit = 100 ) {
global $wpdb;
return $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$this->table_name}
ORDER BY created_at DESC
LIMIT %d",
$limit
)
);
}
/**
* Clear old logs.
*
* @param int $days Number of days to keep.
* @return int Number of rows deleted.
*/
public function clear_old_logs( $days = 30 ) {
global $wpdb;
return $wpdb->query(
$wpdb->prepare(
"DELETE FROM {$this->table_name}
WHERE created_at < DATE_SUB(NOW(), INTERVAL %d DAY)",
$days
)
);
}
}