PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.1.4
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.1.4
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / classes / class-cartflows-logger.php

class-cartflows-logger.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.1.4, at classes/class-cartflows-logger.php

208 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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();
208