| @@ -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; |
| @@ -187,8 +180,10 @@ | ||
| 187 | 180 | } |
| 188 | 181 | |
| 189 | 182 | /** |
| 190 | 183 | * Get things started |
| 184 | + * | |
| 185 | + * @return void | |
| 191 | 186 | */ |
| 192 | 187 | public function init() { |
| 193 | 188 | $upload_dir = $this->get_upload_dir(); |
| 194 | 189 | $file_system = $this->fs(); |
| @@ -198,9 +193,9 @@ | ||
| 198 | 193 | } |
| 199 | 194 | |
| 200 | 195 | $file_token = \get_option( 'content_control_debug_log_token' ); |
| 201 | 196 | if ( false === $file_token ) { |
| 202 | - $file_token = uniqid( wp_rand(), true ); | |
| 197 | + $file_token = uniqid( (string) wp_rand(), true ); | |
| 203 | 198 | \update_option( 'content_control_debug_log_token', $file_token ); |
| 204 | 199 | } |
| 205 | 200 | |
| 206 | 201 | $this->filename = self::LOG_FILE_PREFIX . "debug-{$file_token}.log"; // ex. content-control-debug-5c2f6a9b9b5a3.log. |
| @@ -229,9 +224,9 @@ | ||
| 229 | 224 | |
| 230 | 225 | /** |
| 231 | 226 | * Retrieves the url to the file |
| 232 | 227 | * |
| 233 | - * @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 | |
| 234 | 229 | */ |
| 235 | 230 | public function get_file_url() { |
| 236 | 231 | if ( ! $this->enabled() ) { |
| 237 | 232 | return false; |
| @@ -242,9 +237,9 @@ | ||
| 242 | 237 | |
| 243 | 238 | /** |
| 244 | 239 | * Retrieve the log data |
| 245 | 240 | * |
| 246 | - * @return string | |
| 241 | + * @return false|string | |
| 247 | 242 | */ |
| 248 | 243 | public function get_log() { |
| 249 | 244 | return $this->get_log_content(); |
| 250 | 245 | } |
| @@ -268,8 +263,10 @@ | ||
| 268 | 263 | /** |
| 269 | 264 | * Log message to file |
| 270 | 265 | * |
| 271 | 266 | * @param string $message The message to log. |
| 267 | + * | |
| 268 | + * @return void | |
| 272 | 269 | */ |
| 273 | 270 | public function log( $message = '' ) { |
| 274 | 271 | $this->write_to_log( wp_date( 'Y-n-d H:i:s' ) . ' - ' . $message ); |
| 275 | 272 | } |
| @@ -277,8 +274,10 @@ | ||
| 277 | 274 | /** |
| 278 | 275 | * Log unique message to file. |
| 279 | 276 | * |
| 280 | 277 | * @param string $message The unique message to log. |
| 278 | + * | |
| 279 | + * @return void | |
| 281 | 280 | */ |
| 282 | 281 | public function log_unique( $message = '' ) { |
| 283 | 282 | $contents = $this->get_log_content(); |
| 284 | 283 | |
| @@ -291,9 +290,9 @@ | ||
| 291 | 290 | |
| 292 | 291 | /** |
| 293 | 292 | * Get the log file contents. |
| 294 | 293 | * |
| 295 | - * @return string | |
| 294 | + * @return false|string | |
| 296 | 295 | */ |
| 297 | 296 | public function get_log_content() { |
| 298 | 297 | if ( ! isset( $this->content ) ) { |
| 299 | 298 | $this->content = $this->get_file(); |
| @@ -321,9 +320,9 @@ | ||
| 321 | 320 | * Retrieve the contents of a file. |
| 322 | 321 | * |
| 323 | 322 | * @param string|boolean $file File to get contents of. |
| 324 | 323 | * |
| 325 | - * @return string | |
| 324 | + * @return false|string | |
| 326 | 325 | */ |
| 327 | 326 | protected function get_file( $file = false ) { |
| 328 | 327 | $file = $file ? $file : $this->file; |
| 329 | 328 | |
| @@ -345,8 +344,10 @@ | ||
| 345 | 344 | /** |
| 346 | 345 | * Write the log message |
| 347 | 346 | * |
| 348 | 347 | * @param string $message The message to write. |
| 348 | + * | |
| 349 | + * @return void | |
| 349 | 350 | */ |
| 350 | 351 | protected function write_to_log( $message = '' ) { |
| 351 | 352 | if ( ! $this->enabled() ) { |
| 352 | 353 | return; |
| @@ -363,8 +364,10 @@ | ||
| 363 | 364 | } |
| 364 | 365 | |
| 365 | 366 | /** |
| 366 | 367 | * Save the current contents to file. |
| 368 | + * | |
| 369 | + * @return void | |
| 367 | 370 | */ |
| 368 | 371 | public function save_logs() { |
| 369 | 372 | $file_system = $this->fs(); |
| 370 | 373 | |
| @@ -388,8 +391,10 @@ | ||
| 388 | 391 | } |
| 389 | 392 | |
| 390 | 393 | /** |
| 391 | 394 | * Truncates a log file to maximum of 250 lines. |
| 395 | + * | |
| 396 | + * @return void | |
| 392 | 397 | */ |
| 393 | 398 | public function truncate_log() { |
| 394 | 399 | $content = $this->get_log_content(); |
| 395 | 400 | $lines = explode( "\r\n", $content ); |
| @@ -408,8 +413,10 @@ | ||
| 408 | 413 | } |
| 409 | 414 | |
| 410 | 415 | /** |
| 411 | 416 | * Delete the log file. |
| 417 | + * | |
| 418 | + * @return void | |
| 412 | 419 | */ |
| 413 | 420 | public function clear_log() { |
| 414 | 421 | $file_system = $this->fs(); |
| 415 | 422 | |
| @@ -431,8 +438,10 @@ | ||
| 431 | 438 | * |
| 432 | 439 | * @param string $func_name Function name. |
| 433 | 440 | * @param string $version Versoin deprecated. |
| 434 | 441 | * @param string $replacement Replacement function (optional). |
| 442 | + * | |
| 443 | + * @return void | |
| 435 | 444 | */ |
| 436 | 445 | public function log_deprecated_notice( $func_name, $version, $replacement = null ) { |
| 437 | 446 | if ( ! is_null( $replacement ) ) { |
| 438 | 447 | $notice = sprintf( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $func_name, $version, $replacement ); |