Repository navigation
Clean up data left behind by the old OpenID Connect Generic plugin #224
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Daily background task (WP-Cron) that deletes data left behind by the old OpenID Connect Generic plugin. | ||
| * | ||
| * It keeps running rather than running once, so the data is also removed when it comes back, | ||
| * e.g. from a restored backup or a database pushed from another environment. | ||
| */ | ||
| class BCC_Login_Cleanup { | ||
| const CRON_HOOK = 'bcc_login_cleanup'; | ||
|
|
||
| function __construct() { | ||
| add_action( self::CRON_HOOK, array( __CLASS__, 'run' ) ); | ||
|
|
||
| // WordPress doesn't run activation hooks on plugin updates, | ||
| // so the task is scheduled by the first request that loads this code. | ||
| add_action( 'init', array( $this, 'schedule' ) ); | ||
| } | ||
|
|
||
| function schedule() { | ||
| if ( ! wp_next_scheduled( self::CRON_HOOK ) ) { | ||
| wp_schedule_event( time(), 'daily', self::CRON_HOOK ); | ||
| } | ||
| } | ||
|
|
||
| static function unschedule() { | ||
| wp_clear_scheduled_hook( self::CRON_HOOK ); | ||
| } | ||
|
|
||
| static function run() { | ||
| if ( false === self::delete_legacy_user_meta() ) { | ||
| error_log( 'BCC Login: Could not delete the user meta of the OpenID Connect Generic plugin, retrying tomorrow.' ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Deletes the tokens and claims that the old OpenID Connect Generic plugin stored as user meta. | ||
| * | ||
| * @return int|false The number of deleted rows, or false if not all of them could be deleted. | ||
| */ | ||
| static function delete_legacy_user_meta() { | ||
| global $wpdb; | ||
|
|
||
| $legacy_meta_keys = $wpdb->get_results( | ||
| "SELECT meta_key, COUNT(*) AS count | ||
| FROM {$wpdb->usermeta} | ||
| WHERE meta_key LIKE 'openid-connect-generic%' | ||
| GROUP BY meta_key" | ||
| ); | ||
|
|
||
| $deleted = 0; | ||
| foreach ( (array) $legacy_meta_keys as $legacy_meta_key ) { | ||
| // Unlike a plain DELETE query, this also clears the cached meta of the users. | ||
| delete_metadata( 'user', 0, $legacy_meta_key->meta_key, '', true ); | ||
| $deleted += (int) $legacy_meta_key->count; | ||
| } | ||
|
|
||
| $remaining = $wpdb->get_var( | ||
| "SELECT COUNT(*) FROM {$wpdb->usermeta} WHERE meta_key LIKE 'openid-connect-generic%'" | ||
| ); | ||
|
|
||
| // Null means the query failed. | ||
| if ( null === $remaining || (int) $remaining > 0 ) { | ||
| return false; | ||
| } | ||
|
|
||
| return $deleted; | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.