PluginProbe
WP-PostViews / 1.74
WP-PostViews v1.74
2.0.1 2.0.0 1.01 1.02 1.10 1.11 1.20 1.30 1.31 1.40 1.50 1.66 1.67 1.68 1.69 1.70 1.71 1.72 1.73 1.74 1.75 1.76 1.76.1 1.77 1.78 All 29 releases
wp-postviews / wp-postviews.php

wp-postviews.php in WP-PostViews 1.74, at wp-postviews.php

992 lines 46.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: WP-PostViews
4 Plugin URI: https://lesterchan.net/portfolio/programming/php/
5 Description: Enables you to display how many times a post/page had been viewed.
6 Version: 1.74
7 Author: Lester 'GaMerZ' Chan
8 Author URI: https://lesterchan.net
9 Text Domain: wp-postviews
10 */
11
12
13 /*
14 Copyright 2016 Lester Chan (email : lesterchan@gmail.com)
15
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or
19 (at your option) any later version.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 */
30
31
32 ### Create Text Domain For Translations
33 add_action( 'plugins_loaded', 'postviews_textdomain' );
34 function postviews_textdomain() {
35 load_plugin_textdomain( 'wp-postviews', false, dirname( plugin_basename( __FILE__ ) ) );
36 }
37
38
39 ### Function: Post Views Option Menu
40 add_action('admin_menu', 'postviews_menu');
41 function postviews_menu() {
42 if (function_exists('add_options_page')) {
43 add_options_page(__('PostViews', 'wp-postviews'), __('PostViews', 'wp-postviews'), 'manage_options', 'wp-postviews/postviews-options.php') ;
44 }
45 }
46
47
48 ### Function: Calculate Post Views
49 add_action( 'wp_head', 'process_postviews' );
50 function process_postviews() {
51 global $user_ID, $post;
52 if( is_int( $post ) ) {
53 $post = get_post( $post );
54 }
55 if( ! wp_is_post_revision( $post ) && ! is_preview() ) {
56 if( is_single() || is_page() ) {
57 $id = intval( $post->ID );
58 $views_options = get_option( 'views_options' );
59 if ( !$post_views = get_post_meta( $post->ID, 'views', true ) ) {
60 $post_views = 0;
61 }
62 $should_count = false;
63 switch( intval( $views_options['count'] ) ) {
64 case 0:
65 $should_count = true;
66 break;
67 case 1:
68 if(empty( $_COOKIE[USER_COOKIE] ) && intval( $user_ID ) === 0) {
69 $should_count = true;
70 }
71 break;
72 case 2:
73 if( intval( $user_ID ) > 0 ) {
74 $should_count = true;
75 }
76 break;
77 }
78 if( intval( $views_options['exclude_bots'] ) === 1 ) {
79 $bots = array
80 (
81 'Google Bot' => 'google'
82 , 'MSN' => 'msnbot'
83 , 'Alex' => 'ia_archiver'
84 , 'Lycos' => 'lycos'
85 , 'Ask Jeeves' => 'jeeves'
86 , 'Altavista' => 'scooter'
87 , 'AllTheWeb' => 'fast-webcrawler'
88 , 'Inktomi' => 'slurp@inktomi'
89 , 'Turnitin.com' => 'turnitinbot'
90 , 'Technorati' => 'technorati'
91 , 'Yahoo' => 'yahoo'
92 , 'Findexa' => 'findexa'
93 , 'NextLinks' => 'findlinks'
94 , 'Gais' => 'gaisbo'
95 , 'WiseNut' => 'zyborg'
96 , 'WhoisSource' => 'surveybot'
97 , 'Bloglines' => 'bloglines'
98 , 'BlogSearch' => 'blogsearch'
99 , 'PubSub' => 'pubsub'
100 , 'Syndic8' => 'syndic8'
101 , 'RadioUserland' => 'userland'
102 , 'Gigabot' => 'gigabot'
103 , 'Become.com' => 'become.com'
104 , 'Baidu' => 'baiduspider'
105 , 'so.com' => '360spider'
106 , 'Sogou' => 'spider'
107 , 'soso.com' => 'sosospider'
108 , 'Yandex' => 'yandex'
109 );
110 $useragent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
111 foreach ( $bots as $name => $lookfor ) {
112 if ( ! empty( $useragent ) && ( stristr( $useragent, $lookfor ) !== false ) ) {
113 $should_count = false;
114 break;
115 }
116 }
117 }
118 if( $should_count && ( ( isset( $views_options['use_ajax'] ) && intval( $views_options['use_ajax'] ) === 0 ) || ( !defined( 'WP_CACHE' ) || !WP_CACHE ) ) ) {
119 update_post_meta( $id, 'views', ( $post_views + 1 ) );
120 do_action( 'postviews_increment_views', ( $post_views + 1 ) );
121 }
122 }
123 }
124 }
125
126
127 ### Function: Calculate Post Views With WP_CACHE Enabled
128 add_action('wp_enqueue_scripts', 'wp_postview_cache_count_enqueue');
129 function wp_postview_cache_count_enqueue() {
130 global $user_ID, $post;
131
132 if( !defined( 'WP_CACHE' ) || !WP_CACHE )
133 return;
134
135 $views_options = get_option( 'views_options' );
136
137 if( isset( $views_options['use_ajax'] ) && intval( $views_options['use_ajax'] ) === 0 )
138 return;
139
140 if ( !wp_is_post_revision( $post ) && ( is_single() || is_page() ) ) {
141 $should_count = false;
142 switch( intval( $views_options['count'] ) ) {
143 case 0:
144 $should_count = true;
145 break;
146 case 1:
147 if ( empty( $_COOKIE[USER_COOKIE] ) && intval( $user_ID ) === 0) {
148 $should_count = true;
149 }
150 break;
151 case 2:
152 if ( intval( $user_ID ) > 0 ) {
153 $should_count = true;
154 }
155 break;
156 }
157 if ( $should_count ) {
158 wp_enqueue_script( 'wp-postviews-cache', plugins_url( 'postviews-cache.js', __FILE__ ), array( 'jquery' ), '1.68', true );
159 wp_localize_script( 'wp-postviews-cache', 'viewsCacheL10n', array( 'admin_ajax_url' => admin_url( 'admin-ajax.php' ), 'post_id' => intval( $post->ID ) ) );
160 }
161 }
162 }
163
164
165 ### Function: Determine If Post Views Should Be Displayed (By: David Potter)
166 function should_views_be_displayed($views_options = null) {
167 if ($views_options == null) {
168 $views_options = get_option('views_options');
169 }
170 $display_option = 0;
171 if (is_home()) {
172 if (array_key_exists('display_home', $views_options)) {
173 $display_option = $views_options['display_home'];
174 }
175 } elseif (is_single()) {
176 if (array_key_exists('display_single', $views_options)) {
177 $display_option = $views_options['display_single'];
178 }
179 } elseif (is_page()) {
180 if (array_key_exists('display_page', $views_options)) {
181 $display_option = $views_options['display_page'];
182 }
183 } elseif (is_archive()) {
184 if (array_key_exists('display_archive', $views_options)) {
185 $display_option = $views_options['display_archive'];
186 }
187 } elseif (is_search()) {
188 if (array_key_exists('display_search', $views_options)) {
189 $display_option = $views_options['display_search'];
190 }
191 } else {
192 if (array_key_exists('display_other', $views_options)) {
193 $display_option = $views_options['display_other'];
194 }
195 }
196 return (($display_option == 0) || (($display_option == 1) && is_user_logged_in()));
197 }
198
199
200 ### Function: Display The Post Views
201 function the_views($display = true, $prefix = '', $postfix = '', $always = false) {
202 $post_views = intval( get_post_meta( get_the_ID(), 'views', true ) );
203 $views_options = get_option('views_options');
204 if ($always || should_views_be_displayed($views_options)) {
205 $output = $prefix.str_replace( array( '%VIEW_COUNT%', '%VIEW_COUNT_ROUNDED%' ), array( number_format_i18n( $post_views ), postviews_round_number( $post_views) ), stripslashes( $views_options['template'] ) ).$postfix;
206 if($display) {
207 echo apply_filters('the_views', $output);
208 } else {
209 return apply_filters('the_views', $output);
210 }
211 }
212 elseif (!$display) {
213 return '';
214 }
215 }
216
217 ### Function: Short Code For Inserting Views Into Posts
218 add_shortcode( 'views', 'views_shortcode' );
219 function views_shortcode( $atts ) {
220 $attributes = shortcode_atts( array( 'id' => 0 ), $atts );
221 $id = intval( $attributes['id'] );
222 if( $id === 0) {
223 $id = get_the_ID();
224 }
225 $views_options = get_option( 'views_options' );
226 $post_views = intval( get_post_meta( $id, 'views', true ) );
227 $output = str_replace( array( '%VIEW_COUNT%', '%VIEW_COUNT_ROUNDED%' ), array( number_format_i18n( $post_views ), postviews_round_number( $post_views) ), stripslashes( $views_options['template'] ) );
228
229 return apply_filters( 'the_views', $output );
230 }
231
232
233 ### Function: Display Least Viewed Page/Post
234 if(!function_exists('get_least_viewed')) {
235 function get_least_viewed($mode = '', $limit = 10, $chars = 0, $display = true) {
236 global $wpdb;
237 $views_options = get_option('views_options');
238 $where = '';
239 $temp = '';
240 $output = '';
241 if(!empty($mode) && $mode != 'both') {
242 if(is_array($mode)) {
243 $mode = implode("','",$mode);
244 $where = "post_type IN ('".$mode."')";
245 } else {
246 $where = "post_type = '$mode'";
247 }
248 } else {
249 $where = '1=1';
250 }
251 $most_viewed = $wpdb->get_results("SELECT DISTINCT p.*, (pm1.meta_value+0) AS views, IF((pm2.meta_value IS NULL) OR (pm2.meta_value = ''), tt.term_id, pm2.meta_value) AS cat_id FROM $wpdb->posts p LEFT JOIN $wpdb->postmeta pm1 ON pm1.post_id = p.ID LEFT JOIN $wpdb->postmeta pm2 ON (pm2.post_id = p.ID AND pm2.meta_key='_yoast_wpseo_primary_category') INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'category') WHERE post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND pm1.meta_key = 'views' AND post_password = '' GROUP BY p.ID ORDER BY views ASC LIMIT $limit");
252 if($most_viewed) {
253 foreach ($most_viewed as $post) {
254 $post_views = intval($post->views);
255 $post_title = get_the_title($post);
256 if($chars > 0) {
257 $post_title = snippet_text($post_title, $chars);
258 }
259 $post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
260 $thumbnail = get_the_post_thumbnail($post->ID,'thumbnail',true);
261 $temp = stripslashes($views_options['most_viewed_template']);
262 $temp = str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $temp);
263 $temp = str_replace('%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp);
264 $temp = str_replace('%POST_TITLE%', $post_title, $temp);
265 $temp = str_replace('%POST_EXCERPT%', $post_excerpt, $temp);
266 $temp = str_replace('%POST_CONTENT%', $post->post_content, $temp);
267 $temp = str_replace('%POST_URL%', get_permalink($post), $temp);
268 $temp = str_replace('%POST_DATE%', get_the_time(get_option('date_format'), $post), $temp);
269 $temp = str_replace('%POST_TIME%', get_the_time(get_option('time_format'), $post), $temp);
270 $temp = str_replace('%POST_THUMBNAIL%', $thumbnail, $temp);
271 $temp = str_replace('%POST_CATEGORY_ID%', $post->cat_id, $temp);
272 $output .= $temp;
273 }
274 } else {
275 $output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
276 }
277 if($display) {
278 echo $output;
279 } else {
280 return $output;
281 }
282 }
283 }
284
285
286 ### Function: Display Most Viewed Page/Post
287 if(!function_exists('get_most_viewed')) {
288 function get_most_viewed($mode = '', $limit = 10, $chars = 0, $display = true) {
289 global $wpdb;
290 $views_options = get_option('views_options');
291 $where = '';
292 $temp = '';
293 $output = '';
294 if(!empty($mode) && $mode != 'both') {
295 if(is_array($mode)) {
296 $mode = implode("','",$mode);
297 $where = "post_type IN ('".$mode."')";
298 } else {
299 $where = "post_type = '$mode'";
300 }
301 } else {
302 $where = '1=1';
303 }
304 $most_viewed = $wpdb->get_results("SELECT DISTINCT p.*, (pm1.meta_value+0) AS views, IF((pm2.meta_value IS NULL) OR (pm2.meta_value = ''), tt.term_id, pm2.meta_value) AS cat_id FROM $wpdb->posts p LEFT JOIN $wpdb->postmeta pm1 ON pm1.post_id = p.ID LEFT JOIN $wpdb->postmeta pm2 ON (pm2.post_id = p.ID AND pm2.meta_key='_yoast_wpseo_primary_category') INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'category') WHERE post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND pm1.meta_key = 'views' AND post_password = '' GROUP BY p.ID ORDER BY views DESC LIMIT $limit");
305 if($most_viewed) {
306 foreach ($most_viewed as $post) {
307 $post_views = intval($post->views);
308 $post_title = get_the_title($post);
309 if($chars > 0) {
310 $post_title = snippet_text($post_title, $chars);
311 }
312 $post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
313 $thumbnail = get_the_post_thumbnail($post->ID,'thumbnail',true);
314 $temp = stripslashes($views_options['most_viewed_template']);
315 $temp = str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $temp);
316 $temp = str_replace('%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp);
317 $temp = str_replace('%POST_TITLE%', $post_title, $temp);
318 $temp = str_replace('%POST_EXCERPT%', $post_excerpt, $temp);
319 $temp = str_replace('%POST_CONTENT%', $post->post_content, $temp);
320 $temp = str_replace('%POST_URL%', get_permalink($post), $temp);
321 $temp = str_replace('%POST_DATE%', get_the_time(get_option('date_format'), $post), $temp);
322 $temp = str_replace('%POST_TIME%', get_the_time(get_option('time_format'), $post), $temp);
323 $temp = str_replace('%POST_THUMBNAIL%', $thumbnail, $temp);
324 $temp = str_replace('%POST_CATEGORY_ID%', $post->cat_id, $temp);
325 $output .= $temp;
326 }
327 } else {
328 $output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
329 }
330 if($display) {
331 echo $output;
332 } else {
333 return $output;
334 }
335 }
336 }
337
338
339 ### Function: Display Least Viewed Page/Post By Category ID
340 if(!function_exists('get_least_viewed_category')) {
341 function get_least_viewed_category($category_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) {
342 global $wpdb;
343 $views_options = get_option('views_options');
344 $where = '';
345 $temp = '';
346 $output = '';
347 if(is_array($category_id)) {
348 $category_sql = 'tt.term_id IN ('.join(',', $category_id).')';
349 } else {
350 $category_sql = "tt.term_id = $category_id";
351 }
352 if(!empty($mode) && $mode != 'both') {
353 if(is_array($mode)) {
354 $mode = implode("','",$mode);
355 $where = "post_type IN ('".$mode."')";
356 } else {
357 $where = "post_type = '$mode'";
358 }
359 } else {
360 $where = '1=1';
361 }
362 $most_viewed = $wpdb->get_results("SELECT DISTINCT p.*, (pm1.meta_value+0) AS views, IF((pm2.meta_value IS NULL) OR (pm2.meta_value = ''), tt.term_id, pm2.meta_value) AS cat_id FROM $wpdb->posts p LEFT JOIN $wpdb->postmeta pm1 ON pm1.post_id = p.ID LEFT JOIN $wpdb->postmeta pm2 ON (pm2.post_id = p.ID AND pm2.meta_key='_yoast_wpseo_primary_category') INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) INNER JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'category' AND $category_sql) WHERE post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND pm1.meta_key = 'views' AND post_password = '' GROUP BY p.ID ORDER BY views ASC LIMIT $limit");
363 if($most_viewed) {
364 foreach ($most_viewed as $post) {
365 $post_views = intval($post->views);
366 $post_title = get_the_title($post);
367 if($chars > 0) {
368 $post_title = snippet_text($post_title, $chars);
369 }
370 $thumbnail = get_the_post_thumbnail($post->ID,'thumbnail',true);
371 $post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
372 $temp = stripslashes($views_options['most_viewed_template']);
373 $temp = str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $temp);
374 $temp = str_replace('%POST_TITLE%', $post_title, $temp);
375 $temp = str_replace('%POST_EXCERPT%', $post_excerpt, $temp);
376 $temp = str_replace('%POST_CONTENT%', $post->post_content, $temp);
377 $temp = str_replace('%POST_URL%', get_permalink($post), $temp);
378 $temp = str_replace('%POST_DATE%', get_the_time(get_option('date_format'), $post), $temp);
379 $temp = str_replace('%POST_TIME%', get_the_time(get_option('time_format'), $post), $temp);
380 $temp = str_replace('%POST_THUMBNAIL%', $thumbnail, $temp);
381 $temp = str_replace('%POST_CATEGORY_ID%', $post->cat_id, $temp);
382 $output .= $temp;
383 }
384 } else {
385 $output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
386 }
387 if($display) {
388 echo $output;
389 } else {
390 return $output;
391 }
392 }
393 }
394
395
396 ### Function: Display Most Viewed Page/Post By Category ID
397 if(!function_exists('get_most_viewed_category')) {
398 function get_most_viewed_category($category_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) {
399 global $wpdb;
400 $views_options = get_option('views_options');
401 $where = '';
402 $temp = '';
403 $output = '';
404 if(is_array($category_id)) {
405 $category_sql = 'tt.term_id IN ('.join(',', $category_id).')';
406 } else {
407 $category_sql = "tt.term_id = $category_id";
408 }
409 if(!empty($mode) && $mode != 'both') {
410 if(is_array($mode)) {
411 $mode = implode("','",$mode);
412 $where = "post_type IN ('".$mode."')";
413 } else {
414 $where = "post_type = '$mode'";
415 }
416 } else {
417 $where = '1=1';
418 }
419 $most_viewed = $wpdb->get_results("SELECT DISTINCT p.*, (pm1.meta_value+0) AS views, IF((pm2.meta_value IS NULL) OR (pm2.meta_value = ''), tt.term_id, pm2.meta_value) AS cat_id FROM $wpdb->posts p LEFT JOIN $wpdb->postmeta pm1 ON pm1.post_id = p.ID LEFT JOIN $wpdb->postmeta pm2 ON (pm2.post_id = p.ID AND pm2.meta_key='_yoast_wpseo_primary_category') INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) INNER JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'category' AND $category_sql) WHERE post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND pm1.meta_key = 'views' AND post_password = '' GROUP BY p.ID ORDER BY views DESC LIMIT $limit");
420 if($most_viewed) {
421 foreach ($most_viewed as $post) {
422 $post_views = intval($post->views);
423 $post_title = get_the_title($post);
424 if($chars > 0) {
425 $post_title = snippet_text($post_title, $chars);
426 }
427 $thumbnail = get_the_post_thumbnail($post->ID,'thumbnail',true);
428 $post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
429 $temp = stripslashes($views_options['most_viewed_template']);
430 $temp = str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $temp);
431 $temp = str_replace('%POST_TITLE%', $post_title, $temp);
432 $temp = str_replace('%POST_EXCERPT%', $post_excerpt, $temp);
433 $temp = str_replace('%POST_CONTENT%', $post->post_content, $temp);
434 $temp = str_replace('%POST_URL%', get_permalink($post), $temp);
435 $temp = str_replace('%POST_DATE%', get_the_time(get_option('date_format'), $post), $temp);
436 $temp = str_replace('%POST_TIME%', get_the_time(get_option('time_format'), $post), $temp);
437 $temp = str_replace('%POST_THUMBNAIL%', $thumbnail, $temp);
438 $temp = str_replace('%POST_CATEGORY_ID%', $post->cat_id, $temp);
439 $output .= $temp;
440 }
441 } else {
442 $output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
443 }
444 if($display) {
445 echo $output;
446 } else {
447 return $output;
448 }
449 }
450 }
451
452
453 ### Function: Display Most Viewed Page/Post By Tag ID
454 if(!function_exists('get_most_viewed_tag')) {
455 function get_most_viewed_tag($tag_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) {
456 global $wpdb;
457 $views_options = get_option('views_options');
458 $where = '';
459 $temp = '';
460 $output = '';
461 if(is_array($tag_id)) {
462 $tag_sql = 'tt.term_id IN ('.join(',', $tag_id).')';
463 } else {
464 $tag_sql = "tt.term_id = $tag_id";
465 }
466 if(!empty($mode) && $mode != 'both') {
467 if(is_array($mode)) {
468 $mode = implode("','",$mode);
469 $where = "post_type IN ('".$mode."')";
470 } else {
471 $where = "post_type = '$mode'";
472 }
473 } else {
474 $where = '1=1';
475 }
476 $most_viewed = $wpdb->get_results("SELECT DISTINCT p.*, (pm1.meta_value+0) AS views, IF((pm2.meta_value IS NULL) OR (pm2.meta_value = ''), tt2.term_id, pm2.meta_value) AS cat_id FROM $wpdb->posts p LEFT JOIN $wpdb->postmeta pm1 ON pm1.post_id = p.ID LEFT JOIN $wpdb->postmeta pm2 ON (pm2.post_id = p.ID AND pm2.meta_key='_yoast_wpseo_primary_category') INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) INNER JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'post_tag' AND $tag_sql) INNER JOIN $wpdb->term_relationships tr2 ON (p.ID = tr2.object_id) LEFT JOIN $wpdb->term_taxonomy tt2 ON (tr2.term_taxonomy_id = tt2.term_taxonomy_id AND tt2.taxonomy = 'category') WHERE post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND pm1.meta_key = 'views' AND post_password = '' GROUP BY p.ID ORDER BY views DESC LIMIT $limit");
477 if($most_viewed) {
478 foreach ($most_viewed as $post) {
479 $post_views = intval($post->views);
480 $post_title = get_the_title($post);
481 if($chars > 0) {
482 $post_title = snippet_text($post_title, $chars);
483 }
484 $thumbnail = get_the_post_thumbnail($post->ID,'thumbnail',true);
485 $post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
486 $temp = stripslashes($views_options['most_viewed_template']);
487 $temp = str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $temp);
488 $temp = str_replace('%POST_TITLE%', $post_title, $temp);
489 $temp = str_replace('%POST_EXCERPT%', $post_excerpt, $temp);
490 $temp = str_replace('%POST_CONTENT%', $post->post_content, $temp);
491 $temp = str_replace('%POST_URL%', get_permalink($post), $temp);
492 $temp = str_replace('%POST_DATE%', get_the_time(get_option('date_format'), $post), $temp);
493 $temp = str_replace('%POST_TIME%', get_the_time(get_option('time_format'), $post), $temp);
494 $temp = str_replace('%POST_THUMBNAIL%', $thumbnail, $temp);
495 $temp = str_replace('%POST_CATEGORY_ID%', $post->cat_id, $temp);
496 $output .= $temp;
497 }
498 } else {
499 $output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
500 }
501 if($display) {
502 echo $output;
503 } else {
504 return $output;
505 }
506 }
507 }
508
509
510 ### Function: Display Least Viewed Page/Post By Tag ID
511 if(!function_exists('get_least_viewed_tag')) {
512 function get_least_viewed_tag($tag_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) {
513 global $wpdb;
514 $views_options = get_option('views_options');
515 $where = '';
516 $temp = '';
517 $output = '';
518 if(is_array($tag_id)) {
519 $tag_sql = 'tt.term_id IN ('.join(',', $tag_id).')';
520 } else {
521 $tag_sql = "tt.term_id = $tag_id";
522 }
523 if(!empty($mode) && $mode != 'both') {
524 if(is_array($mode)) {
525 $mode = implode("','",$mode);
526 $where = "post_type IN ('".$mode."')";
527 } else {
528 $where = "post_type = '$mode'";
529 }
530 } else {
531 $where = '1=1';
532 }
533 $most_viewed = $wpdb->get_results("SELECT DISTINCT p.*, (pm1.meta_value+0) AS views, IF((pm2.meta_value IS NULL) OR (pm2.meta_value = ''), tt2.term_id, pm2.meta_value) AS cat_id FROM $wpdb->posts p LEFT JOIN $wpdb->postmeta pm1 ON pm1.post_id = p.ID LEFT JOIN $wpdb->postmeta pm2 ON (pm2.post_id = p.ID AND pm2.meta_key='_yoast_wpseo_primary_category') INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) INNER JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'post_tag' AND $tag_sql) INNER JOIN $wpdb->term_relationships tr2 ON (p.ID = tr2.object_id) LEFT JOIN $wpdb->term_taxonomy tt2 ON (tr2.term_taxonomy_id = tt2.term_taxonomy_id AND tt2.taxonomy = 'category') WHERE post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND pm1.meta_key = 'views' AND post_password = '' GROUP BY p.ID ORDER BY views ASC LIMIT $limit");
534 if($most_viewed) {
535 foreach ($most_viewed as $post) {
536 $post_views = intval($post->views);
537 $post_title = get_the_title($post);
538 if($chars > 0) {
539 $post_title = snippet_text($post_title, $chars);
540 }
541 $thumbnail = get_the_post_thumbnail($post->ID,'thumbnail',true);
542 $post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
543 $temp = stripslashes($views_options['most_viewed_template']);
544 $temp = str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $temp);
545 $temp = str_replace('%POST_TITLE%', $post_title, $temp);
546 $temp = str_replace('%POST_EXCERPT%', $post_excerpt, $temp);
547 $temp = str_replace('%POST_CONTENT%', $post->post_content, $temp);
548 $temp = str_replace('%POST_URL%', get_permalink($post), $temp);
549 $temp = str_replace('%POST_DATE%', get_the_time(get_option('date_format'), $post), $temp);
550 $temp = str_replace('%POST_TIME%', get_the_time(get_option('time_format'), $post), $temp);
551 $temp = str_replace('%POST_THUMBNAIL%', $thumbnail, $temp);
552 $temp = str_replace('%POST_CATEGORY_ID%', $post->cat_id, $temp);
553 $output .= $temp;
554 }
555 } else {
556 $output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
557 }
558 if($display) {
559 echo $output;
560 } else {
561 return $output;
562 }
563 }
564 }
565
566
567 ### Function: Display Total Views
568 if(!function_exists('get_totalviews')) {
569 function get_totalviews($display = true) {
570 global $wpdb;
571 $total_views = intval($wpdb->get_var("SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = 'views'"));
572 if($display) {
573 echo number_format_i18n($total_views);
574 } else {
575 return $total_views;
576 }
577 }
578 }
579
580
581 ### Function: Snippet Text
582 if(!function_exists('snippet_text')) {
583 function snippet_text($text, $length = 0) {
584 if (defined('MB_OVERLOAD_STRING')) {
585 $text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset'));
586 if (mb_strlen($text) > $length) {
587 return htmlentities(mb_substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...';
588 } else {
589 return htmlentities($text, ENT_COMPAT, get_option('blog_charset'));
590 }
591 } else {
592 $text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset'));
593 if (strlen($text) > $length) {
594 return htmlentities(substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...';
595 } else {
596 return htmlentities($text, ENT_COMPAT, get_option('blog_charset'));
597 }
598 }
599 }
600 }
601
602
603 ### Function: Process Post Excerpt, For Some Reasons, The Default get_post_excerpt() Does Not Work As Expected
604 function views_post_excerpt($post_excerpt, $post_content, $post_password, $chars = 200) {
605 if(!empty($post_password)) {
606 if(!isset($_COOKIE['wp-postpass_'.COOKIEHASH]) || $_COOKIE['wp-postpass_'.COOKIEHASH] != $post_password) {
607 return __('There is no excerpt because this is a protected post.', 'wp-postviews');
608 }
609 }
610 if(empty($post_excerpt)) {
611 return snippet_text(strip_tags($post_content), $chars);
612 } else {
613 return $post_excerpt;
614 }
615 }
616
617
618 ### Function: Modify Default WordPress Listing To Make It Sorted By Post Views
619 function views_fields($content) {
620 global $wpdb;
621 $content .= ", ($wpdb->postmeta.meta_value+0) AS views";
622 return $content;
623 }
624 function views_join($content) {
625 global $wpdb;
626 $content .= " LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID";
627 return $content;
628 }
629 function views_where($content) {
630 global $wpdb;
631 $content .= " AND $wpdb->postmeta.meta_key = 'views'";
632 return $content;
633 }
634 function views_orderby($content) {
635 $orderby = trim(addslashes(get_query_var('v_orderby')));
636 if(empty($orderby) || ($orderby != 'asc' && $orderby != 'desc')) {
637 $orderby = 'desc';
638 }
639 $content = " views $orderby";
640 return $content;
641 }
642
643
644 ### Function: Add Views Custom Fields
645 add_action('publish_post', 'add_views_fields');
646 add_action('publish_page', 'add_views_fields');
647 function add_views_fields($post_ID) {
648 global $wpdb;
649 if(!wp_is_post_revision($post_ID)) {
650 add_post_meta($post_ID, 'views', 0, true);
651 }
652 }
653
654
655 ### Function: Views Public Variables
656 add_filter('query_vars', 'views_variables');
657 function views_variables($public_query_vars) {
658 $public_query_vars[] = 'v_sortby';
659 $public_query_vars[] = 'v_orderby';
660 return $public_query_vars;
661 }
662
663
664 ### Function: Sort Views Posts
665 add_action('pre_get_posts', 'views_sorting');
666 function views_sorting($local_wp_query) {
667 if($local_wp_query->get('v_sortby') == 'views') {
668 add_filter('posts_fields', 'views_fields');
669 add_filter('posts_join', 'views_join');
670 add_filter('posts_where', 'views_where');
671 add_filter('posts_orderby', 'views_orderby');
672 } else {
673 remove_filter('posts_fields', 'views_fields');
674 remove_filter('posts_join', 'views_join');
675 remove_filter('posts_where', 'views_where');
676 remove_filter('posts_orderby', 'views_orderby');
677 }
678 }
679
680
681 ### Function: Plug Into WP-Stats
682 add_action( 'plugins_loaded', 'postviews_wp_stats' );
683 function postviews_wp_stats() {
684 add_filter( 'wp_stats_page_admin_plugins', 'postviews_page_admin_general_stats' );
685 add_filter( 'wp_stats_page_admin_most', 'postviews_page_admin_most_stats' );
686 add_filter( 'wp_stats_page_plugins', 'postviews_page_general_stats' );
687 add_filter( 'wp_stats_page_most', 'postviews_page_most_stats' );
688 }
689
690
691 ### Function: Add WP-PostViews General Stats To WP-Stats Page Options
692 function postviews_page_admin_general_stats($content) {
693 $stats_display = get_option('stats_display');
694 if($stats_display['views'] == 1) {
695 $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_views" value="views" checked="checked" />&nbsp;&nbsp;<label for="wpstats_views">'.__('WP-PostViews', 'wp-postviews').'</label><br />'."\n";
696 } else {
697 $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_views" value="views" />&nbsp;&nbsp;<label for="wpstats_views">'.__('WP-PostViews', 'wp-postviews').'</label><br />'."\n";
698 }
699 return $content;
700 }
701
702
703 ### Function: Add WP-PostViews Top Most/Highest Stats To WP-Stats Page Options
704 function postviews_page_admin_most_stats($content) {
705 $stats_display = get_option('stats_display');
706 $stats_mostlimit = intval(get_option('stats_mostlimit'));
707 if($stats_display['viewed_most_post'] == 1) {
708 $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_post" value="viewed_most_post" checked="checked" />&nbsp;&nbsp;<label for="wpstats_viewed_most_post">'.sprintf(_n('%s Most Viewed Post', '%s Most Viewed Posts', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
709 } else {
710 $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_post" value="viewed_most_post" />&nbsp;&nbsp;<label for="wpstats_viewed_most_post">'.sprintf(_n('%s Most Viewed Post', '%s Most Viewed Posts', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
711 }
712 if($stats_display['viewed_most_page'] == 1) {
713 $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_page" value="viewed_most_page" checked="checked" />&nbsp;&nbsp;<label for="wpstats_viewed_most_page">'.sprintf(_n('%s Most Viewed Page', '%s Most Viewed Pages', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
714 } else {
715 $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_page" value="viewed_most_page" />&nbsp;&nbsp;<label for="wpstats_viewed_most_page">'.sprintf(_n('%s Most Viewed Page', '%s Most Viewed Pages', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
716 }
717 return $content;
718 }
719
720
721 ### Function: Add WP-PostViews General Stats To WP-Stats Page
722 function postviews_page_general_stats($content) {
723 $stats_display = get_option('stats_display');
724 if($stats_display['views'] == 1) {
725 $content .= '<p><strong>'.__('WP-PostViews', 'wp-postviews').'</strong></p>'."\n";
726 $content .= '<ul>'."\n";
727 $content .= '<li>'.sprintf(_n('<strong>%s</strong> view was generated.', '<strong>%s</strong> views were generated.', get_totalviews(false), 'wp-postviews'), number_format_i18n(get_totalviews(false))).'</li>'."\n";
728 $content .= '</ul>'."\n";
729 }
730 return $content;
731 }
732
733
734 ### Function: Add WP-PostViews Top Most/Highest Stats To WP-Stats Page
735 function postviews_page_most_stats($content) {
736 $stats_display = get_option('stats_display');
737 $stats_mostlimit = intval(get_option('stats_mostlimit'));
738 if($stats_display['viewed_most_post'] == 1) {
739 $content .= '<p><strong>'.sprintf(_n('%s Most Viewed Post', '%s Most Viewed Posts', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</strong></p>'."\n";
740 $content .= '<ul>'."\n";
741 $content .= get_most_viewed('post', $stats_mostlimit, 0, false);
742 $content .= '</ul>'."\n";
743 }
744 if($stats_display['viewed_most_page'] == 1) {
745 $content .= '<p><strong>'.sprintf(_n('%s Most Viewed Page', '%s Most Viewed Pages', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</strong></p>'."\n";
746 $content .= '<ul>'."\n";
747 $content .= get_most_viewed('page', $stats_mostlimit, 0, false);
748 $content .= '</ul>'."\n";
749 }
750 return $content;
751 }
752
753
754 ### Function: Increment Post Views
755 add_action( 'wp_ajax_postviews', 'increment_views' );
756 add_action( 'wp_ajax_nopriv_postviews', 'increment_views' );
757 function increment_views() {
758 if( empty( $_GET['postviews_id'] ) )
759 return;
760
761 if( !defined( 'WP_CACHE' ) || !WP_CACHE )
762 return;
763
764 $views_options = get_option( 'views_options' );
765
766 if( isset( $views_options['use_ajax'] ) && intval( $views_options['use_ajax'] ) === 0 )
767 return;
768
769 $post_id = intval( $_GET['postviews_id'] );
770 if( $post_id > 0 ) {
771 $post_views = get_post_custom( $post_id );
772 $post_views = intval( $post_views['views'][0] );
773 update_post_meta( $post_id, 'views', ( $post_views + 1 ) );
774 do_action( 'postviews_increment_views_ajax', ( $post_views + 1 ) );
775 echo ( $post_views + 1 );
776 exit();
777 }
778 }
779
780 ### Function Show Post Views Column in WP-Admin
781 add_action('manage_posts_custom_column', 'add_postviews_column_content');
782 add_filter('manage_posts_columns', 'add_postviews_column');
783 add_action('manage_pages_custom_column', 'add_postviews_column_content');
784 add_filter('manage_pages_columns', 'add_postviews_column');
785 function add_postviews_column($defaults) {
786 $defaults['views'] = __( 'Views', 'wp-postviews' );
787 return $defaults;
788 }
789
790
791 ### Functions Fill In The Views Count
792 function add_postviews_column_content($column_name) {
793 if($column_name == 'views') {
794 if(function_exists('the_views')) { the_views(true, '', '', true); }
795 }
796 }
797
798
799 ### Function Sort Columns
800 add_filter('manage_edit-post_sortable_columns', 'sort_postviews_column');
801 add_filter('manage_edit-page_sortable_columns', 'sort_postviews_column');
802 function sort_postviews_column($defaults)
803 {
804 $defaults['views'] = 'views';
805 return $defaults;
806 }
807 add_action('pre_get_posts', 'sort_postviews');
808 function sort_postviews($query) {
809 if(!is_admin())
810 return;
811 $orderby = $query->get('orderby');
812 if('views' == $orderby) {
813 $query->set('meta_key', 'views');
814 $query->set('orderby', 'meta_value_num');
815 }
816 }
817
818 ### Function: Round Numbers To K (Thousand), M (Million) or B (Billion)
819 function postviews_round_number( $number, $min_value = 1000, $decimal = 1 ) {
820 if( $number < $min_value ) {
821 return number_format_i18n( $number );
822 }
823 $alphabets = array( 1000000000 => 'B', 1000000 => 'M', 1000 => 'K' );
824 foreach( $alphabets as $key => $value )
825 if( $number >= $key ) {
826 return round( $number / $key, $decimal ) . '' . $value;
827 }
828 }
829
830
831 ### Class: WP-PostViews Widget
832 class WP_Widget_PostViews extends WP_Widget {
833 // Constructor
834 function __construct() {
835 $widget_ops = array('description' => __('WP-PostViews views statistics', 'wp-postviews'));
836 parent::__construct('views', __('Views', 'wp-postviews'), $widget_ops);
837 }
838
839 // Display Widget
840 function widget($args, $instance) {
841 $title = apply_filters('widget_title', esc_attr($instance['title']));
842 $type = esc_attr($instance['type']);
843 $mode = esc_attr($instance['mode']);
844 $limit = intval($instance['limit']);
845 $chars = intval($instance['chars']);
846 $cat_ids = explode(',', esc_attr($instance['cat_ids']));
847 echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title'];
848 echo '<ul>'."\n";
849 switch($type) {
850 case 'least_viewed':
851 get_least_viewed($mode, $limit, $chars);
852 break;
853 case 'most_viewed':
854 get_most_viewed($mode, $limit, $chars);
855 break;
856 case 'most_viewed_category':
857 get_most_viewed_category($cat_ids, $mode, $limit, $chars);
858 break;
859 case 'least_viewed_category':
860 get_least_viewed_category($cat_ids, $mode, $limit, $chars);
861 break;
862 }
863 echo '</ul>'."\n";
864 echo $args['after_widget'];
865 }
866
867 // When Widget Control Form Is Posted
868 function update($new_instance, $old_instance) {
869 if (!isset($new_instance['submit'])) {
870 return false;
871 }
872 $instance = $old_instance;
873 $instance['title'] = strip_tags($new_instance['title']);
874 $instance['type'] = strip_tags($new_instance['type']);
875 $instance['mode'] = strip_tags($new_instance['mode']);
876 $instance['limit'] = intval($new_instance['limit']);
877 $instance['chars'] = intval($new_instance['chars']);
878 $instance['cat_ids'] = strip_tags($new_instance['cat_ids']);
879 return $instance;
880 }
881
882 // DIsplay Widget Control Form
883 function form($instance) {
884 $instance = wp_parse_args((array) $instance, array('title' => __('Views', 'wp-postviews'), 'type' => 'most_viewed', 'mode' => '', 'limit' => 10, 'chars' => 200, 'cat_ids' => '0'));
885 $title = esc_attr($instance['title']);
886 $type = esc_attr($instance['type']);
887 $mode = trim(esc_attr($instance['mode']));
888 $limit = intval($instance['limit']);
889 $chars = intval($instance['chars']);
890 $cat_ids = esc_attr($instance['cat_ids']);
891 $post_types = get_post_types(array(
892 'public' => true
893 ));
894 ?>
895 <p>
896 <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'wp-postviews'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label>
897 </p>
898 <p>
899 <label for="<?php echo $this->get_field_id('type'); ?>"><?php _e('Statistics Type:', 'wp-postviews'); ?>
900 <select name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>" class="widefat">
901 <option value="least_viewed"<?php selected('least_viewed', $type); ?>><?php _e('Least Viewed', 'wp-postviews'); ?></option>
902 <option value="least_viewed_category"<?php selected('least_viewed_category', $type); ?>><?php _e('Least Viewed By Category', 'wp-postviews'); ?></option>
903 <optgroup>&nbsp;</optgroup>
904 <option value="most_viewed"<?php selected('most_viewed', $type); ?>><?php _e('Most Viewed', 'wp-postviews'); ?></option>
905 <option value="most_viewed_category"<?php selected('most_viewed_category', $type); ?>><?php _e('Most Viewed By Category', 'wp-postviews'); ?></option>
906 </select>
907 </label>
908 </p>
909 <p>
910 <label for="<?php echo $this->get_field_id('mode'); ?>"><?php _e('Include Views From:', 'wp-postviews'); ?>
911 <select name="<?php echo $this->get_field_name('mode'); ?>" id="<?php echo $this->get_field_id('mode'); ?>" class="widefat">
912 <option value=""<?php selected('', $mode); ?>><?php _e('All', 'wp-postviews'); ?></option>
913 <?php if($post_types > 0): ?>
914 <?php foreach($post_types as $post_type): ?>
915 <option value="<?php echo $post_type; ?>"<?php selected($post_type, $mode); ?>><?php printf(__('%s Only', 'wp-postviews'), ucfirst($post_type)); ?></option>
916 <?php endforeach; ?>
917 <?php endif; ?>
918 </select>
919 </label>
920 </p>
921 <p>
922 <label for="<?php echo $this->get_field_id('limit'); ?>"><?php _e('No. Of Records To Show:', 'wp-postviews'); ?> <input class="widefat" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo $limit; ?>" /></label>
923 </p>
924 <p>
925 <label for="<?php echo $this->get_field_id('chars'); ?>"><?php _e('Maximum Post Title Length (Characters):', 'wp-postviews'); ?> <input class="widefat" id="<?php echo $this->get_field_id('chars'); ?>" name="<?php echo $this->get_field_name('chars'); ?>" type="text" value="<?php echo $chars; ?>" /></label><br />
926 <small><?php _e('<strong>0</strong> to disable.', 'wp-postviews'); ?></small>
927 </p>
928 <p>
929 <label for="<?php echo $this->get_field_id('cat_ids'); ?>"><?php _e('Category IDs:', 'wp-postviews'); ?> <span style="color: red;">*</span> <input class="widefat" id="<?php echo $this->get_field_id('cat_ids'); ?>" name="<?php echo $this->get_field_name('cat_ids'); ?>" type="text" value="<?php echo $cat_ids; ?>" /></label><br />
930 <small><?php _e('Separate mutiple categories with commas.', 'wp-postviews'); ?></small>
931 </p>
932 <p style="color: red;">
933 <small><?php _e('* If you are not using any category statistics, you can ignore it.', 'wp-postviews'); ?></small>
934 <p>
935 <input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
936 <?php
937 }
938 }
939
940
941 ### Function: Init WP-PostViews Widget
942 add_action( 'widgets_init', 'widget_views_init' );
943 function widget_views_init() {
944 register_widget( 'WP_Widget_PostViews' );
945 }
946
947
948 ### Function: Post Views Options
949 register_activation_hook( __FILE__, 'views_activation' );
950 function views_activation( $network_wide ) {
951 // Add Options
952 $option_name = 'views_options';
953 $option = array(
954 'count' => 1
955 , 'exclude_bots' => 0
956 , 'display_home' => 0
957 , 'display_single' => 0
958 , 'display_page' => 0
959 , 'display_archive' => 0
960 , 'display_search' => 0
961 , 'display_other' => 0
962 , 'use_ajax' => 1
963 , 'template' => __('%VIEW_COUNT% views', 'wp-postviews')
964 , 'most_viewed_template' => '<li><a href="%POST_URL%" title="%POST_TITLE%">%POST_TITLE%</a> - %VIEW_COUNT% '.__('views', 'wp-postviews').'</li>'
965 );
966
967 if ( is_multisite() && $network_wide )
968 {
969 $ms_sites = wp_get_sites();
970
971 if( 0 < sizeof( $ms_sites ) )
972 {
973 foreach ( $ms_sites as $ms_site )
974 {
975 switch_to_blog( $ms_site['blog_id'] );
976 add_option( $option_name, $option );
977 }
978 }
979
980 restore_current_blog();
981 }
982 else
983 {
984 add_option( $option_name, $option );
985 }
986 }
987
988 ### Function: Parse View Options
989 function views_options_parse($key) {
990 return !empty($_POST[$key]) ? $_POST[$key] : null;
991 }
992