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

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