DemoSites.php
1 year ago
DemoSitesHelpers.php
1 year ago
DemoSitesImportBlockMap.php
1 year ago
DemoSitesImporter.php
1 year ago
DemoSitesLogger.php
4 years ago
DemoSitesRepository.php
1 year ago
WXRExporter.php
1 year ago
WXRImporter.php
1 year ago
DemoSites.php
367 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 | // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 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 $inner_block ) { |
| 80 | $result_block = static::findTemplatePartBlock( $inner_block ); |
| 81 | if ( ! ! $result_block ) { |
| 82 | return $result_block; |
| 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 | // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 97 | 'tax_query' => array( |
| 98 | array( |
| 99 | 'taxonomy' => 'wp_theme', |
| 100 | 'field' => 'name', |
| 101 | 'terms' => array( $stylesheet, $template ), |
| 102 | ), |
| 103 | ), |
| 104 | ); |
| 105 | |
| 106 | $menu_location = 'header-menu'; |
| 107 | $current_set_locations = get_nav_menu_locations(); |
| 108 | $primary_menu_id = Arr::get( $current_set_locations, $menu_location, null ); |
| 109 | $menu_items = wp_get_nav_menu_items( $primary_menu_id ); |
| 110 | |
| 111 | $menu_item_in_order = array(); |
| 112 | |
| 113 | //Order menu items by their position in the menu. This also treats submenus |
| 114 | static::getMenuItemsInOrder( $menu_items, 0, $menu_item_in_order ); |
| 115 | |
| 116 | $frontpage_id = intval( get_option( 'page_on_front' ) ); |
| 117 | $blog_id = intval( get_option( 'page_for_posts' ) ); |
| 118 | |
| 119 | $menu_items = $menu_item_in_order; |
| 120 | |
| 121 | $wp_templates = get_posts( array_merge( $fse_base_query, array( 'post_type' => 'wp_template' ) ) ); |
| 122 | $wp_template_parts = get_posts( array_merge( $fse_base_query, array( 'post_type' => 'wp_template_part' ) ) ); |
| 123 | |
| 124 | $blog_template_slugs = array( 'index', 'archive', 'home' ); |
| 125 | $single_template_slugs = array( 'single', 'singular' ); |
| 126 | foreach ( $wp_templates as $key => $template ) { |
| 127 | |
| 128 | if ( in_array( $template->post_name, $blog_template_slugs ) ) { |
| 129 | $wp_templates[ $key ]->page_preview_url = get_permalink( $blog_id ); |
| 130 | } |
| 131 | |
| 132 | if ( in_array( $template->post_name, $single_template_slugs ) ) { |
| 133 | $sample_posts = get_posts( |
| 134 | array( |
| 135 | 'numberposts' => 1, |
| 136 | 'post_type' => 'post', |
| 137 | ) |
| 138 | ); |
| 139 | if ( count( $sample_posts ) > 0 ) { |
| 140 | $wp_templates[ $key ]->page_preview_url = get_permalink( $sample_posts[0]->ID ); |
| 141 | } |
| 142 | } |
| 143 | //TODO add permalinks for the other type of templates when they are needed |
| 144 | |
| 145 | $content = $template->post_content; |
| 146 | $blocks = parse_blocks( $content ); |
| 147 | |
| 148 | if ( ! is_array( $blocks ) ) { |
| 149 | continue; |
| 150 | } |
| 151 | |
| 152 | $template_parts = array(); |
| 153 | foreach ( $blocks as $block ) { |
| 154 | $template_part_block = static::findTemplatePartBlock( $block ); |
| 155 | if ( ! $template_part_block ) { |
| 156 | continue; |
| 157 | } |
| 158 | $template_parts[] = $template_part_block['attrs']['slug']; |
| 159 | } |
| 160 | |
| 161 | $wp_templates[ $key ]->template_parts = $template_parts; |
| 162 | } |
| 163 | |
| 164 | foreach ( $wp_template_parts as $template_part ) { |
| 165 | $template_part_areas = get_the_terms( $template_part->ID, 'wp_template_part_area' ); |
| 166 | if ( count( $template_part_areas ) === 1 ) { |
| 167 | $template_part_area = $template_part_areas[0]->slug; |
| 168 | $template_part->area = $template_part_area; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | $pages_query = array( |
| 173 | 'post_status' => array( 'publish' ), |
| 174 | 'post_type' => 'page', |
| 175 | 'posts_per_page' => - 1, |
| 176 | ); |
| 177 | $pages = get_posts( $pages_query ); |
| 178 | |
| 179 | foreach ( $pages as $key => $page ) { |
| 180 | $permalink = get_permalink( $page->ID ); |
| 181 | $pages[ $key ]->permalink = static::removeTrailingSlashFromUrl( $permalink ); |
| 182 | } |
| 183 | |
| 184 | $pages_order_by_id = array(); |
| 185 | $pages_title_by_id = array(); |
| 186 | $page_order_value = 0; |
| 187 | if ( is_array( $menu_items ) ) { |
| 188 | foreach ( $menu_items as $key => $menu_item ) { |
| 189 | if ( $menu_item->type === 'custom' ) { |
| 190 | $url = $menu_item->url; |
| 191 | $diezPosition = strpos( $url, '#' ); |
| 192 | if ( $diezPosition !== false ) { |
| 193 | $url = substr( $url, 0, $diezPosition ); |
| 194 | } |
| 195 | $url = static::removeTrailingSlashFromUrl( $url ); |
| 196 | foreach ( $pages as $page ) { |
| 197 | if ( $page->permalink === $url && ! array_key_exists( $page->ID, $pages_order_by_id ) ) { |
| 198 | $pages_order_by_id[ $page->ID ] = $page_order_value++; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | if ( $menu_item->type === 'post_type' && $menu_item->object === 'page' ) { |
| 203 | $is_blog_page = intval( $menu_item->object_id ) === $blog_id; |
| 204 | |
| 205 | //we don't care about the blog page, because we'll use the blog template in the import per page logic |
| 206 | if ( $is_blog_page ) { |
| 207 | continue; |
| 208 | } |
| 209 | $id = intval( $menu_item->object_id ); |
| 210 | if ( ! array_key_exists( $id, $pages_order_by_id ) ) { |
| 211 | |
| 212 | //You can change the label of a page even if it's page menu item |
| 213 | $pages_title_by_id[ $id ] = $menu_item->title; |
| 214 | |
| 215 | //Save the order found in the menu |
| 216 | $pages_order_by_id[ $id ] = $page_order_value++; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | foreach ( $pages as $key => $page ) { |
| 223 | $page_id = $page->ID; |
| 224 | |
| 225 | if ( isset( $pages_order_by_id[ $page_id ] ) ) { |
| 226 | $pages[ $key ]->order = $pages_order_by_id[ $page_id ]; |
| 227 | } |
| 228 | if ( isset( $pages_title_by_id[ $page_id ] ) ) { |
| 229 | $pages[ $key ]->post_title = $pages_title_by_id[ $page_id ]; |
| 230 | } |
| 231 | $pages[ $key ]->page_preview_url = get_permalink( $page_id ); |
| 232 | if ( $frontpage_id == $page_id ) { |
| 233 | $pages[ $key ]->template = 'front-page'; |
| 234 | continue; |
| 235 | } |
| 236 | |
| 237 | //remove the blog page |
| 238 | if ( $blog_id == $page_id ) { |
| 239 | unset( $pages[ $key ] ); |
| 240 | continue; |
| 241 | } |
| 242 | $template = get_post_meta( $page_id, '_wp_page_template', true ); |
| 243 | if ( $template === 'default' || ! $template ) { |
| 244 | $template = 'page'; |
| 245 | } |
| 246 | |
| 247 | $pages[ $key ]->template = $template; |
| 248 | |
| 249 | } |
| 250 | |
| 251 | $global_data = \kubio_get_global_data_content(); |
| 252 | |
| 253 | function get_post_list_content( $post_list, $extra_columns = array() ) { |
| 254 | return array_map( |
| 255 | function ( $post ) use ( $extra_columns ) { |
| 256 | $extra_content = array(); |
| 257 | |
| 258 | foreach ( $extra_columns as $key => $post_key ) { |
| 259 | if ( isset( $post->$post_key ) ) { |
| 260 | $extra_content[ $key ] = $post->$post_key; |
| 261 | } else { |
| 262 | $extra_content[ $key ] = null; |
| 263 | } |
| 264 | } |
| 265 | return array_merge( |
| 266 | array( |
| 267 | 'content' => $post->post_content, |
| 268 | 'slug' => $post->post_name, |
| 269 | 'title' => $post->post_title, |
| 270 | ), |
| 271 | $extra_content |
| 272 | ); |
| 273 | }, |
| 274 | $post_list |
| 275 | ); |
| 276 | } |
| 277 | |
| 278 | return array( |
| 279 | 'pages' => get_post_list_content( |
| 280 | $pages, |
| 281 | array( |
| 282 | 'template' => 'template', |
| 283 | 'order' => 'order', |
| 284 | 'page_preview_url' => 'page_preview_url', |
| 285 | ) |
| 286 | ), |
| 287 | 'global_data' => $global_data, |
| 288 | 'templates' => get_post_list_content( |
| 289 | $wp_templates, |
| 290 | array( |
| 291 | 'template_parts' => 'template_parts', |
| 292 | 'page_preview_url' => 'page_preview_url', |
| 293 | ) |
| 294 | ), |
| 295 | 'template_parts' => get_post_list_content( $wp_template_parts, array( 'area' => 'area' ) ), |
| 296 | ); |
| 297 | } |
| 298 | |
| 299 | public static function getMenuItemsInOrder( $menuItems, $parentId, &$output ) { |
| 300 | foreach ( $menuItems as $key => $menuItem ) { |
| 301 | if ( intval( $menuItem->menu_item_parent ) === $parentId ) { |
| 302 | $output[] = $menuItem; |
| 303 | static::getMenuItemsInOrder( $menuItems, $menuItem->ID, $output ); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | return $output; |
| 308 | } |
| 309 | |
| 310 | public static function removeTrailingSlashFromUrl( $url ) { |
| 311 | $last_character = substr( $url, -1 ); |
| 312 | if ( $last_character === '/' ) { |
| 313 | $url = substr( $url, 0, -1 ); |
| 314 | } |
| 315 | return $url; |
| 316 | } |
| 317 | |
| 318 | |
| 319 | public function init() { |
| 320 | DemoSitesImporter::load(); |
| 321 | DemoSitesRepository::load(); |
| 322 | |
| 323 | add_action( 'wp_ajax_kubio-demo-site-install-plugin', array( $this, 'installPlugin' ) ); |
| 324 | add_action( 'wp_ajax_kubio-demo-site-activate-plugin', array( $this, 'activatePlugin' ) ); |
| 325 | } |
| 326 | |
| 327 | public function installPlugin() { |
| 328 | DemoSitesHelpers::verifyAjaxCall(); |
| 329 | |
| 330 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 331 | $slug = sanitize_text_field( Arr::get( $_REQUEST, 'slug', null ) ); |
| 332 | |
| 333 | if ( empty( $slug ) ) { |
| 334 | DemoSitesHelpers::sendAjaxError( __( 'Slug not found', 'kubio' ) ); |
| 335 | } |
| 336 | |
| 337 | $result = PluginsManager::getInstance()->installPlugin( $slug ); |
| 338 | |
| 339 | //for contact form 7 we don't stop the import |
| 340 | if ( is_wp_error( $result ) && $slug !== 'contact-form-7' ) { |
| 341 | DemoSitesHelpers::sendAjaxError( $result ); |
| 342 | } else { |
| 343 | wp_send_json_success(); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | public function activatePlugin() { |
| 348 | DemoSitesHelpers::verifyAjaxCall(); |
| 349 | |
| 350 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 351 | $slug = sanitize_text_field( Arr::get( $_REQUEST, 'slug', null ) ); |
| 352 | |
| 353 | if ( empty( $slug ) ) { |
| 354 | DemoSitesHelpers::sendAjaxError( __( 'Slug not found', 'kubio' ) ); |
| 355 | } |
| 356 | |
| 357 | $result = PluginsManager::getInstance()->activatePlugin( $slug, false ); |
| 358 | |
| 359 | //for contact form 7 we don't stop the import |
| 360 | if ( is_wp_error( $result ) && $slug !== 'contact-form-7' ) { |
| 361 | DemoSitesHelpers::sendAjaxError( $result ); |
| 362 | } else { |
| 363 | wp_send_json_success(); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 |