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

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