| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
use Yoast\WP\Lib\Model; |
| 6 |
|
| 7 |
/** |
| 8 |
* A helper object for author archives. |
| 9 |
*/ |
| 10 |
class Author_Archive_Helper { |
| 11 |
|
| 12 |
/** |
| 13 |
* Gets the array of post types that are shown on an author's archive. |
| 14 |
* |
| 15 |
* @return array The post types that are shown on an author's archive. |
| 16 |
*/ |
| 17 |
public function get_author_archive_post_types() { |
| 18 |
/** |
| 19 |
* Filters the array of post types that are shown on an author's archive. |
| 20 |
* |
| 21 |
* @param array $args The post types that are shown on an author archive. |
| 22 |
*/ |
| 23 |
return \apply_filters( 'wpseo_author_archive_post_types', [ 'post' ] ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Returns whether the author has at least one public post. |
| 28 |
* |
| 29 |
* @param int $author_id The author ID. |
| 30 |
* |
| 31 |
* @return bool|null Whether the author has at least one public post. |
| 32 |
*/ |
| 33 |
public function author_has_public_posts( $author_id ) { |
| 34 |
// First check if the author has at least one public post. |
| 35 |
$has_public_post = $this->author_has_a_public_post( $author_id ); |
| 36 |
if ( $has_public_post ) { |
| 37 |
return true; |
| 38 |
} |
| 39 |
|
| 40 |
// Then check if the author has at least one post where the status is the same as the global setting. |
| 41 |
$has_public_post_depending_on_the_global_setting = $this->author_has_a_post_with_is_public_null( $author_id ); |
| 42 |
if ( $has_public_post_depending_on_the_global_setting ) { |
| 43 |
return null; |
| 44 |
} |
| 45 |
|
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Returns whether the author has at least one public post. |
| 51 |
* |
| 52 |
* @codeCoverageIgnore It looks for the first ID through the ORM and converts it to a boolean. |
| 53 |
* |
| 54 |
* @param int $author_id The author ID. |
| 55 |
* |
| 56 |
* @return bool Whether the author has at least one public post. |
| 57 |
*/ |
| 58 |
protected function author_has_a_public_post( $author_id ) { |
| 59 |
$cache_key = 'author_has_a_public_post_' . $author_id; |
| 60 |
$indexable_exists = \wp_cache_get( $cache_key ); |
| 61 |
|
| 62 |
if ( $indexable_exists === false ) { |
| 63 |
$indexable_exists = Model::of_type( 'Indexable' ) |
| 64 |
->select( 'id' ) |
| 65 |
->where( 'object_type', 'post' ) |
| 66 |
->where_in( 'object_sub_type', $this->get_author_archive_post_types() ) |
| 67 |
->where( 'author_id', $author_id ) |
| 68 |
->where( 'is_public', 1 ) |
| 69 |
->find_one(); |
| 70 |
|
| 71 |
if ( $indexable_exists === false ) { |
| 72 |
// Cache no results to prevent full table scanning on authors with no public posts. |
| 73 |
\wp_cache_set( $cache_key, 0, '', \wp_rand( ( 2 * \HOUR_IN_SECONDS ), ( 4 * \HOUR_IN_SECONDS ) ) ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return (bool) $indexable_exists; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns whether the author has at least one post with the is public null. |
| 82 |
* |
| 83 |
* @codeCoverageIgnore It looks for the first ID through the ORM and converts it to a boolean. |
| 84 |
* |
| 85 |
* @param int $author_id The author ID. |
| 86 |
* |
| 87 |
* @return bool Whether the author has at least one post with the is public null. |
| 88 |
*/ |
| 89 |
protected function author_has_a_post_with_is_public_null( $author_id ) { |
| 90 |
$cache_key = 'author_has_a_post_with_is_public_null_' . $author_id; |
| 91 |
$indexable_exists = \wp_cache_get( $cache_key ); |
| 92 |
|
| 93 |
if ( $indexable_exists === false ) { |
| 94 |
$indexable_exists = Model::of_type( 'Indexable' ) |
| 95 |
->select( 'id' ) |
| 96 |
->where( 'object_type', 'post' ) |
| 97 |
->where_in( 'object_sub_type', $this->get_author_archive_post_types() ) |
| 98 |
->where( 'author_id', $author_id ) |
| 99 |
->where_null( 'is_public' ) |
| 100 |
->find_one(); |
| 101 |
|
| 102 |
if ( $indexable_exists === false ) { |
| 103 |
// Cache no results to prevent full table scanning on authors with no is public null posts. |
| 104 |
\wp_cache_set( $cache_key, 0, '', \wp_rand( ( 2 * \HOUR_IN_SECONDS ), ( 4 * \HOUR_IN_SECONDS ) ) ); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return (bool) $indexable_exists; |
| 109 |
} |
| 110 |
} |
| 111 |
|