-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add uninstall.php which deletes all URL Metrics posts
- Loading branch information
1 parent
21a5cec
commit a52e0ce
Showing
4 changed files
with
212 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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,45 @@ | ||
<?php | ||
/** | ||
* Plugin uninstaller logic. | ||
* | ||
* @package optimization-detective | ||
* @since 0.1.0 | ||
*/ | ||
|
||
// If uninstall.php is not called by WordPress, bail. | ||
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { | ||
exit; | ||
} | ||
|
||
require_once __DIR__ . '/class-od-url-metrics-post-type.php'; | ||
|
||
// Delete all URL Metrics posts for the current site. | ||
OD_URL_Metrics_Post_Type::delete_all_posts(); | ||
|
||
/* | ||
* For a multisite, delete the URL Metrics for all other sites (however limited to 100 sites to avoid memory limit or | ||
* timeout problems in large scale networks). | ||
*/ | ||
if ( is_multisite() ) { | ||
$site_ids = get_sites( | ||
array( | ||
'fields' => 'ids', | ||
'number' => 100, | ||
'update_site_cache' => false, | ||
'update_site_meta_cache' => false, | ||
) | ||
); | ||
|
||
// Skip iterating over self. | ||
$site_ids = array_diff( | ||
$site_ids, | ||
array( get_current_blog_id() ) | ||
); | ||
|
||
// Delete all other blogs' URL Metrics posts. | ||
foreach ( $site_ids as $site_id ) { | ||
switch_to_blog( $site_id ); | ||
OD_URL_Metrics_Post_Type::delete_all_posts(); | ||
restore_current_blog(); | ||
} | ||
} |
This file contains 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 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,29 @@ | ||
<?php | ||
/** | ||
* Tests for optimization-detective module uninstall.php. | ||
* | ||
* @runInSeparateProcess | ||
* @package optimization-detective | ||
*/ | ||
|
||
class OD_Uninstall_Tests extends WP_UnitTestCase { | ||
|
||
/** | ||
* Make sure post deletion is happening. | ||
*/ | ||
public function test_post_deletion() { | ||
// Mock uninstall const. | ||
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { | ||
define( 'WP_UNINSTALL_PLUGIN', 'Yes' ); | ||
} | ||
|
||
$post_id = self::factory()->post->create(); | ||
$url_metrics_post_id = self::factory()->post->create( array( 'post_type' => OD_URL_Metrics_Post_Type::SLUG ) ); | ||
|
||
require __DIR__ . '/../../../plugins/optimization-detective/uninstall.php'; | ||
wp_cache_flush(); | ||
|
||
$this->assertInstanceOf( WP_Post::class, get_post( $post_id ) ); | ||
$this->assertNull( get_post( $url_metrics_post_id ) ); | ||
} | ||
} |