Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement garbage collection of URL metrics including at uninstallation #1055

Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
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
38 changes: 2 additions & 36 deletions plugins/optimization-detective/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,6 @@
exit; // Exit if accessed directly.
}

/**
* Starts output buffering at the end of the 'template_include' filter.
*
* This is to implement #43258 in core.
*
* This is a hack which would eventually be replaced with something like this in wp-includes/template-loader.php:
*
* $template = apply_filters( 'template_include', $template );
* + ob_start( 'wp_template_output_buffer_callback' );
* if ( $template ) {
* include $template;
* } elseif ( current_user_can( 'switch_themes' ) ) {
*
* @since 0.1.0
* @access private
* @link https://core.trac.wordpress.org/ticket/43258
*
* @param string $passthrough Optional. Filter value. Default null.
* @return string Unmodified value of $passthrough.
*/
function od_buffer_output( string $passthrough ): string {
ob_start(
static function ( string $output ): string {
/**
* Filters the template output buffer prior to sending to the client.
*
* @since 0.1.0
*
* @param string $output Output buffer.
* @return string Filtered output buffer.
*/
return (string) apply_filters( 'od_template_output_buffer', $output );
}
);
return $passthrough;
}
add_filter( 'template_include', 'od_buffer_output', PHP_INT_MAX );
OD_URL_Metrics_Post_Type::add_hooks();
add_action( 'wp', 'od_maybe_add_template_output_buffer_filter' );
9 changes: 6 additions & 3 deletions plugins/optimization-detective/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,22 @@

define( 'OPTIMIZATION_DETECTIVE_VERSION', '0.1.0' );

require_once __DIR__ . '/hooks.php';

// Storage logic.
require_once __DIR__ . '/class-od-data-validation-exception.php';
require_once __DIR__ . '/class-od-url-metric.php';
require_once __DIR__ . '/class-od-url-metrics-group.php';
require_once __DIR__ . '/class-od-url-metrics-group-collection.php';
require_once __DIR__ . '/class-od-storage-lock.php';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess all of these should go into storage then too.

Copy link
Member Author

@westonruter westonruter Mar 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved in b5623fd 814f57d

Copy link
Member

@felixarntz felixarntz Mar 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@westonruter Maybe. OD_Storage_Lock probably yes, for the URL metric classes not necessarily - though I don't feel strongly about it. Those are the central infrastructure classes used for the entire functionality, IMO they aren't just storage related as e.g. the post type class is.

I'm okay with either, though please align the test files structure with whatever you go with here as well (e.g. right now the post type class tests are not in the storage folder even though that class is in the storage folder). Nit-picking... :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 33b0272

require_once __DIR__ . '/storage/post-type.php';
require_once __DIR__ . '/storage/class-od-url-metrics-post-type.php';
require_once __DIR__ . '/storage/data.php';
require_once __DIR__ . '/storage/rest-api.php';

// Detection logic.
require_once __DIR__ . '/detection.php';

// Optimization logic.
require_once __DIR__ . '/class-od-html-tag-processor.php';
require_once __DIR__ . '/optimization.php';

// Add hooks for the above requires.
require_once __DIR__ . '/hooks.php';
42 changes: 39 additions & 3 deletions plugins/optimization-detective/optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,43 @@
exit; // Exit if accessed directly.
}

/**
* Starts output buffering at the end of the 'template_include' filter.
*
* This is to implement #43258 in core.
*
* This is a hack which would eventually be replaced with something like this in wp-includes/template-loader.php:
*
* $template = apply_filters( 'template_include', $template );
* + ob_start( 'wp_template_output_buffer_callback' );
* if ( $template ) {
* include $template;
* } elseif ( current_user_can( 'switch_themes' ) ) {
*
* @since 0.1.0
* @access private
* @link https://core.trac.wordpress.org/ticket/43258
*
* @param string $passthrough Optional. Filter value. Default null.
* @return string Unmodified value of $passthrough.
*/
function od_buffer_output( string $passthrough ): string {
ob_start(
static function ( string $output ): string {
/**
* Filters the template output buffer prior to sending to the client.
*
* @since 0.1.0
*
* @param string $output Output buffer.
* @return string Filtered output buffer.
*/
return (string) apply_filters( 'od_template_output_buffer', $output );
}
);
return $passthrough;
}

/**
* Adds template output buffer filter for optimization if eligible.
*
Expand All @@ -26,7 +63,6 @@ function od_maybe_add_template_output_buffer_filter() {
}
add_filter( 'od_template_output_buffer', $callback );
}
add_action( 'wp', 'od_maybe_add_template_output_buffer_filter' );

/**
* Determines whether the current response can be optimized.
Expand Down Expand Up @@ -147,10 +183,10 @@ function od_construct_preload_links( array $lcp_elements_by_minimum_viewport_wid
*/
function od_optimize_template_output_buffer( string $buffer ): string {
$slug = od_get_url_metrics_slug( od_get_normalized_query_vars() );
$post = od_get_url_metrics_post( $slug );
$post = OD_URL_Metrics_Post_Type::get_post( $slug );

$group_collection = new OD_URL_Metrics_Group_Collection(
$post ? od_parse_stored_url_metrics( $post ) : array(),
$post ? OD_URL_Metrics_Post_Type::get_url_metrics_from_post( $post ) : array(),
od_get_breakpoint_max_widths(),
od_get_url_metrics_breakpoint_sample_size(),
od_get_url_metric_freshness_ttl()
Expand Down
Loading
Loading