| @@ -4,8 +4,12 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * @package CartFlows |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | +// Exit if accessed directly. | |
| 9 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 10 | + exit; | |
| 11 | +} | |
| 8 | 12 | /** |
| 9 | 13 | * Initialization |
| 10 | 14 | * |
| 11 | 15 | * @since 1.0.0 |
| @@ -31,9 +35,9 @@ | ||
| 31 | 35 | * Initiator |
| 32 | 36 | */ |
| 33 | 37 | public static function get_instance() { |
| 34 | 38 | if ( ! isset( self::$instance ) ) { |
| 35 | - self::$instance = new self; | |
| 39 | + self::$instance = new self(); | |
| 36 | 40 | } |
| 37 | 41 | return self::$instance; |
| 38 | 42 | } |
| 39 | 43 | |
| @@ -43,20 +47,34 @@ | ||
| 43 | 47 | public function __construct() { |
| 44 | 48 | |
| 45 | 49 | /* Load WC Logger */ |
| 46 | 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' ) ); | |
| 47 | 54 | } |
| 48 | 55 | |
| 49 | 56 | /** |
| 50 | - * Inint Logger. | |
| 57 | + * Init Logger. | |
| 51 | 58 | * |
| 52 | 59 | * @since 1.0.0 |
| 53 | 60 | */ |
| 54 | - function init_wc_logger() { | |
| 55 | - $this->logger = new WC_Logger(); | |
| 61 | + public function init_wc_logger() { | |
| 62 | + if ( class_exists( 'CartFlows_WC_Logger' ) ) { | |
| 63 | + $this->logger = new CartFlows_WC_Logger(); | |
| 64 | + } | |
| 56 | 65 | } |
| 57 | 66 | |
| 58 | 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 | + /** | |
| 59 | 77 | * Write log |
| 60 | 78 | * |
| 61 | 79 | * @param string $message log message. |
| 62 | 80 | * @param string $level type of log. |
| @@ -61,14 +79,12 @@ | ||
| 61 | 79 | * @param string $message log message. |
| 62 | 80 | * @param string $level type of log. |
| 63 | 81 | * @since 1.0.0 |
| 64 | 82 | */ |
| 65 | - function log( $message, $level = 'info' ) { | |
| 83 | + public function log( $message, $level = 'info' ) { | |
| 66 | 84 | |
| 67 | - $enable_log = apply_filters( 'cartflows_enable_log', 'enable' ); | |
| 68 | - | |
| 69 | - if ( 'enable' === $enable_log && | |
| 70 | - is_a( $this->logger, 'WC_Logger' ) && | |
| 85 | + if ( 'enable' === $this->is_log_enable() && | |
| 86 | + is_a( $this->logger, 'CartFlows_WC_Logger' ) && | |
| 71 | 87 | did_action( 'plugins_loaded' ) |
| 72 | 88 | ) { |
| 73 | 89 | |
| 74 | 90 | $this->logger->log( $level, $message, array( 'source' => 'cartflows' ) ); |
| @@ -81,17 +97,107 @@ | ||
| 81 | 97 | * @param string $message log message. |
| 82 | 98 | * @param string $level type of log. |
| 83 | 99 | * @since 1.0.0 |
| 84 | 100 | */ |
| 85 | - function import_log( $message, $level = 'info' ) { | |
| 101 | + public function import_log( $message, $level = 'info' ) { | |
| 86 | 102 | |
| 87 | - if ( defined( 'WP_DEBUG' ) && | |
| 103 | + if ( 'enable' === $this->is_log_enable() && defined( 'WP_DEBUG' ) && | |
| 88 | 104 | WP_DEBUG && |
| 89 | - is_a( $this->logger, 'WC_Logger' ) && | |
| 105 | + is_a( $this->logger, 'CartFlows_WC_Logger' ) && | |
| 90 | 106 | did_action( 'plugins_loaded' ) |
| 91 | 107 | ) { |
| 92 | 108 | |
| 93 | 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 | + } | |
| 94 | 200 | } |
| 95 | 201 | } |
| 96 | 202 | } |
| 97 | 203 | |