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

Fix/ir and pw protected posts #2646

Merged
merged 4 commits into from
Mar 8, 2022
Merged
Changes from 1 commit
Commits
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
PW Protected Content: Prevent full content to be indexed
  • Loading branch information
felipeelia committed Mar 8, 2022
commit 5949170779242a3f0650623a6fc87a85db43d6d6
51 changes: 51 additions & 0 deletions includes/classes/Feature/ProtectedContent/ProtectedContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function setup() {
add_filter( 'ep_post_formatted_args', [ $this, 'exclude_protected_posts' ], 10, 2 );
add_filter( 'ep_index_posts_args', [ $this, 'query_password_protected_posts' ] );
add_filter( 'ep_post_sync_args', [ $this, 'include_post_password' ], 10, 2 );
add_filter( 'ep_post_sync_args', [ $this, 'remove_fields_from_password_protected' ], 11, 2 );
add_filter( 'ep_search_post_return_args', [ $this, 'return_post_password' ] );

if ( is_admin() ) {
Expand Down Expand Up @@ -226,6 +227,56 @@ public function include_post_password( $post_args, $post_id ) {
return $post_args;
}

/**
* Prevent some fields in password protected posts from being indexed.
*
* As some solutions publicly expose full post contents, this method prevents password
* protected posts to have their full content and their meta fields indexed. Developers
* wanting to bypass this behavior can use the `ep_pc_skip_post_content_cleanup` filter.
*
* @param array $post_args Post arguments
* @param int $post_id Post ID
* @return array
*/
public function remove_fields_from_password_protected( $post_args, $post_id ) {
if ( empty( $post_args['post_password'] ) ) {
return $post_args;
}

/**
* Filter to skip the password protected content clean up.
*
* @hook ep_pc_skip_post_content_cleanup
* @since 4.0.0
* @param {bool} $skip Whether the password protected content should have their content, and meta removed.
* @return {bool}
*/
if ( apply_filters( 'ep_pc_skip_post_content_cleanup', false ) ) {
return $post_args;
}

$fields_to_remove = [
'post_content_filtered',
'post_content',
'meta',
'thumbnail',
'post_content_plain',
'price_html',
];

foreach ( $fields_to_remove as $field ) {
if ( ! empty( $post_args[ $field ] ) ) {
if ( is_array( $post_args[ $field ] ) ) {
$post_args[ $field ] = [];
} else {
$post_args[ $field ] = '';
}
}
}

return $post_args;
}

/**
* Exclude proctected post from the frontend queries.
*
Expand Down