Completed
Push — master ( e47c17...035b95 )
by
unknown
04:49
created

admin.php ➔ simcal_sanitize_input()   C

Complexity

Conditions 11
Paths 13

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 19
nc 13
nop 2
dl 0
loc 33
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Admin functions
4
 *
5
 * Functions for the admin back end components only.
6
 *
7
 * @package SimpleCalendar/Admin/Functions
8
 */
9
10
if ( ! defined( 'ABSPATH' ) ) {
11
	exit;
12
}
13
14
/**
15
 * Get settings pages and tabs.
16
 *
17
 * @since  3.0.0
18
 *
19
 * @return array
20
 */
21
function simcal_get_admin_pages() {
22
	$objects = \SimpleCalendar\plugin()->objects;
23
	return $objects instanceof \SimpleCalendar\Objects ? $objects->get_admin_pages() : array();
24
}
25
26
/**
27
 * Get a settings page tab.
28
 *
29
 * @since  3.0.0
30
 *
31
 * @param  string $page
32
 *
33
 * @return null|\SimpleCalendar\Abstracts\Admin_Page
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
34
 */
35
function simcal_get_admin_page( $page ) {
36
	$objects = \SimpleCalendar\plugin()->objects;
37
	return $objects instanceof \SimpleCalendar\Objects ? $objects->get_admin_page( $page ) : null;
38
}
39
40
/**
41
 * Get a field.
42
 *
43
 * @since  3.0.0
44
 *
45
 * @param  array  $args
46
 * @param  string $name
47
 *
48
 * @return null|\SimpleCalendar\Abstracts\Field
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
49
 */
50
function simcal_get_field( $args, $name = '' ) {
51
	$objects = \SimpleCalendar\plugin()->objects;
52
	return $objects instanceof \SimpleCalendar\Objects ? $objects->get_field( $args, $name ) : null;
53
}
54
55
/**
56
 * Print a field.
57
 *
58
 * @since  3.0.0
59
 *
60
 * @param  array  $args
61
 * @param  string $name
62
 *
63
 * @return void
64
 */
65
function simcal_print_field( $args, $name = '' ) {
66
67
	$field = simcal_get_field( $args, $name );
68
69
	if ( $field instanceof \SimpleCalendar\Abstracts\Field ) {
70
		$field->html();
71
	}
72
}
73
74
/**
75
 * Sanitize a variable of unknown type.
76
 *
77
 * Recursive helper function to sanitize a variable from input,
78
 * which could also be a multidimensional array of variable depth.
79
 *
80
 * @since  3.0.0
81
 *
82
 * @param  mixed  $var  Variable to sanitize.
83
 * @param  string $func Function to use for sanitizing text strings (default 'sanitize_text_field')
84
 *
85
 * @return array|string Sanitized variable
86
 */
87
function simcal_sanitize_input( $var, $func = 'sanitize_text_field'  ) {
88
89
	if ( is_null( $var ) ) {
90
		return '';
91
	}
92
93
	if ( is_bool( $var ) ) {
94
		if ( $var === true ) {
0 ignored issues
show
introduced by
Found "=== true". Use Yoda Condition checks, you must
Loading history...
95
			return 'yes';
96
		} else {
97
			return 'no';
98
		}
99
	}
100
101
	if ( is_string( $var ) || is_numeric( $var ) ) {
102
		$func = is_string( $func ) && function_exists( $func ) ? $func : 'sanitize_text_field';
103
		return call_user_func( $func, trim( strval( $var ) ) );
104
	}
105
106
	if ( is_object( $var ) ) {
107
		$var = (array) $var;
108
	}
109
110
	if ( is_array( $var ) ) {
111
		$array = array();
112
		foreach ( $var as $k => $v ) {
113
			$array[ $k ] = simcal_sanitize_input( $v );
114
		}
115
		return $array;
116
	}
117
118
	return '';
119
}
120
121
/**
122
 * Check if a screen is a plugin admin view.
123
 * Returns the screen id if true, false (bool) if not.
124
 *
125
 * @since  3.0.0
126
 *
127
 * @return string|bool
128
 */
