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