-
Notifications
You must be signed in to change notification settings - Fork 384
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
Cache error index counts and URL count in "At a Glance" widget #5343
Conversation
Plugin builds for 4fe39ad are ready 🛎️!
|
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.
The transient will also need to be deleted in \AMP_Validated_URL_Post_Type::handle_validation_error_status_update()
.
Also when an amp_validated_url
post is deleted.
Basically I think wherever we have a delete_transient( AMP_Validated_URL_Post_Type::NEW_VALIDATION_ERROR_URLS_COUNT_TRANSIENT )
we'll need to also add deletion of this other transient.
All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the ℹ️ Googlers: Go here for more info. |
@googlebot I consent. |
Added transient deletion in more places, and it looks like Pierre helped with the unrelated e2e test failure. |
@@ -219,6 +219,7 @@ static function () { | |||
$handle_delete = static function ( $post_id ) { | |||
if ( static::POST_TYPE_SLUG === get_post_type( $post_id ) ) { | |||
delete_transient( static::NEW_VALIDATION_ERROR_URLS_COUNT_TRANSIENT ); | |||
delete_transient( AMP_Validation_Error_Taxonomy::TRANSIENT_KEY_ERROR_INDEX_COUNTS ); |
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.
Just saw that the value stored in the TRANSIENT_KEY_ERROR_INDEX_COUNTS
is only amp_validation_error
taxonomy terms irrespective of amp_validated_url
posts (hide_empty
is not set to true
when calling wp_count_terms()
). But someone could pass 'hide_empty' => true
when calling \AMP_Validation_Error_Taxonomy::get_validation_error_count()
. So I suppose this is safest to include.
@@ -1970,6 +1972,7 @@ static function( $result ) { | |||
} | |||
|
|||
delete_transient( static::NEW_VALIDATION_ERROR_URLS_COUNT_TRANSIENT ); | |||
delete_transient( AMP_Validation_Error_Taxonomy::TRANSIENT_KEY_ERROR_INDEX_COUNTS ); |
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.
I realize this may not be necessary because recheck_post()
will call AMP_Validated_URL_Post_Type::store_validation_errors()
which will then clear both of these transients.
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.
Yeah, it won't be needed with clearing transient at the edit_amp_validation_error
action.
delete_transient( AMP_Validation_Error_Taxonomy::TRANSIENT_KEY_ERROR_INDEX_COUNTS ); |
$count = get_transient( static::NEW_VALIDATION_ERROR_URLS_COUNT_TRANSIENT ); | ||
|
||
$query = new WP_Query( | ||
[ | ||
'post_type' => self::POST_TYPE_SLUG, | ||
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_STATUS_QUERY_VAR => [ | ||
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_REJECTED_STATUS, | ||
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_ACCEPTED_STATUS, | ||
], | ||
'update_post_meta_cache' => false, | ||
'update_post_term_cache' => false, | ||
] | ||
); | ||
if ( false === $count ) { | ||
|
||
$query = new WP_Query( | ||
[ | ||
'post_type' => self::POST_TYPE_SLUG, | ||
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_STATUS_QUERY_VAR => [ | ||
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_REJECTED_STATUS, | ||
AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_ACCEPTED_STATUS, | ||
], | ||
'update_post_meta_cache' => false, | ||
'update_post_term_cache' => false, | ||
] | ||
); | ||
|
||
$count = $query->found_posts; | ||
set_transient( static::NEW_VALIDATION_ERROR_URLS_COUNT_TRANSIENT, $count, DAY_IN_SECONDS ); | ||
} |
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.
It turns out the logic here is duplicated with the logic in \AMP_Validated_URL_Post_Type::get_validation_error_urls_count()
. What we are missing is setting the transient inside this method.
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.
Done in b98b921
@@ -957,6 +958,7 @@ public static function store_validation_errors( $validation_errors, $url, $args | |||
} | |||
|
|||
delete_transient( static::NEW_VALIDATION_ERROR_URLS_COUNT_TRANSIENT ); | |||
delete_transient( AMP_Validation_Error_Taxonomy::TRANSIENT_KEY_ERROR_INDEX_COUNTS ); |
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.
This is correct as terms may be created/updated.
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.
However, it is not needed if we delete the transient at the edit_amp_validation_error
action.
delete_transient( AMP_Validation_Error_Taxonomy::TRANSIENT_KEY_ERROR_INDEX_COUNTS ); |
|
||
delete_transient( self::TRANSIENT_KEY_ERROR_INDEX_COUNTS ); | ||
|
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.
Instead of deleting the transient here, we can catch more cases by adding a delete_amp_validation_error
action above.
delete_transient( self::TRANSIENT_KEY_ERROR_INDEX_COUNTS ); |
@@ -279,6 +286,8 @@ public static function register() { | |||
if ( is_admin() ) { | |||
self::add_admin_hooks(); | |||
} | |||
|
|||
add_action( 'created_' . self::TAXONOMY_SLUG, [ __CLASS__, 'clear_cached_counts' ] ); |
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.
add_action( 'created_' . self::TAXONOMY_SLUG, [ __CLASS__, 'clear_cached_counts' ] ); | |
add_action( 'created_' . self::TAXONOMY_SLUG, [ __CLASS__, 'clear_cached_counts' ] ); | |
add_action( 'delete_' . self::TAXONOMY_SLUG, [ __CLASS__, 'clear_cached_counts' ] ); | |
add_action( 'edit_' . self::TAXONOMY_SLUG, [ __CLASS__, 'clear_cached_counts' ] ); |
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.
Done in 0675a76.
@pierlon @johnwatkins0 Thoughts?
@@ -2846,6 +2874,7 @@ public static function handle_validation_error_update( $redirect_to, $action, $t | |||
|
|||
if ( $updated_count ) { | |||
delete_transient( AMP_Validated_URL_Post_Type::NEW_VALIDATION_ERROR_URLS_COUNT_TRANSIENT ); | |||
delete_transient( self::TRANSIENT_KEY_ERROR_INDEX_COUNTS ); |
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.
This is not needed with adding a delete_amp_validation_error
action callback above.
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.
But looking for confirmation prior to merging.
@westonruter I see the same. |
So you endorse my additional changes? |
✅ Endorsed. I think we're still deleting the transient more than technically needed, but that's better than less than needed. |
Oh wait! Making a quick review. |
add_action( 'edit_' . self::TAXONOMY_SLUG, [ __CLASS__, 'clear_cached_counts' ] ); | ||
add_action( 'delete_' . self::TAXONOMY_SLUG, [ __CLASS__, 'clear_cached_counts' ] ); |
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.
Should these instead be edited_
and deleted_
? The edit_
and delete_
actions are run before the term is filtered, while the past tense counterpart is run after the term cache has been cleaned.
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.
Note that created_
is used above.
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.
I'm seeing that edit_{$taxonomy}
also happens after the edit has been made. It's docblock says:
Fires after a term in a specific taxonomy has been updated, but before the term cache has been cleaned.
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.
I don't see an action for deleted_{$taxonomy}
.
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.
Yes your right. Just was wondering why the use of created_{$taxonomy}
and not create_{$taxonomy}
.
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.
I suppose we could use created_{$taxonomy}
and edited_{$taxonomy}
instead?
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.
I see that saved_{$taxonomy}
is called when both inserting and editing, so that's an alternative.
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.
Oh wait that was introduced in WP 5.5, nevermind.
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.
See 4fe39ad, where edited_{$taxonomy}
is instead used.
$new_validation_error_urls = (int) $new_validation_error_urls; | ||
} | ||
|
||
$new_validation_error_urls = static::get_validation_error_urls_count(); |
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.
Should this instead be self::
(although the class is not marked as final
)?
$new_validation_error_urls = static::get_validation_error_urls_count(); | |
$new_validation_error_urls = self::get_validation_error_urls_count(); |
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.
Just wondering after seeing the alternating uses of static
and self
within the class.
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.
I think it's in part because we used to support PHP 5.2 in which static::
didn't exist.
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.
Ah OK, that can be attended to at a later time.
Co-authored-by: John Watkins <johnwatkins0@gmail.com> Co-authored-by: Pierre Gordon <pierregordon@protonmail.com>
Summary
Fixes #5271
This PR adds transient-based caching to the
get_validation_error_count
function. The transient data is an associative array with the JSON-encodedget_validation_error_count
used as the key. The transient data is deleted when terms are created, deleted, or updated.Additionally, this PR caches the validated URL count shown in the "At a Glance" admin dashboard widget using the transient used to show the count in the sidebar.
Checklist