Completed
Push — master ( f34f00...328911 )
by Stephanie
02:55
created

FrmUsage::permissions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 12
rs 9.8666
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
			'themename'      => $theme_data->Name,
79
			'plugins'        => $this->plugins(),
80
			'settings'       => $this->settings(),
81
			'forms'          => $this->forms(),
82
			'fields'         => $this->fields(),
83
			'actions'        => $this->actions(),
84
		);
85
86
		return apply_filters( 'frm_usage_snapshot', $snap );
87
	}
88
89
	/**
90
	 * @since 3.06.04
91
	 * @return array
92
	 */
93
	private function plugins() {
94
		$plugin_list = get_plugins();
95
96
		$plugins = array();
97
		foreach ( $plugin_list as $slug => $info ) {
98
			$plugins[] = array(
99
				'name'        => $info['Name'],
100
				'slug'        => $slug,
101
				'version'     => $info['Version'],
102
				'active'      => is_plugin_active( $slug ),
103
			);
104
		}
105
106
		return $plugins;
107
	}
108
109
	/**
110
	 * Add global settings to tracking data.
111
	 *
112
	 * @since 3.06.04
113
	 * @return array
114
	 */
115
	private function settings() {
116
		$settings_list  = FrmAppHelper::get_settings();
117
		$settings      = array(
118
			'messages'    => $this->messages( $settings_list ),
119
			'permissions' => $this->permissions( $settings_list ),
120
		);
121
		$pass_settings = array(
122
			'load_style',
123
			'use_html',
124
			'old_css',
125
			'fade_form',
126
			'jquery_css',
127
			're_type',
128
			're_lang',
129
			're_multi',
130
			'menu',
131
			'mu_menu',
132
			'no_ips',
133
			'btsp_css',
134
			'btsp_errors',
135
		);
136
		
137
		foreach ( $pass_settings as $setting ) {
138
			if ( isset( $settings_list->$setting ) ) {
139
				$settings[ $setting ] = $settings_list->$setting;
140
			}
141
		}
142
143
		return apply_filters( 'frm_usage_settings', $settings );
144
	}
145
146
	/**
147
	 * Include the permissions settings for each capability.
148
	 *
149
	 * @since 3.06.04
150
	 * @return array
151
	 */
152
	private function messages( $settings_list ) {
153
		$messages = array(
154
			'success_msg',
155
			'blank_msg',
156
			'unique_msg',
157
			'invalid_msg',
158
			'failed_msg',
159
			'submit_value',
160
			'login_msg',
161
			'admin_permission',
162
		);
163
164
		$message_settings = array();
165
		foreach ( $messages as $message ) {
166
			$message_settings[ $message ] = $settings_list->$message;
167
		}
168
169
		return $message_settings;
170
	}
171
172
	/**
173
	 * Include the permissions settings for each capability.
174
	 *
175
	 * @since 3.06.04
176
	 * @return array
177
	 */
178
	private function permissions( $settings_list ) {
179
		$permissions = array();
180
        $frm_roles   = FrmAppHelper::frm_capabilities();
181
182
        foreach ( $frm_roles as $frm_role => $frm_role_description ) {
183
			if ( isset( $settings_list->$frm_role ) ) {
184
				$permissions[ $frm_role ] = $settings_list->$frm_role;
185
			}
186
		}
187
188
		return $permissions;
189
	}
190
191
	/**
192
	 * @since 3.06.04
193
	 * @return array
194
	 */
