Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active February 7, 2022 13:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonruter/621137b5a5ae1caaaee48c63f61ce7b7 to your computer and use it in GitHub Desktop.
Save westonruter/621137b5a5ae1caaaee48c63f61ce7b7 to your computer and use it in GitHub Desktop.
<?php
/**
* AMP Query Monitor Compat
*
* @package AMP_Query_Monitor_Compat
* @author Weston Ruter, Google
* @license GPL-2.0-or-later
* @copyright 2019 Google LLC
*
* @wordpress-plugin
* Plugin Name: AMP Query Monitor Compat
* Description: Integrate Query Monitor with AMP dev mode so that its scripts/styles are not removed in the sanitization process. Depends on <a href="https://wordpress.org/plugins/amp/">AMP plugin</a> <a href="https://github.com/ampproject/amp-wp/releases/tag/1.3-beta1">v1.3-beta1</a>+.
* Plugin URI: https://gist.github.com/westonruter/621137b5a5ae1caaaee48c63f61ce7b7
* Version: 0.1.0
* 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/621137b5a5ae1caaaee48c63f61ce7b7
*/
namespace AMP_Query_Monitor_Compat;
/**
* Determine whether it is an AMP request.
*
* @return bool Whether AMP.
*/
function is_amp() {
return function_exists( 'is_amp_endpoint' ) && is_amp_endpoint();
}
/**
* Add inline scripts added by Query Monitor to AMP dev mode.
*
* @param string[] $xpaths XPaths.
* @return string[] XPaths.
*/
function add_dev_mode_attributes( $xpaths ) {
$xpaths[] = '//script[ contains( text(), "qm_number_format" ) ]';
$xpaths[] = '//script[ contains( text(), "QM_i18n" ) ]';
$xpaths[] = '//script[ contains( text(), "query-monitor-" ) ]';
return $xpaths;
}
add_filter( 'amp_dev_mode_element_xpaths', __NAMESPACE__ . '\add_dev_mode_attributes' );
/**
* Get all dependency handles for the supplied handles.
*
* @see \WP_Dependencies::all_deps()
*
* @param \WP_Dependencies $dependencies Dependencies.
* @param string[] $handles Handles.
* @return string[] Dependency handles.
*/
function get_all_deps( \WP_Dependencies $dependencies, $handles ) {
$dependency_handles = [];
foreach ( $handles as $handle ) {
if ( isset( $dependencies->registered[ $handle ] ) ) {
$dependency_handles = array_merge(
$dependency_handles,
$dependencies->registered[ $handle ]->deps,
get_all_deps( $dependencies, $dependencies->registered[ $handle ]->deps )
);
}
}
return $dependency_handles;
}
/**
* Mark Query Monitor assets as being part of AMP dev mode.
*/
function make_query_monitor_scripts_dependent_on_admin_bar() {
if ( ! is_amp() ) {
return;
}
$qm_handle = 'query-monitor';
if ( wp_script_is( $qm_handle, 'enqueued' ) ) {
$script_dependencies = array_merge( [ $qm_handle ], get_all_deps( wp_scripts(), [ $qm_handle ] ) );
add_filter(
'script_loader_tag',
function ( $tag, $handle ) use ( $script_dependencies ) {
if ( in_array( $handle, $script_dependencies, true ) ) {
$tag = preg_replace( '/(?<=<script)(?=\s|>)/i', ' data-ampdevmode', $tag );
}
return $tag;
},
10,
2
);
}
if ( wp_style_is( $qm_handle, 'enqueued' ) ) {
$style_dependencies = array_merge( [ $qm_handle ], get_all_deps( wp_styles(), [ $qm_handle ] ) );
add_filter(
'style_loader_tag',
function ( $tag, $handle ) use ( $style_dependencies ) {
if ( in_array( $handle, $style_dependencies, true ) ) {
$tag = preg_replace( '/(?<=<link)(?=\s|>)/i', ' data-ampdevmode', $tag );
}
return $tag;
},
10,
2
);
}
}
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\make_query_monitor_scripts_dependent_on_admin_bar' );
@westonruter
Copy link
Author

@westonruter
Copy link
Author

To be made obsolete by johnbillion/query-monitor#535

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