Advertisement
kura2yamato

coba2

Apr 14th, 2019
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.91 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Post Views Counter pluggable template functions
  4.  *
  5.  * Override any of those functions by copying it to your theme or replace it via plugin
  6.  *
  7.  * @author Digital Factory
  8.  * @package Post Views Counter
  9.  * @since 1.0.0
  10.  */
  11. // exit if accessed directly
  12. if ( ! defined( 'ABSPATH' ) )
  13.     exit;
  14.  
  15. /**
  16.  * Get post views for a post or array of posts.
  17.  *
  18.  * @global $wpdb
  19.  * @param int|array $post_id
  20.  * @return int
  21.  */
  22. if ( ! function_exists( 'pvc_get_post_views' ) ) {
  23.  
  24.     function pvc_get_post_views( $post_id = 0 ) {
  25.         if ( empty( $post_id ) )
  26.             $post_id = get_the_ID();
  27.  
  28.         if ( is_array( $post_id ) )
  29.             $post_id = implode( ',', array_map( 'intval', $post_id ) );
  30.         else
  31.             $post_id = (int) $post_id;
  32.  
  33.         global $wpdb;
  34.  
  35.         $query = "SELECT SUM(count) AS views
  36.         FROM " . $wpdb->prefix . "post_views
  37.         WHERE id IN (" . $post_id . ") AND type = 4";
  38.  
  39.         // get cached data
  40.         $post_views = wp_cache_get( md5( $query ), 'pvc-get_post_views' );
  41.  
  42.         // cached data not found?
  43.         if ( $post_views === false ) {
  44.             $post_views = (int) $wpdb->get_var( $query );
  45.            
  46.             // set the cache expiration, 5 minutes by default
  47.             $expire = absint( apply_filters( 'pvc_object_cache_expire', 5 * 60 ) );
  48.  
  49.             wp_cache_add( md5( $query ), $post_views, 'pvc-get_post_views', $expire );
  50.         }
  51.  
  52.         return apply_filters( 'pvc_get_post_views', $post_views, $post_id );
  53.     }
  54.  
  55. }
  56.  
  57. /**
  58.  * Get views query.
  59.  *
  60.  * @global $wpdb
  61.  * @param array $args
  62.  * @return int|array
  63.  */
  64. if ( ! function_exists( 'pvc_get_views' ) ) {
  65.  
  66.     function pvc_get_views( $args = array() ) {
  67.         $range = array();
  68.         $defaults = array(
  69.             'fields'        => 'views',
  70.             'post_id'       => '',
  71.             'post_type'     => '',
  72.             'views_query'   => array(
  73.                 'year'      => '',
  74.                 'month'     => '',
  75.                 'week'      => '',
  76.                 'day'       => ''
  77.             )
  78.         );
  79.  
  80.         $args = apply_filters( 'pvc_get_views_args', array_merge( $defaults, $args ) );
  81.  
  82.         // check post types
  83.         if ( is_array( $args['post_type'] ) && ! empty( $args['post_type'] ) ) {
  84.             $post_types = array();
  85.  
  86.             foreach( $args['post_type'] as $post_type ) {
  87.                 $post_types[] = "'" . $post_type . "'";
  88.             }
  89.  
  90.             $args['post_type'] = implode( ', ', $post_types );
  91.         } elseif ( ! is_string( $args['post_type'] ) )
  92.             $args['post_type'] = $defaults['post_type'];
  93.         else
  94.             $args['post_type'] = "'" . $args['post_type'] . "'";
  95.  
  96.         // check post ids
  97.         if ( is_array( $args['post_id'] ) && ! empty( $args['post_id'] ) )
  98.             $args['post_id'] = implode( ', ', array_unique( array_map( 'intval', $args['post_id'] ) ) );
  99.         else
  100.             $args['post_id'] = (int) $args['post_id'];
  101.  
  102.         // check fields
  103.         if ( ! in_array( $args['fields'], array( 'views', 'date=>views' ), true ) )
  104.             $args['fields'] = $defaults['fields'];
  105.  
  106.         // check views query
  107.         if ( ! is_array( $args['views_query'] ) )
  108.             $args['views_query'] = $defaults['views_query'];
  109.  
  110.         // check views query year
  111.         if ( ! empty( $args['views_query']['year'] ) )
  112.             $year = str_pad( (int) $args['views_query']['year'], 4, 0, STR_PAD_LEFT );
  113.  
  114.         // check views query month
  115.         if ( ! empty( $args['views_query']['month'] ) )
  116.             $month = str_pad( (int) $args['views_query']['month'], 2, 0, STR_PAD_LEFT );
  117.  
  118.         // check views query week
  119.         if ( ! empty( $args['views_query']['week'] ) )
  120.             $week = str_pad( (int) $args['views_query']['week'], 2, 0, STR_PAD_LEFT );
  121.  
  122.         // check views query day
  123.         if ( ! empty( $args['views_query']['day'] ) )
  124.             $day = str_pad( (int) $args['views_query']['day'], 2, 0, STR_PAD_LEFT );
  125.  
  126.         $views_query = '';
  127.  
  128.         // year
  129.         if ( isset( $year ) ) {
  130.             // year, week
  131.             if ( isset( $week ) ) {
  132.                 if ( $args['fields'] === 'date=>views' ) {
  133.                     // create date based on week number
  134.                     $date = new DateTime( $year . 'W' . $week );
  135.  
  136.                     // get monday
  137.                     $monday = $date->format( 'd' );
  138.  
  139.                     // get month of monday
  140.                     $monday_month = $date->format( 'm' );
  141.  
  142.                     // prepare range
  143.                     for( $i = 1; $i <= 6; $i++ ) {
  144.                         $range[(string) ( $date->format( 'Y' ) . $date->format( 'm' ) . $date->format( 'd' ) )] = 0;
  145.  
  146.                         $date->modify( '+1days' );
  147.                     }
  148.  
  149.                     $range[(string) ( $date->format( 'Y' ) . $date->format( 'm' ) . $date->format( 'd' ) )] = 0;
  150.  
  151.                     // get month of sunday
  152.                     $sunday_month = $date->format( 'm' );
  153.  
  154.                     $views_query = " AND pvc.type = 0 AND pvc.period >= '" . $year . $monday_month . $monday . "' AND pvc.period <= '" . $date->format( 'Y' ) . $sunday_month . $date->format( 'd' ) . "'";
  155.                 } else
  156.                     $views_query = " AND pvc.type = 1 AND pvc.period = '" . $year . $week . "'";
  157.             // year, month
  158.             } elseif ( isset( $month ) ) {
  159.                 // year, month, day
  160.                 if ( isset( $day ) ) {
  161.                     if ( $args['fields'] === 'date=>views' )
  162.                         // prepare range
  163.                         $range[(string) ( $year . $month . $day )] = 0;
  164.  
  165.                     $views_query = " AND pvc.type = 0 AND pvc.period = '" . $year . $month . $day . "'";
  166.                 // year, month
  167.                 } else {
  168.                     if ( $args['fields'] === 'date=>views' ) {
  169.                         // create date
  170.                         $date = new DateTime( $year . '-' . $month . '-01' );
  171.  
  172.                         // get last day
  173.                         $last = $date->format( 't' );
  174.  
  175.                         // prepare range
  176.                         for( $i = 1; $i <= $last; $i++ ) {
  177.                             $range[(string) ( $year . $month . str_pad( $i, 2, 0, STR_PAD_LEFT ) )] = 0;
  178.                         }
  179.  
  180.                         $views_query = " AND pvc.type = 0 AND pvc.period >= '" . $year . $month . "01' AND pvc.period <= '" . $year . $month . $last . "'";
  181.                     } else
  182.                         $views_query = " AND pvc.type = 2 AND pvc.period = '" . $year . $month . "'";
  183.                 }
  184.             // year
  185.             } else {
  186.                 if ( $args['fields'] === 'date=>views' ) {
  187.                     // prepare range
  188.                     for( $i = 1; $i <= 12; $i++ ) {
  189.                         $range[(string) ( $year . str_pad( $i, 2, 0, STR_PAD_LEFT ) )] = 0;
  190.                     }
  191.  
  192.                     // create date
  193.                     $date = new DateTime( $year . '-12-01' );
  194.  
  195.                     $views_query = " AND pvc.type = 2 AND pvc.period >= '" . $year . "01' AND pvc.period <= '" . $year . "12'";
  196.                 } else
  197.                     $views_query = " AND pvc.type = 3 AND pvc.period = '" . $year . "'";
  198.             }
  199.         // month
  200.         } elseif ( isset( $month ) ) {
  201.             // month, day
  202.             if ( isset( $day ) ) {
  203.                 $views_query = " AND pvc.type = 0 AND RIGHT( pvc.period, 4 ) = '" . $month . $day . "'";
  204.             // month
  205.             } else {
  206.                 $views_query = " AND pvc.type = 2 AND RIGHT( pvc.period, 2 ) = '" . $month . "'";
  207.             }
  208.         // week
  209.         } elseif ( isset( $week ) ) {
  210.             $views_query = " AND pvc.type = 1 AND RIGHT( pvc.period, 2 ) = '" . $week . "'";
  211.         // day
  212.         } elseif ( isset( $day ) ) {
  213.             $views_query = " AND pvc.type = 0 AND RIGHT( pvc.period, 2 ) = '" . $day . "'";
  214.         }
  215.  
  216.         global $wpdb;
  217.  
  218.         $query = "SELECT " . ( $args['fields'] === 'date=>views' ? 'pvc.period, ' : '' ) . "SUM( IFNULL( pvc.count, 0 ) ) AS post_views
  219.                 FROM " . $wpdb->prefix . "posts wpp
  220.                 LEFT JOIN " . $wpdb->prefix . "post_views pvc ON pvc.id = wpp.ID" . ( $views_query !== '' ? ' ' . $views_query : ' AND pvc.type = 4' ) .
  221.                 ( ! empty( $args['post_id'] ) ? ' AND pvc.id IN (' . $args['post_id'] . ')' : '' ) . "
  222.                 " . ( $args['post_type'] !== '' ? "WHERE wpp.post_type IN (" . $args['post_type'] . ")" : '' ) . "
  223.                 " . ( $views_query !== '' ? 'GROUP BY pvc.period' : '' ) . "
  224.                 HAVING post_views > 0";
  225.  
  226.         // get cached data
  227.         $post_views = wp_cache_get( md5( $query ), 'pvc-get_views' );
  228.  
  229.         // cached data not found?
  230.         if ( $post_views === false ) {
  231.             if ( $args['fields'] === 'date=>views' && ! empty( $range ) ) {
  232.                 $results = $wpdb->get_results( $query );
  233.  
  234.                 if ( ! empty( $results ) ) {
  235.                     foreach( $results as $row ) {
  236.                         $range[$row->period] = (int) $row->post_views;
  237.                     }
  238.                 }
  239.  
  240.                 $post_views = $range;
  241.             } else
  242.                 $post_views = (int) $wpdb->get_var( $query );
  243.  
  244.             // set the cache expiration, 5 minutes by default
  245.             $expire = absint( apply_filters( 'pvc_object_cache_expire', 5 * 60 ) );
  246.  
  247.             wp_cache_add( md5( $query ), $post_views, 'pvc-get_views', $expire );
  248.         }
  249.  
  250.         return apply_filters( 'pvc_get_views', $post_views );
  251.     }
  252.  
  253. }
  254.  
  255. /**
  256.  * Display post views for a given post.
  257.  *
  258.  * @param  int|array $post_id
  259.  * @param bool $display
  260.  * @return mixed
  261.  */
  262. if ( ! function_exists( 'pvc_post_views' ) ) {
  263.  
  264.     function pvc_post_views( $post_id = 0, $echo = true ) {
  265.  
  266.         // get all data
  267.         $post_id = (int) ( empty( $post_id ) ? get_the_ID() : $post_id );
  268.         $options = Post_Views_Counter()->options['display'];
  269.         $views = pvc_get_post_views( $post_id );
  270.  
  271.         // prepare display
  272.         $label = apply_filters( 'pvc_post_views_label', ( function_exists( 'icl_t' ) ?
  273.         icl_t( 'Post Views Counter', 'Post Views Label', $options['label'] ) : $options['label'] ), $post_id );
  274.         // get icon
  275.         $icon_class = ( $options['icon_class'] !== '' ? esc_attr( $options['icon_class'] ) : '' );
  276.         // add dashicons class if needed
  277.         $icon_class = strpos( $icon_class, 'dashicons' ) === 0 ? 'dashicons ' . $icon_class : $icon_class;
  278.        
  279.         $icon = apply_filters( 'pvc_post_views_icon', '<span class="post-views-icon ' . $icon_class . '"></span>', $post_id );
  280.  
  281.         $html = apply_filters(
  282.             'pvc_post_views_html', '<div class="post-views post-' . $post_id . ' entry-meta">
  283.             ' . ($options['display_style']['icon'] && $icon_class !== '' ? $icon : '') . '
  284.             ' . ($options['display_style']['text'] ? '<span class="post-views-label">' . $label . ' </span>' : '') . '
  285.             <span class="post-views-count">' . number_format_i18n( $views ) . '</span>
  286.             </div>', $post_id, $views, $label, $icon
  287.         );
  288.  
  289.         if ( $echo )
  290.             echo $html;
  291.         else
  292.             return $html;
  293.     }
  294.  
  295. }
  296.  
  297. /**
  298.  * Get most viewed posts.
  299.  *
  300.  * @param array $args
  301.  * @return array
  302.  */
  303. if ( ! function_exists( 'pvc_get_most_viewed_posts' ) ) {
  304.  
  305.     function pvc_get_most_viewed_posts( $args = array() ) {
  306.         $args = array_merge(
  307.             array(
  308.                 'posts_per_page' => 10,
  309.                 'order'          => 'desc',
  310.                 'post_type'      => 'post'
  311.             ), $args
  312.         );
  313.  
  314.         $args = apply_filters( 'pvc_get_most_viewed_posts_args', $args );
  315.  
  316.         // force to use filters
  317.         $args['suppress_filters'] = false;
  318.  
  319.         // force to use post views as order
  320.         $args['orderby'] = 'post_views';
  321.  
  322.         // force to get all fields
  323.         $args['fields'] = '';
  324.  
  325.         return apply_filters( 'pvc_get_most_viewed_posts', get_posts( $args ), $args );
  326.     }
  327.  
  328. }
  329.  
  330. /**
  331.  * Display a list of most viewed posts.
  332.  *
  333.  * @param array $post_id
  334.  * @param bool $display
  335.  * @return mixed
  336.  */
  337. if ( ! function_exists( 'pvc_most_viewed_posts' ) ) {
  338.  
  339.     function pvc_most_viewed_posts( $args = array(), $display = true ) {
  340.         $defaults = array(
  341.             'number_of_posts'        => 5,
  342.             'post_type'              => array( 'post' ),
  343.             'order'                  => 'desc',
  344.             'thumbnail_size'         => 'thumbnail',
  345.             'show_post_views'        => true,
  346.             'show_post_thumbnail'    => false,
  347.             'show_post_excerpt'      => false,
  348.             'no_posts_message'       => __( 'No Posts', 'post-views-counter' ),
  349.             'item_before'            => '',
  350.             'item_after'             => ''
  351.         );
  352.  
  353.         $args = apply_filters( 'pvc_most_viewed_posts_args', wp_parse_args( $args, $defaults ) );
  354.  
  355.         $args['show_post_views'] = (bool) $args['show_post_views'];
  356.         $args['show_post_thumbnail'] = (bool) $args['show_post_thumbnail'];
  357.         $args['show_post_excerpt'] = (bool) $args['show_post_excerpt'];
  358.  
  359.         $posts = pvc_get_most_viewed_posts(
  360.         array(
  361.             'posts_per_page' => (isset( $args['number_of_posts'] ) ? (int) $args['number_of_posts'] : $defaults['number_of_posts']),
  362.             'order'          => (isset( $args['order'] ) ? $args['order'] : $defaults['order']),
  363.             'post_type'      => (isset( $args['post_type'] ) ? $args['post_type'] : $defaults['post_type'])
  364.         )
  365.         );
  366.  
  367.         if ( ! empty( $posts ) ) {
  368.             $html = '
  369.             <ul id="most-viewed">';
  370.                 $i = 1 ;
  371.                 $x = 0;
  372.                
  373.                 foreach ( $posts as $num_pos=>$post ) {
  374.                     setup_postdata( $post );
  375.                     $cond = number_format_i18n( pvc_get_post_views( $post->ID ) );
  376.                    
  377.                     $html .= '
  378.                     <li class="most-viewed row">';
  379.                            
  380.                     if ( $i <= $args['number_of_posts'] && $x < $i ){
  381.                         $x++;
  382.                         $html .= '
  383.                                 <div class="row">
  384.                                 <div class="most-viewed-left">';
  385.                         $html .= apply_filters('pvc_most_viewed_posts_item_before', $args['item_before'], $post);
  386.                                 //$html .= $i . '<br/>'
  387.                                 //. $cond .' views<br/>';
  388.                                            
  389.                         $html .= '<span id="detailed">NO ' . $x . '</span><br/>' . '
  390.                             <a class="row post-title" href="' . get_permalink( $post->ID ) . '">' .
  391.                                 get_the_title( $post->ID ) . '</a>' . ($args['show_post_views'] ? '<br/><span class="count">' .
  392.                                 $cond . ' views</span><br/>' : '');
  393.                         if($num_pos==0){ $html.=$post->post_excerpt; }                 
  394.                                                
  395.                         $excerpt = '';
  396.  
  397.                         if ( $args['show_post_excerpt'] ) {
  398.                             if ( empty( $post->post_excerpt ) )
  399.                                 $text = $post->post_content;
  400.                             else
  401.                                 $text = $post->post_excerpt;
  402.              
  403.                             if ( ! empty( $text ) )
  404.                                 $excerpt = wp_trim_words( str_replace( ']]>', ']]&gt;', strip_shortcodes( $text ) ),
  405.                                 apply_filters( 'excerpt_length', 55 ), apply_filters( 'excerpt_more', ' ' . '[&hellip;]' ) );
  406.                         }
  407.                                        
  408.                         if ( ! empty( $excerpt ) )
  409.                        
  410.                         $html .= '                             
  411.                                 <div class="post-excerpt">' . esc_html( $excerpt ) . '</div>';
  412.                         $html .= '</div>
  413.                                 <div class="most-viewed-right">';
  414.                                        
  415.                         if ( $args['show_post_thumbnail'] && has_post_thumbnail( $post->ID ) ) {
  416.                                 $html .= '
  417.                                 <div class="post-thumbnail">' . get_the_post_thumbnail( $post->ID,
  418.                                 $args['thumbnail_size'] ) . '</div>';
  419.                         }
  420.                                            
  421.                         $html .= apply_filters( 'pvc_most_viewed_posts_item_after', $args['item_after'], $post );
  422.                         $html .= '</div></div>';
  423.                        
  424.                        
  425.                     /*switch( $j = max(array($cond)) ){
  426.                         case( $j = $args['number_of_posts'] ): 
  427.                             $html .= '
  428.                             <div class="row">
  429.                             <div class="most-viewed-left">';
  430.                                    
  431.                             $html .= apply_filters('pvc_most_viewed_posts_item_before', $args['item_before'], $post);
  432.                                 //$html .= $i . '<br/>'
  433.                                 //. $cond .' views<br/>';
  434.                                        
  435.                                 $html .= '<span id="detailed">NO ' . $i . '</span><br/>' . '
  436.                                     <a class="row post-title" href="' . get_permalink( $post->ID ) . '">' .
  437.                                     get_the_title( $post->ID ) . '</a>' . ($args['show_post_views'] ? '<br/><span class="count">' .
  438.                                     $cond . ' views</span><br/>' : '');
  439.                                        
  440.                                            
  441.                                 $excerpt = '';
  442.  
  443.                                 if ( $args['show_post_excerpt'] ) {
  444.                                     if ( empty( $post->post_excerpt ) )
  445.                                         $text = $post->post_content;
  446.                                     else
  447.                                         $text = $post->post_excerpt;
  448.          
  449.                                     if ( ! empty( $text ) )
  450.                                         $excerpt = wp_trim_words( str_replace( ']]>', ']]&gt;', strip_shortcodes( $text ) ),
  451.                                         apply_filters( 'excerpt_length', 55 ), apply_filters( 'excerpt_more', ' ' . '[&hellip;]' ) );
  452.                                 }
  453.                                    
  454.                                 if ( ! empty( $excerpt ) )
  455.                                     $html .= '                             
  456.                                     <div class="post-excerpt">' . esc_html( $excerpt ) . '</div>';
  457.                                            
  458.                                 $html .= '</div>
  459.                                 <div class="most-viewed-right">';
  460.                                    
  461.                                 if ( $args['show_post_thumbnail'] && has_post_thumbnail( $post->ID ) ) {
  462.                                     $html .= '
  463.                                     <div class="post-thumbnail">' . get_the_post_thumbnail( $post->ID,
  464.                                     $args['thumbnail_size'] ) . '</div>';
  465.                                 }
  466.                                        
  467.                                 $html .= apply_filters( 'pvc_most_viewed_posts_item_after', $args['item_after'], $post );
  468.                                 $html .= '</div></div>';
  469.                                 break;
  470.                                
  471.                             case( $j > $i ):
  472.                                 $html = '<div id="list-viewed1"><div id="number2">' . $i . '</div>' .
  473.                                 '<a class="post-title-2" href="' . get_permalink( $post->ID ) . '">' .
  474.                                     get_the_title( $post->ID ) . '</a>' . '<div class="count-views">' . $cond . '</div></div>';
  475.                                 break;
  476.                                
  477.                             default:
  478.                                 $html = '<div id="list-viewed1"><div id="number2">' . $i . '</div>' .
  479.                                 '<a class="post-title-2" href="' . get_permalink( $post->ID ) . '">' .
  480.                                         get_the_title( $post->ID ) . '</a>' . '<div class="count-views">' . $cond . '</div></div>';
  481.                                
  482.                         }
  483.                                     */
  484.                     }
  485.                     $i++;
  486.                     if ( $x = $i ){
  487.                         $html .= '<div id="list-viewed1" class="row"><div id="number2">' . $x . '</div>' .
  488.                                 '<a class="post-title-2" href="' . get_permalink( $post->ID ) . '">' .
  489.                                         get_the_title( $post->ID ) . '</a>' . '<div class="count-views">' . $cond . '</div></div>';
  490.                     }else
  491.                         $html .= '<div id="list-viewed1"><div id="number2">' . $x . '</div>' .
  492.                                 '<a class="post-title-2" href="' . get_permalink( $post->ID ) . '">' .
  493.                                         get_the_title( $post->ID ) . '</a>' . '<div class="count-views">' . $cond . '</div></div>';
  494.                    
  495.                     $html .= '
  496.                     </li>';
  497.            
  498.                 }
  499.                 wp_reset_postdata();
  500.  
  501.             $html .= '
  502.             </ul>';
  503.         }else
  504.             $html = $args['no_posts_message'];
  505.  
  506.         $html = apply_filters( 'pvc_most_viewed_posts_html', $html, $args );
  507.  
  508.         if ( $display )
  509.             echo $html;
  510.         else
  511.             return $html;
  512.     }
  513.  
  514. }
  515.  
  516. /**
  517.  * View post manually function.
  518.  *
  519.  * @since 1.2.0
  520.  * @param int $post_id
  521.  * @return bool
  522.  */
  523. function pvc_view_post( $post_id = 0 ) {
  524.     $post_id = (int) ( empty( $post_id ) ? get_the_ID() : $post_id );
  525.  
  526.     if ( ! $post_id )
  527.         return false;
  528.  
  529.     Post_Views_Counter()->counter->check_post( $post_id );
  530.  
  531.     return true;
  532. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement