post_type !== 'post') { return; } if (!class_exists('\AKSRT\Settings\Settings')) { return; } $page = \AKSRT\Settings\Settings::get_post_page_settings(); $views_min = max(0, (int) ($page['default_views_min'] ?? 20)); $views_max = max($views_min, (int) ($page['default_views_max'] ?? 50)); $likes_min = max(0, (int) ($page['default_likes_min'] ?? 5)); $likes_max = max($likes_min, (int) ($page['default_likes_max'] ?? 15)); if ('' === (string) get_post_meta($post_id, \AKSRT\Core\DomainModel::POST_VIEW_COUNT_KEY, true)) { update_post_meta($post_id, \AKSRT\Core\DomainModel::POST_VIEW_COUNT_KEY, wp_rand($views_min, $views_max)); } if ('' === (string) get_post_meta($post_id, '_aksrt_like_count', true)) { update_post_meta($post_id, '_aksrt_like_count', wp_rand($likes_min, $likes_max)); } }, 10, 2); // ============================================================ // 前端隐藏 WordPress Admin Bar:由 SystemTools 按「扩展&增强 → 系统工具」 // 的 hide_admin_bar 设置门控(默认开启),不再无条件注入。 // ============================================================ // ============================================================ // SMTP 邮件配置注入 // ============================================================ add_filter('wp_mail_from', function ($from) { if (!class_exists('\AKSRT\Settings\Settings')) return $from; $mail = \AKSRT\Settings\Settings::get_smtp(); if (empty($mail['enabled'])) return $from; $configured = sanitize_email((string) ($mail['from_address'] ?? '')); return $configured !== '' ? $configured : $from; }); add_filter('wp_mail_from_name', function ($from_name) { if (!class_exists('\AKSRT\Settings\Settings')) return $from_name; $mail = \AKSRT\Settings\Settings::get_smtp(); if (empty($mail['enabled'])) return $from_name; $configured = trim((string) ($mail['from_name'] ?? '')); return $configured !== '' ? $configured : $from_name; }); add_action('phpmailer_init', function ($phpmailer) { $mail = class_exists('\\AKSRT\\Settings\\Settings') ? \AKSRT\Settings\Settings::get_smtp() : []; if (empty($mail['enabled']) || empty($mail['host'])) return; $phpmailer->isSMTP(); $phpmailer->Host = $mail['host']; $phpmailer->Port = (int) ($mail['port'] ?? 465); $phpmailer->SMTPAuth = !empty($mail['auth']) && !empty($mail['username']); $phpmailer->Username = (string) ($mail['username'] ?? ''); $phpmailer->Password = (string) ($mail['password'] ?? ''); $phpmailer->SMTPSecure = (string) ($mail['encryption'] ?? 'ssl'); $phpmailer->Timeout = 10; $phpmailer->Timelimit = 15; }); // mail_title_prefix is applied by the canonical renderer and by third-party wp_mail callers. add_filter('wp_mail', function ($atts) { if (!class_exists('\AKSRT\Settings\Settings') || empty($atts['subject'])) return $atts; $branding = \AKSRT\Settings\Settings::get_mail_branding(); $subject = (string) $atts['subject']; $site = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); $subject = preg_replace('/^\[' . preg_quote($site, '/') . '\]\s*/u', '', $subject); if ($branding['title_prefix'] !== '' && strpos($subject, $branding['title_prefix']) !== 0) { $atts['subject'] = $branding['title_prefix'] . ' ' . $subject; } else { $atts['subject'] = $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); ?>
and
$enlighter_attrs = [
'data-enlighter-language' => true,
'data-enlighter-theme' => true,
'data-enlighter-group' => true,
'data-enlighter-title' => true,
'data-enlighter-linenumbers' => true,
'data-enlighter-highlight' => true,
'data-enlighter-lineoffset' => true,
];
if (isset($allowed['pre'])) {
$allowed['pre'] = array_merge($allowed['pre'], $enlighter_attrs);
}
if (isset($allowed['code'])) {
$allowed['code'] = array_merge($allowed['code'], $enlighter_attrs);
}
return $allowed;
}, 10, 2);
// ============================================================
// Gutenberg 自定义块注册
// ============================================================
// Register category server-side so it exists before JS registerBlockType runs.
// Keep untyped + defensive: some plugins pass unexpected shapes; typed params can 500 the editor.
add_filter('block_categories_all', static function ($categories, $context = null) {
if (!is_array($categories)) {
return $categories;
}
foreach ($categories as $category) {
$slug = '';
if (is_array($category)) {
$slug = (string) ($category['slug'] ?? '');
} elseif (is_object($category) && isset($category->slug)) {
$slug = (string) $category->slug;
}
if ($slug === 'aksrt-blocks') {
return $categories;
}
}
$categories[] = [
'slug' => 'aksrt-blocks',
'title' => 'AKSRT',
'icon' => null,
];
return $categories;
}, 10, 2);
add_action('enqueue_block_editor_assets', function () {
$base_uri = get_template_directory_uri();
$blocks_js = get_template_directory() . '/assets/js/gutenberg-blocks.js';
if (file_exists($blocks_js)) {
wp_enqueue_script(
'aksrt-gutenberg-blocks',
$base_uri . '/assets/js/gutenberg-blocks.js',
// wp-media-utils: MediaUpload for carousel block
['wp-blocks', 'wp-element', 'wp-components', 'wp-block-editor', 'wp-hooks', 'wp-media-utils', 'wp-data'],
(string) filemtime($blocks_js),
true
);
}
$blocks_css = get_template_directory() . '/assets/css/gutenberg-blocks.css';
if (file_exists($blocks_css)) {
wp_enqueue_style(
'aksrt-gutenberg-blocks',
$base_uri . '/assets/css/gutenberg-blocks.css',
[],
(string) filemtime($blocks_css)
);
}
});
// Front-end styles for Gutenberg blocks
add_action('enqueue_block_assets', function () {
if (is_admin()) return; // Already loaded in editor
$posts = [];
$queried = get_queried_object();
if ($queried instanceof \WP_Post) {
$posts[$queried->ID] = $queried;
}
global $wp_query;
if ($wp_query instanceof \WP_Query) {
foreach ((array) $wp_query->posts as $post) {
if ($post instanceof \WP_Post) {
$posts[$post->ID] = $post;
}
}
}
$has_carousel = false;
foreach ($posts as $post) {
if (has_block('aksrt/carousel', $post)) {
$has_carousel = true;
break;
}
}
if (!$has_carousel) return;
$base_uri = get_template_directory_uri();
$blocks_css = get_template_directory() . '/assets/css/gutenberg-blocks.css';
if (file_exists($blocks_css)) {
wp_enqueue_style(
'aksrt-gutenberg-blocks-front',
$base_uri . '/assets/css/gutenberg-blocks.css',
[],
(string) filemtime($blocks_css)
);
}
});
// ============================================================
// Gutenberg Carousel Block 渲染回调
// ============================================================
add_action('init', function () {
if (!function_exists('register_block_type')) return;
register_block_type('aksrt/carousel', [
'render_callback' => function (array $attributes): string {
$images = $attributes['images'] ?? [];
if (empty($images)) return '';
$autoplay = !empty($attributes['autoplay']) ? 'true' : 'false';
$delay = absint($attributes['delay'] ?? 3000);
$effect = sanitize_text_field($attributes['effect'] ?? 'slide');
$slides = '';
foreach ($images as $img) {
if (empty($img['url'])) continue;
$alt = esc_attr($img['alt'] ?? '');
$url = esc_url($img['url']);
$slides .= '';
}
if (empty($slides)) return '';
return ''
. '' . $slides . ''
. ''
. ''
. '';
},
]);
});
// ============================================================
// 付费内容系统:the_content 过滤器
// ============================================================
/**
* 未购买的图库/视频正文只保留公开预览;购买卡由 single 模板渲染。
*/
add_filter('the_content', 'aksrt_paid_content_filter', 20);