Skip to content

Commit 42005e6

Browse files
committed
Improve docs and tidiness
1 parent 06f0fea commit 42005e6

File tree

7 files changed

+47
-35
lines changed

7 files changed

+47
-35
lines changed

plugins/image-prioritizer/hooks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
add_action( 'od_init', 'image_prioritizer_init' );
1414
add_filter( 'od_extension_module_urls', 'image_prioritizer_filter_extension_module_urls' );
1515
add_filter( 'od_url_metric_schema_root_additional_properties', 'image_prioritizer_add_element_item_schema_properties' );
16-
add_filter( 'od_store_url_metric_validity', 'image_prioritizer_filter_store_url_metric_validity', 10, 2 );
16+
add_filter( 'od_url_metric_storage_validity', 'image_prioritizer_filter_store_url_metric_validity', 10, 2 );

plugins/image-prioritizer/tests/test-hooks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ public function test_hooks_added(): void {
1414
$this->assertEquals( 10, has_action( 'od_init', 'image_prioritizer_init' ) );
1515
$this->assertEquals( 10, has_filter( 'od_extension_module_urls', 'image_prioritizer_filter_extension_module_urls' ) );
1616
$this->assertEquals( 10, has_filter( 'od_url_metric_schema_root_additional_properties', 'image_prioritizer_add_element_item_schema_properties' ) );
17-
$this->assertEquals( 10, has_filter( 'od_store_url_metric_validity', 'image_prioritizer_filter_store_url_metric_validity' ) );
17+
$this->assertEquals( 10, has_filter( 'od_url_metric_storage_validity', 'image_prioritizer_filter_store_url_metric_validity' ) );
1818
}
1919
}

plugins/optimization-detective/class-od-url-metric-group-collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public function get_last_group(): OD_URL_Metric_Group {
225225
}
226226

227227
/**
228-
* Clear result cache.
228+
* Clears result cache.
229229
*
230230
* @since 0.3.0
231231
*/
@@ -234,7 +234,7 @@ public function clear_cache(): void {
234234
}
235235

236236
/**
237-
* Create groups.
237+
* Creates groups.
238238
*
239239
* @since 0.1.0
240240
*

plugins/optimization-detective/class-od-url-metric-group.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ public function count(): int {
471471
}
472472

473473
/**
474-
* Clear result cache.
474+
* Clears result cache.
475475
*
476476
* @since n.e.x.t
477477
*/

plugins/optimization-detective/readme.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,20 @@ The ETag is a unique identifier that changes whenever the underlying data used t
246246

247247
When the ETag for URL Metrics in a complete viewport group no longer matches the current environment's ETag, new URL Metrics will then begin to be collected until there are no more stored URL Metrics with the old ETag. These new URL Metrics will include data relevant to the newly activated plugins and their tag visitors.
248248

249+
**Filter:** `od_url_metric_storage_validity` (default args: true)
250+
251+
Filters whether a URL Metric is valid for storage.
252+
253+
Three paramters are passed to this filter:
254+
255+
1. `$validity` (`bool|WP_Error`): Validity. Invalid if false or a WP_Error with errors.
256+
2. `$url_metric` (`OD_Strict_URL_Metric`): URL Metric, already validated against the JSON Schema.
257+
3. `$url_metric_data` (`array<string, mixed>`): Original URL Metric data before any mutations.
258+
259+
This allows for custom validation constraints to be applied beyond what can be expressed in JSON Schema. This filter only applies when storing a URL Metric via the REST API. It does not run when a stored URL Metric is loaded from the `od_url_metrics` post type. This means that validation logic enforced via this filter can be more expensive, such as doing filesystem checks or HTTP requests.
260+
261+
In addition to having the filter return `false` or a non-empty `WP_Error` to block storing the URL Metric, a plugin may also mutate the `OD_URL_Metric` instance passed by reference to the filter callback. This is useful for plugins in particular to unset extended properties which couldn't be validated using JSON Schema alone.
262+
249263
**Action:** `od_url_metric_stored` (argument: `OD_URL_Metric_Store_Request_Context`)
250264

251265
Fires whenever a URL Metric was successfully stored.

plugins/optimization-detective/storage/rest-api.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -210,31 +210,31 @@ function od_handle_rest_request( WP_REST_Request $request ) {
210210
);
211211
}
212212

