fix(pksh): parse external platforms from raw CF meta (any key layout)

This commit is contained in:
Dekart Deploy Bot 2026-10-12 18:01:33 +00:00
parent 7823f1e406
commit 5a5f7c73e2
2 changed files with 90 additions and 17 deletions

View File

@ -5934,6 +5934,94 @@ function dekart_branch_add_menu()
add_action('admin_init', 'dekart_branch_register_settings');
// Внешние площадки ПКШ: собираем из сырой меты (любая схема ключей CF)
function dekart_pksh_external_platforms(int $post_id): array
{
if (! $post_id) {
return array();
}
global $wpdb;
$like = $wpdb->esc_like('_pksh_external_platforms') . '%';
$rows = $wpdb->get_results($wpdb->prepare(
"SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key LIKE %s",
$post_id,
$like
));
if (empty($rows)) {
return array();
}
$sub_names = array( 'name', 'icon', 'rating', 'url' ); // порядок подполей в CF
$items = array();
foreach ($rows as $r) {
$parts = explode('|', (string) $r->meta_key);
array_shift($parts); // имя поля
if (end($parts) === 'value') {
array_pop($parts); // хвост 'value'
}
if (empty($parts)) {
continue;
}
// строка = первый числовой сегмент
$row_pos = null;
foreach ($parts as $i => $seg) {
if (ctype_digit((string) $seg)) {
$row_pos = $i;
break;
}
}
if (null === $row_pos) {
continue;
}
$row = (int) $parts[ $row_pos ];
$rest = $parts;
unset($rest[ $row_pos ]);
// имя подполя: известное имя сегмента или числовой индекс по порядку
$sub = '';
foreach ($rest as $seg) {
$cand = preg_replace('/^platform_/', '', (string) $seg);
if (in_array($cand, $sub_names, true)) {
$sub = $cand;
break;
}
}
if ('' === $sub) {
foreach ($rest as $seg) {
if (ctype_digit((string) $seg) && isset($sub_names[ (int) $seg ])) {
$sub = $sub_names[ (int) $seg ];
break;
}
}
}
if ('' === $sub) {
continue;
}
$items[ $row ][ $sub ] = (string) $r->meta_value;
}
$out = array();
foreach ($items as $item) {
$url = trim((string) ( $item['url'] ?? '' ));
if ('' === $url) {
continue;
}
$out[] = array(
'url' => $url,
'icon' => (string) ( $item['icon'] ?? '' ),
'name' => (string) ( $item['name'] ?? '' ),
'rating' => (string) ( $item['rating'] ?? '' ),
);
}
return $out;
}
function dekart_post_author(int $post_id = 0): array
{
$post_id = $post_id ?: (int) get_the_ID();

View File

@ -11,23 +11,8 @@ $city_prep = $data->get_city_prep();
$platforms = array();
$cf_platforms = carbon_get_post_meta(get_the_ID(), 'pksh_external_platforms');
if (is_array($cf_platforms)) {
foreach ($cf_platforms as $row) {
if (! is_array($row)) {
continue;
}
$url = trim((string) ( $row['platform_url'] ?? $row['url'] ?? '' ));
if ('' === $url) {
continue;
}
$platforms[] = array(
'url' => $url,
'icon' => (string) ( $row['platform_icon'] ?? $row['icon'] ?? '' ),
'name' => (string) ( $row['platform_name'] ?? $row['name'] ?? '' ),
'rating' => (string) ( $row['platform_rating'] ?? $row['rating'] ?? '' ),
);
}
if (function_exists('dekart_pksh_external_platforms')) {
$platforms = dekart_pksh_external_platforms((int) get_the_ID());
}
if (empty($platforms)) {