| 1 |
<?php |
| 2 |
/* |
| 3 |
Name: 缩略图设置 |
| 4 |
URI: https://mp.weixin.qq.com/s/93TRBqSdiTzissW-c0bLRQ |
| 5 |
Description: 缩略图设置可以无需预定义就可以进行动态裁图,并且还支持文章和分类缩略图 |
| 6 |
Version: 2.0 |
| 7 |
*/ |
| 8 |
class WPJAM_Thumbnail{ |
| 9 |
public static function __callStatic($method, $args){ |
| 10 |
return wpjam_call_option(static::class, $method, ...$args); |
| 11 |
} |
| 12 |
|
| 13 |
public static function get_fields(){ |
| 14 |
$tax_options = wpjam_reduce(get_object_taxonomies('post', 'objects'), fn($c, $v, $k)=> $v->show_ui && $v->public ? wpjam_set($c, $k, wpjam_get_taxonomy_setting($k, 'title')) : $c, []); |
| 15 |
|
| 16 |
$show_if = ['term_thumbnail_type', '!=', '']; |
| 17 |
$term_fields = [ |
| 18 |
'type' => ['options'=>[''=>'� |
| 19 |
�闭分类缩略图', 'img'=>'本地媒体模式', 'image'=>'输� |
| 20 |
�图片链接模式']], |
| 21 |
'taxonomies' => ['type'=>'checkbox', 'before'=>'支持的分类模式:', 'show_if'=>$show_if, 'options'=>$tax_options], |
| 22 |
'size' => ['type'=>'size', 'before'=>'缩略图尺寸:', 'show_if'=>$show_if], |
| 23 |
]; |
| 24 |
|
| 25 |
$tax_options = wpjam_map($tax_options, fn($v, $k)=> ['title'=>$v, 'show_if'=>['term_thumbnail_taxonomies', 'IN', $k]]); |
| 26 |
$order_options = [ |
| 27 |
'' => '请选择来源', |
| 28 |
'first' => '第一张图', |
| 29 |
'post_meta' => [ |
| 30 |
'label' => '自定义字段', |
| 31 |
'fields' => ['post_meta'=>['type'=>'text', 'class'=>'all-options', 'placeholder'=>'请输� |
| 32 |
�自定义字段的 meta_key']] |
| 33 |
], |
| 34 |
'term' =>[ |
| 35 |
'label' => '分类缩略图', |
| 36 |
'show_if' => ['term_thumbnail_type', 'IN', ['img','image']], |
| 37 |
'fields' => ['taxonomy'=>['options'=>[''=>'请选择分类模式']+$tax_options]] |
| 38 |
] |
| 39 |
]; |
| 40 |
|
| 41 |
return [ |
| 42 |
'auto' => ['title'=>'缩略图设置', 'type'=>'radio', 'direction'=>'column', 'options'=>[ |
| 43 |
0 =>'修改主题代码,手动使用 <a href="https://blog.wpjam.com/m/wpjam-basic-thumbnail-functions/" target="_blank">WPJAM 的缩略图函数</a>。', |
| 44 |
1 =>'无需修改主题,自动应用 WPJAM 的缩略图设置。' |
| 45 |
]], |
| 46 |
'pdf' => ['title'=>'PDF预览图', 'name'=>'disable_pdf_preview', 'label'=>'屏蔽 PDF 生成预览图功能。'], |
| 47 |
'default' => ['title'=>'默认缩略图', 'type'=>'mu-img', 'item_type'=>'url'], |
| 48 |
'term_set' => ['title'=>'分类缩略图', 'prefix'=>'term_thumbnail', 'fields'=>$term_fields], |
| 49 |
|
| 50 |
'post_thumbnail_orders' => ['title'=>'文章缩略图', 'type'=>'mu-fields', 'group'=>true, 'max_items'=>5, 'before'=>'首� |
| 51 |
�使用文章特色图片,如未设置,将按� |
| 52 |
�下面的顺序获取:<br />', 'fields'=>['type'=>['options'=>$order_options]]] |
| 53 |
]; |
| 54 |
} |
| 55 |
|
| 56 |
public static function get_default(){ |
| 57 |
$default = self::get_setting('default', []); |
| 58 |
$default = $default && is_array($default) ? $default[array_rand($default)] : ''; |
| 59 |
|
| 60 |
return apply_filters('wpjam_default_thumbnail_url', $default); |
| 61 |
} |
| 62 |
|
| 63 |
public static function get_post_thumbnail_url($post, $order=null){ |
| 64 |
if($order){ |
| 65 |
if($order['type'] == 'first'){ |
| 66 |
return wpjam_get_post_first_image_url($post); |
| 67 |
}elseif($order['type'] == 'post_meta'){ |
| 68 |
if(!empty($order['post_meta'])){ |
| 69 |
return get_post_meta($post->ID, $order['post_meta'], true); |
| 70 |
} |
| 71 |
}elseif($order['type'] == 'term'){ |
| 72 |
if($order['taxonomy'] && is_object_in_taxonomy($post, $order['taxonomy'])){ |
| 73 |
return wpjam_find(get_the_terms($post, $order['taxonomy']) ?: [], fn($v)=> $v, fn($term)=> wpjam_get_term_thumbnail_url($term)); |
| 74 |
} |
| 75 |
} |
| 76 |
}else{ |
| 77 |
foreach(self::get_setting('post_thumbnail_orders') ?: [] as $order){ |
| 78 |
if($order && ($url = self::get_post_thumbnail_url($post, $order))){ |
| 79 |
return $url; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
public static function get_post_thumbnail_html($post_id, $size, $attr){ |
| 86 |
if(self::get_setting('auto')){ |
| 87 |
$src = wpjam_get_post_thumbnail_url($post_id, wpjam_parse_size($size, 2)); |
| 88 |
}else{ |
| 89 |
$images = wpjam_get_post_images($post_id, false); |
| 90 |
$src = $images ? wpjam_get_thumbnail(array_first($images), wpjam_parse_size($size, 2)) : ''; |
| 91 |
} |
| 92 |
|
| 93 |
if($src){ |
| 94 |
$name = is_array($size) ? join('x', $size) : $size; |
| 95 |
$attr = wp_parse_args($attr, ['src'=>$src, 'class'=>"attachment-$name size-$name wp-post-image", 'decoding'=>'async', 'loading'=>'lazy']); |
| 96 |
|
| 97 |
return (string)wpjam_tag('img', $attr)->attr(wpjam_pick(wpjam_parse_size($size), ['width', 'height'])); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
public static function init(){ |
| 102 |
foreach(self::get_setting('term_thumbnail_taxonomies') ?: [] as $tax){ |
| 103 |
if(is_object_in_taxonomy('post', $tax) && ($object = wpjam_get_taxonomy($tax))){ |
| 104 |
$object->add_support('thumbnail')->update_args(wpjam_fill(['thumbnail_type', 'thumbnail_size'], fn($k)=> self::get_setting('term_'.$k))); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
public static function add_hooks(){ |
| 110 |
return [ |
| 111 |
self::get_setting('disable_pdf_preview') ? ['fallback_intermediate_image_sizes', []] : '', |
| 112 |
self::get_setting('auto') ? ['has_post_thumbnail', fn($has, $post)=> $has ?: (bool)wpjam_get_post_thumbnail_url($post), 10, 2] : '', |
| 113 |
['wpjam_post_thumbnail_url', fn($url, $post)=> is_object_in_taxonomy($post, 'category') ? (self::get_post_thumbnail_url($post) ?: ($url ?: self::get_default())) : $url, 1, 2], |
| 114 |
['post_thumbnail_html', fn($html, $id, $_thumb_id, $size, $attr)=> self::get_post_thumbnail_html($id, $size, $attr) ?: $html, 10, 5] |
| 115 |
]; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
function wpjam_get_default_thumbnail_url($size='full', $crop=1){ |
| 120 |
return wpjam_get_thumbnail(WPJAM_Thumbnail::get_default(), $size, $crop); |
| 121 |
} |
| 122 |
|
| 123 |
wpjam_register_option('wpjam-thumbnail', [ |
| 124 |
'title' => '缩略图设置', |
| 125 |
'model' => 'WPJAM_Thumbnail', |
| 126 |
'site_default' => true, |
| 127 |
'menu_page' => ['parent'=>'wpjam-basic', 'position'=>3, 'summary'=>__FILE__] |
| 128 |
]); |
| 129 |
|