|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Adds data entry capabilities. |
| 4 | + * |
| 5 | + * @package data-types |
| 6 | + */ |
| 7 | + |
| 8 | +add_action( 'save_post', 'redirect_content_areas', 99, 2 ); |
| 9 | + |
| 10 | +/** |
| 11 | + * Redirects content areas to the appropriate place after saving the post. |
| 12 | + * |
| 13 | + * @param int $post_id The post ID. |
| 14 | + * @param WP_Post $post The post. |
| 15 | + */ |
| 16 | +function redirect_content_areas( $post_id, $post ) { |
| 17 | + // TODO: Include all data types. |
| 18 | + if ( 'movie' !== $post->post_type || 'publish' !== $post->post_status ) { |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + remove_action( 'save_post', 'redirect_content_areas', 99 ); |
| 27 | + |
| 28 | + $blocks = parse_blocks( wp_unslash( $post->post_content ) ); |
| 29 | + |
| 30 | + $data = extract_content_from_blocks( $blocks ); |
| 31 | + |
| 32 | + wp_update_post( |
| 33 | + array( |
| 34 | + 'ID' => $post_id, |
| 35 | + 'post_content' => $data['post_content'], |
| 36 | + 'meta_input' => $data['meta_fields'], |
| 37 | + ) |
| 38 | + ); |
| 39 | + |
| 40 | + add_action( 'save_post', 'redirect_content_areas', 99, 2 ); |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Extract contents from the blocks. |
| 45 | + * |
| 46 | + * TODO: Fix recursion. |
| 47 | + * |
| 48 | + * @param array $blocks The blocks from the post. |
| 49 | + */ |
| 50 | +function extract_content_from_blocks( $blocks ) { |
| 51 | + $data = array( |
| 52 | + 'post_content' => '', |
| 53 | + 'meta_fields' => array(), |
| 54 | + ); |
| 55 | + |
| 56 | + foreach ( $blocks as $block ) { |
| 57 | + $binding = $block['attrs']['metadata']['data-types/binding'] ?? null; |
| 58 | + |
| 59 | + if ( is_null( $binding ) ) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + if ( 'post_content' === $binding ) { |
| 64 | + $data['post_content'] = serialize_blocks( $block['innerBlocks'] ); |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + $data['meta_fields'][ $binding ] = serialize_blocks( $block['innerBlocks'] ); |
| 69 | + } |
| 70 | + |
| 71 | + return $data; |
| 72 | +} |
| 73 | + |
| 74 | +add_action( 'the_post', 'hydrate_data_with_content' ); |
| 75 | + |
| 76 | +/** |
| 77 | + * In the editor, hydrate the template with the actual data and display it. |
| 78 | + * |
| 79 | + * @param WP_Post $post The current post. |
| 80 | + */ |
| 81 | +function hydrate_data_with_content( $post ) { |
| 82 | + // TODO: Include all data types. |
| 83 | + if ( 'movie' !== $post->post_type ) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + // TODO: Get the template from the post type. |
| 88 | + $template = get_post( 80 ); |
| 89 | + $template = parse_blocks( $template->post_content ); |
| 90 | + |
| 91 | + // TODO: Fix recursion. |
| 92 | + foreach ( $template as $key => $block ) { |
| 93 | + $binding = $block['attrs']['metadata']['data-types/binding'] ?? null; |
| 94 | + |
| 95 | + if ( is_null( $binding ) ) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + if ( 'post_content' === $binding ) { |
| 100 | + $content = $post->post_content; |
| 101 | + } else { |
| 102 | + $content = get_post_meta( get_the_ID(), $binding, true ); |
| 103 | + } |
| 104 | + |
| 105 | + $template[ $key ]['innerBlocks'] = parse_blocks( $content ); |
| 106 | + |
| 107 | + $new_content = backfill_html( $block['innerHTML'], $content ); |
| 108 | + |
| 109 | + $template[ $key ]['innerHTML'] = $new_content; |
| 110 | + $template[ $key ]['innerContent'] = array( $new_content ); |
| 111 | + } |
| 112 | + |
| 113 | + $post->post_content = serialize_blocks( $template ); |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Backfill the HTML. |
| 118 | + * |
| 119 | + * @param string $inner_html The markup. |
| 120 | + * @param string $block_content The content of the block. |
| 121 | + */ |
| 122 | +function backfill_html( $inner_html, $block_content ) { |
| 123 | + $p = new WP_HTML_Tag_Processor( $inner_html ); |
| 124 | + $p->next_tag(); |
| 125 | + $p2 = new WP_HTML_Tag_Processor( '<div>' ); |
| 126 | + $p2->next_tag(); |
| 127 | + foreach ( $p->get_attribute_names_with_prefix( '' ) ?? array() as $attribute ) { |
| 128 | + $p2->set_attribute( $attribute, $p->get_attribute( $attribute ) ); |
| 129 | + } |
| 130 | + |
| 131 | + return $p2->get_updated_html() . $block_content . '</div>'; |
| 132 | +} |
0 commit comments