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