block-based-templates
1 month ago
styles
4 years ago
customizer-options.php
1 year ago
editor-hooks.php
1 year ago
fallback-compatibility.php
4 years ago
front-page.html
2 years ago
frontend-hooks.php
1 year ago
preview.php
4 years ago
templates-importer.php
1 year ago
templates.php
1 year ago
third-party-themes.php
1 year ago
templates-importer.php
484 lines
| 1 | <?php |
| 2 | |
| 3 | use Kubio\Core\Activation; |
| 4 | use Kubio\Core\Importer; |
| 5 | use Kubio\Core\Utils; |
| 6 | use Kubio\Flags; |
| 7 | |
| 8 | class KubioThirdPartyThemeBlockImporter { |
| 9 | |
| 10 | public static function mapBlocksTemplateParts( $content ) { |
| 11 | |
| 12 | $blocks = parse_blocks( $content ); |
| 13 | $updated_blocks = kubio_blocks_update_template_parts_theme( |
| 14 | $blocks, |
| 15 | get_stylesheet() |
| 16 | ); |
| 17 | |
| 18 | return kubio_serialize_blocks( $updated_blocks ); |
| 19 | |
| 20 | return $content; |
| 21 | } |
| 22 | |
| 23 | private static function importTemplates( $mapped_templates, $is_fresh_site = false ) { |
| 24 | $files = glob( |
| 25 | KUBIO_3RD_PARTY_DEFAULT_TEMPLATES_PATH . |
| 26 | '/default-blog/templates/*.html' |
| 27 | ); |
| 28 | $templates = array(); |
| 29 | |
| 30 | foreach ( $files as $template ) { |
| 31 | $slug = preg_replace( |
| 32 | '#(.*)/templates/(.*).html#', |
| 33 | '$2', |
| 34 | wp_normalize_path( $template ) |
| 35 | ); |
| 36 | $templates[ $slug ] = $template; |
| 37 | } |
| 38 | |
| 39 | if ( $is_fresh_site ) { |
| 40 | $files = glob( |
| 41 | KUBIO_3RD_PARTY_DEFAULT_TEMPLATES_PATH . |
| 42 | '/fresh-site/templates/*.html' |
| 43 | ); |
| 44 | |
| 45 | foreach ( $files as $template ) { |
| 46 | $slug = preg_replace( |
| 47 | '#(.*)/fresh-site/templates/(.*).html#', |
| 48 | '$2', |
| 49 | wp_normalize_path( $template ) |
| 50 | ); |
| 51 | $templates[ $slug ] = $template; |
| 52 | $mapped_templates[ $slug ] = $slug; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | foreach ( $mapped_templates as $slug => $template_key ) { |
| 57 | $content = file_get_contents( $templates[ $template_key ] ); |
| 58 | $result = Importer::createTemplate( |
| 59 | $slug, |
| 60 | static::mapBlocksTemplateParts( $content ), |
| 61 | true, |
| 62 | 'kubio' |
| 63 | ); |
| 64 | |
| 65 | if ( is_wp_error( $result ) ) { |
| 66 | break; |
| 67 | return $result; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | private static function importTemplateParts( $is_fresh_site = false ) { |
| 75 | $files = glob( |
| 76 | KUBIO_3RD_PARTY_DEFAULT_TEMPLATES_PATH . |
| 77 | '/default-blog/parts/*.html' |
| 78 | ); |
| 79 | $templates = array(); |
| 80 | |
| 81 | foreach ( $files as $template ) { |
| 82 | $slug = preg_replace( |
| 83 | '#(.*)/parts/(.*).html#', |
| 84 | '$2', |
| 85 | wp_normalize_path( $template ) |
| 86 | ); |
| 87 | $templates[ $slug ] = $template; |
| 88 | } |
| 89 | |
| 90 | if ( $is_fresh_site ) { |
| 91 | $files = glob( |
| 92 | KUBIO_3RD_PARTY_DEFAULT_TEMPLATES_PATH . |
| 93 | '/fresh-site/parts/*.html' |
| 94 | ); |
| 95 | |
| 96 | foreach ( $files as $template ) { |
| 97 | $slug = preg_replace( |
| 98 | '#(.*)/fresh-site/parts/(.*).html#', |
| 99 | '$2', |
| 100 | wp_normalize_path( $template ) |
| 101 | ); |
| 102 | $templates[ $slug ] = $template; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | foreach ( $templates as $slug => $file ) { |
| 107 | $content = file_get_contents( $file ); |
| 108 | $result = Importer::createTemplatePart( |
| 109 | $slug, |
| 110 | static::mapBlocksTemplateParts( $content ), |
| 111 | false, |
| 112 | 'kubio' |
| 113 | ); |
| 114 | |
| 115 | if ( is_wp_error( $result ) ) { |
| 116 | break; |
| 117 | return $result; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | private static function importContent( $templates, $is_fresh_site = false ) { |
| 125 | $mapped_templates = static::mapTemplatesToImportSlug( $templates ); |
| 126 | |
| 127 | $result = static::importTemplates( $mapped_templates, $is_fresh_site ); |
| 128 | |
| 129 | if ( is_wp_error( $result ) ) { |
| 130 | return $result; |
| 131 | } |
| 132 | |
| 133 | $result = static::importTemplateParts( $is_fresh_site ); |
| 134 | |
| 135 | if ( is_wp_error( $result ) ) { |
| 136 | return $result; |
| 137 | } |
| 138 | |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | private static function importFSETheme( $is_fresh_site = false ) { |
| 143 | $block_templates = get_block_templates( array(), 'wp_template' ); |
| 144 | $block_templates_to_replace = array(); |
| 145 | $blog_templates_slugs = array( |
| 146 | 'home', |
| 147 | 'index', |
| 148 | 'single', |
| 149 | 'search', |
| 150 | 'archive', |
| 151 | 'singular', |
| 152 | ); |
| 153 | |
| 154 | foreach ( $block_templates as $template ) { |
| 155 | if ( in_array( $template->slug, $blog_templates_slugs, true ) ) { |
| 156 | $block_templates_to_replace[] = $template->slug; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return static::importContent( $block_templates_to_replace, $is_fresh_site ); |
| 161 | } |
| 162 | |
| 163 | private static function importClassicTheme( $is_fresh_site = false ) { |
| 164 | $theme = wp_get_theme(); |
| 165 | $files = (array) $theme->get_files( 'php', 0, true ); |
| 166 | $templates = array_keys( $files ); |
| 167 | |
| 168 | $block_templates_to_install = array( |
| 169 | 'index', |
| 170 | 'single', |
| 171 | 'search', |
| 172 | 'archive', |
| 173 | ); |
| 174 | $other_blog_related_templates = array( |
| 175 | 'home', |
| 176 | 'singular', |
| 177 | ); |
| 178 | |
| 179 | foreach ( $templates as $template ) { |
| 180 | $template_slug = preg_replace( |
| 181 | '#(.*).php#', |
| 182 | '$1', |
| 183 | wp_normalize_path( $template ) |
| 184 | ); |
| 185 | |
| 186 | if ( in_array( $template_slug, $other_blog_related_templates ) ) { |
| 187 | $block_templates_to_install[] = $template_slug; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return static::importContent( $block_templates_to_install, $is_fresh_site ); |
| 192 | } |
| 193 | |
| 194 | public static function mapTemplatesToImportSlug( $templates ) { |
| 195 | $result = array(); |
| 196 | $index_fallback_templates = array( 'home', 'index', 'archive' ); |
| 197 | $single_fallback_templates = array( 'singular' ); |
| 198 | |
| 199 | foreach ( $templates as $template ) { |
| 200 | $result[ $template ] = $template; |
| 201 | if ( in_array( $template, $index_fallback_templates, true ) ) { |
| 202 | $result[ $template ] = 'index'; |
| 203 | } |
| 204 | |
| 205 | if ( in_array( $template, $single_fallback_templates, true ) ) { |
| 206 | $result[ $template ] = 'single'; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return $result; |
| 211 | } |
| 212 | |
| 213 | private static function is_fse() { |
| 214 | $is_fse = is_readable( get_template_directory() . '/templates/index.html' ) || |
| 215 | is_readable( get_stylesheet_directory() . '/templates/index.html' ); |
| 216 | |
| 217 | return $is_fse; |
| 218 | } |
| 219 | |
| 220 | public static function import() { |
| 221 | |
| 222 | $result = null; |
| 223 | |
| 224 | if ( static::is_fse() ) { |
| 225 | $result = static::importFSETheme(); |
| 226 | } else { |
| 227 | $result = static::importClassicTheme(); |
| 228 | } |
| 229 | |
| 230 | if ( is_wp_error( $result ) ) { |
| 231 | return $result; |
| 232 | } |
| 233 | |
| 234 | return array( 'success' => true ); |
| 235 | } |
| 236 | |
| 237 | public static function importFreshSite( \WP_REST_Request $req ) { |
| 238 | |
| 239 | $response = array( |
| 240 | 'success' => true, |
| 241 | 'redirect' => Utils::kubioGetEditorURL(), |
| 242 | ); |
| 243 | $start_with_frontpage = Utils::isTrue( $req->get_param( 'start_with_frontpage' ) ); |
| 244 | |
| 245 | // for a kubio supported theme, treat it as a normal import |
| 246 | if ( kubio_theme_has_kubio_block_support() ) { |
| 247 | add_filter( 'kubio/activation/activate_with_frontpage', $start_with_frontpage ? '__return_true' : '__return_false' ); |
| 248 | add_filter( 'kubio/activation/override_front_page_content', $start_with_frontpage ? '__return_true' : '__return_false' ); |
| 249 | |
| 250 | $activation = new Activation(); |
| 251 | $activation->addCommonFilters(); |
| 252 | $activation->prepareRemoteData(); |
| 253 | |
| 254 | $activation->importDesign(); |
| 255 | $activation->importTemplates(); |
| 256 | $activation->importTemplateParts(); |
| 257 | |
| 258 | add_filter( 'kubio/importer/page_path', array( $activation, 'getDesignPagePath' ), 10, 2 ); |
| 259 | |
| 260 | } else { |
| 261 | $fp_content = ''; |
| 262 | |
| 263 | if ( static::is_fse() ) { |
| 264 | $result = static::importFSETheme( true ); |
| 265 | } else { |
| 266 | $result = static::importClassicTheme( true ); |
| 267 | } |
| 268 | |
| 269 | if ( is_wp_error( $result ) ) { |
| 270 | return $result; |
| 271 | } |
| 272 | |
| 273 | if ( $start_with_frontpage ) { |
| 274 | $fp_content = file_get_contents( KUBIO_ROOT_DIR . '/lib/integrations/third-party-themes/front-page.html' ); |
| 275 | } |
| 276 | |
| 277 | $posts_page_id = intval( get_option( 'page_for_posts' ) ); |
| 278 | |
| 279 | if ( ! $posts_page_id ) { |
| 280 | $posts_page_id = wp_insert_post( |
| 281 | array( |
| 282 | 'comment_status' => 'closed', |
| 283 | 'ping_status' => 'closed', |
| 284 | 'post_name' => 'blog', |
| 285 | 'post_title' => __( 'Blog', 'kubio' ), |
| 286 | 'post_status' => 'publish', |
| 287 | 'post_type' => 'page', |
| 288 | 'page_template' => apply_filters( |
| 289 | 'kubio/front_page_template', |
| 290 | 'page-templates/homepage.php' |
| 291 | ), |
| 292 | 'post_content' => '', |
| 293 | 'meta_input' => array( |
| 294 | '_kubio_created_at_activation' => 1, |
| 295 | ), |
| 296 | ) |
| 297 | ); |
| 298 | |
| 299 | if ( ! is_wp_error( $posts_page_id ) ) { |
| 300 | update_option( 'page_for_posts', $posts_page_id ); |
| 301 | } else { |
| 302 | return $posts_page_id; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | $page_on_front = get_option( 'page_on_front' ); |
| 307 | |
| 308 | $query = new \WP_Query( |
| 309 | array( |
| 310 | 'post__in' => array( $page_on_front ), |
| 311 | 'post_status' => array( 'publish' ), |
| 312 | 'fields' => 'ids', |
| 313 | 'post_type' => 'page', |
| 314 | ) |
| 315 | ); |
| 316 | |
| 317 | if ( ! $query->have_posts() ) { |
| 318 | $page_on_front = wp_insert_post( |
| 319 | array( |
| 320 | 'comment_status' => 'closed', |
| 321 | 'ping_status' => 'closed', |
| 322 | 'post_name' => 'front_page', |
| 323 | 'post_title' => __( 'Home', 'kubio' ), |
| 324 | 'post_status' => 'publish', |
| 325 | 'post_type' => 'page', |
| 326 | 'page_template' => 'kubio-full-width', |
| 327 | 'post_content' => wp_slash( kubio_serialize_blocks( parse_blocks( $fp_content ) ) ), |
| 328 | 'meta_input' => array( |
| 329 | '_kubio_created_at_activation' => 1, |
| 330 | ), |
| 331 | ) |
| 332 | ); |
| 333 | |
| 334 | if ( ! is_wp_error( $page_on_front ) ) { |
| 335 | update_option( 'page_on_front', $page_on_front ); |
| 336 | } else { |
| 337 | return $page_on_front; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | update_option( 'show_on_front', 'page' ); |
| 342 | |
| 343 | Activation::preparePrimaryMenu(); |
| 344 | |
| 345 | } |
| 346 | |
| 347 | if ( ! is_wp_error( $response ) ) { |
| 348 | Flags::set( 'kubio_installed_via_fresh_site', true ); |
| 349 | Flags::set( 'start_source', 'fresh-site' ); |
| 350 | } |
| 351 | |
| 352 | return $response; |
| 353 | } |
| 354 | |
| 355 | public static function registerRestRoute() { |
| 356 | $namespace = 'kubio/v1'; |
| 357 | register_rest_route( |
| 358 | $namespace, |
| 359 | '/3rd_party_themes/import_blog', |
| 360 | array( |
| 361 | 'methods' => 'GET', |
| 362 | 'callback' => array( KubioThirdPartyThemeBlockImporter::class, 'import' ), |
| 363 | 'permission_callback' => function () { |
| 364 | return current_user_can( 'edit_theme_options' ); |
| 365 | }, |
| 366 | ) |
| 367 | ); |
| 368 | |
| 369 | register_rest_route( |
| 370 | $namespace, |
| 371 | '/3rd_party_themes/import_fresh_site', |
| 372 | array( |
| 373 | 'methods' => 'GET', |
| 374 | 'callback' => array( KubioThirdPartyThemeBlockImporter::class, 'importFreshSite' ), |
| 375 | 'permission_callback' => function () { |
| 376 | return current_user_can( 'edit_theme_options' ); |
| 377 | }, |
| 378 | ) |
| 379 | ); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | add_action( |
| 384 | 'rest_api_init', |
| 385 | array( |
| 386 | KubioThirdPartyThemeBlockImporter::class, |
| 387 | 'registerRestRoute', |
| 388 | ) |
| 389 | ); |
| 390 | |
| 391 | |
| 392 | |
| 393 | function kubio_new_template_get_appropriate_content( $data ) { |
| 394 | if ( $data['post_type'] !== 'wp_template' ) { |
| 395 | return $data; |
| 396 | } |
| 397 | |
| 398 | if ( $data['post_status'] !== 'publish' ) { |
| 399 | return $data; |
| 400 | } |
| 401 | |
| 402 | if ( $data['post_content'] === '__KUBIO_REPLACE_WITH_APPROPRIATE_CONTENT__' ) { |
| 403 | $template = $data['post_name']; |
| 404 | $mapped_templates = KubioThirdPartyThemeBlockImporter::mapTemplatesToImportSlug( array( $template ) ); |
| 405 | $template = $mapped_templates[ $template ]; |
| 406 | |
| 407 | $data['post_content'] = ''; |
| 408 | |
| 409 | $file = null; |
| 410 | |
| 411 | if ( file_exists( KUBIO_3RD_PARTY_DEFAULT_TEMPLATES_PATH . "/default-blog/templates/{$template}.html" ) ) { |
| 412 | $file = KUBIO_3RD_PARTY_DEFAULT_TEMPLATES_PATH . "/default-blog/templates/{$template}.html"; |
| 413 | } |
| 414 | |
| 415 | if ( file_exists( KUBIO_3RD_PARTY_DEFAULT_TEMPLATES_PATH . "/primary/templates/{$template}.html" ) ) { |
| 416 | $file = KUBIO_3RD_PARTY_DEFAULT_TEMPLATES_PATH . "/primary/templates/{$template}.html"; |
| 417 | } |
| 418 | |
| 419 | if ( $file ) { |
| 420 | $data['post_content'] = KubioThirdPartyThemeBlockImporter::mapBlocksTemplateParts( file_get_contents( $file ) ); |
| 421 | } |
| 422 | |
| 423 | // import parts if needed |
| 424 | |
| 425 | // header |
| 426 | if ( $template === 'front-page' ) { |
| 427 | $file = KUBIO_3RD_PARTY_DEFAULT_TEMPLATES_PATH . '/primary/parts/kubio-front-header.html'; |
| 428 | $header = file_get_contents( $file ); |
| 429 | Importer::createTemplatePart( |
| 430 | 'kubio-front-header', |
| 431 | KubioThirdPartyThemeBlockImporter::mapBlocksTemplateParts( $header ), |
| 432 | false, |
| 433 | 'kubio' |
| 434 | ); |
| 435 | } else { |
| 436 | $header = file_get_contents( KUBIO_3RD_PARTY_DEFAULT_TEMPLATES_PATH . '/parts/kubio-header.html' ); |
| 437 | Importer::createTemplatePart( |
| 438 | 'kubio-header', |
| 439 | KubioThirdPartyThemeBlockImporter::mapBlocksTemplateParts( $header ), |
| 440 | false, |
| 441 | 'kubio' |
| 442 | ); |
| 443 | |
| 444 | } |
| 445 | |
| 446 | // header |
| 447 | $footer = file_get_contents( KUBIO_3RD_PARTY_DEFAULT_TEMPLATES_PATH . '/parts/kubio-footer.html' ); |
| 448 | Importer::createTemplatePart( |
| 449 | 'kubio-header', |
| 450 | KubioThirdPartyThemeBlockImporter::mapBlocksTemplateParts( $footer ), |
| 451 | false, |
| 452 | 'kubio' |
| 453 | ); |
| 454 | |
| 455 | // sidebar |
| 456 | if ( in_array( $template, array( 'index', 'single', 'search' ), true ) ) { |
| 457 | $sidebar = file_get_contents( KUBIO_3RD_PARTY_DEFAULT_TEMPLATES_PATH . '/default-blog/parts/kubio-blog-sidebar.html' ); |
| 458 | Importer::createTemplatePart( |
| 459 | 'kubio-blog-sidebar', |
| 460 | KubioThirdPartyThemeBlockImporter::mapBlocksTemplateParts( $sidebar ), |
| 461 | false, |
| 462 | 'kubio' |
| 463 | ); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | return $data; |
| 468 | } |
| 469 | |
| 470 | add_filter( |
| 471 | 'wp_insert_post_data', |
| 472 | 'kubio_new_template_get_appropriate_content', |
| 473 | 10, |
| 474 | 1 |
| 475 | ); |
| 476 | |
| 477 | |
| 478 | function kubio_add_installed_via_fresh_site_to_utils_data( $data ) { |
| 479 | $data['installedViaFreshSite'] = Flags::get( 'kubio_installed_via_fresh_site', false ); |
| 480 | return $data; |
| 481 | } |
| 482 | |
| 483 | add_filter( 'kubio/kubio-utils-data/extras', 'kubio_add_installed_via_fresh_site_to_utils_data' ); |
| 484 |