213-
/**
214-
* Filters whether a URL Metric is valid for storage.
215-
*
216-
* This allows for custom validation constraints to be applied beyond what can be expressed in JSON Schema. This is
217-
* also necessary because the 'validate_callback' key in a JSON Schema is not respected when gathering the REST API
218-
* endpoint args via the {@see rest_get_endpoint_args_for_schema()} function. Besides this, the REST API doesn't
219-
* support 'validate_callback' for any nested arguments in any case, meaning that custom constraints would be able
220-
* to be applied to multidimensional objects, such as the items inside 'elements'.
221-
*
222-
* This filter only applies when storing a URL Metric via the REST API. It does not run when a stored URL Metric
223-
* loaded from the od_url_metric post type. This means that validation logic enforced via this filter can be more
224-
* expensive, such as doing filesystem checks or HTTP requests.
225-
*
226-
* In addition to having the filter return `false` or a non-empty `WP_Error` to block storing the URL Metric, a
227-
* plugin may also mutate the OD_URL_Metric instance passed by reference to the filter callback. This is useful
228-
* for plugins in particular to unset extended properties which couldn't be validated using JSON Schema alone.
229-
*
230-
* @since n.e.x.t
231-
*
232-
* @param bool|WP_Error $validity Validity. Valid if true or a WP_Error without any errors, or invalid otherwise.
233-
* @param OD_Strict_URL_Metric $url_metric URL Metric, already validated against the JSON Schema.
234-
* @param array<string, mixed> $url_metric_data Original URL Metric data before any mutations.
235-
*/
236213
try {
237-
$validity = apply_filters( 'od_store_url_metric_validity', true, $url_metric, $url_metric->jsonSerialize() ); // TODO: A better name might be `od_url_metric_storage_validity`.
214+
/**
215+
* Filters whether a URL Metric is valid for storage.
216+
*
217+
* This allows for custom validation constraints to be applied beyond what can be expressed in JSON Schema. This is
218+
* also necessary because the 'validate_callback' key in a JSON Schema is not respected when gathering the REST API
219+
* endpoint args via the {@see rest_get_endpoint_args_for_schema()} function. Besides this, the REST API doesn't
220+
* support 'validate_callback' for any nested arguments in any case, meaning that custom constraints would be able
221+
* to be applied to multidimensional objects, such as the items inside 'elements'.
222+
*
223+
* This filter only applies when storing a URL Metric via the REST API. It does not run when a stored URL Metric is
224+
* loaded from the od_url_metrics post type. This means that validation logic enforced via this filter can be more
225+
* expensive, such as doing filesystem checks or HTTP requests.
226+
*
227+
* In addition to having the filter return `false` or a non-empty `WP_Error` to block storing the URL Metric, a
228+
* plugin may also mutate the OD_URL_Metric instance passed by reference to the filter callback. This is useful
229+
* for plugins in particular to unset extended properties which couldn't be validated using JSON Schema alone.
230+
*
231+
* @since n.e.x.t
232+
*
233+
* @param bool|WP_Error $validity Validity. Invalid if false or a WP_Error with errors.
234+
* @param OD_Strict_URL_Metric $url_metric URL Metric, already validated against the JSON Schema.
235+
* @param array<string, mixed> $url_metric_data Original URL Metric data before any mutations.
236+
*/
237+
$validity = apply_filters( 'od_url_metric_storage_validity', true, $url_metric, $url_metric->jsonSerialize() );
238238
} catch ( Exception $e ) {
239239
$error_data = null;
240240
if ( WP_DEBUG ) {
@@ -247,9 +247,9 @@ function od_handle_rest_request( WP_REST_Request $request ) {
247247
$validity = new WP_Error(
248248
'exception',
249249
sprintf(
250-
/* translators: %s is the filter name 'od_store_url_metric_validity' */
250+
/* translators: %s is the filter name 'od_url_metric_storage_validity' */
251251
__( 'An %s filter callback threw an exception.', 'optimization-detective' ),
252-
'od_store_url_metric_validity'
252+
'od_url_metric_storage_validity'
253253
),
254254
$error_data
255255
);

plugins/optimization-detective/tests/test-class-od-url-metrics-group-collection.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,6 @@ public function test_clear_cache(): void {
226226
$this->assertSame( array(), $collection_result_cache_reflection_property->getValue( $collection ) );
227227

228228
// Test that adding a URL metric to a collection clears the caches.
229-
$collection_result_cache_reflection_property->setValue( $collection, $populated_value );
230-
$group_result_cache_reflection_property->setValue( $group, $populated_value );
231-
232229
add_filter(
233230
'od_url_metric_schema_root_additional_properties',
234231
static function ( $schema ) {
@@ -247,7 +244,8 @@ static function ( $schema ) {
247244
return $schema;
248245
}
249246
);
250-
247+
$collection_result_cache_reflection_property->setValue( $collection, $populated_value );
248+
$group_result_cache_reflection_property->setValue( $group, $populated_value );
251249
$collection->add_url_metric(
252250
$this->get_sample_url_metric(
253251
array(

0 commit comments

Comments
 (0)