Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix the callback function used to update the published post count for…

… authors and co-authors.

Plus, include a wp-cli command for updating this on your site

Closes #68
  • Loading branch information...
commit 904ca11a384dde1159f5e8e1d184a086ce67f55a 1 parent 56a4661
@danielbachhuber danielbachhuber authored
Showing with 45 additions and 36 deletions.
  1. +27 −36 co-authors-plus.php
  2. +18 −0 php/class-wp-cli.php
View
63 co-authors-plus.php
@@ -473,55 +473,46 @@ function _filter_manage_users_custom_column( $value, $column_name, $user_id ) {
/**
* When we update the terms at all, we should update the published post count for each author
*/
- function _update_users_posts_count( $terms, $taxonomy ) {
+ function _update_users_posts_count( $tt_ids, $taxonomy ) {
global $wpdb;
- $object_types = (array) $taxonomy->object_type;
+ $tt_ids = implode( ', ', array_map( 'intval', $tt_ids ) );
+ $term_ids = $wpdb->get_results( "SELECT term_id FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)" );
- foreach ( $object_types as &$object_type ) {
- list( $object_type ) = explode( ':', $object_type );
+ foreach( (array)$term_ids as $term_id_result ) {
+ $term = get_term_by( 'id', $term_id_result->term_id, $this->coauthor_taxonomy );
+ $this->update_author_term_post_count( $term );
}
+ $tt_ids = explode( ', ', $tt_ids );
+ clean_term_cache( $tt_ids, '', false );
- if ( $object_types )
- $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );
+ }
- $object_types = array_unique( $object_types );
+ /**
+ * Update the post count associated with an author term
+ */
+ public function update_author_term_post_count( $term ) {
+ global $wpdb;
- foreach( (array)$terms as $term_taxonomy_id ) {
- $count = 0;
- if ( 0 == $term_taxonomy_id )
- continue;
- // Get the post IDs for all published posts with this co-author
- $query = $wpdb->prepare( "SELECT $wpdb->posts.ID FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type IN ('" . implode("', '", $object_types ) . "') AND term_taxonomy_id = %d", $term_taxonomy_id );
- $all_coauthor_posts = $wpdb->get_results( $query );
+ $coauthor = $this->get_coauthor_by( 'user_nicename', $term->slug );
+ if ( ! $coauthor )
+ return new WP_Error( 'missing-coauthor', __( 'No co-author exists for that term', 'co-authors-plus' ) );
- // Find the term_id from the term_taxonomy_id, and then get the user's user_login from that
- $query = $wpdb->prepare( "SELECT $wpdb->terms.slug FROM $wpdb->term_taxonomy INNER JOIN $wpdb->terms ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id WHERE $wpdb->term_taxonomy.term_taxonomy_id = %d", $term_taxonomy_id );
- $term_slug = $wpdb->get_var( $query );
- $author = get_user_by( 'slug', $term_slug );
+ $query = "SELECT COUNT({$wpdb->posts}.ID) FROM {$wpdb->posts}";
- // If there's no author object, then we're probably editing a coauthor w/o a user
- if ( empty( $author ) )
- continue;
+ $query .= " LEFT JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
+ $query .= " LEFT JOIN {$wpdb->term_taxonomy} ON ( {$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id )";
- // Get all of the post IDs where the user is the primary author
- $query = $wpdb->prepare( "SELECT $wpdb->posts.ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ('" . implode("', '", $object_types ) . "') AND post_author = %d;", $author->ID );
- $all_author_posts = $wpdb->get_results( $query );
+ $having_terms_and_authors = $having_terms = $wpdb->prepare( "{$wpdb->term_taxonomy}.term_id = %d", $term->term_id );
+ if ( 'wpuser' == $coauthor->type )
+ $having_terms_and_authors .= $wpdb->prepare( " OR {$wpdb->posts}.post_author = %d", $coauthor->ID );
- // Dedupe the post IDs and then provide a final count
- $all_posts = array();
- foreach( $all_coauthor_posts as $coauthor_post ) {
- $all_posts[] = $coauthor_post->ID;
- }
- foreach( $all_author_posts as $author_post ) {
- $all_posts[] = $author_post->ID;
- }
- $count = count( array_unique( $all_posts ) );
+ $query .= " WHERE ({$having_terms_and_authors}) AND {$wpdb->posts}.post_type = 'post' AND {$wpdb->posts}.post_status = 'publish'";
- // Save the count to the term's count column
- $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term_taxonomy_id ) );
- }
+ $query .= $wpdb->prepare( " GROUP BY {$wpdb->posts}.ID HAVING MAX( IF( {$wpdb->term_taxonomy}.taxonomy = '%s', IF( {$having_terms},2,1 ),0 ) ) <> 1 ", $this->coauthor_taxonomy );
+ $count = $wpdb->query( $query );
+ $wpdb->update( $wpdb->term_taxonomy, array( 'count' => $count ), array( 'term_taxonomy_id' => $term->term_taxonomy_id ) );
}
/**
View
18 php/class-wp-cli.php
@@ -29,6 +29,7 @@ public static function help() {
--new_term= Term to reassign to. Create a term if one doesn't exist
list_posts_without_terms List all posts without Co-Authors Plus terms
migrate_author_terms Migrate author terms without prefixes to ones with prefixes
+ update_author_post_counts Update the published post count for all coauthors
EOB
);
}
@@ -316,6 +317,23 @@ public function migrate_author_terms( $args, $assoc_args ) {
}
/**
+ * Update the term's count field for all author terms
+ */
+ public function update_author_post_counts() {
+ global $coauthors_plus;
+ $author_terms = get_terms( $coauthors_plus->coauthor_taxonomy, array( 'hide_empty' => false ) );
+ WP_CLI::line( "Now updating " . count( $author_terms ) . " terms" );
+ foreach( $author_terms as $author_term ) {
+ $old_count = $author_term->count;
+ $coauthors_plus->update_author_term_post_count( $author_term );
+ wp_cache_delete( $author_term->term_id, $coauthors_plus->coauthor_taxonomy );
+ $new_count = get_term_by( 'id', $author_term->term_id, $coauthors_plus->coauthor_taxonomy )->count;
+ WP_CLI::line( "Term {$author_term->slug} ({$author_term->term_id}) changed from {$old_count} to {$new_count}" );
+ }
+ WP_CLI::success( "All done" );
+ }
+
+ /**
* Clear all of the caches for memory management
*/
private function stop_the_insanity() {
Please sign in to comment.
Something went wrong with that request. Please try again.