-
Notifications
You must be signed in to change notification settings - Fork 206
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
More term ordering fixes #406
Changes from 8 commits
2086458
4aea6b1
0a640ad
8101750
d5305b5
d58d3ae
b1fe8c2
934a261
fe48900
3248dfc
f53d8f7
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 |
---|---|---|
|
@@ -119,6 +119,7 @@ function __construct() { | |
// Delete CoAuthor Cache on Post Save & Post Delete | ||
add_action( 'save_post', array( $this, 'clear_cache') ); | ||
add_action( 'delete_post', array( $this, 'clear_cache') ); | ||
add_action( 'set_object_terms', array( $this, 'clear_cache_on_terms_set' ), 10, 6 ); | ||
} | ||
|
||
/** | ||
|
@@ -1473,6 +1474,38 @@ public function filter_jetpack_open_graph_tags( $og_tags, $image_dimensions ) { | |
return apply_filters( 'coauthors_open_graph_tags', $og_tags ); | ||
} | ||
|
||
/** | ||
* Retrieve a list of coauthor terms for a single post. | ||
* | ||
* Grabs a correctly ordered list of authors for a single post, appropriately | ||
* cached because it requires `wp_get_object_terms()` to succeed. | ||
* | ||
* @param int $post_id ID of the post for which to retrieve authors. | ||
* @return array Array of coauthor WP_Term objects | ||
*/ | ||
public function get_coauthor_terms_for_post( $post_id ) { | ||
|
||
if ( ! $post_id ) { | ||
return array(); | ||
} | ||
|
||
$cache_key = 'coauthors_post_' . $post_id; | ||
$coauthor_terms = wp_cache_get( $cache_key, 'co-authors-plus' ); | ||
|
||
if ( false === $coauthor_terms ) { | ||
$cached = wp_get_object_terms( $post_id, $this->coauthor_taxonomy, array( | ||
'orderby' => 'term_order', | ||
'order' => 'ASC', | ||
) ); | ||
// Cache an empty array if the taxonomy doesn't exist. | ||
$coauthor_terms = ( is_wp_error( $cached ) ) ? array() : $cached; | ||
wp_cache_set( $cache_key, $coauthor_terms, 'co-authors-plus' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should be caching when there is an error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in fe48900 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @philipjohn let me know if that changeset makes sense to you and we can merge. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My worry about not caching errors is that a simple typo could easily result in |
||
} | ||
|
||
return $coauthor_terms; | ||
|
||
} | ||
|
||
/** | ||
* Callback to clear the cache on post save and post delete. | ||
* | ||
|
@@ -1481,6 +1514,23 @@ public function filter_jetpack_open_graph_tags( $og_tags, $image_dimensions ) { | |
public function clear_cache( $post_id ) { | ||
wp_cache_delete( 'coauthors_post_' . $post_id, 'co-authors-plus' ); | ||
} | ||
|
||
/** | ||
* Callback to clear the cache when an object's terms are changed. | ||
* | ||
* @param $post_id The Post ID. | ||
*/ | ||
public function clear_cache_on_terms_set( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) { | ||
|
||
// We only care about the coauthors taxonomy | ||
if ( $this->coauthor_taxonomy !== $taxonomy ) { | ||
return; | ||
} | ||
|
||
wp_cache_delete( 'coauthors_post_' . $object_id, 'co-authors-plus' ); | ||
|
||
} | ||
|
||
} | ||
|
||
global $coauthors_plus; | ||
|
@@ -1624,3 +1674,17 @@ function cap_filter_comment_moderation_email_recipients( $recipients, $comment_i | |
} | ||
return $recipients; | ||
} | ||
|
||
/** | ||
* Retrieve a list of coauthor terms for a single post. | ||
* | ||
* Grabs a correctly ordered list of authors for a single post, appropriately | ||
* cached because it requires `wp_get_object_terms()` to succeed. | ||
* | ||
* @param int $post_id ID of the post for which to retrieve authors. | ||
* @return array Array of coauthor WP_Term objects | ||
*/ | ||
function cap_get_coauthor_terms_for_post( $post_id ) { | ||
global $coauthors_plus; | ||
return $coauthors_plus->get_coauthor_terms_for_post( $post_id ); | ||
} |
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.
Minor:
$cached
is not quite the best name here. We should probably swap the var names$coauthor_terms
and$cached
.