PluginProbe
Property Hive / 2.3.0
Property Hive v2.3.0
2.3.0 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 1.4.62 All 260 releases
propertyhive / includes / class-ph-search-analytics.php

class-ph-search-analytics.php in Property Hive 2.3.0, at includes/class-ph-search-analytics.php

114 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 class PH_Search_Analytics
8 {
9 private $recorded = false;
10
11 public function __construct()
12 {
13 add_action(
14 'template_redirect',
15 array( $this, 'look_for_submitted_search' )
16 );
17
18 add_action(
19 'propertyhive_property_search_performed',
20 array( $this, 'record_property_search' )
21 );
22
23 add_action(
24 'propertyhive_cleanup_search_analytics',
25 array( $this, 'clear_old_searches' )
26 );
27 }
28
29 public function look_for_submitted_search()
30 {
31 if ( is_admin() || wp_doing_ajax() || wp_doing_cron() )
32 {
33 return;
34 }
35
36 if ( current_user_can( 'manage_propertyhive' ) )
37 {
38 return;
39 }
40
41 if (
42 !is_post_type_archive( 'property' ) &&
43 !is_page( ph_get_page_id( 'search_results' ) )
44 )
45 {
46 return;
47 }
48
49 if ( apply_filters( 'propertyhive_enable_search_analytics', true ) === false )
50 {
51 return;
52 }
53
54 // Ignore page 2 onwards
55 if ( is_paged() )
56 {
57 return;
58 }
59
60 do_action( 'propertyhive_property_search_performed' );
61 }
62
63 public function record_property_search()
64 {
65 global $wpdb;
66
67 if ( $this->recorded )
68 {
69 return;
70 }
71
72 $this->recorded = true;
73
74 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Append a UTC timestamp to the plugin's custom analytics table using wpdb field formats.
75 $result = $wpdb->insert(
76 $wpdb->prefix . 'ph_search_log',
77 array(
78 'searched_at' => current_time( 'mysql', true ),
79 ),
80 array(
81 '%s',
82 )
83 );
84
85 if ( false === $result )
86 {
87 $this->recorded = false;
88 }
89 }
90
91 public function clear_old_searches()
92 {
93 global $wpdb;
94
95 $cutoff = gmdate( 'Y-m-d H:i:s', strtotime( '-90 days' ) );
96
97 do
98 {
99 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Retention deletes batches from the custom analytics table; its counts are read live and have no object-cache entries.
100 $deleted = $wpdb->query(
101 $wpdb->prepare(
102 "DELETE FROM {$wpdb->prefix}ph_search_log
103 WHERE searched_at < %s
104 ORDER BY searched_at ASC
105 LIMIT 500",
106 $cutoff
107 )
108 );
109 }
110 while ( 500 === $deleted );
111 }
112 }
113
114 new PH_Search_Analytics();