Completed
Push — master ( 660841...0e90b8 )
by Stephanie
02:51
created

FrmUsage::actions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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