Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Indexable visibility columns #17757

Open
wants to merge 58 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
b3fd2cd
Ensure array return type
diedexx Nov 12, 2021
87c11c3
Allow for deprecating columns
diedexx Nov 12, 2021
2eef6e2
Add method for creating a noindex query for indexables
diedexx Nov 15, 2021
b5bc8db
Rename is_public to is_publicly_viewable and number_of_public_posts t…
diedexx Nov 15, 2021
6c015ba
Use PHP 5.6 syntax
diedexx Nov 16, 2021
3d96968
Add migrations
diedexx Nov 16, 2021
acdf97a
Deprecate is_public instead of immediately replacing it
diedexx Nov 16, 2021
e18c9ac
Keep the method name the same and deprecate
diedexx Nov 16, 2021
58d6a71
Access properties that were renamed earlier
diedexx Nov 16, 2021
b7cfa04
Make the homepage get a number_of_publicly_viewable_posts and is_publ…
diedexx Nov 16, 2021
c2d121e
Use new column name
diedexx Nov 16, 2021
38b552d
Add deprecation tags
diedexx Nov 16, 2021
2fdce2f
Fix cs
diedexx Nov 16, 2021
3a18f72
Mark enabled date archives as publicly viewable
diedexx Nov 26, 2021
5e89640
Delegate logic to central helper
diedexx Nov 26, 2021
0a38f49
Make global namespace usage explicit
diedexx Nov 26, 2021
81e7421
Flip argument order
diedexx Nov 26, 2021
1564f53
Allow for considering empty archives as indexed
diedexx Nov 26, 2021
ecae28e
Update deperatation based on the expected release version
diedexx Nov 26, 2021
ea0a46e
Prevent new integrators from accessing is_public
diedexx Nov 26, 2021
6ccacd5
Update tests
diedexx Nov 26, 2021
ccf7f9b
Remove resolved todo comment
diedexx Nov 26, 2021
af7b219
Rebuild date archive indexable when disabling date archives
diedexx Nov 26, 2021
510f9de
Fix cs
diedexx Nov 26, 2021
ed46abb
Prevent deprecation warnings when (re)indexing
diedexx Nov 26, 2021
ff247f9
Include password protected posts in aggegrate queries
diedexx Nov 30, 2021
ca85a07
Keep the most recent last modified date.
diedexx Nov 30, 2021
faf1180
Merge branch 'trunk' of https://github.com/Yoast/wordpress-seo into F…
diedexx Nov 30, 2021
f6a8adf
Adhere to naming conventions
diedexx Dec 2, 2021
274df53
Don't hardcode the deprecation version number
diedexx Dec 2, 2021
d51829e
Update deprecation versions and descriptions
diedexx Dec 2, 2021
5c1193a
Rename migration
diedexx Dec 2, 2021
661e9a3
Merge branch 'trunk' of https://github.com/Yoast/wordpress-seo into F…
diedexx Dec 3, 2021
82f8a78
Fix cs
diedexx Dec 3, 2021
66d6e4d
Update tests
diedexx Dec 3, 2021
cf56df4
Fill WP 5.7 function
diedexx Dec 3, 2021
68aba31
Mock calls to newly introduced functions
diedexx Dec 3, 2021
3329a1b
Polyfill WP 5.7 function
diedexx Dec 3, 2021
e906ace
Lower error threshold
diedexx Dec 3, 2021
29739ee
Remove unused function that never made it into trunk
diedexx Dec 3, 2021
66c9433
Remove test double funtion that was removed in its parent
diedexx Dec 6, 2021
9eb9263
Make global namespace explicit
diedexx Dec 7, 2021
0c9ac1a
Ensure that post_types is an array
diedexx Dec 7, 2021
2059186
Automatically update date-archive indexables
diedexx Dec 7, 2021
b95492e
Use the type of the originally requested property in case of replaced…
diedexx Dec 10, 2021
101edcf
Merge branch 'trunk' of https://github.com/Yoast/wordpress-seo into F…
diedexx Dec 10, 2021
5d095a5
Fix CS warnings and errors
diedexx Dec 10, 2021
d8f754f
Fix tests
diedexx Dec 10, 2021
f613dfc
Clarify that the comment is about a function
diedexx Dec 17, 2021
3e7e755
Add instructions to remove related code
diedexx Dec 17, 2021
c24488e
Reduce risk of using the same builder version twice.
diedexx Dec 17, 2021
4ed8b38
Update post relation indexables when a post is set to private
diedexx Dec 17, 2021
275c423
Fix cs
diedexx Dec 18, 2021
08c1f5f
Fix precedence issues in query
diedexx Dec 19, 2021
e1c5055
Merge branch 'trunk' of https://github.com/Yoast/wordpress-seo into F…
diedexx Dec 20, 2021
6cf15bf
Increase threhshold
diedexx Dec 20, 2021
bad2929
Exclude password protected posts from the publicly_viewable_count
diedexx Dec 21, 2021
148d47c
Merge branch 'trunk' of github.com:Yoast/wordpress-seo into FIX-22-nu…
Dec 30, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add method for creating a noindex query for indexables
  • Loading branch information
diedexx committed Nov 15, 2021
commit 2eef6e2745c0db148caef022dbedd4cc84ba0f4c
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idea: how about getting post ID's for an author first and iterating over those - using primary keys seems more efficient than where-ing over a set of fields.

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 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a check if $post_types is array here, before merging it to the $replacement?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! that could've been a nasty bug. I added validation higher up in the get_author_post_types so we're sure that we get an array.


// 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 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would then remove the has_public_posts() method from the Indexable_Post_Builder_Double class

// 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