| 1 |
<?php |
| 2 |
namespace epiphyt\Embed_Privacy\integration; |
| 3 |
|
| 4 |
use DOMDocument; |
| 5 |
use epiphyt\Embed_Privacy\data\Replacer; |
| 6 |
use epiphyt\Embed_Privacy\embed\Provider; |
| 7 |
use epiphyt\Embed_Privacy\Embed_Privacy; |
| 8 |
|
| 9 |
/** |
| 10 |
* Maps Marker integration for Embed Privacy. |
| 11 |
* |
| 12 |
* @author Epiphyt |
| 13 |
* @license GPL2 |
| 14 |
* @package epiphyt\Embed_Privacy |
| 15 |
* @since 1.10.0 |
| 16 |
*/ |
| 17 |
final class Maps_Marker { |
| 18 |
/** |
| 19 |
* Initialize functionality. |
| 20 |
*/ |
| 21 |
public static function init() { |
| 22 |
\add_filter( 'do_shortcode_tag', [ self::class, 'replace' ], 10, 2 ); |
| 23 |
\add_filter( 'embed_privacy_overlay_provider', [ self::class, 'set_provider' ], 10, 2 ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Get the map dimensions. |
| 28 |
* |
| 29 |
* @param string $content Embedded content |
| 30 |
* @return array Embed dimensions (height and width) |
| 31 |
*/ |
| 32 |
private static function get_dimensions( $content ) { |
| 33 |
$height = ''; |
| 34 |
$width = '100%'; |
| 35 |
$use_errors = \libxml_use_internal_errors( true ); |
| 36 |
$dom = new DOMDocument(); |
| 37 |
$dom->loadHTML( |
| 38 |
'<html><meta charset="utf-8">' . $content . '</html>', |
| 39 |
\LIBXML_HTML_NOIMPLIED | \LIBXML_HTML_NODEFDTD |
| 40 |
); |
| 41 |
|
| 42 |
/** @var \DOMElement $element */ |
| 43 |
foreach ( $dom->getElementsByTagName( 'div' ) as $element ) { |
| 44 |
if ( $element->getAttribute( 'class' ) === 'mmp-map' ) { |
| 45 |
$style = $element->getAttribute( 'style' ); |
| 46 |
\preg_match( '/height:\s*(?<height>\d+)/', $style, $height_matches ); |
| 47 |
\preg_match( '/width:\s*(?<width>\d+)/', $style, $width_matches ); |
| 48 |
|
| 49 |
if ( ! empty( $height_matches['height'] ) ) { |
| 50 |
$height = $height_matches['height']; |
| 51 |
} |
| 52 |
|
| 53 |
if ( ! empty( $width_matches['width'] ) ) { |
| 54 |
$width = $width_matches['width']; |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
\libxml_use_internal_errors( $use_errors ); |
| 60 |
|
| 61 |
return [ |
| 62 |
'height' => $height, |
| 63 |
'width' => $width, |
| 64 |
]; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Replace Maps Marker (Pro) shortcodes. |
| 69 |
* |
| 70 |
* @param string $output Shortcode output |
| 71 |
* @param string $tag Shortcode tag |
| 72 |
* @return string Updated shortcode output |
| 73 |
*/ |
| 74 |
public static function replace( $output, $tag ) { |
| 75 |
if ( $tag !== 'mapsmarker' ) { |
| 76 |
return $output; |
| 77 |
} |
| 78 |
|
| 79 |
if ( Embed_Privacy::get_instance()->is_ignored_request ) { |
| 80 |
return $output; |
| 81 |
} |
| 82 |
|
| 83 |
$attributes = self::get_dimensions( $output ); |
| 84 |
$attributes['is_oembed'] = true; |
| 85 |
|
| 86 |
return Replacer::replace_oembed( $output, '', $attributes ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Set the Maps Marker Pro provider. |
| 91 |
* |
| 92 |
* @param \epiphyt\Embed_Privacy\embed\Provider|null $provider Current provider |
| 93 |
* @param string $content Embedded content |
| 94 |
* @return \epiphyt\Embed_Privacy\embed\Provider|null Updated provider |
| 95 |
*/ |
| 96 |
public static function set_provider( $provider, $content ) { |
| 97 |
if ( |
| 98 |
! $provider |
| 99 |
&& ( \str_contains( $content, 'maps-marker-pro' ) || \str_contains( $content, '[mapsmarker' ) ) |
| 100 |
) { |
| 101 |
$provider = new Provider(); |
| 102 |
$provider->set_name( 'maps-marker-pro' ); |
| 103 |
$provider->set_pattern( '/maps-marker-pro|^\[mapsmarker/' ); |
| 104 |
$provider->set_title( \__( 'Maps Marker Pro', 'embed-privacy' ) ); |
| 105 |
} |
| 106 |
|
| 107 |
return $provider; |
| 108 |
} |
| 109 |
} |
| 110 |
|