DemoSites.php
2 years ago
DemoSitesHelpers.php
3 years ago
DemoSitesImportBlockMap.php
4 years ago
DemoSitesImporter.php
2 years ago
DemoSitesLogger.php
4 years ago
DemoSitesRepository.php
2 years ago
WXRExporter.php
4 years ago
WXRImporter.php
2 years ago
DemoSitesImportBlockMap.php
185 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\DemoSites; |
| 4 | |
| 5 | use IlluminateAgnostic\Arr\Support\Arr; |
| 6 | |
| 7 | class DemoSitesImportBlockMap { |
| 8 | |
| 9 | private static $mapped_data = array(); |
| 10 | |
| 11 | public static function init() { |
| 12 | add_filter( 'kubio/demo-import/should-map', array( static::class, 'isBlockMappedFilter' ), 10, 2 ); |
| 13 | add_filter( 'kubio/demo-import/apply-block-mapping', array( static::class, 'applyBlockMapping' ), 10, 2 ); |
| 14 | static::loadDefaultImports(); |
| 15 | } |
| 16 | |
| 17 | private static function loadDefaultImports() { |
| 18 | static::addMap( |
| 19 | 'kubio/contact', |
| 20 | array( |
| 21 | static::class, |
| 22 | 'updateContactFormData', |
| 23 | ) |
| 24 | ); |
| 25 | |
| 26 | static::addMap( |
| 27 | 'kubio/subscribe-form', |
| 28 | array( |
| 29 | static::class, |
| 30 | 'updateContactFormData', |
| 31 | ) |
| 32 | ); |
| 33 | |
| 34 | static::addMap( |
| 35 | 'kubio/image-gallery', |
| 36 | array( |
| 37 | static::class, |
| 38 | 'updateImageGalleryData', |
| 39 | ) |
| 40 | ); |
| 41 | |
| 42 | static::addMap( |
| 43 | 'kubio/button', |
| 44 | array( |
| 45 | static::class, |
| 46 | 'updateBaseUrl', |
| 47 | ) |
| 48 | ); |
| 49 | static::addMap( |
| 50 | 'kubio/link', |
| 51 | array( |
| 52 | static::class, |
| 53 | 'updateBaseUrl', |
| 54 | ) |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | public static function addMap( $block_name, $resolver_callback ) { |
| 59 | static::$mapped_data[ $block_name ] = $resolver_callback; |
| 60 | |
| 61 | } |
| 62 | |
| 63 | public static function isBlockMappedFilter( $should_map, $block_name ) { |
| 64 | |
| 65 | if ( Arr::get( static::$mapped_data, $block_name ) ) { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | return $should_map; |
| 70 | } |
| 71 | |
| 72 | public static function applyBlockMapping( $block, $wxr_importer ) { |
| 73 | $block_name = Arr::get( $block, 'blockName', null ); |
| 74 | if ( $block_name && $map_fn = Arr::get( static::$mapped_data, $block_name ) ) { |
| 75 | return call_user_func( $map_fn, $block, $wxr_importer ); |
| 76 | } |
| 77 | |
| 78 | return $block; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @param array $block |
| 83 | * @param WXRImporter $wxr_importer |
| 84 | * |
| 85 | * @return mixed |
| 86 | */ |
| 87 | public static function updateImageGalleryData( $block, $wxr_importer ) { |
| 88 | $mappings = $wxr_importer->get_mapping(); |
| 89 | $post_mapping = Arr::get( $mappings, 'post', array() ); |
| 90 | $upload_dir = wp_upload_dir(); |
| 91 | $upload_dir_url = $upload_dir['url']; |
| 92 | |
| 93 | foreach ( Arr::get( $block, 'attrs.imagesData', array() ) as $index => $initial_image_data ) { |
| 94 | $initial_image_id = Arr::get( $initial_image_data, 'id', 0 ); |
| 95 | $image_id = Arr::get( $post_mapping, $initial_image_id, 0 ); |
| 96 | |
| 97 | if ( $image_id ) { |
| 98 | $image_data = wp_get_attachment_metadata( $image_id ); |
| 99 | |
| 100 | $sizes = array(); |
| 101 | |
| 102 | foreach ( $image_data['sizes'] as $size => $size_data ) { |
| 103 | $sizes[ $size ] = array( |
| 104 | 'url' => "$upload_dir_url/{$size_data['file']}", |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | Arr::set( |
| 109 | $block, |
| 110 | "attrs.imagesData.{$index}", |
| 111 | array_replace_recursive( |
| 112 | $initial_image_data, |
| 113 | array( |
| 114 | 'id' => $image_id, |
| 115 | 'url' => "$upload_dir_url/{$image_data['file']}", |
| 116 | 'link' => "$upload_dir_url/{$image_data['file']}", |
| 117 | 'sizes' => $sizes, |
| 118 | ) |
| 119 | ) |
| 120 | ); |
| 121 | |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return $block; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /** |
| 130 | * @param array $block |
| 131 | * @param WXRImporter $wxr_importer |
| 132 | * |
| 133 | * @return mixed |
| 134 | */ |
| 135 | public static function updateContactFormData( $block, $wxr_importer ) { |
| 136 | |
| 137 | $mappings = $wxr_importer->get_mapping(); |
| 138 | $post_mapping = Arr::get( $mappings, 'post', array() ); |
| 139 | |
| 140 | // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure |
| 141 | if ( $id = Arr::get( $block, 'attrs.formType' ) ) { |
| 142 | Arr::set( |
| 143 | $block, |
| 144 | 'attrs.formType', |
| 145 | Arr::get( $post_mapping, $id, $id ) |
| 146 | ); |
| 147 | } |
| 148 | |
| 149 | // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure |
| 150 | if ( $id = Arr::get( $block, 'attrs.formId' ) ) { |
| 151 | Arr::set( |
| 152 | $block, |
| 153 | 'attrs.formId', |
| 154 | Arr::get( $post_mapping, $id, $id ) |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | $shortcode = Arr::get( $block, 'attrs.shortcode', '' ); |
| 159 | $shortcode = preg_replace_callback( |
| 160 | '#id="(\d+)"#', |
| 161 | function ( $matches ) use ( $post_mapping ) { |
| 162 | $id = array_pop( $matches ); |
| 163 | $id = Arr::get( $post_mapping, $id, $id ); |
| 164 | |
| 165 | return 'id="' . $id . '"'; |
| 166 | }, |
| 167 | $shortcode |
| 168 | ); |
| 169 | |
| 170 | Arr::set( $block, 'attrs.shortcode', $shortcode ); |
| 171 | |
| 172 | return $block; |
| 173 | } |
| 174 | |
| 175 | public static function updateBaseUrl( $block, $wxr_importer ) { |
| 176 | if ( ! ! ( $linkValue = Arr::get( $block, 'attrs.link.value' ) ) ) { |
| 177 | |
| 178 | $link = &$block['attrs']['link']; |
| 179 | $baseUrl = $wxr_importer->getBaseUrl(); |
| 180 | $link['value'] = str_replace( $baseUrl, site_url(), $linkValue ); |
| 181 | } |
| 182 | return $block; |
| 183 | } |
| 184 | } |
| 185 |