Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active January 21, 2021 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonruter/a120556c80897d4f7fbf200cb7aa5258 to your computer and use it in GitHub Desktop.
Save westonruter/a120556c80897d4f7fbf200cb7aa5258 to your computer and use it in GitHub Desktop.
<?php
/**
* AMP Comment Form YesValidate plugin bootstrap.
*
* @package Google\AMP_Comment_Form_YesValidate
* @author Weston Ruter, Google
* @license GPL-2.0-or-later
* @copyright 2020 Google Inc.
*
* @wordpress-plugin
* Plugin Name: AMP Comment Form YesValidate
* Plugin URI: https://gist.github.com/westonruter/a120556c80897d4f7fbf200cb7aa5258
* Description: Remove novalidate attribute from comment form to force client-side form validation to prevent relying on server-side validation which will not be available during offline commenting. This ensures that background sync wont POST a comment that we already know to be invalid.
* Version: 0.1
* Author: Weston Ruter, Google
* Author URI: https://weston.ruter.net/
* License: GNU General Public License v2 (or later)
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Gist Plugin URI: https://gist.github.com/westonruter/a120556c80897d4f7fbf200cb7aa5258
*/
namespace Google\AMP_Comment_Form_YesValidate;
add_filter(
'amp_content_sanitizers',
function ( $sanitizers ) {
require_once __DIR__ . '/Sanitizer.php';
$sanitizers[ __NAMESPACE__ . '\Sanitizer' ] = [];
return $sanitizers;
}
);
<?php
/**
* Sanitizer file.
*
* @package Google\AMP_Comment_Form_YesValidate
*/
namespace Google\AMP_Comment_Form_YesValidate;
use AMP_Base_Sanitizer;
use DOMElement;
/**
* Class Sanitizer
*
* @todo Core should allow for this to be omitted removed.
*/
class Sanitizer extends AMP_Base_Sanitizer {
/**
* Remove the novalidate attribute is removed from the comment form.
*
* This needs to be made part of the plugin.
*/
public function sanitize() {
/**
* Comment form.
*
* @var DOMElement $form
*/
$form = $this->dom->getElementById( 'commentform' );
if ( $form ) {
$form->removeAttribute( 'novalidate' );
}
}
}
@westonruter
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment