child-themes.php
50 lines
| 1 | <?php |
| 2 | |
| 3 | use Kubio\Core\Activation; |
| 4 | use Kubio\Core\Importer; |
| 5 | |
| 6 | function kubio_sync_child_theme_templates_and_parts() { |
| 7 | $stylesheet = get_stylesheet(); |
| 8 | $template = get_template(); |
| 9 | |
| 10 | if ( $stylesheet !== $template ) { |
| 11 | |
| 12 | $parent_templates = kubio_get_block_templates( array( 'theme' => $template ), 'wp_template' ); |
| 13 | foreach ( $parent_templates as $parent_template ) { |
| 14 | $source = kubio_retrieve_template_source( $parent_template ); |
| 15 | $blocks = parse_blocks( $parent_template->content ); |
| 16 | $blocks = kubio_blocks_update_template_parts_theme( $blocks, $stylesheet ); |
| 17 | Importer::createTemplate( $parent_template->slug, kubio_serialize_blocks( $blocks ), false, $source ); |
| 18 | } |
| 19 | |
| 20 | $parent_templates = kubio_get_block_templates( array( 'theme' => $template ), 'wp_template_part' ); |
| 21 | foreach ( $parent_templates as $parent_template ) { |
| 22 | $source = kubio_retrieve_template_source( $parent_template ); |
| 23 | Importer::createTemplatePart( $parent_template->slug, $parent_template->content, false, $source ); |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | |
| 29 | function kubio_sync_child_theme_global_data() { |
| 30 | $stylesheet = get_stylesheet(); |
| 31 | $template = get_template(); |
| 32 | |
| 33 | if ( $stylesheet !== $template ) { |
| 34 | $has_global_data = kubio_has_global_data( $stylesheet ); |
| 35 | |
| 36 | if ( ! $has_global_data ) { |
| 37 | $global_data = kubio_get_theme_global_data_content( $template ); |
| 38 | if ( $global_data ) { |
| 39 | Activation::skipAfterSwitchTheme(); |
| 40 | kubio_replace_global_data_content( $global_data ); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | add_action( 'admin_init', 'kubio_sync_child_theme_templates_and_parts', 10 ); |
| 47 | |
| 48 | add_action( 'switch_theme', 'kubio_sync_child_theme_global_data', 5 ); // priority should be less than 10 to execute earlyear |
| 49 | add_action( 'switch_theme', 'kubio_sync_child_theme_templates_and_parts', 5 ); // priority should be less than 10 to execute earlyear |
| 50 |