Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Introduce OD_Template_Optimization_Context
  • Loading branch information
westonruter committed Mar 13, 2025
commit f7da494939047503e0552626a0ed532e463eaa9d
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
/**
* Optimization Detective: OD_Template_Optimization_Context class
*
* @package optimization-detective
* @since n.e.x.t
*/

// @codeCoverageIgnoreStart
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
// @codeCoverageIgnoreEnd

/**
* Context for optimizing a template prior to rendering.
*
* @since n.e.x.t
*
* @property-read OD_URL_Metric_Group_Collection $url_metric_group_collection URL Metric group collection.
* @property-read OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
* @property-read positive-int|null $url_metrics_id ID for the od_url_metrics post which provided the URL Metrics in the collection.
* @property-read array<string, mixed> $normalized_query_vars Normalized query vars.
* @property-read non-empty-string $url_metrics_slug Slug for the od_url_metrics post.
* @property-read non-empty-string $current_etag Current ETag.
*/
final class OD_Template_Optimization_Context {

/**
* URL Metric group collection.
*
* @since n.e.x.t
* @var OD_URL_Metric_Group_Collection
*/
private $url_metric_group_collection;

/**
* Tag visitor registry.
*
* @since n.e.x.t
* @var OD_Tag_Visitor_Registry
*/
private $tag_visitor_registry;

/**
* 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.
*
* @since n.e.x.t
* @var positive-int|null
*/
private $url_metrics_id;

/**
* Normalized query vars.
*
* @since n.e.x.t
* @var array<string, mixed>
*/
private $normalized_query_vars;

/**
* Slug for the od_url_metrics post.
*
* @since n.e.x.t
* @var non-empty-string
*/
private $url_metrics_slug;

/**
* Current ETag.
*
* @since n.e.x.t
* @var non-empty-string
*/
private $current_etag;

/**
* Constructor.
*
* @since n.e.x.t
*
* @param OD_URL_Metric_Group_Collection $url_metric_group_collection URL Metric group collection.
* @param OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
* @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.
* @param array<string, mixed> $normalized_query_vars Normalized query vars.
* @param non-empty-string $url_metrics_slug Slug for the od_url_metrics post.
* @param non-empty-string $current_etag Current ETag.
*/
public function __construct( OD_URL_Metric_Group_Collection $url_metric_group_collection, OD_Tag_Visitor_Registry $tag_visitor_registry, ?int $url_metrics_id, array $normalized_query_vars, string $url_metrics_slug, string $current_etag ) {
$this->url_metric_group_collection = $url_metric_group_collection;
$this->tag_visitor_registry = $tag_visitor_registry;
$this->url_metrics_id = $url_metrics_id;
$this->normalized_query_vars = $normalized_query_vars;
$this->url_metrics_slug = $url_metrics_slug;
$this->current_etag = $current_etag;
}

/**
* Gets a property.
*
* @since n.e.x.t
*
* @param string $name Property name.
* @return mixed Property value.
*
* @throws Error When property is unknown.
*/
public function __get( string $name ) {
switch ( $name ) {
case 'tag_visitor_registry':
return $this->tag_visitor_registry;
case 'url_metrics_id':
return $this->url_metrics_id;
case 'url_metric_group_collection':
return $this->url_metric_group_collection;
case 'normalized_query_vars':
return $this->normalized_query_vars;
case 'url_metrics_slug':
return $this->url_metrics_slug;
case 'current_etag':
return $this->current_etag;
default:
throw new Error(
esc_html(
sprintf(
/* translators: %s is class member variable name */
__( 'Unknown property %s.', 'optimization-detective' ),
__CLASS__ . '::$' . $name
)
)
);
}
}
}
1 change: 1 addition & 0 deletions plugins/optimization-detective/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class_alias( OD_URL_Metric_Group_Collection::class, 'OD_URL_Metrics_Group_Collec
require_once __DIR__ . '/detection.php';

// Optimization logic.
require_once __DIR__ . '/class-od-template-optimization-context.php';
require_once __DIR__ . '/class-od-link-collection.php';
require_once __DIR__ . '/class-od-tag-visitor-registry.php';
require_once __DIR__ . '/class-od-visited-tag-state.php';
Expand Down
24 changes: 16 additions & 8 deletions plugins/optimization-detective/optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ static function () use ( $reasons ): void {
* @return non-empty-string|mixed Passed-through template.
*/
function od_add_template_output_buffer_filter( $template ) {
$slug = od_get_url_metrics_slug( od_get_normalized_query_vars() );
$post = OD_URL_Metrics_Post_Type::get_post( $slug );
$post_id = $post instanceof WP_Post && $post->ID > 0 ? $post->ID : null;
$query_vars = od_get_normalized_query_vars();
$slug = od_get_url_metrics_slug( $query_vars );
$post = OD_URL_Metrics_Post_Type::get_post( $slug );
$post_id = $post instanceof WP_Post && $post->ID > 0 ? $post->ID : null;

$tag_visitor_registry = new OD_Tag_Visitor_Registry();

Expand Down Expand Up @@ -167,13 +168,20 @@ function od_add_template_output_buffer_filter( $template ) {
* Fires when Optimization Detective is initialized to optimize the current response.
*
* @since n.e.x.t
* @todo The parameters should be put into a context object as is done with other such actions.
*
* @param OD_URL_Metric_Group_Collection $group_collection URL Metric group collection.
* @param OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
* @param WP_Post|null $post The od_url_metrics post if it exists.
* @param OD_Template_Optimization_Context $context Template optimization context.
*/
do_action( 'od_start_template_optimization', $group_collection, $tag_visitor_registry, $post );
do_action(
'od_start_template_optimization',
new OD_Template_Optimization_Context(
$group_collection,
$tag_visitor_registry,
$post_id,
$query_vars,
$slug,
$current_etag
)
);

$callback = static function ( string $buffer ) use ( $tag_visitor_registry, $group_collection, $slug, $post_id ): string {
return od_optimize_template_output_buffer(
Expand Down
2 changes: 1 addition & 1 deletion plugins/optimization-detective/storage/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function od_get_current_theme_template( ?string $template ) {
}
}
if ( isset( $template ) ) {
return basename( $template );
return basename( $template ); // TODO: Why basename here?
}
return null;
}
Expand Down