| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Yoast\WP\SEO\Helpers; |
| 4 | 4 | |
| 5 | +use WP_Query; | |
| 5 | 6 | use Yoast\WP\Lib\Model; |
| 6 | 7 | |
| 7 | 8 | /** |
| 8 | 9 | * A helper object for author archives. |
| @@ -9,8 +10,33 @@ | ||
| 9 | 10 | */ |
| 10 | 11 | class Author_Archive_Helper { |
| 11 | 12 | |
| 12 | 13 | /** |
| 14 | + * The options helper. | |
| 15 | + * | |
| 16 | + * @var Options_Helper | |
| 17 | + */ | |
| 18 | + private $options_helper; | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * The post type helper. | |
| 22 | + * | |
| 23 | + * @var Post_Type_Helper | |
| 24 | + */ | |
| 25 | + private $post_type_helper; | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * Creates a new author archive helper. | |
| 29 | + * | |
| 30 | + * @param Options_Helper $options_helper The options helper. | |
| 31 | + * @param Post_Type_Helper $post_type_helper The post type helper. | |
| 32 | + */ | |
| 33 | + public function __construct( Options_Helper $options_helper, Post_Type_Helper $post_type_helper ) { | |
| 34 | + $this->options_helper = $options_helper; | |
| 35 | + $this->post_type_helper = $post_type_helper; | |
| 36 | + } | |
| 37 | + | |
| 38 | + /** | |
| 13 | 39 | * Gets the array of post types that are shown on an author's archive. |
| 14 | 40 | * |
| 15 | 41 | * @return array The post types that are shown on an author's archive. |
| 16 | 42 | */ |
| @@ -43,8 +69,51 @@ | ||
| 43 | 69 | return null; |
| 44 | 70 | } |
| 45 | 71 | |
| 46 | 72 | return false; |
| 73 | + } | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * Returns whether the author has at least one public post. | |
| 77 | + * | |
| 78 | + * **Note**: It uses WP_Query to determine the number of posts, | |
| 79 | + * not the indexables table. | |
| 80 | + * | |
| 81 | + * @param string $user_id The user ID. | |
| 82 | + * | |
| 83 | + * @return bool Whether the author has at least one public post. | |
| 84 | + */ | |
| 85 | + public function author_has_public_posts_wp( $user_id ) { | |
| 86 | + $post_types = \array_intersect( $this->get_author_archive_post_types(), $this->post_type_helper->get_indexable_post_types() ); | |
| 87 | + $public_post_stati = \array_values( \array_filter( \get_post_stati(), 'is_post_status_viewable' ) ); | |
| 88 | + | |
| 89 | + $args = [ | |
| 90 | + 'post_type' => $post_types, | |
| 91 | + 'post_status' => $public_post_stati, | |
| 92 | + 'author' => $user_id, | |
| 93 | + 'update_post_term_cache' => false, | |
| 94 | + 'update_post_meta_cache' => false, | |
| 95 | + 'no_found_rows' => true, | |
| 96 | + 'fields' => 'ids', | |
| 97 | + 'posts_per_page' => 1, | |
| 98 | + ]; | |
| 99 | + | |
| 100 | + $query = new WP_Query( $args ); | |
| 101 | + | |
| 102 | + if ( $query->have_posts() ) { | |
| 103 | + return true; | |
| 104 | + } | |
| 105 | + | |
| 106 | + return false; | |
| 107 | + } | |
| 108 | + | |
| 109 | + /** | |
| 110 | + * Checks whether author archives are disabled. | |
| 111 | + * | |
| 112 | + * @return bool `true` if author archives are disabled, `false` if not. | |
| 113 | + */ | |
| 114 | + public function are_disabled() { | |
| 115 | + return $this->options_helper->get( 'disable-author' ); | |
| 47 | 116 | } |
| 48 | 117 | |
| 49 | 118 | /** |
| 50 | 119 | * Returns whether the author has at least one public post. |