PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.6.2
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.6.2
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.6.2, at includes/Core/Importer/Utils/LogHandler.php

108 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 = SessionData::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 if ($session_id = SessionData::get_session_id()) {
32 SessionData::set($session_id, 'log_type', 'file');
33 }
34
35 if (!is_dir($log_dir)) {
36 wp_mkdir_p($log_dir);
37 } else {
38 $files = array_diff(scandir($log_dir), ['.', '..']);
39
40 if (count($files) > 10) {
41 usort($files, function($a, $b) use ($log_dir) {
42 return filemtime($log_dir . '/' . $a) - filemtime($log_dir . '/' . $b);
43 });
44
45 $files_to_delete = array_slice($files, 0, count($files) - 10);
46
47 foreach ($files_to_delete as $file) {
48 unlink($log_dir . '/' . $file);
49 }
50 }
51 }
52 }
53
54 public static function sse_log_file($log) {
55 $file_path = self::get_log_file_path();
56
57 // Write to the log file
58 return file_put_contents($file_path, json_encode($log) . PHP_EOL, FILE_APPEND);
59 }
60
61 public static function read_log_file($start_line = 0) {
62 $file_path = self::get_log_file_path();
63
64 $log = [];
65
66 // Check if SplFileObject exists
67 if (class_exists('SplFileObject')) {
68 try {
69 $file = new \SplFileObject($file_path);
70
71 // Seek to the specified line number (0-indexed in SplFileObject)
72 $file->seek($start_line);
73 while (!$file->eof()) {
74 $line = trim($file->current());
75 if ($line) {
76 $log[] = json_decode($line, true); // JSON decode each line
77 }
78 $file->next();
79 }
80 } catch (\Exception $e) {
81 // Handle exception if file operations fail
82 error_log("Failed to read log file: " . $e->getMessage());
83 }
84 } else {
85 // Fallback if SplFileObject doesn't exist
86 $current_line = 1;
87 $handle = fopen($file_path, "r");
88 if ($handle) {
89 while (($line = fgets($handle)) !== false) {
90 if ($current_line >= $start_line) {
91 $line = trim($line);
92 if ($line) {
93 $log[] = json_decode($line, true); // JSON decode each line
94 }
95 }
96 $current_line++;
97 }
98 fclose($handle);
99 } else {
100 // Handle error if file can't be opened
101 error_log("Failed to open log file: $file_path");
102 }
103 }
104
105 return $log;
106 }
107 }
108