• Resolved jhtjards

    (@jhtjards)


    Hi, since after 1.9.2 there has been a lot of API- and other changes, I needed to migrate my template function to embedding google maps via script tag.

    In my template I use my custom function as follows:

    $map_content = '<script src="https://maps.google.com/maps/api/js?v=3&callback=mapLibReadyHandler&key=APIKEY" async defer></script>';

    echo custom_replace_content_with_overlay( $map_content );

    Following the Migration Guide at https://epiph.yt/en/embed-privacy/documentation/migration-to-version-1-10-0/ the function custom_replace_content_with_overlay looks like this now:

    function custom_replace_content_with_overlay($content) {
    if (!class_exists('epiphyt\Embed_Privacy\data\Replacer')) {
    return $content;
    }

    $wrapped_content = '<div class="embed-privacy google-maps">' . $content . '</div>';
    $content = \epiphyt\Embed_Privacy\data\Replacer::replace_embeds($wrapped_content);

    return $content;
    }

    I did create a custom embed provider in wp-admin via embed privacy settings called Google Maps.

    In the updated documentation it is explicitly discouraged to use just the url as a Regex pattern like before, so I tried to use /<script\s+src="https:\/\/maps.google.com\/maps\/api\/js\?[^"]+".*?><\/script>/ which positively matches my tag in the regexr web tool.

    But I was not able to get the overlay to show up this way. The script tag would always just be returned.

    I did eventually get the overlay when I reverted back to the discouraged maps.google.com\/maps\/api\/js and added the script tag to the allowed tags via the filter:

    add_filter('embed_privacy_replacer_matcher_elements', function($allowed_tags, $provider) {
    $allowed_tags[] = 'script';
    return $allowed_tags;
    }, 10, 2);

    I’m sure there are good reasons not to generally include the script tag, so I’d still like to use the more precise Regex.

    Any idea what am I missing here?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Matthias Kittsteiner

    (@kittmedia)

    You should not need to add a custom embed provider, since there already is a Google Maps embed provider with the following regular expression: /(google.com\/maps\/embed|maps.google.com\/(maps)?)/

    It is discouraged to use regular expressions without tag name, because they are more likely to produce false-positives. If you don’t encounter such problems, there is no need to force it.

    A side-note regarding your matcher elements filter: Check for the provider and only return the script tag as allowed for Google Maps, which enhances performance. ??

    Thread Starter jhtjards

    (@jhtjards)

    Hi Matthias, ah ok, thanks for your clarification regarding script tag in the regex!

    And thanks for the performance suggestion. Can you maybe provide an example of how to check for the provider?

    When I take the above filter function and var_dump the $provider in the embed_privacy_replacer_matcher_elements hook, I get a huge object of all the possible providers – not just the one used in this instance. I feel like I’m missing a puzzle piece here..

    Plugin Author Matthias Kittsteiner

    (@kittmedia)

    Hm, you only should get one provider object. Can you post a var_dump of the provider object? And maybe also a var_dump of \debug_backtrace( \DEBUG_BACKTRACE_IGNORE_ARGS ) at this stage?

    echo '<pre>'; die( \var_dump( $provider, \debug_backtrace( \DEBUG_BACKTRACE_IGNORE_ARGS ) ) );

    Usually, you should get the check working by checking for $provider->get_name() !== 'google-maps'

    Thread Starter jhtjards

    (@jhtjards)

    Oh, it’s working now.
    Sorry, I realise now where I was mistaken. It wasn’t one big object, it was all the looped providers sequentially in separate objects. So using get_name() to check for the current provider works just fine.

    So to allow for the script tag only for google maps the filter looks like this:

    add_filter('embed_privacy_replacer_matcher_elements', function($allowed_tags, $provider) {
    if($provider->get_name() === 'google-maps'){
    $allowed_tags[] = 'script';
    }
    return $allowed_tags;
    }, 10, 2);

    Within my wrapper function it looks like this:

    /**
    * Replace specific content with the Embed Privacy overlay of type 'google-maps'.
    *
    * @param string $content The content to replace
    * @return string The updated content
    */
    function spielortemap_replace_content_with_overlay($content) {
    if (!class_exists('epiphyt\Embed_Privacy\data\Replacer')) {
    return $content;
    }

    $wrapped_content = '<div class="embed-privacy google-maps">' . $content . '</div>';


    add_filter('embed_privacy_replacer_matcher_elements', function($allowed_tags, $provider) {
    if($provider->get_name() === 'google-maps'){
    $allowed_tags[] = 'script';
    }
    return $allowed_tags;
    }, 10, 2);



    $content = \epiphyt\Embed_Privacy\data\Replacer::replace_embeds($wrapped_content);

    return $content;
    }

    I hope somebody in the future will find this useful too.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.