PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.4.5
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.4.5
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 in SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz 1.4.5, at admin/analytics.php

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