Files
wordpress-theme-aksrt/templates/parts/submit/context.php
T
2026-08-26 16:32:18 +08:00

222 lines
7.8 KiB
PHP

<?php
/**
* Submit page context builder — shared by page-submit shell + partials.
*
* Expects no output. Returns $ctx array.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
$front = class_exists('\AKSRT\Core\FrontSubmit') ? \AKSRT\Core\FrontSubmit::get_instance() : null;
$user_id = get_current_user_id();
$settings = $front ? $front->settings() : (class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_submit_settings() : []);
$enabled = !isset($settings['enabled']) || !empty($settings['enabled']);
$edit_id = isset($_GET['edit']) ? absint($_GET['edit']) : 0;
$edit_post = null;
if ($edit_id > 0 && $front) {
if ($front->can_edit_post($edit_id, $user_id)) {
$edit_post = get_post($edit_id);
if (!$edit_post || $edit_post->post_type !== 'post') {
$edit_post = null;
$edit_id = 0;
}
} else {
$edit_id = 0;
}
}
$is_guest = $user_id <= 0;
$can_add = $front ? $front->can_submit($user_id) : false;
$can_edit = $edit_post instanceof WP_Post;
$is_can_new = $can_edit || $can_add;
// Caps.
$cap = static function (string $c) use ($front, $user_id, $edit_id): bool {
if ($front) {
return $front->user_can_cap($c, $user_id, $edit_id ?: null);
}
return function_exists('aksrt_user_can') ? aksrt_user_can($c, $user_id, $edit_id ?: null) : ($user_id > 0);
};
$post_meta = [];
if ($edit_post) {
$raw_meta = get_post_meta($edit_post->ID, \AKSRT\Core\DomainModel::POST_META_KEY, true);
$post_meta = is_array($raw_meta) ? $raw_meta : [];
}
$pay_meta = ($edit_post && function_exists('aksrt_get_pay_meta'))
? aksrt_get_pay_meta($edit_post->ID)
: (class_exists('\AKSRT\Core\DomainModel') ? \AKSRT\Core\DomainModel::default_pay_meta() : []);
// Featured initial args for JS.
$featured_args = ['type' => '', 'data' => new stdClass(), 'options' => [
'image' => $cap('new_post_image_cover'),
'slide' => $cap('new_post_slide_cover'),
'video' => $cap('new_post_video_cover'),
'ratio' => 50,
]];
if ($edit_post) {
if (!empty($post_meta['featured_video'])) {
$featured_args['type'] = 'video';
$featured_args['data'] = [
'url' => (string) $post_meta['featured_video'],
'pic' => (string) ($post_meta['thumbnail_url'] ?? ''),
];
} elseif (!empty($post_meta['featured_slide'])) {
$slides = array_filter(array_map('trim', explode(',', (string) $post_meta['featured_slide'])));
$slide_data = [];
foreach ($slides as $attachment_id) {
$id = absint($attachment_id);
$url = $id > 0 ? wp_get_attachment_image_url($id, 'full') : false;
if ($url) {
$slide_data[] = ['id' => $id, 'url' => $url];
}
}
if ($slide_data) {
$featured_args['type'] = 'slide';
$featured_args['data'] = $slide_data;
}
} else {
$thumb = get_the_post_thumbnail_url($edit_post, 'full')
?: (string) ($post_meta['thumbnail_url'] ?? $post_meta['cover_image'] ?? '');
if ($thumb !== '') {
$featured_args['type'] = 'image';
$featured_args['data'] = ['url' => $thumb];
}
}
}
// Terms.
$cat_args = [
'taxonomy' => 'category',
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
];
if (!empty($settings['allowed_cat_ids'])) {
$cat_args['include'] = $settings['allowed_cat_ids'];
} else {
$cat_args['parent'] = 0;
}
$categories = get_terms($cat_args);
if (is_wp_error($categories)) {
$categories = [];
}
$current_cats = $edit_post ? wp_get_post_categories($edit_post->ID) : [];
$custom_dimensions = [];
$current_cf = [];
if (!empty($settings['custom_filter_s']) && class_exists('\AKSRT\Settings\Settings')) {
$filter_all = \AKSRT\Settings\Settings::get_post_filter_settings();
$custom_dimensions = $filter_all['custom_filter'] ?? [];
if ($edit_post && !empty($custom_dimensions)) {
foreach ($custom_dimensions as $dim) {
$key = sanitize_key((string) ($dim['key'] ?? ''));
if ($key === '') {
continue;
}
$current_cf[$key] = get_post_meta($edit_post->ID, '_aksrt_cf_' . $key, true);
}
}
}
$current_title = $edit_post ? $edit_post->post_title : '';
$current_content = $edit_post ? $edit_post->post_content : '';
$current_excerpt = $edit_post ? $edit_post->post_excerpt : '';
$current_tags = $edit_post ? implode(', ', wp_get_post_tags($edit_post->ID, ['fields' => 'names'])) : '';
$post_status = $edit_post ? $edit_post->post_status : '';
$post_modified = $edit_post ? $edit_post->post_modified : '';
$draft_count = 0;
$user_drafts = [];
if ($user_id > 0) {
$user_drafts = get_posts([
'post_type' => 'post',
'post_status' => 'draft',
'author' => $user_id,
'numberposts' => 10,
'orderby' => 'modified',
'order' => 'DESC',
]);
$draft_count = count($user_drafts);
}
// VIP labels / points.
$vip = class_exists('\AKSRT\Settings\Settings') && method_exists('\AKSRT\Settings\Settings', 'get_vip_settings')
? \AKSRT\Settings\Settings::get_vip_settings()
: [];
if (empty($vip) && class_exists('\AKSRT\Settings\PaymentRuntime')) {
// Fallback: read common keys.
$all = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_all() : [];
$vip = [
'vip1_enabled' => !empty($all['pay_user_vip_1_s']),
'vip2_enabled' => !empty($all['pay_user_vip_2_s']),
'vip1_name' => (string) ($all['pay_user_vip_1_name'] ?? 'VIP1'),
'vip2_name' => (string) ($all['pay_user_vip_2_name'] ?? 'VIP2'),
];
}
$vip_levels = is_array($vip['levels'] ?? null) ? $vip['levels'] : [];
$vip = [
'vip1_enabled' => !empty($vip['vip1_enabled']) || !empty($vip_levels[1]['enabled']),
'vip2_enabled' => !empty($vip['vip2_enabled']) || !empty($vip_levels[2]['enabled']),
'vip1_name' => (string) ($vip['vip1_name'] ?? $vip_levels[1]['name'] ?? 'VIP1'),
'vip2_name' => (string) ($vip['vip2_name'] ?? $vip_levels[2]['name'] ?? 'VIP2'),
];
$points_enabled = class_exists('\AKSRT\Settings\Settings') && method_exists('\AKSRT\Settings\Settings', 'credits_enabled')
? \AKSRT\Settings\Settings::credits_enabled()
: false;
$submit_label = __('发布', 'aksrt');
if ($is_guest) {
$submit_label = __('审核', 'aksrt');
} elseif ($edit_post && in_array($post_status, ['publish', 'pending'], true)) {
$submit_label = __('保存', 'aksrt');
} elseif (($settings['review_mode'] ?? 'pending') === 'pending' && !$cap('new_post_audit_no')) {
$submit_label = __('审核', 'aksrt');
}
$ai_available = class_exists('\AKSRT\Ai\AI') && \AKSRT\Ai\AI::get_instance()->is_available();
return [
'front' => $front,
'user_id' => $user_id,
'settings' => $settings,
'enabled' => $enabled,
'edit_id' => $edit_id,
'edit_post' => $edit_post,
'is_guest' => $is_guest,
'is_can_new' => $is_can_new,
'cap' => $cap,
'post_meta' => $post_meta,
'pay_meta' => $pay_meta,
'featured_args' => $featured_args,
'categories' => $categories,
'current_cats' => $current_cats,
'custom_dimensions' => $custom_dimensions,
'current_cf' => $current_cf,
'current_title' => $current_title,
'current_content' => $current_content,
'current_excerpt' => $current_excerpt,
'current_tags' => $current_tags,
'post_status' => $post_status,
'post_modified' => $post_modified,
'draft_count' => $draft_count,
'user_drafts' => $user_drafts,
'vip' => $vip,
'points_enabled' => $points_enabled,
'submit_label' => $submit_label,
'ai_available' => $ai_available,
'can_pay' => $cap('new_post_pay'),
'can_pay_download' => $cap('new_post_pay_download'),
'can_cover_image' => $cap('new_post_image_cover'),
'can_cover_slide' => $cap('new_post_slide_cover'),
'can_cover_video' => $cap('new_post_video_cover'),
'can_upload_img' => $cap('upload_submit_image'),
'can_upload_video' => $cap('new_post_upload_video'),
'can_upload_file' => $cap('upload_resource_attachment'),
'can_hide' => $cap('new_post_hide'),
'can_iframe' => $cap('new_post_iframe_video'),
];