| 1 |
<?php |
| 2 |
|
| 3 |
namespace ABlocks\import\ImageParsers; |
| 4 |
|
| 5 |
use DOMDocument; |
| 6 |
|
| 7 |
class CommonParser extends AbstractImageParser { |
| 8 |
|
| 9 |
protected function parse() { |
| 10 |
$this->parse_background(); |
| 11 |
$this->parse_background_hover(); |
| 12 |
$this->parse_background_overlay(); |
| 13 |
$this->parse_background_overlay_hover(); |
| 14 |
$this->parse_icon(); |
| 15 |
} |
| 16 |
|
| 17 |
private function parse_background() { |
| 18 |
if ( ! isset( $this->block['attrs']['_background'], $this->block['attrs']['_background']['backgroundType'] ) || 'image' !== $this->block['attrs']['_background']['backgroundType'] ) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
$imgUrl = $this->block['attrs']['_background']['imgUrl']; |
| 23 |
if ( empty( $imgUrl ) ) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
if ( $this->check_external_url( $imgUrl ) ) { |
| 28 |
$attachment_id = $this->upload_file( $imgUrl ); |
| 29 |
if ( is_wp_error( $attachment_id ) ) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
$attachment_url = wp_get_attachment_url( $attachment_id ); |
| 34 |
$this->block['attrs']['_background']['imgId'] = $attachment_id; |
| 35 |
$this->block['attrs']['_background']['imgUrl'] = $attachment_url; |
| 36 |
} |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
private function parse_background_hover() { |
| 41 |
if ( ! isset( $this->block['attrs']['_background'], $this->block['attrs']['_background']['backgroundTypeH'] ) || 'image' !== $this->block['attrs']['_background']['backgroundTypeH'] ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
$imgUrl = $this->block['attrs']['_background']['imgUrlH']; |
| 46 |
if ( empty( $imgUrl ) ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
if ( $this->check_external_url( $imgUrl ) ) { |
| 51 |
$attachment_id = $this->upload_file( $imgUrl ); |
| 52 |
if ( is_wp_error( $attachment_id ) ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
$attachment_url = wp_get_attachment_url( $attachment_id ); |
| 57 |
$this->block['attrs']['_background']['imgIdH'] = $attachment_id; |
| 58 |
$this->block['attrs']['_background']['imgUrlH'] = $attachment_url; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
private function parse_background_overlay() { |
| 63 |
if ( ! isset( $this->block['attrs']['_backgroundOverlay'], $this->block['attrs']['_backgroundOverlay']['backgroundOverlayType'] ) || 'image' !== $this->block['attrs']['_backgroundOverlay']['backgroundOverlayType'] ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
$imgUrl = $this->block['attrs']['_backgroundOverlay']['imgUrl']; |
| 68 |
if ( empty( $imgUrl ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
if ( $this->check_external_url( $imgUrl ) ) { |
| 73 |
$attachment_id = $this->upload_file( $imgUrl ); |
| 74 |
if ( is_wp_error( $attachment_id ) ) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
$attachment_url = wp_get_attachment_url( $attachment_id ); |
| 79 |
$this->block['attrs']['_backgroundOverlay']['imgId'] = $attachment_id; |
| 80 |
$this->block['attrs']['_backgroundOverlay']['imgUrl'] = $attachment_url; |
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
private function parse_background_overlay_hover() { |
| 86 |
if ( ! isset( $this->block['attrs']['_backgroundOverlay'] ) || 'image' !== $this->block['attrs']['_backgroundOverlay']['backgroundOverlayTypeH'] ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
$imgUrl = $this->block['attrs']['_backgroundOverlay']['imgUrlH']; |
| 91 |
if ( empty( $imgUrl ) ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
if ( $this->check_external_url( $imgUrl ) ) { |
| 96 |
$attachment_id = $this->upload_file( $imgUrl ); |
| 97 |
if ( is_wp_error( $attachment_id ) ) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
$attachment_url = wp_get_attachment_url( $attachment_id ); |
| 102 |
$this->block['attrs']['_backgroundOverlay']['imgIdH'] = $attachment_id; |
| 103 |
$this->block['attrs']['_backgroundOverlay']['imgUrlH'] = $attachment_url; |
| 104 |
} |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
private function parse_icon() { |
| 109 |
if ( ! isset( $this->block['attrs']['iconImageUrl'] ) ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
$dom = new DOMDocument(); |
| 114 |
$dom->loadHTML( $this->block['innerHTML'], LIBXML_NOERROR | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); |
| 115 |
|
| 116 |
$parsed_img = $this->parse_img_tag( $dom, 0 ); |
| 117 |
if ( ! $parsed_img ) { |
| 118 |
return; |
| 119 |
} |
| 120 |
list($attachment_id, $attachment_url, $remote_image_url) = $parsed_img; |
| 121 |
|
| 122 |
$this->block['attrs']['iconImageID'] = $attachment_id; |
| 123 |
$this->block['attrs']['iconImageUrl'] = $attachment_url; |
| 124 |
$this->block['innerHTML'] = str_replace( $remote_image_url, $attachment_url, $this->block['innerHTML'] ); |
| 125 |
$this->block['innerContent'] = array_map( fn( $content) => str_replace( $remote_image_url, $attachment_url, $content ), $this->block['innerContent'] ); |
| 126 |
|
| 127 |
} |
| 128 |
} |
| 129 |
|