PluginProbe
seQura / 3.0.7
seQura v3.0.7
4.3.4 4.3.3 4.3.2 4.3.1 trunk 2.0.0 2.0.10 2.0.11 2.0.12 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0.0 3.0.2 3.0.5 3.0.6 3.0.7 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 4.0.0 All 30 releases
sequra / src / Services / class-log-file.php

class-log-file.php in seQura 3.0.7, at src/Services/class-log-file.php

173 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Log File Interface
4 *
5 * @package SeQura/WC
6 * @subpackage SeQura/WC/Services
7 */
8
9 namespace SeQura\WC\Services;
10
11 use Exception;
12 use Throwable;
13 use WP_Filesystem_Base;
14
15 /**
16 * Provides methods to perform read and write operations on a log file.
17 */
18 class Log_File implements Interface_Log_File {
19
20 private const MAX_LOG_SIZE = 2 * 1024 * 1024; // 2MB
21
22 /**
23 * The path to the log file.
24 *
25 * @var string
26 */
27 private $log_file_path;
28
29 /**
30 * Constructor.
31 *
32 * @param string $log_file_path The path to the log file.
33 */
34 public function __construct( $log_file_path ) {
35 $this->log_file_path = $log_file_path;
36 }
37
38 /**
39 * Append content at the end of the log file.
40 *
41 * @param string $content The content to append.
42 * @throws \Exception If something goes wrong. The exception message will contain the error message.
43 */
44 public function append_content( $content ): void {
45 // WP_Filesystem does not support FILE_APPEND flag, so we need to use file_put_contents.
46 // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_file_put_contents
47 file_put_contents( $this->get_log_file_path(), $content, FILE_APPEND );
48 }
49
50 /**
51 * Get the WP Filesystem object.
52 *
53 * @throws \Exception If something goes wrong. The exception message will contain the error message.
54 */
55 private function get_file_system(): WP_Filesystem_Base {
56 global $wp_filesystem;
57 require_once ABSPATH . '/wp-admin/includes/file.php';
58 if ( ! WP_Filesystem() ) {
59 throw new Exception( 'Could not load WP Filesystem.' );
60 }
61 return $wp_filesystem;
62 }
63
64 /**
65 * Get the content of the log file.
66 *
67 * @param string $store_id The store ID.
68 * @throws \Exception If something goes wrong. The exception message will contain the error message.
69 *
70 * @return string[] Each element is a line of the log file.
71 */
72 public function get_content( $store_id ): array {
73 if ( ! $this->setup() ) {
74 throw new Exception( 'Could not setup log file.' );
75 }
76
77 $wp_filesystem = $this->get_file_system();
78 $content = array();
79 $path = $this->get_log_file_path( $store_id );
80 if ( ! $wp_filesystem->exists( $path ) ) {
81 throw new Exception( 'Could not read log file.' );
82 }
83
84 $content = $wp_filesystem->get_contents_array( $path );
85
86 if ( false === $content ) {
87 throw new Exception( 'Could not read log file.' );
88 }
89
90 return $content;
91 }
92
93 /**
94 * Clear the log file.
95 *
96 * @param string $store_id The store ID.
97 * @throws \Exception If something goes wrong. The exception message will contain the error message.
98 */
99 public function clear( $store_id = null ): void {
100 if ( null === $store_id ) {
101 $store_id = $this->current_store_id();
102 }
103 $log_file_path = $this->get_log_file_path( $store_id );
104 $wp_filesystem = $this->get_file_system();
105 if ( $wp_filesystem->exists( $log_file_path ) && ! $wp_filesystem->delete( $log_file_path, false, 'f' ) ) {
106 throw new Exception( 'Could not clear log file.' );
107 }
108
109 if ( ! $this->setup() ) {
110 throw new Exception( 'Could not setup log file.' );
111 }
112 }
113
114 /**
115 * Make sure the log file exists and is writable.
116 */
117 public function setup(): bool {
118 try {
119 $log_file_path = $this->get_log_file_path();
120 $wp_filesystem = $this->get_file_system();
121
122 if ( ! $wp_filesystem->exists( $log_file_path ) ) {
123 $dir = dirname( $log_file_path );
124 if ( ! $wp_filesystem->is_dir( $dir ) && $wp_filesystem->exists( $dir ) ) {
125 return false;
126 }
127
128 if ( ! $wp_filesystem->exists( $dir ) && ! $wp_filesystem->mkdir( $dir, 0755 ) ) {
129 return false;
130 }
131
132 if ( ! $wp_filesystem->put_contents( $log_file_path, '' ) ) {
133 return false;
134 }
135 }
136
137 if ( ! $wp_filesystem->is_writable( dirname( $log_file_path ) ) ) {
138 return false;
139 }
140
141 // check if log file exceed 2MB and if so, clear it.
142 if ( filesize( $log_file_path ) >= self::MAX_LOG_SIZE ) {
143 $this->clear();
144 return $this->setup();
145 }
146
147 return true;
148 } catch ( Throwable $e ) {
149 return false;
150 }
151 }
152
153 /**
154 * Get the log file path.
155 *
156 * @param string|null $store_id The store ID.
157 */
158 private function get_log_file_path( $store_id = null ): string {
159 $path = $this->log_file_path;
160 if ( null === $store_id ) {
161 $store_id = $this->current_store_id();
162 }
163 return str_replace( '{storeId}', $store_id, $path );
164 }
165
166 /**
167 * Get the current store ID.
168 */
169 private function current_store_id(): string {
170 return (string) \get_current_blog_id();
171 }
172 }
173