PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.21
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.21
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / models / FrmUsage.php

FrmUsage.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.21, at classes/models/FrmUsage.php

477 lines 10.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'enable_gdpr',
217 'no_gdpr_cookies',
218 'btsp_css',
219 'btsp_version',
220 'admin_bar',
221 'summary_emails',
222 'active_captcha',
223 'honeypot',
224 'wp_spam_check',
225 );
226
227 foreach ( $pass_settings as $setting ) {
228 if ( isset( $settings_list->$setting ) ) {
229 $settings[ $setting ] = $this->maybe_json( $settings_list->$setting );
230 }
231 }
232
233 $settings = apply_filters( 'frm_usage_settings', $settings );
234
235 $settings['messages'] = $this->maybe_json( $settings['messages'] );
236 $settings['permissions'] = $this->maybe_json( $settings['permissions'] );
237
238 return $settings;
239 }
240
241 /**
242 * Include the permissions settings for each capability.
243 *
244 * @since 3.06.04
245 *
246 * @param FrmSettings $settings_list
247 * @return array
248 */
249 private function messages( $settings_list ) {
250 $messages = array(
251 'success_msg',
252 'blank_msg',
253 'unique_msg',
254 'invalid_msg',
255 'failed_msg',
256 'submit_value',
257 'login_msg',
258 'admin_permission',
259 );
260
261 $default = $settings_list->default_options();
262
263 $message_settings = array();
264 foreach ( $messages as $message ) {
265 $message_settings[ 'changed-' . $message ] = $settings_list->$message === $default[ $message ] ? 0 : 1;
266 }
267
268 return $message_settings;
269 }
270
271 /**
272 * Include the permissions settings for each capability.
273 *
274 * @since 3.06.04
275 *
276 * @param FrmSettings $settings_list
277 * @return array
278 */
279 private function permissions( $settings_list ) {
280 $permissions = array();
281 $frm_roles = FrmAppHelper::frm_capabilities();
282
283 foreach ( $frm_roles as $frm_role => $frm_role_description ) {
284 if ( isset( $settings_list->$frm_role ) ) {
285 $permissions[ $frm_role ] = $settings_list->$frm_role;
286 }
287 }
288
289 return $permissions;
290 }
291
292 /**
293 * @since 3.06.04
294 * @return array
295 */
296 private function forms() {
297 $s_query = array(
298 array(
299 'or' => 1,
300 'parent_form_id' => null,
301 'parent_form_id <' => 1,
302 ),
303 );
304
305 $saved_forms = FrmForm::getAll( $s_query );
306 $forms = array();
307 $settings = array(
308 'form_class',
309 'akismet',
310 'antispam',
311 'stopforumspam',
312 'custom_style',
313 'success_action',
314 'show_form',
315 'no_save',
316 'ajax_load',
317 'ajax_submit',
318 'js_validate',
319 'logged_in_role',
320 'single_entry',
321 'single_entry_type',
322 'editable_role',
323 'open_editable_role',
324 'edit_action',
325 'edit_value',
326 'edit_msg',
327 'save_draft',
328 'draft_msg',
329 'submit_align',
330 'protect_files',
331 'protect_files_role',
332 'max_entries',
333 'open_status',
334 'closed_msg',
335 'open_date',
336 'close_date',
337 'copy',
338 'prev_value',
339 'submit_conditions',
340 );
341
342 $style = new FrmStyle();
343 foreach ( $saved_forms as $form ) {
344 $new_form = array(
345 'form_id' => $form->id,
346 'description' => $form->description,
347 'logged_in' => $form->logged_in,
348 'editable' => $form->editable,
349 'is_template' => $form->is_template,
350 'entry_count' => FrmEntry::getRecordCount( $form->id ),
351 'field_count' => $this->form_field_count( $form->id ),
352 'form_action_count' => $this->form_action_count( $form->id ),
353 );
354
355 foreach ( $settings as $setting ) {
356 if ( isset( $form->options[ $setting ] ) ) {
357 if ( 'custom_style' === $setting ) {
358 $style->id = $form->options[ $setting ];
359
360 if ( ! $style->id ) {
361 $style_name = 0;
362 } elseif ( 1 === intval( $style->id ) ) {
363 $style_name = 'formidable-style';
364 } else {
365 $style_post = $style->get_one();
366 $style_name = $style_post ? $style_post->post_name : 'formidable-style';
367 }
368
369 $new_form[ $setting ] = $style_name;
370 } else {
371 $new_form[ $setting ] = $this->maybe_json( $form->options[ $setting ] );
372 }
373 }
374 }
375
376 $forms[] = apply_filters( 'frm_usage_form', $new_form, compact( 'form' ) );
377 }//end foreach
378
379 // If the array uses numeric keys, reset them.
380 return $forms;
381 }
382
383 /**
384 * @since 3.06.04
385 * @return int
386 */
387 private function form_field_count( $form_id ) {
388 global $wpdb;
389
390 $join = $wpdb->prefix . 'frm_fields fi JOIN ' . $wpdb->prefix . 'frm_forms fo ON (fi.form_id=fo.id)';
391
392 $field_query = array(
393 'or' => 1,
394 'fi.form_id' => $form_id,
395 'parent_form_id' => $form_id,
396 );
397
398 return FrmDb::get_count( $join, $field_query );
399 }
400
401 /**
402 * @since 3.06.04
403 * @return int
404 */
405 private function form_action_count( $form_id ) {
406 $args = array(
407 'post_type' => FrmFormActionsController::$action_post_type,
408 'menu_order' => $form_id,
409 'fields' => 'ids',
410 );
411
412 $actions = FrmDb::check_cache( json_encode( $args ), 'frm_actions', $args, 'get_posts' );
413 return count( $actions );
414 }
415
416 /**
417 * Get the last 100 fields created.
418 *
419 * @since 3.06.04
420 * @return array
421 */
422 private function fields() {
423 $args = array(
424 'limit' => 50,
425 'order_by' => 'id DESC',
426 );
427
428 $fields = FrmDb::get_results( 'frm_fields', array(), 'id, form_id, name, type, field_options', $args );
429 foreach ( $fields as $k => $field ) {
430 FrmAppHelper::unserialize_or_decode( $field->field_options );
431 $fields[ $k ]->field_options = json_encode( $field->field_options );
432 }
433 return $fields;
434 }
435
436 /**
437 * @since 3.06.04
438 * @return array
439 */
440 private function actions() {
441 $args = array(
442 'post_type' => FrmFormActionsController::$action_post_type,
443 'numberposts' => 100,
444 );
445
446 $actions = array();
447
448 $saved_actions = FrmDb::check_cache( json_encode( $args ), 'frm_actions', $args, 'get_posts' );
449 foreach ( $saved_actions as $action ) {
450 $actions[] = array(
451 'form_id' => $action->menu_order,
452 'type' => $action->post_excerpt,
453 'status' => $action->post_status,
454 'settings' => $action->post_content,
455 );
456 }
457
458 return $actions;
459 }
460
461 /**
462 * @since 3.06.04
463 * @return bool
464 */
465 private function tracking_allowed() {
466 return FrmUsageController::tracking_allowed();
467 }
468
469 /**
470 * @since 3.06.04
471 * @return string
472 */
473 private function maybe_json( $value ) {
474 return is_array( $value ) ? json_encode( $value ) : $value;
475 }
476 }
477