| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 4 |
|
| 5 |
class WP_Optimization_orphandata extends WP_Optimization { |
| 6 |
|
| 7 |
public $ui_sort_order = 10000; |
| 8 |
|
| 9 |
public $available_for_auto = true; |
| 10 |
|
| 11 |
public $available_for_saving = true; |
| 12 |
|
| 13 |
public $support_preview = false; |
| 14 |
|
| 15 |
/** |
| 16 |
* Do actions after optimize() function. |
| 17 |
*/ |
| 18 |
public function after_optimize() { |
| 19 |
// translators: %s - number of deleted orphaned relationship data |
| 20 |
$message = sprintf(_n('%s orphaned relationship data deleted', '%s orphaned relationship data deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count)); |
| 21 |
|
| 22 |
if ($this->is_multisite_mode()) { |
| 23 |
// translators: %s - number of sites |
| 24 |
$message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 25 |
} |
| 26 |
|
| 27 |
$this->logger->info($message); |
| 28 |
$this->register_output($message); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Do optimization. |
| 33 |
*/ |
| 34 |
public function optimize() { |
| 35 |
$clean = "DELETE FROM `" . $this->wpdb->term_relationships . "` WHERE term_taxonomy_id=1 AND object_id NOT IN (SELECT id FROM `" . $this->wpdb->posts . "`);"; |
| 36 |
|
| 37 |
$orphandata = $this->query($clean); |
| 38 |
$this->processed_count += $orphandata; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Do actions after get_info() function. |
| 43 |
*/ |
| 44 |
public function after_get_info() { |
| 45 |
if ($this->found_count > 0) { |
| 46 |
// translators: %s - number of orphaned relationship data |
| 47 |
$message = sprintf(_n('%s orphaned relationship data in your database', '%s orphaned relationship data in your database', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count)); |
| 48 |
} else { |
| 49 |
$message = __('No orphaned relationship data in your database', 'wp-optimize'); |
| 50 |
} |
| 51 |
|
| 52 |
if ($this->is_multisite_mode()) { |
| 53 |
// translators: %s - number of sites |
| 54 |
$message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 55 |
} |
| 56 |
|
| 57 |
$this->register_output($message); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get count of unoptimized items. |
| 62 |
*/ |
| 63 |
public function get_info() { |
| 64 |
$sql = "SELECT COUNT(*) FROM `{$this->wpdb->term_relationships}` WHERE term_taxonomy_id=%d AND object_id NOT IN (SELECT id FROM `{$this->wpdb->posts}`);"; |
| 65 |
$orphandata = $this->wpdb->get_var($this->wpdb->prepare($sql, 1)); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- `$this->wpdb` is `$wpdb` |
| 66 |
|
| 67 |
$this->found_count += $orphandata; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Returns settings label |
| 72 |
* |
| 73 |
* @return string |
| 74 |
*/ |
| 75 |
public function settings_label() { |
| 76 |
return __('Clean orphaned relationship data', 'wp-optimize'); |
| 77 |
} |
| 78 |
} |
| 79 |
|