PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.25.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.25.1
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.25.1, at classes/models/FrmUsage.php

480 lines 11.0 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 'welcome-tour' => FrmWelcomeTourController::get_usage_data(),
100 'flows' => FrmUsageController::get_flows_data(),
101 'payments' => $this->payments(),
102 'subscriptions' => $this->payments( 'frm_subscriptions' ),
103 );
104
105 if ( method_exists( 'FrmProAddonsController', 'get_readable_license_type' ) ) {
106 $snap['active_license'] = FrmProAddonsController::get_readable_license_type();
107 }
108
109 return apply_filters( 'frm_usage_snapshot', $snap );
110 }
111
112 /**
113 * Gets onboarding wizard data.
114 *
115 * @since 6.16.1
116 *
117 * @return array
118 */
119 private function onboarding_wizard() {
120 $data = FrmOnboardingWizardController::get_usage_data();
121 $skipped_keys = array(
122 'default_email',
123 'is_subscribed',
124 'allows_tracking',
125 'summary_emails',
126 'installed_addons',
127 );
128
129 foreach ( $skipped_keys as $skipped_key ) {
130 unset( $data[ $skipped_key ] );
131 }
132
133 return $data;
134 }
135
136 /**
137 * Gets payments data.
138 *
139 * @since 6.16.1
140 *
141 * @param string $table Database table name.
142 * @return array
143 */
144 private function payments( $table = 'frm_payments' ) {
145 $allowed_tables = array( 'frm_payments', 'frm_subscriptions' );
146 if ( ! in_array( $table, $allowed_tables, true ) ) {
147 return array();
148 }
149
150 if ( ! FrmTransLiteAppHelper::payments_table_exists() ) {
151 return array();
152 }
153
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 );
161
162 $payments = array();
163 foreach ( $rows as $row ) {
164 $payments[] = array(
165 'amount' => (float) $row->amount,
166 'status' => $row->status,
167 'gateway' => $row->paysys,
168 'created_at' => $row->created_at,
169 );
170 }
171
172 return $payments;
173 }
174
175 /**
176 * @since 3.06.04
177 * @return array
178 */
179 private function plugins() {
180 $plugin_list = FrmAppHelper::get_plugins();
181
182 $plugins = array();
183 foreach ( $plugin_list as $slug => $info ) {
184 $plugins[] = array(
185 'name' => $info['Name'],
186 'slug' => $slug,
187 'version' => $info['Version'],
188 'active' => is_plugin_active( $slug ),
189 );
190 }
191
192 return $plugins;
193 }
194
195 /**
196 * Add global settings to tracking data.
197 *
198 * @since 3.06.04
199 * @return array
200 */
201 private function settings() {
202 $settings_list = FrmAppHelper::get_settings();
203 $settings = array(
204 'messages' => $this->messages( $settings_list ),
205 'permissions' => $this->permissions( $settings_list ),
206 );
207 $pass_settings = array(
208 'load_style',
209 'fade_form',
210 're_type',
211 're_lang',
212 're_multi',
213 'menu',
214 'mu_menu',
215 'no_ips',
216 'custom_header_ip',
217 'enable_gdpr',
218 'no_gdpr_cookies',
219 'btsp_css',
220 'btsp_version',
221 'admin_bar',
222 'summary_emails',
223 'active_captcha',
224 'honeypot',
225 'wp_spam_check',
226 'denylist_check',
227 'email_style',
228 );
229
230 foreach ( $pass_settings as $setting ) {
231 if ( isset( $settings_list->$setting ) ) {
232 $settings[ $setting ] = $this->maybe_json( $settings_list->$setting );
233 }
234 }
235
236 $settings = apply_filters( 'frm_usage_settings', $settings );
237
238 $settings['messages'] = $this->maybe_json( $settings['messages'] );
239 $settings['permissions'] = $this->maybe_json( $settings['permissions'] );
240
241 return $settings;
242 }
243
244 /**
245 * Include the permissions settings for each capability.
246 *
247 * @since 3.06.04
248 *
249 * @param FrmSettings $settings_list
250 * @return array
251 */
252 private function messages( $settings_list ) {
253 $messages = array(
254 'success_msg',
255 'blank_msg',
256 'unique_msg',
257 'invalid_msg',
258 'failed_msg',
259 'submit_value',
260 'login_msg',
261 'admin_permission',
262 );
263
264 $default = $settings_list->default_options();
265
266 $message_settings = array();
267 foreach ( $messages as $message ) {
268 $message_settings[ 'changed-' . $message ] = $settings_list->$message === $default[ $message ] ? 0 : 1;
269 }
270
271 return $message_settings;
272 }
273
274 /**
275 * Include the permissions settings for each capability.
276 *
277 * @since 3.06.04
278 *
279 * @param FrmSettings $settings_list
280 * @return array
281 */
282 private function permissions( $settings_list ) {
283 $permissions = array();
284 $frm_roles = FrmAppHelper::frm_capabilities();
285
286 foreach ( $frm_roles as $frm_role => $frm_role_description ) {
287 if ( isset( $settings_list->$frm_role ) ) {
288 $permissions[ $frm_role ] = $settings_list->$frm_role;
289 }
290 }
291
292 return $permissions;
293 }
294
295 /**
296 * @since 3.06.04
297 * @return array
298 */
299 private function forms() {
300 $s_query = array(
301 array(
302 'or' => 1,
303 'parent_form_id' => null,
304 'parent_form_id <' => 1,
305 ),
306 );
307
308 $saved_forms = FrmForm::getAll( $s_query );
309 $forms = array();
310 $settings = array(
311 'form_class',
312 'akismet',
313 'antispam',
314 'stopforumspam',
315 'custom_style',
316 'success_action',
317 'show_form',
318 'no_save',
319 'ajax_load',
320 'ajax_submit',
321 'js_validate',
322 'logged_in_role',
323 'single_entry',
324 'single_entry_type',
325 'editable_role',
326 'open_editable_role',
327 'edit_action',
328 'edit_value',
329 'edit_msg',
330 'save_draft',
331 'draft_msg',
332 'submit_align',
333 'protect_files',
334 'protect_files_role',
335 'max_entries',
336 'open_status',
337 'closed_msg',
338 'open_date',
339 'close_date',
340 'copy',
341 'prev_value',
342 'submit_conditions',
343 );
344
345 $style = new FrmStyle();
346 foreach ( $saved_forms as $form ) {
347 $new_form = array(
348 'form_id' => $form->id,
349 'description' => $form->description,
350 'logged_in' => $form->logged_in,
351 'editable' => $form->editable,
352 'is_template' => $form->is_template,
353 'entry_count' => FrmEntry::getRecordCount( $form->id ),
354 'field_count' => $this->form_field_count( $form->id ),
355 'form_action_count' => $this->form_action_count( $form->id ),
356 );
357
358 foreach ( $settings as $setting ) {
359 if ( isset( $form->options[ $setting ] ) ) {
360 if ( 'custom_style' === $setting ) {
361 $style->id = $form->options[ $setting ];
362
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 }
371
372 $new_form[ $setting ] = $style_name;
373 } else {
374 $new_form[ $setting ] = $this->maybe_json( $form->options[ $setting ] );
375 }
376 }
377 }
378
379 $forms[] = apply_filters( 'frm_usage_form', $new_form, compact( 'form' ) );
380 }//end foreach
381
382 // If the array uses numeric keys, reset them.
383 return $forms;
384 }
385
386 /**
387 * @since 3.06.04
388 * @return int
389 */
390 private function form_field_count( $form_id ) {
391 global $wpdb;
392
393 $join = $wpdb->prefix . 'frm_fields fi JOIN ' . $wpdb->prefix . 'frm_forms fo ON (fi.form_id=fo.id)';
394
395 $field_query = array(
396 'or' => 1,
397 'fi.form_id' => $form_id,
398 'parent_form_id' => $form_id,
399 );
400
401 return FrmDb::get_count( $join, $field_query );
402 }
403
404 /**
405 * @since 3.06.04
406 * @return int
407 */
408 private function form_action_count( $form_id ) {
409 $args = array(
410 'post_type' => FrmFormActionsController::$action_post_type,
411 'menu_order' => $form_id,
412 'fields' => 'ids',
413 );
414
415 $actions = FrmDb::check_cache( json_encode( $args ), 'frm_actions', $args, 'get_posts' );
416 return count( $actions );
417 }
418
419 /**
420 * Get the last 100 fields created.
421 *
422 * @since 3.06.04
423 * @return array
424 */
425 private function fields() {
426 $args = array(
427 'limit' => 50,
428 'order_by' => 'id DESC',
429 );
430
431 $fields = FrmDb::get_results( 'frm_fields', array(), 'id, form_id, name, type, field_options', $args );
432 foreach ( $fields as $k => $field ) {
433 FrmAppHelper::unserialize_or_decode( $field->field_options );
434 $fields[ $k ]->field_options = json_encode( $field->field_options );
435 }
436 return $fields;
437 }
438
439 /**
440 * @since 3.06.04
441 * @return array
442 */
443 private function actions() {
444 $args = array(
445 'post_type' => FrmFormActionsController::$action_post_type,
446 'numberposts' => 100,
447 );
448
449 $actions = array();
450
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 }
460
461 return $actions;
462 }
463
464 /**
465 * @since 3.06.04
466 * @return bool
467 */
468 private function tracking_allowed() {
469 return FrmUsageController::tracking_allowed();
470 }
471
472 /**
473 * @since 3.06.04
474 * @return string
475 */
476 private function maybe_json( $value ) {
477 return is_array( $value ) ? json_encode( $value ) : $value;
478 }
479 }
480