PluginProbe
Embed Privacy / 1.11.2
Embed Privacy v1.11.2
1.14.0 1.13.0 trunk 0.1 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.10.0 1.10.1 1.10.10 1.10.2 1.10.3 1.10.4 1.10.5 1.10.6 1.10.7 1.10.8 1.10.9 1.11.0 1.11.1 1.11.2 All 68 releases
embed-privacy / inc / integration / class-maps-marker.php

class-maps-marker.php in Embed Privacy 1.11.2, at inc/integration/class-maps-marker.php

104 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace epiphyt\Embed_Privacy\integration;
3
4 use DOMDocument;
5 use epiphyt\Embed_Privacy\data\Providers;
6 use epiphyt\Embed_Privacy\data\Replacer;
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 ( \str_contains( $content, 'maps-marker-pro' ) || \str_contains( $content, '[mapsmarker' ) ) {
98 $provider = Providers::get_instance()->get_by_name( 'maps-marker-pro' );
99 }
100
101 return $provider;
102 }
103 }
104