Query
1 year ago
BlockBase.php
1 year ago
BlockContainerBase.php
1 year ago
BlockElement.php
3 years ago
BlockStyle.php
4 years ago
DataHelper.php
1 year ago
TemplatePartBlockBase.php
1 year ago
TemplatePartBlockBase.php
148 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Kubio\Core\Blocks; |
| 5 | |
| 6 | |
| 7 | |
| 8 | class TemplatePartBlockBase extends BlockBase { |
| 9 | const CONTAINER = 'container'; |
| 10 | |
| 11 | public function mapPropsToElements() { |
| 12 | $html_tag = esc_attr( $this->getAttribute( 'tagName', 'div' ) ); |
| 13 | |
| 14 | return array( |
| 15 | self::CONTAINER => array( |
| 16 | 'tag' => $html_tag, |
| 17 | 'innerHTML' => $this->getContent(), |
| 18 | ), |
| 19 | ); |
| 20 | } |
| 21 | |
| 22 | public function getContent() { |
| 23 | $content = $this->getTemplateContent(); |
| 24 | |
| 25 | if ( is_null( $content ) ) { |
| 26 | return __( 'Template Part Not Found', 'kubio' ); |
| 27 | } |
| 28 | |
| 29 | // Run through the actions that are typically taken on the_content. |
| 30 | $content = do_blocks( $content ); |
| 31 | $content = wptexturize( $content ); |
| 32 | $content = convert_smilies( $content ); |
| 33 | |
| 34 | //corrupts the html for link wrappers. It adds extra paragraphs and the html tag structure get |
| 35 | //$content = wpautop( $content ); |
| 36 | |
| 37 | $content = shortcode_unautop( $content ); |
| 38 | if ( function_exists( 'wp_filter_content_tags' ) ) { |
| 39 | $content = wp_filter_content_tags( $content ); |
| 40 | } else { |
| 41 | // backward compatibility for WP < 5.5 |
| 42 | // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_make_content_images_responsiveFound |
| 43 | $content = wp_make_content_images_responsive( $content ); |
| 44 | } |
| 45 | $content = do_shortcode( $content ); |
| 46 | |
| 47 | return str_replace( ']]>', ']]>', $content ); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | private function getTemplateContent() { |
| 52 | $content = null; |
| 53 | $post_id = $this->getAttribute( 'postId' ); |
| 54 | $theme = $this->getAttribute( 'theme' ); |
| 55 | $slug = $this->getAttribute( 'slug' ); |
| 56 | |
| 57 | $post = null; |
| 58 | if ( ! empty( $post_id ) && get_post_status( $post_id ) && ( $post = get_post( $post_id ) ) ) { |
| 59 | $content = $post->post_content; |
| 60 | } else { |
| 61 | if ( basename( wp_get_theme()->get_stylesheet() ) === $theme ) { |
| 62 | |
| 63 | $query_args = array( |
| 64 | 'post_type' => 'wp_template_part', |
| 65 | 'post_status' => 'publish', |
| 66 | 'post_name__in' => array( $slug ), |
| 67 | // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 68 | 'tax_query' => array( |
| 69 | array( |
| 70 | 'taxonomy' => 'wp_theme', |
| 71 | 'field' => 'slug', |
| 72 | 'terms' => $theme, |
| 73 | ), |
| 74 | ), |
| 75 | 'posts_per_page' => 1, |
| 76 | 'no_found_rows' => true, |
| 77 | ); |
| 78 | $template_part_query = new \WP_Query( |
| 79 | $query_args |
| 80 | ); |
| 81 | |
| 82 | $template_part_post = $template_part_query->have_posts() ? $template_part_query->next_post() : null; |
| 83 | |
| 84 | if ( $template_part_post ) { |
| 85 | |
| 86 | // A published post might already exist if this template part was customized elsewhere |
| 87 | // or if it's part of a customized template. |
| 88 | $content = $template_part_post->post_content; |
| 89 | } else { |
| 90 | // Else, if the template part was provided by the active theme, |
| 91 | // render the corresponding file content. |
| 92 | $template_part_file_paths = array( |
| 93 | get_stylesheet_directory() . '/full-site-editing/block-template-parts/' . $slug . '.html', |
| 94 | get_stylesheet_directory() . '/block-template-parts/' . $slug . '.html', |
| 95 | ); |
| 96 | |
| 97 | foreach ( $template_part_file_paths as $template_part_file_path ) { |
| 98 | if ( 0 === validate_file( $slug ) && file_exists( $template_part_file_path ) ) { |
| 99 | $content = file_get_contents( $template_part_file_path ); |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if ( ! $content && kubio_wpml_is_active() ) { |
| 105 | $content = $this->getWpmlContent( $query_args ); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return $content; |
| 112 | } |
| 113 | |
| 114 | public function getWpmlContent( $query_args ) { |
| 115 | global $wpml_query_filter; |
| 116 | $had_filter = false; |
| 117 | if ( has_filter( 'posts_join', array( $wpml_query_filter, 'posts_join_filter' ) ) ) { |
| 118 | $had_filter = true; |
| 119 | remove_filter( 'posts_join', array( $wpml_query_filter, 'posts_join_filter' ), 10 ); |
| 120 | remove_filter( 'posts_where', array( $wpml_query_filter, 'posts_where_filter' ), 10 ); |
| 121 | } |
| 122 | |
| 123 | $slug = $this->getAttribute( 'slug' ); |
| 124 | $template_part_query = new \WP_Query( |
| 125 | array_merge( |
| 126 | $query_args, |
| 127 | array( |
| 128 | 'post_name__in' => array( $slug ), |
| 129 | ) |
| 130 | ) |
| 131 | ); |
| 132 | |
| 133 | $content = null; |
| 134 | $template_part_post = $template_part_query->have_posts() ? $template_part_query->next_post() : null; |
| 135 | if ( $template_part_post ) { |
| 136 | |
| 137 | // A published post might already exist if this template part was customized elsewhere |
| 138 | // or if it's part of a customized template. |
| 139 | $content = $template_part_post->post_content; |
| 140 | } |
| 141 | if ( $had_filter ) { |
| 142 | add_filter( 'posts_join', array( $wpml_query_filter, 'posts_join_filter' ), 10, 2 ); |
| 143 | add_filter( 'posts_where', array( $wpml_query_filter, 'posts_where_filter' ), 10, 2 ); |
| 144 | } |
| 145 | return $content; |
| 146 | } |
| 147 | } |
| 148 |