Skip to content

Commit

Permalink
Add method for creating a noindex query for indexables
Browse files Browse the repository at this point in the history
  • Loading branch information
diedexx committed Nov 15, 2021
1 parent 87c11c3 commit 2eef6e2
Show file tree
Hide file tree
Showing 20 changed files with 494 additions and 252 deletions.
34 changes: 23 additions & 11 deletions src/builders/indexable-author-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,27 @@ public function build( $user_id, Indexable $indexable ) {
$indexable->is_robots_noarchive = null;
$indexable->is_robots_noimageindex = null;
$indexable->is_robots_nosnippet = null;
$indexable->is_public = ( $indexable->is_robots_noindex ) ? false : null;
$indexable->has_public_posts = $this->author_archive->author_has_public_posts( $user_id );
$indexable->blog_id = \get_current_blog_id();

$this->reset_social_images( $indexable );
$this->handle_social_images( $indexable );

$timestamps = $this->get_object_timestamps( $user_id );
$indexable->object_published_at = $timestamps->published_at;
$indexable->object_last_modified = $timestamps->last_modified;
$indexable = $this->set_aggregate_values( $indexable );

$indexable->version = $this->version;

return $indexable;
}

public function set_aggregate_values( Indexable $indexable ) {
$aggregates = $this->get_public_post_archive_aggregates( $indexable->object_id );
$indexable->object_published_at = $aggregates->first_published_at;
$indexable->object_last_modified = max($indexable->object_last_modified, $aggregates->most_recent_last_modified);
$indexable->number_of_public_posts = $aggregates->number_of_public_posts;

return $indexable;
}

/**
* Retrieves the meta data for this indexable.
*
Expand Down Expand Up @@ -168,24 +173,31 @@ protected function find_alternative_image( Indexable $indexable ) {
}

/**
* Returns the timestamps for a given author.
* Returns public post aggregates for a given author.
*
* @param int $author_id The author ID.
* @param int $author_id The author ID.
*
* @return object An object with last_modified and published_at timestamps.
* @return object An object with the number of public posts, most recent last modified and first published at timestamps.
*/
protected function get_object_timestamps( $author_id ) {
protected function get_public_post_archive_aggregates( $author_id ) {
$post_statuses = $this->post_helper->get_public_post_statuses();
$post_types = $this->author_archive->get_author_archive_post_types();
// TODO DIEDE: Protected pages krijgen _geen_ noindex. en staan gewoon in het author archive. moeten die meegeteld worden?
// Private werkt wel zoals verwacht.

$sql = "
SELECT MAX(p.post_modified_gmt) AS last_modified, MIN(p.post_date_gmt) AS published_at
SELECT
COUNT(p.ID) as number_of_public_posts,
MAX(p.post_modified_gmt) AS most_recent_last_modified,
MIN(p.post_date_gmt) AS first_published_at
FROM {$this->wpdb->posts} AS p
WHERE p.post_status IN (" . implode( ', ', array_fill( 0, count( $post_statuses ), '%s' ) ) . ")
AND p.post_password = ''
AND p.post_author = %d
AND p.post_type IN (" . implode( ', ', array_fill( 0, count( $post_types ), '%s' ) ) . ")
";

$replacements = \array_merge( $post_statuses, [ $author_id ] );
$replacements = \array_merge( $post_statuses, [ $author_id ], $post_types );

// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- We are using wpdb prepare.
return $this->wpdb->get_row( $this->wpdb->prepare( $sql, $replacements ) );
Expand Down
44 changes: 42 additions & 2 deletions src/builders/indexable-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public function build_for_post_type_archive( $post_type, $indexable = false ) {
'object_type' => 'post-type-archive',
'object_sub_type' => $post_type,
];

return $this->build( $indexable, $defaults );
}

Expand All @@ -235,6 +236,7 @@ public function build_for_system_page( $page_type, $indexable = false ) {
'object_type' => 'system-page',
'object_sub_type' => $page_type,
];

return $this->build( $indexable, $defaults );
}

Expand Down Expand Up @@ -372,8 +374,7 @@ public function build( $indexable, $defaults = null ) {
}

return $this->save_indexable( $indexable, $indexable_before );
}
catch ( Source_Exception $exception ) {
} catch ( Source_Exception $exception ) {
/**
* The current indexable could not be indexed. Create a placeholder indexable, so we can
* skip this indexable in future indexing runs.
Expand All @@ -396,4 +397,43 @@ public function build( $indexable, $defaults = null ) {
return $this->save_indexable( $indexable, $indexable_before );
}
}

/**
* Recalculates indexable aggregates.
*
* @param Indexable $indexable The Indexable to (re)build.
*
* @return Indexable The resulting Indexable.
*/
public function recalculate_aggregates( Indexable $indexable ) {
// Backup the previous Indexable, if there was one.
$indexable_before = $this->deep_copy_indexable( $indexable );

switch ( $indexable->object_type ) {
case 'system-page':
case 'date-archive':
case 'post':
// Nothing to recalculate.
break;

case 'user':
$indexable = $this->author_builder->set_aggregate_values( $indexable );
break;

case 'term':
$indexable = $this->term_builder->set_aggregate_values( $indexable );
break;

case 'home-page':
$indexable = $this->home_page_builder->set_aggregate_values( $indexable );
break;

case 'post-type-archive':
$indexable = $this->post_type_archive_builder->set_aggregate_values( $indexable );
break;
}

return $this->save_indexable( $indexable, $indexable_before );
}
}

