|
3 | 3 | namespace WeDevs\Dokan;
|
4 | 4 |
|
5 | 5 | /**
|
6 |
| - * Pageviews - for counting product post views. |
| 6 | + * Page views - for counting product post views. |
7 | 7 | */
|
8 | 8 | class PageViews {
|
9 | 9 |
|
10 |
| - private $meta_key = 'pageview'; |
11 |
| - |
12 |
| - public function __construct() { |
13 |
| - /* Registers the entry views extension scripts if we're on the correct page. */ |
14 |
| - add_action( 'template_redirect', array( $this, 'load_views' ), 25 ); |
15 |
| - |
16 |
| - /* Add the entry views AJAX actions to the appropriate hooks. */ |
17 |
| - add_action( 'wp_ajax_dokan_pageview', array( $this, 'update_ajax' ) ); |
18 |
| - add_action( 'wp_ajax_nopriv_dokan_pageview', array( $this, 'update_ajax' ) ); |
19 |
| - } |
20 |
| - |
21 |
| - public function load_scripts() { |
22 |
| - $nonce = wp_create_nonce( 'dokan_pageview' ); |
23 |
| - |
24 |
| - echo '<script type="text/javascript"> |
25 |
| - jQuery(document).ready( function($) { |
26 |
| - if(localStorage){ |
27 |
| - let new_date = new Date().toISOString().slice(0, 10); |
28 |
| - let dokan_pageview_count = JSON.parse(localStorage.getItem("dokan_pageview_count")); |
29 |
| - let post_id = ' . get_the_ID() . '; |
30 |
| -
|
31 |
| - if ( dokan_pageview_count === null || ( dokan_pageview_count.today && dokan_pageview_count.today !== new_date ) ) { |
32 |
| - dokan_pageview_count = { "today": new_date, "post_ids": [] }; |
33 |
| - } |
34 |
| - if ( ! dokan_pageview_count.post_ids.includes( post_id ) ) { |
35 |
| - var data = { |
36 |
| - action: "dokan_pageview", |
37 |
| - _ajax_nonce: "' . esc_html( $nonce ) . '", |
38 |
| - post_id: ' . get_the_ID() . ', |
39 |
| - } |
40 |
| - $.post( "' . esc_url( admin_url( 'admin-ajax.php' ) ) . '", data ); |
41 |
| - dokan_pageview_count.post_ids.push( post_id ); |
42 |
| - localStorage.setItem("dokan_pageview_count", JSON.stringify(dokan_pageview_count)); |
43 |
| - } |
44 |
| - } |
45 |
| - } ); |
46 |
| - </script>'; |
47 |
| - } |
48 |
| - |
49 |
| - public function load_views() { |
50 |
| - if ( is_singular( 'product' ) ) { |
51 |
| - global $post; |
52 |
| - |
53 |
| - if ( $post->post_author !== dokan_get_current_user_id() ) { |
54 |
| - wp_enqueue_script( 'jquery' ); |
55 |
| - add_action( 'wp_footer', array( $this, 'load_scripts' ) ); |
56 |
| - } |
57 |
| - } |
58 |
| - } |
59 |
| - |
60 |
| - public function update_view( $post_id = '' ) { |
61 |
| - if ( ! empty( $post_id ) ) { |
62 |
| - $old_views = get_post_meta( $post_id, $this->meta_key, true ); |
63 |
| - $new_views = absint( $old_views ) + 1; |
64 |
| - |
65 |
| - update_post_meta( $post_id, $this->meta_key, $new_views, $old_views ); |
66 |
| - $seller_id = get_post_field( 'post_author', $post_id ); |
67 |
| - Cache::delete( "pageview_{$seller_id}" ); |
68 |
| - } |
69 |
| - } |
70 |
| - |
71 |
| - public function update_ajax() { |
72 |
| - check_ajax_referer( 'dokan_pageview' ); |
73 |
| - |
74 |
| - if ( isset( $_POST['post_id'] ) ) { |
75 |
| - $post_id = absint( $_POST['post_id'] ); |
76 |
| - } |
77 |
| - |
78 |
| - if ( ! empty( $post_id ) ) { |
79 |
| - $this->update_view( $post_id ); |
80 |
| - } |
81 |
| - |
82 |
| - wp_die(); |
83 |
| - } |
84 |
| - |
| 10 | + private $meta_key = 'pageview'; |
| 11 | + |
| 12 | + public function __construct() { |
| 13 | + /* Registers the entry views extension scripts if we're on the correct page. */ |
| 14 | + add_action( 'template_redirect', array( $this, 'load_views' ), 25 ); |
| 15 | + |
| 16 | + /* Add the entry views AJAX actions to the appropriate hooks. */ |
| 17 | + add_action( 'wp_ajax_dokan_pageview', array( $this, 'update_ajax' ) ); |
| 18 | + add_action( 'wp_ajax_nopriv_dokan_pageview', array( $this, 'update_ajax' ) ); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Load the scripts |
| 23 | + * |
| 24 | + * @return void |
| 25 | + */ |
| 26 | + public function load_scripts() { |
| 27 | + wp_enqueue_script( 'dokan-page-views', DOKAN_PLUGIN_ASSEST . '/js/page-views.js', array( 'jquery' ), DOKAN_PLUGIN_VERSION, true ); |
| 28 | + wp_localize_script( |
| 29 | + 'dokan-page-views', |
| 30 | + 'dokanPageViewsParams', |
| 31 | + array( |
| 32 | + 'nonce' => wp_create_nonce( 'dokan_pageview' ), |
| 33 | + 'post_id' => get_the_ID(), |
| 34 | + 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 35 | + ) |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + public function load_views() { |
| 40 | + if ( is_singular( 'product' ) ) { |
| 41 | + global $post; |
| 42 | + |
| 43 | + if ( dokan_get_current_user_id() !== $post->post_author ) { |
| 44 | + wp_enqueue_script( 'jquery' ); |
| 45 | + add_action( 'wp_footer', array( $this, 'load_scripts' ) ); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Update the view count |
| 52 | + * |
| 53 | + * @param int $post_id The post ID |
| 54 | + * |
| 55 | + * @return void |
| 56 | + */ |
| 57 | + public function update_view( $post_id = '' ) { |
| 58 | + if ( ! empty( $post_id ) ) { |
| 59 | + $old_views = get_post_meta( $post_id, $this->meta_key, true ); |
| 60 | + $new_views = absint( $old_views ) + 1; |
| 61 | + |
| 62 | + update_post_meta( $post_id, $this->meta_key, $new_views, $old_views ); |
| 63 | + $seller_id = get_post_field( 'post_author', $post_id ); |
| 64 | + Cache::delete( "pageview_{$seller_id}" ); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Update the view count via AJAX |
| 70 | + * |
| 71 | + * @return void |
| 72 | + */ |
| 73 | + public function update_ajax() { |
| 74 | + check_ajax_referer( 'dokan_pageview' ); |
| 75 | + |
| 76 | + if ( isset( $_POST['post_id'] ) ) { |
| 77 | + $post_id = absint( $_POST['post_id'] ); |
| 78 | + } |
| 79 | + |
| 80 | + if ( ! empty( $post_id ) ) { |
| 81 | + $this->update_view( $post_id ); |
| 82 | + } |
| 83 | + |
| 84 | + wp_die(); |
| 85 | + } |
85 | 86 | }
|
0 commit comments