PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.4.3
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.4.3
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / Core / Importer / Utils / LogHandler.php

LogHandler.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.4.3, at includes/Core/Importer/Utils/LogHandler.php

107 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Templately\Core\Importer\Utils;
3
4 use Templately\Core\Importer\FullSiteImport;
5
6 class LogHandler {
7 public static function get_log_dir() {
8 $upload_dir = wp_upload_dir();
9 $log_dir = trailingslashit($upload_dir['basedir']) . 'templately' . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR;
10 return $log_dir;
11 }
12
13 public static function get_log_file_path() {
14 $session_id = Utils::get_session_id();
15 $log_dir = self::get_log_dir();
16 return trailingslashit($log_dir) . "fsi-$session_id.log";
17 }
18
19 public static function create_log_dir() {
20 $upload_dir = wp_upload_dir();
21 $log_dir = self::get_log_dir();
22
23 if (!$log_dir) {
24 return false;
25 }
26
27 if (!is_writable($upload_dir['basedir'])) {
28 return false;
29 }
30
31 $data = ['log_type' => 'file'];
32 Utils::update_session_data_by_id($data);
33
34 if (!is_dir($log_dir)) {
35 wp_mkdir_p($log_dir);
36 } else {
37 $files = array_diff(scandir($log_dir), ['.', '..']);
38
39 if (count($files) > 10) {
40 usort($files, function($a, $b) use ($log_dir) {
41 return filemtime($log_dir . '/' . $a) - filemtime($log_dir . '/' . $b);
42 });
43
44 $files_to_delete = array_slice($files, 0, count($files) - 10);
45
46 foreach ($files_to_delete as $file) {
47 unlink($log_dir . '/' . $file);
48 }
49 }
50 }
51 }
52
53 public static function sse_log_file($log) {
54 $file_path = self::get_log_file_path();
55
56 // Write to the log file
57 return file_put_contents($file_path, json_encode($log) . PHP_EOL, FILE_APPEND);
58 }
59
60 public static function read_log_file($start_line = 0) {
61 $file_path = self::get_log_file_path();
62
63 $log = [];
64
65 // Check if SplFileObject exists
66 if (class_exists('SplFileObject')) {
67 try {
68 $file = new \SplFileObject($file_path);
69
70 // Seek to the specified line number (0-indexed in SplFileObject)
71 $file->seek($start_line);
72 while (!$file->eof()) {
73 $line = trim($file->current());
74 if ($line) {
75 $log[] = json_decode($line, true); // JSON decode each line
76 }
77 $file->next();
78 }
79 } catch (\Exception $e) {
80 // Handle exception if file operations fail
81 error_log("Failed to read log file: " . $e->getMessage());
82 }
83 } else {
84 // Fallback if SplFileObject doesn't exist
85 $current_line = 1;
86 $handle = fopen($file_path, "r");
87 if ($handle) {
88 while (($line = fgets($handle)) !== false) {
89 if ($current_line >= $start_line) {
90 $line = trim($line);
91 if ($line) {
92 $log[] = json_decode($line, true); // JSON decode each line
93 }
94 }
95 $current_line++;
96 }
97 fclose($handle);
98 } else {
99 // Handle error if file can't be opened
100 error_log("Failed to open log file: $file_path");
101 }
102 }
103
104 return $log;
105 }
106 }
107