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);
// ============================================================
// WP 后台文章列表增强列 — 浏览/点赞/收藏/下载
// ============================================================
add_filter('manage_post_posts_columns', function ($columns) {
$new_columns = [];
foreach ($columns as $key => $label) {
$new_columns[$key] = $label;
if ($key === 'title') {
$new_columns['aksrt_stats'] = __('互动数据', 'aksrt');
}
}
return $new_columns;
});
add_action('admin_enqueue_scripts', function ($hook) {
if ($hook !== 'edit.php') return;
aksrt_enqueue_fontawesome();
});
add_action('manage_post_posts_custom_column', function ($column, $post_id) {
if ($column !== 'aksrt_stats') return;
$views = (int) get_post_meta($post_id, 'views', true);
$likes = (int) get_post_meta($post_id, 'like', true);
$favorites = (int) get_post_meta($post_id, 'favorite', true);
$downloads = (int) get_post_meta($post_id, 'download_count', true);
$format = function (int $n): string {
if ($n >= 10000) return round($n / 10000, 1) . '万';
if ($n >= 1000) return round($n / 1000, 1) . 'k';
return (string) $n;
};
$parts = [];
$parts[] = ' ' . esc_html($format($views)) . '';
$parts[] = ' ' . esc_html($format($likes)) . '';
$parts[] = ' ' . esc_html($format($favorites)) . '';
if ($downloads > 0) {
$parts[] = ' ' . esc_html($format($downloads)) . '';
}
echo implode(' · ', $parts);
}, 10, 2);
// Support sorting by views/likes/favorites in admin
add_action('pre_get_posts', function ($query) {
if (!is_admin() || !$query->is_main_query()) return;
$orderby = $query->get('orderby');
$allowed = ['views', 'like', 'favorite', 'download_count'];
if (in_array($orderby, $allowed, true)) {
$query->set('meta_key', $orderby);
$query->set('orderby', 'meta_value_num');
}
});
// ============================================================
// 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);