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

Assets for `[embed_privacy_opt_out]` shortcode missing when site runs on non-standard port #185

Closed kraftner closed 1 year ago

kraftner commented 1 year ago

Bug/Problem

The way you detect if the current post has the [embed_privacy_opt_out] shortcode doesn't work when the site is running on a non standard (80, 443) port, e.g. example.test:8080

The problem is this:

https://github.com/epiphyt/embed-privacy/blob/2cf319a685b1320dc17cdbb677191bcd7e3656f0/inc/class-embed-privacy.php#L1632-L1638

$_SERVER['HTTP_HOST'] already includes the port and the port hence is added twice resulting in example.test:8080:8080. This then later results in url_to_postid() failing to find the post and the check for the shortcode always failing.

Looking at how core does it the section above can probably be replaced with a one-liner:

$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

Steps to reproduce

  1. Setup WP and Embed Privacy on any URL that includes a non-standard (other than 80, 443) port.
  2. Create a post with the [embed_privacy_opt_out]
  3. See it not working due to the missing JS

Version

1.7.2

Link

No response

Environment info

No response

Code of Conduct

kraftner commented 1 year ago

Quick workaround for now:

add_action('init', function(){

    if(
        !class_exists( 'epiphyt\Embed_Privacy\Embed_Privacy' )
    ){
        return;
    }

    $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

    if ( empty( $_SERVER['HTTP_HOST'] ) ) {
        return;
    }

    $post_id = url_to_postid( $current_url );

    if ( $post_id ) {
        $post = get_post( $post_id );

        if ( $post instanceof WP_Post && has_shortcode( $post->post_content, 'embed_privacy_opt_out' ) ) {
            \epiphyt\Embed_Privacy\Embed_Privacy::get_instance()->print_assets();
        }
    }

}, 11);