Passed
Push — master ( c2a68e...bcea1f )
by Stiofan
03:51
created

ayecode_connect_helper_installed()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 6
c 1
b 0
f 0
nc 64
nop 0
dl 0
loc 8
rs 8.8333
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
if ( ! class_exists( "AyeCode_Connect_Helper" ) ) {
8
	/**
9
	 * Allow the quick setup and connection of our AyeCode Connect plugin.
10
	 *
11
	 * Class AyeCode_Connect_Helper
12
	 */
13
	class AyeCode_Connect_Helper {
14
15
		// Hold the version number
16
		var $version = "1.0.3";
17
18
		// Hold the default strings.
19
		var $strings = array();
20
21
		// Hold the default pages.
22
		var $pages = array();
23
24
		/**
25
		 * The constructor.
26
		 *
27
		 * AyeCode_Connect_Helper constructor.
28
		 *
29
		 * @param array $strings
30
		 * @param array $pages
31
		 */
32
		public function __construct( $strings = array(), $pages = array() ) {
33
34
			// Only fire if not localhost and the current user has the right permissions.
35
			if ( ! $this->is_localhost() && current_user_can( 'manage_options' ) ) {
36
37
38
				// set default strings
39
				$default_strings = array(
40
					'connect_title'     => __( "Thanks for choosing an AyeCode Product!" ),
41
					'connect_external'  => __( "Please confirm you wish to connect your site?" ),
42
					'connect'           => sprintf( __( "<strong>Have a license?</strong> Forget about entering license keys or downloading zip files, connect your site for instant access. %slearn more%s" ), "<a href='https://ayecode.io/introducing-ayecode-connect/' target='_blank'>", "</a>" ),
43
					'connect_button'    => __( "Connect Site" ),
44
					'connecting_button' => __( "Connecting..." ),
45
					'error_localhost'   => __( "This service will only work with a live domain, not a localhost." ),
46
					'error'             => __( "Something went wrong, please refresh and try again." ),
47
				);
48
				$this->strings   = array_merge( $default_strings, $strings );
49
50
51
				// set default pages
52
				$default_pages = array();
53
				$this->pages   = array_merge( $default_pages, $pages );
54
55
				// maybe show connect site notice
56
				add_action( 'admin_notices', array( $this, 'ayecode_connect_install_notice' ) );
57
58
				// add ajax action if not already added
59
				if ( ! has_action( 'wp_ajax_ayecode_connect_helper' ) ) {
60
					add_action( 'wp_ajax_ayecode_connect_helper', array( $this, 'ayecode_connect_install' ) );
61
				}
62
			}
63
64
			// add ajax action if not already added
65
			if ( ! has_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed' ) ) {
66
				add_action( 'wp_ajax_nopriv_ayecode_connect_helper_installed', array( $this, 'ayecode_connect_helper_installed' ) );
67
			}
68
69
		}
70
71
		/**
72
		 * Give a way to check we can connect via a external redirect.
73
		 */
74
		public function ayecode_connect_helper_installed(){
75
			$active = array(
76
				'gd'    =>  defined('GEODIRECTORY_VERSION') && version_compare(GEODIRECTORY_VERSION,'2.0.0.79','>') ? 1 : 0,
0 ignored issues
show
Bug introduced by
The constant GEODIRECTORY_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
77
				'uwp'    =>  defined('USERSWP_VERSION') && version_compare(USERSWP_VERSION,'1.2.1.5','>') ? 1 : 0,
0 ignored issues
show
Bug introduced by
The constant USERSWP_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
78
				'wpi'    =>  defined('WPINV_VERSION') && version_compare(WPINV_VERSION,'1.0.14','>') ? 1 : 0,
79
			);
80
			wp_send_json_success( $active );
81
			wp_die();
82
		}
83
84
		/**
85
		 * Get slug from path
86
		 *
87
		 * @param  string $key
88
		 *
89
		 * @return string
90
		 */
91
		private function format_plugin_slug( $key ) {
92
			$slug = explode( '/', $key );
93
			$slug = explode( '.', end( $slug ) );
94
95
			return $slug[0];
96
		}
97
98
		/**
99
		 * Install and activate the AyeCode Connect Plugin
100
		 */
101
		public function ayecode_connect_install() {
102
103
			// bail if localhost
104
			if ( $this->is_localhost() ) {
105
				wp_send_json_error( $this->strings['error_localhost'] );
106
			}
107
108
			// Explicitly clear the event.
109
			wp_clear_scheduled_hook( 'geodir_plugin_background_installer', func_get_args() );
110
111
			$success     = true;
112
			$plugin_slug = "ayecode-connect";
113
			if ( ! empty( $plugin_slug ) ) {
0 ignored issues
show
introduced by
The condition empty($plugin_slug) is always false.
Loading history...
114
				require_once( ABSPATH . 'wp-admin/includes/file.php' );
115
				require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
116
				require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
117
				require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
118
119
				WP_Filesystem();
120
121
				$skin              = new Automatic_Upgrader_Skin;
122
				$upgrader          = new WP_Upgrader( $skin );
123
				$installed_plugins = array_map( array( $this, 'format_plugin_slug' ), array_keys( get_plugins() ) );
124
				$plugin_slug       = $plugin_slug;
125
				$plugin            = $plugin_slug . '/' . $plugin_slug . '.php';
126
				$installed         = false;
127
				$activate          = false;
128
129
				// See if the plugin is installed already
130
				if ( in_array( $plugin_slug, $installed_plugins ) ) {
131
					$installed = true;
132
					$activate  = ! is_plugin_active( $plugin );
133
				}
134
135
				// Install this thing!
136
				if ( ! $installed ) {
137
138
					// Suppress feedback
139
					ob_start();
140
141
					try {
142
						$plugin_information = plugins_api( 'plugin_information', array(
143
							'slug'   => $plugin_slug,
144
							'fields' => array(
145
								'short_description' => false,
146
								'sections'          => false,
147
								'requires'          => false,
148
								'rating'            => false,
149
								'ratings'           => false,
150
								'downloaded'        => false,
151
								'last_updated'      => false,
152
								'added'             => false,
153
								'tags'              => false,
154
								'homepage'          => false,
155
								'donate_link'       => false,
156
								'author_profile'    => false,
157
								'author'            => false,
158
							),
159
						) );
160
161
						if ( is_wp_error( $plugin_information ) ) {
162
							throw new Exception( $plugin_information->get_error_message() );
163
						}
164
165
						$package  = $plugin_information->download_link;
0 ignored issues
show
Bug introduced by
The property download_link does not seem to exist on WP_Error.
Loading history...
166
						$download = $upgrader->download_package( $package );
167
168
						if ( is_wp_error( $download ) ) {
169
							throw new Exception( $download->get_error_message() );
170
						}
171
172
						$working_dir = $upgrader->unpack_package( $download, true );
0 ignored issues
show
Bug introduced by
It seems like $download can also be of type WP_Error; however, parameter $package of WP_Upgrader::unpack_package() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

172
						$working_dir = $upgrader->unpack_package( /** @scrutinizer ignore-type */ $download, true );
Loading history...
173
174
						if ( is_wp_error( $working_dir ) ) {
175
							throw new Exception( $working_dir->get_error_message() );
176
						}
177
178
						$result = $upgrader->install_package( array(
179
							'source'                      => $working_dir,
180
							'destination'                 => WP_PLUGIN_DIR,
181
							'clear_destination'           => false,
182
							'abort_if_destination_exists' => false,
183
							'clear_working'               => true,
184
							'hook_extra'                  => array(
185
								'type'   => 'plugin',
186
								'action' => 'install',
187
							),
188
						) );
189
190
						if ( is_wp_error( $result ) ) {
191
							throw new Exception( $result->get_error_message() );
192
						}
193
194
						$activate = true;
195
196
					} catch ( Exception $e ) {
197
						$success = false;
198
					}
199
200
					// Discard feedback
201
					ob_end_clean();
202
				}
203
204
				wp_clean_plugins_cache();
205
206
				// Activate this thing
207
				if ( $activate ) {
208
					try {
209
						$result = activate_plugin( $plugin );
210
211
						if ( is_wp_error( $result ) ) {
212
							$success = false;
213
						} else {
214
							$success = true;
215
						}
216
					} catch ( Exception $e ) {
217
						$success = false;
218
					}
219
				}
220
			}
221
222
			if ( $success && function_exists( 'ayecode_connect_args' ) ) {
223
				ayecode_connect();// init
0 ignored issues
show
Bug introduced by
The function ayecode_connect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

223
				/** @scrutinizer ignore-call */ 
224
    ayecode_connect();// init
Loading history...
224
				$args        = ayecode_connect_args();
225
				$client      = new AyeCode_Connect( $args );
226
				$redirect_to = ! empty( $_POST['redirect_to'] ) ? esc_url_raw( $_POST['redirect_to'] ) : '';
227
				$redirect    = $client->build_connect_url( $redirect_to );
228
				wp_send_json_success( array( 'connect_url' => $redirect ) );
229
			} else {
230
				wp_send_json_error( $this->strings['error_localhost'] );
231
			}
232
			wp_die();
233
		}
234
235
		/**
236
		 * Check if maybe localhost.
237
		 *
238
		 * @return bool
239
		 */
240
		public function is_localhost() {
241
			$localhost = false;
242
243
			$host              = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '';
244
			$localhost_domains = array(
245
				'localhost',
246
				'localhost.localdomain',
247
				'127.0.0.1',
248
				'::1'
249
			);
250
251
			if ( in_array( $host, $localhost_domains ) ) {
252
				$localhost = true;
253
			}
254
255
			return $localhost;
256
		}
257
258
		/**
259
		 * Show notice to connect site.
260
		 */
261
		public function ayecode_connect_install_notice() {
262
			if ( $this->maybe_show() ) {
263
				$connect_title_string     = $this->strings['connect_title'];
264
				$connect_external_string  = $this->strings['connect_external'];
265
				$connect_string           = $this->strings['connect'];
266
				$connect_button_string    = $this->strings['connect_button'];
267
				$connecting_button_string = $this->strings['connecting_button'];
268
				?>
269
				<div class="notice notice-info acch-notice">
270
					<span class="acch-float-left">
271
						<svg width="61px" height="61px" viewBox="0 0 61 61" version="1.1"
272
						     xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
273
						<defs>
274
							<polygon id="path-1"
275
							         points="4.70437018e-05 0.148846272 60.8504481 0.148846272 60.8504481 61 4.70437018e-05 61"></polygon>
276
						</defs>
277
						<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
278
							<g id="Group-6" transform="translate(-8.000000, -4.000000)">
279
								<g id="Group-5" transform="translate(8.000000, 4.000000)">
280
									<g id="Group-17">
281
										<mask id="mask-2" fill="white">
282
											<use xlink:href="#path-1"></use>
283
										</mask>
284
										<g id="Clip-16"></g>
285
										<path
286
											d="M60.8504481,30.5740468 C60.8504481,47.3793116 47.229101,61.0000314 30.4252476,61.0000314 C13.6215509,61.0000314 4.70437018e-05,47.3793116 4.70437018e-05,30.5740468 C4.70437018e-05,13.7711342 13.6215509,0.148846272 30.4252476,0.148846272 C47.229101,0.148846272 60.8504481,13.7711342 60.8504481,30.5740468"
287
											id="Fill-15" fill="#4C96D7" mask="url(#mask-2)"></path>
288
									</g>
289
									<path
290
										d="M7.34736527,20.4434788 C7.34736527,14.815441 10.231253,12 16,12 L16,16.2224505 C13.1153027,16.2224505 11.6736826,17.6294599 11.6736826,20.4434788 L11.6736826,26.7780236 C11.6736826,28.9016534 10.59182,30.310085 8.42858032,30.9988939 C10.5775721,31.7039788 11.6519871,33.1116203 11.6519871,35.2221344 L11.6519871,41.5566793 C11.6519871,44.3705401 13.0927976,45.7777075 15.9783045,45.7777075 L15.9783045,50 C10.2246148,50 7.34736527,47.184717 7.34736527,41.5566793 L7.34736527,35.2221344 C7.34736527,33.815283 5.89748795,33.1116203 3,33.1116203 L3,28.8883797 C5.89748795,28.8883797 7.34736527,28.185033 7.34736527,26.7780236 L7.34736527,20.4434788 Z"
291
										id="Fill-18" fill="#FFFFFF"></path>
292
									<path
293
										d="M53.6524181,41.5551342 C53.6524181,47.1845707 50.7690344,50 45,50 L45,45.7775671 C47.8841934,45.7775671 49.3266948,44.3691413 49.3266948,41.5551342 L49.3266948,35.2221959 C49.3266948,33.0969947 50.4079637,31.689201 52.5719588,30.9989729 C50.4222123,30.2954711 49.3483914,28.8884675 49.3483914,26.77654 L49.3483914,20.4434437 C49.3483914,17.6281723 47.90589,16.2225909 45.021049,16.2225909 L45.021049,12 C50.7758348,12 53.6524181,14.8140072 53.6524181,20.4434437 L53.6524181,26.77654 C53.6524181,28.1835435 55.1023677,28.8884675 58,28.8884675 L58,33.1116905 C55.1023677,33.1116905 53.6524181,33.8151923 53.6524181,35.2221959 L53.6524181,41.5551342 Z"
294
										id="Fill-20" fill="#FFFFFF"></path>
295
									<path
296
										d="M46.0272652,44 C48.1048141,44 48.9396754,43.2042837 48.9795214,41.1979284 L34.4844624,30.499526 L49,19.7867451 C48.9558678,17.7920822 48.1210065,17 46.0479025,17 L45.8064452,17 L30.9992856,27.9275105 L16.1929198,17 L15.9727348,17 C13.8943922,17 13.0596896,17.7958743 13.0206374,19.8023876 L27.5141088,30.499526 L13,41.2126229 C13.0434972,43.2071278 13.8781998,44 15.9513037,44 L16.1929198,44 L30.9992856,33.0718574 L45.8064452,44 L46.0272652,44 Z"
297
										id="Fill-22" fill="#FFFFFF"></path>
298
								</g>
299
							</g>
300
						</g>
301
					</svg>
302
					</span>
303
					<span class="acch-float-left acch-text">
304
						<h3 class="acch-title"><?php echo esc_attr( $connect_title_string ); ?></h3>
305
					<p>
306
						<?php
307
						echo $connect_string;
308
						?>
309
					</p>
310
					</span>
311
312
					<span class="acch-float-left acch-button">
313
						<button onclick="ayecode_connect_helper(this);" id="gd-connect-site"
314
						        class="button button-primary"
315
						        data-connecting="<?php echo esc_attr( $connecting_button_string ); ?>"><?php echo esc_attr( $connect_button_string ) ?></button>
316
					</span>
317
318
				</div>
319
320
				<?php
321
				// only include the popup HTML if needed.
322
				if ( ! empty( $_REQUEST['external-connect-request'] ) ) {
323
					?>
324
					<div id="ayecode-connect-helper-external-confirm" style="display:none;">
325
						<div class="noticex notice-info acch-notice" style="border: none;">
326
					<span class="acch-float-left">
327
						<svg width="61px" height="61px" viewBox="0 0 61 61" version="1.1"
328
						     xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
329
						<defs>
330
							<polygon id="path-1"
331
							         points="4.70437018e-05 0.148846272 60.8504481 0.148846272 60.8504481 61 4.70437018e-05 61"></polygon>
332
						</defs>
333
						<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
334
							<g id="Group-6" transform="translate(-8.000000, -4.000000)">
335
								<g id="Group-5" transform="translate(8.000000, 4.000000)">
336
									<g id="Group-17">
337
										<mask id="mask-2" fill="white">
338
											<use xlink:href="#path-1"></use>
339
										</mask>
340
										<g id="Clip-16"></g>
341
										<path
342
											d="M60.8504481,30.5740468 C60.8504481,47.3793116 47.229101,61.0000314 30.4252476,61.0000314 C13.6215509,61.0000314 4.70437018e-05,47.3793116 4.70437018e-05,30.5740468 C4.70437018e-05,13.7711342 13.6215509,0.148846272 30.4252476,0.148846272 C47.229101,0.148846272 60.8504481,13.7711342 60.8504481,30.5740468"
343
											id="Fill-15" fill="#4C96D7" mask="url(#mask-2)"></path>
344
									</g>
345
									<path
346
										d="M7.34736527,20.4434788 C7.34736527,14.815441 10.231253,12 16,12 L16,16.2224505 C13.1153027,16.2224505 11.6736826,17.6294599 11.6736826,20.4434788 L11.6736826,26.7780236 C11.6736826,28.9016534 10.59182,30.310085 8.42858032,30.9988939 C10.5775721,31.7039788 11.6519871,33.1116203 11.6519871,35.2221344 L11.6519871,41.5566793 C11.6519871,44.3705401 13.0927976,45.7777075 15.9783045,45.7777075 L15.9783045,50 C10.2246148,50 7.34736527,47.184717 7.34736527,41.5566793 L7.34736527,35.2221344 C7.34736527,33.815283 5.89748795,33.1116203 3,33.1116203 L3,28.8883797 C5.89748795,28.8883797 7.34736527,28.185033 7.34736527,26.7780236 L7.34736527,20.4434788 Z"
347
										id="Fill-18" fill="#FFFFFF"></path>
348
									<path
349
										d="M53.6524181,41.5551342 C53.6524181,47.1845707 50.7690344,50 45,50 L45,45.7775671 C47.8841934,45.7775671 49.3266948,44.3691413 49.3266948,41.5551342 L49.3266948,35.2221959 C49.3266948,33.0969947 50.4079637,31.689201 52.5719588,30.9989729 C50.4222123,30.2954711 49.3483914,28.8884675 49.3483914,26.77654 L49.3483914,20.4434437 C49.3483914,17.6281723 47.90589,16.2225909 45.021049,16.2225909 L45.021049,12 C50.7758348,12 53.6524181,14.8140072 53.6524181,20.4434437 L53.6524181,26.77654 C53.6524181,28.1835435 55.1023677,28.8884675 58,28.8884675 L58,33.1116905 C55.1023677,33.1116905 53.6524181,33.8151923 53.6524181,35.2221959 L53.6524181,41.5551342 Z"
350
										id="Fill-20" fill="#FFFFFF"></path>
351
									<path
352
										d="M46.0272652,44 C48.1048141,44 48.9396754,43.2042837 48.9795214,41.1979284 L34.4844624,30.499526 L49,19.7867451 C48.9558678,17.7920822 48.1210065,17 46.0479025,17 L45.8064452,17 L30.9992856,27.9275105 L16.1929198,17 L15.9727348,17 C13.8943922,17 13.0596896,17.7958743 13.0206374,19.8023876 L27.5141088,30.499526 L13,41.2126229 C13.0434972,43.2071278 13.8781998,44 15.9513037,44 L16.1929198,44 L30.9992856,33.0718574 L45.8064452,44 L46.0272652,44 Z"
353
										id="Fill-22" fill="#FFFFFF"></path>
354
								</g>
355
							</g>
356
						</g>
357
					</svg>
358
					</span>
359
					<span class="acch-float-left acch-text">
360
						<h3 class="acch-title"><?php echo esc_attr( $connect_external_string ); ?></h3>
361
					</span>
362
363
					<span class="acch-float-left acch-button">
364
						<button onclick="ayecode_connect_helper(this);" id="gd-connect-site"
365
						        class="button button-primary"
366
						        data-connecting="<?php echo esc_attr( $connecting_button_string ); ?>"><?php echo esc_attr( $connect_button_string ) ?></button>
367
					</span>
368
369
						</div>
370
					</div>
371
					<?php
372
				}
373
374
				// add required scripts
375
				$this->script();
376
			}
377
		}
378
379
		/**
380
		 * Get the JS Script.
381
		 */
382
		public function script() {
383
384
			// add thickbox if external request is requested
385
			if ( ! empty( $_REQUEST['external-connect-request'] ) ) {
386
				add_thickbox();
387
			}
388
			?>
389
			<style>
390
				.acch-title {
391
					margin: 0;
392
					padding: 0;
393
				}
394
395
				.acch-notice {
396
					display: table;
397
					width: 99%;
398
					position: relative;
399
					margin: 0;
400
					padding: 5px;
401
					border: 1px solid #ccc;
402
					border-radius: 3px;
403
				}
404
405
				.acch-float-left {
406
					display: table-cell;
407
					vertical-align: middle;
408
				}
409
410
				.acch-float-left svg {
411
					vertical-align: middle;
412
				}
413
414
				.acch-button {
415
					zoom: 1.3;
416
				}
417
418
			</style>
419
			<script>
420
				/**
421
				 * Ajax function to install and activate the AyeCode Connect plugin.
422
				 *
423
				 * @param $this
424
				 */
425
				function ayecode_connect_helper($this) {
426
					$connect_text = jQuery($this).text();
427
					$connecting_text = jQuery($this).data('connecting');
428
					$current_url = window.location.href + "&ayecode-connected=1";
429
					$current_url = $current_url.replace("&external-connect-request=true", ""); // strip external request param
430
					$current_url = $current_url.replace("&external-connect-request=1", ""); // strip external request param
431
432
					jQuery.ajax({
433
						type: "POST",
434
						url: ajaxurl,
435
						data: {
436
							action: 'ayecode_connect_helper',
437
							security: '<?php echo wp_create_nonce( 'ayecode-connect-helper' );?>',
438
							redirect_to: $current_url
439
						},
440
						beforeSend: function () {
441
							jQuery($this).html('<i class="fas fa-circle-notch fa-spin"></i> ' + $connecting_text).prop('disabled', true);// disable submit
442
						},
443
						success: function (data) {
444
							console.log(data);
445
							if (data.success == true && data.data.connect_url) {
446
								window.location.href = data.data.connect_url;
447
							} else if (data.success === false) {
448
								alert(data.data);
449
								jQuery($this).html($connect_text).prop('disabled', false);// enable submit
450
							}
451
						}
452
					});
453
				}
454
455
456
				<?php
457
				// add thickbox if external request is requested
458
				if(! empty( $_REQUEST['external-connect-request'] )) {
459
				?>
460
				jQuery(function () {
461
					setTimeout(function () {
462
						tb_show("AyeCode Connect", "?TB_inline?width=300&height=80&inlineId=ayecode-connect-helper-external-confirm");
463
					}, 200);
464
				});
465
				<?php
466
				}
467
				?>
468
469
			</script>
470
			<?php
471
		}
472
473
		/**
474
		 * Decide what pages to show on.
475
		 *
476
		 * @return bool
477
		 */
478
		public function maybe_show() {
479
			$show = false;
480
481
			// check if on a page set to show
482
			if ( isset( $_REQUEST['page'] ) && in_array( $_REQUEST['page'], $this->pages ) ) {
483
484
				// check if not active and connected
485
				if ( ! defined( 'AYECODE_CONNECT_VERSION' ) || ! get_option( 'ayecode_connect_blog_token' ) ) {
486
					$show = true;
487
				}
488
489
			}
490
491
			return $show;
492
		}
493
494
	}
495
496
}
497