initial
This commit is contained in:
+78
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Logging\Debug;
|
||||
|
||||
use Gravity_Forms\Gravity_SMTP\Apps\Config\Tools_Config;
|
||||
use Gravity_Forms\Gravity_SMTP\Data_Store\Data_Store_Router;
|
||||
use Gravity_Forms\Gravity_SMTP\Gravity_SMTP;
|
||||
use Gravity_Forms\Gravity_SMTP\Logging\Endpoints\View_Log_Endpoint;
|
||||
use Gravity_Forms\Gravity_SMTP\Logging\Logging_Service_Provider;
|
||||
|
||||
class Debug_Log_Event_Handler {
|
||||
|
||||
const DEBUG_LOG_ENABLE_TIME = 'debug_log_enable_time';
|
||||
|
||||
/**
|
||||
* @var Debug_Logger
|
||||
*/
|
||||
protected $debug_logger;
|
||||
|
||||
/**
|
||||
* @var Data_Store_Router
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
public function __construct( Debug_Logger $debug_logger, Data_Store_Router $data ) {
|
||||
$this->debug_logger = $debug_logger;
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function on_setting_update( $setting, $value ) {
|
||||
if ( $setting !== Tools_Config::SETTING_DEBUG_LOG_ENABLED ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Delete verification key when disabled.
|
||||
if ( $value === false || $value === 0 || $value === '0' || $value === 'false' ) {
|
||||
delete_option( View_Log_Endpoint::OPTION_VERIFICATION_KEY );
|
||||
delete_option( self::DEBUG_LOG_ENABLE_TIME );
|
||||
|
||||
$delete_log_on_deactivate = apply_filters( 'gravitysmtp_delete_log_on_deactivate', false );
|
||||
|
||||
if ( $delete_log_on_deactivate ) {
|
||||
$this->debug_logger->delete_log();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Refresh verficiation key when enabled.
|
||||
$bytes = random_bytes( 12 );
|
||||
$random = bin2hex( $bytes );
|
||||
|
||||
update_option( View_Log_Endpoint::OPTION_VERIFICATION_KEY, $random );
|
||||
update_option( self::DEBUG_LOG_ENABLE_TIME, time() );
|
||||
}
|
||||
|
||||
public function on_retention_cron() {
|
||||
$enabled = get_option( self::DEBUG_LOG_ENABLE_TIME );
|
||||
|
||||
if ( empty( $enabled ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$period_in_days = $this->data->get_plugin_setting( Tools_Config::SETTING_DEBUG_LOG_RETENTION, 7 );
|
||||
|
||||
$now = date_create( 'now' );
|
||||
$enabled = date_create( date( 'Y-m-d H:i:s', $enabled ) );
|
||||
|
||||
$interval = date_diff( $now, $enabled );
|
||||
$diff = $interval->format( '%a' );
|
||||
|
||||
if ( (int) $diff < (int) $period_in_days ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->debug_logger->delete_log();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Logging\Debug;
|
||||
|
||||
use Gravity_Forms\Gravity_SMTP\Apps\Config\Tools_Config;
|
||||
use Gravity_Forms\Gravity_SMTP\Connectors\Connector_Service_Provider;
|
||||
use Gravity_Forms\Gravity_SMTP\Gravity_SMTP;
|
||||
use Gravity_Forms\Gravity_SMTP\Logging\Logging_Service_Provider;
|
||||
use Gravity_Forms\Gravity_Tools\Logging\DB_Logging_Provider;
|
||||
use Gravity_Forms\Gravity_Tools\Logging\Logger;
|
||||
use Gravity_Forms\Gravity_Tools\Logging\Logging_Provider;
|
||||
|
||||
class Debug_Logger extends Logger {
|
||||
|
||||
protected $log_level;
|
||||
|
||||
protected $priority_map = array(
|
||||
'debug' => DB_Logging_Provider::DEBUG,
|
||||
'info' => DB_Logging_Provider::INFO,
|
||||
'warning' => DB_Logging_Provider::WARN,
|
||||
'error' => DB_Logging_Provider::ERROR,
|
||||
'fatal' => DB_Logging_Provider::FATAL,
|
||||
);
|
||||
|
||||
public function __construct( Logging_Provider $provider, $log_level ) {
|
||||
parent::__construct( $provider );
|
||||
$this->log_level = $log_level;
|
||||
}
|
||||
|
||||
public static function log_message( $message, $priority ) {
|
||||
$container = Gravity_SMTP::$container;
|
||||
/**
|
||||
* @var self $logger
|
||||
*/
|
||||
$logger = $container->get( Logging_Service_Provider::DEBUG_LOGGER );
|
||||
|
||||
$logger->log( $message, $priority );
|
||||
}
|
||||
|
||||
public function should_log( $log_level = 'all' ) {
|
||||
$container = Gravity_SMTP::container();
|
||||
$data = $container->get( Connector_Service_Provider::DATA_STORE_ROUTER );
|
||||
$enabled = $data->get_plugin_setting( Tools_Config::SETTING_DEBUG_LOG_ENABLED, false );
|
||||
|
||||
if ( $enabled === false || $enabled === 0 || $enabled === '0' || $enabled === 'false' ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( $log_level === 'all' ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$level_prio = $this->map_priority_string( $log_level );
|
||||
|
||||
return $level_prio >= $this->log_level;
|
||||
}
|
||||
|
||||
public function delete_log() {
|
||||
$this->provider->delete_log();
|
||||
}
|
||||
|
||||
public function map_priority_string( $priority_string ) {
|
||||
if ( isset( $this->priority_map[ $priority_string ] ) ) {
|
||||
return $this->priority_map[ $priority_string ];
|
||||
}
|
||||
|
||||
return DB_Logging_Provider::DEBUG;
|
||||
}
|
||||
|
||||
public function get_log_items() {
|
||||
return $this->provider->get_lines();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Logging\Debug;
|
||||
|
||||
use Gravity_Forms\Gravity_Tools\Logging\Logger;
|
||||
|
||||
class Null_Logger extends Logger {
|
||||
|
||||
protected function should_log() {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function delete_log() {
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_SMTP\Logging\Debug;
|
||||
|
||||
use Gravity_Forms\Gravity_Tools\Logging\Logging_Provider;
|
||||
|
||||
class Null_Logging_Provider implements Logging_Provider {
|
||||
|
||||
public function log_info( $line ) {
|
||||
return;
|
||||
}
|
||||
|
||||
public function log_debug( $line ) {
|
||||
return;
|
||||
}
|
||||
|
||||
public function log_warning( $line ) {
|
||||
return;
|
||||
}
|
||||
|
||||
public function log_error( $line ) {
|
||||
return;
|
||||
}
|
||||
|
||||
public function log_fatal( $line ) {
|
||||
return;
|
||||
}
|
||||
|
||||
public function log( $line, $priority ) {
|
||||
return;
|
||||
}
|
||||
|
||||
public function delete_log() {
|
||||
return;
|
||||
}
|
||||
|
||||
public function write_line_to_log( $line ) {
|
||||
return;
|
||||
}
|
||||
|
||||
public function get_lines() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user