Last active
November 19, 2024 23:00
-
-
Save westonruter/6fb9531ace2ebb86725407311546df58 to your computer and use it in GitHub Desktop.
[OUTDATED] Gist moved to full repo: https://github.com/westonruter/od-store-query-vars
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
/** | |
* Finalizes extension. | |
* | |
* @type {FinalizeCallback} | |
* @param {FinalizeArgs} args Args. | |
*/ | |
export async function finalize( { extendRootData } ) { | |
const script = document.getElementById( 'od-normalized-query-vars' ); | |
if ( script instanceof HTMLScriptElement ) { | |
extendRootData( { queryVars: JSON.parse( script.text ) } ); | |
} | |
} |
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 | |
/** | |
* Plugin Name: Optimization Detective Store Query Vars | |
* Plugin URI: https://gist.github.com/westonruter/6fb9531ace2ebb86725407311546df58 | |
* Description: Stores the Query Vars with a URL Metric in the Optimization Detective plugin. This is useful for debugging URL Metrics, in particular what the slug was computed from. | |
* Requires at least: 6.5 | |
* Requires PHP: 7.2 | |
* Requires Plugins: optimization-detective | |
* Version: 0.1.0 | |
* Author: Weston Ruter | |
* Author URI: https://weston.ruter.net/ | |
* License: GPLv2 or later | |
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | |
* Text Domain: optimization-detective-store-user-agent | |
* Update URI: https://gist.github.com/westonruter/6fb9531ace2ebb86725407311546df58 | |
* Gist Plugin URI: https://gist.github.com/westonruter/6fb9531ace2ebb86725407311546df58 | |
* | |
* @package OptimizationDetective\StoreQueryVars | |
*/ | |
namespace OptimizationDetective\StoreQueryVars; | |
// Important: If a plugin manually adds query vars which aren't in $wp->public_query_vars then the URL Metric storage will be rejected. | |
add_filter( | |
'od_url_metric_schema_root_additional_properties', | |
static function ( array $properties ): array { | |
global $wp; | |
$query_vars_properties = array( | |
// Introduced by od_get_normalized_query_vars(). | |
'user_logged_in' => array( | |
'type' => 'boolean', | |
), | |
); | |
foreach ( $wp->public_query_vars as $key ) { | |
$query_vars_properties[ $key ] = array( | |
'type' => array( 'string', 'number' ), | |
'maxLength' => 100, // Something reasonable to guard against abuse. | |
); | |
} | |
$properties['queryVars'] = array( | |
'type' => 'object', | |
'properties' => $query_vars_properties, | |
'additionalProperties' => false, | |
); | |
return $properties; | |
} | |
); | |
add_filter( | |
'od_extension_module_urls', | |
static function ( array $urls ): array { | |
$urls[] = plugins_url( 'detect.js', __FILE__ ); | |
return $urls; | |
} | |
); | |
add_action( | |
'wp_footer', | |
static function (): void { | |
if ( ! od_can_optimize_response() ) { | |
return; | |
} | |
?> | |
<script type="application/json" id="od-normalized-query-vars"><?php echo wp_json_encode( od_get_normalized_query_vars() ); ?></script> | |
<?php | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment