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

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