$data */ private function save_featured_meta(int $post_id, array $data, int $user_id): void { if (empty($data['featured_data'])) { return; } $featured = $data['featured_data'] ?? null; if (is_string($featured)) { $decoded = json_decode(wp_unslash($featured), true); $featured = is_array($decoded) ? $decoded : null; } if (!is_array($featured)) { return; } $type = sanitize_key((string) ($featured['type'] ?? '')); $payload = is_array($featured['data'] ?? null) ? $featured['data'] : $featured; $post_meta = get_post_meta($post_id, DomainModel::POST_META_KEY, true); if (!is_array($post_meta)) { $post_meta = DomainModel::default_post_meta(); } $post_meta = array_intersect_key($post_meta, DomainModel::default_post_meta()); switch ($type) { case 'video': if (!$this->user_can_cap('new_post_video_cover', $user_id) && !$this->user_can_cap('new_post_image_cover', $user_id)) { return; } $video = esc_url_raw((string) ($payload['url'] ?? '')); $video_id = absint($payload['id'] ?? 0); if ($video_id > 0 && !$this->can_use_front_submit_video($video_id, $user_id)) { $video_id = 0; } $pic_id = absint($payload['pic_id'] ?? 0); if ($pic_id > 0 && !$this->can_use_front_submit_image($pic_id, $user_id)) { $pic_id = 0; } $pic = ''; if ($pic_id > 0) { $pic = (string) wp_get_attachment_image_url($pic_id, 'full'); } if ($pic === '') { $pic = esc_url_raw((string) ($payload['pic'] ?? '')); if ($pic !== '' && !$this->can_use_front_submit_image_url($pic, $user_id)) { $pic = ''; } } $post_meta['featured_video'] = $video; $post_meta['featured_video_attachment_id'] = $video_id; $post_meta['featured_slide'] = ''; $post_meta['thumbnail_url'] = ''; $post_meta['cover_image'] = ''; if ($pic !== '') { $post_meta['thumbnail_url'] = $pic; $post_meta['cover_image'] = $pic; if ($pic_id > 0) { set_post_thumbnail($post_id, $pic_id); } else { $this->maybe_set_thumbnail_url($post_id, $pic); } } else { delete_post_thumbnail($post_id); } break; case 'slide': if (!$this->user_can_cap('new_post_slide_cover', $user_id) && !$this->user_can_cap('new_post_image_cover', $user_id)) { return; } $ids = array_is_list($payload) ? $payload : []; $attachment_ids = []; $urls = []; if (is_string($ids)) { $ids = array_filter(array_map('trim', explode(',', $ids))); } if (is_array($ids)) { foreach ($ids as $item) { if (is_array($item)) { $u = esc_url_raw((string) ($item['url'] ?? '')); $attachment_id = absint($item['id'] ?? 0); if ($attachment_id > 0 && $this->can_use_front_submit_image($attachment_id, $user_id)) { $src = wp_get_attachment_image_url($attachment_id, 'full'); if ($src) { $attachment_ids[] = $attachment_id; $urls[] = $src; } } elseif ($u !== '' && $this->user_can_cap('upload_submit_image', $user_id)) { $urls[] = $u; } } elseif (is_numeric($item)) { $attachment_id = (int) $item; $src = $this->can_use_front_submit_image($attachment_id, $user_id) ? wp_get_attachment_image_url($attachment_id, 'full') : false; if ($src) { $attachment_ids[] = $attachment_id; $urls[] = $src; } } else { $u = esc_url_raw((string) $item); if ($u !== '' && $this->user_can_cap('upload_submit_image', $user_id)) { $urls[] = $u; } } } } $post_meta['featured_slide'] = implode(',', array_values(array_unique($attachment_ids))); $post_meta['featured_video'] = ''; $post_meta['featured_video_attachment_id'] = 0; $post_meta['thumbnail_url'] = ''; $post_meta['cover_image'] = ''; delete_post_thumbnail($post_id); if (!empty($urls[0]) && $this->can_use_front_submit_image_url((string) $urls[0], $user_id)) { $post_meta['thumbnail_url'] = $urls[0]; $post_meta['cover_image'] = $urls[0]; $this->maybe_set_thumbnail_url($post_id, $urls[0]); } break; case 'image': if (!$this->user_can_cap('new_post_image_cover', $user_id)) { return; } $url = esc_url_raw((string) ($payload['url'] ?? '')); $id = absint($payload['id'] ?? 0); if ($id > 0 && !$this->can_use_front_submit_image($id, $user_id)) { return; } if ($url === '' && $id > 0) { $src = wp_get_attachment_image_url($id, 'full'); if ($src) { $url = $src; } } $post_meta['featured_video'] = ''; $post_meta['featured_video_attachment_id'] = 0; $post_meta['featured_slide'] = ''; if ($url !== '' && $this->can_use_front_submit_image_url($url, $user_id)) { $post_meta['thumbnail_url'] = $url; $post_meta['cover_image'] = $url; if ($id > 0) { set_post_thumbnail($post_id, $id); } elseif ($this->user_can_cap('upload_submit_image', $user_id)) { $this->maybe_set_thumbnail_url($post_id, $url); } } break; case 'close': case 'none': case 'clear': $post_meta['featured_video'] = ''; $post_meta['featured_video_attachment_id'] = 0; $post_meta['featured_slide'] = ''; $post_meta['thumbnail_url'] = ''; $post_meta['cover_image'] = ''; delete_post_thumbnail($post_id); break; default: return; } unset($post_meta['views'], $post_meta['like']); update_post_meta($post_id, DomainModel::POST_META_KEY, $post_meta); } private function maybe_set_thumbnail_url(int $post_id, string $url): void { if ($url === '') { return; } // Prefer attachment if URL is local media. $attach_id = attachment_url_to_postid($url); if ($attach_id > 0) { set_post_thumbnail($post_id, $attach_id); return; } $this->set_thumbnail_from_url($post_id, $url); } private function can_use_front_submit_image(int $attachment_id, int $user_id): bool { if ($attachment_id <= 0 || !$this->user_can_cap('upload_submit_image', $user_id)) { return false; } $attachment = get_post($attachment_id); return $attachment instanceof \WP_Post && $attachment->post_type === 'attachment' && (int) $attachment->post_author === $user_id && wp_attachment_is_image($attachment_id); } private function can_use_front_submit_image_url(string $url, int $user_id): bool { $attachment_id = attachment_url_to_postid($url); return $attachment_id > 0 && $this->can_use_front_submit_image($attachment_id, $user_id); } private function can_use_front_submit_video(int $attachment_id, int $user_id): bool { if ($attachment_id <= 0 || !$this->user_can_cap('new_post_video_cover', $user_id)) { return false; } $attachment = get_post($attachment_id); return $attachment instanceof \WP_Post && $attachment->post_type === 'attachment' && (int) $attachment->post_author === $user_id && in_array($attachment->post_status, ['inherit', 'private'], true) && strpos((string) $attachment->post_mime_type, 'video/') === 0; } }