PluginProbe
WPJAM Basic / trunk
WPJAM Basic vtrunk
wpjam-basic / extends / related-posts.php

related-posts.php in WPJAM Basic trunk, at extends/related-posts.php

106 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Name: 相�
4 �文章
5 URI: https://mp.weixin.qq.com/s/J6xYFAySlaaVw8_WyDGa1w
6 Description: 相�
7 �文章扩展根据文章的标签和分类自动生成相�
8 �文章列表,并显示在文章末尾。
9 Version: 1.0
10 */
11 class WPJAM_Related_Posts{
12 public static function __callStatic($method, $args){
13 return wpjam_call_option(static::class, $method, ...$args);
14 }
15
16 public static function get_fields(){
17 $options = self::get_options();
18
19 return [
20 'title' => ['title'=>'列表标题', 'type'=>'text', 'value'=>'相�
21 �文章', 'class'=>''],
22 'list' => ['title'=>'列表设置', 'sep'=>'', 'fields'=>[
23 'number' => ['type'=>'number', 'value'=>5, 'class'=>'small-text', 'before'=>'显示', 'after'=>'篇相�
24 �文章,'],
25 'days' => ['type'=>'number', 'value'=>0, 'class'=>'small-text', 'before'=>'从最近', 'after'=>'天的文章中筛选,0则不限制。'],
26 ]],
27 'item' => ['title'=>'列表�
28 ', 'fields'=>[
29 'excerpt' => ['label'=>'显示文章摘要。', 'id'=>'_excerpt'],
30 'thumb' => ['label'=>'显示文章缩略图。', 'group'=>'size', 'value'=>1, 'fields'=>[
31 'size' => ['type'=>'size', 'group'=>'size', 'before'=>'缩略图尺寸:'],
32 ]]
33 ], 'description'=>['如勾选之后缩略图不显示,请到「<a href="'.admin_url('page=wpjam-thumbnail').'">缩略图设置</a>」勾选「无需修改主题,自动应用 WPJAM 的缩略图设置」。', ['show_if'=>['thumb', 1]]]],
34 ]+(get_theme_support('related-posts') ? [] : [
35 'style' => ['title'=>'列表样式', 'type'=>'fieldset', 'fields'=>[
36 'div_id' => ['type'=>'text', 'class'=>'', 'value'=>'related_posts', 'before'=>'外层 DIV id: &emsp;', 'after'=>'不填则无外层 DIV。'],
37 'class' => ['type'=>'text', 'class'=>'', 'value'=>'', 'before'=>'列表 UL class'],
38 ]],
39 'auto' => ['title'=>'自动附加', 'value'=>1, 'label'=>'自动附加到文章末尾。'],
40 ])+(count($options) <= 1 ? [] : [
41 'post_types' => ['title'=>'文章类型', 'before'=>'显示相�
42 �文章的文章类型:', 'type'=>'checkbox', 'options'=>$options]
43 ]);
44 }
45
46 public static function get_options(){
47 return ['post'=>__('Post')]+wpjam_reduce(get_post_types(['_builtin'=>false]), fn($c, $v, $k)=> is_post_type_viewable($v) && get_object_taxonomies($v) ? wpjam_set($c, $v, wpjam_get_post_type_setting($v, 'title')) : $c, []);
48 }
49
50 public static function on_the_post($post, $query){
51 $options = self::get_options();
52 $options = count($options) > 1 && ($setting = self::get_setting('post_types')) ? wpjam_pick($options, $setting) : $options;
53
54 if(!isset($options[$post->post_type])){
55 return;
56 }
57
58 $args = self::get_setting() ?: [];
59
60 if(current_theme_supports('related-posts')){
61 $support = get_theme_support('related-posts');
62 $args = array_merge(is_array($support) ? array_first($support) : [], wpjam_except($args, ['div_id', 'class', 'auto']));
63
64 add_theme_support('related-posts', $args);
65 }
66
67 if(wpjam_is_json_request()){
68 if(!empty($args['thumb']) && !empty($args['size'])){
69 $args['size'] = wpjam_parse_size($args['size'], 2);
70 }
71
72 empty($args['rendered']) && wpjam_once('wpjam_post_json', '+', fn($json, $id, $args)=> wpjam_is(wpjam_get($args, 'query'), 'single', $id) ? ['related'=>wpjam_get_related_posts($id, $args, true)] : [], 10, 3);
73 }else{
74 !empty($args['auto']) && wpjam_once('the_content', '.', fn()=> wpjam_is('single', get_the_ID()) ? wpjam_get_related_posts(get_the_ID(), $args, false) : '', 11);
75 }
76 }
77
78 public static function shortcode($atts){
79 return !empty($atts['tag']) ? wpjam_query('render', [
80 'post_type' => 'any',
81 'no_found_rows' => true,
82 'post_status' => 'publish',
83 'post__not_in' => [get_the_ID()],
84 'tax_query' => [[
85 'taxonomy' => 'post_tag',
86 'terms' => wp_parse_list($atts['tag']),
87 'operator' => 'AND',
88 'field' => 'name'
89 ]]
90 ], ['thumb'=>false, 'class'=>'related-posts']) : '';
91 }
92
93 public static function add_hooks(){
94 is_admin() || wpjam_once('the_post', fn($post, $query)=> wpjam_is($query, 'single', $post->ID) && self::on_the_post($post, $query), 10, 2);
95
96 add_shortcode('related', [self::class, 'shortcode']);
97 }
98 }
99
100 wpjam_register_option('wpjam-related-posts', [
101 'model' => 'WPJAM_Related_Posts',
102 'title' => '相�
103 �文章',
104 'menu_page' => ['tab_slug'=>'related', 'plugin_page'=>'wpjam-posts', 'order'=>19, 'summary'=>__FILE__]
105 ]);
106