15 changes: 7 additions & 8 deletions src/builders/indexable-date-archive-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@ public function __construct(
* @return Indexable The extended indexable.
*/
public function build( $indexable ) {
$indexable->object_type = 'date-archive';
$indexable->title = $this->options->get( 'title-archive-wpseo' );
$indexable->description = $this->options->get( 'metadesc-archive-wpseo' );
$indexable->is_robots_noindex = $this->options->get( 'noindex-archive-wpseo' );
$indexable->is_public = ( (int) $indexable->is_robots_noindex !== 1 );
$indexable->blog_id = \get_current_blog_id();
$indexable->permalink = null;
$indexable->version = $this->version;
$indexable->object_type = 'date-archive';
$indexable->title = $this->options->get( 'title-archive-wpseo' );
$indexable->description = $this->options->get( 'metadesc-archive-wpseo' );
$indexable->is_robots_noindex = $this->options->get( 'noindex-archive-wpseo' );
$indexable->blog_id = \get_current_blog_id();
$indexable->permalink = null;
$indexable->version = $this->version;

return $indexable;
}
Expand Down
24 changes: 17 additions & 7 deletions src/builders/indexable-home-page-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,25 +114,35 @@ public function build( $indexable ) {
$this->set_open_graph_image_meta_data( $indexable );
}

$timestamps = $this->get_object_timestamps();
$indexable->object_published_at = $timestamps->published_at;
$indexable->object_last_modified = $timestamps->last_modified;
$indexable = $this->set_aggregate_values( $indexable );

$indexable->version = $this->version;

return $indexable;
}

public function set_aggregate_values( Indexable $indexable ) {
$aggregates = $this->get_public_post_archive_aggregates();
$indexable->object_published_at = $aggregates->first_published_at;
$indexable->object_last_modified = $aggregates->most_recent_last_modified;
$indexable->number_of_public_posts = $aggregates->number_of_public_posts;

return $indexable;
}

