| 1 |
<?php |
| 2 |
|
| 3 |
declare(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 |
|
| 13 |
namespace Beyondwords\Wordpress\Core; |
| 14 |
|
| 15 |
use Beyondwords\Wordpress\Core\CoreUtils; |
| 16 |
|
| 17 |
/** |
| 18 |
* BeyondWords uninstaller. |
| 19 |
* |
| 20 |
* @since 3.7.0 |
| 21 |
*/ |
| 22 |
defined('ABSPATH') || exit; |
| 23 |
|
| 24 |
class 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 |
} |
| 108 |
|