Skip to content

Implement log clear schedulers #11582

@zutigrm

Description

@zutigrm

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_cleanup is 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_log posts older than six months (based on post date), leaving newer logs untouched.

Implementation Brief

  • Extend Email_Reporting_Scheduler class

    • Add const ACTION_CLEANUP = 'googlesitekit_email_reporting_cleanup';.
    • Provide schedule_cleanup() that checks wp_next_scheduled( self::ACTION_CLEANUP ) and, if not set, registers a recurring job via wp_schedule_event( time(), 'monthly', self::ACTION_CLEANUP );.
    • In unschedule_all method call wp_unschedule_hook( self::ACTION_CLEANUP )
  • Update Google\Site_Kit\Core\Email_Reporting\Email_Reporting

    • In register()
      • Instantiate Email_Log_Cleanup, and hook into Email_Reporting_Scheduler::ACTION_CLEANUP and invoke handle_cleanup_action method of Email_Log_Cleanup class 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 invoke Email_Reporting_Scheduler:: unschedule_all() if not implemented already.
  • Add the cleanup action to Uninstallation::SCHEDULED_EVENTS so uninstall wipes it automatically.

  • Add Core\Email_Reporting\Email_Log_Cleanup class

    • Implement handle_cleanup_action():
      • Early return if the feature is currently disabled (Email_Reporting_Settings::is_email_reporting_enabled()).
      • Use WP_Query on Email_Log::POST_TYPE with date_query set to posts older than six months ('before' => gmdate( 'Y-m-d', time() - 6 * MONTH_IN_SECONDS )), post_status any, 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_chunk of 100 IDs).

Test Coverage

  • Update existing tests for Email_Reporting_Scheduler to test new methods
  • Add tests for Email_Log_Cleanup::handle_cleanup_action
    • Seed googlesitekit_email_log posts with varying ages; after running the handler, assert that posts older than six months are deleted and newer ones remain.

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

No one assigned

    Labels

    P0High priorityPHPTeam SIssues for Squad 1Type: EnhancementImprovement of an existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions