PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.4.3
Kubio AI Page Builder v2.4.3
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 / src / DemoSites / DemoSites.php
kubio / lib / src / DemoSites Last commit date
DemoSites.php 2 years ago DemoSitesHelpers.php 1 year ago DemoSitesImportBlockMap.php 4 years ago DemoSitesImporter.php 1 year ago DemoSitesLogger.php 4 years ago DemoSitesRepository.php 2 years ago WXRExporter.php 4 years ago WXRImporter.php 2 years ago
DemoSites.php
364 lines
1 <?php
2
3 namespace Kubio\DemoSites;
4
5 use IlluminateAgnostic\Arr\Support\Arr;
6 use Kubio\PluginsManager;
7
8 class DemoSites {
9
10 public function __construct() {
11 add_action( 'admin_init', array( $this, 'init' ) );
12 }
13
14 public static function load() {
15 new DemoSites();
16 }
17
18 public static function exportDemoSiteContent() {
19 $option_keys = array( 'site_icon', 'site_logo', 'page_on_front', 'page_for_posts', 'show_on_front' );
20 $dummy_fallback_value = uniqid( 'kubio-dummy-option-' );
21 $options = array();
22 foreach ( $option_keys as $option_key ) {
23 $option_value = get_option( $option_key, $dummy_fallback_value );
24
25 if ( $option_value !== $dummy_fallback_value ) {
26 $options[ $option_key ] = $option_value;
27 }
28 }
29
30 $stylesheet = get_stylesheet();
31 $template = get_template();
32 $fse_base_query = array(
33 'post_status' => array( 'publish' ),
34 'posts_per_page' => - 1,
35 'tax_query' => array(
36 array(
37 'taxonomy' => 'wp_theme',
38 'field' => 'name',
39 'terms' => array( $stylesheet, $template ),
40 ),
41 ),
42 );
43
44 $wp_templates = get_posts( array_merge( $fse_base_query, array( 'post_type' => 'wp_template' ) ) );
45 $wp_template_parts = get_posts( array_merge( $fse_base_query, array( 'post_type' => 'wp_template_part' ) ) );
46 $template_slugs = array();
47 $template_parts_slugs = array();
48
49 foreach ( $wp_templates as $wp_template ) {
50 $template_slugs[] = $wp_template->post_name;
51 }
52
53 foreach ( $wp_template_parts as $wp_template_part ) {
54 $template_parts_slugs[] = $wp_template_part->post_name;
55 }
56
57 return array(
58 'site_url' => WXRExporter::getSiteURL(),
59 'content' => WXRExporter::export(),
60 'customizer' => get_theme_mods(),
61 'options' => $options,
62 'templates' => $template_slugs,
63 'template_parts' => $template_parts_slugs,
64 );
65 }
66
67 /**
68 * Return the first template part block found or return null. Also search in inner blocks
69 * @param $block
70 * @return mixed|null
71 */
72 public static function findTemplatePartBlock( $block ) {
73 $template_part_blocks_names = array( 'kubio/header', 'kubio/footer', 'kubio/sidebar' );
74 if ( in_array( $block['blockName'], $template_part_blocks_names ) ) {
75 return $block;
76 }
77
78 foreach ( $block['innerBlocks'] as $inner_block ) {
79 $result_block = static::findTemplatePartBlock( $inner_block );
80 if ( ! ! $result_block ) {
81 return $result_block;
82 }
83 }
84 return null;
85 }
86 /*
87 * Exports demo sites splited per page, templates, tempalte parts, global data.
88 */
89 public static function exportDemoSiteContentPerPage() {
90 $stylesheet = get_stylesheet();
91 $template = get_template();
92 $fse_base_query = array(
93 'post_status' => array( 'publish' ),
94 'posts_per_page' => - 1,
95 'tax_query' => array(
96 array(
97 'taxonomy' => 'wp_theme',
98 'field' => 'name',
99 'terms' => array( $stylesheet, $template ),
100 ),
101 ),
102 );
103
104 $menu_location = 'header-menu';
105 $current_set_locations = get_nav_menu_locations();
106 $primary_menu_id = Arr::get( $current_set_locations, $menu_location, null );
107 $menu_items = wp_get_nav_menu_items( $primary_menu_id );
108
109 $menu_item_in_order = array();
110
111 //Order menu items by their position in the menu. This also treats submenus
112 static::getMenuItemsInOrder( $menu_items, 0, $menu_item_in_order );
113
114 $frontpage_id = intval( get_option( 'page_on_front' ) );
115 $blog_id = intval( get_option( 'page_for_posts' ) );
116
117 $menu_items = $menu_item_in_order;
118
119 $wp_templates = get_posts( array_merge( $fse_base_query, array( 'post_type' => 'wp_template' ) ) );
120 $wp_template_parts = get_posts( array_merge( $fse_base_query, array( 'post_type' => 'wp_template_part' ) ) );
121
122 $blog_template_slugs = array( 'index', 'archive', 'home' );
123 $single_template_slugs = array( 'single', 'singular' );
124 foreach ( $wp_templates as $key => $template ) {
125
126 if ( in_array( $template->post_name, $blog_template_slugs ) ) {
127 $wp_templates[ $key ]->page_preview_url = get_permalink( $blog_id );
128 }
129
130 if ( in_array( $template->post_name, $single_template_slugs ) ) {
131 $sample_posts = get_posts(
132 array(
133 'numberposts' => 1,
134 'post_type' => 'post',
135 )
136 );
137 if ( count( $sample_posts ) > 0 ) {
138 $wp_templates[ $key ]->page_preview_url = get_permalink( $sample_posts[0]->ID );
139 }
140 }
141 //TODO add permalinks for the other type of templates when they are needed
142
143 $content = $template->post_content;
144 $blocks = parse_blocks( $content );
145
146 if ( ! is_array( $blocks ) ) {
147 continue;
148 }
149
150 $template_parts = array();
151 foreach ( $blocks as $block ) {
152 $template_part_block = static::findTemplatePartBlock( $block );
153 if ( ! $template_part_block ) {
154 continue;
155 }
156 $template_parts[] = $template_part_block['attrs']['slug'];
157 }
158
159 $wp_templates[ $key ]->template_parts = $template_parts;
160 }
161
162 foreach ( $wp_template_parts as $template_part ) {
163 $template_part_areas = get_the_terms( $template_part->ID, 'wp_template_part_area' );
164 if ( count( $template_part_areas ) === 1 ) {
165 $template_part_area = $template_part_areas[0]->slug;
166 $template_part->area = $template_part_area;
167 }
168 }
169
170 $pages_query = array(
171 'post_status' => array( 'publish' ),
172 'post_type' => 'page',
173 'posts_per_page' => - 1,
174 );
175 $pages = get_posts( $pages_query );
176
177 foreach ( $pages as $key => $page ) {
178 $permalink = get_permalink( $page->ID );
179 $pages[ $key ]->permalink = static::removeTrailingSlashFromUrl( $permalink );
180 }
181
182 $pages_order_by_id = array();
183 $pages_title_by_id = array();
184 $page_order_value = 0;
185 if ( is_array( $menu_items ) ) {
186 foreach ( $menu_items as $key => $menu_item ) {
187 if ( $menu_item->type === 'custom' ) {
188 $url = $menu_item->url;
189 $diezPosition = strpos( $url, '#' );
190 if ( $diezPosition !== false ) {
191 $url = substr( $url, 0, $diezPosition );
192 }
193 $url = static::removeTrailingSlashFromUrl( $url );
194 foreach ( $pages as $page ) {
195 if ( $page->permalink === $url && ! array_key_exists( $page->ID, $pages_order_by_id ) ) {
196 $pages_order_by_id[ $page->ID ] = $page_order_value++;
197 }
198 }
199 }
200 if ( $menu_item->type === 'post_type' && $menu_item->object === 'page' ) {
201 $is_blog_page = intval( $menu_item->object_id ) === $blog_id;
202
203 //we don't care about the blog page, because we'll use the blog template in the import per page logic
204 if ( $is_blog_page ) {
205 continue;
206 }
207 $id = intval( $menu_item->object_id );
208 if ( ! array_key_exists( $id, $pages_order_by_id ) ) {
209
210 //You can change the label of a page even if it's page menu item
211 $pages_title_by_id[ $id ] = $menu_item->title;
212
213 //Save the order found in the menu
214 $pages_order_by_id[ $id ] = $page_order_value++;
215 }
216 }
217 }
218 }
219
220 foreach ( $pages as $key => $page ) {
221 $page_id = $page->ID;
222
223 if ( isset( $pages_order_by_id[ $page_id ] ) ) {
224 $pages[ $key ]->order = $pages_order_by_id[ $page_id ];
225 }
226 if ( isset( $pages_title_by_id[ $page_id ] ) ) {
227 $pages[ $key ]->post_title = $pages_title_by_id[ $page_id ];
228 }
229 $pages[ $key ]->page_preview_url = get_permalink( $page_id );
230 if ( $frontpage_id == $page_id ) {
231 $pages[ $key ]->template = 'front-page';
232 continue;
233 }
234
235 //remove the blog page
236 if ( $blog_id == $page_id ) {
237 unset( $pages[ $key ] );
238 continue;
239 }
240 $template = get_post_meta( $page_id, '_wp_page_template', true );
241 if ( $template === 'default' || ! $template ) {
242 $template = 'page';
243 }
244
245 $pages[ $key ]->template = $template;
246
247 }
248
249 $global_data = \kubio_get_global_data_content();
250
251 function get_post_list_content( $post_list, $extra_columns = array() ) {
252 return array_map(
253 function( $post ) use ( $extra_columns ) {
254 $extra_content = array();
255
256 foreach ( $extra_columns as $key => $post_key ) {
257 if ( isset( $post->$post_key ) ) {
258 $extra_content[ $key ] = $post->$post_key;
259 } else {
260 $extra_content[ $key ] = null;
261 }
262 }
263 return array_merge(
264 array(
265 'content' => $post->post_content,
266 'slug' => $post->post_name,
267 'title' => $post->post_title,
268 ),
269 $extra_content
270 );
271 },
272 $post_list
273 );
274 }
275
276 return array(
277 'pages' => get_post_list_content(
278 $pages,
279 array(
280 'template' => 'template',
281 'order' => 'order',
282 'page_preview_url' => 'page_preview_url',
283 )
284 ),
285 'global_data' => $global_data,
286 'templates' => get_post_list_content(
287 $wp_templates,
288 array(
289 'template_parts' => 'template_parts',
290 'page_preview_url' => 'page_preview_url',
291 )
292 ),
293 'template_parts' => get_post_list_content( $wp_template_parts, array( 'area' => 'area' ) ),
294 );
295 }
296
297 public static function getMenuItemsInOrder( $menuItems, $parentId, &$output ) {
298 foreach ( $menuItems as $key => $menuItem ) {
299 if ( intval( $menuItem->menu_item_parent ) === $parentId ) {
300 $output[] = $menuItem;
301 static::getMenuItemsInOrder( $menuItems, $menuItem->ID, $output );
302 }
303 }
304
305 return $output;
306 }
307
308 public static function removeTrailingSlashFromUrl( $url ) {
309 $last_character = substr( $url, -1 );
310 if ( $last_character === '/' ) {
311 $url = substr( $url, 0, -1 );
312 }
313 return $url;
314 }
315
316
317 public function init() {
318 DemoSitesImporter::load();
319 DemoSitesRepository::load();
320
321 add_action( 'wp_ajax_kubio-demo-site-install-plugin', array( $this, 'installPlugin' ) );
322 add_action( 'wp_ajax_kubio-demo-site-activate-plugin', array( $this, 'activatePlugin' ) );
323 }
324
325 public function installPlugin() {
326 DemoSitesHelpers::verifyAjaxCall();
327
328 $slug = sanitize_text_field( Arr::get( $_REQUEST, 'slug', null ) );
329
330 if ( empty( $slug ) ) {
331 DemoSitesHelpers::sendAjaxError( __( 'Slug not found', 'kubio' ) );
332 }
333
334 $result = PluginsManager::getInstance()->installPlugin( $slug );
335
336 //for contact form 7 we don't stop the import
337 if ( is_wp_error( $result ) && $slug !== 'contact-form-7' ) {
338 DemoSitesHelpers::sendAjaxError( $result );
339 } else {
340 wp_send_json_success();
341 }
342 }
343
344 public function activatePlugin() {
345 DemoSitesHelpers::verifyAjaxCall();
346
347 $slug = sanitize_text_field( Arr::get( $_REQUEST, 'slug', null ) );
348
349 if ( empty( $slug ) ) {
350 DemoSitesHelpers::sendAjaxError( __( 'Slug not found', 'kubio' ) );
351 }
352
353 $result = PluginsManager::getInstance()->activatePlugin( $slug, false );
354
355 //for contact form 7 we don't stop the import
356 if ( is_wp_error( $result ) && $slug !== 'contact-form-7' ) {
357 DemoSitesHelpers::sendAjaxError( $result );
358 } else {
359 wp_send_json_success();
360 }
361 }
362
363 }
364