PluginProbe
Image Source Control Lite – Show Image Credits and Captions / 3.11.0
Image Source Control Lite – Show Image Credits and Captions v3.11.0
3.12.0 3.11.0 trunk 1.1 1.1.1 1.1.2 1.1.2.1 1.1.3 1.10 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.2 1.2.0.1 1.2.0.2 1.2.0.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.4.1 1.3.5 All 110 releases
image-source-control-isc / includes / log.php

log.php in Image Source Control Lite – Show Image Credits and Captions 3.11.0, at includes/log.php

165 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use ISC\Plugin;
4
5 /**
6 * Log events when creating the image source lists
7 */
8 class ISC_Log {
9
10 /**
11 * Get the name of the log file
12 *
13 * @return string
14 */
15 public static function get_file_name(): string {
16 // Hash the AUTH_KEY to create a unique but persistent filename
17 return 'image-source-control_' . hash( 'crc32', AUTH_KEY ) . '.log';
18 }
19
20 /**
21 * Check the log type
22 *
23 * @param string $type Type of the log depth.
24 *
25 * @return bool
26 */
27 public static function is_type( string $type ): bool {
28 return self::get_type() === $type;
29 }
30
31 /**
32 * Return the log type
33 *
34 * Type of the log depth
35 * - default
36 * - content - logs the content
37 * - backtrace - logs the function backtrace
38 */
39 private static function get_type(): string {
40 $accepted_types = [ 'default', 'content', 'backtrace' ];
41
42 // phpcs:ignore WordPress.Security.NonceVerification
43 $isc_log = sanitize_key( wp_unslash( $_GET['isc-log'] ?? '' ) );
44
45 return in_array( $isc_log, $accepted_types, true ) ? $isc_log : 'default';
46 }
47
48 /**
49 * Check if the log feature is enabled
50 *
51 * @param array $args optional arguments.
52 *
53 * @return bool
54 */
55 public static function enabled( array $args = [] ): bool {
56 // true if the Debug Log option is enabled and either ?isc-log is set or force_without_parameter is provided
57 // phpcs:ignore WordPress.Security.NonceVerification
58 return ( ! empty( Plugin::get_options()['enable_log'] ) && ( isset( $_REQUEST['isc-log'] ) || ! empty( $args['force_without_parameter'] ) ) );
59 }
60
61 /**
62 * Log image source extraction into a separate file
63 * can be used for debugging
64 * set define( 'ISC_LOG', true ); in wp-config.php to enable it
65 *
66 * @param string|array $message log message. Arrays will be converted into strings.
67 * @param array $args optional arguments.
68 */
69 public static function log( $message = '', array $args = [] ) {
70
71 if ( ! self::enabled( $args ) || null === $message ) {
72 return;
73 }
74
75 // get the current function name
76 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
77 $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 );
78 $method = sprintf( '%s:%s', $backtrace[1]['class'] ?? '', $backtrace[1]['function'] );
79
80 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
81 $message = is_array( $message ) ? print_r( $message, true ) : $message;
82
83 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
84 error_log( '[' . date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ) . "] $method: $message\n", 3, self::get_log_file_path() );
85 }
86
87 /**
88 * Delete the log file without any conditions
89 */
90 public static function delete_log_file() {
91 wp_delete_file( self::get_log_file_path() );
92 }
93
94 /**
95 * Get the URL to the log file
96 *
97 * @return string
98 */
99 public static function get_log_file_url(): string {
100 $upload_dir = wp_upload_dir();
101 return $upload_dir['baseurl'] . '/' . self::get_file_name();
102 }
103
104 /**
105 * Get the path to the log file
106 *
107 * @return string
108 */
109 public static function get_log_file_path(): string {
110 $upload_dir = wp_upload_dir();
111 return $upload_dir['basedir'] . '/' . self::get_file_name();
112 }
113
114 /**
115 * Check if the log file exists
116 *
117 * @return bool
118 */
119 public static function log_file_exists(): bool {
120 return file_exists( self::get_log_file_path() );
121 }
122
123 /**
124 * Return true if internal caches should be ignored
125 * only works in combination with an activated log
126 */
127 public static function ignore_caches(): bool {
128 // phpcs:ignore WordPress.Security.NonceVerification
129 return self::enabled() && isset( $_GET['isc-ignore-cache'] );
130 }
131
132 /**
133 * Return true if existing indexer data (Pro) should be ignored
134 * only works in combination with an activated log
135 */
136 public static function ignore_index(): bool {
137 // phpcs:ignore WordPress.Security.NonceVerification
138 return self::enabled() && isset( $_GET['isc-ignore-index'] );
139 }
140
141 /**
142 * Return true if the log should be cleared automatically before writing
143 */
144 public static function clear_log(): bool {
145 // phpcs:ignore WordPress.Security.NonceVerification
146 return self::enabled() && isset( $_GET['isc-clear-log'] );
147 }
148
149 /**
150 * Print a concise stack trace in the log
151 */
152 public static function log_stack_trace() {
153 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
154 $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS );
155
156 // remove the "args" key to reduce the output size
157 foreach ( $backtrace as $key => $trace ) {
158 unset( $backtrace[ $key ]['args'] );
159 }
160
161 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
162 self::log( 'Stack trace: ' . "\n - " . print_r( $backtrace, true ) );
163 }
164 }
165