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 |