-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Privacy: Reset wp_page_for_privacy_policy when the page is trashed or deleted #11443
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
Open
masteradhoc
wants to merge
14
commits into
WordPress:trunk
Choose a base branch
from
masteradhoc:56694-uncached-database-reads
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
869faa0
add _reset_privacy_policy_page_for_post function
masteradhoc e5745b5
add actions
masteradhoc 6b3e05b
add reset option
masteradhoc 3930d1c
Removed the "page is in trash" error
masteradhoc e391d36
add feedback of mukesh
masteradhoc f4dfb44
add feedback of mukesh
masteradhoc 555e5c6
Fix whitespace
masteradhoc 47d48d6
fix phpstan error
masteradhoc beb4349
add westons feedback
masteradhoc fbf18e4
remove redundant code
masteradhoc f8e9ddb
add unit tests
masteradhoc d398eeb
upgrade failing test
masteradhoc ccfd7f2
update test
masteradhoc 49426c4
fix tests
masteradhoc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
tests/phpunit/tests/privacy/wpPrivacyResetPolicyPageForPost.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| <?php | ||
| /** | ||
| * Tests for _reset_privacy_policy_page_for_post() and the self-healing | ||
| * check in WP_Privacy_Policy_Content::notice(). | ||
| * | ||
| * @package WordPress | ||
| * @subpackage UnitTests | ||
| * @since 7.1.0 | ||
| * | ||
| * @group privacy | ||
| * | ||
| * @covers ::_reset_privacy_policy_page_for_post | ||
| */ | ||
| class Tests_Privacy_WpPrivacyResetPolicyPageForPost extends WP_UnitTestCase { | ||
| /** | ||
| * ID of the page set as the Privacy Policy page. | ||
| * | ||
| * @var int | ||
| */ | ||
| private $policy_page_id; | ||
|
|
||
| public function set_up() { | ||
| parent::set_up(); | ||
|
|
||
| $this->policy_page_id = self::factory()->post->create( array( 'post_type' => 'page' ) ); | ||
| update_option( 'wp_page_for_privacy_policy', $this->policy_page_id ); | ||
| } | ||
|
|
||
| public function tear_down() { | ||
| delete_option( 'wp_page_for_privacy_policy' ); | ||
| parent::tear_down(); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that trashing the Privacy Policy page resets the option to 0. | ||
| * | ||
| * @ticket 56694 | ||
| */ | ||
| public function test_trashing_privacy_policy_page_resets_option() { | ||
| wp_trash_post( $this->policy_page_id ); | ||
|
|
||
| $this->assertSame( 0, (int) get_option( 'wp_page_for_privacy_policy' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that permanently deleting the Privacy Policy page resets the option to 0. | ||
| * | ||
| * @ticket 56694 | ||
| */ | ||
| public function test_deleting_privacy_policy_page_resets_option() { | ||
| wp_delete_post( $this->policy_page_id, true ); | ||
|
|
||
| $this->assertSame( 0, (int) get_option( 'wp_page_for_privacy_policy' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that trashing a different page does not change the option. | ||
| * | ||
| * @ticket 56694 | ||
| */ | ||
| public function test_trashing_a_different_page_does_not_reset_option() { | ||
| $other_page_id = self::factory()->post->create( array( 'post_type' => 'page' ) ); | ||
| wp_trash_post( $other_page_id ); | ||
|
|
||
| $this->assertSame( | ||
| $this->policy_page_id, | ||
| (int) get_option( 'wp_page_for_privacy_policy' ), | ||
| 'Trashing an unrelated page should not reset wp_page_for_privacy_policy.' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that deleting a non-page post type does not change the option. | ||
| * | ||
| * @ticket 56694 | ||
| */ | ||
| public function test_deleting_non_page_post_type_does_not_reset_option() { | ||
| $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) ); | ||
| wp_delete_post( $post_id, true ); | ||
|
|
||
| $this->assertSame( | ||
| $this->policy_page_id, | ||
| (int) get_option( 'wp_page_for_privacy_policy' ), | ||
| 'Deleting a non-page post should not reset wp_page_for_privacy_policy.' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that WP_Privacy_Policy_Content::notice() resets the option to 0 | ||
| * when the stored ID points to a page that no longer exists. | ||
| * | ||
| * @ticket 56694 | ||
| * | ||
| * @covers WP_Privacy_Policy_Content::notice | ||
| */ | ||
| public function test_notice_self_heals_when_policy_page_does_not_exist() { | ||
| require_once ABSPATH . 'wp-admin/includes/class-wp-privacy-policy-content.php'; | ||
|
|
||
| update_option( 'wp_page_for_privacy_policy', 99999 ); | ||
|
|
||
| $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); | ||
| wp_set_current_user( $user_id ); | ||
| if ( is_multisite() ) { | ||
| grant_super_admin( $user_id ); | ||
| } | ||
| set_current_screen( 'post' ); | ||
|
|
||
| $post = self::factory()->post->create_and_get( array( 'post_type' => 'page' ) ); | ||
| WP_Privacy_Policy_Content::notice( $post ); | ||
|
|
||
| $this->assertSame( | ||
| 0, | ||
| (int) get_option( 'wp_page_for_privacy_policy' ), | ||
| 'notice() should reset the option to 0 when the stored page does not exist.' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that _reset_privacy_policy_page_for_post() does not call | ||
| * update_option() when wp_page_for_privacy_policy is already 0. | ||
| * | ||
| * @ticket 56694 | ||
| */ | ||
| public function test_no_update_option_when_policy_page_already_zero() { | ||
| update_option( 'wp_page_for_privacy_policy', 0 ); | ||
|
|
||
| $call_count = 0; | ||
| add_filter( | ||
| 'pre_update_option_wp_page_for_privacy_policy', | ||
| static function ( $value ) use ( &$call_count ) { | ||
| ++$call_count; | ||
| return $value; | ||
| } | ||
| ); | ||
|
|
||
| $other_page_id = self::factory()->post->create( array( 'post_type' => 'page' ) ); | ||
| wp_trash_post( $other_page_id ); | ||
|
|
||
| $this->assertSame( | ||
| 0, | ||
| $call_count, | ||
| 'update_option() should not be called when wp_page_for_privacy_policy is already 0.' | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In practice, would this logic ever run now? Considering that the
wp_page_for_privacy_policyoption is set to0whenever the policy page is trashed or deleted, this condition would seem to just be here for extreme defensive programming. It doesn't seem necessary though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@westonruter as far as i tested back then it was added so also existing websites where the privacy policy page was deleted it gets catched after the patch has been added. for websites deleting pages after the patch your right this will not be needed.