← All changes
|
includes/import/image-parsers/ablocks/image-parser.php
+45
-0
2.7.7
→
2.14.0
View file →
| @@ -1,0 +1,45 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace ABlocks\import\ImageParsers\ablocks; | |
| 4 | + | |
| 5 | +use ABlocks\import\ImageParsers\AbstractImageParser; | |
| 6 | +use DOMDocument; | |
| 7 | + | |
| 8 | +class ImageParser extends AbstractImageParser { | |
| 9 | + | |
| 10 | + protected function parse() { | |
| 11 | + $dom = new DOMDocument(); | |
| 12 | + $dom->loadHTML( $this->block['innerHTML'], LIBXML_NOERROR | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); | |
| 13 | + $source_elements = $dom->getElementsByTagName( 'source' ); | |
| 14 | + | |
| 15 | + $parsed_img = $this->parse_img_tag( $dom, 0 ); | |
| 16 | + if ( ! $parsed_img ) { | |
| 17 | + return; | |
| 18 | + } | |
| 19 | + list($attachment_id, $attachment_url, $image_url) = $parsed_img; | |
| 20 | + $remote_image_url = [ $image_url => $attachment_url ]; | |
| 21 | + | |
| 22 | + $i = 0; | |
| 23 | + while ( $i < $source_elements->count() ) { | |
| 24 | + $source_element = $source_elements->item( $i ); | |
| 25 | + $i++; | |
| 26 | + | |
| 27 | + $source_image_url = $source_element->getAttribute( 'srcset' ); | |
| 28 | + if ( ! $source_image_url || ! $this->check_external_url( $source_image_url ) ) { | |
| 29 | + continue; | |
| 30 | + } | |
| 31 | + | |
| 32 | + $attachment_id = $this->upload_file( $source_image_url ); | |
| 33 | + if ( is_wp_error( $attachment_id ) ) { | |
| 34 | + continue; | |
| 35 | + } | |
| 36 | + | |
| 37 | + $source_attachment_url = wp_get_attachment_url( $attachment_id ); | |
| 38 | + $remote_image_url[ $source_image_url ] = $source_attachment_url; | |
| 39 | + } | |
| 40 | + | |
| 41 | + $this->block['attrs']['imgId'] = $attachment_id; | |
| 42 | + $this->block['innerHTML'] = str_replace( array_keys( $remote_image_url ), array_values( $remote_image_url ), $this->block['innerHTML'] ); | |
| 43 | + $this->block['innerContent'] = array_map( fn( $content) => str_replace( array_keys( $remote_image_url ), array_values( $remote_image_url ), $content ), $this->block['innerContent'] ); | |
| 44 | + } | |
| 45 | +} | |