| 1 |
<?php |
| 2 |
/** |
| 3 |
* Logger |
| 4 |
* |
| 5 |
* @package |
| 6 |
*/ |
| 7 |
use WPFunnels\Traits\SingletonTrait; |
| 8 |
|
| 9 |
class Wpfnl_Logger { |
| 10 |
|
| 11 |
use SingletonTrait; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
/** |
| 16 |
* Log location, URL path. |
| 17 |
* |
| 18 |
* @since 2.5.9 |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected $log_location_url = ''; |
| 22 |
|
| 23 |
/** |
| 24 |
* Log location, server path. |
| 25 |
* |
| 26 |
* @since 2.5.9 |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
protected $log_location_file = ''; |
| 30 |
|
| 31 |
/** |
| 32 |
* Log directory location, server path. |
| 33 |
* |
| 34 |
* @since 2.5.9 |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
protected $log_location_dir = ''; |
| 38 |
|
| 39 |
/** |
| 40 |
* The location of the log folder's index file. |
| 41 |
* |
| 42 |
* @since 2.5.9 |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
protected $log_index_file = ''; |
| 46 |
|
| 47 |
/** |
| 48 |
* The logging directory name. |
| 49 |
* |
| 50 |
* @since 2.5.9 |
| 51 |
* @var string |
| 52 |
*/ |
| 53 |
protected $log_file_dir = 'wpfunnels/wpfunnels-logs'; |
| 54 |
|
| 55 |
|
| 56 |
/** |
| 57 |
* Constructor |
| 58 |
* |
| 59 |
* @return void |
| 60 |
* @since 2.5.9 |
| 61 |
*/ |
| 62 |
public function __construct() { |
| 63 |
|
| 64 |
$uploads_dir = wp_upload_dir(); |
| 65 |
$log_file_name = 'wpfunnels'; |
| 66 |
$this->log_location_url = "{$uploads_dir['baseurl']}/{$this->log_file_dir}/{$log_file_name}"; |
| 67 |
$this->log_location_dir = "{$uploads_dir['basedir']}/{$this->log_file_dir}"; |
| 68 |
$this->log_location_file = "{$this->log_location_dir}/{$log_file_name}"; |
| 69 |
$this->log_index_file = "{$this->log_location_dir}/index.php"; |
| 70 |
|
| 71 |
define('WPFNL_LOG_FILE_DIR', $this->log_location_dir ); |
| 72 |
define('WPFNL_LOG_FILE', $this->log_location_file); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
/** |
| 77 |
* Create the log folder. |
| 78 |
* |
| 79 |
* @since 2.5.9 |
| 80 |
*/ |
| 81 |
public function create_log_folder() { |
| 82 |
wp_mkdir_p( $this->log_location_dir ); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
/** |
| 87 |
* Create log file. |
| 88 |
* |
| 89 |
* @param String $log_location_dir |
| 90 |
* @param String $file_name |
| 91 |
* |
| 92 |
* @since 2.5.9 |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
public static function create_log_file( $log_location_dir, $file_name ) { |
| 96 |
if ( ! is_writable( $log_location_dir ) ) { |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
if ( file_exists( $file_name ) ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
touch( $file_name ); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
/** |
| 109 |
* Retrieve logging file location. |
| 110 |
* |
| 111 |
* @return string Logging file location. |
| 112 |
* |
| 113 |
* @since 2.5.9 |
| 114 |
*/ |
| 115 |
public function get_logging_location() { |
| 116 |
return $this->log_location_file; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* Initialize Logging directory |
| 124 |
* |
| 125 |
* @since 2.5.9 |
| 126 |
*/ |
| 127 |
public function initialize_logging() { |
| 128 |
$this->create_log_folder(); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
/** |
| 133 |
* Update log file |
| 134 |
* |
| 135 |
* @param String $log_type |
| 136 |
* @param Mix $content |
| 137 |
* @param String $header_text |
| 138 |
* |
| 139 |
* @since 2.5.9 |
| 140 |
*/ |
| 141 |
public static function modify_log_file( $log_type, $content, $header_text = '' ){ |
| 142 |
|
| 143 |
if( $log_type && $content && defined( 'WPFNL_LOG_FILE' ) && defined( 'WPFNL_LOG_FILE_DIR' ) ){ |
| 144 |
|
| 145 |
$file_name = WPFNL_LOG_FILE.'-'.trim(strtolower($log_type)).'-log-'.date('Y-m-d').'.log'; |
| 146 |
$time = new \DateTimeImmutable('now', wp_timezone()); |
| 147 |
$current_time = $time->format("h:i A"); |
| 148 |
$header_content = "\nWPFunnels - ".$current_time." - ".$header_text."\n"; |
| 149 |
|
| 150 |
if ( !file_exists( $file_name ) ) { |
| 151 |
self::create_log_file( WPFNL_LOG_FILE_DIR, $file_name ); |
| 152 |
} |
| 153 |
|
| 154 |
if ( ! is_writable( $file_name ) ) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
file_put_contents ($file_name, $header_content, FILE_APPEND ); |
| 159 |
file_put_contents ($file_name, $content, FILE_APPEND ); |
| 160 |
|
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
/** |
| 166 |
* Delete existing log files. |
| 167 |
* |
| 168 |
* @since 2.5.9 |
| 169 |
* |
| 170 |
* @return Bool |
| 171 |
*/ |
| 172 |
public static function delete_log_file( $file_name ) { |
| 173 |
if ( file_exists( $file_name ) ) { |
| 174 |
unlink( $file_name ); |
| 175 |
return true; |
| 176 |
} |
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
/** |
| 182 |
* Get all log files in the log directory. |
| 183 |
* |
| 184 |
* @since 2.5.9 |
| 185 |
* @return array |
| 186 |
*/ |
| 187 |
public static function get_log_files() { |
| 188 |
$files = @scandir( WPFNL_LOG_FILE_DIR ); // @codingStandardsIgnoreLine. |
| 189 |
$result = array(); |
| 190 |
if ( ! empty( $files ) ) { |
| 191 |
foreach ( $files as $key => $value ) { |
| 192 |
if ( ! in_array( $value, array( '.', '..' ), true ) ) { |
| 193 |
if ( ! is_dir( $value ) && strstr( $value, '.log' ) ) { |
| 194 |
$result[ sanitize_title( $value ) ] = $value; |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return $result; |
| 201 |
} |
| 202 |
} |
| 203 |
|