| 1 |
<?php |
| 2 |
/* |
| 3 |
Name: 文章数量 |
| 4 |
URI: https://mp.weixin.qq.com/s/gY0AG1vnR285bmOfKO8SCw |
| 5 |
Description: 文章数量扩展可以设置不同页面不同的文章列表数量和文章类型,也可开启不同的分类不同文章数量。 |
| 6 |
Version: 2.0 |
| 7 |
*/ |
| 8 |
class WPJAM_Posts_Per_Page{ |
| 9 |
public static function __callStatic($method, $args){ |
| 10 |
return wpjam_call_option(static::class, $method, ...$args); |
| 11 |
} |
| 12 |
|
| 13 |
public static function sanitize_callback($value){ |
| 14 |
wpjam_map(wpjam_pull($value, ['posts_per_page', 'posts_per_rss']), fn($v, $k)=> $v ? update_option($k, $v) : null); |
| 15 |
|
| 16 |
return $value; |
| 17 |
} |
| 18 |
|
| 19 |
public static function get_fields(){ |
| 20 |
$field = ['type'=>'number', 'class'=>'small-text']; |
| 21 |
|
| 22 |
$fields = [ |
| 23 |
'posts_per_page' => $field+['title'=>'� |
| 24 |
�局', 'value_callback'=>fn()=> get_option('posts_per_page'), 'description'=>'博客� |
| 25 |
�局设置的文章列表数量'], |
| 26 |
'home' => $field+['title'=>'首页'], |
| 27 |
'posts_per_rss' => $field+['title'=>'Feed', 'value_callback'=>fn()=> get_option('posts_per_rss')] |
| 28 |
]; |
| 29 |
|
| 30 |
if($pt_objects = WPJAM_Post_Type::get_registereds(['exclude_from_search'=>false, '_builtin'=>false], 'objects')){ |
| 31 |
$options = ['post'=>'文章']+wp_list_pluck($pt_objects, 'title'); |
| 32 |
$ptype_field = ['before'=>'文章类型:', 'type'=>'checkbox', 'value'=>['post'], 'options'=>$options]; |
| 33 |
|
| 34 |
$fields['home'] = ['title'=>'首页', 'fields'=>[ |
| 35 |
'home' => $field+['before'=>'文章数量:'], |
| 36 |
'home_post_types' => $ptype_field, |
| 37 |
]]; |
| 38 |
|
| 39 |
$fields['posts_per_rss'] = ['title'=>'Feed', 'fields'=>[ |
| 40 |
'posts_per_rss' => wpjam_except($fields['posts_per_rss'], 'title')+['before'=>'文章数量:'], |
| 41 |
'feed_post_types' => $ptype_field, |
| 42 |
]]; |
| 43 |
} |
| 44 |
|
| 45 |
foreach(wpjam_sort(get_taxonomies(['public'=>true,'show_ui'=>true], 'objects'), ['hierarchical'=>'DESC']) as $tax => $tax_object){ |
| 46 |
if(isset($tax_object->posts_per_page)){ |
| 47 |
continue; |
| 48 |
} |
| 49 |
|
| 50 |
$title = wpjam_get_taxonomy_setting($tax, 'title'); |
| 51 |
|
| 52 |
$tax_fields[$tax] = ['title'=>$title, 'group'=>'tax']+$field; |
| 53 |
|
| 54 |
if($tax_object->hierarchical){ |
| 55 |
$individual[$tax.'_individual'] = ['label'=>'每个'.$title.'可独立设置', 'show_if'=>[$tax, '!=', '']]; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
$other_fields = wpjam_map(['author'=>'作� |
| 60 |
页','search'=>'搜索页','archive'=>'存档页'], fn($v)=>$field+['title'=>$v]); |
| 61 |
$fields['other'] = ['title'=>'� |
| 62 |
�他页面', 'wrap_tag'=>'fieldset', 'group'=>true, 'sep'=>'<br />', 'fields'=>$other_fields]; |
| 63 |
$fields['tax'] = ['title'=>'分类模式', 'wrap_tag'=>'fieldset', 'fields'=>$tax_fields+(empty($individual) ? [] : $individual)]; |
| 64 |
|
| 65 |
foreach(get_post_types(['public'=>true, 'has_archive'=>true]) as $post_type){ |
| 66 |
if(wpjam_get_post_type_setting($post_type, 'posts_per_page')){ |
| 67 |
continue; |
| 68 |
} |
| 69 |
|
| 70 |
$pt_field[$post_type] = $field+['title'=>wpjam_get_post_type_setting($post_type, 'title')]; |
| 71 |
} |
| 72 |
|
| 73 |
if(!empty($pt_field)){ |
| 74 |
$fields['post_type_set'] = ['title'=>'文章类型<br />存档页面', 'group'=>true, 'sep'=>'<br />', 'wrap_tag'=>'fieldset','fields'=>$pt_field]; |
| 75 |
} |
| 76 |
|
| 77 |
return $fields; |
| 78 |
} |
| 79 |
|
| 80 |
public static function builtin_page_load($screen){ |
| 81 |
if(is_taxonomy_hierarchical($screen->taxonomy) && self::get_setting($screen->taxonomy.'_individual')){ |
| 82 |
$default = self::get_setting($screen->taxonomy) ?: get_option('posts_per_page'); |
| 83 |
|
| 84 |
wpjam_register_list_table_action('posts_per_page',[ |
| 85 |
'title' => '文章数量', |
| 86 |
'page_title' => '设置文章数量', |
| 87 |
'submit_text' => '设置', |
| 88 |
'fields' => [ |
| 89 |
'default' => ['title'=>'默认数量', 'type'=>'view', 'value'=>$default], |
| 90 |
'posts_per_page' => ['title'=>'文章数量', 'type'=>'number', 'class'=>'small-text'] |
| 91 |
] |
| 92 |
]); |
| 93 |
|
| 94 |
wpjam_hook($screen->taxonomy.'_row_actions', '+', fn($term, $actions)=> ($v = get_term_meta($term->term_id, 'posts_per_page', true)) ? ['posts_per_page'=>str_replace('>文章数量<', '>文章数量'.'('.$v.')'.'<', $actions['posts_per_page'])] : [], 10, 2); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
public static function on_pre_get_posts($wp_query){ |
| 99 |
if(!$wp_query->is_main_query() || (wp_doing_ajax() && get_current_screen())){ |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
$is = wpjam_is($wp_query); |
| 104 |
$queried = $wp_query->get_queried_object(); |
| 105 |
|
| 106 |
if(in_array($is, ['home', 'author', 'search', 'archive'], true)){ |
| 107 |
$number = self::get_setting($is); |
| 108 |
}elseif(in_array($is, ['category', 'tag', 'tax'], true)){ |
| 109 |
if($queried){ |
| 110 |
$tax = $queried->taxonomy; |
| 111 |
$number = wpjam_get_taxonomy_setting($tax, 'posts_per_page') ?: self::get_setting($tax); |
| 112 |
|
| 113 |
if(self::get_setting($tax.'_individual')){ |
| 114 |
$number = get_term_meta($queried->term_id, 'posts_per_page', true) ?: $number; |
| 115 |
} |
| 116 |
} |
| 117 |
}elseif($is === 'post_type_archive'){ |
| 118 |
if($queried){ |
| 119 |
$number = wpjam_get_post_type_setting($queried->name, 'posts_per_page') ?: self::get_setting($queried->name); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
empty($number) || $wp_query->set('posts_per_page', $number); |
| 124 |
|
| 125 |
if(!isset($wp_query->query['post_type']) && get_post_types(['exclude_from_search'=>false, '_builtin'=>false])){ |
| 126 |
if(in_array($is, ['home', 'feed'], true)){ |
| 127 |
$post_types = self::get_setting($is.'_post_types'); |
| 128 |
}elseif(in_array($is, ['category', 'tag'], true)){ |
| 129 |
if($queried){ |
| 130 |
$post_types = array_intersect(get_taxonomy($queried->taxonomy)->object_type, get_post_types(['public'=>true])); |
| 131 |
} |
| 132 |
}elseif($is === 'author'){ |
| 133 |
$post_types = array_intersect(get_post_types_by_support('author'), get_post_types(['public'=>true])); |
| 134 |
}elseif($is === 'archive'){ |
| 135 |
$post_types = 'any'; |
| 136 |
} |
| 137 |
|
| 138 |
empty($post_types) || $wp_query->set('post_type', (is_array($post_types) && count($post_types) == 1) ? array_first($post_types) : $post_types); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
public static function add_hooks(){ |
| 143 |
(!is_admin() || wp_doing_ajax()) && add_action('pre_get_posts', [self::class, 'on_pre_get_posts']); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
wpjam_register_option('wpjam-posts-per-page', [ |
| 148 |
'model' => 'WPJAM_Posts_Per_Page', |
| 149 |
'title' => '文章数量', |
| 150 |
'menu_page' => ['tab_slug'=>'posts-per-page', 'plugin_page'=>'wpjam-posts', 'order'=>18, 'summary'=>__FILE__], |
| 151 |
'admin_load' => ['base'=>'edit-tags'] |
| 152 |
]); |
| 153 |
|