| @@ -17,11 +17,14 @@ | ||
| 17 | 17 | if ( ! $this->tracking_allowed() ) { |
| 18 | 18 | return; |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | + $snapshot = $this->snapshot(); | |
| 22 | + $this->clean_before_send( $snapshot ); | |
| 23 | + | |
| 21 | 24 | $ep = 'aHR0cHM6Ly91c2FnZTIuZm9ybWlkYWJsZWZvcm1zLmNvbS9zbmFwc2hvdA=='; |
| 22 | 25 | // $ep = base64_encode( 'http://localhost:4567/snapshot' ); // Uncomment for testing |
| 23 | - $body = json_encode( $this->snapshot() ); | |
| 26 | + $body = json_encode( $snapshot ); | |
| 24 | 27 | |
| 25 | 28 | // Setup variable for wp_remote_request. |
| 26 | 29 | $post = array( |
| 27 | 30 | 'method' => 'POST', |
| @@ -44,8 +47,11 @@ | ||
| 44 | 47 | } |
| 45 | 48 | |
| 46 | 49 | /** |
| 47 | 50 | * @since 3.06.04 |
| 51 | + * | |
| 52 | + * @param bool $regenerate | |
| 53 | + * | |
| 48 | 54 | * @return string |
| 49 | 55 | */ |
| 50 | 56 | public function uuid( $regenerate = false ) { |
| 51 | 57 | $uuid_key = 'frm-usage-uuid'; |
| @@ -50,13 +56,13 @@ | ||
| 50 | 56 | public function uuid( $regenerate = false ) { |
| 51 | 57 | $uuid_key = 'frm-usage-uuid'; |
| 52 | 58 | $uuid = get_option( $uuid_key ); |
| 53 | 59 | |
| 54 | - if ( $regenerate || empty( $uuid ) ) { | |
| 60 | + if ( $regenerate || ! $uuid ) { | |
| 55 | 61 | // Definitely not cryptographically secure but |
| 56 | - // close enough to provide an unique id | |
| 62 | + // close enough to provide a unique id | |
| 57 | 63 | $uuid = md5( uniqid() . site_url() ); |
| 58 | - update_option( $uuid_key, $uuid, 'no' ); | |
| 64 | + update_option( $uuid_key, $uuid, false ); | |
| 59 | 65 | } |
| 60 | 66 | |
| 61 | 67 | return $uuid; |
| 62 | 68 | } |
| @@ -62,8 +68,9 @@ | ||
| 62 | 68 | } |
| 63 | 69 | |
| 64 | 70 | /** |
| 65 | 71 | * @since 3.06.04 |
| 72 | + * | |
| 66 | 73 | * @return array |
| 67 | 74 | */ |
| 68 | 75 | public function snapshot() { |
| 69 | 76 | global $wpdb, $wp_version; |
| @@ -72,10 +79,8 @@ | ||
| 72 | 79 | $form_counts = FrmForm::get_count(); |
| 73 | 80 | |
| 74 | 81 | $snap = array( |
| 75 | 82 | 'uuid' => $this->uuid(), |
| 76 | - // Let's keep it anonymous. | |
| 77 | - 'admin_email' => '', | |
| 78 | 83 | 'wp_version' => $wp_version, |
| 79 | 84 | 'php_version' => phpversion(), |
| 80 | 85 | 'mysql_version' => $wpdb->db_version(), |
| 81 | 86 | 'os' => FrmAppHelper::get_server_os(), |
| @@ -84,8 +89,9 @@ | ||
| 84 | 89 | 'active_license' => FrmAppHelper::pro_is_installed(), |
| 85 | 90 | 'form_count' => $form_counts->published, |
| 86 | 91 | 'entry_count' => FrmEntry::getRecordCount(), |
| 87 | 92 | 'timestamp' => gmdate( 'c' ), |
| 93 | + 'misc' => array(), | |
| 88 | 94 | |
| 89 | 95 | 'theme_name' => is_object( $theme_data ) ? $theme_data->Name : '', // phpcs:ignore WordPress.NamingConventions |
| 90 | 96 | 'plugins' => $this->plugins(), |
| 91 | 97 | 'settings' => array( |
| @@ -109,8 +115,125 @@ | ||
| 109 | 115 | return apply_filters( 'frm_usage_snapshot', $snap ); |
| 110 | 116 | } |
| 111 | 117 | |
| 112 | 118 | /** |
| 119 | + * Cleans the snapshot data before sending. | |
| 120 | + * | |
| 121 | + * @param array $data Snapshot data. | |
| 122 | + * | |
| 123 | + * @return void | |
| 124 | + */ | |
| 125 | + private function clean_before_send( &$data ) { | |
| 126 | + $skip_keys = array( | |
| 127 | + 'plan_id', | |
| 128 | + 'paypal_item_name', | |
| 129 | + 'account_num', | |
| 130 | + 'routing_num', | |
| 131 | + 'bank_name', | |
| 132 | + 'business_email', | |
| 133 | + | |
| 134 | + // Form action settings. | |
| 135 | + 'quiz', | |
| 136 | + 'email_to', | |
| 137 | + 'cc', | |
| 138 | + 'bcc', | |
| 139 | + 'from', | |
| 140 | + 'email_subject', | |
| 141 | + 'email_msg', | |
| 142 | + 'email_message', | |
| 143 | + 'success_msg', | |
| 144 | + 'success_url', | |
| 145 | + 'success_page_id', | |
| 146 | + 'redirect_msg', | |
| 147 | + 'conditions', | |
| 148 | + 'list_id', | |
| 149 | + 'api_key', | |
| 150 | + 'url', | |
| 151 | + 'data_fields', | |
| 152 | + 'reg_email_msg', | |
| 153 | + | |
| 154 | + // Form settings. | |
| 155 | + 'form_id', | |
| 156 | + 'description', | |
| 157 | + 'submit_conditions-hide_field', | |
| 158 | + 'submit_conditions-hide_field_cond', | |
| 159 | + 'submit_conditions-hide_opt', | |
| 160 | + 'submit_conditions-show_hide', | |
| 161 | + 'submit_conditions-any_all', | |
| 162 | + | |
| 163 | + // Field settings. | |
| 164 | + 'show_hide', | |
| 165 | + 'any_all', | |
| 166 | + 'hide_field', | |
| 167 | + 'hide_field_cond', | |
| 168 | + 'hide_opt', | |
| 169 | + 'custom_html', | |
| 170 | + 'blank', | |
| 171 | + 'required_indicator', | |
| 172 | + 'invalid', | |
| 173 | + 'unique_msg', | |
| 174 | + 'edit_text', | |
| 175 | + 'start_over_label', | |
| 176 | + 'add_label', | |
| 177 | + 'remove_label', | |
| 178 | + 'draft', | |
| 179 | + ); | |
| 180 | + | |
| 181 | + /** | |
| 182 | + * Filter the keys to skip when cleaning the snapshot data before sending. | |
| 183 | + * | |
| 184 | + * @since 6.28 | |
| 185 | + * | |
| 186 | + * @param array $skip_keys Data keys. | |
| 187 | + */ | |
| 188 | + $skip_keys = apply_filters( 'frm_usage_skip_keys', $skip_keys ); | |
| 189 | + | |
| 190 | + $long_text_keys = array( 'description' ); | |
| 191 | + | |
| 192 | + /** | |
| 193 | + * Filter the keys to replace with a long text placeholder before sending. | |
| 194 | + * | |
| 195 | + * @since 6.28 | |
| 196 | + * | |
| 197 | + * @paramm array $long_text_keys Data keys. | |
| 198 | + */ | |
| 199 | + $long_text_keys = apply_filters( 'frm_usage_long_text_keys', $long_text_keys ); | |
| 200 | + | |
| 201 | + foreach ( $data as $key => &$value ) { | |
| 202 | + if ( ! $value ) { | |
| 203 | + continue; | |
| 204 | + } | |
| 205 | + | |
| 206 | + if ( in_array( $key, $skip_keys, true ) || str_ends_with( $key, '_url' ) ) { | |
| 207 | + unset( $data[ $key ] ); | |
| 208 | + continue; | |
| 209 | + } | |
| 210 | + | |
| 211 | + if ( is_object( $value ) ) { | |
| 212 | + $value = '{{object}}'; | |
| 213 | + continue; | |
| 214 | + } | |
| 215 | + | |
| 216 | + if ( is_array( $value ) ) { | |
| 217 | + $this->clean_before_send( $value ); | |
| 218 | + continue; | |
| 219 | + } | |
| 220 | + | |
| 221 | + if ( in_array( $key, $long_text_keys, true ) || str_ends_with( $key, '_msg' ) ) { | |
| 222 | + // Replace it with a placeholder. | |
| 223 | + $value = '{{long_text}}'; | |
| 224 | + continue; | |
| 225 | + } | |
| 226 | + | |
| 227 | + // If key ends with `_email`, or value contains `@`, this might contain an email address. | |
| 228 | + if ( str_ends_with( $key, '_email' ) || ( is_string( $value ) && str_contains( $value, '@' ) ) ) { | |
| 229 | + // Replace it with a placeholder. | |
| 230 | + $value = '{{contain_email}}'; | |
| 231 | + } | |
| 232 | + }//end foreach | |
| 233 | + } | |
| 234 | + | |
| 235 | + /** | |
| 113 | 236 | * Gets onboarding wizard data. |
| 114 | 237 | * |
| 115 | 238 | * @since 6.16.1 |
| 116 | 239 | * |
| @@ -138,29 +261,21 @@ | ||
| 138 | 261 | * |
| 139 | 262 | * @since 6.16.1 |
| 140 | 263 | * |
| 141 | 264 | * @param string $table Database table name. |
| 265 | + * | |
| 142 | 266 | * @return array |
| 143 | 267 | */ |
| 144 | 268 | private function payments( $table = 'frm_payments' ) { |
| 145 | 269 | $allowed_tables = array( 'frm_payments', 'frm_subscriptions' ); |
| 146 | - if ( ! in_array( $table, $allowed_tables, true ) ) { | |
| 147 | - return array(); | |
| 148 | - } | |
| 149 | 270 | |
| 150 | - if ( ! FrmTransLiteAppHelper::payments_table_exists() ) { | |
| 271 | + if ( ! in_array( $table, $allowed_tables, true ) || ! FrmTransLiteAppHelper::payments_table_exists() ) { | |
| 151 | 272 | return array(); |
| 152 | 273 | } |
| 153 | 274 | |
| 154 | - global $wpdb; | |
| 155 | - $rows = $wpdb->get_results( | |
| 156 | - $wpdb->prepare( | |
| 157 | - 'SELECT amount, status, paysys, created_at FROM %1$s', | |
| 158 | - $wpdb->prefix . $table | |
| 159 | - ) | |
| 160 | - ); | |
| 275 | + $rows = $this->get_payment_db_rows( $table ); | |
| 276 | + $payments = array(); | |
| 161 | 277 | |
| 162 | - $payments = array(); | |
| 163 | 278 | foreach ( $rows as $row ) { |
| 164 | 279 | $payments[] = array( |
| 165 | 280 | 'amount' => (float) $row->amount, |
| 166 | 281 | 'status' => $row->status, |
| @@ -172,15 +287,44 @@ | ||
| 172 | 287 | return $payments; |
| 173 | 288 | } |
| 174 | 289 | |
| 175 | 290 | /** |
| 291 | + * @since 6.30 | |
| 292 | + * | |
| 293 | + * @param string $table | |
| 294 | + * | |
| 295 | + * @return array | |
| 296 | + */ | |
| 297 | + private function get_payment_db_rows( $table ) { | |
| 298 | + global $wpdb; | |
| 299 | + | |
| 300 | + if ( FrmDb::db_column_exists( $table, 'test' ) ) { | |
| 301 | + return $wpdb->get_results( | |
| 302 | + $wpdb->prepare( | |
| 303 | + 'SELECT amount, status, paysys, created_at FROM %i WHERE test IS NULL OR test != 1', | |
| 304 | + $wpdb->prefix . $table | |
| 305 | + ) | |
| 306 | + ); | |
| 307 | + } | |
| 308 | + | |
| 309 | + // Fallback for PayPal add-on where this column does not exist. | |
| 310 | + return $wpdb->get_results( | |
| 311 | + $wpdb->prepare( | |
| 312 | + 'SELECT amount, status, paysys, created_at FROM %i', | |
| 313 | + $wpdb->prefix . $table | |
| 314 | + ) | |
| 315 | + ); | |
| 316 | + } | |
| 317 | + | |
| 318 | + /** | |
| 176 | 319 | * @since 3.06.04 |
| 320 | + * | |
| 177 | 321 | * @return array |
| 178 | 322 | */ |
| 179 | 323 | private function plugins() { |
| 180 | 324 | $plugin_list = FrmAppHelper::get_plugins(); |
| 325 | + $plugins = array(); | |
| 181 | 326 | |
| 182 | - $plugins = array(); | |
| 183 | 327 | foreach ( $plugin_list as $slug => $info ) { |
| 184 | 328 | $plugins[] = array( |
| 185 | 329 | 'name' => $info['Name'], |
| 186 | 330 | 'slug' => $slug, |
| @@ -195,8 +339,9 @@ | ||
| 195 | 339 | /** |
| 196 | 340 | * Add global settings to tracking data. |
| 197 | 341 | * |
| 198 | 342 | * @since 3.06.04 |
| 343 | + * | |
| 199 | 344 | * @return array |
| 200 | 345 | */ |
| 201 | 346 | private function settings() { |
| 202 | 347 | $settings_list = FrmAppHelper::get_settings(); |
| @@ -246,8 +391,9 @@ | ||
| 246 | 391 | * |
| 247 | 392 | * @since 3.06.04 |
| 248 | 393 | * |
| 249 | 394 | * @param FrmSettings $settings_list |
| 395 | + * | |
| 250 | 396 | * @return array |
| 251 | 397 | */ |
| 252 | 398 | private function messages( $settings_list ) { |
| 253 | 399 | $messages = array( |
| @@ -260,11 +406,11 @@ | ||
| 260 | 406 | 'login_msg', |
| 261 | 407 | 'admin_permission', |
| 262 | 408 | ); |
| 263 | 409 | |
| 264 | - $default = $settings_list->default_options(); | |
| 410 | + $default = $settings_list->default_options(); | |
| 411 | + $message_settings = array(); | |
| 265 | 412 | |
| 266 | - $message_settings = array(); | |
| 267 | 413 | foreach ( $messages as $message ) { |
| 268 | 414 | $message_settings[ 'changed-' . $message ] = $settings_list->$message === $default[ $message ] ? 0 : 1; |
| 269 | 415 | } |
| 270 | 416 | |
| @@ -276,8 +422,9 @@ | ||
| 276 | 422 | * |
| 277 | 423 | * @since 3.06.04 |
| 278 | 424 | * |
| 279 | 425 | * @param FrmSettings $settings_list |
| 426 | + * | |
| 280 | 427 | * @return array |
| 281 | 428 | */ |
| 282 | 429 | private function permissions( $settings_list ) { |
| 283 | 430 | $permissions = array(); |
| @@ -293,8 +440,9 @@ | ||
| 293 | 440 | } |
| 294 | 441 | |
| 295 | 442 | /** |
| 296 | 443 | * @since 3.06.04 |
| 444 | + * | |
| 297 | 445 | * @return array |
| 298 | 446 | */ |
| 299 | 447 | private function forms() { |
| 300 | 448 | $s_query = array( |
| @@ -342,8 +490,9 @@ | ||
| 342 | 490 | 'submit_conditions', |
| 343 | 491 | ); |
| 344 | 492 | |
| 345 | 493 | $style = new FrmStyle(); |
| 494 | + | |
| 346 | 495 | foreach ( $saved_forms as $form ) { |
| 347 | 496 | $new_form = array( |
| 348 | 497 | 'form_id' => $form->id, |
| 349 | 498 | 'description' => $form->description, |
| @@ -355,28 +504,31 @@ | ||
| 355 | 504 | 'form_action_count' => $this->form_action_count( $form->id ), |
| 356 | 505 | ); |
| 357 | 506 | |
| 358 | 507 | foreach ( $settings as $setting ) { |
| 359 | - if ( isset( $form->options[ $setting ] ) ) { | |
| 360 | - if ( 'custom_style' === $setting ) { | |
| 361 | - $style->id = $form->options[ $setting ]; | |
| 508 | + if ( ! isset( $form->options[ $setting ] ) ) { | |
| 509 | + continue; | |
| 510 | + } | |
| 362 | 511 | |
| 363 | - if ( ! $style->id ) { | |
| 364 | - $style_name = 0; | |
| 365 | - } elseif ( 1 === intval( $style->id ) ) { | |
| 366 | - $style_name = 'formidable-style'; | |
| 367 | - } else { | |
| 368 | - $style_post = $style->get_one(); | |
| 369 | - $style_name = $style_post ? $style_post->post_name : 'formidable-style'; | |
| 370 | - } | |
| 512 | + if ( 'custom_style' !== $setting ) { | |
| 513 | + $new_form[ $setting ] = $this->maybe_json( $form->options[ $setting ] ); | |
| 514 | + continue; | |
| 515 | + } | |
| 371 | 516 | |
| 372 | - $new_form[ $setting ] = $style_name; | |
| 373 | - } else { | |
| 374 | - $new_form[ $setting ] = $this->maybe_json( $form->options[ $setting ] ); | |
| 375 | - } | |
| 517 | + $style->id = $form->options[ $setting ]; | |
| 518 | + | |
| 519 | + if ( ! $style->id ) { | |
| 520 | + $style_name = 0; | |
| 521 | + } elseif ( 1 === intval( $style->id ) ) { | |
| 522 | + $style_name = 'formidable-style'; | |
| 523 | + } else { | |
| 524 | + $style_post = $style->get_one(); | |
| 525 | + $style_name = $style_post ? $style_post->post_name : 'formidable-style'; | |
| 376 | 526 | } |
| 377 | - } | |
| 378 | 527 | |
| 528 | + $new_form[ $setting ] = $style_name; | |
| 529 | + }//end foreach | |
| 530 | + | |
| 379 | 531 | $forms[] = apply_filters( 'frm_usage_form', $new_form, compact( 'form' ) ); |
| 380 | 532 | }//end foreach |
| 381 | 533 | |
| 382 | 534 | // If the array uses numeric keys, reset them. |
| @@ -384,8 +536,11 @@ | ||
| 384 | 536 | } |
| 385 | 537 | |
| 386 | 538 | /** |
| 387 | 539 | * @since 3.06.04 |
| 540 | + * | |
| 541 | + * @param int|string $form_id Form ID. | |
| 542 | + * | |
| 388 | 543 | * @return int |
| 389 | 544 | */ |
| 390 | 545 | private function form_field_count( $form_id ) { |
| 391 | 546 | global $wpdb; |
| @@ -402,8 +557,11 @@ | ||
| 402 | 557 | } |
| 403 | 558 | |
| 404 | 559 | /** |
| 405 | 560 | * @since 3.06.04 |
| 561 | + * | |
| 562 | + * @param int|string $form_id Form ID. | |
| 563 | + * | |
| 406 | 564 | * @return int |
| 407 | 565 | */ |
| 408 | 566 | private function form_action_count( $form_id ) { |
| 409 | 567 | $args = array( |
| @@ -419,8 +577,9 @@ | ||
| 419 | 577 | /** |
| 420 | 578 | * Get the last 100 fields created. |
| 421 | 579 | * |
| 422 | 580 | * @since 3.06.04 |
| 581 | + * | |
| 423 | 582 | * @return array |
| 424 | 583 | */ |
| 425 | 584 | private function fields() { |
| 426 | 585 | $args = array( |
| @@ -428,42 +587,65 @@ | ||
| 428 | 587 | 'order_by' => 'id DESC', |
| 429 | 588 | ); |
| 430 | 589 | |
| 431 | 590 | $fields = FrmDb::get_results( 'frm_fields', array(), 'id, form_id, name, type, field_options', $args ); |
| 591 | + | |
| 432 | 592 | foreach ( $fields as $k => $field ) { |
| 433 | 593 | FrmAppHelper::unserialize_or_decode( $field->field_options ); |
| 434 | - $fields[ $k ]->field_options = json_encode( $field->field_options ); | |
| 594 | + $fields[ $k ]->field_options = $field->field_options; | |
| 435 | 595 | } |
| 596 | + | |
| 436 | 597 | return $fields; |
| 437 | 598 | } |
| 438 | 599 | |
| 439 | 600 | /** |
| 440 | 601 | * @since 3.06.04 |
| 602 | + * | |
| 441 | 603 | * @return array |
| 442 | 604 | */ |
| 443 | 605 | private function actions() { |
| 444 | - $args = array( | |
| 445 | - 'post_type' => FrmFormActionsController::$action_post_type, | |
| 446 | - 'numberposts' => 100, | |
| 447 | - ); | |
| 606 | + $actions = array(); | |
| 607 | + $offset = 0; | |
| 608 | + $limit = 100; | |
| 609 | + $limit_plus_one = $limit + 1; | |
| 448 | 610 | |
| 449 | - $actions = array(); | |
| 611 | + do { | |
| 612 | + $args = array( | |
| 613 | + 'post_type' => FrmFormActionsController::$action_post_type, | |
| 614 | + 'numberposts' => $limit_plus_one, | |
| 615 | + 'offset' => $offset, | |
| 616 | + ); | |
| 617 | + $saved_actions = get_posts( $args ); | |
| 618 | + $has_more = count( $saved_actions ) === $limit_plus_one; | |
| 450 | 619 | |
| 451 | - $saved_actions = FrmDb::check_cache( json_encode( $args ), 'frm_actions', $args, 'get_posts' ); | |
| 452 | - foreach ( $saved_actions as $action ) { | |
| 453 | - $actions[] = array( | |
| 454 | - 'form_id' => $action->menu_order, | |
| 455 | - 'type' => $action->post_excerpt, | |
| 456 | - 'status' => $action->post_status, | |
| 457 | - 'settings' => $action->post_content, | |
| 458 | - ); | |
| 459 | - } | |
| 620 | + if ( $has_more ) { | |
| 621 | + // Pop off the last item as it is only queried to determine if there are more results. | |
| 622 | + array_pop( $saved_actions ); | |
| 623 | + } | |
| 460 | 624 | |
| 625 | + foreach ( $saved_actions as $action ) { | |
| 626 | + $actions[] = array( | |
| 627 | + 'form_id' => $action->menu_order, | |
| 628 | + 'type' => $action->post_excerpt, | |
| 629 | + 'status' => $action->post_status, | |
| 630 | + 'settings' => FrmAppHelper::maybe_json_decode( $action->post_content ), | |
| 631 | + ); | |
| 632 | + unset( $action ); | |
| 633 | + } | |
| 634 | + | |
| 635 | + $offset += $limit; | |
| 636 | + | |
| 637 | + unset( $saved_actions ); | |
| 638 | + | |
| 639 | + gc_collect_cycles(); | |
| 640 | + } while ( $has_more ); | |
| 641 | + | |
| 461 | 642 | return $actions; |
| 462 | 643 | } |
| 463 | 644 | |
| 464 | 645 | /** |
| 465 | 646 | * @since 3.06.04 |
| 647 | + * | |
| 466 | 648 | * @return bool |
| 467 | 649 | */ |
| 468 | 650 | private function tracking_allowed() { |
| 469 | 651 | return FrmUsageController::tracking_allowed(); |
| @@ -470,8 +652,11 @@ | ||
| 470 | 652 | } |
| 471 | 653 | |
| 472 | 654 | /** |
| 473 | 655 | * @since 3.06.04 |
| 656 | + * | |
| 657 | + * @param mixed $value Value. | |
| 658 | + * | |
| 474 | 659 | * @return string |
| 475 | 660 | */ |
| 476 | 661 | private function maybe_json( $value ) { |
| 477 | 662 | return is_array( $value ) ? json_encode( $value ) : $value; |