nonce = wp_create_nonce( 'cookie-compliance-amp-consent' );
add_action( 'init', [ $this, 'handle_iframe' ] );
add_action( 'wp_head', [ $this, 'load_amp_consent' ] );
}
/**
* Load AMP consent module.
*
* @return void
*/
public function load_amp_consent() {
// is banner allowed to display?
if ( ! Cookie_Notice()->frontend->maybe_display_banner( [ 'skip_amp' => true ] ) )
return;
if ( function_exists( 'amp_is_request' ) && amp_is_request() ) {
// load styles
echo '
';
// load scripts
echo '
';
// get iframe url
$url = apply_filters( 'cn_cookie_compliance_amp_iframe_url', $this->add_subdomain_to_url( get_site_url(), 'www' ) );
// load consent iframe
echo '
';
}
}
/**
* Add subdomain to url.
*
* @param string $url
* @param string $subdomain
* @return string
*/
public function add_subdomain_to_url( $url, $subdomain ) {
// parse url
$parts = parse_url( $url );
// subdomain does not exist?
if ( substr( $parts['host'], 0, strlen( $subdomain ) + 1 ) !== $subdomain . '.' ) {
// find host
$pos = strpos( $url, $parts['host'] );
// update url and add subdomain
$url = substr_replace( $url, $subdomain . '.' . $parts['host'], $pos, strlen( $parts['host'] ) );
}
return $url;
}
/**
* Generate consent iframe.
*
* @return void
*/
public function handle_iframe() {
if ( isset( $_GET['cn-amp-consent-iframe'] ) && $_GET['cn-amp-consent-iframe'] === $this->nonce ) {
wp_ob_end_flush_all();
// display iframe
echo $this->generate_iframe_html();
exit;
}
}
/**
* Generate consent iframe.
*
* @return string
*/
public function generate_iframe_html() {
// get main instance
$cn = Cookie_Notice();
// get options
$options = $cn->frontend->get_cc_options();
// get output
$cc_output = $cn->frontend->get_cc_output( $options );
// get allowed html for cookie compliance html output
$allowed_html = array_merge(
wp_kses_allowed_html( 'post' ),
[
'script' => [
'type' => true,
'src' => true
]
]
);
$html = '
' . esc_html__( 'Cookie Compliance AMP Consent', 'cookie-notice' ) . '
' . wp_kses( $cc_output, $allowed_html ) . '
';
return $html;
}
}
new Cookie_Notice_Modules_AMP();