esc_html__('左文案 + 右侧叠层图片案例展示。', 'aksrt'), 'classname' => 'widget_aksrt_tilt_cases', ] ); } public function widget($args, $instance): void { $title = trim((string) ($instance['intro_title'] ?? '')); $subtitle = trim((string) ($instance['intro_subtitle'] ?? '')); $badge = trim((string) ($instance['intro_badge_text'] ?? '')); $images = $this->normalize_images($instance['intro_showcase_images'] ?? []); $features = $this->normalize_features($instance['intro_features'] ?? []); $buttons = $this->normalize_buttons($instance['intro_buttons'] ?? []); if ($title === '' && $subtitle === '' && !$images) { return; } echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo '
'; echo '
'; if ($badge !== '') { $icon = sanitize_text_field((string) ($instance['intro_badge_icon'] ?? '')); echo '
'; if ($icon !== '') { echo ' '; } echo esc_html($badge) . '
'; } if ($title !== '') { $highlight = trim((string) ($instance['intro_title_highlight'] ?? '')); $title_html = esc_html($title); if ($highlight !== '') { $h = esc_html($highlight); $title_html = str_replace($h, '' . $h . '', $title_html); } echo '

' . $title_html . '

'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } if ($subtitle !== '') { echo '

' . esc_html($subtitle) . '

'; } if ($features) { echo ''; } if ($buttons) { echo '
'; foreach ($buttons as $b) { $url = $b['url']; if ($url !== '' && !empty($instance['go_link']) && function_exists('aksrt_go_link')) { $url = aksrt_go_link($url, true); } echo '' . esc_html($b['text']) . ''; } echo '
'; } echo '
'; echo '
'; if ($images) { foreach ($images as $i => $img) { $cls = $i === 0 ? ' is-main' : ''; echo '
'; } } else { echo '
'; } echo '
'; echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** @param mixed $raw @return array */ private function normalize_images($raw): array { if (is_string($raw)) { $decoded = json_decode($raw, true); if (is_array($decoded)) { $raw = $decoded; } else { $parts = preg_split('/\r\n|\r|\n|,/', $raw) ?: []; $raw = $parts; } } if (!is_array($raw)) { return []; } $out = []; foreach ($raw as $row) { if (is_array($row)) { $url = esc_url_raw(trim((string) ($row['image'] ?? $row['url'] ?? ''))); } else { $url = esc_url_raw(trim((string) $row)); } if ($url !== '') { $out[] = $url; } if (count($out) >= 5) { break; } } return $out; } /** @param mixed $raw @return array */ private function normalize_features($raw): array { if (is_string($raw)) { $decoded = json_decode($raw, true); $raw = is_array($decoded) ? $decoded : []; } if (!is_array($raw)) { return []; } $out = []; foreach ($raw as $row) { if (is_string($row)) { $text = sanitize_text_field($row); $icon = ''; } elseif (is_array($row)) { $text = sanitize_text_field((string) ($row['text'] ?? '')); $icon = sanitize_text_field((string) ($row['icon'] ?? '')); } else { continue; } if ($text === '') { continue; } $out[] = ['text' => $text, 'icon' => $icon]; if (count($out) >= 8) { break; } } return $out; } /** @param mixed $raw @return array */ private function normalize_buttons($raw): array { if (is_string($raw)) { $decoded = json_decode($raw, true); $raw = is_array($decoded) ? $decoded : []; } if (!is_array($raw)) { return []; } $out = []; foreach ($raw as $row) { if (!is_array($row)) { continue; } $text = sanitize_text_field((string) ($row['text'] ?? $row['btn']['text'] ?? '')); $url = esc_url_raw(trim((string) ($row['url'] ?? $row['btn']['url'] ?? ''))); if ($text === '') { continue; } $out[] = ['text' => $text, 'url' => $url]; if (count($out) >= 4) { break; } } return $out; } public function form($instance): void { $images = is_string($instance['intro_showcase_images'] ?? null) ? (string) $instance['intro_showcase_images'] : wp_json_encode($this->normalize_images($instance['intro_showcase_images'] ?? []), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); $features = is_string($instance['intro_features'] ?? null) ? (string) $instance['intro_features'] : wp_json_encode($this->normalize_features($instance['intro_features'] ?? []), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); $buttons = is_string($instance['intro_buttons'] ?? null) ? (string) $instance['intro_buttons'] : wp_json_encode($this->normalize_buttons($instance['intro_buttons'] ?? []), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>

sanitize_text_field((string) ($new_instance['intro_badge_text'] ?? '')), 'intro_badge_icon' => sanitize_text_field((string) ($new_instance['intro_badge_icon'] ?? '')), 'intro_title' => sanitize_textarea_field((string) ($new_instance['intro_title'] ?? '')), 'intro_title_highlight' => sanitize_text_field((string) ($new_instance['intro_title_highlight'] ?? '')), 'intro_subtitle' => sanitize_textarea_field((string) ($new_instance['intro_subtitle'] ?? '')), 'intro_features' => $this->normalize_features($new_instance['intro_features'] ?? []), 'intro_buttons' => $this->normalize_buttons($new_instance['intro_buttons'] ?? []), 'intro_showcase_images' => $this->normalize_images($new_instance['intro_showcase_images'] ?? []), 'go_link' => !empty($new_instance['go_link']) ? 1 : 0, ]; } }