class-wordpress-popular-posts-activator.php
8 years ago
class-wordpress-popular-posts-deactivator.php
8 years ago
class-wordpress-popular-posts-helper.php
8 years ago
class-wordpress-popular-posts-i18n.php
8 years ago
class-wordpress-popular-posts-image.php
8 years ago
class-wordpress-popular-posts-loader.php
8 years ago
class-wordpress-popular-posts-output.php
8 years ago
class-wordpress-popular-posts-query.php
8 years ago
class-wordpress-popular-posts-rest-controller.php
8 years ago
class-wordpress-popular-posts-settings.php
8 years ago
class-wordpress-popular-posts-template.php
8 years ago
class-wordpress-popular-posts-translate.php
8 years ago
class-wordpress-popular-posts-widget.php
8 years ago
class-wordpress-popular-posts.php
8 years ago
widget-form.php
8 years ago
class-wordpress-popular-posts-template.php
106 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WordPress Popular Posts template tags for use in themes. |
| 4 | */ |
| 5 | |
| 6 | /** |
| 7 | * Template tag - gets views count. |
| 8 | * |
| 9 | * @since 2.0.3 |
| 10 | * @global object wpdb |
| 11 | * @param int id |
| 12 | * @param string range |
| 13 | * @param bool number_format |
| 14 | * @return string |
| 15 | */ |
| 16 | function wpp_get_views($id = NULL, $range = NULL, $number_format = true) { |
| 17 | |
| 18 | // have we got an id? |
| 19 | if ( empty($id) || is_null($id) || !is_numeric($id) ) { |
| 20 | return "-1"; |
| 21 | } else { |
| 22 | global $wpdb; |
| 23 | |
| 24 | $table_name = $wpdb->prefix . "popularposts"; |
| 25 | |
| 26 | if ( !$range || 'all' == $range ) { |
| 27 | $query = "SELECT pageviews FROM {$table_name}data WHERE postid = '{$id}'"; |
| 28 | } else { |
| 29 | $interval = ""; |
| 30 | |
| 31 | switch( $range ){ |
| 32 | case "last24hours": |
| 33 | case "daily": |
| 34 | $interval = "24 HOUR"; |
| 35 | break; |
| 36 | |
| 37 | case "last7days": |
| 38 | case "weekly": |
| 39 | $interval = "6 DAY"; |
| 40 | break; |
| 41 | |
| 42 | case "last30days": |
| 43 | case "monthly": |
| 44 | $interval = "29 DAY"; |
| 45 | break; |
| 46 | |
| 47 | default: |
| 48 | $interval = "24 HOUR"; |
| 49 | break; |
| 50 | } |
| 51 | |
| 52 | $now = current_time('mysql'); |
| 53 | |
| 54 | $query = "SELECT SUM(pageviews) FROM {$table_name}summary WHERE postid = '{$id}' AND view_datetime > DATE_SUB('{$now}', INTERVAL {$interval}) LIMIT 1;"; |
| 55 | } |
| 56 | |
| 57 | $result = $wpdb->get_var($query); |
| 58 | |
| 59 | if ( !$result ) { |
| 60 | return "0"; |
| 61 | } |
| 62 | |
| 63 | return ($number_format) ? number_format_i18n( intval($result) ) : $result; |
| 64 | } |
| 65 | |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Template tag - gets popular posts. |
| 70 | * |
| 71 | * @since 2.0.3 |
| 72 | * @param mixed args |
| 73 | */ |
| 74 | function wpp_get_mostpopular($args = NULL) { |
| 75 | |
| 76 | $shortcode = '[wpp'; |
| 77 | |
| 78 | if ( is_null( $args ) ) { |
| 79 | $shortcode .= ']'; |
| 80 | } else { |
| 81 | if( is_array( $args ) ){ |
| 82 | $atts = ''; |
| 83 | foreach( $args as $key => $arg ){ |
| 84 | $atts .= ' ' . $key . '="' . htmlspecialchars($arg, ENT_QUOTES, $encoding = ini_get("default_charset"), false) . '"'; |
| 85 | } |
| 86 | } else { |
| 87 | $atts = trim( str_replace( "&", " ", $args ) ); |
| 88 | } |
| 89 | |
| 90 | $shortcode .= ' ' . $atts . ' php=true]'; |
| 91 | } |
| 92 | |
| 93 | echo do_shortcode( $shortcode ); |
| 94 | |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Template tag - gets popular posts. Deprecated in 2.0.3, use wpp_get_mostpopular instead. |
| 99 | * |
| 100 | * @since 1.0 |
| 101 | * @param mixed args |
| 102 | */ |
| 103 | function get_mostpopular($args = NULL) { |
| 104 | trigger_error( 'The get_mostpopular() template tag has been deprecated since 2.0.3. Please use wpp_get_mostpopular() instead.', E_USER_WARNING ); |
| 105 | } |
| 106 |