Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active July 31, 2024 20:56
Show Gist options
  • Save westonruter/3bad77db53496c7103dd64ca1a0e1cf1 to your computer and use it in GitHub Desktop.
Save westonruter/3bad77db53496c7103dd64ca1a0e1cf1 to your computer and use it in GitHub Desktop.
<?php
/**
* AMP Wordfence Compat plugin bootstrap.
*
* @package Google\AMP_Wordfence_Compat
* @author Weston Ruter, Google
* @license GPL-2.0-or-later
* @copyright 2020 Google Inc.
*
* @wordpress-plugin
* Plugin Name: AMP Wordfence Compat
* Plugin URI: https://gist.github.com/westonruter/3bad77db53496c7103dd64ca1a0e1cf1
* Description: Compatibility plugin to prevent Wordfence from causing AMP validation errors. In particular, this ensures the Ajax watcher logic is included in AMP Dev Mode.
* Version: 0.2
* 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
*/
namespace Google\AMP_Wordfence_Compat;
add_filter(
'amp_dev_mode_element_xpaths',
function ( $xpaths ) {
// @todo The following can be revised once ampdevmode flags can be added to enqueued scripts and styles. See <https://github.com/ampproject/amp-wp/issues/4598>.
$xpaths[] = '//script[ contains( text(), "var WFAJAXWatcherVars" ) ]';
$xpaths[] = '//script[ contains( @src, "wp-includes/js/jquery/jquery.js" ) ]';
$xpaths[] = '//script[ contains( @src, "wp-includes/js/jquery/jquery.min.js" ) ]'; // As of WordPress 5.6.
$xpaths[] = '//script[ contains( @src, "wp-includes/js/jquery/jquery-migrate.js" ) ]';
$xpaths[] = '//script[ contains( @src, "wp-includes/js/jquery/jquery-migrate.min.js" ) ]';
$xpaths[] = '//script[ contains( @src, "wordfence/js/admin.ajaxWatcher." ) ]';
$xpaths[] = '//link[ contains( @href, "wordfence/css/wordfenceBox." ) ]';
// Props @titopu per https://gist.github.com/westonruter/3bad77db53496c7103dd64ca1a0e1cf1?permalink_comment_id=5139391#gistcomment-5139391
$xpaths[] = '//script[ contains( text(), "var WordfenceI18nStrings" ) ]';
$xpaths[] = '//script[ contains( @src, "wordfence/js/wfi18n." ) ]';
return $xpaths;
}
);
@westonruter
Copy link
Author

Copy link

ghost commented Sep 5, 2022

@westonruter Hello. I was directed to your mini plugin by WordFence after asking WordFence for help with a validation error that the official Google AMP plugin is flagging. The plugin (WordFence Compatibility) did not resolve the error. I can provide the raw validation data to you directly, if helpful. I have asked WordFence on the chain linked above to continue to look into the issue, and would appreciate your help, too. Thank you.

@westonruter
Copy link
Author

@Marguerite-star Please open a topic on our support forum: https://wordpress.org/support/plugin/amp/

@titopu
Copy link

titopu commented Jul 31, 2024

I added two new lines to the file, and now it works for me. I'm not sure if it's safe, but the AMP error has been resolved.

$xpaths[] = '//script[ contains( text(), "var WordfenceI18nStrings" ) ]';
$xpaths[] = '//script[ contains( @src, "wordfence/js/wfi18n." ) ]';

@westonruter
Copy link
Author

@titopu Thanks. Those changes look good. I'll add those to my Gist.

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