← All changes
|
includes/import/image-parsers/ablocks/tabs-parser.php
+37
-0
2.7.7
→
2.15.0
View file →
| @@ -1,0 +1,37 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace ABlocks\import\ImageParsers\ablocks; | |
| 4 | + | |
| 5 | +use ABlocks\import\ImageParsers\AbstractImageParser; | |
| 6 | +use DOMDocument; | |
| 7 | + | |
| 8 | +class TabsParser extends AbstractImageParser { | |
| 9 | + | |
| 10 | + protected function parse() { | |
| 11 | + if ( ! isset( $this->block['attrs']['iconImageUrl'] ) ) { | |
| 12 | + return; | |
| 13 | + } | |
| 14 | + | |
| 15 | + $dom = new DOMDocument(); | |
| 16 | + $dom->loadHTML( $this->block['innerHTML'], LIBXML_NOERROR | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); | |
| 17 | + | |
| 18 | + $image_url = $this->block['attrs']['iconImageUrl']; | |
| 19 | + $images = $dom->getElementsByTagName( 'img' ); | |
| 20 | + $remote_images = []; | |
| 21 | + | |
| 22 | + $i = 1; | |
| 23 | + while ( $i < $images->count() ) { | |
| 24 | + $image = $images->item( $i ); | |
| 25 | + $i++; | |
| 26 | + | |
| 27 | + $remote_image_url = $image->getAttribute( 'src' ); | |
| 28 | + if ( ! $this->check_external_url( $remote_image_url ) ) { | |
| 29 | + continue; | |
| 30 | + } | |
| 31 | + $remote_images[] = $remote_image_url; | |
| 32 | + } | |
| 33 | + | |
| 34 | + $this->block['innerHTML'] = str_replace( $remote_images, $image_url, $this->block['innerHTML'] ); | |
| 35 | + $this->block['innerContent'] = array_map( fn( $content) => str_replace( $remote_images, $image_url, $content ), $this->block['innerContent'] ); | |
| 36 | + } | |
| 37 | +} | |