← All changes
|
includes/import/image-parsers/ablocks/image-hotspot-parser.php
+49
-0
2.7.4
→
2.14.0
View file →
| @@ -1,0 +1,49 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace ABlocks\import\ImageParsers\ablocks; | |
| 4 | + | |
| 5 | +use ABlocks\import\ImageParsers\AbstractImageParser; | |
| 6 | + | |
| 7 | +class ImageHotspotParser extends AbstractImageParser { | |
| 8 | + | |
| 9 | + protected function parse() { | |
| 10 | + if ( ! isset( $this->block['attrs']['backgroundImage'] ) ) { | |
| 11 | + return; | |
| 12 | + } | |
| 13 | + | |
| 14 | + $image_url = $this->block['attrs']['backgroundImage']; | |
| 15 | + if ( ! $this->check_external_url( $image_url ) ) { | |
| 16 | + return; | |
| 17 | + } | |
| 18 | + | |
| 19 | + $attachment_id = $this->upload_file( $image_url ); | |
| 20 | + if ( is_wp_error( $attachment_id ) ) { | |
| 21 | + return; | |
| 22 | + } | |
| 23 | + | |
| 24 | + $attachment_url = wp_get_attachment_url( $attachment_id ); | |
| 25 | + $this->block['attrs']['backgroundImage'] = $attachment_url; | |
| 26 | + $remote_images = [ $image_url => $attachment_url ]; | |
| 27 | + | |
| 28 | + if ( isset( $this->block['attrs']['imageSizes']['thumbnail'] ) ) { | |
| 29 | + $thumbnail_url = wp_get_attachment_image_url( $attachment_id ); | |
| 30 | + $remote_images[ $this->block['attrs']['imageSizes']['thumbnail']['url'] ] = $thumbnail_url; | |
| 31 | + $this->block['attrs']['imageSizes']['thumbnail']['url'] = $thumbnail_url; | |
| 32 | + } | |
| 33 | + | |
| 34 | + if ( isset( $this->block['attrs']['imageSizes']['medium'] ) ) { | |
| 35 | + $medium_url = wp_get_attachment_image_url( $attachment_id, 'medium' ); | |
| 36 | + $remote_images[ $this->block['attrs']['imageSizes']['medium']['url'] ] = $medium_url; | |
| 37 | + $this->block['attrs']['imageSizes']['medium']['url'] = $medium_url; | |
| 38 | + } | |
| 39 | + | |
| 40 | + if ( isset( $this->block['attrs']['imageSizes']['full'] ) ) { | |
| 41 | + $full_url = wp_get_attachment_image_url( $attachment_id, 'full' ); | |
| 42 | + $remote_images[ $this->block['attrs']['imageSizes']['full']['url'] ] = $full_url; | |
| 43 | + $this->block['attrs']['imageSizes']['full']['url'] = $full_url; | |
| 44 | + } | |
| 45 | + | |
| 46 | + $this->block['innerHTML'] = str_replace( array_keys( $remote_images ), array_values( $remote_images ), $this->block['innerHTML'] ); | |
| 47 | + $this->block['innerContent'] = array_map( fn( $content) => str_replace( array_keys( $remote_images ), array_values( $remote_images ), $content ), $this->block['innerContent'] ); | |
| 48 | + } | |
| 49 | +} | |