Skip to content

Commit

Permalink
Delegate logic to central helper
Browse files Browse the repository at this point in the history
  • Loading branch information
diedexx committed Nov 26, 2021
1 parent 3a18f72 commit 5e89640
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/builders/indexable-post-type-archive-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use wpdb;
use Yoast\WP\SEO\Helpers\Options_Helper;
use Yoast\WP\SEO\Helpers\Post_Helper;
use Yoast\WP\SEO\Helpers\Post_Type_Helper;
use Yoast\WP\SEO\Models\Indexable;
use Yoast\WP\SEO\Values\Indexables\Indexable_Builder_Versions;

Expand Down Expand Up @@ -38,6 +39,13 @@ class Indexable_Post_Type_Archive_Builder {
*/
protected $post_helper;

/**
* A helper for post types.
*
* @var Post_Type_Helper
*/
private $post_type_helper;

/**
* The WPDB instance.
*
Expand All @@ -57,11 +65,13 @@ public function __construct(
Options_Helper $options,
Indexable_Builder_Versions $versions,
Post_Helper $post_helper,
Post_Type_Helper $post_type_helper,
wpdb $wpdb
) {
$this->options = $options;
$this->version = $versions->get_latest_version_for_type( 'post-type-archive' );
$this->post_helper = $post_helper;
$this->post_type_helper = $post_type_helper;
$this->wpdb = $wpdb;
}

Expand All @@ -83,11 +93,8 @@ public function build( $post_type, Indexable $indexable ) {
$indexable->is_robots_noindex = (bool) $this->options->get( 'noindex-ptarchive-' . $post_type );
$indexable->is_public = ( (int) $indexable->is_robots_noindex !== 1 );
$indexable->blog_id = \get_current_blog_id();

$post_type_object = get_post_type_object( $post_type );
if ( $post_type_object !== null ) {
$indexable->is_publicly_viewable = $post_type_object->has_archive && $post_type_object->rewrite !== false;
}
// TODO Diede Does the watcher post type changes this?
$indexable->is_publicly_viewable = $this->post_type_helper->has_publicly_viewable_archive( $post_type );

$indexable = $this->set_aggregate_values( $indexable );

Expand Down
24 changes: 24 additions & 0 deletions src/helpers/post-type-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,30 @@ public function get_accessible_post_types() {
return $post_types;
}

/**
* Checks whether a post_type has a publicly viewable archive.
*
* @param string $post_type The name of a registered post type.
*
* @return bool Whether a post_type has a publicly viewable archive.
*/
public function has_publicly_viewable_archive( $post_type ) {
$post_type_object = get_post_type_object( $post_type );
if ( $post_type_object === null ) {
return false;
}

if ( $post_type_object->publicly_queryable === false ) {
return false;
}

if ( $post_type_object->rewrite === false ) {
return false;
}

return $post_type_object->has_archive;
}

/**
* Returns an array of post types that are excluded from being indexed for the
* indexables.
Expand Down

0 comments on commit 5e89640

Please sign in to comment.