-
Notifications
You must be signed in to change notification settings - Fork 1
/
uninstall.php
70 lines (61 loc) · 1.7 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
<?php
/**
* Plugin uninstall handler.
*/
declare( strict_types = 1 );
namespace Imageshop\WordPress\Uninstall;
if ( ! \defined( 'WP_UNINSTALL_PLUGIN' ) ) {
die();
}
global $wpdb;
// Remove dangling database entries after the plugin has been removed.
$attachments = $wpdb->get_results(
"
SELECT
DISTINCT( p.ID )
FROM
{$wpdb->posts} AS p
LEFT JOIN
{$wpdb->postmeta} AS pm
ON (p.ID = pm.post_id)
WHERE
p.post_type = 'attachment'
AND
(
pm.meta_key = '_imageshop_document_id'
AND
(
pm.meta_value IS NOT NULL
OR
pm.meta_value != ''
)
)
AND
NOT EXISTS (
SELECT
1
FROM
{$wpdb->postmeta} as spm
WHERE
spm.post_id = p.ID
AND
spm.meta_key = '_wp_attached_file'
AND
(
spm.meta_value IS NOT NULL
AND
spm.meta_value != ''
)
)
"
);
if ( empty( $attachments ) ) {
return;
}
$removable = array();
foreach ( $attachments as $attachment ) {
$removable[] = \absint( $attachment->ID );
}
$wpdb->query( "DELETE FROM {$wpdb->posts} WHERE ID IN (" . \implode( ',', $removable ) . ')' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- We need to implode a variable to use the `IN` SQL operator.
$wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE post_id IN (" . \implode( ',', $removable ) . ')' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- We need to implode a variable to use the `IN` SQL operator.
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '%imageshop%'" ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- We are doing a non-variable query.