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