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

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