PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.6.1
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.6.1
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
← All changes | classes/Plugin/Logging.php +77 -40 2.0.12.6.1 View file →
@@ -17,18 +17,11 @@
17 17 */
18 18 const LOG_FILE_PREFIX = 'content-control-';
19 19
20 20 /**
21 - * Container.
22 - *
23 - * @var \ContentControl\Base\Container
24 - */
25 - private $c;
26 -
27 - /**
28 21 * Whether the log file is writable.
29 22 *
30 - * @var bool
23 + * @var bool|null
31 24 */
32 25 private $is_writable;
33 26
34 27 /**
@@ -47,9 +40,9 @@
47 40
48 41 /**
49 42 * File system API.
50 43 *
51 - * @var WP_Filesystem_Base
44 + * @var \WP_Filesystem_Base|null
52 45 */
53 46 private $fs;
54 47
55 48 /**
@@ -54,20 +47,16 @@
54 47
55 48 /**
56 49 * Log file content.
57 50 *
58 - * @var string
51 + * @var string|null
59 52 */
60 53 private $content;
61 54
62 55 /**
63 56 * Initialize logging.
64 - *
65 - * @param \ContentControl\Base\Container $c Container.
66 57 */
67 - public function __construct( $c ) {
68 - $this->c = $c;
69 -
58 + public function __construct() {
70 59 $this->init();
71 60
72 61 $this->register_hooks();
73 62 }
@@ -73,8 +62,10 @@
73 62 }
74 63
75 64 /**
76 65 * Register hooks.
66 + *
67 + * @return void
77 68 */
78 69 public function register_hooks() {
79 70 // On shutdown, save the log file.
80 71 add_action( 'shutdown', [ $this, 'save_logs' ] );
@@ -82,17 +73,17 @@
82 73
83 74 /**
84 75 * Gets the Uploads directory
85 76 *
86 - * @return bool|array An associated array with baseurl and basedir or false on failure
77 + * @return bool|array{path: string, url: string, subdir: string, basedir: string, baseurl: string, error: string|false} An associated array with baseurl and basedir or false on failure
87 78 */
88 79 public function get_upload_dir() {
89 - if ( defined( '\IS_WPCOM' ) && \IS_WPCOM ) {
90 - $wp_upload_dir = wp_get_upload_dir();
91 - } else {
92 - $wp_upload_dir = wp_upload_dir();
93 - }
80 + // Used if you only need to fetch data, not create missing folders.
81 + $wp_upload_dir = wp_get_upload_dir();
94 82
83 + // phpcs:ignore Squiz.PHP.CommentedOutCode.Found
84 + // $wp_upload_dir = wp_upload_dir(); // Disable this on IS_WPCOM if used.
85 +
95 86 if ( isset( $wp_upload_dir['error'] ) && false !== $wp_upload_dir['error'] ) {
96 87 return false;
97 88 } else {
98 89 return $wp_upload_dir;
@@ -126,15 +117,17 @@
126 117 *
127 118 * @return bool
128 119 */
129 120 public function enabled() {
130 - return ! defined( 'CONTENT_CONTROL_DISABLE_LOGGING' ) || ! CONTENT_CONTROL_DISABLE_LOGGING && $this->is_writable();
121 + $disabled = defined( '\CONTENT_CONTROL_DISABLE_LOGGING' ) && true === \CONTENT_CONTROL_DISABLE_LOGGING;
122 +
123 + return ! $disabled && $this->is_writable();
131 124 }
132 125
133 126 /**
134 127 * Get working WP Filesystem instance
135 128 *
136 - * @return WP_Filesystem_Base|false
129 + * @return \WP_Filesystem_Base|false
137 130 */
138 131 public function fs() {
139 132 if ( isset( $this->fs ) ) {
140 133 return $this->fs;
@@ -167,13 +160,20 @@
167 160 if ( isset( $this->is_writable ) ) {
168 161 return $this->is_writable;
169 162 }
170 163
171 - $this->is_writable = false !== $this->fs() && 'direct' === $this->fs()->method;
164 + $file_system = $this->fs();
172 165
166 + if ( false === $file_system ) {
167 + $this->is_writable = false;
168 + return $this->is_writable;
169 + }
170 +
171 + $this->is_writable = 'direct' === $file_system->method;
172 +
173 173 $upload_dir = $this->get_upload_dir();
174 174
175 - if ( ! $this->fs()->is_writable( $upload_dir['basedir'] ) ) {
175 + if ( ! $file_system->is_writable( $upload_dir['basedir'] ) ) {
176 176 $this->is_writable = false;
177 177 }
178 178
179 179 return $this->is_writable;
@@ -180,15 +180,22 @@
180 180 }
181 181
182 182 /**
183 183 * Get things started
184 + *
185 + * @return void
184 186 */
185 187 public function init() {
186 - $upload_dir = $this->get_upload_dir();
188 + $upload_dir = $this->get_upload_dir();
189 + $file_system = $this->fs();
187 190
191 + if ( false === $upload_dir || false === $file_system ) {
192 + return;
193 + }
194 +
188 195 $file_token = \get_option( 'content_control_debug_log_token' );
189 196 if ( false === $file_token ) {
190 - $file_token = uniqid( wp_rand(), true );
197 + $file_token = uniqid( (string) wp_rand(), true );
191 198 \update_option( 'content_control_debug_log_token', $file_token );
192 199 }
193 200
194 201 $this->filename = self::LOG_FILE_PREFIX . "debug-{$file_token}.log"; // ex. content-control-debug-5c2f6a9b9b5a3.log.
@@ -193,9 +200,9 @@
193 200
194 201 $this->filename = self::LOG_FILE_PREFIX . "debug-{$file_token}.log"; // ex. content-control-debug-5c2f6a9b9b5a3.log.
195 202 $this->file = trailingslashit( $upload_dir['basedir'] ) . $this->filename;
196 203
197 - if ( ! $this->fs()->exists( $this->file ) ) {
204 + if ( ! $file_system->exists( $this->file ) ) {
198 205 $this->setup_new_log();
199 206 } else {
200 207 $this->content = $this->get_file( $this->file );
201 208 }
@@ -200,9 +207,9 @@
200 207 $this->content = $this->get_file( $this->file );
201 208 }
202 209
203 210 // Truncate long log files.
204 - if ( $this->fs()->exists( $this->file ) && $this->fs()->size( $this->file ) >= 1048576 ) {
211 + if ( $file_system->exists( $this->file ) && $file_system->size( $this->file ) >= 1048576 ) {
205 212 $this->truncate_log();
206 213 }
207 214 }
208 215
@@ -217,9 +224,9 @@
217 224
218 225 /**
219 226 * Retrieves the url to the file
220 227 *
221 - * @returns string|bool The url to the file or false on failure
228 + * @return string|bool The url to the file or false on failure
222 229 */
223 230 public function get_file_url() {
224 231 if ( ! $this->enabled() ) {
225 232 return false;
@@ -230,9 +237,9 @@
230 237
231 238 /**
232 239 * Retrieve the log data
233 240 *
234 - * @return string
241 + * @return false|string
235 242 */
236 243 public function get_log() {
237 244 return $this->get_log_content();
238 245 }
@@ -242,9 +249,15 @@
242 249 *
243 250 * @return void
244 251 */
245 252 public function delete_logs() {
246 - $this->fs->delete( $this->file );
253 + $file_system = $this->fs();
254 +
255 + if ( false === $file_system ) {
256 + return;
257 + }
258 +
259 + $file_system->delete( $this->file );
247 260 \delete_option( 'content_control_debug_log_token' );
248 261 }
249 262
250 263 /**
@@ -250,8 +263,10 @@
250 263 /**
251 264 * Log message to file
252 265 *
253 266 * @param string $message The message to log.
267 + *
268 + * @return void
254 269 */
255 270 public function log( $message = '' ) {
256 271 $this->write_to_log( wp_date( 'Y-n-d H:i:s' ) . ' - ' . $message );
257 272 }
@@ -259,8 +274,10 @@
259 274 /**
260 275 * Log unique message to file.
261 276 *
262 277 * @param string $message The unique message to log.
278 + *
279 + * @return void
263 280 */
264 281 public function log_unique( $message = '' ) {
265 282 $contents = $this->get_log_content();
266 283
@@ -273,9 +290,9 @@
273 290
274 291 /**
275 292 * Get the log file contents.
276 293 *
277 - * @return string
294 + * @return false|string
278 295 */
279 296 public function get_log_content() {
280 297 if ( ! isset( $this->content ) ) {
281 298 $this->content = $this->get_file();
@@ -303,21 +320,23 @@
303 320 * Retrieve the contents of a file.
304 321 *
305 322 * @param string|boolean $file File to get contents of.
306 323 *
307 - * @return string
324 + * @return false|string
308 325 */
309 326 protected function get_file( $file = false ) {
310 327 $file = $file ? $file : $this->file;
311 328
312 - if ( ! $this->enabled() ) {
329 + $file_system = $this->fs();
330 +
331 + if ( false === $file_system || ! $this->enabled() ) {
313 332 return '';
314 333 }
315 334
316 335 $content = '';
317 336
318 - if ( $this->fs()->exists( $file ) ) {
319 - $content = $this->fs()->get_contents( $file );
337 + if ( $file_system->exists( $file ) ) {
338 + $content = $file_system->get_contents( $file );
320 339 }
321 340
322 341 return $content;
323 342 }
@@ -325,8 +344,10 @@
325 344 /**
326 345 * Write the log message
327 346 *
328 347 * @param string $message The message to write.
348 + *
349 + * @return void
329 350 */
330 351 protected function write_to_log( $message = '' ) {
331 352 if ( ! $this->enabled() ) {
332 353 return;
@@ -343,15 +364,19 @@
343 364 }
344 365
345 366 /**
346 367 * Save the current contents to file.
368 + *
369 + * @return void
347 370 */
348 371 public function save_logs() {
349 - if ( ! $this->enabled() ) {
372 + $file_system = $this->fs();
373 +
374 + if ( false === $file_system || ! $this->enabled() ) {
350 375 return;
351 376 }
352 377
353 - $this->fs()->put_contents( $this->file, $this->content, FS_CHMOD_FILE );
378 + $file_system->put_contents( $this->file, $this->content, FS_CHMOD_FILE );
354 379 }
355 380
356 381 /**
357 382 * Get a line count.
@@ -366,8 +391,10 @@
366 391 }
367 392
368 393 /**
369 394 * Truncates a log file to maximum of 250 lines.
395 + *
396 + * @return void
370 397 */
371 398 public function truncate_log() {
372 399 $content = $this->get_log_content();
373 400 $lines = explode( "\r\n", $content );
@@ -386,13 +413,21 @@
386 413 }
387 414
388 415 /**
389 416 * Delete the log file.
417 + *
418 + * @return void
390 419 */
391 420 public function clear_log() {
421 + $file_system = $this->fs();
422 +
423 + if ( false === $file_system ) {
424 + return;
425 + }
426 +
392 427 // Delete the file.
393 428 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
394 - @$this->fs()->delete( $this->file );
429 + @$file_system->delete( $this->file );
395 430
396 431 if ( $this->enabled() ) {
397 432 $this->setup_new_log();
398 433 }
@@ -403,8 +438,10 @@
403 438 *
404 439 * @param string $func_name Function name.
405 440 * @param string $version Versoin deprecated.
406 441 * @param string $replacement Replacement function (optional).
442 + *
443 + * @return void
407 444 */
408 445 public function log_deprecated_notice( $func_name, $version, $replacement = null ) {
409 446 if ( ! is_null( $replacement ) ) {
410 447 $notice = sprintf( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $func_name, $version, $replacement );