| 1 |
<?php |
| 2 |
namespace epiphyt\Embed_Privacy; |
| 3 |
|
| 4 |
use epiphyt\Embed_Privacy\thumbnail\Thumbnail; |
| 5 |
use RecursiveDirectoryIterator; |
| 6 |
use RecursiveIteratorIterator; |
| 7 |
|
| 8 |
// if uninstall.php is not called by WordPress, die |
| 9 |
if ( ! \defined( 'WP_UNINSTALL_PLUGIN' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
$GLOBALS['options'] = [ |
| 14 |
'embed_privacy_disable_link', |
| 15 |
'embed_privacy_download_thumbnails', |
| 16 |
'embed_privacy_is_migrating', |
| 17 |
'embed_privacy_javascript_detection', |
| 18 |
'embed_privacy_local_tweets', |
| 19 |
'embed_privacy_migrate_version', |
| 20 |
'embed_privacy_migration_count', |
| 21 |
'embed_privacy_preserve_data_on_uninstall', |
| 22 |
]; |
| 23 |
|
| 24 |
if ( \is_multisite() ) { |
| 25 |
$sites = \get_sites( [ |
| 26 |
'fields' => 'ids', |
| 27 |
'number' => 99999, |
| 28 |
] ); |
| 29 |
|
| 30 |
foreach ( $sites as $site_blog_id ) { |
| 31 |
\switch_to_blog( $site_blog_id ); |
| 32 |
|
| 33 |
// do nothing if option says so |
| 34 |
if ( \get_option( 'embed_privacy_preserve_data_on_uninstall' ) ) { |
| 35 |
continue; |
| 36 |
} |
| 37 |
|
| 38 |
delete_data(); |
| 39 |
\restore_current_blog(); |
| 40 |
} |
| 41 |
|
| 42 |
// delete site options |
| 43 |
foreach ( $GLOBALS['options'] as $option ) { |
| 44 |
\delete_site_option( $option ); |
| 45 |
} |
| 46 |
} |
| 47 |
else if ( ! \get_option( 'embed_privacy_preserve_data_on_uninstall' ) ) { |
| 48 |
delete_data(); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Delete all data |
| 53 |
* |
| 54 |
* @since 1.5.0 |
| 55 |
*/ |
| 56 |
function delete_data() { |
| 57 |
global $options; |
| 58 |
|
| 59 |
// delete options |
| 60 |
foreach ( $options as $option ) { |
| 61 |
\delete_option( $option ); |
| 62 |
} |
| 63 |
|
| 64 |
// delete posts of custom post type |
| 65 |
$post_args = [ |
| 66 |
'fields' => 'ids', |
| 67 |
'no_found_rows' => true, |
| 68 |
'post_status' => 'any', |
| 69 |
'post_type' => 'epi_embed', |
| 70 |
'update_post_meta_cache' => false, |
| 71 |
'update_post_term_cache' => false, |
| 72 |
]; |
| 73 |
$embeds = \get_posts( $post_args ); |
| 74 |
|
| 75 |
while ( \count( $embeds ) ) { |
| 76 |
foreach ( $embeds as $embed_id ) { |
| 77 |
\wp_delete_post( $embed_id, true ); |
| 78 |
} |
| 79 |
|
| 80 |
$embeds = \get_posts( $post_args ); |
| 81 |
} |
| 82 |
|
| 83 |
$post_args = [ |
| 84 |
'fields' => 'ids', |
| 85 |
'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 86 |
[ |
| 87 |
'compare' => '!=', |
| 88 |
'compare_key' => 'LIKE', |
| 89 |
'key' => 'embed_privacy_thumbnail_', |
| 90 |
'value' => '', |
| 91 |
], |
| 92 |
], |
| 93 |
'no_found_rows' => true, |
| 94 |
'offset' => 0, |
| 95 |
'post_type' => 'any', |
| 96 |
'update_post_term_cache' => false, |
| 97 |
]; |
| 98 |
$posts = \get_posts( $post_args ); |
| 99 |
|
| 100 |
while ( \count( $posts ) ) { |
| 101 |
foreach ( $posts as $post_id ) { |
| 102 |
$metadata = \get_post_meta( $post_id ); |
| 103 |
|
| 104 |
foreach ( $metadata as $meta_key => $meta_value ) { |
| 105 |
if ( \strpos( $meta_key, 'embed_privacy_thumbnail_' ) === false ) { |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
\delete_post_meta( $post_id, $meta_key ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
$post_args['offset'] += 5; |
| 114 |
$posts = \get_posts( $post_args ); |
| 115 |
} |
| 116 |
|
| 117 |
require_once __DIR__ . '/inc/thumbnail/class-thumbnail.php'; |
| 118 |
|
| 119 |
// delete thumbnail directory |
| 120 |
delete_directory( Thumbnail::get_directory()['base_dir'] ); |
| 121 |
// delete old thumbnail directory |
| 122 |
delete_directory( WP_CONTENT_DIR . '/uploads/embed-privacy' ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Delete a directory recursively. |
| 127 |
* |
| 128 |
* @since 1.7.3 |
| 129 |
* |
| 130 |
* @param string $directory The directory to delete |
| 131 |
*/ |
| 132 |
function delete_directory( $directory ) { |
| 133 |
if ( ! \file_exists( $directory ) ) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
$iterator = new RecursiveDirectoryIterator( $directory, RecursiveDirectoryIterator::SKIP_DOTS ); |
| 138 |
$files = new RecursiveIteratorIterator( $iterator, RecursiveIteratorIterator::CHILD_FIRST ); |
| 139 |
|
| 140 |
foreach ( $files as $file ) { |
| 141 |
if ( $file->isDir() ) { |
| 142 |
\rmdir( $file->getRealPath() ); |
| 143 |
} |
| 144 |
else { |
| 145 |
\unlink( $file->getRealPath() ); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
\rmdir( $directory ); |
| 150 |
} |
| 151 |
|