Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add a filter to support modifying the post type used for an author's …

…published post count
  • Loading branch information...
commit cecf4bcfbb5d2f6fdac476266e10e4922b2da7fa 1 parent b3f46cd
@danielbachhuber danielbachhuber authored
View
6 co-authors-plus.php
@@ -556,7 +556,11 @@ public function update_author_term_post_count( $term ) {
if ( 'wpuser' == $coauthor->type )
$having_terms_and_authors .= $wpdb->prepare( " OR {$wpdb->posts}.post_author = %d", $coauthor->ID );
- $query .= " WHERE ({$having_terms_and_authors}) AND {$wpdb->posts}.post_type = 'post' AND {$wpdb->posts}.post_status = 'publish'";
+ $post_types = apply_filters( 'coauthors_count_published_post_types', array( 'post' ) );
+ $post_types = array_map( 'sanitize_key', $post_types );
+ $post_types = "'" . implode( "','", $post_types ) . "'";
+
+ $query .= " WHERE ({$having_terms_and_authors}) AND {$wpdb->posts}.post_type IN ({$post_types}) AND {$wpdb->posts}.post_status = 'publish'";
$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 );
View
31 tests/coauthorsplus-testcase.php
@@ -23,9 +23,40 @@ public function setUp() {
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => rand_str(),
+ 'post_type' => 'post',
);
$this->author1_post1 = wp_insert_post( $post );
+
+ $post = array(
+ 'post_author' => $this->author1,
+ 'post_status' => 'publish',
+ 'post_content' => rand_str(),
+ 'post_title' => rand_str(),
+ 'post_type' => 'post',
+ );
+
+ $this->author1_post2 = wp_insert_post( $post );
+
+ $page = array(
+ 'post_author' => $this->author1,
+ 'post_status' => 'publish',
+ 'post_content' => rand_str(),
+ 'post_title' => rand_str(),
+ 'post_type' => 'page',
+ );
+
+ $this->author1_page1 = wp_insert_post( $page );
+
+ $page = array(
+ 'post_author' => $this->author1,
+ 'post_status' => 'publish',
+ 'post_content' => rand_str(),
+ 'post_title' => rand_str(),
+ 'post_type' => 'page',
+ );
+
+ $this->author1_page2 = wp_insert_post( $page );
}
public function tearDown() {
View
50 tests/test-manage-coauthors.php
@@ -44,5 +44,55 @@ public function test_add_coauthor_updates_post_author() {
}
+ /**
+ * Post published count should default to 'post', but be filterable
+ *
+ * @see https://github.com/Automattic/Co-Authors-Plus/issues/170
+ */
+ public function test_post_publish_count_for_coauthor() {
+ global $coauthors_plus;
+
+ $editor1 = get_user_by( 'id', $this->editor1 );
+
+ /**
+ * Two published posts
+ */
+ $coauthors_plus->add_coauthors( $this->author1_post1, array( $editor1->user_login ) );
+ $coauthors_plus->add_coauthors( $this->author1_post2, array( $editor1->user_login ) );
+ $this->assertEquals( 2, count_user_posts( $editor1->ID ) );
+
+ /**
+ * One published page too, but no filter
+ */
+ $coauthors_plus->add_coauthors( $this->author1_page1, array( $editor1->user_login ) );
+ $this->assertEquals( 2, count_user_posts( $editor1->ID ) );
+
+ // Publish count to include posts and pages
+ $filter = function() {
+ return array( 'post', 'page' );
+ };
+ add_filter( 'coauthors_count_published_post_types', $filter );
+
+ /**
+ * Two published posts and pages
+ */
+ $coauthors_plus->add_coauthors( $this->author1_page2, array( $editor1->user_login ) );
+ $this->assertEquals( 4, count_user_posts( $editor1->ID ) );
+
+ // Publish count is just pages
+ remove_filter( 'coauthors_count_published_post_types', $filter );
+ $filter = function() {
+ return array( 'page' );
+ };
+ add_filter( 'coauthors_count_published_post_types', $filter );
+
+ /**
+ * Just one published page now for the editor
+ */
+ $author1 = get_user_by( 'id', $this->author1 );
+ $coauthors_plus->add_coauthors( $this->author1_page2, array( $author1->user_login ) );
+ $this->assertEquals( 1, count_user_posts( $editor1->ID ) );
+
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.