PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.8.3
Kubio AI Page Builder v2.8.3
2.8.4 2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / full-site-editing / template-loader.php
kubio / lib / full-site-editing Last commit date
block-templates.php 1 year ago class-templates-list-table.php 2 years ago default-template-types.php 7 months ago full-site-editing.php 1 year ago get-block-templates.php 1 year ago template-loader.php 1 year ago template-parts.php 1 month ago templates.php 1 month ago
template-loader.php
301 lines
1 <?php
2
3 function kubio_template_locate( $template, $type, $templates ) {
4 $template_fn = function_exists( 'gutenberg_override_query_template' ) ? 'gutenberg_override_query_template' : 'locate_block_template';
5
6 $filtered_template = null;
7 $filtered_template = apply_filters(
8 'kubio/template/override-block-filter',
9 $filtered_template,
10 $type,
11 $template,
12 $templates
13 );
14
15 if ( ! is_null( $filtered_template ) ) {
16 return $filtered_template;
17 }
18
19 if ( $type === 'frontpage' && get_option( 'show_on_front' ) !== 'page' ) {
20 $type = 'home';
21 $templates = array( 'home.php', 'index.php' );
22
23 }
24
25 $template_fn = apply_filters( 'kubio/template/template-loader-callback', $template_fn );
26
27 global $kubio_located_template_data;
28 $kubio_located_template_data = array(
29 'template' => $template,
30 'type' => $type,
31 'templates' => $templates,
32 );
33
34 return call_user_func( $template_fn, $template, $type, $templates );
35 }
36
37 /**
38 * Adds necessary filters to use 'wp_template' posts instead of theme template files.
39 */
40 function kubio_add_template_loader_filters() {
41 foreach ( kubio_get_template_type_slugs() as $template_type ) {
42 if ( 'embed' === $template_type ) { // Skip 'embed' for now because it is not a regular template type.
43 continue;
44 }
45
46 $tag = str_replace( '-', '', $template_type ) . '_template';
47 add_filter( $tag, 'kubio_template_locate', 20, 3 );
48 }
49 }
50
51 add_action( 'wp_loaded', 'kubio_add_template_loader_filters' );
52
53 /*
54 * zip export
55 *
56 */
57 function kubio_find_template_post_and_parts( $template_type, $template_hierarchy = array() ) {
58 if ( ! $template_type ) {
59 return null;
60 }
61
62 if ( empty( $template_hierarchy ) ) {
63
64 if ( 'index' === $template_type ) {
65 $template_hierarchy = kubio_get_template_hierarchy( 'index' );
66 } else {
67 $template_hierarchy = array_merge( array( $template_type ), kubio_get_template_hierarchy( 'index' ) );
68 }
69 }
70
71 $slugs = array_map(
72 function ( $template_file ) {
73 return preg_replace( '/\.(php|html)$/', '', $template_file );
74 },
75 $template_hierarchy
76 );
77
78 // Find most specific 'wp_template' post matching the hierarchy.
79 $template_query = new WP_Query(
80 array(
81 'post_type' => 'wp_template',
82 'post_status' => 'publish',
83 'post_name__in' => $slugs,
84 'orderby' => 'post_name__in',
85 'posts_per_page' => 1,
86 'no_found_rows' => true,
87 )
88 );
89
90 $current_template_post = $template_query->have_posts() ? $template_query->next_post() : null;
91
92 // Build map of template slugs to their priority in the current hierarchy.
93 $slug_priorities = array_flip( $slugs );
94
95 // See if there is a theme block template with higher priority than the resolved template post.
96 $higher_priority_block_template_path = null;
97 $higher_priority_block_template_priority = PHP_INT_MAX;
98 $block_template_files = kubio_get_template_paths();
99 foreach ( $block_template_files as $path ) {
100 if ( ! isset( $slug_priorities[ basename( $path, '.html' ) ] ) ) {
101 continue;
102 }
103 $theme_block_template_priority = $slug_priorities[ basename( $path, '.html' ) ];
104 if (
105 $theme_block_template_priority < $higher_priority_block_template_priority &&
106 ( empty( $current_template_post ) || $theme_block_template_priority < $slug_priorities[ $current_template_post->post_name ] )
107 ) {
108 $higher_priority_block_template_path = $path;
109 $higher_priority_block_template_priority = $theme_block_template_priority;
110 }
111 }
112
113 // If there is, use it instead.
114 if ( isset( $higher_priority_block_template_path ) ) {
115 $post_name = basename( $higher_priority_block_template_path, '.html' );
116 $file_contents = file_get_contents( $higher_priority_block_template_path );
117 $current_template_post = array(
118 'post_content' => $file_contents,
119 'post_title' => $post_name,
120 'post_status' => 'publish',
121 'post_type' => 'wp_template',
122 'post_name' => $post_name,
123 );
124 if ( is_admin() || defined( 'REST_REQUEST' ) ) {
125 $template_query = new WP_Query(
126 array(
127 'post_type' => 'wp_template',
128 'post_status' => 'publish',
129 'name' => $post_name,
130 'posts_per_page' => 1,
131 'no_found_rows' => true,
132 )
133 );
134 $current_template_post = $template_query->have_posts() ? $template_query->next_post() : $current_template_post;
135
136 // Only create auto-draft of block template for editing
137 // in admin screens, when necessary, because the underlying
138 // file has changed.
139 if ( is_array( $current_template_post ) || $current_template_post->post_content !== $file_contents ) {
140 if ( ! is_array( $current_template_post ) ) {
141 $current_template_post->post_content = $file_contents;
142 }
143 $current_template_post = get_post(
144 wp_insert_post( $current_template_post )
145 );
146 }
147 } else {
148 $current_template_post = new WP_Post(
149 (object) $current_template_post
150 );
151 }
152 }
153
154 // If we haven't found any template post by here, it means that this theme doesn't even come with a fallback
155 // `index.html` block template. We create one so that people that are trying to access the editor are greeted
156 // with a blank page rather than an error.
157 if ( ! $current_template_post && ( is_admin() || defined( 'REST_REQUEST' ) ) ) {
158 $current_template_post = array(
159 'post_title' => 'index',
160 'post_status' => 'publish',
161 'post_type' => 'wp_template',
162 'post_name' => 'index',
163 );
164 $current_template_post = get_post(
165 wp_insert_post( $current_template_post )
166 );
167 }
168
169 if ( $current_template_post ) {
170 $template_part_ids = array();
171 if ( is_admin() || defined( 'REST_REQUEST' ) ) {
172 foreach ( parse_blocks( $current_template_post->post_content ) as $block ) {
173 $template_part_ids = array_merge( $template_part_ids, kubio_create_entity_for_template_part_block( $block ) );
174 }
175 }
176
177 return array(
178 'template_post' => $current_template_post,
179 'template_part_ids' => $template_part_ids,
180 );
181 }
182
183 return null;
184 }
185
186
187 function kubio_create_entity_for_template_part_block( $block ) {
188 $template_part_ids = array();
189
190 $block_types = apply_filters( 'kubio/preview/template_part_blocks', array() );
191
192 if ( in_array( $block['blockName'], $block_types ) && isset( $block['attrs']['slug'] ) ) {
193 if ( isset( $block['attrs']['postId'] ) ) {
194 // Template part is customized.
195 $template_part_id = $block['attrs']['postId'];
196 } else {
197 // A published post might already exist if this template part
198 // was customized elsewhere or if it's part of a customized
199 // template. We also check if an auto-draft was already created
200 // because preloading can make this run twice, so, different code
201 // paths can end up with different posts for the same template part.
202 // E.g. The server could send back post ID 1 to the client, preload,
203 // and create another auto-draft. So, if the client tries to resolve the
204 // post ID from the slug and theme, it won't match with what the server sent.
205
206 $template_part_query = new WP_Query(
207 array(
208 'post_type' => 'wp_template_part',
209 'post_status' => array( 'publish' ),
210 'name' => $block['attrs']['slug'],
211 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query, WordPress.DB.SlowDBQuery.slow_db_query_meta_key
212 'meta_key' => 'theme',
213 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
214 'meta_value' => $block['attrs']['theme'],
215 'posts_per_page' => 1,
216 'no_found_rows' => true,
217 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
218 'tax_query' => array(
219 array(
220 'taxonomy' => 'wp_theme',
221 'field' => 'slug',
222 'terms' => $block['attrs']['theme'],
223 ),
224 ),
225 )
226 );
227 $template_part_post = $template_part_query->have_posts() ? $template_part_query->next_post() : null;
228 if ( $template_part_post && 'auto-draft' !== $template_part_post->post_status ) {
229 $template_part_id = $template_part_post->ID;
230 } else {
231 // Template part is not customized, get it from a file and make an auto-draft for it, unless one already exists
232 // and the underlying file hasn't changed.
233 $template_part_file_paths = array(
234 get_stylesheet_directory() . '/full-site-editing/block-template-parts/' . $block['attrs']['slug'] . '.html',
235 get_stylesheet_directory() . '/block-template-parts/' . $block['attrs']['slug'] . '.html',
236 );
237
238 $template_part_file_path = false;
239
240 foreach ( $template_part_file_paths as $tpl_part_path ) {
241 if ( file_exists( $tpl_part_path ) ) {
242 $template_part_file_path = $tpl_part_path;
243 break;
244 }
245 }
246
247 if ( $template_part_file_path ) {
248 $file_contents = file_get_contents( $template_part_file_path );
249 if ( $template_part_post && $template_part_post->post_content === $file_contents ) {
250 $template_part_id = $template_part_post->ID;
251 } else {
252
253 $slug = $block['attrs']['slug'];
254 $title = kubio_maybe_transform_slug_to_title( $slug );
255 $area = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
256
257 if ( strpos( $slug, 'header' ) !== false ) {
258 $area = WP_TEMPLATE_PART_AREA_HEADER;
259 } else {
260 if ( strpos( $slug, 'footer' ) !== false ) {
261 $area = WP_TEMPLATE_PART_AREA_FOOTER;
262 } else {
263 if ( strpos( $slug, 'sidebar' ) !== false ) {
264 $area = WP_TEMPLATE_PART_AREA_SIDEBAR;
265 }
266 }
267 }
268
269 $template_part_id = wp_insert_post(
270 array(
271 'post_content' => $file_contents,
272 'post_title' => $title,
273 'post_status' => 'publish',
274 'post_type' => 'wp_template_part',
275 'post_name' => $slug,
276 'meta_input' => array(
277 'theme' => $block['attrs']['theme'],
278 ),
279 'tax_input' => array(
280 'wp_theme' => $block['attrs']['theme'],
281 'wp_template_part_area' => array(
282 $area,
283 ),
284 ),
285 ),
286 true
287 );
288 }
289 }
290 }
291 }
292 $template_part_ids[ $block['attrs']['slug'] ] = $template_part_id;
293 }
294
295 foreach ( $block['innerBlocks'] as $inner_block ) {
296 $template_part_ids = array_merge( $template_part_ids, kubio_create_entity_for_template_part_block( $inner_block ) );
297 }
298
299 return $template_part_ids;
300 }
301