/**
* Returns the timestamps for the homepage.
* Returns public post aggregates for the homepage.
*
* @return object An object with last_modified and published_at timestamps.
* @return object An object with the number of posts, most recent last modified and first published at timestamps.
*/
protected function get_object_timestamps() {
protected function get_public_post_archive_aggregates() {
$post_statuses = $this->post_helper->get_public_post_statuses();

$sql = "
SELECT MAX(p.post_modified_gmt) AS last_modified, MIN(p.post_date_gmt) AS published_at
SELECT
COUNT(p.ID) as number_of_public_posts,
MAX(p.post_modified_gmt) AS most_recent_last_modified,
MIN(p.post_date_gmt) AS first_published_at
FROM {$this->wpdb->posts} AS p
WHERE p.post_status IN (" . implode( ', ', array_fill( 0, count( $post_statuses ), '%s' ) ) . ")
AND p.post_password = ''
Expand Down
72 changes: 10 additions & 62 deletions src/builders/indexable-post-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,20 @@ public function build( $post_id, $indexable ) {
$indexable->author_id = $post->post_author;
$indexable->post_parent = $post->post_parent;

$indexable->number_of_pages = $this->get_number_of_pages_for_post( $post );
$indexable->post_status = $post->post_status;
$indexable->is_protected = $post->post_password !== '';
$indexable->is_public = $this->is_public( $indexable );
$indexable->has_public_posts = $this->has_public_posts( $indexable );
$indexable->blog_id = \get_current_blog_id();
$indexable->number_of_pages = $this->get_number_of_pages_for_post( $post );
$indexable->post_status = $post->post_status;
$indexable->is_protected = $post->post_password !== '';

$indexable->blog_id = \get_current_blog_id();

$indexable->schema_page_type = $this->get_meta_value( $post_id, 'schema_page_type' );
$indexable->schema_article_type = $this->get_meta_value( $post_id, 'schema_article_type' );

$indexable->object_last_modified = $post->post_modified_gmt;
$indexable->object_published_at = $post->post_date_gmt;

$indexable->number_of_public_posts = 0;

$indexable->version = $this->version;

return $indexable;
Expand All @@ -181,31 +182,10 @@ protected function get_permalink( $post_type, $post_id ) {
*
* @param Indexable $indexable The indexable.
*
* @return bool|null Whether or not the post type is public. Null if no override is set.
* @return bool|null Whether the post type is public. Null if no override is set.
*/
protected function is_public( $indexable ) {
if ( $indexable->is_protected === true ) {
return false;
}

if ( $indexable->is_robots_noindex === true ) {
return false;
}

// Attachments behave differently than the other post types, since they inherit from their parent.
if ( $indexable->object_sub_type === 'attachment' ) {
return $this->is_public_attachment( $indexable );
}

if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) {
return false;
}

if ( $indexable->is_robots_noindex === false ) {
return true;
}

return null;
protected function is_accessible_post( $indexable ) {
return is_post_type_viewable($indexable->object_sub_type) && is_post_status_viewable($indexable->post_status);
}

/**
Expand All @@ -225,38 +205,6 @@ protected function is_public_attachment( $indexable ) {
return null;
}

/**
* Determines the value of has_public_posts.
*
* @param Indexable $indexable The indexable.
*
* @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment.
*/
protected function has_public_posts( $indexable ) {
// Only attachments (and authors) have this value.
if ( $indexable->object_sub_type !== 'attachment' ) {
return null;
}

// The attachment should have a post parent.
if ( empty( $indexable->post_parent ) ) {
return false;
}

// The attachment should inherit the post status.
if ( $indexable->post_status !== 'inherit' ) {
return false;
}

// The post parent should be public.
$post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' );
if ( $post_parent_indexable !== false ) {
return $post_parent_indexable->is_public;
}

return false;
}

/**
* Converts the meta robots noindex value to the indexable value.
*
Expand Down
30 changes: 20 additions & 10 deletions src/builders/indexable-post-type-archive-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,21 @@ public function build( $post_type, Indexable $indexable ) {
$indexable->description = $this->options->get( 'metadesc-ptarchive-' . $post_type );
$indexable->breadcrumb_title = $this->get_breadcrumb_title( $post_type );
$indexable->permalink = \get_post_type_archive_link( $post_type );
$indexable->is_robots_noindex = $this->options->get( 'noindex-ptarchive-' . $post_type );
$indexable->is_public = ( (int) $indexable->is_robots_noindex !== 1 );
$indexable->is_robots_noindex = (bool) $this->options->get( 'noindex-ptarchive-' . $post_type );
$indexable->blog_id = \get_current_blog_id();
$indexable->version = $this->version;

$timestamps = $this->get_object_timestamps( $post_type );
$indexable->object_published_at = $timestamps->published_at;
$indexable->object_last_modified = $timestamps->last_modified;
$indexable = $this->set_aggregate_values( $indexable );

$indexable->version = $this->version;

return $indexable;
}

public function set_aggregate_values( Indexable $indexable ) {
$aggregates = $this->get_public_post_archive_aggregates( $indexable->object_sub_type );
$indexable->object_published_at = $aggregates->published_at;
$indexable->object_last_modified = $aggregates->last_modified;
$indexable->number_of_public_posts = $aggregates->number_of_public_posts;

return $indexable;
}
Expand Down Expand Up @@ -124,17 +131,20 @@ private function get_breadcrumb_title( $post_type ) {
}

/**
* Returns the timestamps for a given post type.
* Returns public post aggregates for a given post type.
*
* @param string $post_type The post type.
*
* @return object An object with last_modified and published_at timestamps.
* @return object An object with the number of posts, most recent last modified and first published at timestamps.
*/
protected function get_object_timestamps( $post_type ) {
protected function get_public_post_archive_aggregates( $post_type ) {
$post_statuses = $this->post_helper->get_public_post_statuses();

$sql = "
SELECT MAX(p.post_modified_gmt) AS last_modified, MIN(p.post_date_gmt) AS published_at
SELECT
COUNT(p.ID) as number_of_public_posts,
MAX(p.post_modified_gmt) AS most_recent_last_modified,
MIN(p.post_date_gmt) AS first_published_at
FROM {$this->wpdb->posts} AS p
WHERE p.post_status IN (" . implode( ', ', array_fill( 0, count( $post_statuses ), '%s' ) ) . ")
AND p.post_password = ''
Expand Down
13 changes: 7 additions & 6 deletions src/builders/indexable-system-page-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Indexable_System_Page_Builder {
*/
const OPTION_MAPPING = [
'search-result' => [
'title' => 'title-search-wpseo',
'title' => 'title-search-wpseo',
],
'404' => [
'title' => 'title-404-wpseo',
Expand Down Expand Up @@ -65,11 +65,12 @@ public function __construct(
* @return Indexable The extended indexable.
*/
public function build( $object_sub_type, Indexable $indexable ) {
$indexable->object_type = 'system-page';
$indexable->object_sub_type = $object_sub_type;
$indexable->title = $this->options->get( static::OPTION_MAPPING[ $object_sub_type ]['title'] );
$indexable->is_robots_noindex = true;
$indexable->blog_id = \get_current_blog_id();
$indexable->object_type = 'system-page';
$indexable->object_sub_type = $object_sub_type;
$indexable->title = $this->options->get( static::OPTION_MAPPING[ $object_sub_type ]['title'] );
$indexable->is_robots_noindex = true;
$indexable->number_of_public_posts = 0;
$indexable->blog_id = \get_current_blog_id();

if ( \array_key_exists( 'breadcrumb_title', static::OPTION_MAPPING[ $object_sub_type ] ) ) {
$indexable->breadcrumb_title = $this->options->get( static::OPTION_MAPPING[ $object_sub_type ]['breadcrumb_title'] );
Expand Down
Loading

0 comments on commit 2eef6e2

Please sign in to comment.