Register AKSRT 写文章/投稿 page template, wire header publish entry to front submit URL, rewrite page-submit as dual-column TinyMCE compose with featured cover, multi-category/topics/filters, pay box, drafts, and capability-gated FrontSubmit AJAX (pay/featured traits).
158 lines
5.9 KiB
PHP
158 lines
5.9 KiB
PHP
<?php
|
|
/**
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Core;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
/**
|
|
* Featured cover (image / slide / video) for front-end submit.
|
|
*/
|
|
trait FrontSubmitFeatured {
|
|
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
private function save_featured_meta(int $post_id, array $data, int $user_id): void {
|
|
if (empty($data['featured_data']) && empty($data['featured_type'])) {
|
|
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)) {
|
|
// Flat payload: featured_type + fields.
|
|
$featured = [
|
|
'type' => sanitize_key((string) ($data['featured_type'] ?? '')),
|
|
'data' => $data['featured_payload'] ?? $data,
|
|
];
|
|
}
|
|
|
|
$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();
|
|
}
|
|
|
|
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'] ?? $payload['video'] ?? ''));
|
|
$pic = esc_url_raw((string) ($payload['pic'] ?? $payload['poster'] ?? $payload['cover'] ?? ''));
|
|
$post_meta['featured_video'] = $video;
|
|
$post_meta['featured_slide'] = '';
|
|
if ($pic !== '') {
|
|
$post_meta['thumbnail_url'] = $pic;
|
|
$post_meta['cover_images'] = $pic;
|
|
$this->maybe_set_thumbnail_url($post_id, $pic);
|
|
}
|
|
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 = $payload['ids'] ?? $payload['slides'] ?? $payload['images'] ?? [];
|
|
$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'] ?? ''));
|
|
if ($u !== '') {
|
|
$urls[] = $u;
|
|
} elseif (!empty($item['id'])) {
|
|
$src = wp_get_attachment_image_url((int) $item['id'], 'full');
|
|
if ($src) {
|
|
$urls[] = $src;
|
|
}
|
|
}
|
|
} elseif (is_numeric($item)) {
|
|
$src = wp_get_attachment_image_url((int) $item, 'full');
|
|
if ($src) {
|
|
$urls[] = $src;
|
|
}
|
|
} else {
|
|
$u = esc_url_raw((string) $item);
|
|
if ($u !== '') {
|
|
$urls[] = $u;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$post_meta['featured_slide'] = implode(',', $urls);
|
|
$post_meta['featured_video'] = '';
|
|
if (!empty($urls[0])) {
|
|
$post_meta['thumbnail_url'] = $urls[0];
|
|
$post_meta['cover_images'] = $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'] ?? $payload['image'] ?? ''));
|
|
$id = absint($payload['id'] ?? 0);
|
|
if ($url === '' && $id > 0) {
|
|
$src = wp_get_attachment_image_url($id, 'full');
|
|
if ($src) {
|
|
$url = $src;
|
|
}
|
|
}
|
|
$post_meta['featured_video'] = '';
|
|
$post_meta['featured_slide'] = '';
|
|
if ($url !== '') {
|
|
$post_meta['thumbnail_url'] = $url;
|
|
$post_meta['cover_images'] = $url;
|
|
if ($id > 0) {
|
|
set_post_thumbnail($post_id, $id);
|
|
} else {
|
|
$this->maybe_set_thumbnail_url($post_id, $url);
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 'close':
|
|
case 'none':
|
|
case 'clear':
|
|
$post_meta['featured_video'] = '';
|
|
$post_meta['featured_slide'] = '';
|
|
$post_meta['thumbnail_url'] = '';
|
|
$post_meta['cover_images'] = '';
|
|
delete_post_thumbnail($post_id);
|
|
break;
|
|
|
|
default:
|
|
return;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|