PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 1.0.0
Kubio AI Page Builder v1.0.0
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 / shared-style.php
kubio / lib Last commit date
admin-pages 4 years ago api 4 years ago blog 4 years ago full-site-editing 4 years ago importer 4 years ago integrations 4 years ago menu 4 years ago polyfills 4 years ago preview 4 years ago shapes 4 years ago shortcodes 4 years ago src 4 years ago add-edit-in-kubio.php 4 years ago editor-assets.php 4 years ago env.php 4 years ago filters.php 4 years ago frontend.php 4 years ago global-data.php 4 years ago kubio-block-library.php 4 years ago kubio-editor.php 4 years ago load.php 4 years ago shared-style.php 4 years ago
shared-style.php
242 lines
1 <?php
2
3 use Kubio\Core\LodashBasic;
4 use Kubio\Core\Utils;
5
6 function kubio_replace_shared_style( $post_content, $styleRefs ) {
7 $blocks = parse_blocks( $post_content );
8
9 Utils::walkBlocks(
10 $blocks,
11 function ( &$block ) use ( $styleRefs ) {
12 if ( ! str_contains( $block['blockName'], 'kubio' ) ) {
13 return;
14 }
15
16 $template_parts = apply_filters( 'kubio/preview/template_part_blocks', array() );
17
18 if ( in_array( $block['blockName'], $template_parts ) ) {
19 return;
20 }
21
22 $main_attr = LodashBasic::get( $block, 'attrs.kubio', array() );
23 $block_style_ref = LodashBasic::get( $main_attr, 'styleRef' );
24 if ( isset( $styleRefs[ $block_style_ref ] ) ) {
25 if ( isset( $styleRefs[ $block_style_ref ]['style'] ) ) {
26 LodashBasic::set( $block, 'attrs.kubio.style', $styleRefs[ $block_style_ref ]['style'] );
27 }
28
29 if ( isset( $styleRefs[ $block_style_ref ]['props'] ) ) {
30 LodashBasic::set( $block, 'attrs.kubio.props', $styleRefs[ $block_style_ref ]['props'] );
31 }
32 }
33 }
34 );
35
36 return wp_slash( serialize_blocks( $blocks ) );
37
38 }
39 function kubio_post_is_header( $post ) {
40 $isHeader = false;
41 if ( isset( $post ) && $post->post_type === 'wp_template_part' ) {
42 $terms = get_the_terms( $post, 'wp_template_part_area' );
43 if ( $terms ) {
44 foreach ( $terms as $term ) {
45 if ( $term->taxonomy === 'wp_template_part_area' && $term->slug === 'header' ) {
46 $isHeader = true;
47 }
48 }
49 }
50 }
51 return $isHeader;
52 }
53
54 function kubio_replace_shared_style_in_posts( $posts, $styleRefs ) {
55 remove_filter( 'wp_insert_post_data', 'kubio_on_post_update' );
56
57 try {
58 foreach ( $posts as $post ) {
59 $post_data = array();
60 $post_data['ID'] = $post->ID;
61 $post_data['post_content'] = kubio_replace_shared_style( $post->post_content, $styleRefs );
62 $result = wp_update_post( $post_data, true );
63 }
64 } catch ( Exception $e ) {
65 }
66
67 add_filter( 'wp_insert_post_data', 'kubio_on_post_update', 10, 3 );
68 }
69
70 function kubio_count_style_refs( $post_content ) {
71 $blocks = parse_blocks( $post_content );
72 $styleRefs = array();
73
74 Utils::walkBlocks(
75 $blocks,
76 function ( $block ) use ( &$styleRefs ) {
77 if ( ! isset( $block['blockName'] ) || ! str_contains( $block['blockName'], 'kubio' ) ) {
78 return;
79 }
80
81 $block_style_ref = LodashBasic::get( $block, 'attrs.kubio.styleRef', false );
82
83 if ( $block_style_ref ) {
84 if ( ! isset( $styleRefs[ $block_style_ref ] ) ) {
85 $styleRefs[ $block_style_ref ] = 0;
86 }
87
88 if ( $block_style_ref ) {
89 $styleRefs[ $block_style_ref ] = $styleRefs[ $block_style_ref ] + 1;
90 }
91 }
92 }
93 );
94
95 return $styleRefs;
96 }
97
98
99 function kubio_recursive_unset_empty_arrays( $source ) {
100 if ( is_array( $source ) ) {
101 foreach ( $source as $key => $value ) {
102 $result = kubio_recursive_unset_empty_arrays( $value );
103 if ( is_array( $result ) && empty( $result ) ) {
104 unset( $source[ $key ] );
105 } else {
106 $source[ $key ] = $result;
107 }
108 }
109 }
110
111 return $source;
112 }
113
114 function kubio_get_third_party_blocks() {
115 $thirdPartyBlocks = array(
116 'core/archives',
117 'core/calendar',
118 'core/categories',
119 'core/latest-comments',
120 'core/latest-posts',
121 'core/page-list',
122 'core/rss',
123 'core/search',
124 'core/social-links',
125 'core/tag-cloud',
126 );
127
128 return apply_filters( 'kubio/get_third_party_blocks', $thirdPartyBlocks );
129
130 }
131
132 function kubio_normalize_kubio_props( $post_content ) {
133 $blocks = parse_blocks( $post_content );
134 $third_party_blocks = kubio_get_third_party_blocks();
135 Utils::walkBlocks(
136 $blocks,
137 function ( &$block ) use ( $third_party_blocks ) {
138 if ( ! str_contains( $block['blockName'], 'kubio' ) && ! in_array( $block['blockName'], $third_party_blocks ) ) {
139 return;
140 }
141
142 $template_parts = apply_filters( 'kubio/preview/template_part_blocks', array() );
143
144 if ( in_array( $block['blockName'], $template_parts ) ) {
145 return;
146 }
147
148 $attrs = LodashBasic::get( $block, 'attrs', array() );
149 $block['attrs'] = apply_filters(
150 'kubio/modify_block_attrs_on_save',
151 kubio_recursive_unset_empty_arrays( $attrs ),
152 $block
153 );
154 }
155 );
156
157 return array(
158 wp_slash( serialize_blocks( $blocks ) ),
159 $blocks,
160 );
161
162 }
163
164 function kubio_collect_style_refs( $blocks ) {
165 $styleRefs = array();
166
167 Utils::walkBlocks(
168 $blocks,
169 function ( $block ) use ( &$styleRefs ) {
170 if ( ! isset( $block['blockName'] ) || ! str_contains( $block['blockName'], 'kubio' ) ) {
171 return;
172 }
173 $main_attr = LodashBasic::get( $block, 'attrs.kubio', array() );
174 $block_style_ref = LodashBasic::get( $main_attr, 'styleRef' );
175
176 if ( $block_style_ref ) {
177 $styleRefs[ $block_style_ref ] = $main_attr;
178 }
179 }
180 );
181
182 return $styleRefs;
183 }
184
185 function kubio_get_posts_with_style_refs( $style_refs, $exclude = array() ) {
186 $query = "SELECT * FROM wp_posts WHERE post_type != 'revision' AND (%s)";
187 $like_clauses = array();
188 $result = array();
189 foreach ( array_keys( $style_refs ) as $style_ref ) {
190 $like_clauses[] = 'post_content LIKE \'%styleRef":"' . sanitize_text_field( $style_ref ) . '"%\'';
191 }
192
193 foreach ( $exclude as $exclude_index => $to_exclude ) {
194 if ( is_numeric( $to_exclude ) ) {
195 array_splice( $exclude, $exclude_index, 1 );
196 }
197 }
198
199 if ( count( $exclude ) ) {
200 $query .= sprintf( 'AND id NOT IN (%s) ', implode( ',', $exclude ) );
201 }
202 if ( count( $like_clauses ) ) {
203 global $wpdb;
204 $query = sprintf( $query, implode( ' OR ', $like_clauses ) );
205 // the query is sanitized the OR clauses are composed
206 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
207 $result = $wpdb->get_results( $query );
208 }
209
210 return $result;
211 }
212
213 function kubio_update_meta( $post_ID, $post, $update ) {
214 $styleRefsCount = kubio_count_style_refs( $post->post_content );
215 // delete previous meta tu ensure it's unique
216 delete_post_meta( $post_ID, 'styleRefs' );
217 update_post_meta( $post_ID, 'styleRefs', $styleRefsCount );
218 }
219
220 function kubio_on_post_update( $data, $postarr, $unsanitized_postarr ) {
221 if ( is_array( $unsanitized_postarr ) && isset( $unsanitized_postarr['post_content'] ) ) {
222 $is_revision = $data['post_type'] === 'revision';
223 list( $content, $blocks ) = kubio_normalize_kubio_props( wp_unslash( $unsanitized_postarr['post_content'] ) );
224 //only update shared style for the header template part
225 $isHeader = kubio_post_is_header( (object) $postarr );
226
227 if ( ! $is_revision && $isHeader ) {
228 $style_refs = kubio_collect_style_refs( $blocks );
229 $exclude = isset( $postarr['ID'] ) ? array( $postarr['ID'] ) : array();
230 $posts_to_update = kubio_get_posts_with_style_refs( $style_refs, $exclude );
231 kubio_replace_shared_style_in_posts( $posts_to_update, $style_refs );
232 }
233
234 $data['post_content'] = $content;
235 }
236
237 return $data;
238 }
239
240 add_filter( 'wp_insert_post_data', 'kubio_on_post_update', 10, 3 );
241 add_action( 'wp_insert_post', 'kubio_update_meta', 10, 3 );
242