PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.14.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.14.0
2.14.0 2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 All 81 releases
← 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 +}