css, 'root' => css, 'stage' => css ]. */ $rules = (array) apply_filters( 'goconsent_map_triggers', array( array( 'trigger' => '[data-goconsent-map-trigger]', 'root' => '[data-goconsent-map-root]', 'stage' => '[data-goconsent-map-stage]', ), array( 'trigger' => '[data-mlsg-gb-gallery-views] [data-view="map"], [data-mlsg-gb-gallery-views] [data-view="sat"], [data-mlsg-gb-gallery-views] [data-view="street"]', 'root' => '[data-mlsg-gb-gallery-views]', 'stage' => '.mlsg-gb-gallery-views__stage', ), ) ); $out = array(); foreach ( $rules as $r ) { if ( is_array( $r ) && ! empty( $r['trigger'] ) ) { $out[] = array( 'trigger' => (string) $r['trigger'], 'root' => isset( $r['root'] ) ? (string) $r['root'] : '', 'stage' => isset( $r['stage'] ) ? (string) $r['stage'] : '', ); } } return $out; } /** * Whether the gate is on. * * @return bool */ public static function enabled() { /** * Filter whether Google Maps waits for a click or consent. * * @param bool $on Default true. */ return (bool) apply_filters( 'goconsent_gate_maps', true ); } /** * Hold Maps scripts and images in a document. * * @param string $html HTML. * @return string */ public static function rewrite( $html ) { if ( ! self::enabled() || ! self::mentions( $html ) ) { return $html; } $out = preg_replace_callback( '#]*)>(.*?)#is', array( __CLASS__, 'script' ), $html ); $out = null === $out ? $html : $out; $img = preg_replace_callback( '#]*)>#i', array( __CLASS__, 'image' ), $out ); return null === $img ? $out : $img; } /** * Quick check before running the regexes. * * @param string $html HTML. * @return bool */ private static function mentions( $html ) { return false !== stripos( $html, '/maps/api/' ) || false !== stripos( $html, 'maps-api-v3' ); } /** * Whether a URL is a Google Maps load. * * @param string $url URL. * @return bool */ public static function is_maps_url( $url ) { $u = strtolower( html_entity_decode( (string) $url, ENT_QUOTES | ENT_HTML5 ) ); foreach ( self::needles() as $n ) { if ( false !== strpos( $u, $n ) ) { return true; } } return false; } /** * Hold one script tag. * * @param array $m Regex match. * @return string */ public static function script( $m ) { $attrs = $m[1]; if ( false !== stripos( $attrs, 'data-goconsent' ) ) { return $m[0]; } $src = Classifier::attr( $attrs, 'src' ); if ( ! $src || ! self::is_maps_url( $src ) ) { return $m[0]; } $type = ''; if ( preg_match( '#\btype\s*=\s*(["\']?)([^"\'\s>]+)\1#i', $attrs, $tm ) ) { $type = strtolower( $tm[2] ); } if ( '' !== $type && ! in_array( $type, array( 'text/javascript', 'application/javascript', 'module' ), true ) ) { return $m[0]; } $attrs = preg_replace( '#\s*\btype\s*=\s*(["\']?)[^"\'\s>]+\1#i', '', $attrs ); $extra = ' type="text/plain" data-goconsent="maps"' . ( 'module' === $type ? ' data-goconsent-type="module"' : '' ); return '' . $m[2] . ''; } /** * Hold one Static Maps / Street View image. * * @param array $m Regex match. * @return string */ public static function image( $m ) { $attrs = $m[1]; if ( false !== stripos( $attrs, 'data-goconsent' ) ) { return $m[0]; } $src = Classifier::attr( $attrs, 'src' ); if ( ! $src || ! self::is_maps_url( $src ) ) { return $m[0]; } $attrs = preg_replace( '#\s(src|srcset)\s*=\s*("[^"]*"|\'[^\']*\'|[^\s>]+)#i', ' data-goconsent-$1=$2', $attrs ); return ''; } }