collector->get_data(); if ( empty( $data->post_blocks ) ) { return; } if ( ! $data->post_has_blocks ) { $this->before_non_tabular_output(); $notice = __( 'This post contains no blocks.', 'query-monitor' ); echo $this->build_notice( $notice ); // WPCS: XSS ok. $this->after_non_tabular_output(); return; } $this->before_tabular_output(); echo ''; echo ''; echo '#'; echo '' . esc_html__( 'Block Name', 'query-monitor' ) . ''; echo '' . esc_html__( 'Attributes', 'query-monitor' ) . ''; if ( $data->has_block_context ) { echo '' . esc_html__( 'Context', 'query-monitor' ) . ''; } echo '' . esc_html__( 'Render Callback', 'query-monitor' ) . ''; if ( $data->has_block_timing ) { echo '' . esc_html__( 'Render Time', 'query-monitor' ) . ''; } echo '' . esc_html__( 'Inner HTML', 'query-monitor' ) . ''; echo ''; echo ''; echo ''; foreach ( $data->post_blocks as $i => $block ) { self::render_block( ++$i, $block, $data ); } echo ''; echo ''; echo ''; $colspan = 5; if ( $data->has_block_context ) { $colspan++; } if ( $data->has_block_timing ) { $colspan++; } printf( '%2$s', intval( $colspan ), sprintf( /* translators: %s: Total number of content blocks used */ esc_html( _nx( 'Total: %s', 'Total: %s', $data->total_blocks, 'Content blocks used', 'query-monitor' ) ), '' . esc_html( number_format_i18n( $data->total_blocks ) ) . '' ) ); echo ''; echo ''; $this->after_tabular_output(); } /** * @param int|string $i * @param array $block * @param QM_Data_Block_Editor $data * @return void */ protected static function render_block( $i, array $block, QM_Data_Block_Editor $data ) { $block_error = false; $row_class = ''; $referenced_post = null; $referenced_type = null; $referenced_template_part = null; $referenced_pto = null; $error_message = null; if ( 'core/block' === $block['blockName'] && ! empty( $block['attrs']['ref'] ) ) { $referenced_post = get_post( $block['attrs']['ref'] ); if ( ! $referenced_post ) { $block_error = true; $error_message = esc_html__( 'Referenced block does not exist.', 'query-monitor' ); } else { $referenced_type = $referenced_post->post_type; $referenced_pto = get_post_type_object( $referenced_type ); if ( 'wp_block' !== $referenced_type ) { $block_error = true; $error_message = sprintf( /* translators: %1$s: Erroneous post type name, %2$s: WordPress block post type name */ esc_html__( 'Referenced post is of type %1$s instead of %2$s.', 'query-monitor' ), '' . esc_html( $referenced_type ) . '', 'wp_block' ); } } } $media_blocks = array( 'core/audio' => 'id', 'core/cover' => 'id', 'core/cover-image' => 'id', 'core/file' => 'id', 'core/image' => 'id', 'core/media-text' => 'mediaId', // (╯°□°)╯︵ ┻━┻ 'core/video' => 'id', ); if ( isset( $media_blocks[ $block['blockName'] ] ) && is_array( $block['attrs'] ) && ! empty( $block['attrs'][ $media_blocks[ $block['blockName'] ] ] ) ) { $referenced_post = get_post( $block['attrs'][ $media_blocks[ $block['blockName'] ] ] ); if ( ! $referenced_post ) { $block_error = true; $error_message = esc_html__( 'Referenced media does not exist.', 'query-monitor' ); } else { $referenced_type = $referenced_post->post_type; $referenced_pto = get_post_type_object( $referenced_type ); if ( 'attachment' !== $referenced_type ) { $block_error = true; $error_message = sprintf( /* translators: %1$s: Erroneous post type name, %2$s: WordPress attachment post type name */ esc_html__( 'Referenced media is of type %1$s instead of %2$s.', 'query-monitor' ), '' . esc_html( $referenced_type ) . '', 'attachment' ); } } } $template_part_blocks = array( 'core/template-part' => true, ); if ( isset( $template_part_blocks[ $block['blockName'] ] ) && is_array( $block['attrs'] ) && ! empty( $block['attrs']['slug'] ) && ! empty( $block['attrs']['theme'] ) ) { $referenced_template_part = sprintf( '%s//%s', $block['attrs']['theme'], $block['attrs']['slug'] ); $referenced_pto = get_post_type_object( 'wp_template_part' ); } if ( $block_error ) { $row_class = 'qm-warn'; } echo ''; echo '' . esc_html( $i ) . ''; echo ''; if ( $block['blockName'] ) { echo esc_html( $block['blockName'] ); } else { echo '' . esc_html__( 'None (Classic block)', 'query-monitor' ) . ''; } if ( $error_message ) { echo '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo QueryMonitor::icon( 'warning' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $error_message; } if ( ! empty( $referenced_post ) && ! empty( $referenced_pto ) ) { echo '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo self::build_link( get_edit_post_link( $referenced_post ), esc_html( $referenced_pto->labels->edit_item ) ); } if ( ! empty( $referenced_template_part ) ) { echo '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo self::build_link( QM_Util::get_site_editor_url( $referenced_template_part ), esc_html( $referenced_pto->labels->edit_item ) ); } echo '
'; echo ''; if ( $block['attrs'] && is_array( $block['attrs'] ) ) { echo '
' . esc_html( QM_Util::json_format( $block['attrs'] ) ) . '
'; } echo ''; if ( $data->has_block_context ) { echo ''; if ( isset( $block['context'] ) ) { echo '
' . esc_html( QM_Util::json_format( $block['context'] ) ) . '
'; } echo ''; } if ( isset( $block['callback']['error'] ) ) { $class = ' qm-warn'; } else { $class = ''; } if ( $block['dynamic'] ) { if ( isset( $block['callback']['file'] ) ) { if ( self::has_clickable_links() ) { echo ''; echo self::output_filename( QM_Util::get_callback_name( $block['callback'] ), $block['callback']['file'], $block['callback']['line'] ); // WPCS: XSS ok. echo ''; } else { echo ''; echo self::build_toggler(); // WPCS: XSS ok; echo '
    '; echo '
  1. '; echo self::output_filename( QM_Util::get_callback_name( $block['callback'] ), $block['callback']['file'], $block['callback']['line'] ); // WPCS: XSS ok. echo '
  2. '; echo '
'; } } else { echo ''; echo '' . esc_html( QM_Util::get_callback_name( $block['callback'] ) ) . ''; if ( isset( $block['callback']['error'] ) ) { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo '
' . QueryMonitor::icon( 'warning' ); echo esc_html( sprintf( /* translators: %s: Error message text */ __( 'Error: %s', 'query-monitor' ), $block['callback']['error']->get_error_message() ) ); } echo ''; } if ( $data->has_block_timing ) { echo ''; if ( isset( $block['timing'] ) ) { echo esc_html( number_format_i18n( $block['timing'], 4 ) ); } echo ''; } } else { echo ''; if ( $data->has_block_timing ) { echo ''; } } $inner_html = $block['innerHTML']; if ( $block['size'] > 300 ) { echo ''; echo self::build_toggler(); // WPCS: XSS ok; echo '
';
			echo esc_html( substr( $inner_html, 0, 200 ) ) . ' …';
			echo '
'; echo '
';
			echo esc_html( $inner_html );
			echo '
'; echo ''; } else { echo '
';
			echo esc_html( $inner_html );
			echo '
'; } echo ''; if ( ! empty( $block['innerBlocks'] ) ) { foreach ( $block['innerBlocks'] as $j => $inner_block ) { $x = ++$j; self::render_block( "{$i}.{$x}", $inner_block, $data ); } } } /** * @param array $menu * @return array */ public function admin_menu( array $menu ) { /** @var QM_Data_Block_Editor */ $data = $this->collector->get_data(); if ( empty( $data->post_blocks ) ) { return $menu; } $menu[ $this->collector->id() ] = $this->menu( array( 'title' => esc_html__( 'Blocks', 'query-monitor' ), ) ); return $menu; } } /** * @param array $output * @param QM_Collectors $collectors * @return array */ function register_qm_output_html_block_editor( array $output, QM_Collectors $collectors ) { $collector = QM_Collectors::get( 'block_editor' ); if ( $collector ) { $output['block_editor'] = new QM_Output_Html_Block_Editor( $collector ); } return $output; } add_filter( 'qm/outputter/html', 'register_qm_output_html_block_editor', 60, 2 );