Last active
January 21, 2021 19:13
-
-
Save westonruter/a120556c80897d4f7fbf200cb7aa5258 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Installation instructions: https://gist.github.com/westonruter/6110fbc4bef0c4b8c021a112012f7e9c