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

Add supported_post_types() method #1006

Merged
merged 2 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion php/class-coauthors-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function _build_authors_response( &$response, $request ) {
*/
public function modify_responses() {

$post_types = $this->coauthors->supported_post_types;
$post_types = $this->coauthors->supported_post_types();

if ( empty( $post_types ) || ! is_array( $post_types ) ) {
return;
Expand Down
56 changes: 44 additions & 12 deletions php/class-coauthors-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,7 @@ public function action_init_late() {
add_action( 'edited_term_taxonomy', array( $this, 'action_edited_term_taxonomy_flush_cache' ), 10, 2 );
}

$post_types_with_authors = array_values( get_post_types() );
foreach ( $post_types_with_authors as $key => $name ) {
if ( ! post_type_supports( $name, $this->coauthor_taxonomy ) || in_array( $name, array( 'revision', 'attachment' ) ) ) {
unset( $post_types_with_authors[ $key ] );
}
}
$this->supported_post_types = apply_filters( 'coauthors_supported_post_types', $post_types_with_authors );
register_taxonomy( $this->coauthor_taxonomy, $this->supported_post_types, $args );
register_taxonomy( $this->coauthor_taxonomy, $this->supported_post_types(), $args );
}

/**
Expand Down Expand Up @@ -246,6 +239,45 @@ public function admin_init() {
add_action( 'load-edit.php', array( $this, 'load_edit' ) );
}

/**
* Get the list of supported post types.
*
* By default, this is the built-in and custom post types that have authors.
*
* @since 3.5.16
*
* @return array Supported post types.
*/
public function supported_post_types() {
$post_types = array_values( get_post_types() );

$excluded_built_in = array(
'revision',
'attachment',
'customize_changeset',
'wp_template',
'wp_template_part',
);

foreach ( $post_types as $key => $name ) {
if ( ! post_type_supports( $name, 'author' ) || in_array( $name, $excluded_built_in, true ) ) {
unset( $post_types[ $key ] );
}
}

/**
* Filter the list of supported post types.
*
* @param array $post_types Post types.
*/
$supported_post_types = (array) apply_filters( 'coauthors_supported_post_types', $post_types );

// Set class property for back-compat.
$this->supported_post_types = $supported_post_types;

return $supported_post_types;
}

/**
* Check whether the guest authors functionality is enabled or not
* Guest authors can be disabled entirely with:
Expand Down Expand Up @@ -334,7 +366,7 @@ public function is_post_type_enabled( $post_type = null ) {
}
}

return in_array( $post_type, $this->supported_post_types );
return in_array( $post_type, $this->supported_post_types() );
}

/**
Expand Down Expand Up @@ -1371,7 +1403,7 @@ public function enqueue_scripts( $hook_suffix ) {
public function load_edit() {

$screen = get_current_screen();
if ( in_array( $screen->post_type, $this->supported_post_types ) ) {
if ( in_array( $screen->post_type, $this->supported_post_types() ) ) {
add_filter( 'views_' . $screen->id, array( $this, 'filter_views' ) );
}
}
Expand Down Expand Up @@ -1457,11 +1489,11 @@ public function is_valid_page() {
* @return array caps that CAP should filter
*/
public function get_to_be_filtered_caps() {
if ( ! empty( $this->supported_post_types ) && empty( $this->to_be_filtered_caps ) ) {
if ( ! empty( $this->supported_post_types() ) && empty( $this->to_be_filtered_caps ) ) {
$this->to_be_filtered_caps[] = 'edit_post'; // Need to filter this too, unfortunately: http://core.trac.wordpress.org/ticket/22415
$this->to_be_filtered_caps[] = 'read_post';

foreach ( $this->supported_post_types as $single ) {
foreach ( $this->supported_post_types() as $single ) {
$obj = get_post_type_object( $single );

$this->to_be_filtered_caps[] = $obj->cap->edit_post;
Expand Down
4 changes: 2 additions & 2 deletions php/class-wp-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function create_terms_for_posts() {
$args = array(
'order' => 'ASC',
'orderby' => 'ID',
'post_type' => $coauthors_plus->supported_post_types,
'post_type' => $coauthors_plus->supported_post_types(),
'posts_per_page' => 100,
'paged' => 1,
'update_meta_cache' => false,
Expand Down Expand Up @@ -235,7 +235,7 @@ public function assign_user_to_coauthor( $args, $assoc_args ) {
WP_CLI::error( __( 'Please specify a valid co-author login', 'co-authors-plus' ) );
}

$post_types = implode( "','", $coauthors_plus->supported_post_types );
$post_types = implode( "','", $coauthors_plus->supported_post_types() );
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$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;
Expand Down
53 changes: 53 additions & 0 deletions tests/Integration/CoAuthorsPlusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -732,4 +732,57 @@ public function test_add_coauthors_box() {
$this->assertNull( $wp_meta_boxes, 'Failed to assert the coauthors metabox is not added when the block editor is loaded.' );

}

/**
* Test the expected default supported post types.
*/
public function test_default_supported_post_types() {
$supported_post_types = (new \CoAuthors_Plus())->supported_post_types();
$expected = array(
'post',
'page',
);
$this->assertEquals( array_values( $expected ), array_values( $supported_post_types ) );
}

/**
* Test whether the supported post types can be filtered.
*/
public function test_can_filter_supported_post_types() {
// This should be detected.
$post_type_with_author = register_post_type(
'foo',
array(
'supports' => array( 'author' ),
)
);

// This doesn't support the author, so should not be detected.
$post_type_without_author = register_post_type(
'bar',
array(
'supports' => array( 'title' ),
)
);

$callback = function( $post_types ) {
$key = array_search( 'page', $post_types, true );
unset( $post_types[ $key ] );

return $post_types;
};
add_filter( 'coauthors_supported_post_types', $callback );

$supported_post_types = ( new \CoAuthors_Plus() )->supported_post_types();

$expected = array(
'post',
'foo',
);
$this->assertEquals( array_values( $expected ), array_values( $supported_post_types ) );

// Clean up.
remove_filter( 'coauthors_supported_post_types', $callback );
unregister_post_type( 'foo' );
}
}
2 changes: 1 addition & 1 deletion tests/Integration/EndpointsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public function test_remove_author_link() {
public function test_modify_response() {
$this->_api->modify_responses();

foreach ( $this->_cap->supported_post_types as $post_type ) {
foreach ( $this->_cap->supported_post_types() as $post_type ) {
$this->assertEquals(
10,
has_filter(
Expand Down