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/wp/core-image-parser.php +43 -0 2.7.4 → 2.14.0 View file →
@@ -1,0 +1,43 @@
1 +<?php
2 +
3 +namespace ABlocks\import\ImageParsers\wp;
4 +
5 +use ABlocks\import\ImageParsers\AbstractImageParser;
6 +use DOMDocument;
7 +
8 +class CoreImageParser extends AbstractImageParser {
9 +
10 + public function parse() {
11 + $dom = new DOMDocument();
12 + // Suppress warnings for malformed HTML.
13 + libxml_use_internal_errors( true );
14 + $dom->loadHTML( $this->block['innerHTML'] );
15 + libxml_clear_errors();
16 + $imageHTML = $dom->getElementsByTagName( 'img' )->item( 0 );
17 + if ( ! $imageHTML ) {
18 + return;
19 + }
20 +
21 + $imgSRC = $imageHTML->attributes->getNamedItem( 'src' );
22 + if ( ! $imgSRC ) {
23 + return;
24 + }
25 +
26 + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Calling a property from native class `DOMDocument`.
27 + $imgURL = $imgSRC->nodeValue;
28 + if ( ! $this->check_external_url( $imgURL ) ) {
29 + return;
30 + }
31 +
32 + $attachment_id = $this->upload_file( $imgURL );
33 + if ( is_wp_error( $attachment_id ) ) {
34 + return;
35 + }
36 +
37 + $attachment_url = wp_get_attachment_url( $attachment_id );
38 +
39 + $this->block['attrs']['id'] = $attachment_id;
40 + $this->block['innerHTML'] = str_replace( $imgURL, $attachment_url, $this->block['innerHTML'] );
41 + $this->block['innerContent'] = array( str_replace( $imgURL, $attachment_url, $this->block['innerHTML'] ) );
42 + }
43 +}