2025-12-23 07:48:45 +01:00
|
|
|
<?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;
|
2026-01-21 08:29:25 +01:00
|
|
|
$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;
|
2025-12-23 07:48:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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() ) {
|
2026-01-21 08:29:25 +01:00
|
|
|
$logger = $this->get_wc_logger();
|
|
|
|
|
if ( $logger ) {
|
|
|
|
|
$logger->info( $message, array_merge( array( 'source' => $this->source ), $context ) );
|
|
|
|
|
}
|
2025-12-23 07:48:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Log error message.
|
|
|
|
|
*
|
|
|
|
|
* @param string $message Message to log.
|
|
|
|
|
* @param array $context Additional context.
|
|
|
|
|
*/
|
|
|
|
|
public function error( $message, $context = array() ) {
|
2026-01-21 08:29:25 +01:00
|
|
|
$logger = $this->get_wc_logger();
|
|
|
|
|
if ( $logger ) {
|
|
|
|
|
$logger->error( $message, array_merge( array( 'source' => $this->source ), $context ) );
|
|
|
|
|
}
|
2025-12-23 07:48:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Log warning message.
|
|
|
|
|
*
|
|
|
|
|
* @param string $message Message to log.
|
|
|
|
|
* @param array $context Additional context.
|
|
|
|
|
*/
|
|
|
|
|
public function warning( $message, $context = array() ) {
|
2026-01-21 08:29:25 +01:00
|
|
|
$logger = $this->get_wc_logger();
|
|
|
|
|
if ( $logger ) {
|
|
|
|
|
$logger->warning( $message, array_merge( array( 'source' => $this->source ), $context ) );
|
|
|
|
|
}
|
2025-12-23 07:48:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|