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

378 lines 8.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 = 'aHR0cHM6Ly91c2FnZS5mb3JtaWRhYmxlZm9ybXMuY29tL2FwcC9zbmFwc2hvdAo=';
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 );
35
36 wp_remote_request( base64_decode( $ep ), $post );
37 }
38
39 /**
40 * @since 3.06.04
41 * @return string
42 */
43 public function uuid( $regenerate = false ) {
44 $uuid_key = 'frm-usage-uuid';
45 $uuid = get_option( $uuid_key );
46
47 if ( $regenerate || empty( $uuid ) ) {
48 // Definitely not cryptographically secure but
49 // close enough to provide an unique id
50 $uuid = md5( uniqid() . site_url() );
51 update_option( $uuid_key, $uuid, 'no' );
52 }
53
54 return $uuid;
55 }
56
57 /**
58 * @since 3.06.04
59 * @return array
60 */
61 public function snapshot() {
62 global $wpdb, $wp_version;
63
64 $theme_data = wp_get_theme();
65 $form_counts = FrmForm::get_count();
66
67 $snap = array(
68 'uuid' => $this->uuid(),
69 // Let's keep it anonymous.
70 'admin_email' => '',
71 'wp_version' => $wp_version,
72 'php_version' => phpversion(),
73 'mysql_version' => $wpdb->db_version(),
74 'os' => FrmAppHelper::get_server_os(),
75 'locale' => get_locale(),
76
77 'active_license' => FrmAppHelper::pro_is_installed(),
78 'form_count' => $form_counts->published,
79 'entry_count' => FrmEntry::getRecordCount(),
80 'timestamp' => gmdate( 'c' ),
81
82 'theme_name' => is_object( $theme_data ) ? $theme_data->Name : '', // phpcs:ignore WordPress.NamingConventions
83 'plugins' => $this->plugins(),
84 'settings' => array(
85 $this->settings(),
86 ),
87 'forms' => $this->forms(),
88 'fields' => $this->fields(),
89 'actions' => $this->actions(),
90
91 'onboarding-wizard' => FrmOnboardingWizardController::get_usage_data(),
92 );
93
94 return apply_filters( 'frm_usage_snapshot', $snap );
95 }
96
97 /**
98 * @since 3.06.04
99 * @return array
100 */
101 private function plugins() {
102 $plugin_list = FrmAppHelper::get_plugins();
103
104 $plugins = array();
105 foreach ( $plugin_list as $slug => $info ) {
106 $plugins[] = array(
107 'name' => $info['Name'],
108 'slug' => $slug,
109 'version' => $info['Version'],
110 'active' => is_plugin_active( $slug ),
111 );
112 }
113
114 return $plugins;
115 }
116
117 /**
118 * Add global settings to tracking data.
119 *
120 * @since 3.06.04
121 * @return array
122 */
123 private function settings() {
124 $settings_list = FrmAppHelper::get_settings();
125 $settings = array(
126 'messages' => $this->messages( $settings_list ),
127 'permissions' => $this->permissions( $settings_list ),
128 );
129 $pass_settings = array(
130 'load_style',
131 'use_html',
132 'fade_form',
133 'jquery_css',
134 're_type',
135 're_lang',
136 're_multi',
137 'menu',
138 'mu_menu',
139 'no_ips',
140 'btsp_css',
141 'btsp_errors',
142 'admin_bar',
143 );
144
145 foreach ( $pass_settings as $setting ) {
146 if ( isset( $settings_list->$setting ) ) {
147 $settings[ $setting ] = $this->maybe_json( $settings_list->$setting );
148 }
149 }
150
151 $settings = apply_filters( 'frm_usage_settings', $settings );
152
153 $settings['messages'] = $this->maybe_json( $settings['messages'] );
154 $settings['permissions'] = $this->maybe_json( $settings['permissions'] );
155
156 return $settings;
157 }
158
159 /**
160 * Include the permissions settings for each capability.
161 *
162 * @since 3.06.04
163 *
164 * @param FrmSettings $settings_list
165 * @return array
166 */
167 private function messages( $settings_list ) {
168 $messages = array(
169 'success_msg',
170 'blank_msg',
171 'unique_msg',
172 'invalid_msg',
173 'failed_msg',
174 'submit_value',
175 'login_msg',
176 'admin_permission',
177 );
178
179 $message_settings = array();
180 foreach ( $messages as $message ) {
181 $message_settings[ $message ] = $settings_list->$message;
182 }
183
184 return $message_settings;
185 }
186
187 /**
188 * Include the permissions settings for each capability.
189 *
190 * @since 3.06.04
191 *
192 * @param FrmSettings $settings_list
193 * @return array
194 */
195 private function permissions( $settings_list ) {
196 $permissions = array();
197 $frm_roles = FrmAppHelper::frm_capabilities();
198
199 foreach ( $frm_roles as $frm_role => $frm_role_description ) {
200 if ( isset( $settings_list->$frm_role ) ) {
201 $permissions[ $frm_role ] = $settings_list->$frm_role;
202 }
203 }
204
205 return $permissions;
206 }
207
208 /**
209 * @since 3.06.04
210 * @return array
211 */
212 private function forms() {
213 $s_query = array(
214 array(
215 'or' => 1,
216 'parent_form_id' => null,
217 'parent_form_id <' => 1,
218 ),
219 );
220
221 $saved_forms = FrmForm::getAll( $s_query );
222 $forms = array();
223 $settings = array(
224 'form_class',
225 'akismet',
226 'honeypot',
227 'antispam',
228 'custom_style',
229 'success_action',
230 'show_form',
231 'no_save',
232 'ajax_load',
233 'ajax_submit',
234 'js_validate',
235 'logged_in_role',
236 'single_entry',
237 'single_entry_type',
238 'editable_role',
239 'open_editable_role',
240 'edit_action',
241 'edit_value',
242 'edit_msg',
243 'save_draft',
244 'draft_msg',
245 'submit_align',
246 'protect_files',
247 'protect_files_role',
248 'max_entries',
249 'open_status',
250 'closed_msg',
251 'open_date',
252 'close_date',
253 'copy',
254 'prev_value',
255 'submit_conditions',
256 );
257
258 foreach ( $saved_forms as $form ) {
259 $new_form = array(
260 'form_id' => $form->id,
261 'description' => $form->description,
262 'logged_in' => $form->logged_in,
263 'editable' => $form->editable,
264 'is_template' => $form->is_template,
265 'entry_count' => FrmEntry::getRecordCount( $form->id ),
266 'field_count' => $this->form_field_count( $form->id ),
267 'form_action_count' => $this->form_action_count( $form->id ),
268 );
269
270 foreach ( $settings as $setting ) {
271 if ( isset( $form->options[ $setting ] ) ) {
272 $new_form[ $setting ] = $this->maybe_json( $form->options[ $setting ] );
273 }
274 }
275
276 $forms[] = apply_filters( 'frm_usage_form', $new_form, compact( 'form' ) );
277 }
278
279 // If the array uses numeric keys, reset them.
280 return $forms;
281 }
282
283 /**
284 * @since 3.06.04
285 * @return int
286 */
287 private function form_field_count( $form_id ) {
288 global $wpdb;
289
290 $join = $wpdb->prefix . 'frm_fields fi LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fo ON (fi.form_id=fo.id)';
291
292 $field_query = array(
293 'or' => 1,
294 'fi.form_id' => $form_id,
295 'parent_form_id' => $form_id,
296 );
297
298 return FrmDb::get_count( $join, $field_query );
299 }
300
301 /**
302 * @since 3.06.04
303 * @return int
304 */
305 private function form_action_count( $form_id ) {
306 $args = array(
307 'post_type' => FrmFormActionsController::$action_post_type,
308 'menu_order' => $form_id,
309 'fields' => 'ids',
310 );
311
312 $actions = FrmDb::check_cache( json_encode( $args ), 'frm_actions', $args, 'get_posts' );
313 return count( $actions );
314 }
315
316 /**
317 * Get the last 100 fields created.
318 *
319 * @since 3.06.04
320 * @return array
321 */
322 private function fields() {
323 $args = array(
324 'limit' => 50,
325 'order_by' => 'id DESC',
326 );
327
328 $fields = FrmDb::get_results( 'frm_fields', array(), 'form_id, name, type, field_options', $args );
329 foreach ( $fields as $k => $field ) {
330 FrmAppHelper::unserialize_or_decode( $field->field_options );
331 $fields[ $k ]->field_options = json_encode( $field->field_options );
332 }
333 return $fields;
334 }
335
336 /**
337 * @since 3.06.04
338 * @return array
339 */
340 private function actions() {
341 $args = array(
342 'post_type' => FrmFormActionsController::$action_post_type,
343 'numberposts' => 100,
344 );
345
346 $actions = array();
347
348 $saved_actions = FrmDb::check_cache( json_encode( $args ), 'frm_actions', $args, 'get_posts' );
349 foreach ( $saved_actions as $action ) {
350 $actions[] = array(
351 'form_id' => $action->menu_order,
352 'type' => $action->post_excerpt,
353 'status' => $action->post_status,
354 'settings' => $action->post_content,
355 );
356 }
357
358 return $actions;
359 }
360
361 /**
362 * @since 3.06.04
363 * @return bool
364 */
365 private function tracking_allowed() {
366 $settings = FrmAppHelper::get_settings();
367 return $settings->tracking;
368 }
369
370 /**
371 * @since 3.06.04
372 * @return string
373 */
374 private function maybe_json( $value ) {
375 return is_array( $value ) ? json_encode( $value ) : $value;
376 }
377 }
378