129
function simcal_is_admin_screen() {
130
131
	$view = function_exists( 'get_current_screen' ) ? get_current_screen() : false;
132
133
	if ( $view instanceof WP_Screen ) {
134
135
		// Screens used by this plugin.
136
		$screens = array(
137
			'customize',
138
			'calendar',
139
			'calendar_page_simple-calendar_add_ons',
140
			'calendar_page_simple-calendar_settings',
141
			'calendar_page_simple-calendar_tools',
142
			'edit-calendar',
143
			'edit-calendar_category',
144
			'dashboard_page_simple-calendar_about',
145
			'dashboard_page_simple-calendar_credits',
146
			'dashboard_page_simple-calendar_translators',
147
		);
148
		if ( in_array( $view->id, $screens ) ) {
149
			return $view->id;
150
		}
151
	}
152
153
	return false;
154
}
155
156
/**
157
 * Update add-on.
158
 *
159
 * @since 3.0.0
160
 *
161
 * @param string $_api_url     The URL pointing to the custom API end
162
 * @param string $_plugin_file Path to the plugin file.
163
 * @param array  $_api_data    Optional data to send with API calls.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $_api_data not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
164
 *
165
 * @return \SimpleCalendar\Admin\Updater
166
 */
167
function simcal_addon_updater( $_api_url, $_plugin_file, $_api_data = null ) {
168
	return new \SimpleCalendar\Admin\Updater( $_api_url, $_plugin_file, $_api_data );
169
}
170
171
/**
172
 * Get add-on license key.
173
 *
174
 * @since  3.0.0
175
 *
176
 * @param  string $addon Unique add-on id.
177
 *
178
 * @return null|string
179
 */
180
function simcal_get_license_key( $addon ) {
181
	$licenses = get_option( 'simple-calendar_settings_licenses', array() );
182
	if ( isset( $licenses['keys'][ $addon ] ) ) {
183
		return empty( $licenses['keys'][ $addon ] ) ? null : $licenses['keys'][ $addon ];
184
	}
185
	return null;
186
}
187
188
/**
189
 * Get add-on license status.
190
 *
191
 * Without passing arguments returns all the statuses array.
192
 *
193
 * @since  3.0.0
194
 *
195
 * @param  null|string $addon Unique add-on slug.
196
 *
197
 * @return array|string
198
 */
199
function simcal_get_license_status( $addon = null ) {
200
	$licenses = get_option( 'simple-calendar_licenses_status', array() );
201
	return isset( $licenses[ $addon ] ) ? $licenses[ $addon ] : $licenses;
202
}
203
204
/**
205
 * Get admin notices.
206
 *
207
 * @since  3.0.0
208
 *
209
 * @return array
210
 */
211
function simcal_get_admin_notices() {
212
	$notices = new \SimpleCalendar\Admin\Notices();
213
	return $notices->get_notices();
214
}
215
216
/**
217
 * Delete admin notices.
218
 *
219
 * @since 3.0.0
220
 */
221
function simcal_delete_admin_notices() {
222
	delete_option( 'simple-calendar_admin_notices' );
223
}
224
225
/**
226
 * Print a shortcode tip.
227
 *
228
 * @since  3.0.0
229
 *
230
 * @param  int $post_id
231
 *
232
 * @return void
233
 */
