| 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 |
*/ |
| 11 |
public function wipe(bool $private = false): bool |
| 12 |
{ |
| 13 |
global $wpdb; |
| 14 |
|
| 15 |
if ($private) { |
| 16 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Bulk delete has no WP API equivalent; cache is flushed below. |
| 17 |
$result = $wpdb->query( |
| 18 |
$wpdb->prepare( |
| 19 |
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", |
| 20 |
'metricool_%', |
| 21 |
'_metricool_%' |
| 22 |
) |
| 23 |
); |
| 24 |
} else { |
| 25 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Bulk delete has no WP API equivalent; cache is flushed below. |
| 26 |
$result = $wpdb->query( |
| 27 |
$wpdb->prepare( |
| 28 |
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", |
| 29 |
'metricool_%' |
| 30 |
) |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
// Make sure deleted options are not cached |
| 35 |
if (function_exists('wp_cache_flush')) { |
| 36 |
wp_cache_flush(); |
| 37 |
} |
| 38 |
|
| 39 |
return $result !== false; |
| 40 |
} |
| 41 |
} |
| 42 |
|