> 문서 > 랜덤 사용자 뷰 카운트

랜덤 사용자 뷰 카운트

2022년 3월 31일

function random_viewer_number($atts){
       global $post;

       $atts = shortcode_atts(
                     array(
                                   'min'   => '0',
                                   'max'   => '10',
                                   'time'  => '180'
                     ),
                     $atts
       );

       if( !is_user_logged_in() ){
              $nologin = '<span class="viewer_count" data-pid="'.$post->ID.'" data-a="'.$atts["min"].'" data-x="'.$atts["max"].'" data-t="'.$atts["time"].'"></span>';
              return $nologin;
       }

       $view_count = get_transient("viewer_count_".$post->ID );
       if ( false === $view_count ){
              $view_count = get_viewer_count($post->ID, $atts['min'], $atts['max'], $atts['time']);
       }

       return $view_count;
}
add_shortcode( 'random_viewer_number', 'random_viewer_number' );


function get_viewer_count( $pid, $min, $max , $time ){
       $view_count = rand($min,$max);
       set_transient("viewer_count_".$pid ,  $view_count , $time);
       return $view_count;
}

function ajax_get_viewer_count(){
       $view_count = get_transient("viewer_count_".$_POST['pid'] );
       if ( false === $view_count ){
              $view_count = get_viewer_count($_POST['pid'], $_POST['a'], $_POST['x'], $_POST['t']);
       }
       $return_results = array(
              'view_count'	=> $view_count,
       );

       wp_send_json_success($return_results);
}
add_action( 'wp_ajax_nopriv_get_viewer_count', 'ajax_get_viewer_count' );

function post_prev_viewcount($pid){
       if(get_post_type($pid) != 'product'){
              return '';
       }
       $view_count = get_transient("viewer_count_".$pid );
       if ( false === $view_count ){
              $view_count = get_viewer_count($pid, 15, 35, 180);
       }
       return $view_count;
}

 

0개의 댓글

댓글 제출

    0
    장바구니
    장바구니가 비었습니다.
      할인쿠폰 적용