analytics.php
| 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 | ], |
| 74 | ] |
| 75 | ); |
| 76 | |
| 77 | add_filter( 'bsf_core_stats', [ $this, 'add_srfm_analytics_data' ] ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Callback function to add SureForms specific analytics data. |
| 82 | * |
| 83 | * @param array $stats_data existing stats_data. |
| 84 | * @since 1.4.0 |
| 85 | * @return array |
| 86 | */ |
| 87 | public function add_srfm_analytics_data( $stats_data ) { |
| 88 | $stats_data['plugin_data']['sureforms'] = [ |
| 89 | 'free_version' => SRFM_VER, |
| 90 | 'site_language' => get_locale(), |
| 91 | 'most_used_anti_spam' => $this->most_used_anti_spam(), |
| 92 | ]; |
| 93 | $stats_data['plugin_data']['sureforms']['numeric_values'] = [ |
| 94 | 'total_forms' => wp_count_posts( SRFM_FORMS_POST_TYPE )->publish ?? 0, |
| 95 | 'instant_forms_enabled' => $this->instant_forms_enabled(), |
| 96 | 'forms_using_custom_css' => $this->forms_using_custom_css(), |
| 97 | 'ai_generated_forms' => $this->ai_generated_forms(), |
| 98 | 'total_entries' => Entries::get_total_entries_by_status(), |
| 99 | ]; |
| 100 | |
| 101 | $stats_data['plugin_data']['sureforms'] = array_merge_recursive( $stats_data['plugin_data']['sureforms'], $this->global_settings_data() ); |
| 102 | |
| 103 | return $stats_data; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Return total number of forms using instant forms. |
| 108 | * |
| 109 | * @since 1.4.0 |
| 110 | * @return int |
| 111 | */ |
| 112 | public function instant_forms_enabled() { |
| 113 | $meta_query = [ |
| 114 | [ |
| 115 | 'key' => '_srfm_instant_form_settings', |
| 116 | 'value' => '"enable_instant_form";b:1;', |
| 117 | 'compare' => 'LIKE', |
| 118 | ], |
| 119 | ]; |
| 120 | |
| 121 | return $this->custom_wp_query_total_posts( $meta_query ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Return total number of ai generated forms. |
| 126 | * |
| 127 | * @since 1.4.0 |
| 128 | * @return int |
| 129 | */ |
| 130 | public function ai_generated_forms() { |
| 131 | $meta_query = [ |
| 132 | [ |
| 133 | 'key' => '_srfm_is_ai_generated', |
| 134 | 'value' => '', |
| 135 | 'compare' => '!=', // Checks if the value is NOT empty. |
| 136 | ], |
| 137 | ]; |
| 138 | |
| 139 | return $this->custom_wp_query_total_posts( $meta_query ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Return most used anti-spam type on this site. |
| 144 | * |
| 145 | * @since 1.4.4 |
| 146 | * @return int |
| 147 | */ |
| 148 | public function most_used_anti_spam() { |
| 149 | global $wpdb; |
| 150 | |
| 151 | // Attempt to get from cache first. |
| 152 | $cache_key = 'most_used_anti_spam'; |
| 153 | $cached_result = wp_cache_get( $cache_key, 'sureforms' ); |
| 154 | |
| 155 | if ( false !== $cached_result ) { |
| 156 | return $cached_result; |
| 157 | } |
| 158 | |
| 159 | $meta_key = '_srfm_captcha_security_type'; |
| 160 | |
| 161 | // Query to get the most used captcha type. |
| 162 | // PHPCS: Ignore direct database query warning, as there is no built-in alternative. |
| 163 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 164 | $result = $wpdb->get_row( |
| 165 | $wpdb->prepare( |
| 166 | " |
| 167 | SELECT meta_value, COUNT(meta_value) as count |
| 168 | FROM {$wpdb->postmeta} |
| 169 | WHERE meta_key = %s |
| 170 | AND meta_value != '' |
| 171 | GROUP BY meta_value |
| 172 | ORDER BY count DESC |
| 173 | LIMIT 1 |
| 174 | ", |
| 175 | $meta_key |
| 176 | ), |
| 177 | ARRAY_A |
| 178 | ); |
| 179 | |
| 180 | $output = ''; |
| 181 | if ( $result && ! empty( $result['meta_value'] ) ) { |
| 182 | switch ( $result['meta_value'] ) { |
| 183 | case 'g-recaptcha': |
| 184 | $output = 'Google reCAPTCHA'; |
| 185 | break; |
| 186 | |
| 187 | case 'cf-turnstile': |
| 188 | $output = 'CloudFlare Turnstile'; |
| 189 | break; |
| 190 | |
| 191 | case 'hcaptcha': |
| 192 | $output = 'hCaptcha'; |
| 193 | break; |
| 194 | |
| 195 | default: |
| 196 | $output = ''; |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // Store result in cache for 1 hour. |
| 202 | wp_cache_set( $cache_key, $output, 'sureforms', HOUR_IN_SECONDS ); |
| 203 | |
| 204 | return $output; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Returns total number of forms using custom css. |
| 209 | * |
| 210 | * @since 1.4.0 |
| 211 | * @return int |
| 212 | */ |
| 213 | public function forms_using_custom_css() { |
| 214 | $meta_query = [ |
| 215 | [ |
| 216 | 'key' => '_srfm_form_custom_css', |
| 217 | 'value' => '', |
| 218 | 'compare' => '!=', // Checks if the value is NOT empty. |
| 219 | ], |
| 220 | ]; |
| 221 | |
| 222 | return $this->custom_wp_query_total_posts( $meta_query ); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Generates global setting data for analytics |
| 227 | * |
| 228 | * @since 1.4.0 |
| 229 | * @return array |
| 230 | */ |
| 231 | public function global_settings_data() { |
| 232 | $global_data = []; |
| 233 | |
| 234 | $security_settings = get_option( 'srfm_security_settings_options', [] ); |
| 235 | $global_data['boolean_values']['honeypot_enabled'] = isset( $security_settings['srfm_honeypot'] ) && true === $security_settings['srfm_honeypot']; |
| 236 | |
| 237 | $email_summary_data = get_option( 'srfm_email_summary_settings_options', [] ); |
| 238 | $global_data['boolean_values']['email_summary_enabled'] = isset( $email_summary_data['srfm_email_summary'] ) && true === $email_summary_data['srfm_email_summary']; |
| 239 | |
| 240 | $global_data['boolean_values']['suretriggers_active'] = is_plugin_active( 'suretriggers/suretriggers.php' ); |
| 241 | |
| 242 | $bsf_internal_referrer = get_option( 'bsf_product_referers', [] ); |
| 243 | if ( ! empty( $bsf_internal_referrer['sureforms'] ) ) { |
| 244 | $global_data['internal_referer'] = $bsf_internal_referrer['sureforms']; |
| 245 | } else { |
| 246 | $global_data['internal_referer'] = ''; |
| 247 | } |
| 248 | |
| 249 | $general_settings = get_option( 'srfm_general_settings_options', [] ); |
| 250 | $global_data['boolean_values']['ip_logging_enabled'] = ! empty( $general_settings['srfm_ip_log'] ); |
| 251 | |
| 252 | $validation_messages = get_option( 'srfm_default_dynamic_block_option', [] ); |
| 253 | $global_data['boolean_values']['custom_validation_message'] = ! empty( $validation_messages ) && is_array( $validation_messages ); |
| 254 | |
| 255 | return $global_data; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Runs custom WP_Query to fetch data as per requirement |
| 260 | * |
| 261 | * @param array $meta_query meta query array for WP_Query. |
| 262 | * @since 1.4.0 |
| 263 | * @return int |
| 264 | */ |
| 265 | private function custom_wp_query_total_posts( $meta_query ) { |
| 266 | |
| 267 | $args = [ |
| 268 | 'post_type' => SRFM_FORMS_POST_TYPE, |
| 269 | 'post_status' => 'publish', |
| 270 | 'posts_per_page' => -1, |
| 271 | '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. |
| 272 | ]; |
| 273 | |
| 274 | $query = new \WP_Query( $args ); |
| 275 | $posts_count = $query->found_posts; |
| 276 | |
| 277 | wp_reset_postdata(); |
| 278 | |
| 279 | return $posts_count; |
| 280 | } |
| 281 | } |
| 282 |