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 48 commits
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"Yoast\\WP\\SEO\\Composer\\Actions::check_coding_standards"
],
"check-cs-thresholds": [
"@putenv YOASTCS_THRESHOLD_ERRORS=251",
"@putenv YOASTCS_THRESHOLD_ERRORS=252",
"@putenv YOASTCS_THRESHOLD_WARNINGS=220",
"Yoast\\WP\\SEO\\Composer\\Actions::check_cs_thresholds"
],
Expand Down
2 changes: 1 addition & 1 deletion lib/migrations/adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public function select_one( $query ) {
return false;
}

return $wpdb->last_result[0];
return (array) $wpdb->last_result[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

Confirmed that select_one() is only used at column_info() which is turn is used only at rename_column() which is not actively used anywhere in our org.

}

return false;
Expand Down
89 changes: 85 additions & 4 deletions lib/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ class Model implements JsonSerializable {
*/
protected $float_columns = [];

/**
* Which columns are deprecated.
*
* @var array
*/
protected $deprecated_columns = [];

/**
* Hacks around the Model to provide WordPress prefix to tables.
*
Expand Down Expand Up @@ -504,15 +511,18 @@ public function set_orm( $orm ) {
* @return mixed The value of the property
*/
public function __get( $property ) {
$value = $this->orm->get( $property );
$original_property = $property;
$property = $this->handle_deprecation( $property );
$value = $this->orm->get( $property );

if ( $value !== null && \in_array( $property, $this->boolean_columns, true ) ) {
// The types of a deprecated property and its replacement may differ. To prevent this kind of deprecation from being a breaking change, use the old/originally requested type.
if ( $value !== null && \in_array( $original_property, $this->boolean_columns, true ) ) {
return (bool) $value;
}
if ( $value !== null && \in_array( $property, $this->int_columns, true ) ) {
if ( $value !== null && \in_array( $original_property, $this->int_columns, true ) ) {
return (int) $value;
}
if ( $value !== null && \in_array( $property, $this->float_columns, true ) ) {
if ( $value !== null && \in_array( $original_property, $this->float_columns, true ) ) {
return (float) $value;
}

Expand All @@ -528,6 +538,7 @@ public function __get( $property ) {
* @return void
*/
public function __set( $property, $value ) {
$property = $this->handle_deprecation( $property );
if ( $value !== null && \in_array( $property, $this->boolean_columns, true ) ) {
$value = ( $value ) ? '1' : '0';
}
Expand All @@ -549,6 +560,7 @@ public function __set( $property, $value ) {
* @return void
*/
public function __unset( $property ) {
$property = $this->handle_deprecation( $property );
$this->orm->__unset( $property );
}

Expand Down Expand Up @@ -578,6 +590,7 @@ public function __debugInfo() {
* @return bool True when value is set.
*/
public function __isset( $property ) {
$property = $this->handle_deprecation( $property );
return $this->orm->__isset( $property );
}

Expand All @@ -589,6 +602,8 @@ public function __isset( $property ) {
* @return string The value of a property.
*/
public function get( $property ) {
$property = $this->handle_deprecation( $property );

return $this->orm->get( $property );
}

Expand All @@ -601,6 +616,37 @@ public function get( $property ) {
* @return static Current object.
*/
public function set( $property, $value = null ) {
$property = $this->handle_deprecation( $property );
$this->orm->set( $property, $value );

return $this;
}

/**
* Setter method for model properties that are deprecated, but still need to updated while they wait to be removed.
*
* @param string|array $property The property to set.
* @param string|null $value The value to give.
*
* @return static Current object.
*
* @internal
*/
public function set_deprecated_property( $property, $value = null ) {
if ( ! array_key_exists( $property, $this->deprecated_columns ) ) {
return $this;
}

if ( $value !== null && \in_array( $property, $this->boolean_columns, true ) ) {
$value = ( $value ) ? '1' : '0';
}
if ( $value !== null && \in_array( $property, $this->int_columns, true ) ) {
$value = (string) $value;
}
if ( $value !== null && \in_array( $property, $this->float_columns, true ) ) {
$value = (string) $value;
}

$this->orm->set( $property, $value );

return $this;
Expand All @@ -615,6 +661,7 @@ public function set( $property, $value = null ) {
* @return static Current object.
*/
public function set_expr( $property, $value = null ) {
$property = $this->handle_deprecation( $property );
$this->orm->set_expr( $property, $value );

return $this;
Expand All @@ -628,6 +675,7 @@ public function set_expr( $property, $value = null ) {
* @return bool True when field is changed.
*/
public function is_dirty( $property ) {
$property = $this->handle_deprecation( $property );
return $this->orm->is_dirty( $property );
}

Expand Down Expand Up @@ -718,4 +766,37 @@ public static function __callStatic( $method, $arguments ) {

return \call_user_func_array( [ $model, $method ], $arguments );
}

/**
* Checks if a property is deprecated and handles notifications and replacement.
*
* @param string $property The property to check for deprecation.
*
* @return string The deprecated property name. Or its replacement if available.
*/
protected function handle_deprecation( $property ) {
if ( ! array_key_exists( $property, $this->deprecated_columns ) ) {
return $property;
}
$deprecation = $this->deprecated_columns[ $property ];

if ( ! empty( $deprecation['replacement'] ) ) {
// There is no _deprecated_property. This matches our usecase best.
\_deprecated_argument(
__FUNCTION__,
esc_html( $deprecation['since'] ),
'Use the \"' . esc_html( $deprecation['replacement'] ) . '\" property instead of \"' . esc_html( $property ) . '\" '
);

if ( $deprecation['automatically_use_replacement'] === true ) {
return $deprecation['replacement'];
}

return $property;
}
// There is no _deprecated_property. This matches our usecase best.
diedexx marked this conversation as resolved.
Show resolved Hide resolved
\_deprecated_argument( __FUNCTION__, esc_html( $deprecation['since'] ), 'The \"' . esc_html( $property ) . '\" property will be removed in a future version' );

return $property;
}
}
1 change: 0 additions & 1 deletion lib/orm.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
* @see http:https://www.php-fig.org/psr/psr-1/
*/
class ORM implements \ArrayAccess {

/*
* --- CLASS CONSTANTS ---
*/
Expand Down
45 changes: 31 additions & 14 deletions src/builders/indexable-author-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,35 @@ 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();
$indexable->set_deprecated_property( 'is_public', ( $indexable->is_robots_noindex ) ? false : null );

$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;
}

/**
* Sets the aggregate values for an author indexable.
*
* @param Indexable $indexable The indexable to set the aggregates for.
*
* @return Indexable The indexable with set aggregates.
*/
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_publicly_viewable_posts = $aggregates->number_of_public_posts;
Comment on lines +112 to +114
Copy link
Contributor

Choose a reason for hiding this comment

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

This block is repeated 4 times throughout the builders.

Maybe create a separate function to avoid Code Duplication?

Copy link
Member Author

Choose a reason for hiding this comment

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

I did consider it, but decided to not change more than I had to. I want to better abstract this when we revise the builders entirely. Do you think that's ok?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeap, for sure it's ok 👍 :)


return $indexable;
}

/**
* Retrieves the meta data for this indexable.
*
Expand Down Expand Up @@ -168,24 +181,28 @@ 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();

$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 = ''
WHERE p.post_status IN (" . implode( ', ', array_fill( 0, count( $post_statuses ), '%s' ) ) . ')
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 );
}
}

17 changes: 9 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,15 @@ 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->is_publicly_viewable = ! (bool) $this->options->get( 'disable-date' );
$indexable->blog_id = \get_current_blog_id();
$indexable->permalink = null;
$indexable->version = $this->version;
$indexable->set_deprecated_property( 'is_public', (int) $indexable->is_robots_noindex !== 1 );

return $indexable;
}
Expand Down
Loading