Skip to content

Commit ab6b54e

Browse files
authored
Merge pull request #1847 from WordPress/add/context-post-id
Add post ID for the `od_url_metrics` post to the tag visitor context
2 parents ff428a3 + 145f89e commit ab6b54e

File tree

12 files changed

+217
-57
lines changed

12 files changed

+217
-57
lines changed

plugins/optimization-detective/class-od-tag-visitor-context.php

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
*
1818
* @since 0.4.0
1919
*
20-
* @property-read OD_URL_Metric_Group_Collection $url_metrics_group_collection Deprecated property accessed via magic getter. Use the url_metric_group_collection property instead.
20+
* @property-read OD_HTML_Tag_Processor $processor HTML tag processor.
21+
* @property-read OD_URL_Metric_Group_Collection $url_metric_group_collection URL Metric group collection.
22+
* @property-read OD_Link_Collection $link_collection Link collection.
23+
* @property-read positive-int|null $url_metrics_id ID for the od_url_metrics post which provided the URL Metrics in the collection.
24+
* @property-read OD_URL_Metric_Group_Collection $url_metrics_group_collection Deprecated alias for the $url_metric_group_collection property.
2125
*/
2226
final class OD_Tag_Visitor_Context {
2327

@@ -26,31 +30,40 @@ final class OD_Tag_Visitor_Context {
2630
*
2731
* @since 0.4.0
2832
* @var OD_HTML_Tag_Processor
29-
* @readonly
3033
*/
31-
public $processor;
34+
private $processor;
3235

3336
/**
3437
* URL Metric group collection.
3538
*
3639
* @since 0.4.0
3740
* @var OD_URL_Metric_Group_Collection
38-
* @readonly
3941
*/
40-
public $url_metric_group_collection;
42+
private $url_metric_group_collection;
4143

4244
/**
4345
* Link collection.
4446
*
4547
* @since 0.4.0
4648
* @var OD_Link_Collection
47-
* @readonly
4849
*/
49-
public $link_collection;
50+
private $link_collection;
51+
52+
/**
53+
* ID for the od_url_metrics post which provided the URL Metrics in the collection.
54+
*
55+
* May be null if no post has been created yet.
56+
*
57+
* @since n.e.x.t
58+
* @var positive-int|null
59+
*/
60+
private $url_metrics_id;
5061

5162
/**
5263
* Visited tag state.
5364
*
65+
* Important: This object is not exposed directly by the getter. It is only exposed via {@see self::track_tag()}.
66+
*
5467
* @since 1.0.0
5568
* @var OD_Visited_Tag_State
5669
*/
@@ -65,12 +78,14 @@ final class OD_Tag_Visitor_Context {
6578
* @param OD_URL_Metric_Group_Collection $url_metric_group_collection URL Metric group collection.
6679
* @param OD_Link_Collection $link_collection Link collection.
6780
* @param OD_Visited_Tag_State $visited_tag_state Visited tag state.
81+
* @param positive-int|null $url_metrics_id ID for the od_url_metrics post which provided the URL Metrics in the collection. May be null if no post has been created yet.
6882
*/
69-
public function __construct( OD_HTML_Tag_Processor $processor, OD_URL_Metric_Group_Collection $url_metric_group_collection, OD_Link_Collection $link_collection, OD_Visited_Tag_State $visited_tag_state ) {
83+
public function __construct( OD_HTML_Tag_Processor $processor, OD_URL_Metric_Group_Collection $url_metric_group_collection, OD_Link_Collection $link_collection, OD_Visited_Tag_State $visited_tag_state, ?int $url_metrics_id ) {
7084
$this->processor = $processor;
7185
$this->url_metric_group_collection = $url_metric_group_collection;
7286
$this->link_collection = $link_collection;
7387
$this->visited_tag_state = $visited_tag_state;
88+
$this->url_metrics_id = $url_metrics_id;
7489
}
7590

7691
/**
@@ -85,39 +100,50 @@ public function track_tag(): void {
85100
}
86101

87102
/**
88-
* Gets deprecated property.
103+
* Gets a property.
89104
*
90105
* @since 0.7.0
91-
* @todo Remove this when no plugins are possibly referring to the url_metrics_group_collection property anymore.
92106
*
93107
* @param string $name Property name.
94-
* @return OD_URL_Metric_Group_Collection URL Metric group collection.
108+
* @return mixed Property value.
95109
*
96110
* @throws Error When property is unknown.
97111
*/
98-
public function __get( string $name ): OD_URL_Metric_Group_Collection {
99-
if ( 'url_metrics_group_collection' === $name ) {
100-
_doing_it_wrong(
101-
__CLASS__ . '::$url_metrics_group_collection',
102-
esc_html(
103-
sprintf(
104-
/* translators: %s is class member variable name */
105-
__( 'Use %s instead.', 'optimization-detective' ),
106-
__CLASS__ . '::$url_metric_group_collection'
112+
public function __get( string $name ) {
113+
// Note that there is intentionally not a case for 'visited_tag_state'.
114+
switch ( $name ) {
115+
case 'processor':
116+
return $this->processor;
117+
case 'link_collection':
118+
return $this->link_collection;
119+
case 'url_metrics_id':
120+
return $this->url_metrics_id;
121+
case 'url_metric_group_collection':
122+
return $this->url_metric_group_collection;
123+
case 'url_metrics_group_collection':
124+
// TODO: Remove this when no plugins are possibly referring to the url_metrics_group_collection property anymore.
125+
_doing_it_wrong(
126+
esc_html( __CLASS__ . '::$' . $name ),
127+
esc_html(
128+
sprintf(
129+
/* translators: %s is class member variable name */
130+
__( 'Use %s instead.', 'optimization-detective' ),
131+
__CLASS__ . '::$url_metric_group_collection'
132+
)
133+
),
134+
'optimization-detective 0.7.0'
135+
);
136+
return $this->url_metric_group_collection;
137+
default:
138+
throw new Error(
139+
esc_html(
140+
sprintf(
141+
/* translators: %s is class member variable name */
142+
__( 'Unknown property %s.', 'optimization-detective' ),
143+
__CLASS__ . '::$' . $name
144+
)
107145
)
108-
),
109-
'optimization-detective 0.7.0'
110-
);
111-
return $this->url_metric_group_collection;
146+
);
112147
}
113-
throw new Error(
114-
esc_html(
115-
sprintf(
116-
/* translators: %s is class member variable name */
117-
__( 'Unknown property %s.', 'optimization-detective' ),
118-
__CLASS__ . '::$' . $name
119-
)
120-
)
121-
);
122148
}
123149
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,18 @@ public function get_freshness_ttl(): int {
208208
return $this->freshness_ttl;
209209
}
210210

211+
/**
212+
* Gets the collection that this group is a part of.
213+
*
214+
* @since n.e.x.t
215+
*
216+
* @todo Eliminate in favor of readonly public property.
217+
* @return OD_URL_Metric_Group_Collection Collection.
218+
*/
219+
public function get_collection(): OD_URL_Metric_Group_Collection {
220+
return $this->collection;
221+
}
222+
211223
/**
212224
* Checks whether the provided viewport width is between the minimum (exclusive) and maximum (inclusive).
213225
*

plugins/optimization-detective/load.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Description: Provides an API for leveraging real user metrics to detect optimizations to apply on the frontend to improve page performance.
66
* Requires at least: 6.6
77
* Requires PHP: 7.2
8-
* Version: 1.0.0-beta1
8+
* Version: 1.0.0-beta2
99
* Author: WordPress Performance Team
1010
* Author URI: https://make.wordpress.org/performance/
1111
* License: GPLv2 or later
@@ -71,7 +71,7 @@ static function ( string $global_var_name, string $version, Closure $load ): voi
7171
}
7272
)(
7373
'optimization_detective_pending_plugin',
74-
'1.0.0-beta1',
74+
'1.0.0-beta2',
7575
static function ( string $version ): void {
7676
if ( defined( 'OPTIMIZATION_DETECTIVE_VERSION' ) ) {
7777
return;

plugins/optimization-detective/optimization.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,13 @@ function od_optimize_template_output_buffer( string $buffer ): string {
269269
);
270270
$link_collection = new OD_Link_Collection();
271271
$visited_tag_state = new OD_Visited_Tag_State();
272-
$tag_visitor_context = new OD_Tag_Visitor_Context( $processor, $group_collection, $link_collection, $visited_tag_state );
272+
$tag_visitor_context = new OD_Tag_Visitor_Context(
273+
$processor,
274+
$group_collection,
275+
$link_collection,
276+
$visited_tag_state,
277+
$post instanceof WP_Post && $post->ID > 0 ? $post->ID : null
278+
);
273279
$current_tag_bookmark = 'optimization_detective_current_tag';
274280
$visitors = iterator_to_array( $tag_visitor_registry );
275281

plugins/optimization-detective/readme.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Contributors: wordpressdotorg
44
Tested up to: 6.7
5-
Stable tag: 1.0.0-beta1
5+
Stable tag: 1.0.0-beta2
66
License: GPLv2 or later
77
License URI: https://www.gnu.org/licenses/gpl-2.0.html
88
Tags: performance, optimization, rum
@@ -55,6 +55,8 @@ The [plugin source code](https://github.com/WordPress/performance/tree/trunk/plu
5555

5656
== Changelog ==
5757

58+
= 1.0.0-beta2 =
59+
5860
= 1.0.0-beta1 =
5961

6062
**Enhancements**

plugins/optimization-detective/storage/class-od-url-metric-store-request-context.php

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,66 +16,124 @@
1616
* Context for when a URL Metric is successfully stored via the REST API.
1717
*
1818
* @since 0.7.0
19-
* @access private
19+
*
20+
* @property-read WP_REST_Request<array<string, mixed>> $request Request.
21+
* @property-read positive-int $url_metrics_id ID for the od_url_metrics post.
22+
* @property-read OD_URL_Metric_Group_Collection $url_metric_group_collection URL Metric group collection.
23+
* @property-read OD_URL_Metric_Group $url_metric_group URL Metric group.
24+
* @property-read OD_URL_Metric $url_metric URL Metric.
25+
* @property-read positive-int $post_id Deprecated alias for the $url_metrics_id property.
2026
*/
2127
final class OD_URL_Metric_Store_Request_Context {
2228

2329
/**
2430
* Request.
2531
*
32+
* @since 0.7.0
2633
* @var WP_REST_Request<array<string, mixed>>
27-
* @readonly
2834
*/
29-
public $request;
35+
private $request;
3036

3137
/**
32-
* ID for the URL Metric post.
38+
* ID for the od_url_metrics post.
39+
*
40+
* This was originally $post_id which was introduced in 0.7.0.
3341
*
42+
* @since n.e.x.t
3443
* @var positive-int
35-
* @readonly
3644
*/
37-
public $post_id;
45+
private $url_metrics_id;
3846

3947
/**
4048
* URL Metric group collection.
4149
*
50+
* @since 0.7.0
4251
* @var OD_URL_Metric_Group_Collection
43-
* @readonly
4452
*/
45-
public $url_metric_group_collection;
53+
private $url_metric_group_collection;
4654

4755
/**
4856
* URL Metric group.
4957
*
58+
* @since 0.7.0
5059
* @var OD_URL_Metric_Group
51-
* @readonly
5260
*/
53-
public $url_metric_group;
61+
private $url_metric_group;
5462

5563
/**
5664
* URL Metric.
5765
*
66+
* @since 0.7.0
5867
* @var OD_URL_Metric
59-
* @readonly
6068
*/
61-
public $url_metric;
69+
private $url_metric;
6270

6371
/**
6472
* Constructor.
6573
*
74+
* @since 0.7.0
75+
*
6676
* @phpstan-param WP_REST_Request<array<string, mixed>> $request
6777
*
6878
* @param WP_REST_Request $request REST API request.
69-
* @param positive-int $post_id ID for the URL Metric post.
79+
* @param positive-int $url_metrics_id ID for the URL Metric post.
7080
* @param OD_URL_Metric_Group_Collection $url_metric_group_collection URL Metric group collection.
7181
* @param OD_URL_Metric_Group $url_metric_group URL Metric group.
7282
* @param OD_URL_Metric $url_metric URL Metric.
7383
*/
74-
public function __construct( WP_REST_Request $request, int $post_id, OD_URL_Metric_Group_Collection $url_metric_group_collection, OD_URL_Metric_Group $url_metric_group, OD_URL_Metric $url_metric ) {
84+
public function __construct( WP_REST_Request $request, int $url_metrics_id, OD_URL_Metric_Group_Collection $url_metric_group_collection, OD_URL_Metric_Group $url_metric_group, OD_URL_Metric $url_metric ) {
7585
$this->request = $request;
76-
$this->post_id = $post_id;
86+
$this->url_metrics_id = $url_metrics_id;
7787
$this->url_metric_group_collection = $url_metric_group_collection;
7888
$this->url_metric_group = $url_metric_group;
7989
$this->url_metric = $url_metric;
8090
}
91+
92+
/**
93+
* Gets a property.
94+
*
95+
* @since n.e.x.t
96+
*
97+
* @param string $name Property name.
98+
* @return mixed Property value.
99+
*
100+
* @throws Error When property is unknown.
101+
*/
102+
public function __get( string $name ) {
103+
switch ( $name ) {
104+
case 'request':
105+
return $this->request;
106+
case 'url_metrics_id':
107+
return $this->url_metrics_id;
108+
case 'url_metric_group_collection':
109+
return $this->url_metric_group_collection;
110+
case 'url_metric_group':
111+
return $this->url_metric_group;
112+
case 'url_metric':
113+
return $this->url_metric;
114+
case 'post_id':
115+
_doing_it_wrong(
116+
esc_html( __CLASS__ . '::$' . $name ),
117+
esc_html(
118+
sprintf(
119+
/* translators: %s is class member variable name */
120+
__( 'Use %s instead.', 'optimization-detective' ),
121+
__CLASS__ . '::$url_metrics_id'
122+
)
123+
),
124+
'optimization-detective n.e.x.t'
125+
);
126+
return $this->url_metrics_id;
127+
default:
128+
throw new Error(
129+
esc_html(
130+
sprintf(
131+
/* translators: %s is class member variable name */
132+
__( 'Unknown property %s.', 'optimization-detective' ),
133+
__CLASS__ . '::$' . $name
134+
)
135+
)
136+
);
137+
}
138+
}
81139
}

0 commit comments

Comments
 (0)