195
	private function forms() {
196
		$s_query = array(
197
			array(
198
				'or' => 1,
199
				'parent_form_id' => null,
200
				'parent_form_id <' => 1,
201
			),
202
		);
203
204
		$saved_forms = FrmForm::getAll( $s_query );
205
		$forms       = array();
206
		$settings    = array(
207
			'form_class',
208
			'akismet',
209
			'custom_style',
210
			'success_action',
211
			'show_form',
212
			'no_save',
213
			'ajax_load',
214
			'ajax_submit',
215
			'js_validate',
216
			'logged_in_role',
217
			'single_entry',
218
			'single_entry_type',
219
			'editable_role',
220
			'open_editable_role',
221
			'edit_action',
222
			'edit_value',
223
			'edit_msg',
224
			'save_draft',
225
			'draft_msg',
226
			'submit_align',
227
			'protect_files',
228
			'max_entries',
229
			'open_status',
230
			'closed_msg',
231
			'open_date',
232
			'close_date',
233
			'copy',
234
			'prev_value',
235
			'submit_conditions',
236
		);
237
238
		foreach ( $saved_forms as $form ) {
239
			$forms[ $form->id ] = array(
240
				'id'          => $form->id,
241
				'description' => $form->description,
242
				'logged_in'   => $form->logged_in,
243
				'editable'    => $form->editable,
244
				'is_template' => $form->is_template,
245
				'entry_count' => FrmEntry::getRecordCount( $form->id ),
246
				'field_count' => $this->form_field_count( $form->id ),
247
				'form_action_count' => $this->form_action_count( $form->id ),
248
			);
249
250
			foreach ( $settings as $setting ) {
251
				if ( isset( $form->options[ $setting ] ) ) {
252
					$forms[ $form->id ][ $setting ] = $form->options[ $setting ];
253
				}
254
			}
255
		}
256
257
		return apply_filters( 'frm_usage_forms', $forms, compact( 'saved_forms' ) );
258
	}
259
260
	/**
261
	 * @since 3.06.04
262
	 * @return int
263
	 */
264
	private function form_field_count( $form_id ) {
265
		global $wpdb;
266
267
		$join = $wpdb->prefix . 'frm_fields fi LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fo ON (fi.form_id=fo.id)';
268
269
		$field_query = array(
270
			'or'             => 1,
271
			'form_id'        => $form_id,
272
			'parent_form_id' => $form_id,
273
		);
274
275
		return FrmDb::get_count( $join, $field_query );
276
	}
277
278
	/**
279
	 * @since 3.06.04
280
	 * @return int
281
	 */
282
	private function form_action_count( $form_id ) {
283
		$args = array(
284
			'post_type'  => FrmFormActionsController::$action_post_type,
285
			'menu_order' => $form_id,
286
			'fields'     => 'ids',
287
		);
288
289
		$actions = FrmDb::check_cache( serialize( $args ), 'frm_actions', $args, 'get_posts' );
290
		return count( $actions );
291
	}
292
293
	/**
294
	 * Get the last 100 fields created.
295
	 *
296
	 * @since 3.06.04
297
	 * @return array
298
	 */
299
	private function fields() {
300
		$args   = array(
301
			'limit'    => 100,
302
			'order_by' => 'id DESC',
303
		);
304
305
		return FrmDb::get_results( 'frm_fields', array(), 'form_id, name, type', $args );
306
	}
307
308
	/**
309
	 * @since 3.06.04
310
	 * @return array
311
	 */
312
	private function actions() {
313
		$args = array(
314
			'post_type'   => FrmFormActionsController::$action_post_type,
315
			'numberposts' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, numberposts is set to 100
Loading history...
316
		);
317
318
		$actions = array();
319
320
		$saved_actions = FrmDb::check_cache( serialize( $args ), 'frm_actions', $args, 'get_posts' );
321
		foreach ( $saved_actions as $action ) {
322
			$actions[ $action->ID ] = array(
323
				'form_id'  => $action->menu_order,
324
				'type'     => $action->post_excerpt,
325
				'status'   => $action->post_status,
326
				'settings' => $action->post_content,
327
			);
328
		}
329
330
		return $actions;
331
	}
332
333
	/**
334
	 * @since 3.06.04
335
	 * @return bool
336
	 */
337
	private function tracking_allowed() {
338
		$settings = FrmAppHelper::get_settings();
339
		return $settings->tracking;
340
	}
341
}
342
343