-
Notifications
You must be signed in to change notification settings - Fork 842
Improve Post Stats cache handling for invalid or error data #46211
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
Changes from 4 commits
e0ceb39
8877038
a736465
ffe8c0c
3c3624b
4ba2b0e
68d3fe3
3bbe021
5b9fc39
bd5a7b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: patch | ||
| Type: changed | ||
|
|
||
| Improve Post Stats cache handling for invalid or error data |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -508,15 +508,15 @@ protected function fetch_post_stats( $args, $post_id ) { | |
| || empty( $data ) | ||
| || is_wp_error( $data ) | ||
| ) { | ||
| return $data; | ||
| return $this->refresh_post_stats_cache( $endpoint, $args, $post_id, $meta_name ); | ||
| } | ||
|
|
||
| $time = key( $data ); | ||
| $views = $data[ $time ] ?? null; | ||
|
|
||
| // Bail if data is malformed. | ||
| if ( ! is_numeric( $time ) || ! is_array( $views ) ) { | ||
| return $data; | ||
| return $this->refresh_post_stats_cache( $endpoint, $args, $post_id, $meta_name ); | ||
| } | ||
|
|
||
| /** This filter is already documented in projects/packages/stats/src/class-wpcom-stats.php */ | ||
|
|
@@ -530,8 +530,26 @@ protected function fetch_post_stats( $args, $post_id ) { | |
| } | ||
| } | ||
|
|
||
| return $this->refresh_post_stats_cache( $endpoint, $args, $post_id, $meta_name ); | ||
| } | ||
|
|
||
| /** | ||
| * Force fetch stats from WPCOM, and update cache if needed. | ||
| * | ||
| * @param string $endpoint The stats endpoint. | ||
| * @param array $args The query arguments. | ||
| * @param int $post_id The post ID. | ||
| * @param string $meta_name The meta name. | ||
| * | ||
| * @return array|WP_Error | ||
| */ | ||
| protected function refresh_post_stats_cache( $endpoint, $args, $post_id, $meta_name ) { | ||
| $wpcom_stats = $this->fetch_remote_stats( $endpoint, $args ); | ||
| update_post_meta( $post_id, $meta_name, array( time() => $wpcom_stats ) ); | ||
|
|
||
| // Don't write error or empty results to cache. | ||
| if ( ! is_wp_error( $wpcom_stats ) && ! empty( $wpcom_stats ) ) { | ||
|
||
| update_post_meta( $post_id, $meta_name, array( time() => $wpcom_stats ) ); | ||
| } | ||
|
||
|
|
||
| return $wpcom_stats; | ||
| } | ||
kangzj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.