Skip to content
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

Merged
merged 11 commits into from
Apr 3, 2017
Merged

More term ordering fixes #406

merged 11 commits into from
Apr 3, 2017

Conversation

philipjohn
Copy link
Contributor

Following on from #390 we also needed to revert the change introduce in #367 which meant that the CLI commands would also be affected by the bug.

This change moves the fix from #391 into it's own helper function - cap_get_coauthor_terms_for_post() to avoid unnecessary code duplication for the cache getting/setting. It updates the existing uses of the cache to use the helper, and fixes the CLI commands' use of get_the_terms() to use our new helper instead.

@philipjohn philipjohn requested a review from mjangda March 20, 2017 13:42
Copy link
Member

@mjangda mjangda left a comment

Choose a reason for hiding this comment

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

Added a few notes on improving the code. The main blocking issue is the WP_Error.

It would also be nice to have tests for the new function.

) );
wp_cache_set( $cache_key, $coauthor_terms, 'co-authors-plus' );
} else {
$coauthor_terms = array();
Copy link
Member

Choose a reason for hiding this comment

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

We should remove the else and just do the variable init + assignment before the if

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, I considered that and got lazy.

* @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 = false ) {
Copy link
Member

Choose a reason for hiding this comment

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

Why do we have a default of false if we just return early in that case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just like being explicit :) but I'll remove it.

'orderby' => 'term_order',
'order' => 'ASC',
) );
wp_cache_set( $cache_key, $coauthor_terms, 'co-authors-plus' );
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to catch (and not cache) when a WP_Error is returned by wp_get_object_terms as that could lead to some "interesting" bugs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I guess we missed this in the original fix PR too. We'll get a WP_Error when the taxonomy doesn't exist, so I'll cache an empty array in this case. Otherwise, we could end up with lots of wp_get_object_terms() and the resultant unpleasant queries.

$coauthor_terms = array();
}

return $coauthor_terms;
Copy link
Member

Choose a reason for hiding this comment

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

Thoughts on moving the logic for this function inside the coauthors_plus class (and this function can just call that)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I did think about putting this into the class but thought it silly to force everyone to do global $coauthors just to make a simple function call. Your idea solves that though, so I'll do that!

@philipjohn
Copy link
Contributor Author

Okay, all good. The failing test is still just that one from the earlier JOIN change.

@trepmal
Copy link
Contributor

trepmal commented Mar 22, 2017

@trepmal
Copy link
Contributor

trepmal commented Mar 22, 2017

Failing test comments: #398 (comment)

In Test_Author_Queried_Object, tests will fail when, by default, tables for subsite are created as temporary.
@trepmal
Copy link
Contributor

trepmal commented Mar 23, 2017

934a261 updates the test to prevent subsite tables from being created as temporary, thus allowing the test to pass.

ping @mjangda, @sboisvert, @dartiss

related: #398, #404

@philipjohn
Copy link
Contributor Author

Thanks @trepmal !

Looks like those remaining issues are thanks to this core bug. Given that, are we safe to merge this @mjangda ?

Copy link
Member

@mjangda mjangda left a comment

Choose a reason for hiding this comment

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

Looking good. I think the big thing here is that we should skip caching when wp_get_object_terms returns an error.

) );
// 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' );
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

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

Addressed in fe48900

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 wp_get_object_terms() triggering a lot more than we'd like, and the uncached queries that comes with it.

$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(
Copy link
Member

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.

 + $cache_coauthors = wp_cache_get( $cache_key, 'co-authors-plus' );
 +
 +		if ( false === $coauthor_terms ) {
 +			$coauthor_terms = wp_get_object_terms( $post_id, $this->coauthor_taxonomy, array(
...

It can lead to unexpected bugs and make it not clear why we cached an
empty array (and therefore harder to debug).
@mjangda mjangda dismissed their stale review March 28, 2017 15:47

I fixed the feedback.

@mjangda
Copy link
Member

mjangda commented Mar 28, 2017

Remaining test failures are because the version of Travis running for CAP doesn't have PHP5.2 and PHPCS errors.

@philipjohn philipjohn merged commit ed38119 into master Apr 3, 2017
@philipjohn philipjohn deleted the fix/get_the_terms-ordering branch April 3, 2017 15:16
rebeccahum pushed a commit that referenced this pull request Mar 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants