From 2086458e8b496aa62c9728ddd98e1b5fe7900bea Mon Sep 17 00:00:00 2001 From: Philip John Date: Mon, 20 Mar 2017 13:35:29 +0000 Subject: [PATCH 01/11] Introduce helper function for grabbing cached list of coauthor terms. --- co-authors-plus.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/co-authors-plus.php b/co-authors-plus.php index ad68feb4..4535623c 100644 --- a/co-authors-plus.php +++ b/co-authors-plus.php @@ -1624,3 +1624,37 @@ 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 = false ) { + + if ( ! $post_id ) { + return array(); + } + + global $coauthors_plus; + + $cache_key = 'coauthors_post_' . $post_id; + $coauthor_terms = wp_cache_get( $cache_key, 'co-authors-plus' ) + + if ( false === $coauthor_terms ) { + $coauthor_terms = wp_get_object_terms( $post_id, $coauthors_plus->coauthor_taxonomy, array( + 'orderby' => 'term_order', + 'order' => 'ASC', + ) ); + wp_cache_set( $cache_key, $coauthor_terms, 'co-authors-plus' ); + } else { + $coauthor_terms = array(); + } + + return $coauthor_terms; + +} From 4aea6b153941b34da5e9eb848283ffb0885288d2 Mon Sep 17 00:00:00 2001 From: Philip John Date: Mon, 20 Mar 2017 13:35:55 +0000 Subject: [PATCH 02/11] Use the new helper function for grabbing cached co-author terms lists. --- php/class-wp-cli.php | 19 +++++++++---------- template-tags.php | 6 +----- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/php/class-wp-cli.php b/php/class-wp-cli.php index 9b334317..08706e80 100644 --- a/php/class-wp-cli.php +++ b/php/class-wp-cli.php @@ -76,10 +76,9 @@ public function create_terms_for_posts() { $count++; - $terms = get_the_terms( $single_post->ID, $coauthors_plus->coauthor_taxonomy ); - - if ( is_wp_error( $terms ) ) { - WP_CLI::error( $terms->get_error_message() ); + $terms = cap_get_coauthor_terms_for_post( $single_post->ID ); + if ( empty( $terms ) ) { + WP_CLI::error( sprintf( 'No co-authors found for post #%d.', $single_post->ID ) ); } if ( ! empty( $terms ) ) { @@ -236,7 +235,7 @@ public function assign_user_to_coauthor( $args, $assoc_args ) { $posts = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author=%d AND post_type IN ('$post_types')", $user->ID ) ); $affected = 0; foreach ( $posts as $post_id ) { - if ( $coauthors = get_the_terms( $post_id, $coauthors_plus->coauthor_taxonomy ) ) { + if ( $coauthors = cap_get_coauthor_terms_for_post( $post_id ) ) { WP_CLI::line( sprintf( __( 'Skipping - Post #%d already has co-authors assigned: %s', 'co-authors-plus' ), $post_id, implode( ', ', wp_list_pluck( $coauthors, 'slug' ) ) ) ); continue; } @@ -545,8 +544,8 @@ public function list_posts_without_terms( $args, $assoc_args ) { while ( $posts->post_count ) { foreach ( $posts->posts as $single_post ) { - - $terms = get_the_terms( $single_post->ID, $coauthors_plus->coauthor_taxonomy ); + + $terms = cap_get_coauthor_terms_for_post( $single_post->ID ); if ( empty( $terms ) ) { $saved = array( $single_post->ID, @@ -698,9 +697,9 @@ public function remove_terms_from_revisions() { WP_CLI::line( 'Found ' . count( $ids ) . ' revisions to look through' ); $affected = 0; foreach ( $ids as $post_id ) { - - $terms = get_the_terms( $post_id, $coauthors_plus->coauthor_taxonomy ); - if ( ! $terms ) { + + $terms = cap_get_coauthor_terms_for_post( $post_id ); + if ( empty( $terms ) ) { continue; } diff --git a/template-tags.php b/template-tags.php index a5cd99ab..ffc398ae 100644 --- a/template-tags.php +++ b/template-tags.php @@ -14,11 +14,7 @@ function get_coauthors( $post_id = 0 ) { } if ( $post_id ) { - $cache_key = 'coauthors_post_' . $post_id; - if ( false === ( $coauthor_terms = wp_cache_get( $cache_key, 'co-authors-plus' ) ) ) { - $coauthor_terms = wp_get_object_terms( $post_id, $coauthors_plus->coauthor_taxonomy, array( 'orderby' => 'term_order', 'order' => 'ASC' ) ); - wp_cache_set( $cache_key, $coauthor_terms, 'co-authors-plus' ); - } + $coauthor_terms = cap_get_coauthor_terms_for_post( $post_id ); if ( is_array( $coauthor_terms ) && ! empty( $coauthor_terms ) ) { foreach ( $coauthor_terms as $coauthor ) { $coauthor_slug = preg_replace( '#^cap\-#', '', $coauthor->slug ); From 0a640ad7d4782b2daa554afe1c05d4895180078e Mon Sep 17 00:00:00 2001 From: Philip John Date: Mon, 20 Mar 2017 13:44:49 +0000 Subject: [PATCH 03/11] Explicitly check for a non-empty array and tidy up the code a little. --- php/class-wp-cli.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/php/class-wp-cli.php b/php/class-wp-cli.php index 08706e80..7e96d10d 100644 --- a/php/class-wp-cli.php +++ b/php/class-wp-cli.php @@ -235,8 +235,13 @@ public function assign_user_to_coauthor( $args, $assoc_args ) { $posts = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author=%d AND post_type IN ('$post_types')", $user->ID ) ); $affected = 0; foreach ( $posts as $post_id ) { - if ( $coauthors = cap_get_coauthor_terms_for_post( $post_id ) ) { - WP_CLI::line( sprintf( __( 'Skipping - Post #%d already has co-authors assigned: %s', 'co-authors-plus' ), $post_id, implode( ', ', wp_list_pluck( $coauthors, 'slug' ) ) ) ); + $coauthors = cap_get_coauthor_terms_for_post( $post_id ) + if ( ! empty( $coauthors ) ) { + WP_CLI::line( sprintf( + __( 'Skipping - Post #%d already has co-authors assigned: %s', 'co-authors-plus' ), + $post_id, + implode( ', ', wp_list_pluck( $coauthors, 'slug' ) ) + ) ); continue; } From 81017509869c98bf95d1e8aba95e272e26dcb6df Mon Sep 17 00:00:00 2001 From: Philip John Date: Mon, 20 Mar 2017 13:50:01 +0000 Subject: [PATCH 04/11] Must. Remember. Semicolons. --- co-authors-plus.php | 2 +- php/class-wp-cli.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/co-authors-plus.php b/co-authors-plus.php index 4535623c..0ac73e12 100644 --- a/co-authors-plus.php +++ b/co-authors-plus.php @@ -1643,7 +1643,7 @@ function cap_get_coauthor_terms_for_post( $post_id = false ) { global $coauthors_plus; $cache_key = 'coauthors_post_' . $post_id; - $coauthor_terms = wp_cache_get( $cache_key, 'co-authors-plus' ) + $coauthor_terms = wp_cache_get( $cache_key, 'co-authors-plus' ); if ( false === $coauthor_terms ) { $coauthor_terms = wp_get_object_terms( $post_id, $coauthors_plus->coauthor_taxonomy, array( diff --git a/php/class-wp-cli.php b/php/class-wp-cli.php index 7e96d10d..568f988e 100644 --- a/php/class-wp-cli.php +++ b/php/class-wp-cli.php @@ -235,7 +235,7 @@ public function assign_user_to_coauthor( $args, $assoc_args ) { $posts = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author=%d AND post_type IN ('$post_types')", $user->ID ) ); $affected = 0; foreach ( $posts as $post_id ) { - $coauthors = cap_get_coauthor_terms_for_post( $post_id ) + $coauthors = cap_get_coauthor_terms_for_post( $post_id ); if ( ! empty( $coauthors ) ) { WP_CLI::line( sprintf( __( 'Skipping - Post #%d already has co-authors assigned: %s', 'co-authors-plus' ), From d5305b5a941f89fe4aaa60b596c02512c8c18563 Mon Sep 17 00:00:00 2001 From: Philip John Date: Mon, 20 Mar 2017 14:35:49 +0000 Subject: [PATCH 05/11] Bust the coauthors terms order cache when the objects terms are updated too. --- co-authors-plus.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/co-authors-plus.php b/co-authors-plus.php index 0ac73e12..4a7aeaa0 100644 --- a/co-authors-plus.php +++ b/co-authors-plus.php @@ -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 ); } /** @@ -1481,6 +1482,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; From d58d3ae664e2175fa7371fdbc8cf5c7df54200ea Mon Sep 17 00:00:00 2001 From: Philip John Date: Tue, 21 Mar 2017 08:05:29 +0000 Subject: [PATCH 06/11] Cache empty arrays when wp_get_object_terms() returns a WP_Error --- co-authors-plus.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/co-authors-plus.php b/co-authors-plus.php index 4a7aeaa0..5db8b313 100644 --- a/co-authors-plus.php +++ b/co-authors-plus.php @@ -1652,7 +1652,7 @@ function cap_filter_comment_moderation_email_recipients( $recipients, $comment_i * @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 ) { +function cap_get_coauthor_terms_for_post( $post_id ) { if ( ! $post_id ) { return array(); @@ -1664,13 +1664,13 @@ function cap_get_coauthor_terms_for_post( $post_id = false ) { $coauthor_terms = wp_cache_get( $cache_key, 'co-authors-plus' ); if ( false === $coauthor_terms ) { - $coauthor_terms = wp_get_object_terms( $post_id, $coauthors_plus->coauthor_taxonomy, array( + $cached = wp_get_object_terms( $post_id, $coauthors_plus->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' ); - } else { - $coauthor_terms = array(); } return $coauthor_terms; From b1fe8c2d4e0fbdcd3373ef6d3747b74b4fa4faf8 Mon Sep 17 00:00:00 2001 From: Philip John Date: Tue, 21 Mar 2017 08:27:32 +0000 Subject: [PATCH 07/11] Move cap_get_coauthor_terms_for_post() logic into the CoAuthors_Plus class and make the helper dumb --- co-authors-plus.php | 54 +++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/co-authors-plus.php b/co-authors-plus.php index 5db8b313..89186ab8 100644 --- a/co-authors-plus.php +++ b/co-authors-plus.php @@ -1474,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' ); + } + + return $coauthor_terms; + + } + /** * Callback to clear the cache on post save and post delete. * @@ -1653,26 +1685,6 @@ function cap_filter_comment_moderation_email_recipients( $recipients, $comment_i * @return array Array of coauthor WP_Term objects */ function cap_get_coauthor_terms_for_post( $post_id ) { - - if ( ! $post_id ) { - return array(); - } - global $coauthors_plus; - - $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, $coauthors_plus->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' ); - } - - return $coauthor_terms; - + return $coauthors_plus->get_coauthor_terms_for_post( $post_id ); } From 934a2615e329514504b97768f8a55d9cbdd484c8 Mon Sep 17 00:00:00 2001 From: Kailey Lampert Date: Thu, 23 Mar 2017 09:20:29 -0700 Subject: [PATCH 08/11] Don't create test tables as temporary In Test_Author_Queried_Object, tests will fail when, by default, tables for subsite are created as temporary. --- tests/test-author-queried-object.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test-author-queried-object.php b/tests/test-author-queried-object.php index 94e357f8..25689cf5 100644 --- a/tests/test-author-queried-object.php +++ b/tests/test-author-queried-object.php @@ -5,6 +5,20 @@ class Test_Author_Queried_Object extends CoAuthorsPlus_TestCase { + /** + * Set up for test + * + * Don't create tables as 'temporary'. + * + * @see https://github.com/Automattic/Co-Authors-Plus/issues/398 + */ + function setUp() { + parent::setUp(); + + remove_filter( 'query', array( $this, '_create_temporary_tables' ) ); + remove_filter( 'query', array( $this, '_drop_temporary_tables' ) ); + } + /** * On author pages, the queried object should only be set * to a user that's not a member of the blog if they From fe48900551c20bb6a2b41412e756d55d7f65624f Mon Sep 17 00:00:00 2001 From: Mohammad Jangda Date: Tue, 28 Mar 2017 11:46:03 -0400 Subject: [PATCH 09/11] Don't cache when get_object_terms fails It can lead to unexpected bugs and make it not clear why we cached an empty array (and therefore harder to debug). --- co-authors-plus.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/co-authors-plus.php b/co-authors-plus.php index 89186ab8..66b18e0c 100644 --- a/co-authors-plus.php +++ b/co-authors-plus.php @@ -1493,12 +1493,16 @@ public function get_coauthor_terms_for_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( + $coauthor_terms = 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; + + // This usually happens if the taxonomy doesn't exist, which should never happen, but you never know. + if ( is_wp_error( $coauthor_terms ) ) { + return array(); + } + wp_cache_set( $cache_key, $coauthor_terms, 'co-authors-plus' ); } From 3248dfcd10941a7cec2b7e0acffd90c4e16a20b7 Mon Sep 17 00:00:00 2001 From: Mohammad Jangda Date: Tue, 28 Mar 2017 13:59:59 -0400 Subject: [PATCH 10/11] Update travis config to handle phpunit + php7 https://core.trac.wordpress.org/changeset/40255 --- .travis.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.travis.yml b/.travis.yml index fea742a8..38be5006 100644 --- a/.travis.yml +++ b/.travis.yml @@ -48,6 +48,15 @@ before_script: - if [[ "$SNIFF" == "1" ]]; then $PHPCS_DIR/scripts/phpcs --config-set installed_paths $SNIFFS_DIR; fi # After CodeSniffer install you should refresh your path. - if [[ "$SNIFF" == "1" ]]; then phpenv rehash; fi + # Properly handle PHPunit versions + - export PATH="$HOME/.composer/vendor/bin:$PATH" + - | + if [[ ${TRAVIS_PHP_VERSION:0:2} == "7." ]]; then + composer global require "phpunit/phpunit=5.7.*" + elif [[ ${TRAVIS_PHP_VERSION:0:3} != "5.2" ]]; then + composer global require "phpunit/phpunit=4.8.*" + fi + - phpunit --version script: # Search for PHP syntax errors. From f53d8f767b0b3ceece487f2c0d6921c06ecb918e Mon Sep 17 00:00:00 2001 From: Mohammad Jangda Date: Tue, 28 Mar 2017 14:00:41 -0400 Subject: [PATCH 11/11] Update Travis environment matrix No need to test PHP nightly for now; 7 + 7.1 are good for now. Drop WordPress 4.5. --- .travis.yml | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/.travis.yml b/.travis.yml index 38be5006..28765d3b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,35 +1,30 @@ language: php php: - - "nightly" + - 7.1 env: - WP_VERSION=latest +matrix: matrix: include: -# nightly+latest already included above as first build. -# - php: "nightly" -# env: WP_VERSION=latest - - php: "nightly" - env: WP_VERSION=4.6 - - php: "nightly" - env: WP_VERSION=4.5 - php: "5.2" env: WP_VERSION=latest - php: "5.2" env: WP_VERSION=4.6 - - php: "5.2" - env: WP_VERSION=4.5 - - php: "5.6" - env: WP_VERSION=latest - php: "5.6" env: - - WP_VERSION=4.6 + - WP_VERSION=latest - SNIFF=1 - php: "5.6" - env: WP_VERSION=4.5 - - php: "7" + env: WP_VERSION=4.6 + - php: "7.0" + env: WP_VERSION=latest + - php: "7.0" + env: WP_VERSION=4.6 + # 7.1 / latest already included above as first build. + - php: "7.1" env: WP_VERSION=4.6 before_script: