Skip to content

Warnings when using the filter ‘coauthors_supported_post_types’ to restrict the plugin functionalities to a unique custom post type #1113

@garubi

Description

@garubi

The context

  • WordPress 6.8.1 with one (or more) Custom Post Type added
  • Co-Authors-Plus 3.6.5 installed and activated
  • the filter coauthors_supported_post_types used to restrict the plugin functionalities to the Custom Post Type only:
add_filter( 'coauthors_supported_post_types', 'pandora_coauthors_supported_post_types' );
function pandora_coauthors_supported_post_types($post_types_with_authors){
	return array( 'sedi' );
}

The issue

In the error log we have warnings like this on every page view

[08-May-2025 14:44:58 UTC] PHP Warning:  Attempt to read property "cap" on null in C:\Users\Stefano Garuti\Local Sites\co-author-debug\app\public\wp-content\plugins\co-authors-plus\php\class-coauthors-plus.php on line 1594
[08-May-2025 14:44:58 UTC] PHP Warning:  Attempt to read property "edit_post" on null in C:\Users\Stefano Garuti\Local Sites\co-author-debug\app\public\wp-content\plugins\co-authors-plus\php\class-coauthors-plus.php on line 1594
[08-May-2025 14:44:58 UTC] PHP Warning:  Attempt to read property "cap" on null in C:\Users\Stefano Garuti\Local Sites\co-author-debug\app\public\wp-content\plugins\co-authors-plus\php\class-coauthors-plus.php on line 1595
[08-May-2025 14:44:58 UTC] PHP Warning:  Attempt to read property "edit_others_posts" on null in C:\Users\Stefano Garuti\Local Sites\co-author-debug\app\public\wp-content\plugins\co-authors-plus\php\class-coauthors-plus.php on line 1595
[08-May-2025 14:44:58 UTC] PHP Warning:  Attempt to read property "cap" on null in C:\Users\Stefano Garuti\Local Sites\co-author-debug\app\public\wp-content\plugins\co-authors-plus\php\class-coauthors-plus.php on line 1596
[08-May-2025 14:44:58 UTC] PHP Warning:  Attempt to read property "read_post" on null in C:\Users\Stefano Garuti\Local Sites\co-author-debug\app\public\wp-content\plugins\co-authors-plus\php\class-coauthors-plus.php on line 1596

Look at the issue live on WP Playground

I setup an instance of Playground with the mimunum to replicate the issue. You can check it here
(To see the debug log in playground open the browser's delevopers tools and look at the console)

Other informations

If you remove the filter the warnings go away.

The issue is there since CAP ver 3.6.0. (here is a Playground instance with CAP 3.5.15 where the issue is not present)

The errors come from the get_to_be_filtered_caps() function, because in some conditions get_post_type_object( $single ) returns NULL
The code assumes that the function get_post_type_object() returns an object, but it can also return NULL (see WordPress Developer Resources ).

To me looks like the filter_user_has_cap at line 1609 is called multiple times and in the first ones the custom posts types aren't registered yet.

Quck fix

To "silence" those warnings is sufficient to enclose those lines in an IF statement checking that the object isn't NULL

	public function get_to_be_filtered_caps(): array {
		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 ) {
				$obj = get_post_type_object( $single );
				if( $obj ){  // <------------   FIx the issue
					$this->to_be_filtered_caps[] = $obj->cap->edit_post;
					$this->to_be_filtered_caps[] = $obj->cap->edit_others_posts; // This as well: http://core.trac.wordpress.org/ticket/22417
					$this->to_be_filtered_caps[] = $obj->cap->read_post;
				} // <------------   FIx the issue
			}

			$this->to_be_filtered_caps = array_unique( $this->to_be_filtered_caps );
		}

		return $this->to_be_filtered_caps;
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions