From 4e5785e491e20c4e4208f2a599d00b03347c704b Mon Sep 17 00:00:00 2001 From: Justin Maurer Date: Wed, 24 Apr 2024 11:28:46 -0500 Subject: [PATCH 1/3] Initialize $supported_post_types once and then refer to that class property for all supported_post_types() calls. --- php/class-coauthors-plus.php | 113 +++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 53 deletions(-) diff --git a/php/class-coauthors-plus.php b/php/class-coauthors-plus.php index fb7df6ce..a8b13d8b 100644 --- a/php/class-coauthors-plus.php +++ b/php/class-coauthors-plus.php @@ -214,6 +214,7 @@ public function action_init_late() { add_action( 'edited_term_taxonomy', array( $this, 'action_edited_term_taxonomy_flush_cache' ), 10, 2 ); } + // This call to $this->supported_post_types() is important, because it's the first and only time $this->supported_post_types is populated register_taxonomy( $this->coauthor_taxonomy, $this->supported_post_types(), $args ); } @@ -255,6 +256,17 @@ public function admin_init() { * @return array Supported post types. */ public function supported_post_types() { + if ( !empty( $this->supported_post_types ) ) { + $this->init_supported_post_types(); + } + + return $this->supported_post_types; + } + + /** + * This should only be run once, and the value of $this->supported_post_types should not change after this. + */ + public function init_supported_post_types() { $post_types = array_values( get_post_types() ); $excluded_built_in = array( @@ -276,12 +288,7 @@ public function 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; + $this->supported_post_types = (array) apply_filters( 'coauthors_supported_post_types', $post_types ); } /** @@ -436,41 +443,41 @@ public function coauthors_meta_box( $post ) { $count = 0; if ( ! empty( $coauthors ) ) : ?> -
- +
+

Note: To edit post authors, please enable JavaScript or use a JavaScript-capable browser', 'co-authors-plus' ), array( 'strong' => array() ) ); ?>

+
+ -
-

Remove to remove them.', 'co-authors-plus' ), array( 'strong' => array() ) ); ?>

-
+
+

Remove to remove them.', 'co-authors-plus' ), array( 'strong' => array() ) ); ?>

+
@@ -542,13 +549,13 @@ public function _filter_manage_posts_custom_column( $column_name ) { } $author_filter_url = add_query_arg( array_map( 'rawurlencode', $args ), admin_url( 'edit.php' ) ); ?> - display_name ); ?> + display_name ); ?> - post_author ); if ( empty( $post_author_user ) - || ! in_array( $post_author_user->user_login, $coauthors ) ) { + || ! in_array( $post_author_user->user_login, $coauthors ) ) { foreach ( $coauthor_objects as $coauthor_object ) { if ( 'wpuser' === $coauthor_object->type ) { $new_author = $coauthor_object; @@ -1455,9 +1462,9 @@ public function js_vars() { return; } ?> - + ; + user_nicename, $this->coauthor_taxonomy ); if ( is_object( $guest_term ) - && ! empty( $guest_author->linked_account ) - && $guest_term->count ) { + && ! empty( $guest_author->linked_account ) + && $guest_term->count ) { $user = get_user_by( 'login', $guest_author->linked_account ); if ( is_object( $user ) ) { return count_user_posts( $user->ID ); // phpcs:ignore From 4ea98b45d98a42936ad537c0ed903bb86448a0db Mon Sep 17 00:00:00 2001 From: Justin Maurer Date: Wed, 24 Apr 2024 14:52:23 -0500 Subject: [PATCH 2/3] Ack. Fixed backwards empty() logic. --- php/class-coauthors-plus.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/php/class-coauthors-plus.php b/php/class-coauthors-plus.php index a8b13d8b..6d97f2c5 100644 --- a/php/class-coauthors-plus.php +++ b/php/class-coauthors-plus.php @@ -256,7 +256,7 @@ public function admin_init() { * @return array Supported post types. */ public function supported_post_types() { - if ( !empty( $this->supported_post_types ) ) { + if ( empty( $this->supported_post_types ) ) { $this->init_supported_post_types(); } @@ -397,6 +397,7 @@ public function remove_authors_box() { * Adds a custom 'Authors' box */ public function add_coauthors_box() { + var_dump($this->is_post_type_enabled()); if ( $this->is_post_type_enabled() && $this->current_user_can_set_authors() ) { if ( false === $this->is_block_editor() ) { add_meta_box( $this->coauthors_meta_box_name, apply_filters( 'coauthors_meta_box_title', __( 'Authors', 'co-authors-plus' ) ), array( $this, 'coauthors_meta_box' ), get_post_type(), apply_filters( 'coauthors_meta_box_context', 'side' ), apply_filters( 'coauthors_meta_box_priority', 'high' ) ); @@ -834,7 +835,7 @@ public function posts_where_filter( $where, $query ) { $current_coauthor = $this->get_coauthor_by( 'user_nicename', wp_get_current_user()->user_nicename ); $current_coauthor_term = $this->get_author_term( $current_coauthor ); - if ( $current_coauthor_term instanceof \WP_Term ) { + if ( $current_coauthor_term instanceof WP_Term ) { $current_user_query = $wpdb->term_taxonomy . '.taxonomy = \'' . $this->coauthor_taxonomy . '\' AND ' . $wpdb->term_taxonomy . '.term_id = \'' . $current_coauthor_term->term_id . '\''; $this->having_terms .= ' ' . $wpdb->term_taxonomy . '.term_id = \'' . $current_coauthor_term->term_id . '\' OR '; From 55c8948cb8c00f118ef2e4f7d4d493f439dacaec Mon Sep 17 00:00:00 2001 From: Justin Maurer Date: Wed, 24 Apr 2024 14:52:57 -0500 Subject: [PATCH 3/3] Remove debugging. --- php/class-coauthors-plus.php | 1 - 1 file changed, 1 deletion(-) diff --git a/php/class-coauthors-plus.php b/php/class-coauthors-plus.php index 6d97f2c5..8c7f371f 100644 --- a/php/class-coauthors-plus.php +++ b/php/class-coauthors-plus.php @@ -397,7 +397,6 @@ public function remove_authors_box() { * Adds a custom 'Authors' box */ public function add_coauthors_box() { - var_dump($this->is_post_type_enabled()); if ( $this->is_post_type_enabled() && $this->current_user_can_set_authors() ) { if ( false === $this->is_block_editor() ) { add_meta_box( $this->coauthors_meta_box_name, apply_filters( 'coauthors_meta_box_title', __( 'Authors', 'co-authors-plus' ) ), array( $this, 'coauthors_meta_box' ), get_post_type(), apply_filters( 'coauthors_meta_box_context', 'side' ), apply_filters( 'coauthors_meta_box_priority', 'high' ) );