| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WPO_VERSION')) die('No direct access allowed'); |
| 4 |
|
| 5 |
class WP_Optimization_usermeta extends WP_Optimization { |
| 6 |
|
| 7 |
public $ui_sort_order = 8001; |
| 8 |
|
| 9 |
public $available_for_auto = true; |
| 10 |
|
| 11 |
public $available_for_saving = true; |
| 12 |
|
| 13 |
public $auto_default = false; |
| 14 |
|
| 15 |
/** |
| 16 |
* Prepare data for preview widget. |
| 17 |
* |
| 18 |
* @param array $params |
| 19 |
* |
| 20 |
* @return array |
| 21 |
*/ |
| 22 |
public function preview($params) { |
| 23 |
// get data requested for preview. |
| 24 |
// `$this->wpdb->prepare` is global `$wpdb->prepare` |
| 25 |
// phpcs:disable |
| 26 |
$sql = $this->wpdb->prepare( |
| 27 |
"SELECT um.* FROM `" . $this->wpdb->usermeta . "` um". |
| 28 |
" LEFT JOIN `" . $this->wpdb->users . "` wu ON wu.ID = um.user_id". |
| 29 |
" WHERE wu.ID IS NULL". |
| 30 |
" ORDER BY um.umeta_id LIMIT %d, %d;", |
| 31 |
array( |
| 32 |
$params['offset'], |
| 33 |
$params['limit'], |
| 34 |
) |
| 35 |
); |
| 36 |
|
| 37 |
$users = $this->wpdb->get_results($sql, ARRAY_A); |
| 38 |
// phpcs:enable |
| 39 |
|
| 40 |
// get total count user meta for optimization. |
| 41 |
$sql = "SELECT COUNT(*) FROM `" . $this->wpdb->usermeta . "` um LEFT JOIN `" . $this->wpdb->users . "` wu ON wu.ID = um.user_id WHERE wu.ID IS NULL;"; |
| 42 |
|
| 43 |
$total = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- safe, no user input used |
| 44 |
|
| 45 |
return array( |
| 46 |
'id_key' => 'umeta_id', |
| 47 |
'columns' => array( |
| 48 |
'umeta_id' => __('ID', 'wp-optimize'), |
| 49 |
'user_id' => __('User ID', 'wp-optimize'), |
| 50 |
'meta_key' => __('Meta Key', 'wp-optimize'), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- This is not a query |
| 51 |
'meta_value' => __('Meta Value', 'wp-optimize'), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- This is not a query |
| 52 |
), |
| 53 |
'offset' => $params['offset'], |
| 54 |
'limit' => $params['limit'], |
| 55 |
'total' => $total, |
| 56 |
'data' => $this->htmlentities_array($users, array('ID')), |
| 57 |
'message' => $total > 0 ? '' : __('No orphaned user meta data in your database', 'wp-optimize'), |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Do actions after optimize() function. |
| 63 |
*/ |
| 64 |
public function after_optimize() { |
| 65 |
// translators: %s is the number of orphaned user metadata deleted |
| 66 |
$message = sprintf(_n('%s orphaned user meta data deleted', '%s orphaned user meta data deleted', $this->processed_count, 'wp-optimize'), number_format_i18n($this->processed_count)); |
| 67 |
|
| 68 |
if ($this->is_multisite_mode()) { |
| 69 |
// translators: %s is the number sites |
| 70 |
$message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 71 |
} |
| 72 |
|
| 73 |
$this->logger->info($message); |
| 74 |
$this->register_output($message); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Do optimization. |
| 79 |
*/ |
| 80 |
public function optimize() { |
| 81 |
$clean = "DELETE um FROM `" . $this->wpdb->usermeta . "` um LEFT JOIN `" . $this->wpdb->users . "` wu ON wu.ID = um.user_id WHERE wu.ID IS NULL"; |
| 82 |
|
| 83 |
// if posted ids in params, then remove only selected items. used by preview widget. |
| 84 |
if (isset($this->data['ids'])) { |
| 85 |
$clean .= ' AND um.umeta_id in ('.join(',', $this->data['ids']).')'; |
| 86 |
} |
| 87 |
|
| 88 |
$clean .= ";"; |
| 89 |
|
| 90 |
$usermeta = $this->query($clean); |
| 91 |
$this->processed_count += $usermeta; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Do actions after get_info() function. |
| 96 |
*/ |
| 97 |
public function after_get_info() { |
| 98 |
if ($this->found_count) { |
| 99 |
// translators: %s is the number of orphaned user metadata |
| 100 |
$message = sprintf(_n('%s orphaned user meta data in your database', '%s orphaned user meta data in your database', $this->found_count, 'wp-optimize'), number_format_i18n($this->found_count)); |
| 101 |
} else { |
| 102 |
$message = __('No orphaned user meta data in your database', 'wp-optimize'); |
| 103 |
} |
| 104 |
|
| 105 |
if ($this->is_multisite_mode()) { |
| 106 |
// translators: %s is the number sites |
| 107 |
$message .= ' ' . sprintf(_n('across %s site', 'across %s sites', count($this->blogs_ids), 'wp-optimize'), count($this->blogs_ids)); |
| 108 |
} |
| 109 |
|
| 110 |
// add preview link to message. |
| 111 |
if ($this->found_count > 0) { |
| 112 |
$message = $this->get_preview_link($message); |
| 113 |
} |
| 114 |
|
| 115 |
$this->register_output($message); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get count of unoptimized items. |
| 120 |
*/ |
| 121 |
public function get_info() { |
| 122 |
$sql = "SELECT COUNT(*) FROM `" . $this->wpdb->usermeta . "` um LEFT JOIN `" . $this->wpdb->users . "` wu ON wu.ID = um.user_id WHERE wu.ID IS NULL;"; |
| 123 |
$usermeta = $this->wpdb->get_var($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- safe, no user input used |
| 124 |
|
| 125 |
$this->found_count += $usermeta; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Get settings label. |
| 130 |
*/ |
| 131 |
public function settings_label() { |
| 132 |
return __('Clean user meta data', 'wp-optimize'); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get auto option description. |
| 137 |
*/ |
| 138 |
public function get_auto_option_description() { |
| 139 |
return __('Remove orphaned user meta', 'wp-optimize'); |
| 140 |
} |
| 141 |
} |
| 142 |
|