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