esc_html__('大中小尺寸亮点/卖点卡片栅格。', 'aksrt'), 'classname' => 'widget_aksrt_highlights', ] ); } /** * @param array $args * @param array $instance */ public function widget($args, $instance): void { $cards = $this->normalize($instance['cards'] ?? []); if (!$cards) { if (current_user_can('manage_options')) { echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo function_exists('aksrt_get_widget_null') ? aksrt_get_widget_null(__('请添加至少一张亮点卡片', 'aksrt')) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped : '

' . esc_html__('请添加至少一张亮点卡片', 'aksrt') . '

'; echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } return; } $go_link = !empty($instance['go_link']); $anim = sanitize_key((string) ($instance['obs_animation'] ?? 'slideup')); $anim_repeat = !empty($instance['animation_repeat']); echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped $title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base); if ($title !== '') { echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } $large = array_values(array_filter($cards, static fn($c) => ($c['size'] ?? 'medium') === 'large')); $medium = array_values(array_filter($cards, static fn($c) => ($c['size'] ?? 'medium') === 'medium')); $small = array_values(array_filter($cards, static fn($c) => ($c['size'] ?? 'medium') === 'small')); if (!$large && !$medium && !$small) { $medium = $cards; } echo '
'; $idx = 0; foreach (['large' => $large, 'medium' => $medium, 'small' => $small] as $size => $group) { if (!$group) { continue; } echo '
'; foreach ($group as $c) { $pack = function_exists('aksrt_obs_animate_attrs') ? aksrt_obs_animate_attrs($anim, $anim_repeat, $idx * 0.08) : ['class' => '', 'attr' => '']; echo $this->card($c, $size, $go_link, $pack); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped $idx++; } echo '
'; } echo '
'; echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** * @param array $c * @param array{class:string,attr:string} $pack */ private function card(array $c, string $size, bool $go_link, array $pack): string { $color = sanitize_key((string) ($c['color'] ?? 'blue')); if (!in_array($color, ['blue', 'pink', 'purple', 'green', 'orange', 'cyan'], true)) { $color = 'blue'; } $url = (string) ($c['url'] ?? ''); if ($url !== '' && $go_link && function_exists('aksrt_go_link')) { $url = aksrt_go_link($url, true); } $tag = $url !== '' ? 'a' : 'div'; $href = $url !== '' ? ' href="' . esc_url($url) . '"' : ''; $img = (string) ($c['image'] ?? ''); $cls = 'aksrt-hl-card size-' . esc_attr($size) . ' color-' . esc_attr($color) . $pack['class']; $html = '<' . $tag . ' class="' . esc_attr(trim($cls)) . '"' . $href . $pack['attr'] . '>'; if ($img !== '') { $placeholder = get_template_directory_uri() . '/assets/img/placeholder.svg'; $html .= '
'; } $html .= '
'; if (!empty($c['icon'])) { $html .= '
'; } if ($c['title'] !== '') { $html .= '
' . esc_html($c['title']) . '
'; } if ($c['desc'] !== '') { $html .= '
' . esc_html($c['desc']) . '
'; } $html .= '
'; return $html; } /** * @param mixed $raw * @return array> */ private function normalize($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; } $title = sanitize_text_field((string) ($row['title'] ?? '')); $desc = sanitize_text_field((string) ($row['desc'] ?? '')); if ($title === '' && $desc === '') { continue; } $size = sanitize_key((string) ($row['size'] ?? 'medium')); if (!in_array($size, ['large', 'medium', 'small'], true)) { $size = 'medium'; } $out[] = [ 'title' => $title, 'desc' => $desc, 'icon' => sanitize_text_field((string) ($row['icon'] ?? '')), 'image' => esc_url_raw(trim((string) ($row['image'] ?? ''))), 'url' => esc_url_raw(trim((string) ($row['url'] ?? ''))), 'color' => sanitize_key((string) ($row['color'] ?? 'blue')), 'size' => $size, ]; if (count($out) >= 12) { break; } } return $out; } /** * @param array $instance */ public function form($instance): void { $json = is_string($instance['cards'] ?? null) ? (string) $instance['cards'] : wp_json_encode($this->normalize($instance['cards'] ?? []), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>

render_common_form_section($instance); } /** * @param array $new_instance * @param array $old_instance * @return array */ public function update($new_instance, $old_instance): array { $business = [ 'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')), 'cards' => $this->normalize($new_instance['cards'] ?? []), 'go_link' => !empty($new_instance['go_link']) ? 1 : 0, 'obs_animation' => sanitize_key((string) ($new_instance['obs_animation'] ?? 'slideup')), 'animation_repeat' => !empty($new_instance['animation_repeat']) ? 1 : 0, ]; return $this->update_common_fields($new_instance, $old_instance, $business); } }