| 1 |
<?php |
| 2 |
/* |
| 3 |
Name: 文章浏览 |
| 4 |
URI: https://blog.wpjam.com/m/wpjam-postviews/ |
| 5 |
Description: 统计文章� |
| 6 |
读数,激活该扩展,请不要再激活 WP-Postviews 插件。 |
| 7 |
Version: 1.0 |
| 8 |
*/ |
| 9 |
class WPJAM_Postviews{ |
| 10 |
public static function get_fields(){ |
| 11 |
return ['postviews'=> ['title'=>'初始浏览量', 'sep'=>' ', 'prefix'=>'views', 'fields'=>[ |
| 12 |
'begin' => ['type'=>'number', 'class'=>'small-text'], |
| 13 |
'v1' => '和', |
| 14 |
'end' => ['type'=>'number', 'class'=>'small-text'], |
| 15 |
'v2' => '之间随机数', |
| 16 |
]]]; |
| 17 |
} |
| 18 |
|
| 19 |
public static function redirect(){ |
| 20 |
if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) && substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')){ |
| 21 |
ob_start('ob_gzhandler'); |
| 22 |
} |
| 23 |
|
| 24 |
header("Content-Type: image/png"); |
| 25 |
|
| 26 |
$pid = $GLOBALS['wp']->query_vars['p']; |
| 27 |
$views = wpjam_get_post_views($pid); |
| 28 |
$im = @imagecreate(120, 32) or die("Cannot Initialize new GD image stream"); |
| 29 |
$bg = imagecolorallocate($im, 191, 191, 191); |
| 30 |
$color = imagecolorallocate($im, 127, 31, 31); |
| 31 |
$font = 5; |
| 32 |
$y = 8; |
| 33 |
$x = 18 - (strlen($views) - 3)*4; |
| 34 |
|
| 35 |
imagestring($im, $font, $x, $y, $views.' views', $color); |
| 36 |
imagepng($im); |
| 37 |
imagedestroy($im); |
| 38 |
|
| 39 |
wpjam_update_post_views($pid); |
| 40 |
|
| 41 |
exit; |
| 42 |
} |
| 43 |
|
| 44 |
public static function match_callback($post_type, $object){ |
| 45 |
$pt_object = ($post_type && $post_type != 'attachment') ? get_post_type_object($post_type) : null; |
| 46 |
|
| 47 |
if(!$pt_object || (empty($pt_object->viewable) && !is_post_type_viewable($post_type))){ |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
$object->capability = $pt_object->cap->edit_others_posts; |
| 52 |
|
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
public static function update_views($post_id, $data){ |
| 57 |
!empty($data['views']) && update_post_meta($post_id, 'views', $data['views']); |
| 58 |
|
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
public static function add_views($post_id, $data){ |
| 63 |
!empty($data['addon']) && wpjam_update_post_views($post_id, $data['addon']); |
| 64 |
|
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
public static function api_callback($data, $grant){ |
| 69 |
$post_id = (int)$data['post_id']; |
| 70 |
$views = wpjam_get_post_views($post_id); |
| 71 |
|
| 72 |
if($grant){ |
| 73 |
$views = $views + (int)$data['views']; |
| 74 |
|
| 75 |
update_post_meta($post_id, 'views', $views); |
| 76 |
} |
| 77 |
|
| 78 |
return ['views'=>$views]; |
| 79 |
} |
| 80 |
|
| 81 |
public static function api_fields($grant){ |
| 82 |
$fields = ['post_id'=>['required'=>true, 'validate_callback'=>['WPJAM_Post','validate']]]; |
| 83 |
|
| 84 |
return $fields + ($grant ? ['views'=>['required'=>true]] : []); |
| 85 |
} |
| 86 |
|
| 87 |
public static function get_rewrite_rule(){ |
| 88 |
return ['postviews/([0-9]+)\.png?$', 'index.php?module=postviews&p=$matches[1]', 'top']; |
| 89 |
} |
| 90 |
|
| 91 |
public static function init(){ |
| 92 |
if(is_admin()){ |
| 93 |
wpjam_register_list_table_action('update_views', [ |
| 94 |
'data_type' => 'post_type', |
| 95 |
'post_type' => [self::class, 'match_callback'], |
| 96 |
'callback' => [self::class, 'update_views'], |
| 97 |
'title' => '修改浏览数', |
| 98 |
'page_title' => '修改浏览数', |
| 99 |
'submit_text' => '修改', |
| 100 |
'row_action' => false, |
| 101 |
'fields' => ['views'=>['title'=>'浏览数', 'type'=>'number']] |
| 102 |
]); |
| 103 |
|
| 104 |
wpjam_register_list_table_action('add_views', [ |
| 105 |
'data_type' => 'post_type', |
| 106 |
'post_type' => [self::class, 'match_callback'], |
| 107 |
'callback' => [self::class, 'add_views'], |
| 108 |
'title' => '增加浏览数', |
| 109 |
'page_title' => '增加浏览数', |
| 110 |
'submit_text' => '增加', |
| 111 |
'row_action' => false, |
| 112 |
'bulk' => 2, |
| 113 |
'fields' => [ |
| 114 |
'view' =>['title'=>'使用说明', 'type'=>'view', 'value'=>'批量处理是在原有的浏览量上增加'], |
| 115 |
'addon' =>['title'=>'浏览数增量', 'type'=>'number'] |
| 116 |
] |
| 117 |
]); |
| 118 |
|
| 119 |
wpjam_register_list_table_column('views', [ |
| 120 |
'title' => '浏览', |
| 121 |
'data_type' => 'post_type', |
| 122 |
'post_type' => [self::class, 'match_callback'], |
| 123 |
'sortable' => 'views', |
| 124 |
'style' => 'width:7%;', |
| 125 |
'callback' => fn($id) => ['row_action'=>'update_views', 'title'=>wpjam_get_post_views($id) ?: 0, 'fallback'=>true], |
| 126 |
]); |
| 127 |
} |
| 128 |
|
| 129 |
$grant = $_SERVER['REQUEST_METHOD'] == 'POST'; |
| 130 |
|
| 131 |
wpjam_register_json('post.views', [ |
| 132 |
'callback' => fn($data)=> self::api_callback($data, $grant), |
| 133 |
'fields' => fn()=> self::api_fields($grant), |
| 134 |
'grant' => $grant |
| 135 |
]); |
| 136 |
} |
| 137 |
|
| 138 |
public static function add_hooks(){ |
| 139 |
wpjam_route('postviews', self::class); |
| 140 |
|
| 141 |
wpjam_hook('the_content', '.', fn()=> is_feed() ? "\n".'<p><img src="'.home_url('postviews/'.get_the_ID().'.png').'" /></p>'."\n" : '', 999); |
| 142 |
|
| 143 |
// 不指定 post_type ,默认查询 post,这样custom post type 的文章页面就会显示 404 |
| 144 |
// add_action('pre_get_posts', fn($query)=> $query->get('module') == 'postviews' && $query->set('post_type', 'any')); |
| 145 |
|
| 146 |
$begin = (int)wpjam_basic_get_setting('views_begin'); |
| 147 |
$end = (int)wpjam_basic_get_setting('views_end'); |
| 148 |
$views = ($begin && $end) ? rand(min($begin, $end), max($begin, $end)) : 0; |
| 149 |
|
| 150 |
$views && add_action('wp_after_insert_post', fn($post_id, $post, $update)=> (!$update && $post->post_type != 'attachment' && is_post_type_viewable($post->post_type)) && update_post_meta($post_id, 'views', $views), 999, 3); |
| 151 |
|
| 152 |
wpjam_once('wpjam_post_json', '+', fn($id, $args)=> wpjam_is(wpjam_get($args, 'query'), 'single, page', $id) ? ['views'=>wpjam_update_post_views($id)] : [], 10, 3); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
wpjam_add_option_section('wpjam-basic', 'posts', ['model'=>'WPJAM_Postviews']); |
| 157 |
|
| 158 |
function wpjam_get_post_total_views($post_id){ |
| 159 |
return wpjam_get_post_views($post_id); |
| 160 |
} |
| 161 |
|
| 162 |
if(!function_exists('the_views')){ |
| 163 |
function the_views(){ |
| 164 |
echo '<span class="view">浏览:'.(wpjam_get_post_views(get_the_ID()) ?: 0).'</span>'; |
| 165 |
} |
| 166 |
|
| 167 |
add_action('wp_head', fn()=> is_single() && wpjam_get_query_var('module') != 'json' && wpjam_update_post_views(get_queried_object_id())); |
| 168 |
} |
| 169 |
|