PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 1.6.4
Kubio AI Page Builder v1.6.4
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 / preview / autosaves-preview.php
kubio / lib / preview Last commit date
autosaves-preview.php 4 years ago index.php 4 years ago menu-preview.php 4 years ago site-data-preview.php 4 years ago
autosaves-preview.php
163 lines
1 <?php
2
3 use IlluminateAgnostic\Arr\Support\Arr;
4 use IlluminateAgnostic\Str\Support\Str;
5 use Kubio\Core\Utils;
6
7 function & _kubio_find_template_part( &$block, $availableTemplateParts ) {
8 if ( in_array( $block['blockName'], $availableTemplateParts ) ) {
9 return $block;
10 }
11 $templatePart = null;
12 if ( count( $block['innerBlocks'] ) > 0 ) {
13 foreach ( $block['innerBlocks'] as &$innerBlock ) {
14 $found_template_part = & _kubio_find_template_part( $innerBlock, $availableTemplateParts );
15 if ( ! ! $found_template_part ) {
16 $templatePart = & $found_template_part;
17 }
18 }
19 }
20
21 return $templatePart;
22 }
23
24 function kubio_handle_autosaved_posts_and_templates() {
25 $autosaved_posts = kubio_get_current_changeset_data( 'autosaves', array() );
26
27 // remap templates to page is needed becaue in editor a page can be changed on the fly
28 $page_templates_remap = kubio_get_current_changeset_data( 'pageTemplatesMap', array() );
29
30 $template_part_blocks = apply_filters( 'kubio/preview/template_part_blocks', array() );
31 $context_based_blocks = apply_filters(
32 'kubio/preview/template_part_blocks',
33 array(
34 'core/post-content',
35
36 )
37 );
38
39 add_filter(
40 'render_block_data',
41 function ( $parsed_block ) use ( $autosaved_posts, $template_part_blocks ) {
42 $isTemplatePart = in_array( $parsed_block['blockName'], $template_part_blocks );
43 $templatePartBlock = & $parsed_block;
44
45 //search inside inner blocks for the template part. This is needed for the sidebar template part
46 if ( ! $isTemplatePart ) {
47 $nestedTemplatePartBlock = & _kubio_find_template_part( $templatePartBlock, $template_part_blocks );
48 $isTemplatePart = ! ! $nestedTemplatePartBlock && in_array(
49 $nestedTemplatePartBlock['blockName'],
50 $template_part_blocks
51 );
52 if ( $isTemplatePart ) {
53 $templatePartBlock = & $nestedTemplatePartBlock;
54 }
55 }
56 if ( $isTemplatePart ) {
57 $block_template_id = kubio_get_template_part_block_id( $templatePartBlock );
58 foreach ( $autosaved_posts as $autosaved_post ) {
59 $autosaved_parent = intval( Arr::get( $autosaved_post, 'parent', 0 ) );
60 if ( $autosaved_parent === $block_template_id ) {
61 $templatePartBlock['attrs']['postId'] = $autosaved_post['id'];
62 }
63 }
64 }
65 return $parsed_block;
66 },
67 10,
68 1
69 );
70
71 add_filter(
72 'render_block_context',
73 function ( $context, $parsed_block ) use ( $autosaved_posts, $context_based_blocks ) {
74 if ( in_array( $parsed_block['blockName'], $context_based_blocks ) ) {
75 foreach ( $autosaved_posts as $autosaved_post ) {
76 $autosaved_parent = intval( Arr::get( $autosaved_post, 'parent', 0 ) );
77 if ( $autosaved_parent === Arr::get( $context, 'postId', -1 ) ) {
78 $post = get_post( $autosaved_post['id'] );
79
80 //check if revision post exists
81 if ( $post ) {
82 $context['postId'] = $autosaved_post['id'];
83 global $wp_query;
84 //wordpress updated the core/post-content block in 5.9 to ignore the postId from context
85 if ( is_array( $wp_query->posts ) && count( $wp_query->posts ) > 0 ) {
86 $firstPost = $wp_query->posts[0];
87 if ( $firstPost->ID === $autosaved_post['parent'] ) {
88 $wp_query->posts[0] = $post;
89 }
90 }
91 }
92 }
93 }
94 }
95
96 return $context;
97 },
98 10,
99 2
100 );
101
102 // filter template output content to use the autosaved data
103 add_filter(
104 'kubio/template/template-loader-callback',
105 function ( $callback ) use ( $autosaved_posts, $page_templates_remap ) {
106 return function ( $template, $type, $templates ) use ( $autosaved_posts, $callback, $page_templates_remap ) {
107
108 if ( is_page() || is_single() ) {
109 $remapped_template = Arr::get( $page_templates_remap, get_the_ID(), null );
110
111 if ( $remapped_template ) {
112 $templates = array( $remapped_template );
113 }
114 }
115
116 $template_file = call_user_func( $callback, $template, $type, $templates );
117 global $kubio_preview_located_template_data;
118
119 $kubio_preview_located_template_data = array();
120
121 if ( Str::endsWith( $template_file, 'template-canvas.php' ) ) {
122 if ( Utils::wpVersionCompare( '5.9', '>=' ) ) {
123 // use fallback parameter added in 5.9
124 /** @var WP_Block_Template $template_data */
125 $template_data = resolve_block_template( $template, $templates, $templates[0] );
126 } else {
127 /** @var WP_Block_Template $template_data */
128 $template_data = resolve_block_template( $template, $templates );
129 }
130
131 //for third party non fse templates get classic template
132 if ( $template_data === null ) {
133 $new_template = locate_template( $templates );
134 $kubio_preview_located_template_data = array( 'template' => $new_template );
135 return $new_template;
136 }
137
138 $kubio_template_source = get_post_meta( $template_data->wp_id, '_kubio_template_source', true );
139
140 if ( ( $kubio_template_source === 'kubio' || $kubio_template_source === 'kubio-custom' ) && function_exists( 'kubio_dequeue_theme_styles' ) ) {
141 add_action( 'wp_print_styles', 'kubio_dequeue_theme_styles', PHP_INT_MAX );
142 }
143
144 foreach ( $autosaved_posts as $autosaved_post ) {
145 $autosaved_parent = intval( Arr::get( $autosaved_post, 'parent', 0 ) );
146
147 if ( intval( $autosaved_parent ) === intval( $template_data->wp_id ) ) {
148 global $_wp_current_template_content;
149 $post = get_post( $autosaved_post['id'] );
150 $_wp_current_template_content = $post->post_content;
151 break;
152 }
153 }
154 }
155
156 $kubio_preview_located_template_data = array( 'template' => $template_file );
157 return $template_file;
158 };
159
160 }
161 );
162 }
163