Completed
Push — master ( 187862...a3f83c )
by
unknown
02:53
created

admin.php ➔ simcal_upgrade_to_premium()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 0
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
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 ) {
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 ) {
0 ignored issues
show
Bug introduced by
The class WP_Screen does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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.
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'
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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
341
/**
342
 * Upgrade to Premium Add-ons HTML.
343
 *
344
 * @since  3.1.6
345
 *
346
 * @return void
347
 */
348
function simcal_upgrade_to_premium() {
349
350
	if ( $screen = simcal_is_admin_screen() ) {
0 ignored issues
show
Unused Code introduced by
$screen is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
351
		?>
352
		<div class="main">
353
			<p class="heading centered">
354
				<?php _e( 'Some of the features included with our premium add-ons', 'google-calendar-events' ); ?>
355
			</p>
356
357
			<ul>
358
				<li><div class="dashicons dashicons-yes"></div> <?php _e( 'Display color coded events', 'google-calendar-events' ); ?></li>
359
				<li><div class="dashicons dashicons-yes"></div> <?php _e( 'Show week & day views', 'google-calendar-events' ); ?></li>
360
				<li><div class="dashicons dashicons-yes"></div> <?php _e( 'Fast view switching', 'google-calendar-events' ); ?></li>
361
				<li><div class="dashicons dashicons-yes"></div> <?php _e( 'Event titles & start times in grid', 'google-calendar-events' ); ?></li>
362
				<li><div class="dashicons dashicons-yes"></div> <?php _e( 'Limit event display times', 'google-calendar-events' ); ?></li>
363
				<li><div class="dashicons dashicons-yes"></div> <?php _e( 'Display private calendar events', 'google-calendar-events' ); ?></li>
364
				<li><div class="dashicons dashicons-yes"></div> <?php _e( 'Show attendees & RSVP status', 'google-calendar-events' ); ?></li>
365
				<li><div class="dashicons dashicons-yes"></div> <?php _e( 'Display attachments', 'google-calendar-events' ); ?></li>
366
				<li><div class="dashicons dashicons-yes"></div> <?php _e( 'Priority email support', 'google-calendar-events' ); ?></li>
367
			</ul>
368
369
			<div class="centered">
370
				<a href="<?php echo simcal_ga_campaign_url( simcal_get_url( 'addons' ), 'core-plugin', 'sidebar-link' ); ?>"
371
				   class="button-primary button-large" target="_blank">
372
					<?php _e( 'Upgrade to Premium Now', 'google-calendar-events' ); ?></a>
373
			</div>
374
		</div>
375
		<?php
376
	}
377
}
378