autosaves-preview.php
1 year ago
index.php
8 months ago
menu-preview.php
1 year ago
site-data-preview.php
1 year ago
autosaves-preview.php
173 lines
| 1 | <?php |
| 2 | |
| 3 | use IlluminateAgnostic\Arr\Support\Arr; |
| 4 | use IlluminateAgnostic\Str\Support\Str; |
| 5 | use Kubio\Core\Utils; |
| 6 | |
| 7 | function & _kubio_find_template_part( &$block, $availableTemplateParts ) { |
| 8 | if ( in_array( $block['blockName'], $availableTemplateParts ) ) { |
| 9 | return $block; |
| 10 | } |
| 11 | $templatePart = null; |
| 12 | if ( count( $block['innerBlocks'] ) > 0 ) { |
| 13 | foreach ( $block['innerBlocks'] as &$innerBlock ) { |
| 14 | $found_template_part = & _kubio_find_template_part( $innerBlock, $availableTemplateParts ); |
| 15 | if ( ! ! $found_template_part ) { |
| 16 | $templatePart = & $found_template_part; |
| 17 | } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | return $templatePart; |
| 22 | } |
| 23 | |
| 24 | function kubio_handle_autosaved_posts_and_templates() { |
| 25 | $autosaved_posts = kubio_get_current_changeset_data( 'autosaves', array() ); |
| 26 | |
| 27 | // remap templates to page is needed becaue in editor a page can be changed on the fly |
| 28 | $page_templates_remap = kubio_get_current_changeset_data( 'pageTemplatesMap', array() ); |
| 29 | |
| 30 | $template_part_blocks = apply_filters( 'kubio/preview/template_part_blocks', array() ); |
| 31 | $context_based_blocks = apply_filters( |
| 32 | 'kubio/preview/template_part_blocks', |
| 33 | array( |
| 34 | 'core/post-content', |
| 35 | |
| 36 | ) |
| 37 | ); |
| 38 | |
| 39 | add_filter( |
| 40 | 'render_block_data', |
| 41 | function ( $parsed_block ) use ( $autosaved_posts, $template_part_blocks ) { |
| 42 | $isTemplatePart = in_array( $parsed_block['blockName'], $template_part_blocks ); |
| 43 | $templatePartBlock = & $parsed_block; |
| 44 | |
| 45 | //search inside inner blocks for the template part. This is needed for the sidebar template part |
| 46 | if ( ! $isTemplatePart ) { |
| 47 | $nestedTemplatePartBlock = & _kubio_find_template_part( $templatePartBlock, $template_part_blocks ); |
| 48 | $isTemplatePart = ! ! $nestedTemplatePartBlock && in_array( |
| 49 | $nestedTemplatePartBlock['blockName'], |
| 50 | $template_part_blocks |
| 51 | ); |
| 52 | if ( $isTemplatePart ) { |
| 53 | $templatePartBlock = & $nestedTemplatePartBlock; |
| 54 | } |
| 55 | } |
| 56 | if ( $isTemplatePart ) { |
| 57 | $block_template_id = kubio_get_template_part_block_id( $templatePartBlock ); |
| 58 | foreach ( $autosaved_posts as $autosaved_post ) { |
| 59 | $autosaved_parent = intval( Arr::get( $autosaved_post, 'parent', 0 ) ); |
| 60 | if ( $autosaved_parent === $block_template_id ) { |
| 61 | $templatePartBlock['attrs']['postId'] = $autosaved_post['id']; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | return $parsed_block; |
| 66 | }, |
| 67 | 10, |
| 68 | 1 |
| 69 | ); |
| 70 | |
| 71 | add_filter( |
| 72 | 'render_block_context', |
| 73 | function ( $context, $parsed_block ) use ( $autosaved_posts, $context_based_blocks ) { |
| 74 | if ( in_array( $parsed_block['blockName'], $context_based_blocks ) ) { |
| 75 | foreach ( $autosaved_posts as $autosaved_post ) { |
| 76 | $autosaved_parent = intval( Arr::get( $autosaved_post, 'parent', 0 ) ); |
| 77 | if ( $autosaved_parent === Arr::get( $context, 'postId', -1 ) ) { |
| 78 | $new_post = get_post( $autosaved_post['id'] ); |
| 79 | |
| 80 | //check if revision post exists |
| 81 | if ( $new_post ) { |
| 82 | $context['postId'] = $autosaved_post['id']; |
| 83 | global $wp_query, $more; |
| 84 | //wordpress updated the core/post-content block in 5.9 to ignore the postId from context |
| 85 | if ( is_array( $wp_query->posts ) && count( $wp_query->posts ) > 0 ) { |
| 86 | $firstPost = $wp_query->posts[0]; |
| 87 | if ( $firstPost->ID === $autosaved_post['parent'] ) { |
| 88 | $wp_query->posts[0] = $new_post; |
| 89 | |
| 90 | $more = 1; |
| 91 | |
| 92 | if ( Utils::wpVersionCompare( '6.4', '>=' ) ) { |
| 93 | setup_postdata( $new_post ); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return $context; |
| 103 | }, |
| 104 | 10, |
| 105 | 2 |
| 106 | ); |
| 107 | |
| 108 | // filter template output content to use the autosaved data |
| 109 | add_filter( |
| 110 | 'kubio/template/template-loader-callback', |
| 111 | function ( $callback ) use ( $autosaved_posts, $page_templates_remap ) { |
| 112 | return function ( $template, $type, $templates ) use ( $autosaved_posts, $callback, $page_templates_remap ) { |
| 113 | |
| 114 | if ( is_page() || is_single() ) { |
| 115 | |
| 116 | if ( $type === 'frontpage' ) { |
| 117 | $templates = array( 'front-page.php' ); |
| 118 | } else { |
| 119 | $remapped_template = Arr::get( $page_templates_remap, get_the_ID(), null ); |
| 120 | |
| 121 | if ( $remapped_template ) { |
| 122 | $templates = array( $remapped_template ); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | $template_file = call_user_func( $callback, $template, $type, $templates ); |
| 128 | global $kubio_preview_located_template_data; |
| 129 | |
| 130 | $kubio_preview_located_template_data = array(); |
| 131 | |
| 132 | if ( Str::endsWith( $template_file, 'template-canvas.php' ) ) { |
| 133 | if ( Utils::wpVersionCompare( '5.9', '>=' ) ) { |
| 134 | // use fallback parameter added in 5.9 |
| 135 | /** @var WP_Block_Template $template_data */ |
| 136 | $template_data = resolve_block_template( $template, $templates, $templates[0] ); |
| 137 | } else { |
| 138 | /** @var WP_Block_Template $template_data */ |
| 139 | $template_data = resolve_block_template( $template, $templates ); |
| 140 | } |
| 141 | |
| 142 | //for third party non fse templates get classic template |
| 143 | if ( $template_data === null ) { |
| 144 | $new_template = locate_template( $templates ); |
| 145 | $kubio_preview_located_template_data = array( 'template' => $new_template ); |
| 146 | return $new_template; |
| 147 | } |
| 148 | |
| 149 | $kubio_template_source = get_post_meta( $template_data->wp_id, '_kubio_template_source', true ); |
| 150 | |
| 151 | if ( ( $kubio_template_source === 'kubio' || $kubio_template_source === 'kubio-custom' ) && function_exists( 'kubio_dequeue_theme_styles' ) ) { |
| 152 | add_action( 'wp_print_styles', 'kubio_dequeue_theme_styles', PHP_INT_MAX ); |
| 153 | } |
| 154 | |
| 155 | foreach ( $autosaved_posts as $autosaved_post ) { |
| 156 | $autosaved_parent = intval( Arr::get( $autosaved_post, 'parent', 0 ) ); |
| 157 | |
| 158 | if ( intval( $autosaved_parent ) === intval( $template_data->wp_id ) ) { |
| 159 | global $_wp_current_template_content; |
| 160 | $post = get_post( $autosaved_post['id'] ); |
| 161 | $_wp_current_template_content = $post->post_content; |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | $kubio_preview_located_template_data = array( 'template' => $template_file ); |
| 168 | return $template_file; |
| 169 | }; |
| 170 | } |
| 171 | ); |
| 172 | } |
| 173 |