epiphyt / embed-privacy

Embed Privacy prevents loading of embedded external content and allows your site visitors to opt-in.
https://epiph.yt/en/embed-privacy/
GNU General Public License v2.0
19 stars 12 forks source link

:sparkles: Fine-grained control to disable provider #202

Closed abid76 closed 11 months ago

abid76 commented 11 months ago

This pull request introduces a filter hook embed_privacy_disable_provider which is applied just as the very first statement in get_output_template.

public function get_output_template( $embed_provider, $embed_provider_lowercase, $output, $args = [] ) {
    $disable_provider = apply_filters("embed_privacy_disable_provider", $embed_provider, $embed_provider_lowercase, $output, $args);
    if ($disable_provider === true) {
        return $output;
    }

This filter allows fine-grained control on when to disable an embed provider. It applies before the disabled-option from the Wordpress settings is checked (alternatively this could also be placed after checking of disabled option).

Our use case is to disable embed privacy for a Liveticker which receives it's messages as Wordpress post via Ajax. In this case embed privacy doesn't work. The messages of the Liveticker can be recognized by the post type scliveticker_tick.

add_filter('embed_privacy_disable_provider', 'disable_embed_privacy_provider', 10, 4);
function disable_embed_privacy_provider($embed_provider, $embed_provider_lowercase, $output, $args = []) {
    $post_type = get_post_type();
    return $post_type == 'scliveticker_tick';
}

Other implementations of the filter may check the HTTP request or using the wp_doing_ajax() function (which wouldn't work in the actual Liveticker issue).

MatzeKitt commented 11 months ago

Isn’t it possible to check your request and depending on that just deregister the actions of Embed Privacy here in your case?

https://github.com/epiphyt/embed-privacy/blob/main/inc/class-embed-privacy.php#L155-L163

abid76 commented 11 months ago

Isn’t it possible to check your request and depending on that just deregister the actions of Embed Privacy here in your case?

https://github.com/epiphyt/embed-privacy/blob/main/inc/class-embed-privacy.php#L155-L163

Thanks for you suggestion. Unfortunately I cannot make it work. I placed the following lines in functions.php but it does not change anything. Do you have any idea what I am missing?

add_action('wp_loaded', 'apollo_embed_privacy_remove_filters');
function apollo_embed_privacy_remove_filters() {
    $post_type = get_post_type();
    if ($post_type == 'scliveticker_tick') {
        remove_filter('acf_the_content', 'Embed_Privacy::get_instance::replace_embeds' );
        remove_filter('do_shortcode_tag', 'Embed_Privacy::get_instance::replace_embeds', 10 );
        remove_filter('do_shortcode_tag', 'Embed_Privacy::get_instance::replace_maps_marker', 10 );
        remove_filter('embed_oembed_html', 'Embed_Privacy::get_instance::replace_embeds_oembed', 10 );
        remove_filter('embed_privacy_widget_output', 'Embed_Privacy::get_instance::replace_embeds' );
        remove_filter('et_builder_get_oembed', 'Embed_Privacy::get_instance::replace_embeds_divi', 10 );
        remove_filter('pll_get_post_types', 'Embed_Privacy::get_instance::register_polylang_post_type', 10 );
        remove_filter('the_content', 'Embed_Privacy::get_instance::replace_embeds' );
        remove_filter('wp_video_shortcode', 'Embed_Privacy::get_instance::replace_video_shortcode', 10 );
    }
}
MatzeKitt commented 11 months ago

'Embed_Privacy::get_instance::replace_embeds' would translate to a function with exactly this name, which is not available.

Try it with the second parameter as array:

\remove_filter( 'acf_the_content', [ Embed_Privacy::get_instance(), 'replace_embeds' ] );
abid76 commented 11 months ago

Thanks! 🙂 That worked.

Complete solution in functions.php:

add_action('the_post', 'apollo_embed_privacy_remove_filters');
function apollo_embed_privacy_remove_filters() {
    if (!class_exists('epiphyt\Embed_Privacy\Embed_Privacy')) {
        return;
    }

    $post_type = get_post_type();
    if ($post_type == 'scliveticker_tick') {
        remove_filter('acf_the_content', [ epiphyt\Embed_Privacy\Embed_Privacy::get_instance(), 'replace_embeds' ] );
        remove_filter('do_shortcode_tag', [ epiphyt\Embed_Privacy\Embed_Privacy::get_instance(), 'replace_embeds' ] , 10 );
        remove_filter('do_shortcode_tag', [ epiphyt\Embed_Privacy\Embed_Privacy::get_instance(), 'replace_maps_marker' ] , 10 );
        remove_filter('embed_oembed_html', [ epiphyt\Embed_Privacy\Embed_Privacy::get_instance(), 'replace_embeds_oembed' ] , 10 );
        remove_filter('embed_privacy_widget_output', [ epiphyt\Embed_Privacy\Embed_Privacy::get_instance(), 'replace_embeds' ] );
        remove_filter('et_builder_get_oembed', [ epiphyt\Embed_Privacy\Embed_Privacy::get_instance(), 'replace_embeds_divi' ], 10 );
        remove_filter('pll_get_post_types', [ epiphyt\Embed_Privacy\Embed_Privacy::get_instance(), 'register_polylang_post_type' ], 10 );
        remove_filter('the_content', [ epiphyt\Embed_Privacy\Embed_Privacy::get_instance(), 'replace_embeds' ] );
        remove_filter('wp_video_shortcode', [ epiphyt\Embed_Privacy\Embed_Privacy::get_instance(), 'replace_video_shortcode' ], 10 );
    }
}

As there's now a working solution (which hopefully will function even after updates of Embed Privacy) I'll close this Pull request.