Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable popups on non-AMP pages with POST form elements #101

Merged
merged 1 commit into from
Apr 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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