DemoSites.php
1 year ago
DemoSitesHelpers.php
1 month ago
DemoSitesImportBlockMap.php
1 year ago
DemoSitesImporter.php
1 year ago
DemoSitesLogger.php
4 years ago
DemoSitesRepository.php
1 year ago
WXRExporter.php
1 year ago
WXRImporter.php
1 year ago
DemoSitesImportBlockMap.php
184 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 | public static function isBlockMappedFilter( $should_map, $block_name ) { |
| 63 | |
| 64 | if ( Arr::get( static::$mapped_data, $block_name ) ) { |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | return $should_map; |
| 69 | } |
| 70 | |
| 71 | public static function applyBlockMapping( $block, $wxr_importer ) { |
| 72 | $block_name = Arr::get( $block, 'blockName', null ); |
| 73 | if ( $block_name && $map_fn = Arr::get( static::$mapped_data, $block_name ) ) { |
| 74 | return call_user_func( $map_fn, $block, $wxr_importer ); |
| 75 | } |
| 76 | |
| 77 | return $block; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @param array $block |
| 82 | * @param WXRImporter $wxr_importer |
| 83 | * |
| 84 | * @return mixed |
| 85 | */ |
| 86 | public static function updateImageGalleryData( $block, $wxr_importer ) { |
| 87 | $mappings = $wxr_importer->get_mapping(); |
| 88 | $post_mapping = Arr::get( $mappings, 'post', array() ); |
| 89 | $upload_dir = wp_upload_dir(); |
| 90 | $upload_dir_url = $upload_dir['url']; |
| 91 | |
| 92 | foreach ( Arr::get( $block, 'attrs.imagesData', array() ) as $index => $initial_image_data ) { |
| 93 | $initial_image_id = Arr::get( $initial_image_data, 'id', 0 ); |
| 94 | $image_id = Arr::get( $post_mapping, $initial_image_id, 0 ); |
| 95 | |
| 96 | if ( $image_id ) { |
| 97 | $image_data = wp_get_attachment_metadata( $image_id ); |
| 98 | |
| 99 | $sizes = array(); |
| 100 | |
| 101 | foreach ( $image_data['sizes'] as $size => $size_data ) { |
| 102 | $sizes[ $size ] = array( |
| 103 | 'url' => "$upload_dir_url/{$size_data['file']}", |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | Arr::set( |
| 108 | $block, |
| 109 | "attrs.imagesData.{$index}", |
| 110 | array_replace_recursive( |
| 111 | $initial_image_data, |
| 112 | array( |
| 113 | 'id' => $image_id, |
| 114 | 'url' => "$upload_dir_url/{$image_data['file']}", |
| 115 | 'link' => "$upload_dir_url/{$image_data['file']}", |
| 116 | 'sizes' => $sizes, |
| 117 | ) |
| 118 | ) |
| 119 | ); |
| 120 | |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return $block; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /** |
| 129 | * @param array $block |
| 130 | * @param WXRImporter $wxr_importer |
| 131 | * |
| 132 | * @return mixed |
| 133 | */ |
| 134 | public static function updateContactFormData( $block, $wxr_importer ) { |
| 135 | |
| 136 | $mappings = $wxr_importer->get_mapping(); |
| 137 | $post_mapping = Arr::get( $mappings, 'post', array() ); |
| 138 | |
| 139 | // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure |
| 140 | if ( $id = Arr::get( $block, 'attrs.formType' ) ) { |
| 141 | Arr::set( |
| 142 | $block, |
| 143 | 'attrs.formType', |
| 144 | Arr::get( $post_mapping, $id, $id ) |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure |
| 149 | if ( $id = Arr::get( $block, 'attrs.formId' ) ) { |
| 150 | Arr::set( |
| 151 | $block, |
| 152 | 'attrs.formId', |
| 153 | Arr::get( $post_mapping, $id, $id ) |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | $shortcode = Arr::get( $block, 'attrs.shortcode', '' ); |
| 158 | $shortcode = preg_replace_callback( |
| 159 | '#id="(\d+)"#', |
| 160 | function ( $matches ) use ( $post_mapping ) { |
| 161 | $id = array_pop( $matches ); |
| 162 | $id = Arr::get( $post_mapping, $id, $id ); |
| 163 | |
| 164 | return 'id="' . $id . '"'; |
| 165 | }, |
| 166 | $shortcode |
| 167 | ); |
| 168 | |
| 169 | Arr::set( $block, 'attrs.shortcode', $shortcode ); |
| 170 | |
| 171 | return $block; |
| 172 | } |
| 173 | |
| 174 | public static function updateBaseUrl( $block, $wxr_importer ) { |
| 175 | if ( ! ! ( $linkValue = Arr::get( $block, 'attrs.link.value' ) ) ) { |
| 176 | |
| 177 | $link = &$block['attrs']['link']; |
| 178 | $baseUrl = $wxr_importer->getBaseUrl(); |
| 179 | $link['value'] = str_replace( $baseUrl, site_url(), $linkValue ); |
| 180 | } |
| 181 | return $block; |
| 182 | } |
| 183 | } |
| 184 |