Skip to content

Commit

Permalink
fix(amp): disable popups on non-AMP pages with POST form elements
Browse files Browse the repository at this point in the history
  • Loading branch information
adekbadek committed Apr 9, 2020
1 parent ee37d73 commit 961451b
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions includes/class-newspack-popups-inserter.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public function __construct() {
* @param string $content The content of the post.
*/
public static function insert_popups_in_content( $content = '' ) {
if ( self::assess_amp_form_issue( $content ) ) {
return $content;
}

if ( is_admin() || ! is_singular() ) {
return $content;
}
Expand Down Expand Up @@ -268,10 +272,35 @@ public static function insert_popups_amp_access( $popups ) {
}
}

/**
* Disable popups on non-AMP pages with a form with POST method.
* More context: https://github.com/ampproject/amphtml/issues/27638.
*
* @param string $content The content of the post.
* @return bool True if popups should be disabled for current page.
*/
public static function assess_amp_form_issue( $content = false ) {
if ( is_admin() || ! is_singular() ) {
return false;
}
if ( ! $content ) {
$content = get_the_content();
}
$is_amp = function_exists( 'is_amp_endpoint' ) && is_amp_endpoint();

if ( ! $is_amp ) {
return preg_match( "/<form.*method=[\"']post[\"']/", $content );
}
return false;
}

/**
* Register and enqueue all required AMP scripts, if needed.
*/
public static function register_amp_scripts() {
if ( self::assess_amp_form_issue() ) {
return;
}
if ( ! is_admin() && ! wp_script_is( 'amp-runtime', 'registered' ) ) {
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
wp_register_script(
Expand Down

0 comments on commit 961451b

Please sign in to comment.