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