-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathclass-od-strict-url-metric.php
More file actions
86 lines (76 loc) · 2.57 KB
/
class-od-strict-url-metric.php
File metadata and controls
86 lines (76 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* Optimization Detective: OD_Strict_URL_Metric class
*
* @package optimization-detective
* @since 0.6.0
*/
declare( strict_types = 1 );
// @codeCoverageIgnoreStart
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
// @codeCoverageIgnoreEnd
/**
* Representation of the measurements taken from a single client's visit to a specific URL without additionalProperties allowed.
*
* This is used exclusively in the REST API endpoint for capturing new URL Metrics to prevent invalid additional data from being
* submitted in the request. For URL Metrics which have been stored, the looser OD_URL_Metric class is used instead.
*
* @phpstan-import-type JSONSchema from OD_URL_Metric
*
* @since 0.6.0
* @access private
*/
final class OD_Strict_URL_Metric extends OD_URL_Metric {
/**
* Gets JSON schema for URL Metric without additionalProperties.
*
* @since 0.6.0
*
* @return JSONSchema Schema.
*/
public static function get_json_schema(): array {
return self::set_additional_properties_to_false( parent::get_json_schema() );
}
/**
* Recursively processes the schema to ensure that all objects have additionalProperties set to false.
*
* This is a forked version of `rest_default_additional_properties_to_false()` which isn't being used itself because
* it does not override `additionalProperties` to be false, but rather only sets it when it is empty.
*
* @since 0.6.0
* @see rest_default_additional_properties_to_false()
*
* @phpstan-param JSONSchema $schema
*
* @param array<string, mixed> $schema Schema.
* @return JSONSchema Processed schema.
*/
private static function set_additional_properties_to_false( array $schema ): array {
$type = (array) $schema['type'];
if ( in_array( 'object', $type, true ) ) {
if ( isset( $schema['properties'] ) ) {
foreach ( $schema['properties'] as $key => $child_schema ) {
if ( isset( $child_schema['type'] ) ) {
$schema['properties'][ $key ] = self::set_additional_properties_to_false( $child_schema );
}
}
}
if ( isset( $schema['patternProperties'] ) ) {
foreach ( $schema['patternProperties'] as $key => $child_schema ) {
if ( isset( $child_schema['type'] ) ) {
$schema['patternProperties'][ $key ] = self::set_additional_properties_to_false( $child_schema );
}
}
}
$schema['additionalProperties'] = false;
}
if ( in_array( 'array', $type, true ) ) {
if ( isset( $schema['items'], $schema['items']['type'] ) ) {
$schema['items'] = self::set_additional_properties_to_false( $schema['items'] );
}
}
return $schema;
}
}