| @@ -1,101 +1,207 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * Logger. | |
| 4 | - * | |
| 5 | - * @package CartFlows | |
| 6 | - */ | |
| 7 | - | |
| 8 | -/** | |
| 9 | - * Initialization | |
| 10 | - * | |
| 11 | - * @since 1.0.0 | |
| 12 | - */ | |
| 13 | -class Cartflows_Logger { | |
| 14 | - | |
| 15 | - | |
| 16 | - /** | |
| 17 | - * Member Variable | |
| 18 | - * | |
| 19 | - * @var instance | |
| 20 | - */ | |
| 21 | - private static $instance; | |
| 22 | - | |
| 23 | - /** | |
| 24 | - * Member Variable | |
| 25 | - * | |
| 26 | - * @var logger | |
| 27 | - */ | |
| 28 | - public $logger; | |
| 29 | - | |
| 30 | - /** | |
| 31 | - * Initiator | |
| 32 | - */ | |
| 33 | - public static function get_instance() { | |
| 34 | - if ( ! isset( self::$instance ) ) { | |
| 35 | - self::$instance = new self; | |
| 36 | - } | |
| 37 | - return self::$instance; | |
| 38 | - } | |
| 39 | - | |
| 40 | - /** | |
| 41 | - * Constructor | |
| 42 | - */ | |
| 43 | - public function __construct() { | |
| 44 | - | |
| 45 | - /* Load WC Logger */ | |
| 46 | - add_action( 'init', array( $this, 'init_wc_logger' ), 99 ); | |
| 47 | - } | |
| 48 | - | |
| 49 | - /** | |
| 50 | - * Inint Logger. | |
| 51 | - * | |
| 52 | - * @since 1.0.0 | |
| 53 | - */ | |
| 54 | - function init_wc_logger() { | |
| 55 | - $this->logger = new WC_Logger(); | |
| 56 | - } | |
| 57 | - | |
| 58 | - /** | |
| 59 | - * Write log | |
| 60 | - * | |
| 61 | - * @param string $message log message. | |
| 62 | - * @param string $level type of log. | |
| 63 | - * @since 1.0.0 | |
| 64 | - */ | |
| 65 | - function log( $message, $level = 'info' ) { | |
| 66 | - | |
| 67 | - $enable_log = apply_filters( 'cartflows_enable_log', 'enable' ); | |
| 68 | - | |
| 69 | - if ( 'enable' === $enable_log && | |
| 70 | - is_a( $this->logger, 'WC_Logger' ) && | |
| 71 | - did_action( 'plugins_loaded' ) | |
| 72 | - ) { | |
| 73 | - | |
| 74 | - $this->logger->log( $level, $message, array( 'source' => 'cartflows' ) ); | |
| 75 | - } | |
| 76 | - } | |
| 77 | - | |
| 78 | - /** | |
| 79 | - * Write log | |
| 80 | - * | |
| 81 | - * @param string $message log message. | |
| 82 | - * @param string $level type of log. | |
| 83 | - * @since 1.0.0 | |
| 84 | - */ | |
| 85 | - function import_log( $message, $level = 'info' ) { | |
| 86 | - | |
| 87 | - if ( defined( 'WP_DEBUG' ) && | |
| 88 | - WP_DEBUG && | |
| 89 | - is_a( $this->logger, 'WC_Logger' ) && | |
| 90 | - did_action( 'plugins_loaded' ) | |
| 91 | - ) { | |
| 92 | - | |
| 93 | - $this->logger->log( $level, $message, array( 'source' => 'cartflows-import' ) ); | |
| 94 | - } | |
| 95 | - } | |
| 96 | -} | |
| 97 | - | |
| 98 | -/** | |
| 99 | - * Kicking this off by calling 'get_instance()' method | |
| 100 | - */ | |
| 101 | -Cartflows_Logger::get_instance(); | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Logger. | |
| 4 | + * | |
| 5 | + * @package CartFlows | |
| 6 | + */ | |
| 7 | + | |
| 8 | +// Exit if accessed directly. | |
| 9 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 10 | + exit; | |
| 11 | +} | |
| 12 | +/** | |
| 13 | + * Initialization | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + */ | |
| 17 | +class Cartflows_Logger { | |
| 18 | + | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Member Variable | |
| 22 | + * | |
| 23 | + * @var instance | |
| 24 | + */ | |
| 25 | + private static $instance; | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * Member Variable | |
| 29 | + * | |
| 30 | + * @var logger | |
| 31 | + */ | |
| 32 | + public $logger; | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * Initiator | |
| 36 | + */ | |
| 37 | + public static function get_instance() { | |
| 38 | + if ( ! isset( self::$instance ) ) { | |
| 39 | + self::$instance = new self(); | |
| 40 | + } | |
| 41 | + return self::$instance; | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Constructor | |
| 46 | + */ | |
| 47 | + public function __construct() { | |
| 48 | + | |
| 49 | + /* Load WC Logger */ | |
| 50 | + add_action( 'init', array( $this, 'init_wc_logger' ), 99 ); | |
| 51 | + | |
| 52 | + add_action( 'admin_init', array( $this, 'schedule_deletion_of_old_logs' ) ); | |
| 53 | + add_action( 'cartflows_delete_old_log_files', array( $this, 'delete_old_logs_files' ) ); | |
| 54 | + } | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * Init Logger. | |
| 58 | + * | |
| 59 | + * @since 1.0.0 | |
| 60 | + */ | |
| 61 | + public function init_wc_logger() { | |
| 62 | + if ( class_exists( 'CartFlows_WC_Logger' ) ) { | |
| 63 | + $this->logger = new CartFlows_WC_Logger(); | |
| 64 | + } | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Enable log. | |
| 69 | + * | |
| 70 | + * @since 1.7.2 | |
| 71 | + */ | |
| 72 | + public function is_log_enable() { | |
| 73 | + return apply_filters( 'cartflows_enable_log', 'enable' ); | |
| 74 | + } | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * Write log | |
| 78 | + * | |
| 79 | + * @param string $message log message. | |
| 80 | + * @param string $level type of log. | |
| 81 | + * @since 1.0.0 | |
| 82 | + */ | |
| 83 | + public function log( $message, $level = 'info' ) { | |
| 84 | + | |
| 85 | + if ( 'enable' === $this->is_log_enable() && | |
| 86 | + is_a( $this->logger, 'CartFlows_WC_Logger' ) && | |
| 87 | + did_action( 'plugins_loaded' ) | |
| 88 | + ) { | |
| 89 | + | |
| 90 | + $this->logger->log( $level, $message, array( 'source' => 'cartflows' ) ); | |
| 91 | + } | |
| 92 | + } | |
| 93 | + | |
| 94 | + /** | |
| 95 | + * Write log | |
| 96 | + * | |
| 97 | + * @param string $message log message. | |
| 98 | + * @param string $level type of log. | |
| 99 | + * @since 1.0.0 | |
| 100 | + */ | |
| 101 | + public function import_log( $message, $level = 'info' ) { | |
| 102 | + | |
| 103 | + if ( 'enable' === $this->is_log_enable() && defined( 'WP_DEBUG' ) && | |
| 104 | + WP_DEBUG && | |
| 105 | + is_a( $this->logger, 'CartFlows_WC_Logger' ) && | |
| 106 | + did_action( 'plugins_loaded' ) | |
| 107 | + ) { | |
| 108 | + | |
| 109 | + $this->logger->log( $level, $message, array( 'source' => 'cartflows-import' ) ); | |
| 110 | + } | |
| 111 | + } | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * Migration log | |
| 115 | + * | |
| 116 | + * @param string $message migration message. | |
| 117 | + * @param string $level type of log. | |
| 118 | + * @since 1.7.0 | |
| 119 | + */ | |
| 120 | + public function migration_log( $message, $level = 'info' ) { | |
| 121 | + | |
| 122 | + if ( 'enable' === $this->is_log_enable() && defined( 'WP_DEBUG' ) && | |
| 123 | + WP_DEBUG && | |
| 124 | + is_a( $this->logger, 'CartFlows_WC_Logger' ) && | |
| 125 | + did_action( 'plugins_loaded' ) | |
| 126 | + ) { | |
| 127 | + | |
| 128 | + $this->logger->log( $level, $message, array( 'source' => 'cartflows-migration' ) ); | |
| 129 | + } | |
| 130 | + } | |
| 131 | + | |
| 132 | + /** | |
| 133 | + * Sync log | |
| 134 | + * | |
| 135 | + * @param string $message log message. | |
| 136 | + * @param string $level type of log. | |
| 137 | + * @since 1.0.0 | |
| 138 | + */ | |
| 139 | + public function sync_log( $message, $level = 'info' ) { | |
| 140 | + | |
| 141 | + if ( 'enable' === $this->is_log_enable() && defined( 'WP_DEBUG' ) && | |
| 142 | + WP_DEBUG && | |
| 143 | + is_a( $this->logger, 'CartFlows_WC_Logger' ) && | |
| 144 | + did_action( 'plugins_loaded' ) | |
| 145 | + ) { | |
| 146 | + | |
| 147 | + $this->logger->log( $level, $message, array( 'source' => 'cartflows-sync' ) ); | |
| 148 | + } | |
| 149 | + } | |
| 150 | + | |
| 151 | + /** | |
| 152 | + * Schedule the action to delete the CartFlows log files on monthly basis. | |
| 153 | + * | |
| 154 | + * @return void | |
| 155 | + */ | |
| 156 | + public function schedule_deletion_of_old_logs() { | |
| 157 | + | |
| 158 | + if ( ! function_exists( 'as_next_scheduled_action' ) ) { | |
| 159 | + return; | |
| 160 | + } | |
| 161 | + | |
| 162 | + $is_logging_enabled = $this->is_log_enable(); | |
| 163 | + | |
| 164 | + if ( 'enable' === $is_logging_enabled && false === as_next_scheduled_action( 'cartflows_delete_old_log_files' ) ) { | |
| 165 | + | |
| 166 | + $date = new DateTime( 'last day of this month 12am' ); | |
| 167 | + | |
| 168 | + // It will automatically reschedule the action once initiated. | |
| 169 | + as_schedule_recurring_action( $date, MONTH_IN_SECONDS, 'cartflows_delete_old_log_files' ); | |
| 170 | + | |
| 171 | + } elseif ( 'enable' !== $is_logging_enabled && as_next_scheduled_action( 'cartflows_delete_old_log_files' ) ) { | |
| 172 | + as_unschedule_all_actions( 'cartflows_delete_old_log_files' ); | |
| 173 | + } | |
| 174 | + } | |
| 175 | + | |
| 176 | + /** | |
| 177 | + * Delete the CartFlows logs files. | |
| 178 | + * This action will be executed on first day of each month. | |
| 179 | + */ | |
| 180 | + public function delete_old_logs_files() { | |
| 181 | + | |
| 182 | + if ( 'enable' === $this->is_log_enable() ) { | |
| 183 | + | |
| 184 | + $log_status = new CartflowsAdmin\AdminCore\Inc\LogStatus(); | |
| 185 | + $logs = $log_status->get_log_files(); | |
| 186 | + | |
| 187 | + if ( empty( $logs ) || ! is_array( $logs ) ) { | |
| 188 | + return; | |
| 189 | + } | |
| 190 | + | |
| 191 | + foreach ( $logs as $file_name ) { | |
| 192 | + $file_path = CARTFLOWS_LOG_DIR . $file_name; | |
| 193 | + | |
| 194 | + if ( ! file_exists( $file_path ) ) { | |
| 195 | + continue; | |
| 196 | + } | |
| 197 | + | |
| 198 | + wp_delete_file( $file_path ); | |
| 199 | + } | |
| 200 | + } | |
| 201 | + } | |
| 202 | +} | |
| 203 | + | |
| 204 | +/** | |
| 205 | + * Kicking this off by calling 'get_instance()' method | |
| 206 | + */ | |
| 207 | +Cartflows_Logger::get_instance(); | |