Query
3 years ago
BlockBase.php
3 years ago
BlockContainerBase.php
4 years ago
BlockElement.php
4 years ago
BlockStyle.php
4 years ago
DataHelper.php
4 years ago
TemplatePartBlockBase.php
4 years ago
TemplatePartBlockBase.php
103 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Kubio\Core\Blocks; |
| 5 | |
| 6 | |
| 7 | class TemplatePartBlockBase extends BlockBase { |
| 8 | const CONTAINER = 'container'; |
| 9 | |
| 10 | public function mapPropsToElements() { |
| 11 | $html_tag = esc_attr( $this->getAttribute( 'tagName', 'div' ) ); |
| 12 | |
| 13 | return array( |
| 14 | self::CONTAINER => array( |
| 15 | 'tag' => $html_tag, |
| 16 | 'innerHTML' => $this->getContent(), |
| 17 | ), |
| 18 | ); |
| 19 | } |
| 20 | |
| 21 | public function getContent() { |
| 22 | $content = $this->getTemplateContent(); |
| 23 | |
| 24 | if ( is_null( $content ) ) { |
| 25 | return __( 'Template Part Not Found', 'kubio' ); |
| 26 | } |
| 27 | |
| 28 | // Run through the actions that are typically taken on the_content. |
| 29 | $content = do_blocks( $content ); |
| 30 | $content = wptexturize( $content ); |
| 31 | $content = convert_smilies( $content ); |
| 32 | |
| 33 | //corrupts the html for link wrappers. It adds extra paragraphs and the html tag structure get |
| 34 | //$content = wpautop( $content ); |
| 35 | |
| 36 | $content = shortcode_unautop( $content ); |
| 37 | if ( function_exists( 'wp_filter_content_tags' ) ) { |
| 38 | $content = wp_filter_content_tags( $content ); |
| 39 | } else { |
| 40 | $content = wp_make_content_images_responsive( $content ); |
| 41 | } |
| 42 | $content = do_shortcode( $content ); |
| 43 | |
| 44 | return str_replace( ']]>', ']]>', $content ); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | private function getTemplateContent() { |
| 49 | $content = null; |
| 50 | $post_id = $this->getAttribute( 'postId' ); |
| 51 | $theme = $this->getAttribute( 'theme' ); |
| 52 | $slug = $this->getAttribute( 'slug' ); |
| 53 | |
| 54 | $post = null; |
| 55 | if ( ! empty( $post_id ) && get_post_status( $post_id ) && ( $post = get_post( $post_id ) ) ) { |
| 56 | $content = $post->post_content; |
| 57 | } else { |
| 58 | if ( basename( wp_get_theme()->get_stylesheet() ) === $theme ) { |
| 59 | |
| 60 | $template_part_query = new \WP_Query( |
| 61 | array( |
| 62 | 'post_type' => 'wp_template_part', |
| 63 | 'post_status' => 'publish', |
| 64 | 'post_name__in' => array( $slug ), |
| 65 | 'tax_query' => array( |
| 66 | array( |
| 67 | 'taxonomy' => 'wp_theme', |
| 68 | 'field' => 'slug', |
| 69 | 'terms' => $theme, |
| 70 | ), |
| 71 | ), |
| 72 | 'posts_per_page' => 1, |
| 73 | 'no_found_rows' => true, |
| 74 | ) |
| 75 | ); |
| 76 | |
| 77 | $template_part_post = $template_part_query->have_posts() ? $template_part_query->next_post() : null; |
| 78 | if ( $template_part_post ) { |
| 79 | // A published post might already exist if this template part was customized elsewhere |
| 80 | // or if it's part of a customized template. |
| 81 | $content = $template_part_post->post_content; |
| 82 | } else { |
| 83 | // Else, if the template part was provided by the active theme, |
| 84 | // render the corresponding file content. |
| 85 | $template_part_file_paths = array( |
| 86 | get_stylesheet_directory() . '/full-site-editing/block-template-parts/' . $slug . '.html', |
| 87 | get_stylesheet_directory() . '/block-template-parts/' . $slug . '.html', |
| 88 | ); |
| 89 | |
| 90 | foreach ( $template_part_file_paths as $template_part_file_path ) { |
| 91 | if ( 0 === validate_file( $slug ) && file_exists( $template_part_file_path ) ) { |
| 92 | $content = file_get_contents( $template_part_file_path ); |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return $content; |
| 101 | } |
| 102 | } |
| 103 |