375 lines
15 KiB
PHP
375 lines
15 KiB
PHP
<?php
|
||
defined('ABSPATH') || exit;
|
||
|
||
// 前端隐藏 WordPress Admin Bar
|
||
// ============================================================
|
||
|
||
add_filter('show_admin_bar', '__return_false');
|
||
|
||
// ============================================================
|
||
// SMTP 邮件配置注入
|
||
// ============================================================
|
||
|
||
add_action('phpmailer_init', function ($phpmailer) {
|
||
$mail = get_option('aksrt_mail_smtp', array());
|
||
if (empty($mail['enabled']) || empty($mail['host'])) return;
|
||
|
||
$phpmailer->isSMTP();
|
||
$phpmailer->Host = $mail['host'];
|
||
$phpmailer->Port = (int) (isset($mail['port']) ? $mail['port'] : 465);
|
||
$phpmailer->SMTPAuth = !empty($mail['username']);
|
||
$phpmailer->Username = $mail['username'];
|
||
$phpmailer->Password = $mail['password'];
|
||
$phpmailer->SMTPSecure = isset($mail['encryption']) ? $mail['encryption'] : 'ssl';
|
||
|
||
// 性能优化:超时 + 连接保持
|
||
$phpmailer->Timeout = 10; // 连接/读取超时 10 秒
|
||
$phpmailer->Timelimit = 15; // 总执行上限 15 秒
|
||
|
||
if (!empty($mail['from_address'])) {
|
||
$from_name = isset($mail['from_name']) ? $mail['from_name'] : '';
|
||
// mail_showname 覆盖 from_name
|
||
if (class_exists('\AKSRT\Settings\Settings')) {
|
||
$branding = \AKSRT\Settings\Settings::get_mail_branding();
|
||
if ($branding['showname'] !== '') {
|
||
$from_name = $branding['showname'];
|
||
}
|
||
}
|
||
$phpmailer->setFrom($mail['from_address'], $from_name);
|
||
}
|
||
if (!empty($mail['from_name'])) {
|
||
$phpmailer->FromName = $mail['from_name'];
|
||
}
|
||
// mail_showname 全局覆盖 FromName
|
||
if (class_exists('\AKSRT\Settings\Settings')) {
|
||
$branding = \AKSRT\Settings\Settings::get_mail_branding();
|
||
if ($branding['showname'] !== '') {
|
||
$phpmailer->FromName = $branding['showname'];
|
||
}
|
||
}
|
||
});
|
||
|
||
// mail_title_prefix:为所有邮件主题添加前缀
|
||
add_filter('wp_mail', function ($atts) {
|
||
if (!class_exists('\AKSRT\Settings\Settings') || empty($atts['subject'])) return $atts;
|
||
$branding = \AKSRT\Settings\Settings::get_mail_branding();
|
||
if ($branding['title_prefix'] !== '' && strpos($atts['subject'], $branding['title_prefix']) !== 0) {
|
||
$atts['subject'] = $branding['title_prefix'] . ' ' . $atts['subject'];
|
||
}
|
||
return $atts;
|
||
});
|
||
|
||
// ============================================================
|
||
// 分类 Term Meta 字段(封面图 + 推荐描述)
|
||
// ============================================================
|
||
|
||
/**
|
||
* 编辑分类页面 - 显示自定义字段
|
||
*/
|
||
function aksrt_category_meta_fields($term) {
|
||
$cover_id = (int) get_term_meta($term->term_id, 'aksrt_category_cover_id', true);
|
||
$cover_url = $cover_id > 0 ? wp_get_attachment_url($cover_id) : '';
|
||
$recommend_desc = (string) get_term_meta($term->term_id, 'aksrt_category_recommend_desc', true);
|
||
?>
|
||
<tr class="form-field">
|
||
<th scope="row"><label for="aksrt_category_cover_id"><?php esc_html_e('分类封面图', 'aksrt'); ?></label></th>
|
||
<td>
|
||
<div id="aksrt-cover-preview" style="margin-bottom:8px;">
|
||
<?php if ($cover_url) : ?>
|
||
<img src="<?php echo esc_url($cover_url); ?>" style="max-width:200px;height:auto;border-radius:6px;" alt="">
|
||
<?php endif; ?>
|
||
</div>
|
||
<input type="hidden" id="aksrt_category_cover_id" name="aksrt_category_cover_id" value="<?php echo esc_attr((string) $cover_id); ?>">
|
||
<button type="button" class="button" id="aksrt-pick-cover"><?php esc_html_e('选择图片', 'aksrt'); ?></button>
|
||
<button type="button" class="button" id="aksrt-remove-cover" <?php echo $cover_id > 0 ? '' : 'style="display:none;"'; ?>><?php esc_html_e('移除', 'aksrt'); ?></button>
|
||
<p class="description"><?php esc_html_e('分类推荐模块的封面图片。', 'aksrt'); ?></p>
|
||
</td>
|
||
</tr>
|
||
<tr class="form-field">
|
||
<th scope="row"><label for="aksrt_category_recommend_desc"><?php esc_html_e('推荐描述', 'aksrt'); ?></label></th>
|
||
<td>
|
||
<textarea id="aksrt_category_recommend_desc" name="aksrt_category_recommend_desc" rows="3" class="large-text"><?php echo esc_textarea($recommend_desc); ?></textarea>
|
||
<p class="description"><?php esc_html_e('分类推荐模块中显示的描述文字。留空则使用分类默认描述。', 'aksrt'); ?></p>
|
||
</td>
|
||
</tr>
|
||
<?php
|
||
}
|
||
|
||
/**
|
||
* 新增分类页面 - 显示自定义字段
|
||
*/
|
||
function aksrt_category_meta_fields_add() {
|
||
?>
|
||
<div class="form-field">
|
||
<label for="aksrt_category_cover_id"><?php esc_html_e('分类封面图', 'aksrt'); ?></label>
|
||
<div id="aksrt-cover-preview" style="margin-bottom:8px;"></div>
|
||
<input type="hidden" id="aksrt_category_cover_id" name="aksrt_category_cover_id" value="">
|
||
<button type="button" class="button" id="aksrt-pick-cover"><?php esc_html_e('选择图片', 'aksrt'); ?></button>
|
||
<button type="button" class="button" id="aksrt-remove-cover" style="display:none;"><?php esc_html_e('移除', 'aksrt'); ?></button>
|
||
<p class="description"><?php esc_html_e('分类推荐模块的封面图片。', 'aksrt'); ?></p>
|
||
</div>
|
||
<div class="form-field">
|
||
<label for="aksrt_category_recommend_desc"><?php esc_html_e('推荐描述', 'aksrt'); ?></label>
|
||
<textarea id="aksrt_category_recommend_desc" name="aksrt_category_recommend_desc" rows="3"></textarea>
|
||
<p class="description"><?php esc_html_e('分类推荐模块中显示的描述文字。留空则使用分类默认描述。', 'aksrt'); ?></p>
|
||
</div>
|
||
<?php
|
||
}
|
||
|
||
add_action('category_edit_form_fields', 'aksrt_category_meta_fields', 10, 1);
|
||
add_action('category_add_form_fields', 'aksrt_category_meta_fields_add', 10, 1);
|
||
|
||
/**
|
||
* 保存分类 term meta
|
||
*/
|
||
function aksrt_save_category_meta(int $term_id) {
|
||
if (!current_user_can('manage_categories')) return;
|
||
|
||
$cover_id = isset($_POST['aksrt_category_cover_id']) ? absint($_POST['aksrt_category_cover_id']) : 0;
|
||
$recommend_desc = isset($_POST['aksrt_category_recommend_desc']) ? sanitize_textarea_field(wp_unslash($_POST['aksrt_category_recommend_desc'])) : '';
|
||
|
||
if ($cover_id > 0) {
|
||
update_term_meta($term_id, 'aksrt_category_cover_id', $cover_id);
|
||
} else {
|
||
delete_term_meta($term_id, 'aksrt_category_cover_id');
|
||
}
|
||
|
||
if ($recommend_desc !== '') {
|
||
update_term_meta($term_id, 'aksrt_category_recommend_desc', $recommend_desc);
|
||
} else {
|
||
delete_term_meta($term_id, 'aksrt_category_recommend_desc');
|
||
}
|
||
|
||
if (class_exists('\AKSRT\Settings\Settings')) {
|
||
\AKSRT\Settings\Settings::flush_category_recommend_cache();
|
||
}
|
||
}
|
||
|
||
add_action('edited_category', 'aksrt_save_category_meta', 10, 1);
|
||
add_action('created_category', 'aksrt_save_category_meta', 10, 1);
|
||
|
||
/**
|
||
* 分类编辑页加载媒体库脚本(wp_enqueue_media 仅在 AKSRT 面板加载,此处补上)
|
||
*/
|
||
add_action('admin_enqueue_scripts', function (string $hook_suffix) {
|
||
if ($hook_suffix === 'edit-tags.php' || $hook_suffix === 'term.php') {
|
||
wp_enqueue_media();
|
||
}
|
||
});
|
||
|
||
/**
|
||
* 分类编辑页内联 JS - 媒体选择器
|
||
*/
|
||
function aksrt_category_meta_admin_js() {
|
||
$screen = get_current_screen();
|
||
if (!$screen || $screen->taxonomy !== 'category') return;
|
||
?>
|
||
<script>
|
||
jQuery(function($) {
|
||
var frame;
|
||
|
||
$(document).on('click', '#aksrt-pick-cover', function(e) {
|
||
e.preventDefault();
|
||
if (typeof wp === 'undefined' || !wp.media) return;
|
||
if (frame) { frame.open(); return; }
|
||
|
||
frame = wp.media({
|
||
title: '<?php echo esc_js(__('选择分类封面图', 'aksrt')); ?>',
|
||
button: { text: '<?php echo esc_js(__('使用此图片', 'aksrt')); ?>' },
|
||
multiple: false
|
||
});
|
||
|
||
frame.on('select', function() {
|
||
var attachment = frame.state().get('selection').first().toJSON();
|
||
$('#aksrt_category_cover_id').val(attachment.id);
|
||
$('#aksrt-cover-preview').html('<img src="' + attachment.url + '" style="max-width:200px;height:auto;border-radius:6px;" alt="">');
|
||
$('#aksrt-remove-cover').show();
|
||
});
|
||
|
||
frame.open();
|
||
});
|
||
|
||
$(document).on('click', '#aksrt-remove-cover', function(e) {
|
||
e.preventDefault();
|
||
$('#aksrt_category_cover_id').val('');
|
||
$('#aksrt-cover-preview').html('');
|
||
$(this).hide();
|
||
});
|
||
});
|
||
</script>
|
||
<?php
|
||
}
|
||
|
||
add_action('admin_footer', 'aksrt_category_meta_admin_js');
|
||
|
||
// ============================================================
|
||
// 资源列表:/resources 虚拟归档(post + _aksrt_is_resource)
|
||
// ============================================================
|
||
|
||
add_action('init', function () {
|
||
add_rewrite_rule('^resources/?$', 'index.php?aksrt_is_resource_archive=1', 'top');
|
||
add_rewrite_rule('^resources/page/([0-9]+)/?$', 'index.php?aksrt_is_resource_archive=1&paged=$matches[1]', 'top');
|
||
// 旧单篇路径 /resource/{slug} → 301 到 post 永久链接
|
||
add_rewrite_rule('^resource/([^/]+)/?$', 'index.php?aksrt_legacy_resource=$matches[1]', 'top');
|
||
});
|
||
|
||
add_filter('query_vars', function ($vars) {
|
||
$vars[] = 'aksrt_is_resource_archive';
|
||
$vars[] = 'aksrt_legacy_resource';
|
||
$vars[] = 'type';
|
||
$vars[] = 'trem';
|
||
return $vars;
|
||
});
|
||
|
||
add_action('template_redirect', function () {
|
||
$slug = get_query_var('aksrt_legacy_resource');
|
||
if (!$slug) {
|
||
return;
|
||
}
|
||
$page = get_page_by_path((string) $slug, OBJECT, 'post');
|
||
if ($page) {
|
||
wp_redirect(get_permalink($page->ID), 301);
|
||
exit;
|
||
}
|
||
});
|
||
|
||
add_action('pre_get_posts', function ($query) {
|
||
if (is_admin() || !$query->is_main_query()) {
|
||
return;
|
||
}
|
||
|
||
if ((bool) $query->get('aksrt_is_resource_archive')) {
|
||
$query->set('post_type', 'post');
|
||
$query->set('post_status', 'publish');
|
||
|
||
$orderby = isset($_GET['orderby']) ? sanitize_key((string) $_GET['orderby']) : 'date';
|
||
if ($orderby === 'downloads') {
|
||
$query->set('meta_key', \AKSRT\Core\DomainModel::RESOURCE_DOWNLOAD_COUNT_KEY);
|
||
$query->set('orderby', 'meta_value_num');
|
||
$query->set('order', 'DESC');
|
||
} elseif ($orderby === 'views') {
|
||
$query->set('meta_key', \AKSRT\Core\DomainModel::RESOURCE_VIEW_COUNT_KEY);
|
||
$query->set('orderby', 'meta_value_num');
|
||
$query->set('order', 'DESC');
|
||
}
|
||
|
||
$price_filter = isset($_GET['price']) ? sanitize_key((string) $_GET['price']) : '';
|
||
$price_min = isset($_GET['price_min']) && $_GET['price_min'] !== '' ? (float) $_GET['price_min'] : null;
|
||
$price_max = isset($_GET['price_max']) && $_GET['price_max'] !== '' ? (float) $_GET['price_max'] : null;
|
||
|
||
$meta_query = [
|
||
'relation' => 'AND',
|
||
[
|
||
'key' => \AKSRT\Core\DomainModel::RESOURCE_MARKER_KEY,
|
||
'value' => '1',
|
||
'compare' => '=',
|
||
],
|
||
];
|
||
|
||
if ($price_filter === 'free') {
|
||
$meta_query[] = [
|
||
'relation' => 'OR',
|
||
[
|
||
'key' => \AKSRT\Core\DomainModel::PAY_PRICE_KEY,
|
||
'value' => 0,
|
||
'compare' => '=',
|
||
'type' => 'NUMERIC',
|
||
],
|
||
[
|
||
'key' => \AKSRT\Core\DomainModel::PAY_PRICE_KEY,
|
||
'compare' => 'NOT EXISTS',
|
||
],
|
||
];
|
||
} elseif ($price_filter === 'paid') {
|
||
$meta_query[] = [
|
||
'key' => \AKSRT\Core\DomainModel::PAY_PRICE_KEY,
|
||
'value' => 0,
|
||
'compare' => '>',
|
||
'type' => 'NUMERIC',
|
||
];
|
||
}
|
||
|
||
if ($price_min !== null || $price_max !== null) {
|
||
if ($price_min !== null && $price_max !== null) {
|
||
$meta_query[] = [
|
||
'key' => \AKSRT\Core\DomainModel::PAY_PRICE_KEY,
|
||
'value' => [$price_min, $price_max],
|
||
'compare' => 'BETWEEN',
|
||
'type' => 'NUMERIC',
|
||
];
|
||
} elseif ($price_min !== null) {
|
||
$meta_query[] = [
|
||
'key' => \AKSRT\Core\DomainModel::PAY_PRICE_KEY,
|
||
'value' => $price_min,
|
||
'compare' => '>=',
|
||
'type' => 'NUMERIC',
|
||
];
|
||
} else {
|
||
$meta_query[] = [
|
||
'key' => \AKSRT\Core\DomainModel::PAY_PRICE_KEY,
|
||
'value' => $price_max,
|
||
'compare' => '<=',
|
||
'type' => 'NUMERIC',
|
||
];
|
||
}
|
||
}
|
||
|
||
$existing = $query->get('meta_query');
|
||
if (!is_array($existing)) {
|
||
$existing = [];
|
||
}
|
||
$query->set('meta_query', array_merge($existing, $meta_query));
|
||
$query->set('cat', 0);
|
||
$query->set('tag', '');
|
||
}
|
||
|
||
// 站内搜索:分类/标签名搜索(内容类型始终为 post)
|
||
if ($query->is_search() && empty($query->get('post_type'))) {
|
||
$search_type = sanitize_key((string) $query->get('type'));
|
||
$trem = absint($query->get('trem'));
|
||
if ($trem > 0) {
|
||
$query->set('cat', $trem);
|
||
}
|
||
|
||
if (in_array($search_type, ['category', 'tag'], true)) {
|
||
$taxonomy = $search_type === 'category' ? 'category' : 'post_tag';
|
||
$term_ids = get_terms([
|
||
'taxonomy' => $taxonomy,
|
||
'hide_empty' => false,
|
||
'fields' => 'ids',
|
||
'number' => 20,
|
||
'search' => (string) $query->get('s'),
|
||
]);
|
||
|
||
$query->set('s', '');
|
||
|
||
if (is_wp_error($term_ids) || empty($term_ids)) {
|
||
$query->set('post__in', [0]);
|
||
} else {
|
||
$query->set('tax_query', [[
|
||
'taxonomy' => $taxonomy,
|
||
'field' => 'term_id',
|
||
'terms' => array_map('absint', $term_ids),
|
||
]]);
|
||
}
|
||
}
|
||
}
|
||
});
|
||
|
||
add_filter('template_include', function ($template) {
|
||
if (!get_query_var('aksrt_is_resource_archive')) {
|
||
return $template;
|
||
}
|
||
$candidate = locate_template(['archive-resources.php']);
|
||
return $candidate ?: $template;
|
||
});
|
||
|
||
add_filter('document_title_parts', function ($parts) {
|
||
if (get_query_var('aksrt_is_resource_archive')) {
|
||
$parts['title'] = __('资源', 'aksrt');
|
||
}
|
||
return $parts;
|
||
});
|
||
|
||
// ============================================================
|