shkola/wp-content/themes/dekart/inc/cli-city-normalizer.php
2026-10-08 16:59:02 +00:00

176 lines
5.9 KiB
PHP

<?php
if (! defined('WP_CLI') || ! WP_CLI) {
return;
}
class Dekart_City_Replace {
/** @var string */
private $from = '';
/** @var string */
private $to = '';
/** @var bool */
private $dry = false;
/** @var int */
private $changed = 0;
public function __invoke($args, $assoc_args) {
$this->from = isset($assoc_args['from']) ? (string) $assoc_args['from'] : '';
$this->to = isset($assoc_args['to']) ? (string) $assoc_args['to'] : '';
if ('' === $this->from || '' === $this->to) {
WP_CLI::error('Укажите --from=... и --to=...');
}
if ($this->from === $this->to) {
WP_CLI::error('--from и --to совпадают — нечего менять');
}
$this->dry = isset($assoc_args['dry-run']);
$do_posts = ! isset($assoc_args['posts']) || '0' !== (string) $assoc_args['posts'];
$do_opts = ! isset($assoc_args['options']) || '0' !== (string) $assoc_args['options'];
$do_meta = ! isset($assoc_args['postmeta']) || '0' !== (string) $assoc_args['postmeta'];
WP_CLI::log(sprintf('%s «%s» → «%s»', $this->dry ? '[DRY-RUN]' : '[APPLY]', $this->from, $this->to));
if ($do_posts) {
$this->process_posts();
}
if ($do_opts) {
$this->process_options();
}
if ($do_meta) {
$this->process_postmeta();
}
WP_CLI::success(sprintf('%s. Изменено записей: %d', $this->dry ? 'DRY-RUN завершён' : 'Готово', $this->changed));
}
private function process_posts(): void {
global $wpdb;
$table = $wpdb->posts;
$like = '%' . $wpdb->esc_like($this->from) . '%';
$rows = $wpdb->get_results($wpdb->prepare(
"SELECT ID, post_content, post_title FROM {$table} WHERE post_content LIKE BINARY %s OR post_title LIKE BINARY %s",
$like, $like
));
foreach ($rows as $row) {
$new_content = $this->replace_value($row->post_content);
$new_title = $this->replace_value($row->post_title);
if ($new_content === $row->post_content && $new_title === $row->post_title) {
continue;
}
$this->changed++;
if ($this->dry) {
WP_CLI::log(sprintf(' posts #%d %s', $row->ID, $new_title !== $row->post_title ? '(title)' : '(content)'));
continue;
}
$wpdb->update($table, array('post_content' => $new_content, 'post_title' => $new_title), array('ID' => $row->ID));
}
}
private function process_options(): void {
global $wpdb;
$table = $wpdb->options;
$like = '%' . $wpdb->esc_like($this->from) . '%';
$rows = $wpdb->get_results($wpdb->prepare(
"SELECT option_id, option_name, option_value FROM {$table} WHERE option_value LIKE BINARY %s",
$like
));
foreach ($rows as $row) {
$new = $this->replace_value($row->option_value);
if ($new === $row->option_value) {
continue;
}
$this->changed++;
if ($this->dry) {
WP_CLI::log(sprintf(' options %s', $row->option_name));
continue;
}
$wpdb->update($table, array('option_value' => $new), array('option_id' => $row->option_id));
}
}
private function process_postmeta(): void {
global $wpdb;
$table = $wpdb->postmeta;
$like = '%' . $wpdb->esc_like($this->from) . '%';
$rows = $wpdb->get_results($wpdb->prepare(
"SELECT meta_id, post_id, meta_key, meta_value FROM {$table} WHERE meta_value LIKE BINARY %s",
$like
));
foreach ($rows as $row) {
$new = $this->replace_value($row->meta_value);
if ($new === $row->meta_value) {
continue;
}
$this->changed++;
if ($this->dry) {
WP_CLI::log(sprintf(' postmeta #%d %s', $row->post_id, $row->meta_key));
continue;
}
$wpdb->update($table, array('meta_value' => $new), array('meta_id' => $row->meta_id));
}
}
private function replace_value($value) {
if (! is_string($value) || '' === $value) {
return $value;
}
if (is_serialized($value)) {
$data = @unserialize($value);
if (false === $data && 'b:0;' !== $value) {
return $this->replace_string($value);
}
return serialize($this->replace_recursive($data));
}
return $this->replace_string($value);
}
private function replace_recursive($data) {
if (is_array($data)) {
$out = array();
foreach ($data as $k => $v) {
if ('@id' === $k) {
$out[$k] = $v;
continue;
}
$out[$k] = $this->replace_recursive($v);
}
return $out;
}
if (is_object($data)) {
foreach (get_object_vars($data) as $k => $v) {
if ('@id' === $k) {
continue;
}
$data->$k = $this->replace_recursive($v);
}
return $data;
}
if (is_string($data)) {
return $this->replace_string($data);
}
return $data;
}
private function replace_string($s) {
if (! is_string($s) || '' === $s || false === strpos($s, $this->from)) {
return $s;
}
if (preg_match('#^\s*(https?:)?//#i', $s)) {
return $s; // URL целиком
}
if (false !== strpos($s, '@id')) {
return $s; // разметка @id
}
return str_replace($this->from, $this->to, $s);
}
}
WP_CLI::add_command('dekart city-replace', 'Dekart_City_Replace');