Skip to content

De-duplicate logic between REST API and URL Metrics post type#1867

Merged
westonruter merged 12 commits intoWordPress:trunkfrom
ShyamGadde:update/logic-for-updating-url-metrics
Feb 27, 2025
Merged

De-duplicate logic between REST API and URL Metrics post type#1867
westonruter merged 12 commits intoWordPress:trunkfrom
ShyamGadde:update/logic-for-updating-url-metrics

Conversation

@ShyamGadde
Copy link
Copy Markdown
Contributor

Summary

Fixes #1858

Relevant technical choices

  • Introduced OD_URL_Metrics_Post_Type::update_post( string $slug, string $post_title, OD_URL_Metric_Group_Collection $url_metric_group_collection ) to replace store_url_metric(), eliminating redundant logic.
  • Updated REST API to use update_post() instead of store_url_metric(), ensuring the existing OD_URL_Metric_Group_Collection is passed directly.
  • Moved store_url_metric() to Optimization_Detective_Test_Helpers for test compatibility.
  • Refactored affected tests to align with the new method structure.

Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
@ShyamGadde ShyamGadde added [Type] Enhancement A suggestion for improvement of an existing feature [Plugin] Optimization Detective Issues for the Optimization Detective plugin labels Feb 12, 2025
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 12, 2025

Codecov Report

Attention: Patch coverage is 94.44444% with 1 line in your changes missing coverage. Please review.

Project coverage is 69.59%. Comparing base (4a79036) to head (e8cd6bd).
Report is 13 commits behind head on trunk.

Files with missing lines Patch % Lines
...lugins/optimization-detective/storage/rest-api.php 75.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            trunk    #1867   +/-   ##
=======================================
  Coverage   69.59%   69.59%           
=======================================
  Files          86       86           
  Lines        6985     6983    -2     
=======================================
- Hits         4861     4860    -1     
+ Misses       2124     2123    -1     
Flag Coverage Δ
multisite 69.59% <94.44%> (+<0.01%) ⬆️
single 39.62% <0.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
…etric

Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
@ShyamGadde ShyamGadde marked this pull request as ready for review February 12, 2025 08:18
@github-actions
Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ShyamGadde <shyamgadde@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Comment on lines +177 to +235
$post_data = array(
'post_title' => $new_url_metric->get_url(),
);

$post = OD_URL_Metrics_Post_Type::get_post( $slug );
if ( $post instanceof WP_Post ) {
$post_data['ID'] = $post->ID;
$post_data['post_name'] = $post->post_name;
$url_metrics = OD_URL_Metrics_Post_Type::get_url_metrics_from_post( $post );
} else {
$post_data['post_name'] = $slug;
$url_metrics = array();
}

$etag = $new_url_metric->get_etag();

$group_collection = new OD_URL_Metric_Group_Collection(
$url_metrics,
$etag,
od_get_breakpoint_max_widths(),
od_get_url_metrics_breakpoint_sample_size(),
od_get_url_metric_freshness_ttl()
);

try {
$group = $group_collection->get_group_for_viewport_width( $new_url_metric->get_viewport_width() );
$group->add_url_metric( $new_url_metric );
} catch ( InvalidArgumentException $e ) {
return new WP_Error( 'invalid_url_metric', $e->getMessage() );
}

$post_data['post_content'] = wp_json_encode(
$group_collection->get_flattened_url_metrics(),
JSON_UNESCAPED_SLASHES // No need for escaping slashes since this JSON is not embedded in HTML.
);
if ( ! is_string( $post_data['post_content'] ) ) {
return new WP_Error( 'json_encode_error', json_last_error_msg() );
}

$has_kses = false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' );
if ( $has_kses ) {
// Prevent KSES from corrupting JSON in post_content.
kses_remove_filters();
}

$post_data['post_type'] = OD_URL_Metrics_Post_Type::SLUG;
$post_data['post_status'] = 'publish';
$slashed_post_data = wp_slash( $post_data );
if ( isset( $post_data['ID'] ) ) {
$result = wp_update_post( $slashed_post_data, true );
} else {
$result = wp_insert_post( $slashed_post_data, true );
}

if ( $has_kses ) {
kses_init_filters();
}

return $result;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Couldn't this be simplified now to just construct the OD_URL_Metric_Group_Collection, add the URL Metric to it, and then pass it to OD_URL_Metrics_Post_Type::update_post()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. Done in aaf48f6.

Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
Copy link
Copy Markdown
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Sorry for the delay! Just a couple minor things left.

ShyamGadde and others added 3 commits February 27, 2025 09:32
Co-authored-by: Weston Ruter <westonruter@google.com>
…andling

Co-authored-by: Weston Ruter <westonruter@google.com>
Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
Copy link
Copy Markdown
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Enhancement A suggestion for improvement of an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logic should be de-duplicated between REST API and URL Metrics post type class

2 participants