-
Notifications
You must be signed in to change notification settings - Fork 328
Closed
Labels
P0High priorityHigh priorityPHPTeam SIssues for Squad 1Issues for Squad 1Type: EnhancementImprovement of an existing featureImprovement of an existing feature
Description
Feature Description
- Schedule a monthly event using wp_schedule_event with monthly recurrence
- Clear previous CPT posts older than 6 months
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- A recurring cron action
googlesitekit_email_reporting_cleanupis registered to run monthly. - The cleanup cron is scheduled whenever the email reporting feature is enabled, and unscheduled whenever the feature is disabled or the plugin is uninstalled/reset.
- The cleanup handler deletes all
googlesitekit_email_logposts older than six months (based on post date), leaving newer logs untouched.
Implementation Brief
-
Extend
Email_Reporting_Schedulerclass- Add
const ACTION_CLEANUP = 'googlesitekit_email_reporting_cleanup';. - Provide
schedule_cleanup()that checkswp_next_scheduled( self::ACTION_CLEANUP )and, if not set, registers a recurring job viawp_schedule_event( time(), 'monthly', self::ACTION_CLEANUP );. - In
unschedule_allmethod callwp_unschedule_hook( self::ACTION_CLEANUP )
- Add
-
Update
Google\Site_Kit\Core\Email_Reporting\Email_Reporting- In
register()- Instantiate
Email_Log_Cleanup, and hook intoEmail_Reporting_Scheduler::ACTION_CLEANUPand invokehandle_cleanup_actionmethod ofEmail_Log_Cleanupclass as callback. - Update the existing enable/disable hooks so that when the feature is enabled, both initiators and the cleanup event are scheduled; when disabled, call
wp_unschedule_hook( self::ACTION_CLEANUP )- or instead invokeEmail_Reporting_Scheduler:: unschedule_all()if not implemented already.
- Instantiate
- In
-
Add the cleanup action to
Uninstallation::SCHEDULED_EVENTSso uninstall wipes it automatically. -
Add
Core\Email_Reporting\Email_Log_Cleanupclass- Implement
handle_cleanup_action():- Early return if the feature is currently disabled (
Email_Reporting_Settings::is_email_reporting_enabled()). - Use
WP_QueryonEmail_Log::POST_TYPEwithdate_queryset to posts older than six months ('before' => gmdate( 'Y-m-d', time() - 6 * MONTH_IN_SECONDS )),post_statusany,fields => 'ids',posts_per_page => -1, disable meta/term cache. - For each ID returned, call
wp_delete_post( $post_id, true )to permanently remove it. - Optionally, guard against huge batches by chunking the deletions (e.g. use
array_chunkof 100 IDs).
- Early return if the feature is currently disabled (
- Implement
Test Coverage
- Update existing tests for
Email_Reporting_Schedulerto test new methods - Add tests for
Email_Log_Cleanup::handle_cleanup_action- Seed
googlesitekit_email_logposts with varying ages; after running the handler, assert that posts older than six months are deleted and newer ones remain.
- Seed
QA Brief
- Enable the feature flag and install a plugin that lets you see WP scheduled events (like WP Crontrol).
- Toggle the feature in the admin settings, when the feature is enabled you should see a monthly registered event
googlesitekit_email_reporting_cleanup.
Changelog entry
- Implement email reporting log clear schedulers
Metadata
Metadata
Assignees
Labels
P0High priorityHigh priorityPHPTeam SIssues for Squad 1Issues for Squad 1Type: EnhancementImprovement of an existing featureImprovement of an existing feature