diff --git a/wp-content/themes/dekart/functions.php b/wp-content/themes/dekart/functions.php index b094efd..edb79f9 100644 --- a/wp-content/themes/dekart/functions.php +++ b/wp-content/themes/dekart/functions.php @@ -4691,6 +4691,7 @@ function dekart_flush_cache_page() require_once __DIR__ . '/inc/schema.php'; require_once __DIR__ . '/inc/webmaster-recrawl.php'; require_once __DIR__ . '/inc/js-snippet-comments.php'; +require_once __DIR__ . '/inc/cli-city-normalizer.php'; /** * Фильтр: замена H1 для страницы "Подготовка к школе" (ID 2) diff --git a/wp-content/themes/dekart/inc/cli-city-normalizer.php b/wp-content/themes/dekart/inc/cli-city-normalizer.php new file mode 100644 index 0000000..bce65b9 --- /dev/null +++ b/wp-content/themes/dekart/inc/cli-city-normalizer.php @@ -0,0 +1,193 @@ + Искомая строка. + * --to= Строка замены. + * [--dry-run] Только показать, ничего не писать. + * [--posts=<0|1>] Обрабатывать wp_posts (по умолчанию 1). + * [--options=<0|1>] Обрабатывать wp_options (по умолчанию 1). + * [--postmeta=<0|1>] Обрабатывать wp_postmeta (по умолчанию 1). + * + * ## EXAMPLES + * + * wp dekart city-replace --from="Щелково" --to="Щёлково" --dry-run + * wp dekart city-replace --from="Щелково" --to="Щёлково" + * + * @when after_wp_load + */ + 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)); + } + } + + /** Значение: если serialized — распаковать/заменить/запаковать, иначе строка. */ + 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); + } + + /** Рекурсия по массивам/объектам, ключ @id пропускается. */ + 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; + } + + /** Строка: замена, но URL и @id не трогаем. */ + 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'); diff --git a/wp-content/themes/dekart/page-contacts.php b/wp-content/themes/dekart/page-contacts.php index aee46e4..1ad8ccb 100644 --- a/wp-content/themes/dekart/page-contacts.php +++ b/wp-content/themes/dekart/page-contacts.php @@ -7,6 +7,7 @@ Template Name: Шаблон страницы Контакты get_header(); $term = get_queried_object(); $post_id = get_the_ID(); +$branch_city = get_option('branch_city') ?: carbon_get_theme_option('site_city'); ?>
@@ -135,7 +136,7 @@ $post_id = get_the_ID();

Как добраться

Мы находимся в офисном центре «Сбербанк» рядом с гипермаркетом «Глобус» — ссылка на карту

- По городу Щелково до остановки «Новый мост»: Автобусы или маршрутные такси 2, 25, 29, 35, 37, 40, 50, 361.

+ По городу до остановки «Новый мост»: Автобусы или маршрутные такси 2, 25, 29, 35, 37, 40, 50, 361.

Из города Фрязино до остановки «Новый мост»: Автобусы или маршрутное такси 49К, 20, 23, 29, 33, 35, 361, 37, 39, 50, 54

Адрес для координат GPS:

@@ -162,7 +163,7 @@ $post_id = get_the_ID();
Как добраться

Мы находимся в офисном центре «Сбербанк» рядом с гипермаркетом «Глобус» — ссылка на карту

- По городу Щелково до остановки «Новый мост»: Автобусы или маршрутные такси 2, 25, 29, 35, 37, 40, 50, 361.

+ По городу до остановки «Новый мост»: Автобусы или маршрутные такси 2, 25, 29, 35, 37, 40, 50, 361.

Из города Фрязино до остановки «Новый мост»: Автобусы или маршрутное такси 49К, 20, 23, 29, 33, 35, 361, 37, 39, 50, 54

Адрес для координат GPS:

diff --git a/wp-content/themes/dekart/template-parts/school/open-day.php b/wp-content/themes/dekart/template-parts/school/open-day.php index 3361835..88c93f7 100644 --- a/wp-content/themes/dekart/template-parts/school/open-day.php +++ b/wp-content/themes/dekart/template-parts/school/open-day.php @@ -3,6 +3,7 @@ if (! defined('ABSPATH')) exit; $open_day_phone = get_option('branch_phone') ?: carbon_get_theme_option('site_phone') ?: ''; +$od_city = get_option('branch_city') ?: carbon_get_theme_option('site_city'); $open_day_clean = $open_day_phone ? preg_replace('/[\s\-\–\(\)]/', '', $open_day_phone) : ''; $od_post_id = get_the_ID(); $od_photo_id = crb_get_post_meta($od_post_id, 'school_open_day_photo'); @@ -48,7 +49,7 @@ $open_day_points = array_values(array_filter((array) $open_day_points));

- Начальные классы школы Декарт в Щелково + Начальные классы школы Декарт в <?php echo esc_attr($od_city); ?>