234
function simcal_print_shortcode_tip( $post_id ) {
235
236
	$browser = new \SimpleCalendar\Browser();
237
	if ( $browser::PLATFORM_APPLE == $browser->getPlatform() ) {
238
		$cmd = '&#8984;&#43;C';
239
	} else {
240
		$cmd = 'Ctrl&#43;C';
241
	}
242
243
	$shortcut  = sprintf( __( 'Press %s to copy.', 'google-calendar-events' ), $cmd );
244
	$shortcode = sprintf( '[calendar id="%s"]', $post_id );
245
246
	echo "<input readonly='readonly' " .
247
				"class='simcal-shortcode simcal-calendar-shortcode simcal-shortcode-tip' " .
248
				"title='" . $shortcut . "' " .
249
				"onclick='this.select();' value='" . $shortcode . "' />";
250
}
251
252
/**
253
 * Google Analytics campaign URL.
254
 *
255
 * @since   3.0.0
256
 *
257
 * @param   string  $base_url   Plain URL to navigate to
258
 * @param   string  $campaign   GA "campaign" tracking value
259
 * @param   string  $content    GA "content" tracking value
260
 * @param   bool    $raw        Use esc_url_raw instead (default = false)
261
 *
262
 * @return  string  $url        Full Google Analytics campaign URL
263
 */
264
function simcal_ga_campaign_url( $base_url, $campaign, $content, $raw = false ) {
265
266
	$url = add_query_arg( array(
267
		'utm_source'   => 'inside-plugin',
268
		'utm_medium'   => 'link',
269
		'utm_campaign' => $campaign, // i.e. 'core-plugin', 'gcal-pro'
270
		'utm_content'  => $content // i.e. 'sidebar-link', 'settings-link'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
271
	), $base_url );
272
273
	if ( $raw ) {
274
		return esc_url_raw( $url );
275
	}
276
277
	return esc_url( $url );
278
}
279
280
/**
281
 * Newsletter signup form.
282
 *
283
 * @since  3.0.0
284
 *
285
 * @return void
286
 */
287
function simcal_newsletter_signup() {
288
289
	if ( $screen = simcal_is_admin_screen() ) {
290
291
		global $current_user;
292
		wp_get_current_user();
293
294
		$name = $current_user->user_firstname ? $current_user->user_firstname : '';
295
296
		?>
297
		<div id="simcal-drip" class="<?php echo $screen; ?>">
298
			<div class="signup">
299
				<p>
300
					<?php _e( "Enter your name and email and we'll send you a coupon code for <strong>20% off</strong> all Pro Add-on purchases.", 'google-calendar-events' ); ?>
301
				</p>
302
303
				<p>
304
					<label for="simcal-drip-field-email"><?php _e( 'Your Email', 'google-calendar-events' ); ?></label><br />
305
					<input type="email"
306
					       id="simcal-drip-field-email"
307
					       name="fields[email]"
308
					       value="<?php echo $current_user->user_email; ?>" />
309
				</p>
310
311
				<p>
312
					<label for="simcal-drip-field-first_name"><?php _e( 'First Name', 'google-calendar-events' ); ?></label><br />
313
					<input type="text"
314
					       id="simcal-drip-field-first_name"
315
					       name="fields[first_name]"
316
					       value="<?php echo $name; ?>" />
317
				</p>
318
				<p class="textright">
319
					<a href="#"
320
					   id="simcal-drip-signup"
321
					   class="button button-primary"><?php _e( 'Send me the coupon', 'google-calendar-events' ); ?></a>
322
				</p>
323
				<div class="textright">
324
					<em><?php _e( 'No spam. Unsubscribe anytime.', 'google-calendar-events' ); ?></em>
325
					<br/>
326
					<a href="<?php echo simcal_ga_campaign_url( simcal_get_url( 'addons' ), 'core-plugin', 'sidebar-link' ); ?>"
327
					   target="_blank"><?php _e( 'Just take me the add-ons', 'google-calendar-events' ); ?></a>
328
				</div>
329
			</div>
330
			<div class="thank-you" style="display: none;">
331
				<?php _e( 'Thank you!', 'google-calendar-events' ); ?>
332
			</div>
333
			<div class="clear">
334
			</div>
335
		</div>
336
		<?php
337
338
	}
339
}
340