Skip to content

Commit 1869689

Browse files
committed
Do not rely on WP_Query global being set
1 parent 8fe2f8f commit 1869689

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

plugins/optimization-detective/storage/data.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,12 @@ function od_get_current_theme_template() {
177177
* @access private
178178
*
179179
* @param OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
180-
* @param WP_Query $wp_query The WP_Query instance.
180+
* @param WP_Query|null $wp_query The WP_Query instance.
181181
* @param string|WP_Block_Template|null $current_template The current template being used.
182182
* @return non-empty-string Current ETag.
183183
*/
184-
function od_get_current_url_metrics_etag( OD_Tag_Visitor_Registry $tag_visitor_registry, WP_Query $wp_query, $current_template ): string {
185-
$queried_object = $wp_query->get_queried_object();
184+
function od_get_current_url_metrics_etag( OD_Tag_Visitor_Registry $tag_visitor_registry, ?WP_Query $wp_query, $current_template ): string {
185+
$queried_object = $wp_query instanceof WP_Query ? $wp_query->get_queried_object() : null;
186186
$queried_object_data = array(
187187
'id' => null,
188188
'type' => null,
@@ -219,7 +219,7 @@ static function ( $post ): ?array {
219219
'post_modified_gmt' => $post->post_modified_gmt,
220220
);
221221
},
222-
0 === $wp_query->post_count ? array() : $wp_query->posts // Needed in case WP_Query has not been initialized.
222+
( $wp_query instanceof WP_Query && $wp_query->post_count > 0 ) ? $wp_query->posts : array()
223223
)
224224
),
225225
'active_theme' => array(

tests/class-optimization-detective-test-helpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ trait Optimization_Detective_Test_Helpers {
2727
*/
2828
public function populate_url_metrics( array $elements, bool $complete = true ): void {
2929
$slug = od_get_url_metrics_slug( od_get_normalized_query_vars() );
30-
$etag = od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry(), new WP_Query(), trailingslashit( get_template_directory() ) . 'index.php' ); // Note: Tests rely on the od_current_url_metrics_etag_data filter to set the desired value.
30+
$etag = od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry(), null, null ); // Note: Tests rely on the od_current_url_metrics_etag_data filter to set the desired value.
3131
$sample_size = $complete ? od_get_url_metrics_breakpoint_sample_size() : 1;
3232
foreach ( array_merge( od_get_breakpoint_max_widths(), array( 1000 ) ) as $viewport_width ) {
3333
for ( $i = 0; $i < $sample_size; $i++ ) {
@@ -81,7 +81,7 @@ public function get_sample_dom_rect(): array {
8181
public function get_sample_url_metric( array $params ): OD_URL_Metric {
8282
$params = array_merge(
8383
array(
84-
'etag' => od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry(), new WP_Query(), trailingslashit( get_template_directory() ) . 'index.php' ), // Note: Tests rely on the od_current_url_metrics_etag_data filter to set the desired value.
84+
'etag' => od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry(), null, null ), // Note: Tests rely on the od_current_url_metrics_etag_data filter to set the desired value.
8585
'url' => home_url( '/' ),
8686
'viewport_width' => 480,
8787
'elements' => array(),

0 commit comments

Comments
 (0)