Skip to content

Extend the Reset utility class to delete custom post type posts #11545

@benbowler

Description

@benbowler

Feature Description

When the plugin is reset, delete all posts created within each registered custom post type


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • When the plugin is reset, all posts belonging to the custom post type and related post meta registered by Site Kit are deleted.

Implementation Brief

  • Update includes/Core/Util/Reset.php
    • Add delete_posts method
      • Accepts a $scope argument.
      • When $scope is network:
        • Retrieve all sites using get_sites (following the established pattern used in other reset methods).
      • Otherwise:
        • Use only the current site ID via get_current_blog_id().
      • Loop over the collected site IDs and for each:
        • Query and delete posts of type googlesitekit_email_log and it's related post meta values.
      • Use direct wpdb queries for performance instead of core wrapper functions.
    • Invoke delete_posts in all method passing correct scope for both single and multisite

Test Coverage

  • Update tests/phpunit/integration/Core/Util/ResetTest.php to verify that posts of type googlesitekit_email_log are deleted on reset

QA Brief

QA:Eng required as developer access required to create test Email_Log posts currently.

  • Enable the proactiveUserEngagement feature flag.
  • Edit the Email Log post type definition so that it shows in the WP Ui.
		register_post_type(
			self::POST_TYPE,
			array(
				'label'        => 'Email Log',
				'labels'       => array(
					'name'                  => 'Email Log',
					'singular_name'         => 'Email Log',
					'menu_name'             => 'Email Log',
					'all_items'             => 'All Email Logs',
					'view_item'             => 'View Email Log',
					'add_new_item'          => 'Add New Email Log',
					'edit_item'             => 'Edit Email Log',
					'update_item'           => 'Update Email Log',
					'search_items'          => 'Search Email Logs',
					'not_found'             => 'No email logs found',
					'not_found_in_trash'    => 'No email logs found in trash',
				),
				'public'       => true,
				'map_meta_cap' => true,
				'rewrite'      => false,
				'query_var'    => false,
			)
		);
  • Update this section:
  • register_post_status(
    $status,
    array(
    'public' => false,
    'internal' => true,
    'exclude_from_search' => true,
    'show_in_admin_all_list' => false,
    'show_in_admin_status_list' => false,
    )
    );
  • To the following (this is so you can see posts with our custom statuses in the admin):
			register_post_status(
				$status,
				array(
					'public'                    => true,
					'internal'                  => false,
					'exclude_from_search'       => true,
					'show_in_admin_all_list'    => true,
					'show_in_admin_status_list' => true,
				)
			);
  • Create an Email Log post using this code somewhere in the plugin (it's important to use this code because the admin can't create posts with our custom statuses without more significant customisation of plugin JS).
$post_id = wp_insert_post(
    array(
        'post_type'   => \Google\Site_Kit\Core\Email_Reporting\Email_Log::POST_TYPE,
        'post_status' => \Google\Site_Kit\Core\Email_Reporting\Email_Log::STATUS_SENT,
        'post_title'  => 'Test Email Log',
        'meta_input'  => array(
            \Google\Site_Kit\Core\Email_Reporting\Email_Log::META_REPORT_FREQUENCY => 'weekly',
            \Google\Site_Kit\Core\Email_Reporting\Email_Log::META_BATCH_ID         => 'batch-123',
            \Google\Site_Kit\Core\Email_Reporting\Email_Log::META_SEND_ATTEMPTS    => 1,
        ),
    )
);
  • Reset the plugin - see that the posts you created have been deleted.

Changelog entry

  • Extend the Reset utility class to delete remaining error reporting logs

Metadata

Metadata

Assignees

No one assigned

    Labels

    P0High priorityQA: EngRequires specialized QA by an engineerTeam 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