-
Notifications
You must be signed in to change notification settings - Fork 110
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
De-duplicate logic between REST API and URL Metrics post type #1867
De-duplicate logic between REST API and URL Metrics post type #1867
Conversation
Signed-off-by: Shyamsundar Gadde <[email protected]>
Signed-off-by: Shyamsundar Gadde <[email protected]>
Signed-off-by: Shyamsundar Gadde <[email protected]>
Codecov ReportAttention: Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Signed-off-by: Shyamsundar Gadde <[email protected]>
plugins/optimization-detective/storage/class-od-url-metrics-post-type.php
Outdated
Show resolved
Hide resolved
…etric Signed-off-by: Shyamsundar Gadde <[email protected]>
Signed-off-by: Shyamsundar Gadde <[email protected]>
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
plugins/optimization-detective/storage/class-od-url-metrics-post-type.php
Show resolved
Hide resolved
$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; |
There was a problem hiding this comment.
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()
?
There was a problem hiding this comment.
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 <[email protected]>
Signed-off-by: Shyamsundar Gadde <[email protected]>
There was a problem hiding this 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.
plugins/optimization-detective/tests/class-optimization-detective-test-helpers.php
Outdated
Show resolved
Hide resolved
plugins/optimization-detective/storage/class-od-url-metrics-post-type.php
Outdated
Show resolved
Hide resolved
plugins/optimization-detective/tests/class-optimization-detective-test-helpers.php
Outdated
Show resolved
Hide resolved
plugins/optimization-detective/tests/storage/test-class-od-url-metrics-post-type.php
Outdated
Show resolved
Hide resolved
plugins/optimization-detective/tests/storage/test-class-od-url-metrics-post-type.php
Outdated
Show resolved
Hide resolved
Co-authored-by: Weston Ruter <[email protected]>
…andling Co-authored-by: Weston Ruter <[email protected]>
Signed-off-by: Shyamsundar Gadde <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Summary
Fixes #1858
Relevant technical choices
OD_URL_Metrics_Post_Type::update_post( string $slug, string $post_title, OD_URL_Metric_Group_Collection $url_metric_group_collection )
to replacestore_url_metric()
, eliminating redundant logic.update_post()
instead ofstore_url_metric()
, ensuring the existingOD_URL_Metric_Group_Collection
is passed directly.store_url_metric()
toOptimization_Detective_Test_Helpers
for test compatibility.