PluginProbe
Media Cloud Sync / 1.3.10
Media Cloud Sync v1.3.10
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / base / logger.php

logger.php in Media Cloud Sync 1.3.10, at includes/base/logger.php

229 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Dudlewebs\WPMCS;
3
4 defined('ABSPATH') || exit;
5
6 class Logger {
7 private static $instance = null;
8 private $log_dir;
9 private $log_file;
10
11 private function __construct() {
12 $upload_dir = wp_upload_dir();
13 $this->log_dir = trailingslashit($upload_dir['basedir']) . Schema::getConstant('UPLOADS');
14
15 if (!file_exists($this->log_dir)) {
16 wp_mkdir_p($this->log_dir);
17 }
18
19 $this->log_file = $this->log_dir . '/sync-log.json';
20 }
21
22 /**
23 * Singleton instance
24 */
25 public static function instance() {
26 if (self::$instance === null) {
27 self::$instance = new self();
28 }
29 return self::$instance;
30 }
31
32 /**
33 * Add or update a log entry
34 */
35 public function add_log($type, $media_id, $source_type, $log = []) {
36 $entry = [
37 'time' => current_time('mysql'),
38 'type' => $type,
39 'media_id' => $media_id,
40 'source_type' => $source_type,
41 ];
42
43 $this->append_log($entry, $log);
44 }
45
46 /**
47 * Append log using file-based indexing
48 */
49 private function append_log($entry, $log=[]) {
50 $logs = [];
51
52 if (file_exists($this->log_file)) {
53 $content = file_get_contents($this->log_file);
54 $logs = json_decode($content, true) ?: [];
55 }
56
57 // Unique key per media/type/source
58 $key = $entry['media_id'] . '|' . $entry['type'] . '|' . $entry['source_type'];
59
60 if (isset($logs[$key])) {
61 if (!isset($logs[$key]['logs']) || !is_array($logs[$key]['logs'])) {
62 $logs[$key]['logs'] = [];
63 }
64 $logs[$key]['logs'][] = $log;
65 $logs[$key]['time'] = $entry['time'] ?? current_time('mysql');
66 } else {
67 $logs[$key] = $entry;
68 $logs[$key]['logs'][] = $log;
69 }
70
71 file_put_contents($this->log_file, wp_json_encode($logs));
72 }
73
74 /**
75 * Get log by media ID, type, and source type
76 */
77 public function get_log($type, $media_id, $source_type) {
78 if (!file_exists($this->log_file)) return [];
79
80 $key = $media_id . '|' . $type . '|' . $source_type;
81 return json_decode(file_get_contents($this->log_file), true)[$key] ?? [];
82 }
83
84 /**
85 * Get all logs (numeric array, supports pagination)
86 */
87 public function get_all_logs($page = null, $per_page = null) {
88 if (!file_exists($this->log_file)) {
89 return [
90 'logs' => [],
91 'total' => 0,
92 'total_pages' => 0,
93 'page' => 1,
94 'per_page' => $per_page ?:1,
95 ];
96 }
97
98 $logs = array_values(json_decode(file_get_contents($this->log_file), true) ?: []);
99
100 $total = count($logs);
101
102 if ($page === null || $per_page === null) {
103 return [
104 'logs' => $logs,
105 'total' => $total,
106 'total_pages' => 1,
107 'page' => 1,
108 'per_page' => $total,
109 ];
110 }
111
112 $total_pages = (int) ceil($total / $per_page);
113 $page = max(1, (int) $page);
114 $offset = ($page - 1) * $per_page;
115
116 return [
117 'logs' => array_slice($logs, $offset, $per_page),
118 'total' => $total,
119 'total_pages' => $total_pages,
120 'page' => $page,
121 'per_page' => $per_page,
122 ];
123 }
124
125 /**
126 * Get logs by type (supports pagination)
127 */
128 public function get_logs_by_type($type, $page = null, $per_page = null) {
129 if (!file_exists($this->log_file)) {
130 return [
131 'logs' => [],
132 'total' => 0,
133 'total_pages' => 0,
134 'page' => 1,
135 'per_page' => $per_page ?:1,
136 ];
137 };
138
139 $all_logs = json_decode(file_get_contents($this->log_file) ?: '[]', true) ?: [];
140
141 $logs = array_values(array_filter($all_logs, function($log) use ($type) {
142 return isset($log['type']) && $log['type'] === $type;
143 }));
144
145 $total = count($logs);
146
147 if ($page === null || $per_page === null) {
148 return [
149 'logs' => $logs,
150 'total' => $total,
151 'total_pages' => 1,
152 'page' => 1,
153 'per_page' => $total,
154 ];
155 }
156
157
158 $total_pages = (int) ceil($total / $per_page);
159 $page = max(1, (int) $page);
160 $offset = ($page - 1) * $per_page;
161
162 return [
163 'logs' => array_slice($logs, $offset, $per_page),
164 'total' => $total,
165 'total_pages' => $total_pages,
166 'page' => $page,
167 'per_page' => $per_page,
168 ];
169 }
170
171
172 /**
173 * Remove logs by type
174 */
175 public function remove_logs_by_type($type) {
176 if (!file_exists($this->log_file)) return;
177
178 $logs = json_decode(file_get_contents($this->log_file), true) ?: [];
179
180 foreach ($logs as $key => $log) {
181 if (isset($log['type']) && $log['type'] === $type) {
182 unset($logs[$key]);
183 }
184 }
185
186 file_put_contents($this->log_file, wp_json_encode($logs));
187 }
188
189 /**
190 * Remove logs by media ID, type, and source type
191 */
192 public function remove_log($type, $media_id, $source_type) {
193 if (!file_exists($this->log_file)) return;
194
195 $logs = json_decode(file_get_contents($this->log_file), true) ?: [];
196
197 $key = $media_id . '|' . $type . '|' . $source_type;
198
199 if (isset($logs[$key])) {
200 unset($logs[$key]);
201 }
202
203 file_put_contents($this->log_file, wp_json_encode($logs));
204 }
205
206 /**
207 * Remove Log by media ID & source type
208 */
209 public function remove_log_by_media_id($media_id, $source_type) {
210 if (!file_exists($this->log_file)) return;
211
212 $logs = json_decode(file_get_contents($this->log_file), true) ?: [];
213
214 $new_log = array_filter($logs, function($log) use ($media_id, $source_type) {
215 return $log['media_id'] !== $media_id || $log['source_type'] !== $source_type;
216 });
217
218 file_put_contents($this->log_file, wp_json_encode($new_log));
219 }
220
221 /**
222 * Clear all logs
223 */
224 public function clear_logs() {
225 if (file_exists($this->log_file)) {
226 unlink($this->log_file);
227 }
228 }
229 }