| 1 |
<?php |
| 2 |
|
| 3 |
namespace Metricool\Services; |
| 4 |
|
| 5 |
class OptionsService |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Delete all plugin options from the wp_options table |
| 9 |
* @param bool $private Whether to delete private options (prefixed with _) |
| 10 |
* @param string[] $exclude Exclude specific options from deletion |
| 11 |
*/ |
| 12 |
public function wipe(bool $private = false, array $exclude = []): bool |
| 13 |
{ |
| 14 |
global $wpdb; |
| 15 |
|
| 16 |
$excludeSql = ''; |
| 17 |
foreach ($exclude as $optionName) { |
| 18 |
$excludeSql .= $wpdb->prepare(' AND option_name != %s', $optionName); |
| 19 |
} |
| 20 |
|
| 21 |
if ($private) { |
| 22 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- Bulk delete has no WP API equivalent; cache is flushed below. Preserve clause is prepared above. |
| 23 |
$result = $wpdb->query( |
| 24 |
$wpdb->prepare( |
| 25 |
"DELETE FROM {$wpdb->options} WHERE (option_name LIKE %s OR option_name LIKE %s)", |
| 26 |
'metricool_%', |
| 27 |
'_metricool_%' |
| 28 |
) . $excludeSql |
| 29 |
); |
| 30 |
} else { |
| 31 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- Bulk delete has no WP API equivalent; cache is flushed below. Preserve clause is prepared above. |
| 32 |
$result = $wpdb->query( |
| 33 |
$wpdb->prepare( |
| 34 |
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", |
| 35 |
'metricool_%' |
| 36 |
) . $excludeSql |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
// Make sure deleted options are not cached |
| 41 |
if (function_exists('wp_cache_flush')) { |
| 42 |
wp_cache_flush(); |
| 43 |
} |
| 44 |
|
| 45 |
return $result !== false; |
| 46 |
} |
| 47 |
} |
| 48 |
|