| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
use WP_Query; |
| 6 |
use Yoast\WP\Lib\Model; |
| 7 |
|
| 8 |
/** |
| 9 |
* A helper object for author archives. |
| 10 |
*/ |
| 11 |
class Author_Archive_Helper { |
| 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 |
/** |
| 39 |
* Gets the array of post types that are shown on an author's archive. |
| 40 |
* |
| 41 |
* @return array The post types that are shown on an author's archive. |
| 42 |
*/ |
| 43 |
public function get_author_archive_post_types() { |
| 44 |
/** |
| 45 |
* Filters the array of post types that are shown on an author's archive. |
| 46 |
* |
| 47 |
* @param array $args The post types that are shown on an author archive. |
| 48 |
*/ |
| 49 |
return \apply_filters( 'wpseo_author_archive_post_types', [ 'post' ] ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Returns whether the author has at least one public post. |
| 54 |
* |
| 55 |
* @param int $author_id The author ID. |
| 56 |
* |
| 57 |
* @return bool|null Whether the author has at least one public post. |
| 58 |
*/ |
| 59 |
public function author_has_public_posts( $author_id ) { |
| 60 |
// First check if the author has at least one public post. |
| 61 |
$has_public_post = $this->author_has_a_public_post( $author_id ); |
| 62 |
if ( $has_public_post ) { |
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
// Then check if the author has at least one post where the status is the same as the global setting. |
| 67 |
$has_public_post_depending_on_the_global_setting = $this->author_has_a_post_with_is_public_null( $author_id ); |
| 68 |
if ( $has_public_post_depending_on_the_global_setting ) { |
| 69 |
return null; |
| 70 |
} |
| 71 |
|
| 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' ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Returns whether the author has at least one public post. |
| 120 |
* |
| 121 |
* @codeCoverageIgnore It looks for the first ID through the ORM and converts it to a boolean. |
| 122 |
* |
| 123 |
* @param int $author_id The author ID. |
| 124 |
* |
| 125 |
* @return bool Whether the author has at least one public post. |
| 126 |
*/ |
| 127 |
protected function author_has_a_public_post( $author_id ) { |
| 128 |
$cache_key = 'author_has_a_public_post_' . $author_id; |
| 129 |
$indexable_exists = \wp_cache_get( $cache_key ); |
| 130 |
|
| 131 |
if ( $indexable_exists === false ) { |
| 132 |
$indexable_exists = Model::of_type( 'Indexable' ) |
| 133 |
->select( 'id' ) |
| 134 |
->where( 'object_type', 'post' ) |
| 135 |
->where_in( 'object_sub_type', $this->get_author_archive_post_types() ) |
| 136 |
->where( 'author_id', $author_id ) |
| 137 |
->where( 'is_public', 1 ) |
| 138 |
->find_one(); |
| 139 |
|
| 140 |
if ( $indexable_exists === false ) { |
| 141 |
// Cache no results to prevent full table scanning on authors with no public posts. |
| 142 |
\wp_cache_set( $cache_key, 0, '', \wp_rand( ( 2 * \HOUR_IN_SECONDS ), ( 4 * \HOUR_IN_SECONDS ) ) ); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
return (bool) $indexable_exists; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Returns whether the author has at least one post with the is public null. |
| 151 |
* |
| 152 |
* @codeCoverageIgnore It looks for the first ID through the ORM and converts it to a boolean. |
| 153 |
* |
| 154 |
* @param int $author_id The author ID. |
| 155 |
* |
| 156 |
* @return bool Whether the author has at least one post with the is public null. |
| 157 |
*/ |
| 158 |
protected function author_has_a_post_with_is_public_null( $author_id ) { |
| 159 |
$cache_key = 'author_has_a_post_with_is_public_null_' . $author_id; |
| 160 |
$indexable_exists = \wp_cache_get( $cache_key ); |
| 161 |
|
| 162 |
if ( $indexable_exists === false ) { |
| 163 |
$indexable_exists = Model::of_type( 'Indexable' ) |
| 164 |
->select( 'id' ) |
| 165 |
->where( 'object_type', 'post' ) |
| 166 |
->where_in( 'object_sub_type', $this->get_author_archive_post_types() ) |
| 167 |
->where( 'author_id', $author_id ) |
| 168 |
->where_null( 'is_public' ) |
| 169 |
->find_one(); |
| 170 |
|
| 171 |
if ( $indexable_exists === false ) { |
| 172 |
// Cache no results to prevent full table scanning on authors with no is public null posts. |
| 173 |
\wp_cache_set( $cache_key, 0, '', \wp_rand( ( 2 * \HOUR_IN_SECONDS ), ( 4 * \HOUR_IN_SECONDS ) ) ); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
return (bool) $indexable_exists; |
| 178 |
} |
| 179 |
} |
| 180 |
|