| @@ -6,13 +6,8 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace SRFM\Admin; |
| 9 | 9 | |
| 10 | -use SRFM\Inc\Database\Register; | |
| 11 | -use SRFM\Inc\Database\Tables\Entries; | |
| 12 | -use SRFM\Inc\Form_Views; | |
| 13 | -use SRFM\Inc\Helper; | |
| 14 | -use SRFM\Inc\Learn; | |
| 15 | 10 | use SRFM\Inc\Traits\Get_Instance; |
| 16 | 11 | |
| 17 | 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 18 | 13 | exit; // Exit if accessed directly. |
| @@ -25,15 +20,8 @@ | ||
| 25 | 20 | class Analytics { |
| 26 | 21 | use Get_Instance; |
| 27 | 22 | |
| 28 | 23 | /** |
| 29 | - * BSF_Analytics_Events instance for one-time event tracking. | |
| 30 | - * | |
| 31 | - * @var \BSF_Analytics_Events|null | |
| 32 | - */ | |
| 33 | - private static $events = null; | |
| 34 | - | |
| 35 | - /** | |
| 36 | 24 | * Class constructor. |
| 37 | 25 | * |
| 38 | 26 | * @return void |
| 39 | 27 | * @since 1.4.0 |
| @@ -45,10 +33,10 @@ | ||
| 45 | 33 | if ( ! class_exists( 'BSF_Analytics_Loader' ) ) { |
| 46 | 34 | require_once SRFM_DIR . 'inc/lib/bsf-analytics/class-bsf-analytics-loader.php'; |
| 47 | 35 | } |
| 48 | 36 | |
| 49 | - if ( ! class_exists( 'BSF_Admin_Notices' ) ) { | |
| 50 | - require_once SRFM_DIR . 'inc/lib/astra-notices/class-bsf-admin-notices.php'; | |
| 37 | + if ( ! class_exists( 'Astra_Notices' ) ) { | |
| 38 | + require_once SRFM_DIR . 'inc/lib/astra-notices/class-astra-notices.php'; | |
| 51 | 39 | } |
| 52 | 40 | |
| 53 | 41 | add_filter( |
| 54 | 42 | 'uds_survey_allowed_screens', |
| @@ -80,51 +68,16 @@ | ||
| 80 | 68 | 'plugin_version' => SRFM_VER, |
| 81 | 69 | ], |
| 82 | 70 | ] |
| 83 | 71 | ), |
| 84 | - 'hide_optin_checkbox' => true, | |
| 85 | 72 | ], |
| 86 | 73 | ] |
| 87 | 74 | ); |
| 88 | 75 | |
| 89 | 76 | add_filter( 'bsf_core_stats', [ $this, 'add_srfm_analytics_data' ] ); |
| 90 | - | |
| 91 | - // Event tracking hooks. | |
| 92 | - add_action( 'current_screen', [ $this, 'track_first_editor_open' ] ); | |
| 93 | - add_action( 'transition_post_status', [ $this, 'track_first_form_published' ], 10, 3 ); | |
| 94 | - add_action( 'save_post', [ $this, 'track_embed_styling_configured' ], 10, 2 ); | |
| 95 | - | |
| 96 | - // Detect state-based events on admin load (dedup prevents repeat tracking). | |
| 97 | - $this->detect_state_events(); | |
| 98 | 77 | } |
| 99 | 78 | |
| 100 | 79 | /** |
| 101 | - * Get the shared BSF_Analytics_Events instance. | |
| 102 | - * | |
| 103 | - * Uses SureForms' Helper option methods so data stays in the | |
| 104 | - * existing srfm_options row — zero migration required. | |
| 105 | - * | |
| 106 | - * @since 2.7.0 | |
| 107 | - * @return \BSF_Analytics_Events | |
| 108 | - */ | |
| 109 | - public static function events() { | |
| 110 | - if ( null === self::$events ) { | |
| 111 | - if ( ! class_exists( 'BSF_Analytics_Events' ) ) { | |
| 112 | - require_once SRFM_DIR . 'inc/lib/bsf-analytics/class-bsf-analytics-events.php'; | |
| 113 | - } | |
| 114 | - | |
| 115 | - self::$events = new \BSF_Analytics_Events( | |
| 116 | - 'sureforms', | |
| 117 | - [ | |
| 118 | - 'get' => [ Helper::class, 'get_srfm_option' ], | |
| 119 | - 'update' => [ Helper::class, 'update_srfm_option' ], | |
| 120 | - ] | |
| 121 | - ); | |
| 122 | - } | |
| 123 | - return self::$events; | |
| 124 | - } | |
| 125 | - | |
| 126 | - /** | |
| 127 | 80 | * Callback function to add SureForms specific analytics data. |
| 128 | 81 | * |
| 129 | 82 | * @param array $stats_data existing stats_data. |
| 130 | 83 | * @since 1.4.0 |
| @@ -130,56 +83,21 @@ | ||
| 130 | 83 | * @since 1.4.0 |
| 131 | 84 | * @return array |
| 132 | 85 | */ |
| 133 | 86 | public function add_srfm_analytics_data( $stats_data ) { |
| 134 | - $stats_data['plugin_data']['sureforms'] = [ | |
| 135 | - 'free_version' => SRFM_VER, | |
| 136 | - 'site_language' => get_locale(), | |
| 137 | - 'most_used_anti_spam' => $this->most_used_anti_spam(), | |
| 138 | - 'user_status' => $this->user_status(), | |
| 139 | - 'pointer_popup_clicked' => $this->pointer_popup_clicked(), | |
| 87 | + $stats_data['plugin_data']['sureforms'] = [ | |
| 88 | + 'free_version' => SRFM_VER, | |
| 89 | + 'site_language' => get_locale(), | |
| 140 | 90 | ]; |
| 141 | - // Every query against the entries table errors when the table is missing, so | |
| 142 | - // resolve that once here rather than letting each caller below trip over it. | |
| 143 | - $entries_table_missing = Register::is_entries_table_missing(); | |
| 144 | - | |
| 145 | 91 | $stats_data['plugin_data']['sureforms']['numeric_values'] = [ |
| 146 | - 'total_forms' => wp_count_posts( SRFM_FORMS_POST_TYPE )->publish ?? 0, | |
| 147 | - 'instant_forms_enabled' => $this->instant_forms_enabled(), | |
| 148 | - 'forms_using_custom_css' => $this->forms_using_custom_css(), | |
| 149 | - 'ai_generated_forms' => $this->ai_generated_forms(), | |
| 150 | - 'ai_generated_payment_forms' => $this->ai_generated_forms( 'payments' ), | |
| 151 | - 'payment_forms' => $this->get_payment_forms_count(), | |
| 152 | - 'total_entries' => $entries_table_missing ? 0 : Entries::get_total_entries_by_status(), | |
| 153 | - 'restricted_forms' => $this->get_restricted_forms(), | |
| 154 | - 'embed_styling_gb_default' => self::embed_styling_gutenberg_count( 'default' ), | |
| 155 | - 'embed_styling_el_default' => self::embed_styling_elementor_count( 'default' ), | |
| 156 | - 'embed_styling_br_default' => self::embed_styling_bricks_count( 'default' ), | |
| 157 | - 'total_form_views' => $this->total_form_views(), | |
| 158 | - 'forms_with_views' => $this->forms_with_views(), | |
| 92 | + 'total_forms' => wp_count_posts( SRFM_FORMS_POST_TYPE )->publish ?? 0, | |
| 93 | + 'instant_forms_enabled' => $this->instant_forms_enabled(), | |
| 94 | + 'forms_using_custom_css' => $this->forms_using_custom_css(), | |
| 95 | + 'ai_generated_forms' => $this->ai_generated_forms(), | |
| 159 | 96 | ]; |
| 160 | 97 | |
| 161 | - // Whether the entries table is currently missing. An event fires once when a | |
| 162 | - // site first sees the notice; this is what shows the state persisting, and | |
| 163 | - // catches a recurrence that the event's one-time dedup would swallow. | |
| 164 | - $stats_data['plugin_data']['sureforms']['boolean_values']['db_entries_table_missing'] = $entries_table_missing; | |
| 165 | - | |
| 166 | 98 | $stats_data['plugin_data']['sureforms'] = array_merge_recursive( $stats_data['plugin_data']['sureforms'], $this->global_settings_data() ); |
| 167 | - // Add KPI tracking data. | |
| 168 | - $kpi_data = $this->get_kpi_tracking_data(); | |
| 169 | - if ( ! empty( $kpi_data ) ) { | |
| 170 | - $stats_data['plugin_data']['sureforms']['kpi_records'] = $kpi_data; | |
| 171 | - } | |
| 172 | 99 | |
| 173 | - // Build learn progress snapshot. | |
| 174 | - $this->get_learn_tracking_data(); | |
| 175 | - | |
| 176 | - // Flush pending events into payload (only if any exist). | |
| 177 | - $pending_events = self::events()->flush_pending(); | |
| 178 | - if ( ! empty( $pending_events ) ) { | |
| 179 | - $stats_data['plugin_data']['sureforms']['events_record'] = $pending_events; | |
| 180 | - } | |
| 181 | - | |
| 182 | 100 | return $stats_data; |
| 183 | 101 | } |
| 184 | 102 | |
| 185 | 103 | /** |
| @@ -202,15 +120,12 @@ | ||
| 202 | 120 | |
| 203 | 121 | /** |
| 204 | 122 | * Return total number of ai generated forms. |
| 205 | 123 | * |
| 206 | - * @param string $form_type Form type to check. | |
| 207 | - * | |
| 208 | 124 | * @since 1.4.0 |
| 209 | 125 | * @return int |
| 210 | 126 | */ |
| 211 | - public function ai_generated_forms( $form_type = '' ) { | |
| 212 | - $form_type = empty( $form_type ) || ! is_string( $form_type ) ? '' : $form_type; | |
| 127 | + public function ai_generated_forms() { | |
| 213 | 128 | $meta_query = [ |
| 214 | 129 | [ |
| 215 | 130 | 'key' => '_srfm_is_ai_generated', |
| 216 | 131 | 'value' => '', |
| @@ -217,82 +132,12 @@ | ||
| 217 | 132 | 'compare' => '!=', // Checks if the value is NOT empty. |
| 218 | 133 | ], |
| 219 | 134 | ]; |
| 220 | 135 | |
| 221 | - if ( 'payments' === $form_type ) { | |
| 222 | - $search = 'wp:srfm/payment'; | |
| 223 | - return $this->custom_wp_query_total_posts_with_search( $meta_query, $search ); | |
| 224 | - } | |
| 225 | - | |
| 226 | 136 | return $this->custom_wp_query_total_posts( $meta_query ); |
| 227 | 137 | } |
| 228 | 138 | |
| 229 | 139 | /** |
| 230 | - * Return most used anti-spam type on this site. | |
| 231 | - * | |
| 232 | - * @since 1.4.4 | |
| 233 | - * @return int | |
| 234 | - */ | |
| 235 | - public function most_used_anti_spam() { | |
| 236 | - global $wpdb; | |
| 237 | - | |
| 238 | - // Attempt to get from cache first. | |
| 239 | - $cache_key = 'most_used_anti_spam'; | |
| 240 | - $cached_result = wp_cache_get( $cache_key, 'sureforms' ); | |
| 241 | - | |
| 242 | - if ( false !== $cached_result ) { | |
| 243 | - return $cached_result; | |
| 244 | - } | |
| 245 | - | |
| 246 | - $meta_key = '_srfm_captcha_security_type'; | |
| 247 | - | |
| 248 | - // Query to get the most used captcha type. | |
| 249 | - // PHPCS: Ignore direct database query warning, as there is no built-in alternative. | |
| 250 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery | |
| 251 | - $result = $wpdb->get_row( | |
| 252 | - $wpdb->prepare( | |
| 253 | - " | |
| 254 | - SELECT meta_value, COUNT(meta_value) as count | |
| 255 | - FROM {$wpdb->postmeta} | |
| 256 | - WHERE meta_key = %s | |
| 257 | - AND meta_value != '' | |
| 258 | - GROUP BY meta_value | |
| 259 | - ORDER BY count DESC | |
| 260 | - LIMIT 1 | |
| 261 | - ", | |
| 262 | - $meta_key | |
| 263 | - ), | |
| 264 | - ARRAY_A | |
| 265 | - ); | |
| 266 | - | |
| 267 | - $output = ''; | |
| 268 | - if ( $result && ! empty( $result['meta_value'] ) ) { | |
| 269 | - switch ( $result['meta_value'] ) { | |
| 270 | - case 'g-recaptcha': | |
| 271 | - $output = 'Google reCAPTCHA'; | |
| 272 | - break; | |
| 273 | - | |
| 274 | - case 'cf-turnstile': | |
| 275 | - $output = 'CloudFlare Turnstile'; | |
| 276 | - break; | |
| 277 | - | |
| 278 | - case 'hcaptcha': | |
| 279 | - $output = 'hCaptcha'; | |
| 280 | - break; | |
| 281 | - | |
| 282 | - default: | |
| 283 | - $output = ''; | |
| 284 | - break; | |
| 285 | - } | |
| 286 | - } | |
| 287 | - | |
| 288 | - // Store result in cache for 1 hour. | |
| 289 | - wp_cache_set( $cache_key, $output, 'sureforms', HOUR_IN_SECONDS ); | |
| 290 | - | |
| 291 | - return $output; | |
| 292 | - } | |
| 293 | - | |
| 294 | - /** | |
| 295 | 140 | * Returns total number of forms using custom css. |
| 296 | 141 | * |
| 297 | 142 | * @since 1.4.0 |
| 298 | 143 | * @return int |
| @@ -309,167 +154,8 @@ | ||
| 309 | 154 | return $this->custom_wp_query_total_posts( $meta_query ); |
| 310 | 155 | } |
| 311 | 156 | |
| 312 | 157 | /** |
| 313 | - * Count Gutenberg srfm/form embed blocks using a specific formTheme. | |
| 314 | - * | |
| 315 | - * When formTheme is 'inherit' (the block.json default), WordPress does not | |
| 316 | - * serialize it in the block comment. So only 'default' and 'custom' appear. | |
| 317 | - * | |
| 318 | - * Counts individual embed blocks (not pages), since a single page can | |
| 319 | - * contain multiple form embeds each with different styling. | |
| 320 | - * | |
| 321 | - * @param string $theme Theme slug to count ('default' or 'custom'). | |
| 322 | - * @since 2.7.0 | |
| 323 | - * @return int | |
| 324 | - */ | |
| 325 | - public static function embed_styling_gutenberg_count( $theme ) { | |
| 326 | - global $wpdb; | |
| 327 | - | |
| 328 | - $cache_key = 'embed_styling_gb_' . $theme; | |
| 329 | - $cached_result = wp_cache_get( $cache_key, 'sureforms' ); | |
| 330 | - | |
| 331 | - if ( false !== $cached_result ) { | |
| 332 | - return (int) $cached_result; | |
| 333 | - } | |
| 334 | - | |
| 335 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- No WP_Query alternative for cross-post-type content search with multiple LIKE conditions. | |
| 336 | - $posts = $wpdb->get_col( | |
| 337 | - $wpdb->prepare( | |
| 338 | - "SELECT post_content FROM {$wpdb->posts} | |
| 339 | - WHERE post_status = 'publish' | |
| 340 | - AND post_content LIKE %s | |
| 341 | - AND post_content LIKE %s", | |
| 342 | - '%' . $wpdb->esc_like( 'wp:srfm/form' ) . '%', | |
| 343 | - '%' . $wpdb->esc_like( '"formTheme":"' . $theme . '"' ) . '%' | |
| 344 | - ) | |
| 345 | - ); | |
| 346 | - | |
| 347 | - $count = 0; | |
| 348 | - $escaped_theme = preg_quote( $theme, '/' ); | |
| 349 | - foreach ( $posts as $content ) { | |
| 350 | - $count += preg_match_all( '/<!--\s*wp:srfm\/form\s+\{[^}]*"formTheme"\s*:\s*"' . $escaped_theme . '"/', $content ); | |
| 351 | - } | |
| 352 | - | |
| 353 | - wp_cache_set( $cache_key, $count, 'sureforms', HOUR_IN_SECONDS ); | |
| 354 | - | |
| 355 | - return $count; | |
| 356 | - } | |
| 357 | - | |
| 358 | - /** | |
| 359 | - * Count Elementor sureforms_form widgets using a specific formTheme. | |
| 360 | - * | |
| 361 | - * Searches _elementor_data post meta for sureforms_form widgets with | |
| 362 | - * a non-inherit formTheme. Counts individual widgets (not pages). | |
| 363 | - * | |
| 364 | - * @param string $theme Theme slug to count ('default' or 'custom'). | |
| 365 | - * @since 2.7.0 | |
| 366 | - * @return int | |
| 367 | - */ | |
| 368 | - public static function embed_styling_elementor_count( $theme ) { | |
| 369 | - global $wpdb; | |
| 370 | - | |
| 371 | - $cache_key = 'embed_styling_el_' . $theme; | |
| 372 | - $cached_result = wp_cache_get( $cache_key, 'sureforms' ); | |
| 373 | - | |
| 374 | - if ( false !== $cached_result ) { | |
| 375 | - return (int) $cached_result; | |
| 376 | - } | |
| 377 | - | |
| 378 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- No WP_Query alternative for post meta content search with multiple LIKE conditions. | |
| 379 | - $meta_values = $wpdb->get_col( | |
| 380 | - $wpdb->prepare( | |
| 381 | - "SELECT pm.meta_value FROM {$wpdb->postmeta} pm | |
| 382 | - INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id | |
| 383 | - WHERE p.post_status = 'publish' | |
| 384 | - AND pm.meta_key = '_elementor_data' | |
| 385 | - AND pm.meta_value LIKE %s | |
| 386 | - AND pm.meta_value LIKE %s", | |
| 387 | - '%' . $wpdb->esc_like( 'sureforms_form' ) . '%', | |
| 388 | - '%' . $wpdb->esc_like( '"formTheme":"' . $theme . '"' ) . '%' | |
| 389 | - ) | |
| 390 | - ); | |
| 391 | - | |
| 392 | - $count = 0; | |
| 393 | - $escaped_theme = preg_quote( $theme, '/' ); | |
| 394 | - foreach ( $meta_values as $json ) { | |
| 395 | - // Count widget instances with the specific formTheme in the JSON. | |
| 396 | - $count += preg_match_all( '/"widgetType"\s*:\s*"sureforms_form"[^}]*"formTheme"\s*:\s*"' . $escaped_theme . '"/', $json ); | |
| 397 | - } | |
| 398 | - | |
| 399 | - wp_cache_set( $cache_key, $count, 'sureforms', HOUR_IN_SECONDS ); | |
| 400 | - | |
| 401 | - return $count; | |
| 402 | - } | |
| 403 | - | |
| 404 | - /** | |
| 405 | - * Count Bricks sureforms elements using a specific formTheme. | |
| 406 | - * | |
| 407 | - * Searches _bricks_page_content_2 post meta (serialized PHP) for | |
| 408 | - * sureforms elements with a non-inherit formTheme. Counts individual elements. | |
| 409 | - * | |
| 410 | - * @param string $theme Theme slug to count ('default' or 'custom'). | |
| 411 | - * @since 2.7.0 | |
| 412 | - * @return int | |
| 413 | - */ | |
| 414 | - public static function embed_styling_bricks_count( $theme ) { | |
| 415 | - global $wpdb; | |
| 416 | - | |
| 417 | - $cache_key = 'embed_styling_br_' . $theme; | |
| 418 | - $cached_result = wp_cache_get( $cache_key, 'sureforms' ); | |
| 419 | - | |
| 420 | - if ( false !== $cached_result ) { | |
| 421 | - return (int) $cached_result; | |
| 422 | - } | |
| 423 | - | |
| 424 | - // Bricks stores element data as serialized PHP in _bricks_page_content_2. | |
| 425 | - // In serialized format: s:9:"formTheme";s:7:"default" (for 'default' theme). | |
| 426 | - $serialized_theme = sprintf( '"formTheme";s:%d:"%s"', strlen( $theme ), $theme ); | |
| 427 | - | |
| 428 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- No WP_Query alternative for post meta content search with multiple LIKE conditions. | |
| 429 | - $meta_values = $wpdb->get_col( | |
| 430 | - $wpdb->prepare( | |
| 431 | - "SELECT pm.meta_value FROM {$wpdb->postmeta} pm | |
| 432 | - INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id | |
| 433 | - WHERE p.post_status = 'publish' | |
| 434 | - AND pm.meta_key = '_bricks_page_content_2' | |
| 435 | - AND pm.meta_value LIKE %s | |
| 436 | - AND pm.meta_value LIKE %s", | |
| 437 | - '%' . $wpdb->esc_like( '"sureforms"' ) . '%', | |
| 438 | - '%' . $wpdb->esc_like( $serialized_theme ) . '%' | |
| 439 | - ) | |
| 440 | - ); | |
| 441 | - | |
| 442 | - $count = 0; | |
| 443 | - $escaped_serial_theme = preg_quote( $serialized_theme, '/' ); | |
| 444 | - foreach ( $meta_values as $data ) { | |
| 445 | - $count += preg_match_all( '/' . $escaped_serial_theme . '/', $data ); | |
| 446 | - } | |
| 447 | - | |
| 448 | - wp_cache_set( $cache_key, $count, 'sureforms', HOUR_IN_SECONDS ); | |
| 449 | - | |
| 450 | - return $count; | |
| 451 | - } | |
| 452 | - | |
| 453 | - /** | |
| 454 | - * Return total number of restricted forms. | |
| 455 | - * | |
| 456 | - * @since 1.10.1 | |
| 457 | - * @return int | |
| 458 | - */ | |
| 459 | - public function get_restricted_forms() { | |
| 460 | - $meta_query = [ | |
| 461 | - [ | |
| 462 | - 'key' => '_srfm_form_restriction', | |
| 463 | - 'value' => '"status":true', | |
| 464 | - 'compare' => 'LIKE', | |
| 465 | - ], | |
| 466 | - ]; | |
| 467 | - | |
| 468 | - return $this->custom_wp_query_total_posts( $meta_query ); | |
| 469 | - } | |
| 470 | - | |
| 471 | - /** | |
| 472 | 158 | * Generates global setting data for analytics |
| 473 | 159 | * |
| 474 | 160 | * @since 1.4.0 |
| 475 | 161 | * @return array |
| @@ -477,12 +163,12 @@ | ||
| 477 | 163 | public function global_settings_data() { |
| 478 | 164 | $global_data = []; |
| 479 | 165 | |
| 480 | 166 | $security_settings = get_option( 'srfm_security_settings_options', [] ); |
| 481 | - $global_data['boolean_values']['honeypot_enabled'] = isset( $security_settings['srfm_honeypot'] ) && true === $security_settings['srfm_honeypot']; | |
| 167 | + $global_data['boolean_values']['honeypot_enabled'] = true === $security_settings['srfm_honeypot']; | |
| 482 | 168 | |
| 483 | 169 | $email_summary_data = get_option( 'srfm_email_summary_settings_options', [] ); |
| 484 | - $global_data['boolean_values']['email_summary_enabled'] = isset( $email_summary_data['srfm_email_summary'] ) && true === $email_summary_data['srfm_email_summary']; | |
| 170 | + $global_data['boolean_values']['email_summary_enabled'] = true === $email_summary_data['srfm_email_summary']; | |
| 485 | 171 | |
| 486 | 172 | $global_data['boolean_values']['suretriggers_active'] = is_plugin_active( 'suretriggers/suretriggers.php' ); |
| 487 | 173 | |
| 488 | 174 | $bsf_internal_referrer = get_option( 'bsf_product_referers', [] ); |
| @@ -497,377 +183,12 @@ | ||
| 497 | 183 | |
| 498 | 184 | $validation_messages = get_option( 'srfm_default_dynamic_block_option', [] ); |
| 499 | 185 | $global_data['boolean_values']['custom_validation_message'] = ! empty( $validation_messages ) && is_array( $validation_messages ); |
| 500 | 186 | |
| 501 | - // Payment analytics - check if any payment method is enabled. | |
| 502 | - $global_data['boolean_values']['stripe_enabled'] = $this->is_stripe_enabled(); | |
| 503 | - | |
| 504 | - // Delegated rather than read from $general_settings directly, so the telemetry | |
| 505 | - // can never disagree with what the Forms list actually shows. That matters | |
| 506 | - // because the default has already changed once: reimplementing the check here | |
| 507 | - // means a future change has two places to be made and one to be forgotten. | |
| 508 | - $global_data['boolean_values']['form_views_columns_enabled'] = Form_Views::get_instance()->is_tracking_enabled(); | |
| 509 | - | |
| 510 | 187 | return $global_data; |
| 511 | 188 | } |
| 512 | 189 | |
| 513 | 190 | /** |
| 514 | - * Total page views counted across all published forms. | |
| 515 | - * | |
| 516 | - * Paired with `forms_with_views` and the existing `total_entries`, this is what | |
| 517 | - * makes the conversion figure interpretable in the warehouse: a site with views | |
| 518 | - * but no entries reads very differently from one with neither. | |
| 519 | - * | |
| 520 | - * Counts only published forms, so views left behind by a trashed or draft form | |
| 521 | - * do not inflate the total against a denominator that no longer includes them. | |
| 522 | - * | |
| 523 | - * @since 2.12.6 | |
| 524 | - * @return int | |
| 525 | - */ | |
| 526 | - public function total_form_views() { | |
| 527 | - global $wpdb; | |
| 528 | - | |
| 529 | - // Deliberately uncached: the only caller is the analytics payload builder, | |
| 530 | - // which runs once per cron pass. A cache would never see a second read inside | |
| 531 | - // a request, and any TTL would always have expired between passes — while | |
| 532 | - // still going stale if a second caller ever appeared. | |
| 533 | - // PHPCS: Ignore direct database query warning, as there is no built-in alternative. | |
| 534 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 535 | - $total = $wpdb->get_var( | |
| 536 | - $wpdb->prepare( | |
| 537 | - " | |
| 538 | - SELECT SUM( pm.meta_value ) | |
| 539 | - FROM {$wpdb->postmeta} pm | |
| 540 | - INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id | |
| 541 | - WHERE pm.meta_key = %s | |
| 542 | - AND p.post_type = %s | |
| 543 | - AND p.post_status = 'publish' | |
| 544 | - ", | |
| 545 | - Form_Views::META_KEY, | |
| 546 | - SRFM_FORMS_POST_TYPE | |
| 547 | - ) | |
| 548 | - ); | |
| 549 | - | |
| 550 | - return Helper::get_integer_value( $total ); | |
| 551 | - } | |
| 552 | - | |
| 553 | - /** | |
| 554 | - * Number of published forms that have been viewed at least once. | |
| 555 | - * | |
| 556 | - * Distinguishes "the feature is on but nothing is embedded anywhere" from "the | |
| 557 | - * beacon is firing" — a total alone cannot, because one busy form looks the | |
| 558 | - * same as many quiet ones. | |
| 559 | - * | |
| 560 | - * @since 2.12.6 | |
| 561 | - * @return int | |
| 562 | - */ | |
| 563 | - public function forms_with_views() { | |
| 564 | - return $this->custom_wp_query_total_posts( | |
| 565 | - [ | |
| 566 | - [ | |
| 567 | - 'key' => Form_Views::META_KEY, | |
| 568 | - 'value' => 0, | |
| 569 | - 'compare' => '>', | |
| 570 | - 'type' => 'NUMERIC', | |
| 571 | - ], | |
| 572 | - ] | |
| 573 | - ); | |
| 574 | - } | |
| 575 | - | |
| 576 | - /** | |
| 577 | - * Returns user status. | |
| 578 | - * | |
| 579 | - * @since 1.8.0 | |
| 580 | - * @return string | |
| 581 | - */ | |
| 582 | - public function user_status() { | |
| 583 | - // First, check if user_active is already set in srfm_options. | |
| 584 | - if ( Helper::get_srfm_option( 'user_active', false ) ) { | |
| 585 | - return 'active'; | |
| 586 | - } | |
| 587 | - // Get up to 10 published SureForms. | |
| 588 | - $forms = get_posts( | |
| 589 | - [ | |
| 590 | - 'post_type' => SRFM_FORMS_POST_TYPE, | |
| 591 | - 'posts_per_page' => 10, | |
| 592 | - 'post_status' => 'publish', | |
| 593 | - ] | |
| 594 | - ); | |
| 595 | - if ( empty( $forms ) ) { | |
| 596 | - return 'inactive'; | |
| 597 | - } | |
| 598 | - foreach ( $forms as $form ) { | |
| 599 | - if ( ! get_post_meta( $form->ID, '_astra_sites_imported_post', true ) ) { | |
| 600 | - // Mark user as active in srfm_options. | |
| 601 | - Helper::update_srfm_option( 'user_active', true ); | |
| 602 | - return 'active'; | |
| 603 | - } | |
| 604 | - } | |
| 605 | - return 'inactive'; | |
| 606 | - } | |
| 607 | - | |
| 608 | - /** | |
| 609 | - * Return pointer popup clicked status. | |
| 610 | - * | |
| 611 | - * @return string pointer click status. | |
| 612 | - * @since 1.8.0 | |
| 613 | - */ | |
| 614 | - public function pointer_popup_clicked() { | |
| 615 | - // Get both values from srfm_options. | |
| 616 | - $accepted = Helper::get_srfm_option( 'pointer_popup_accepted', false ); | |
| 617 | - $dismissed = Helper::get_srfm_option( 'pointer_popup_dismissed', false ); | |
| 618 | - // If neither action has occurred. | |
| 619 | - if ( ! $accepted && ! $dismissed ) { | |
| 620 | - return ''; | |
| 621 | - } | |
| 622 | - | |
| 623 | - // If both are set, return the most recent one. | |
| 624 | - if ( $accepted && $dismissed ) { | |
| 625 | - return $accepted > $dismissed ? 'accepted' : 'dismissed'; | |
| 626 | - } | |
| 627 | - | |
| 628 | - // If only one is set, return it. | |
| 629 | - return $accepted ? 'accepted' : 'dismissed'; | |
| 630 | - } | |
| 631 | - | |
| 632 | - /** | |
| 633 | - * Runs a custom WP_Query to fetch the total number of posts matching the given meta query and optional search string. | |
| 634 | - * | |
| 635 | - * This function is used to count SureForms posts based on specific meta query conditions. | |
| 636 | - * Optionally, a search string can be included to further filter results by keyword match. | |
| 637 | - * | |
| 638 | - * @since 2.0.0 | |
| 639 | - * | |
| 640 | - * @param array $meta_query Meta query array for WP_Query. | |
| 641 | - * @param string $search Optional. Search string for WP_Query. Default empty. | |
| 642 | - * @return int The number of matching posts. | |
| 643 | - */ | |
| 644 | - public function custom_wp_query_total_posts_with_search( $meta_query = [], $search = '' ) { | |
| 645 | - $args = [ | |
| 646 | - 'post_type' => SRFM_FORMS_POST_TYPE, | |
| 647 | - 'post_status' => 'publish', | |
| 648 | - 'posts_per_page' => -1, | |
| 649 | - ]; | |
| 650 | - | |
| 651 | - if ( ! empty( $meta_query ) && is_array( $meta_query ) ) { | |
| 652 | - // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Meta query required as we need to fetch count of nested data. | |
| 653 | - $args['meta_query'] = $meta_query; | |
| 654 | - } | |
| 655 | - | |
| 656 | - // If search string is provided, add it to the query. | |
| 657 | - if ( ! empty( $search ) ) { | |
| 658 | - $args['s'] = sanitize_text_field( $search ); | |
| 659 | - } | |
| 660 | - | |
| 661 | - $query = new \WP_Query( $args ); | |
| 662 | - $posts_count = $query->found_posts; | |
| 663 | - | |
| 664 | - wp_reset_postdata(); | |
| 665 | - | |
| 666 | - return $posts_count; | |
| 667 | - } | |
| 668 | - | |
| 669 | - /** | |
| 670 | - * Get the total number of forms that utilize payment blocks. | |
| 671 | - * | |
| 672 | - * This function searches for forms containing the payment block identifier | |
| 673 | - * ('wp:srfm/payment') to determine how many forms include payment capabilities. | |
| 674 | - * | |
| 675 | - * @since 2.0.0 | |
| 676 | - * @return int The number of forms that contain payment blocks. | |
| 677 | - */ | |
| 678 | - public function get_payment_forms_count() { | |
| 679 | - $search = 'wp:srfm/payment'; | |
| 680 | - // Runs a custom WP_Query to find the count of forms with payment block. | |
| 681 | - return $this->custom_wp_query_total_posts_with_search( [], $search ); | |
| 682 | - } | |
| 683 | - | |
| 684 | - /** | |
| 685 | - * Track first time a user opens the form editor. | |
| 686 | - * | |
| 687 | - * @since 2.5.1 | |
| 688 | - * @return void | |
| 689 | - */ | |
| 690 | - public function track_first_editor_open() { | |
| 691 | - $screen = get_current_screen(); | |
| 692 | - if ( $screen && 'sureforms_form' === $screen->id ) { | |
| 693 | - self::events()->track( 'first_form_editor_opened' ); | |
| 694 | - } | |
| 695 | - } | |
| 696 | - | |
| 697 | - /** | |
| 698 | - * Track first time a form is published (activation event). | |
| 699 | - * | |
| 700 | - * @param string $new_status New post status. | |
| 701 | - * @param string $old_status Old post status. | |
| 702 | - * @param \WP_Post $post Post object. | |
| 703 | - * @since 2.5.1 | |
| 704 | - * @return void | |
| 705 | - */ | |
| 706 | - public function track_first_form_published( $new_status, $old_status, $post ) { | |
| 707 | - if ( 'publish' !== $new_status || 'publish' === $old_status || SRFM_FORMS_POST_TYPE !== $post->post_type ) { | |
| 708 | - return; | |
| 709 | - } | |
| 710 | - | |
| 711 | - $is_ai = ! empty( get_post_meta( $post->ID, '_srfm_is_ai_generated', true ) ); | |
| 712 | - $block_count = substr_count( $post->post_content, '<!-- wp:srfm/' ); | |
| 713 | - | |
| 714 | - // Time-to-value: days between install and first form published. | |
| 715 | - $install_time = get_site_option( 'sureforms_usage_installed_time', 0 ); | |
| 716 | - $days_since_install = 0; | |
| 717 | - if ( $install_time > 0 ) { | |
| 718 | - $days_since_install = (int) floor( ( time() - $install_time ) / DAY_IN_SECONDS ); | |
| 719 | - } | |
| 720 | - | |
| 721 | - self::events()->track( | |
| 722 | - 'first_form_published', | |
| 723 | - (string) $post->ID, | |
| 724 | - [ | |
| 725 | - 'is_ai_generated' => (string) (int) $is_ai, | |
| 726 | - 'block_count' => (string) $block_count, | |
| 727 | - 'days_since_install' => (string) $days_since_install, | |
| 728 | - ] | |
| 729 | - ); | |
| 730 | - } | |
| 731 | - | |
| 732 | - /** | |
| 733 | - * Track embed styling configuration when a post/page is saved. | |
| 734 | - * | |
| 735 | - * Detects custom formTheme in Gutenberg blocks (post_content), | |
| 736 | - * Elementor widgets (_elementor_data meta), and Bricks elements | |
| 737 | - * (_bricks_page_content_2 meta). Re-tracks on each save by flushing | |
| 738 | - * the dedup flag. | |
| 739 | - * | |
| 740 | - * @param int $post_id Post ID. | |
| 741 | - * @param \WP_Post $post Post object. | |
| 742 | - * @since 2.7.0 | |
| 743 | - * @return void | |
| 744 | - */ | |
| 745 | - public function track_embed_styling_configured( $post_id, $post ) { | |
| 746 | - if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) { | |
| 747 | - return; | |
| 748 | - } | |
| 749 | - | |
| 750 | - if ( 'publish' !== $post->post_status ) { | |
| 751 | - return; | |
| 752 | - } | |
| 753 | - | |
| 754 | - $themes = []; | |
| 755 | - $source = ''; | |
| 756 | - | |
| 757 | - // Check Gutenberg blocks in post_content. | |
| 758 | - if ( preg_match_all( '/<!--\s*wp:srfm\/form\s+\{[^}]*"formTheme"\s*:\s*"(?!inherit")([^"]*)"/', $post->post_content, $matches ) ) { | |
| 759 | - $themes = array_count_values( $matches[1] ); | |
| 760 | - $source = 'gutenberg'; | |
| 761 | - } | |
| 762 | - | |
| 763 | - // Check Elementor widgets in _elementor_data meta. | |
| 764 | - if ( empty( $themes ) ) { | |
| 765 | - $elementor_data = get_post_meta( $post_id, '_elementor_data', true ); | |
| 766 | - if ( is_string( $elementor_data ) | |
| 767 | - && preg_match_all( '/"widgetType"\s*:\s*"sureforms_form"[^}]*"formTheme"\s*:\s*"(?!inherit")([^"]*)"/', $elementor_data, $el_matches ) | |
| 768 | - ) { | |
| 769 | - $themes = array_count_values( $el_matches[1] ); | |
| 770 | - $source = 'elementor'; | |
| 771 | - } | |
| 772 | - } | |
| 773 | - | |
| 774 | - // Check Bricks elements in _bricks_page_content_2 meta (serialized PHP). | |
| 775 | - if ( empty( $themes ) ) { | |
| 776 | - $bricks_data = get_post_meta( $post_id, '_bricks_page_content_2', true ); | |
| 777 | - if ( is_array( $bricks_data ) ) { | |
| 778 | - $bricks_themes = self::extract_bricks_form_themes( $bricks_data ); | |
| 779 | - if ( ! empty( $bricks_themes ) ) { | |
| 780 | - $themes = array_count_values( $bricks_themes ); | |
| 781 | - $source = 'bricks'; | |
| 782 | - } | |
| 783 | - } | |
| 784 | - } | |
| 785 | - | |
| 786 | - if ( empty( $themes ) ) { | |
| 787 | - return; | |
| 788 | - } | |
| 789 | - | |
| 790 | - // Flush dedup so event is re-tracked on each meaningful save. | |
| 791 | - self::events()->flush_pushed( [ 'embed_styling_configured' ] ); | |
| 792 | - | |
| 793 | - self::events()->track( | |
| 794 | - 'embed_styling_configured', | |
| 795 | - (string) $post_id, | |
| 796 | - [ | |
| 797 | - 'themes' => $themes, | |
| 798 | - 'block_count' => array_sum( $themes ), | |
| 799 | - 'source' => $source, | |
| 800 | - ] | |
| 801 | - ); | |
| 802 | - } | |
| 803 | - | |
| 804 | - /** | |
| 805 | - * Track the plugin_activated event with the correct install referer. | |
| 806 | - * | |
| 807 | - * Dedup in self::events()->track() ensures this fires only once. Runs on | |
| 808 | - * 'shutdown' (see detect_state_events()) so it reads bsf_product_referers | |
| 809 | - * after any late-writing referer call has had a chance to run. | |
| 810 | - * | |
| 811 | - * @since 2.12.6 | |
| 812 | - * @return void | |
| 813 | - */ | |
| 814 | - public function track_plugin_activated_event() { | |
| 815 | - $bsf_referrers = get_option( 'bsf_product_referers', [] ); | |
| 816 | - $source = ! empty( $bsf_referrers['sureforms'] ) ? $bsf_referrers['sureforms'] : 'self'; | |
| 817 | - self::events()->track( 'plugin_activated', SRFM_VER, [ 'source' => $source ] ); | |
| 818 | - } | |
| 819 | - | |
| 820 | - /** | |
| 821 | - * Extract non-inherit formTheme values from Bricks element data. | |
| 822 | - * | |
| 823 | - * Recursively walks the unserialized Bricks elements array looking for | |
| 824 | - * sureforms elements with custom formTheme settings. | |
| 825 | - * | |
| 826 | - * @param array<mixed> $elements Bricks elements array. | |
| 827 | - * @return array<string> List of formTheme values (non-inherit). | |
| 828 | - * @since 2.7.0 | |
| 829 | - */ | |
| 830 | - private static function extract_bricks_form_themes( $elements ) { | |
| 831 | - $themes = []; | |
| 832 | - | |
| 833 | - foreach ( $elements as $element ) { | |
| 834 | - if ( ! is_array( $element ) ) { | |
| 835 | - continue; | |
| 836 | - } | |
| 837 | - | |
| 838 | - $name = $element['name'] ?? ''; | |
| 839 | - $settings = $element['settings'] ?? []; | |
| 840 | - $form_theme = $settings['formTheme'] ?? ''; | |
| 841 | - | |
| 842 | - if ( 'sureforms' === $name && ! empty( $form_theme ) && 'inherit' !== $form_theme ) { | |
| 843 | - $themes[] = sanitize_text_field( $form_theme ); | |
| 844 | - } | |
| 845 | - | |
| 846 | - // Recurse into nested children. | |
| 847 | - if ( ! empty( $element['children'] ) && is_array( $element['children'] ) ) { | |
| 848 | - $themes = array_merge( $themes, self::extract_bricks_form_themes( $element['children'] ) ); | |
| 849 | - } | |
| 850 | - } | |
| 851 | - | |
| 852 | - return $themes; | |
| 853 | - } | |
| 854 | - | |
| 855 | - /** | |
| 856 | - * Check if any payment method is enabled. | |
| 857 | - * | |
| 858 | - * This function checks if any payment gateway is connected and enabled. | |
| 859 | - * Currently supports Stripe, but can be extended for other payment methods in the future. | |
| 860 | - * | |
| 861 | - * @since 2.0.0 | |
| 862 | - * @return bool True if any payment method is enabled, false otherwise. | |
| 863 | - */ | |
| 864 | - private function is_stripe_enabled() { | |
| 865 | - // Check if Stripe is connected. | |
| 866 | - return class_exists( 'SRFM\Inc\Payments\Stripe\Stripe_Helper' ) && \SRFM\Inc\Payments\Stripe\Stripe_Helper::is_stripe_connected(); | |
| 867 | - } | |
| 868 | - | |
| 869 | - /** | |
| 870 | 191 | * Runs custom WP_Query to fetch data as per requirement |
| 871 | 192 | * |
| 872 | 193 | * @param array $meta_query meta query array for WP_Query. |
| 873 | 194 | * @since 1.4.0 |
| @@ -888,267 +209,5 @@ | ||
| 888 | 209 | wp_reset_postdata(); |
| 889 | 210 | |
| 890 | 211 | return $posts_count; |
| 891 | 212 | } |
| 892 | - | |
| 893 | - /** | |
| 894 | - * Get KPI tracking data for the last 2 days (excluding today). | |
| 895 | - * | |
| 896 | - * @since 2.4.0 | |
| 897 | - * @return array KPI data organized by date. | |
| 898 | - */ | |
| 899 | - private function get_kpi_tracking_data() { | |
| 900 | - $kpi_data = []; | |
| 901 | - $today = current_time( 'Y-m-d' ); | |
| 902 | - | |
| 903 | - // Get data for yesterday and day before yesterday. | |
| 904 | - for ( $i = 1; $i <= 2; $i++ ) { | |
| 905 | - $date = gmdate( 'Y-m-d', strtotime( $today . ' -' . $i . ' days' ) ); | |
| 906 | - | |
| 907 | - $kpi_data[ $date ] = [ | |
| 908 | - 'numeric_values' => [ | |
| 909 | - 'submissions' => $this->get_daily_submissions_count( $date ), | |
| 910 | - ], | |
| 911 | - ]; | |
| 912 | - } | |
| 913 | - | |
| 914 | - return $kpi_data; | |
| 915 | - } | |
| 916 | - | |
| 917 | - /** | |
| 918 | - * Get daily submissions count for a specific date. | |
| 919 | - * | |
| 920 | - * @param string $date Date in Y-m-d format. | |
| 921 | - * @since 2.4.0 | |
| 922 | - * @return int Daily submissions count. | |
| 923 | - */ | |
| 924 | - private function get_daily_submissions_count( $date ) { | |
| 925 | - // Guarded here rather than at the call site: this is the function that runs | |
| 926 | - // the query, so a future caller is covered too. Every read against a missing | |
| 927 | - // entries table raises a DB error, and the daily send would raise one per | |
| 928 | - // day counted on exactly the sites whose breakage we most need reported. | |
| 929 | - if ( Register::is_entries_table_missing() ) { | |
| 930 | - return 0; | |
| 931 | - } | |
| 932 | - | |
| 933 | - $start_date = $date . ' 00:00:00'; | |
| 934 | - $end_date = $date . ' 23:59:59'; | |
| 935 | - | |
| 936 | - $where_conditions = [ | |
| 937 | - [ | |
| 938 | - [ | |
| 939 | - 'key' => 'created_at', | |
| 940 | - 'compare' => '>=', | |
| 941 | - 'value' => $start_date, | |
| 942 | - ], | |
| 943 | - [ | |
| 944 | - 'key' => 'created_at', | |
| 945 | - 'compare' => '<=', | |
| 946 | - 'value' => $end_date, | |
| 947 | - ], | |
| 948 | - ], | |
| 949 | - ]; | |
| 950 | - | |
| 951 | - return Entries::get_instance()->get_total_count( $where_conditions ); | |
| 952 | - } | |
| 953 | - | |
| 954 | - /** | |
| 955 | - * Detect state-based events that can't use direct hooks. | |
| 956 | - * Uses dedup in self::events()->track() — safe to call repeatedly. | |
| 957 | - * | |
| 958 | - * @since 2.5.1 | |
| 959 | - * @return void | |
| 960 | - */ | |
| 961 | - private function detect_state_events() { | |
| 962 | - // plugin_activated: deferred to 'shutdown' so that a referring plugin/theme's | |
| 963 | - // own BSF_UTM_Analytics::update_referer() call — which some products (incorrectly) | |
| 964 | - // make only after their activate_plugin() call returns, in the same request — has | |
| 965 | - // already run by the time we read bsf_product_referers. Reading this synchronously | |
| 966 | - // here would race that write, since this constructor can execute mid-request while | |
| 967 | - // SureForms itself is being activated by that other plugin. | |
| 968 | - if ( did_action( 'shutdown' ) ) { | |
| 969 | - $this->track_plugin_activated_event(); | |
| 970 | - } else { | |
| 971 | - add_action( 'shutdown', [ $this, 'track_plugin_activated_event' ], PHP_INT_MAX ); | |
| 972 | - } | |
| 973 | - | |
| 974 | - // One-time: re-send onboarding_completed with full properties (v2). | |
| 975 | - if ( ! Helper::get_srfm_option( 'onboarding_event_v2_flushed', false ) | |
| 976 | - && \SRFM\Inc\Onboarding::get_instance()->get_onboarding_status() ) { | |
| 977 | - self::events()->flush_pushed( [ 'onboarding_completed' ] ); | |
| 978 | - Helper::update_srfm_option( 'onboarding_event_v2_flushed', true ); | |
| 979 | - } | |
| 980 | - | |
| 981 | - // onboarding_completed: detect completed state with full onboarding details. | |
| 982 | - if ( \SRFM\Inc\Onboarding::get_instance()->get_onboarding_status() ) { | |
| 983 | - $onboarding_props = []; | |
| 984 | - $onboarding_analytics = Helper::get_srfm_option( 'onboarding_analytics', [] ); | |
| 985 | - | |
| 986 | - if ( ! empty( $onboarding_analytics ) && is_array( $onboarding_analytics ) ) { | |
| 987 | - if ( ! empty( $onboarding_analytics['skippedSteps'] ) && is_array( $onboarding_analytics['skippedSteps'] ) ) { | |
| 988 | - $onboarding_props['skipped_steps'] = implode( ',', $onboarding_analytics['skippedSteps'] ); | |
| 989 | - } | |
| 990 | - | |
| 991 | - if ( isset( $onboarding_analytics['suremailInstalled'] ) ) { | |
| 992 | - $onboarding_props['suremail_installed'] = (bool) $onboarding_analytics['suremailInstalled'] ? 'yes' : 'no'; | |
| 993 | - } | |
| 994 | - | |
| 995 | - if ( isset( $onboarding_analytics['accountConnected'] ) ) { | |
| 996 | - $onboarding_props['account_connected'] = (bool) $onboarding_analytics['accountConnected'] ? 'yes' : 'no'; | |
| 997 | - } | |
| 998 | - | |
| 999 | - if ( isset( $onboarding_analytics['completed'] ) ) { | |
| 1000 | - $onboarding_props['completed'] = (bool) $onboarding_analytics['completed'] ? 'yes' : 'no'; | |
| 1001 | - } | |
| 1002 | - | |
| 1003 | - if ( isset( $onboarding_analytics['exitedEarly'] ) ) { | |
| 1004 | - $onboarding_props['exited_early'] = (bool) $onboarding_analytics['exitedEarly'] ? 'yes' : 'no'; | |
| 1005 | - } | |
| 1006 | - | |
| 1007 | - if ( ! empty( $onboarding_analytics['premiumFeatures']['selectedFeatures'] ) && is_array( $onboarding_analytics['premiumFeatures']['selectedFeatures'] ) ) { | |
| 1008 | - $premium = array_filter( | |
| 1009 | - $onboarding_analytics['premiumFeatures']['selectedFeatures'], | |
| 1010 | - static function( $f ) { | |
| 1011 | - return 'ai-form-generation' !== $f && 'entries' !== $f; | |
| 1012 | - } | |
| 1013 | - ); | |
| 1014 | - $onboarding_props['selected_premium_features'] = implode( ',', $premium ); | |
| 1015 | - $onboarding_props['premium_features_count'] = (string) count( $premium ); | |
| 1016 | - } | |
| 1017 | - } | |
| 1018 | - | |
| 1019 | - self::events()->track( 'onboarding_completed', '', $onboarding_props ); | |
| 1020 | - } | |
| 1021 | - | |
| 1022 | - // stripe_connected: detect connection state. | |
| 1023 | - if ( class_exists( '\SRFM\Inc\Payments\Stripe\Stripe_Helper' ) | |
| 1024 | - && \SRFM\Inc\Payments\Stripe\Stripe_Helper::is_stripe_connected() ) { | |
| 1025 | - $mode = \SRFM\Inc\Payments\Stripe\Stripe_Helper::get_stripe_mode(); | |
| 1026 | - self::events()->track( 'stripe_connected', ! empty( $mode ) ? $mode : 'live' ); | |
| 1027 | - } | |
| 1028 | - | |
| 1029 | - // first_ai_form_generated: detect if any AI-generated form exists. | |
| 1030 | - // Guard with is_tracked() to skip the meta_query after the event is already tracked. | |
| 1031 | - if ( ! self::events()->is_tracked( 'first_ai_form_generated' ) ) { | |
| 1032 | - $ai_forms = get_posts( | |
| 1033 | - [ | |
| 1034 | - 'post_type' => SRFM_FORMS_POST_TYPE, | |
| 1035 | - 'post_status' => 'any', | |
| 1036 | - 'posts_per_page' => 1, | |
| 1037 | - 'fields' => 'ids', | |
| 1038 | - 'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Runs once per lifecycle via is_tracked guard. | |
| 1039 | - [ | |
| 1040 | - 'key' => '_srfm_is_ai_generated', | |
| 1041 | - 'value' => '', | |
| 1042 | - 'compare' => '!=', | |
| 1043 | - ], | |
| 1044 | - ], | |
| 1045 | - ] | |
| 1046 | - ); | |
| 1047 | - if ( ! empty( $ai_forms ) ) { | |
| 1048 | - self::events()->track( 'first_ai_form_generated' ); | |
| 1049 | - } | |
| 1050 | - } | |
| 1051 | - | |
| 1052 | - // MCP / Abilities API first-enable events. | |
| 1053 | - $mcp_settings = get_option( 'srfm_mcp_settings_options', [] ); | |
| 1054 | - | |
| 1055 | - if ( ! empty( $mcp_settings['srfm_abilities_api'] ) ) { | |
| 1056 | - self::events()->track( 'abilities_api_enabled' ); | |
| 1057 | - } | |
| 1058 | - | |
| 1059 | - if ( ! empty( $mcp_settings['srfm_mcp_server'] ) ) { | |
| 1060 | - self::events()->track( 'mcp_server_enabled' ); | |
| 1061 | - } | |
| 1062 | - } | |
| 1063 | - | |
| 1064 | - /** | |
| 1065 | - * Get learn section progress aggregated across all users. | |
| 1066 | - * | |
| 1067 | - * Queries all users with srfm_learn_progress meta, counts completions | |
| 1068 | - * per step. Called at analytics send time (bsf_core_stats filter). | |
| 1069 | - * | |
| 1070 | - * @since 2.8.0 | |
| 1071 | - * @return void | |
| 1072 | - */ | |
| 1073 | - private function get_learn_tracking_data() { | |
| 1074 | - // Only send when progress changed or event has never been tracked. | |
| 1075 | - $has_changed = get_transient( 'srfm_learn_progress_changed' ); | |
| 1076 | - if ( ! $has_changed && self::events()->is_tracked( 'learn' ) ) { | |
| 1077 | - return; | |
| 1078 | - } | |
| 1079 | - | |
| 1080 | - $chapters = Learn::get_chapters_structure(); | |
| 1081 | - $step_counts = []; | |
| 1082 | - $total_steps = 0; | |
| 1083 | - | |
| 1084 | - // Initialize step counters from canonical structure. | |
| 1085 | - foreach ( $chapters as $chapter ) { | |
| 1086 | - if ( ! isset( $chapter['id'], $chapter['steps'] ) || ! is_array( $chapter['steps'] ) ) { | |
| 1087 | - continue; | |
| 1088 | - } | |
| 1089 | - foreach ( $chapter['steps'] as $step ) { | |
| 1090 | - if ( ! isset( $step['id'] ) ) { | |
| 1091 | - continue; | |
| 1092 | - } | |
| 1093 | - $step_counts[ $chapter['id'] . '/' . $step['id'] ] = 0; | |
| 1094 | - ++$total_steps; | |
| 1095 | - } | |
| 1096 | - } | |
| 1097 | - | |
| 1098 | - // Query all users who have learn progress. | |
| 1099 | - $users = get_users( | |
| 1100 | - [ | |
| 1101 | - 'meta_key' => 'srfm_learn_progress', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Runs once per analytics send cycle; bounded by admin user count. | |
| 1102 | - 'fields' => 'ID', | |
| 1103 | - ] | |
| 1104 | - ); | |
| 1105 | - | |
| 1106 | - $users_with_progress = 0; | |
| 1107 | - $users_fully_completed = 0; | |
| 1108 | - $max_completed = 0; | |
| 1109 | - | |
| 1110 | - foreach ( $users as $user_id ) { | |
| 1111 | - $progress = get_user_meta( $user_id, 'srfm_learn_progress', true ); | |
| 1112 | - if ( ! is_array( $progress ) || empty( $progress ) ) { | |
| 1113 | - continue; | |
| 1114 | - } | |
| 1115 | - | |
| 1116 | - $user_completed = 0; | |
| 1117 | - | |
| 1118 | - foreach ( $step_counts as $key => $count ) { | |
| 1119 | - [ $chapter_id, $step_id ] = explode( '/', $key ); | |
| 1120 | - if ( ! empty( $progress[ $chapter_id ][ $step_id ] ) ) { | |
| 1121 | - $step_counts[ $key ] = $count + 1; | |
| 1122 | - ++$user_completed; | |
| 1123 | - } | |
| 1124 | - } | |
| 1125 | - | |
| 1126 | - if ( $user_completed > 0 ) { | |
| 1127 | - ++$users_with_progress; | |
| 1128 | - } | |
| 1129 | - if ( $user_completed === $total_steps ) { | |
| 1130 | - ++$users_fully_completed; | |
| 1131 | - } | |
| 1132 | - $max_completed = max( $max_completed, $user_completed ); | |
| 1133 | - } | |
| 1134 | - | |
| 1135 | - // Build flat properties — step keys use snake_case (hyphens → underscores). | |
| 1136 | - $properties = []; | |
| 1137 | - foreach ( $step_counts as $key => $count ) { | |
| 1138 | - $step_id = explode( '/', $key )[1]; | |
| 1139 | - $properties[ str_replace( '-', '_', $step_id ) ] = (string) $count; | |
| 1140 | - } | |
| 1141 | - | |
| 1142 | - $properties['users_with_progress'] = (string) $users_with_progress; | |
| 1143 | - $properties['users_fully_completed'] = (string) $users_fully_completed; | |
| 1144 | - $properties['total_steps'] = (string) $total_steps; | |
| 1145 | - | |
| 1146 | - // Flush dedup so event re-tracks with latest snapshot. | |
| 1147 | - self::events()->flush_pushed( [ 'learn' ] ); | |
| 1148 | - self::events()->track( 'learn', (string) $max_completed, $properties ); | |
| 1149 | - | |
| 1150 | - // Clear the change flag after tracking. | |
| 1151 | - delete_transient( 'srfm_learn_progress_changed' ); | |
| 1152 | - } | |
| 1153 | - | |
| 1154 | 213 | } |