Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (success)
83.33%
20 / 24
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Uninstaller
82.61% covered (success)
82.61%
19 / 23
33.33% covered (danger)
33.33%
1 / 3
9.43
0.00% covered (danger)
0.00%
0 / 1
 cleanupPluginTransients
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 cleanupPluginOptions
88.89% covered (success)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
4.02
 cleanupCustomFields
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5/**
6 * BeyondWords uninstaller.
7 *
8 * @package Beyondwords\Wordpress
9 * @author  Stuart McAlpine <stu@beyondwords.io>
10 * @since   3.7.0
11 */
12
13namespace Beyondwords\Wordpress\Core;
14
15use Beyondwords\Wordpress\Core\CoreUtils;
16
17/**
18 * BeyondWords uninstaller.
19 *
20 * @since 3.7.0
21 */
22defined('ABSPATH') || exit;
23
24class Uninstaller
25{
26    /**
27     * Clean up (delete) all BeyondWords transients.
28     *
29     * @since 5.0.0
30     *
31     * @return int The number of transients deleted.
32     */
33    public static function cleanupPluginTransients()
34    {
35        global $wpdb;
36
37        // phpcs:ignore WordPress.DB.DirectDatabaseQuery
38        $count = $wpdb->query("DELETE FROM $wpdb->options WHERE `option_name` LIKE '_transient_beyondwords_%'");
39
40        return $count;
41    }
42
43    /**
44     * Clean up (delete) all BeyondWords plugin options.
45     *
46     * @since 3.7.0
47     *
48     * @return int The number of options deleted.
49     */
50    public static function cleanupPluginOptions()
51    {
52        $options = CoreUtils::getOptions('all');
53
54        $total = 0;
55
56        foreach ($options as $option) {
57            if (is_multisite()) {
58                $deleted = delete_site_option($option);
59            } else {
60                $deleted = delete_option($option);
61            }
62
63            if ($deleted) {
64                $total++;
65            }
66        }
67
68        return $total;
69    }
70
71    /**
72     * Clean up (delete) all BeyondWords custom fields.
73     *
74     * @since 3.7.0
75     * @since 4.6.1 Use $wpdb::postmeta variable for table name.
76     *
77     * @return int The number of custom fields deleted.
78     */
79    public static function cleanupCustomFields()
80    {
81        global $wpdb;
82
83        $fields = CoreUtils::getPostMetaKeys('all');
84
85        $total = 0;
86
87        /*
88        * Delete the custom fields one at a time to help prevent very slow
89        * individual MySQL DELETE queries on sites with 1,000s of posts.
90        */
91        foreach ($fields as $field) {
92            $metaIds = $wpdb->get_col($wpdb->prepare("SELECT `meta_id` FROM {$wpdb->postmeta} WHERE `meta_key` = %s;", $field)); // phpcs:ignore
93
94            if (! count($metaIds)) {
95                continue;
96            }
97
98            $count = $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE `meta_id` IN ( " . implode(',', $metaIds) . ' );'); // phpcs:ignore
99
100            if ($count) {
101                $total += $count;
102            }
103        }
104
105        return $total;
106    }
107}