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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / Admin / BlockTemplates / BlockTemplateLogger.php
woocommerce / src / Internal / Admin / BlockTemplates Last commit date
AbstractBlock.php 4 weeks ago AbstractBlockTemplate.php 2 years ago Block.php 2 years ago BlockContainerTrait.php 4 weeks ago BlockFormattedTemplateTrait.php 2 years ago BlockTemplate.php 2 years ago BlockTemplateLogger.php 2 years ago
BlockTemplateLogger.php
508 lines
1 <?php
2
3 namespace Automattic\WooCommerce\Internal\Admin\BlockTemplates;
4
5 use Automattic\WooCommerce\Admin\BlockTemplates\BlockContainerInterface;
6 use Automattic\WooCommerce\Admin\BlockTemplates\BlockInterface;
7 use Automattic\WooCommerce\Admin\BlockTemplates\BlockTemplateInterface;
8 use Automattic\WooCommerce\Admin\BlockTemplates\ContainerInterface;
9
10 /**
11 * Logger for block template modifications.
12 */
13 class BlockTemplateLogger {
14 const BLOCK_ADDED = 'block_added';
15 const BLOCK_REMOVED = 'block_removed';
16 const BLOCK_MODIFIED = 'block_modified';
17 const BLOCK_ADDED_TO_DETACHED_CONTAINER = 'block_added_to_detached_container';
18 const HIDE_CONDITION_ADDED = 'hide_condition_added';
19 const HIDE_CONDITION_REMOVED = 'hide_condition_removed';
20 const HIDE_CONDITION_ADDED_TO_DETACHED_BLOCK = 'hide_condition_added_to_detached_block';
21 const ERROR_AFTER_BLOCK_ADDED = 'error_after_block_added';
22 const ERROR_AFTER_BLOCK_REMOVED = 'error_after_block_removed';
23
24 const LOG_HASH_TRANSIENT_BASE_NAME = 'wc_block_template_events_log_hash_';
25
26 /**
27 * Event types.
28 *
29 * @var array
30 */
31 public static $event_types = array(
32 self::BLOCK_ADDED => array(
33 'level' => \WC_Log_Levels::DEBUG,
34 'message' => 'Block added to template.',
35 ),
36 self::BLOCK_REMOVED => array(
37 'level' => \WC_Log_Levels::NOTICE,
38 'message' => 'Block removed from template.',
39 ),
40 self::BLOCK_MODIFIED => array(
41 'level' => \WC_Log_Levels::NOTICE,
42 'message' => 'Block modified in template.',
43 ),
44 self::BLOCK_ADDED_TO_DETACHED_CONTAINER => array(
45 'level' => \WC_Log_Levels::WARNING,
46 'message' => 'Block added to detached container. Block will not be included in the template, since the container will not be included in the template.',
47 ),
48 self::HIDE_CONDITION_ADDED => array(
49 'level' => \WC_Log_Levels::NOTICE,
50 'message' => 'Hide condition added to block.',
51 ),
52 self::HIDE_CONDITION_REMOVED => array(
53 'level' => \WC_Log_Levels::NOTICE,
54 'message' => 'Hide condition removed from block.',
55 ),
56 self::HIDE_CONDITION_ADDED_TO_DETACHED_BLOCK => array(
57 'level' => \WC_Log_Levels::WARNING,
58 'message' => 'Hide condition added to detached block. Block will not be included in the template, so the hide condition is not needed.',
59 ),
60 self::ERROR_AFTER_BLOCK_ADDED => array(
61 'level' => \WC_Log_Levels::WARNING,
62 'message' => 'Error after block added to template.',
63 ),
64 self::ERROR_AFTER_BLOCK_REMOVED => array(
65 'level' => \WC_Log_Levels::WARNING,
66 'message' => 'Error after block removed from template.',
67 ),
68 );
69
70 /**
71 * Singleton instance.
72 *
73 * @var BlockTemplateLogger
74 */
75 protected static $instance = null;
76
77 /**
78 * Logger instance.
79 *
80 * @var \WC_Logger
81 */
82 protected $logger = null;
83
84 /**
85 * All template events.
86 *
87 * @var array
88 */
89 private $all_template_events = array();
90
91 /**
92 * Templates.
93 *
94 * @var array
95 */
96 private $templates = array();
97
98 /**
99 * Threshold severity.
100 *
101 * @var int
102 */
103 private $threshold_severity = null;
104
105 /**
106 * Get the singleton instance.
107 */
108 public static function get_instance(): BlockTemplateLogger {
109 if ( ! self::$instance ) {
110 self::$instance = new self();
111 }
112
113 return self::$instance;
114 }
115
116 /**
117 * Constructor.
118 */
119 protected function __construct() {
120 $this->logger = wc_get_logger();
121
122 $threshold = get_option( 'woocommerce_block_template_logging_threshold', \WC_Log_Levels::WARNING );
123 if ( ! \WC_Log_Levels::is_valid_level( $threshold ) ) {
124 $threshold = \WC_Log_Levels::INFO;
125 }
126
127 $this->threshold_severity = \WC_Log_Levels::get_level_severity( $threshold );
128
129 add_action(
130 'woocommerce_block_template_after_add_block',
131 function ( BlockInterface $block ) {
132 $is_detached = method_exists( $block->get_parent(), 'is_detached' ) && $block->get_parent()->is_detached();
133
134 $this->log(
135 $is_detached
136 ? $this::BLOCK_ADDED_TO_DETACHED_CONTAINER
137 : $this::BLOCK_ADDED,
138 $block,
139 );
140 },
141 0,
142 );
143
144 add_action(
145 'woocommerce_block_template_after_remove_block',
146 function ( BlockInterface $block ) {
147 $this->log(
148 $this::BLOCK_REMOVED,
149 $block,
150 );
151 },
152 0,
153 );
154
155 add_action(
156 'woocommerce_block_template_after_add_hide_condition',
157 function ( BlockInterface $block ) {
158 $this->log(
159 $block->is_detached()
160 ? $this::HIDE_CONDITION_ADDED_TO_DETACHED_BLOCK
161 : $this::HIDE_CONDITION_ADDED,
162 $block,
163 );
164 },
165 0
166 );
167
168 add_action(
169 'woocommerce_block_template_after_remove_hide_condition',
170 function ( BlockInterface $block ) {
171 $this->log(
172 $this::HIDE_CONDITION_REMOVED,
173 $block,
174 );
175 },
176 0
177 );
178
179 add_action(
180 'woocommerce_block_template_after_add_block_error',
181 function ( BlockInterface $block, string $action, \Exception $exception ) {
182 $this->log(
183 $this::ERROR_AFTER_BLOCK_ADDED,
184 $block,
185 array(
186 'action' => $action,
187 'exception' => $exception,
188 ),
189 );
190 },
191 0,
192 3
193 );
194
195 add_action(
196 'woocommerce_block_template_after_remove_block_error',
197 function ( BlockInterface $block, string $action, \Exception $exception ) {
198 $this->log(
199 $this::ERROR_AFTER_BLOCK_REMOVED,
200 $block,
201 array(
202 'action' => $action,
203 'exception' => $exception,
204 ),
205 );
206 },
207 0,
208 3
209 );
210 }
211
212 /**
213 * Get all template events for a given template as a JSON like array.
214 *
215 * @param string $template_id Template ID.
216 */
217 public function template_events_to_json( string $template_id ): array {
218 if ( ! isset( $this->all_template_events[ $template_id ] ) ) {
219 return array();
220 }
221
222 $template_events = $this->all_template_events[ $template_id ];
223
224 return $this->to_json( $template_events );
225 }
226
227 /**
228 * Get all template events as a JSON like array.
229 *
230 * @param array $template_events Template events.
231 *
232 * @return array The JSON.
233 */
234 private function to_json( array $template_events ): array {
235 $json = array();
236
237 foreach ( $template_events as $template_event ) {
238 $container = $template_event['container'];
239 $block = $template_event['block'];
240
241 $json[] = array(
242 'level' => $template_event['level'],
243 'event_type' => $template_event['event_type'],
244 'message' => $template_event['message'],
245 'container' => $container instanceof BlockInterface
246 ? array(
247 'id' => $container->get_id(),
248 'name' => $container->get_name(),
249 )
250 : null,
251 'block' => array(
252 'id' => $block->get_id(),
253 'name' => $block->get_name(),
254 ),
255 'additional_info' => $this->format_info( $template_event['additional_info'] ),
256 );
257 }
258
259 return $json;
260 }
261
262 /**
263 * Log all template events for a given template to the log file.
264 *
265 * @param string $template_id Template ID.
266 */
267 public function log_template_events_to_file( string $template_id ) {
268 if ( ! isset( $this->all_template_events[ $template_id ] ) ) {
269 return;
270 }
271
272 $template_events = $this->all_template_events[ $template_id ];
273
274 $hash = $this->generate_template_events_hash( $template_events );
275
276 if ( ! $this->has_template_events_changed( $template_id, $hash ) ) {
277 // Nothing has changed since the last time this was logged,
278 // so don't log it again.
279 return;
280 }
281
282 $this->set_template_events_log_hash( $template_id, $hash );
283
284 $template = $this->templates[ $template_id ];
285
286 foreach ( $template_events as $template_event ) {
287 $info = array_merge(
288 array(
289 'template' => $template,
290 'container' => $template_event['container'],
291 'block' => $template_event['block'],
292 ),
293 $template_event['additional_info']
294 );
295
296 $message = $this->format_message( $template_event['message'], $info );
297
298 $this->logger->log(
299 $template_event['level'],
300 $message,
301 array( 'source' => 'block_template' )
302 );
303 }
304 }
305
306 /**
307 * Has the template events changed since the last time they were logged?
308 *
309 * @param string $template_id Template ID.
310 * @param string $events_hash Events hash.
311 */
312 private function has_template_events_changed( string $template_id, string $events_hash ) {
313 $previous_hash = get_transient( self::LOG_HASH_TRANSIENT_BASE_NAME . $template_id );
314
315 return $previous_hash !== $events_hash;
316 }
317
318 /**
319 * Generate a hash for a given set of template events.
320 *
321 * @param array $template_events Template events.
322 */
323 private function generate_template_events_hash( array $template_events ): string {
324 return md5( wp_json_encode( $this->to_json( $template_events ) ) );
325 }
326
327 /**
328 * Set the template events hash for a given template.
329 *
330 * @param string $template_id Template ID.
331 * @param string $hash Hash of template events.
332 */
333 private function set_template_events_log_hash( string $template_id, string $hash ) {
334 set_transient( self::LOG_HASH_TRANSIENT_BASE_NAME . $template_id, $hash );
335 }
336
337 /**
338 * Log an event.
339 *
340 * @param string $event_type Event type.
341 * @param BlockInterface $block Block.
342 * @param array $additional_info Additional info.
343 */
344 private function log( string $event_type, BlockInterface $block, $additional_info = array() ) {
345 if ( ! isset( self::$event_types[ $event_type ] ) ) {
346 /* translators: 1: WC_Logger::log 2: level */
347 wc_doing_it_wrong( __METHOD__, sprintf( __( '%1$s was called with an invalid event type "%2$s".', 'woocommerce' ), '<code>BlockTemplateLogger::log</code>', $event_type ), '8.4' );
348 }
349
350 $event_type_info = isset( self::$event_types[ $event_type ] )
351 ? array_merge(
352 self::$event_types[ $event_type ],
353 array(
354 'event_type' => $event_type,
355 )
356 )
357 : array(
358 'level' => \WC_Log_Levels::ERROR,
359 'event_type' => $event_type,
360 'message' => 'Unknown error.',
361 );
362
363 if ( ! $this->should_handle( $event_type_info['level'] ) ) {
364 return;
365 }
366
367 $template = $block->get_root_template();
368 $container = $block->get_parent();
369
370 $this->add_template_event( $event_type_info, $template, $container, $block, $additional_info );
371 }
372
373 /**
374 * Should the logger handle a given level?
375 *
376 * @param int $level Level to check.
377 */
378 private function should_handle( $level ) {
379 return $this->threshold_severity <= \WC_Log_Levels::get_level_severity( $level );
380 }
381
382 /**
383 * Add a template event.
384 *
385 * @param array $event_type_info Event type info.
386 * @param BlockTemplateInterface $template Template.
387 * @param ContainerInterface $container Container.
388 * @param BlockInterface $block Block.
389 * @param array $additional_info Additional info.
390 */
391 private function add_template_event( array $event_type_info, BlockTemplateInterface $template, ContainerInterface $container, BlockInterface $block, array $additional_info = array() ) {
392 $template_id = $template->get_id();
393
394 if ( ! isset( $this->all_template_events[ $template_id ] ) ) {
395 $this->all_template_events[ $template_id ] = array();
396 $this->templates[ $template_id ] = $template;
397 }
398
399 $template_events = &$this->all_template_events[ $template_id ];
400
401 $template_events[] = array(
402 'level' => $event_type_info['level'],
403 'event_type' => $event_type_info['event_type'],
404 'message' => $event_type_info['message'],
405 'container' => $container,
406 'block' => $block,
407 'additional_info' => $additional_info,
408 );
409 }
410
411 /**
412 * Format a message for logging.
413 *
414 * @param string $message Message to log.
415 * @param array $info Additional info to log.
416 */
417 private function format_message( string $message, array $info = array() ): string {
418 $formatted_message = sprintf(
419 "%s\n%s",
420 $message,
421 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
422 print_r( $this->format_info( $info ), true ),
423 );
424
425 return $formatted_message;
426 }
427
428 /**
429 * Format info for logging.
430 *
431 * @param array $info Info to log.
432 */
433 private function format_info( array $info ): array {
434 $formatted_info = $info;
435
436 if ( isset( $info['exception'] ) && $info['exception'] instanceof \Exception ) {
437 $formatted_info['exception'] = $this->format_exception( $info['exception'] );
438 }
439
440 if ( isset( $info['container'] ) ) {
441 if ( $info['container'] instanceof BlockContainerInterface ) {
442 $formatted_info['container'] = $this->format_block( $info['container'] );
443 } elseif ( $info['container'] instanceof BlockTemplateInterface ) {
444 $formatted_info['container'] = $this->format_template( $info['container'] );
445 } elseif ( $info['container'] instanceof BlockInterface ) {
446 $formatted_info['container'] = $this->format_block( $info['container'] );
447 }
448 }
449
450 if ( isset( $info['block'] ) && $info['block'] instanceof BlockInterface ) {
451 $formatted_info['block'] = $this->format_block( $info['block'] );
452 }
453
454 if ( isset( $info['template'] ) && $info['template'] instanceof BlockTemplateInterface ) {
455 $formatted_info['template'] = $this->format_template( $info['template'] );
456 }
457
458 return $formatted_info;
459 }
460
461 /**
462 * Format an exception for logging.
463 *
464 * @param \Exception $exception Exception to format.
465 */
466 private function format_exception( \Exception $exception ): array {
467 return array(
468 'message' => $exception->getMessage(),
469 'source' => "{$exception->getFile()}: {$exception->getLine()}",
470 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
471 'trace' => print_r( $this->format_exception_trace( $exception->getTrace() ), true ),
472 );
473 }
474
475 /**
476 * Format an exception trace for logging.
477 *
478 * @param array $trace Exception trace to format.
479 */
480 private function format_exception_trace( array $trace ): array {
481 $formatted_trace = array();
482
483 foreach ( $trace as $source ) {
484 $formatted_trace[] = "{$source['file']}: {$source['line']}";
485 }
486
487 return $formatted_trace;
488 }
489
490 /**
491 * Format a block template for logging.
492 *
493 * @param BlockTemplateInterface $template Template to format.
494 */
495 private function format_template( BlockTemplateInterface $template ): string {
496 return "{$template->get_id()} (area: {$template->get_area()})";
497 }
498
499 /**
500 * Format a block for logging.
501 *
502 * @param BlockInterface $block Block to format.
503 */
504 private function format_block( BlockInterface $block ): string {
505 return "{$block->get_id()} (name: {$block->get_name()})";
506 }
507 }
508