| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* @since 3.06.04 |
| 8 |
*/ |
| 9 |
class FrmUsage { |
| 10 |
|
| 11 |
/** |
| 12 |
* @since 3.06.04 |
| 13 |
* |
| 14 |
* @return void |
| 15 |
*/ |
| 16 |
public function send_snapshot() { |
| 17 |
if ( ! $this->tracking_allowed() ) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
$ep = 'aHR0cHM6Ly91c2FnZTIuZm9ybWlkYWJsZWZvcm1zLmNvbS9zbmFwc2hvdA=='; |
| 22 |
// $ep = base64_encode( 'http://localhost:4567/snapshot' ); // Uncomment for testing |
| 23 |
$body = json_encode( $this->snapshot() ); |
| 24 |
|
| 25 |
// Setup variable for wp_remote_request. |
| 26 |
$post = array( |
| 27 |
'method' => 'POST', |
| 28 |
'headers' => array( |
| 29 |
'Accept' => 'application/json', |
| 30 |
'Content-Type' => 'application/json', |
| 31 |
'Content-Length' => strlen( $body ), |
| 32 |
), |
| 33 |
'body' => $body, |
| 34 |
// Without this, Debug Log catches the `http_request_failed` error. |
| 35 |
'timeout' => 45, |
| 36 |
); |
| 37 |
|
| 38 |
// Remove time limit to execute this function. |
| 39 |
if ( function_exists( 'set_time_limit' ) ) { |
| 40 |
set_time_limit( 0 ); |
| 41 |
} |
| 42 |
|
| 43 |
wp_remote_request( base64_decode( $ep ), $post ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @since 3.06.04 |
| 48 |
* @return string |
| 49 |
*/ |
| 50 |
public function uuid( $regenerate = false ) { |
| 51 |
$uuid_key = 'frm-usage-uuid'; |
| 52 |
$uuid = get_option( $uuid_key ); |
| 53 |
|
| 54 |
if ( $regenerate || empty( $uuid ) ) { |
| 55 |
// Definitely not cryptographically secure but |
| 56 |
// close enough to provide an unique id |
| 57 |
$uuid = md5( uniqid() . site_url() ); |
| 58 |
update_option( $uuid_key, $uuid, 'no' ); |
| 59 |
} |
| 60 |
|
| 61 |
return $uuid; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @since 3.06.04 |
| 66 |
* @return array |
| 67 |
*/ |
| 68 |
public function snapshot() { |
| 69 |
global $wpdb, $wp_version; |
| 70 |
|
| 71 |
$theme_data = wp_get_theme(); |
| 72 |
$form_counts = FrmForm::get_count(); |
| 73 |
|
| 74 |
$snap = array( |
| 75 |
'uuid' => $this->uuid(), |
| 76 |
// Let's keep it anonymous. |
| 77 |
'admin_email' => '', |
| 78 |
'wp_version' => $wp_version, |
| 79 |
'php_version' => phpversion(), |
| 80 |
'mysql_version' => $wpdb->db_version(), |
| 81 |
'os' => FrmAppHelper::get_server_os(), |
| 82 |
'locale' => get_locale(), |
| 83 |
|
| 84 |
'active_license' => FrmAppHelper::pro_is_installed(), |
| 85 |
'form_count' => $form_counts->published, |
| 86 |
'entry_count' => FrmEntry::getRecordCount(), |
| 87 |
'timestamp' => gmdate( 'c' ), |
| 88 |
|
| 89 |
'theme_name' => is_object( $theme_data ) ? $theme_data->Name : '', // phpcs:ignore WordPress.NamingConventions |
| 90 |
'plugins' => $this->plugins(), |
| 91 |
'settings' => array( |
| 92 |
$this->settings(), |
| 93 |
), |
| 94 |
'forms' => $this->forms(), |
| 95 |
'fields' => $this->fields(), |
| 96 |
'actions' => $this->actions(), |
| 97 |
|
| 98 |
'onboarding-wizard' => $this->onboarding_wizard(), |
| 99 |
'flows' => FrmUsageController::get_flows_data(), |
| 100 |
'payments' => $this->payments(), |
| 101 |
'subscriptions' => $this->payments( 'frm_subscriptions' ), |
| 102 |
); |
| 103 |
|
| 104 |
if ( method_exists( 'FrmProAddonsController', 'get_readable_license_type' ) ) { |
| 105 |
$snap['active_license'] = FrmProAddonsController::get_readable_license_type(); |
| 106 |
} |
| 107 |
|
| 108 |
return apply_filters( 'frm_usage_snapshot', $snap ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Gets onboarding wizard data. |
| 113 |
* |
| 114 |
* @since 6.16.1 |
| 115 |
* |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
private function onboarding_wizard() { |
| 119 |
$data = FrmOnboardingWizardController::get_usage_data(); |
| 120 |
$skipped_keys = array( |
| 121 |
'default_email', |
| 122 |
'is_subscribed', |
| 123 |
'allows_tracking', |
| 124 |
'summary_emails', |
| 125 |
'installed_addons', |
| 126 |
); |
| 127 |
|
| 128 |
foreach ( $skipped_keys as $skipped_key ) { |
| 129 |
unset( $data[ $skipped_key ] ); |
| 130 |
} |
| 131 |
|
| 132 |
return $data; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Gets payments data. |
| 137 |
* |
| 138 |
* @since 6.16.1 |
| 139 |
* |
| 140 |
* @param string $table Database table name. |
| 141 |
* @return array |
| 142 |
*/ |
| 143 |
private function payments( $table = 'frm_payments' ) { |
| 144 |
$allowed_tables = array( 'frm_payments', 'frm_subscriptions' ); |
| 145 |
if ( ! in_array( $table, $allowed_tables, true ) ) { |
| 146 |
return array(); |
| 147 |
} |
| 148 |
|
| 149 |
if ( ! FrmTransLiteAppHelper::payments_table_exists() ) { |
| 150 |
return array(); |
| 151 |
} |
| 152 |
|
| 153 |
global $wpdb; |
| 154 |
$rows = $wpdb->get_results( |
| 155 |
$wpdb->prepare( |
| 156 |
'SELECT amount, status, paysys, created_at FROM %1$s', |
| 157 |
$wpdb->prefix . $table |
| 158 |
) |
| 159 |
); |
| 160 |
|
| 161 |
$payments = array(); |
| 162 |
foreach ( $rows as $row ) { |
| 163 |
$payments[] = array( |
| 164 |
'amount' => (float) $row->amount, |
| 165 |
'status' => $row->status, |
| 166 |
'gateway' => $row->paysys, |
| 167 |
'created_at' => $row->created_at, |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
return $payments; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* @since 3.06.04 |
| 176 |
* @return array |
| 177 |
*/ |
| 178 |
private function plugins() { |
| 179 |
$plugin_list = FrmAppHelper::get_plugins(); |
| 180 |
|
| 181 |
$plugins = array(); |
| 182 |
foreach ( $plugin_list as $slug => $info ) { |
| 183 |
$plugins[] = array( |
| 184 |
'name' => $info['Name'], |
| 185 |
'slug' => $slug, |
| 186 |
'version' => $info['Version'], |
| 187 |
'active' => is_plugin_active( $slug ), |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
return $plugins; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Add global settings to tracking data. |
| 196 |
* |
| 197 |
* @since 3.06.04 |
| 198 |
* @return array |
| 199 |
*/ |
| 200 |
private function settings() { |
| 201 |
$settings_list = FrmAppHelper::get_settings(); |
| 202 |
$settings = array( |
| 203 |
'messages' => $this->messages( $settings_list ), |
| 204 |
'permissions' => $this->permissions( $settings_list ), |
| 205 |
); |
| 206 |
$pass_settings = array( |
| 207 |
'load_style', |
| 208 |
'fade_form', |
| 209 |
're_type', |
| 210 |
're_lang', |
| 211 |
're_multi', |
| 212 |
'menu', |
| 213 |
'mu_menu', |
| 214 |
'no_ips', |
| 215 |
'custom_header_ip', |
| 216 |
'btsp_css', |
| 217 |
'btsp_version', |
| 218 |
'admin_bar', |
| 219 |
'summary_emails', |
| 220 |
'active_captcha', |
| 221 |
); |
| 222 |
|
| 223 |
foreach ( $pass_settings as $setting ) { |
| 224 |
if ( isset( $settings_list->$setting ) ) { |
| 225 |
$settings[ $setting ] = $this->maybe_json( $settings_list->$setting ); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
$settings = apply_filters( 'frm_usage_settings', $settings ); |
| 230 |
|
| 231 |
$settings['messages'] = $this->maybe_json( $settings['messages'] ); |
| 232 |
$settings['permissions'] = $this->maybe_json( $settings['permissions'] ); |
| 233 |
|
| 234 |
return $settings; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Include the permissions settings for each capability. |
| 239 |
* |
| 240 |
* @since 3.06.04 |
| 241 |
* |
| 242 |
* @param FrmSettings $settings_list |
| 243 |
* @return array |
| 244 |
*/ |
| 245 |
private function messages( $settings_list ) { |
| 246 |
$messages = array( |
| 247 |
'success_msg', |
| 248 |
'blank_msg', |
| 249 |
'unique_msg', |
| 250 |
'invalid_msg', |
| 251 |
'failed_msg', |
| 252 |
'submit_value', |
| 253 |
'login_msg', |
| 254 |
'admin_permission', |
| 255 |
); |
| 256 |
|
| 257 |
$default = $settings_list->default_options(); |
| 258 |
|
| 259 |
$message_settings = array(); |
| 260 |
foreach ( $messages as $message ) { |
| 261 |
$message_settings[ 'changed-' . $message ] = $settings_list->$message === $default[ $message ] ? 0 : 1; |
| 262 |
} |
| 263 |
|
| 264 |
return $message_settings; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Include the permissions settings for each capability. |
| 269 |
* |
| 270 |
* @since 3.06.04 |
| 271 |
* |
| 272 |
* @param FrmSettings $settings_list |
| 273 |
* @return array |
| 274 |
*/ |
| 275 |
private function permissions( $settings_list ) { |
| 276 |
$permissions = array(); |
| 277 |
$frm_roles = FrmAppHelper::frm_capabilities(); |
| 278 |
|
| 279 |
foreach ( $frm_roles as $frm_role => $frm_role_description ) { |
| 280 |
if ( isset( $settings_list->$frm_role ) ) { |
| 281 |
$permissions[ $frm_role ] = $settings_list->$frm_role; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
return $permissions; |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* @since 3.06.04 |
| 290 |
* @return array |
| 291 |
*/ |
| 292 |
private function forms() { |
| 293 |
$s_query = array( |
| 294 |
array( |
| 295 |
'or' => 1, |
| 296 |
'parent_form_id' => null, |
| 297 |
'parent_form_id <' => 1, |
| 298 |
), |
| 299 |
); |
| 300 |
|
| 301 |
$saved_forms = FrmForm::getAll( $s_query ); |
| 302 |
$forms = array(); |
| 303 |
$settings = array( |
| 304 |
'form_class', |
| 305 |
'akismet', |
| 306 |
'honeypot', |
| 307 |
'antispam', |
| 308 |
'custom_style', |
| 309 |
'success_action', |
| 310 |
'show_form', |
| 311 |
'no_save', |
| 312 |
'ajax_load', |
| 313 |
'ajax_submit', |
| 314 |
'js_validate', |
| 315 |
'logged_in_role', |
| 316 |
'single_entry', |
| 317 |
'single_entry_type', |
| 318 |
'editable_role', |
| 319 |
'open_editable_role', |
| 320 |
'edit_action', |
| 321 |
'edit_value', |
| 322 |
'edit_msg', |
| 323 |
'save_draft', |
| 324 |
'draft_msg', |
| 325 |
'submit_align', |
| 326 |
'protect_files', |
| 327 |
'protect_files_role', |
| 328 |
'max_entries', |
| 329 |
'open_status', |
| 330 |
'closed_msg', |
| 331 |
'open_date', |
| 332 |
'close_date', |
| 333 |
'copy', |
| 334 |
'prev_value', |
| 335 |
'submit_conditions', |
| 336 |
); |
| 337 |
|
| 338 |
$style = new FrmStyle(); |
| 339 |
foreach ( $saved_forms as $form ) { |
| 340 |
$new_form = array( |
| 341 |
'form_id' => $form->id, |
| 342 |
'description' => $form->description, |
| 343 |
'logged_in' => $form->logged_in, |
| 344 |
'editable' => $form->editable, |
| 345 |
'is_template' => $form->is_template, |
| 346 |
'entry_count' => FrmEntry::getRecordCount( $form->id ), |
| 347 |
'field_count' => $this->form_field_count( $form->id ), |
| 348 |
'form_action_count' => $this->form_action_count( $form->id ), |
| 349 |
); |
| 350 |
|
| 351 |
foreach ( $settings as $setting ) { |
| 352 |
if ( isset( $form->options[ $setting ] ) ) { |
| 353 |
if ( 'custom_style' === $setting ) { |
| 354 |
$style->id = $form->options[ $setting ]; |
| 355 |
|
| 356 |
if ( ! $style->id ) { |
| 357 |
$style_name = 0; |
| 358 |
} elseif ( 1 === intval( $style->id ) ) { |
| 359 |
$style_name = 'formidable-style'; |
| 360 |
} else { |
| 361 |
$style_post = $style->get_one(); |
| 362 |
$style_name = $style_post ? $style_post->post_name : 'formidable-style'; |
| 363 |
} |
| 364 |
|
| 365 |
$new_form[ $setting ] = $style_name; |
| 366 |
} else { |
| 367 |
$new_form[ $setting ] = $this->maybe_json( $form->options[ $setting ] ); |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
$forms[] = apply_filters( 'frm_usage_form', $new_form, compact( 'form' ) ); |
| 373 |
}//end foreach |
| 374 |
|
| 375 |
// If the array uses numeric keys, reset them. |
| 376 |
return $forms; |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* @since 3.06.04 |
| 381 |
* @return int |
| 382 |
*/ |
| 383 |
private function form_field_count( $form_id ) { |
| 384 |
global $wpdb; |
| 385 |
|
| 386 |
$join = $wpdb->prefix . 'frm_fields fi JOIN ' . $wpdb->prefix . 'frm_forms fo ON (fi.form_id=fo.id)'; |
| 387 |
|
| 388 |
$field_query = array( |
| 389 |
'or' => 1, |
| 390 |
'fi.form_id' => $form_id, |
| 391 |
'parent_form_id' => $form_id, |
| 392 |
); |
| 393 |
|
| 394 |
return FrmDb::get_count( $join, $field_query ); |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* @since 3.06.04 |
| 399 |
* @return int |
| 400 |
*/ |
| 401 |
private function form_action_count( $form_id ) { |
| 402 |
$args = array( |
| 403 |
'post_type' => FrmFormActionsController::$action_post_type, |
| 404 |
'menu_order' => $form_id, |
| 405 |
'fields' => 'ids', |
| 406 |
); |
| 407 |
|
| 408 |
$actions = FrmDb::check_cache( json_encode( $args ), 'frm_actions', $args, 'get_posts' ); |
| 409 |
return count( $actions ); |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* Get the last 100 fields created. |
| 414 |
* |
| 415 |
* @since 3.06.04 |
| 416 |
* @return array |
| 417 |
*/ |
| 418 |
private function fields() { |
| 419 |
$args = array( |
| 420 |
'limit' => 50, |
| 421 |
'order_by' => 'id DESC', |
| 422 |
); |
| 423 |
|
| 424 |
$fields = FrmDb::get_results( 'frm_fields', array(), 'form_id, name, type, field_options', $args ); |
| 425 |
foreach ( $fields as $k => $field ) { |
| 426 |
FrmAppHelper::unserialize_or_decode( $field->field_options ); |
| 427 |
$fields[ $k ]->field_options = json_encode( $field->field_options ); |
| 428 |
} |
| 429 |
return $fields; |
| 430 |
} |
| 431 |
|
| 432 |
/** |
| 433 |
* @since 3.06.04 |
| 434 |
* @return array |
| 435 |
*/ |
| 436 |
private function actions() { |
| 437 |
$args = array( |
| 438 |
'post_type' => FrmFormActionsController::$action_post_type, |
| 439 |
'numberposts' => 100, |
| 440 |
); |
| 441 |
|
| 442 |
$actions = array(); |
| 443 |
|
| 444 |
$saved_actions = FrmDb::check_cache( json_encode( $args ), 'frm_actions', $args, 'get_posts' ); |
| 445 |
foreach ( $saved_actions as $action ) { |
| 446 |
$actions[] = array( |
| 447 |
'form_id' => $action->menu_order, |
| 448 |
'type' => $action->post_excerpt, |
| 449 |
'status' => $action->post_status, |
| 450 |
'settings' => $action->post_content, |
| 451 |
); |
| 452 |
} |
| 453 |
|
| 454 |
return $actions; |
| 455 |
} |
| 456 |
|
| 457 |
/** |
| 458 |
* @since 3.06.04 |
| 459 |
* @return bool |
| 460 |
*/ |
| 461 |
private function tracking_allowed() { |
| 462 |
return FrmUsageController::tracking_allowed(); |
| 463 |
} |
| 464 |
|
| 465 |
/** |
| 466 |
* @since 3.06.04 |
| 467 |
* @return string |
| 468 |
*/ |
| 469 |
private function maybe_json( $value ) { |
| 470 |
return is_array( $value ) ? json_encode( $value ) : $value; |
| 471 |
} |
| 472 |
} |
| 473 |
|