Skip to content
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

Add data deletion for WPJM User Meta #1430

Merged
merged 1 commit into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions includes/class-wp-job-manager-data-cleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ class WP_Job_Manager_Data_Cleaner {
'assign_job_listing_terms',
);

/**
* User meta key names to be deleted.
*
* @var array $user_meta_keys
*/
private static $user_meta_keys = array(
'_company_logo',
'_company_name',
'_company_website',
'_company_tagline',
'_company_twitter',
'_company_video',
);

/**
* Cleanup all data.
*
Expand All @@ -147,6 +161,7 @@ public static function cleanup_all() {
self::cleanup_pages();
self::cleanup_roles_and_caps();
self::cleanup_transients();
self::cleanup_user_meta();
self::cleanup_options();
self::cleanup_site_options();
}
Expand Down Expand Up @@ -300,4 +315,17 @@ private static function remove_all_job_manager_caps( $object ) {
$object->remove_cap( $cap );
}
}

/**
* Cleanup user meta from the database.
*
* @access private
*/
private static function cleanup_user_meta() {
global $wpdb;

foreach ( self::$user_meta_keys as $meta_key ) {
$wpdb->delete( $wpdb->usermeta, array( 'meta_key' => $meta_key ) );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,50 @@ public function testJobManagerRolesAndCapsDeleted() {
$this->assertNull( $role, 'Employer role should be removed overall' );
}

/**
* Ensure the WPJM user meta are deleted from the DB.
*
* @covers WP_Job_Manager_Data_Cleaner::cleanup_all
* @covers WP_Job_Manager_Data_Cleaner::cleanup_user_meta
*/
public function testCleanupUserMeta() {
$user_id = $this->factory->user->create( array( 'role' => 'author' ) );

$keep_meta_keys = array(
'company_logo',
'company_name',
'company_website',
'company_tagline',
'company_twitter',
'company_video',
);

$remove_meta_keys = array(
'_company_logo',
'_company_name',
'_company_website',
'_company_tagline',
'_company_twitter',
'_company_video',
);

foreach ( array_merge( $keep_meta_keys, $remove_meta_keys ) as $meta_key ) {
update_user_meta( $user_id, $meta_key, 'test_value' );
$this->assertTrue( 'test_value' === get_user_meta( $user_id, $meta_key, true ) );
}

WP_Job_Manager_Data_Cleaner::cleanup_all();
wp_cache_flush();

foreach ( $keep_meta_keys as $meta_key ) {
$this->assertTrue( 'test_value' === get_user_meta( $user_id, $meta_key, true ), sprintf( 'The user meta key "%s" was supposed to be preserved.', $meta_key ) );
}

foreach ( $remove_meta_keys as $meta_key ) {
$this->assertTrue( '' === get_user_meta( $user_id, $meta_key, true ), sprintf( 'The user meta key "%s" was supposed to be removed.', $meta_key ) );
}
}

/* Helper functions. */

private function getPostIdsWithTerm( $term_id, $taxonomy ) {
Expand Down