PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.7.3
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.7.3
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 All 96 releases
sureforms / admin / analytics.php
analytics.php
283 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Analytics class helps to connect BSFAnalytics.
4 *
5 * @package sureforms.
6 */
7
8 namespace SRFM\Admin;
9
10 use SRFM\Inc\Database\Tables\Entries;
11 use SRFM\Inc\Traits\Get_Instance;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly.
15 }
16 /**
17 * Analytics class.
18 *
19 * @since 1.4.0
20 */
21 class Analytics {
22 use Get_Instance;
23
24 /**
25 * Class constructor.
26 *
27 * @return void
28 * @since 1.4.0
29 */
30 public function __construct() {
31 /*
32 * BSF Analytics.
33 */
34 if ( ! class_exists( 'BSF_Analytics_Loader' ) ) {
35 require_once SRFM_DIR . 'inc/lib/bsf-analytics/class-bsf-analytics-loader.php';
36 }
37
38 if ( ! class_exists( 'Astra_Notices' ) ) {
39 require_once SRFM_DIR . 'inc/lib/astra-notices/class-astra-notices.php';
40 }
41
42 add_filter(
43 'uds_survey_allowed_screens',
44 static function () {
45 return [ 'plugins' ];
46 }
47 );
48
49 $srfm_bsf_analytics = \BSF_Analytics_Loader::get_instance();
50
51 $srfm_bsf_analytics->set_entity(
52 [
53 'sureforms' => [
54 'product_name' => 'SureForms',
55 'path' => SRFM_DIR . 'inc/lib/bsf-analytics',
56 'author' => 'SureForms',
57 'time_to_display' => '+24 hours',
58 'deactivation_survey' => apply_filters(
59 'srfm_deactivation_survey_data',
60 [
61 [
62 'id' => 'deactivation-survey-sureforms',
63 'popup_logo' => SRFM_URL . 'admin/assets/sureforms-logo.png',
64 'plugin_slug' => 'sureforms',
65 'popup_title' => 'Quick Feedback',
66 'support_url' => 'https://sureforms.com/contact/',
67 'popup_description' => 'If you have a moment, please share why you are deactivating SureForms:',
68 'show_on_screens' => [ 'plugins' ],
69 'plugin_version' => SRFM_VER,
70 ],
71 ]
72 ),
73 'hide_optin_checkbox' => true,
74 ],
75 ]
76 );
77
78 add_filter( 'bsf_core_stats', [ $this, 'add_srfm_analytics_data' ] );
79 }
80
81 /**
82 * Callback function to add SureForms specific analytics data.
83 *
84 * @param array $stats_data existing stats_data.
85 * @since 1.4.0
86 * @return array
87 */
88 public function add_srfm_analytics_data( $stats_data ) {
89 $stats_data['plugin_data']['sureforms'] = [
90 'free_version' => SRFM_VER,
91 'site_language' => get_locale(),
92 'most_used_anti_spam' => $this->most_used_anti_spam(),
93 ];
94 $stats_data['plugin_data']['sureforms']['numeric_values'] = [
95 'total_forms' => wp_count_posts( SRFM_FORMS_POST_TYPE )->publish ?? 0,
96 'instant_forms_enabled' => $this->instant_forms_enabled(),
97 'forms_using_custom_css' => $this->forms_using_custom_css(),
98 'ai_generated_forms' => $this->ai_generated_forms(),
99 'total_entries' => Entries::get_total_entries_by_status(),
100 ];
101
102 $stats_data['plugin_data']['sureforms'] = array_merge_recursive( $stats_data['plugin_data']['sureforms'], $this->global_settings_data() );
103
104 return $stats_data;
105 }
106
107 /**
108 * Return total number of forms using instant forms.
109 *
110 * @since 1.4.0
111 * @return int
112 */
113 public function instant_forms_enabled() {
114 $meta_query = [
115 [
116 'key' => '_srfm_instant_form_settings',
117 'value' => '"enable_instant_form";b:1;',
118 'compare' => 'LIKE',
119 ],
120 ];
121
122 return $this->custom_wp_query_total_posts( $meta_query );
123 }
124
125 /**
126 * Return total number of ai generated forms.
127 *
128 * @since 1.4.0
129 * @return int
130 */
131 public function ai_generated_forms() {
132 $meta_query = [
133 [
134 'key' => '_srfm_is_ai_generated',
135 'value' => '',
136 'compare' => '!=', // Checks if the value is NOT empty.
137 ],
138 ];
139
140 return $this->custom_wp_query_total_posts( $meta_query );
141 }
142
143 /**
144 * Return most used anti-spam type on this site.
145 *
146 * @since 1.4.4
147 * @return int
148 */
149 public function most_used_anti_spam() {
150 global $wpdb;
151
152 // Attempt to get from cache first.
153 $cache_key = 'most_used_anti_spam';
154 $cached_result = wp_cache_get( $cache_key, 'sureforms' );
155
156 if ( false !== $cached_result ) {
157 return $cached_result;
158 }
159
160 $meta_key = '_srfm_captcha_security_type';
161
162 // Query to get the most used captcha type.
163 // PHPCS: Ignore direct database query warning, as there is no built-in alternative.
164 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
165 $result = $wpdb->get_row(
166 $wpdb->prepare(
167 "
168 SELECT meta_value, COUNT(meta_value) as count
169 FROM {$wpdb->postmeta}
170 WHERE meta_key = %s
171 AND meta_value != ''
172 GROUP BY meta_value
173 ORDER BY count DESC
174 LIMIT 1
175 ",
176 $meta_key
177 ),
178 ARRAY_A
179 );
180
181 $output = '';
182 if ( $result && ! empty( $result['meta_value'] ) ) {
183 switch ( $result['meta_value'] ) {
184 case 'g-recaptcha':
185 $output = 'Google reCAPTCHA';
186 break;
187
188 case 'cf-turnstile':
189 $output = 'CloudFlare Turnstile';
190 break;
191
192 case 'hcaptcha':
193 $output = 'hCaptcha';
194 break;
195
196 default:
197 $output = '';
198 break;
199 }
200 }
201
202 // Store result in cache for 1 hour.
203 wp_cache_set( $cache_key, $output, 'sureforms', HOUR_IN_SECONDS );
204
205 return $output;
206 }
207
208 /**
209 * Returns total number of forms using custom css.
210 *
211 * @since 1.4.0
212 * @return int
213 */
214 public function forms_using_custom_css() {
215 $meta_query = [
216 [
217 'key' => '_srfm_form_custom_css',
218 'value' => '',
219 'compare' => '!=', // Checks if the value is NOT empty.
220 ],
221 ];
222
223 return $this->custom_wp_query_total_posts( $meta_query );
224 }
225
226 /**
227 * Generates global setting data for analytics
228 *
229 * @since 1.4.0
230 * @return array
231 */
232 public function global_settings_data() {
233 $global_data = [];
234
235 $security_settings = get_option( 'srfm_security_settings_options', [] );
236 $global_data['boolean_values']['honeypot_enabled'] = isset( $security_settings['srfm_honeypot'] ) && true === $security_settings['srfm_honeypot'];
237
238 $email_summary_data = get_option( 'srfm_email_summary_settings_options', [] );
239 $global_data['boolean_values']['email_summary_enabled'] = isset( $email_summary_data['srfm_email_summary'] ) && true === $email_summary_data['srfm_email_summary'];
240
241 $global_data['boolean_values']['suretriggers_active'] = is_plugin_active( 'suretriggers/suretriggers.php' );
242
243 $bsf_internal_referrer = get_option( 'bsf_product_referers', [] );
244 if ( ! empty( $bsf_internal_referrer['sureforms'] ) ) {
245 $global_data['internal_referer'] = $bsf_internal_referrer['sureforms'];
246 } else {
247 $global_data['internal_referer'] = '';
248 }
249
250 $general_settings = get_option( 'srfm_general_settings_options', [] );
251 $global_data['boolean_values']['ip_logging_enabled'] = ! empty( $general_settings['srfm_ip_log'] );
252
253 $validation_messages = get_option( 'srfm_default_dynamic_block_option', [] );
254 $global_data['boolean_values']['custom_validation_message'] = ! empty( $validation_messages ) && is_array( $validation_messages );
255
256 return $global_data;
257 }
258
259 /**
260 * Runs custom WP_Query to fetch data as per requirement
261 *
262 * @param array $meta_query meta query array for WP_Query.
263 * @since 1.4.0
264 * @return int
265 */
266 private function custom_wp_query_total_posts( $meta_query ) {
267
268 $args = [
269 'post_type' => SRFM_FORMS_POST_TYPE,
270 'post_status' => 'publish',
271 'posts_per_page' => -1,
272 'meta_query' => $meta_query, //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Meta query required as we need to fetch count of nested data.
273 ];
274
275 $query = new \WP_Query( $args );
276 $posts_count = $query->found_posts;
277
278 wp_reset_postdata();
279
280 return $posts_count;
281 }
282 }
283