hookMethods(); } /** * Filters block content. * * @hook render_block * @since 1.0.0 */ public function renderBlock( string $block_content, array $block, WP_Block $instance ): string { if ( 'core/calendar' === $block['blockName'] ) { return $this->coreCalendar( $block_content ); } elseif ( 'core/post-content' === $block['blockName'] ) { return $this->corePostContent( $block_content, $block, $instance ); } return $block_content; } /** * Really hacky method to replace the arrows in the calendar to match * the theme's arrows. * * @since 1.0.0 */ private function coreCalendar( string $block_content ): string { return str_replace( [ '»', '«' ], [ '→', '←' ], $block_content ); } /** * Filters the post content block when viewing single attachment views * and returns block-based media content. * * @since 1.0.0 */ private function corePostContent( string $block_content, array $block, WP_Block $instance ): string { // Bail early if there's no post ID. if ( empty( $instance->context['postId'] ) ) { return $block_content; } // Get the post object. $post = get_post( $instance->context['postId'] ); // Bail if we're not specifically viewing the attachment page // for this specific post. if ( 'attachment' !== $post->post_type || ! is_attachment( $post->ID ) ) { return $block_content; } // Set up some default variables. $filename = 'file'; $html = ''; // Checks if the attachment is one of supported types and sets // the filename based on that type. foreach ( [ 'image', 'video', 'audio'] as $type ) { if ( wp_attachment_is( $type, $post ) ) { $filename = $type; break; } } // Gets a partial (essentially a dynamic pattern) based on the // attachment type. Must be valid block content. ob_start(); include get_parent_theme_file_path( "public/partials/attachment-media-{$filename}.php" ); $media = ob_get_clean(); // Parse and render the blocks. foreach ( parse_blocks( $media ) as $media_block ) { $html .= render_block( $media_block ); } return $html . $block_content; } }