Completed
Pull Request — develop (#1484)
by Zack
07:48
created

Plugin::activate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2.0014

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 28
ccs 13
cts 14
cp 0.9286
crap 2.0014
rs 9.472
c 0
b 0
f 0
1
<?php
2
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * The GravityView WordPress plugin class.
11
 *
12
 * Contains functionality related to GravityView being
13
 * a WordPress plugin and doing WordPress pluginy things.
14
 *
15
 * Accessible via gravityview()->plugin
16
 */
17
final class Plugin {
18
	/**
19
	 * @var string The plugin version.
20
	 *
21
	 * @api
22
	 * @since 2.0
23
	 */
24
	public static $version = GV_PLUGIN_VERSION;
25
26
	/**
27
	 * @var string Minimum WordPress version.
28
	 *
29
	 * GravityView requires at least this version of WordPress to function properly.
30
	 */
31
	private static $min_wp_version = GV_MIN_WP_VERSION;
32
33
	/**
34
	 * @var string Minimum Gravity Forms version.
35
	 *
36
	 * GravityView requires at least this version of Gravity Forms to function properly.
37
	 */
38
	public static $min_gf_version = GV_MIN_GF_VERSION;
39
40
	/**
41
	 * @var string Minimum PHP version.
42
	 *
43
	 * GravityView requires at least this version of PHP to function properly.
44
	 */
45
	private static $min_php_version = GV_MIN_PHP_VERSION;
46
47
	/**
48
	 * @var string|bool Minimum future PHP version.
49
	 *
50
	 * GravityView will require this version of PHP soon. False if no future PHP version changes are planned.
51
	 */
52
	private static $future_min_php_version = GV_FUTURE_MIN_PHP_VERSION;
53
54
	/**
55
	 * @var string|bool Minimum future Gravity Forms version.
56
	 *
57
	 * GravityView will require this version of Gravity Forms soon. False if no future Gravity Forms version changes are planned.
58
	 */
59
	private static $future_min_gf_version = GV_FUTURE_MIN_GF_VERSION;
60
61
	/**
62
	 * @var \GV\Plugin The \GV\Plugin static instance.
63
	 */
64
	private static $__instance = null;
65
66
	/**
67
	 * @var \GV\Addon_Settings The plugin "addon" settings.
68
	 *
69
	 * @api
70
	 * @since 2.0
71
	 */
72
	public $settings;
73
74
	/**
75
	 * @var string The GFQuery functionality identifier.
76
	 */
77
	const FEATURE_GFQUERY = 'gfquery';
78
79
	/**
80
	 * @var string The joins functionality identifier.
81
	 */
82
	const FEATURE_JOINS = 'joins';
83
84
	/**
85
	 * @var string The unions functionality identifier.
86
	 */
87
	const FEATURE_UNIONS = 'unions';
88
89
	/**
90
	 * @var string The REST API functionality identifier.
91
	 */
92
	const FEATURE_REST  = 'rest_api';
93
94
	/**
95
	 * Get the global instance of \GV\Plugin.
96
	 *
97
	 * @return \GV\Plugin The global instance of GravityView Plugin.
98
	 */
99
	public static function get() {
100
		if ( ! self::$__instance instanceof self ) {
101
			self::$__instance = new self;
102
		}
103
		return self::$__instance;
104
	}
105
106
107
	private function __construct() {
108
		/**
109
		 * Load translations.
110
		 */
111
		add_action( 'init', array( $this, 'load_textdomain' ) );
112
113
		/**
114
		 * Load some frontend-related legacy files.
115
		 */
116
		add_action( 'gravityview/loaded', array( $this, 'include_legacy_frontend' ) );
117
118
		/**
119
		 * GFAddOn-backed settings, licensing.
120
		 */
121
		add_action( 'plugins_loaded', array( $this, 'load_license_settings' ) );
122
	}
123
124
	public function load_license_settings() {
125
		require_once $this->dir( 'future/includes/class-gv-license-handler.php' );
126
		require_once $this->dir( 'future/includes/class-gv-settings-addon.php' );
127
		if ( class_exists( '\GV\Addon_Settings' ) ) {
128
			$this->settings = new Addon_Settings();
129
			include_once $this->dir( 'includes/class-gravityview-settings.php' );
130
		} else {
131
			gravityview()->log->notice( '\GV\Addon_Settings not loaded. Missing \GFAddOn.' );
132
		}
133
	}
134
135
	/**
136
	 * Check whether GravityView is network activated.
137
	 *
138
	 * @return bool Whether it's network activated or not.
139
	 */
140 1
	public static function is_network_activated() {
141
142 1
		$plugin_basename = plugin_basename( GRAVITYVIEW_FILE );
143
144 1
		return is_multisite() && ( function_exists( 'is_plugin_active_for_network' ) && is_plugin_active_for_network( $plugin_basename ) );
145
	}
146
147
	/**
148
	 * Include more legacy stuff.
149
	 *
150
	 * @param boolean $force Whether to force the includes.
151
	 *
152
	 * @return void
153
	 */
154
	public function include_legacy_frontend( $force = false ) {
155
156
		if ( gravityview()->request->is_admin() && ! $force ) {
157
			return;
158
		}
159
160
		include_once $this->dir( 'includes/class-gravityview-image.php' );
161
		include_once $this->dir( 'includes/class-template.php' );
162
		include_once $this->dir( 'includes/class-api.php' );
163
		include_once $this->dir( 'includes/class-frontend-views.php' );
164
		include_once $this->dir( 'includes/class-gravityview-change-entry-creator.php' );
165
166
		/**
167
		 * @action `gravityview_include_frontend_actions` Triggered after all GravityView frontend files are loaded
168
		 *
169
		 * @deprecated Use `gravityview/loaded` along with \GV\Request::is_admin(), etc.
170
		 *
171
		 * Nice place to insert extensions' frontend stuff
172
		 */
173
		do_action( 'gravityview_include_frontend_actions' );
174
	}
175
176
	/**
177
	 * Load more legacy core files.
178
	 *
179
	 * @return void
180
	 */
181
	public function include_legacy_core() {
182
183
		if ( ! class_exists( '\GravityView_Extension' ) ) {
184
			include_once $this->dir( 'includes/class-gravityview-extension.php' );
185
		}
186
187
		// Load fields
188
		include_once $this->dir( 'includes/fields/class-gravityview-fields.php' );
189
		include_once $this->dir( 'includes/fields/class-gravityview-field.php' );
190
191
		// Load all field files automatically
192
		foreach ( glob( $this->dir( 'includes/fields/class-gravityview-field*.php' ) ) as $gv_field_filename ) {
193
			include_once $gv_field_filename;
194
		}
195
196
		include_once $this->dir( 'includes/class-gravityview-entry-approval-status.php' );
197
		include_once $this->dir( 'includes/class-gravityview-entry-approval.php' );
198
199
		include_once $this->dir( 'includes/class-gravityview-entry-notes.php' );
200
		include_once $this->dir( 'includes/load-plugin-and-theme-hooks.php' );
201
202
		// Load Extensions
203
		// @todo: Convert to a scan of the directory or a method where this all lives
204
		include_once $this->dir( 'includes/extensions/edit-entry/class-edit-entry.php' );
205
		include_once $this->dir( 'includes/extensions/delete-entry/class-delete-entry.php' );
206
		include_once $this->dir( 'includes/extensions/duplicate-entry/class-duplicate-entry.php' );
207
		include_once $this->dir( 'includes/extensions/entry-notes/class-gravityview-field-notes.php' );
208
		include_once $this->dir( 'includes/extensions/lightbox/class-gravityview-lightbox.php' );
209
210
		// Load WordPress Widgets
211
		include_once $this->dir( 'includes/wordpress-widgets/register-wordpress-widgets.php' );
212
213
		// Load GravityView Widgets
214
		include_once $this->dir( 'includes/widgets/register-gravityview-widgets.php' );
215
216
		// Add oEmbed
217
		include_once $this->dir( 'includes/class-api.php' );
218
		include_once $this->dir( 'includes/class-oembed.php' );
219
220
		// Add logging
221
		include_once $this->dir( 'includes/class-gravityview-logging.php' );
222
223
		include_once $this->dir( 'includes/class-ajax.php' );
224
		include_once $this->dir( 'includes/class-gravityview-html-elements.php' );
225
		include_once $this->dir( 'includes/class-frontend-views.php' );
226
		include_once $this->dir( 'includes/class-gravityview-admin-bar.php' );
227
		include_once $this->dir( 'includes/class-gravityview-entry-list.php' );
228
		include_once $this->dir( 'includes/class-gravityview-merge-tags.php'); /** @since 1.8.4 */
229
		include_once $this->dir( 'includes/class-data.php' );
230
		include_once $this->dir( 'includes/class-gravityview-shortcode.php' );
231
		include_once $this->dir( 'includes/class-gravityview-entry-link-shortcode.php' );
232
		include_once $this->dir( 'includes/class-gvlogic-shortcode.php' );
233
		include_once $this->dir( 'includes/presets/register-default-templates.php' );
234
235
		if ( class_exists( '\GFFormsModel' ) ) {
236
			include_once $this->dir( 'includes/class-gravityview-gfformsmodel.php' );
237
		}
238
	}
239
240
	/**
241
	 * Load the translations.
242
	 *
243
	 * Order of look-ups:
244
	 *
245
	 * 1. /wp-content/languages/plugins/gravityview-{locale}.mo (loaded by WordPress Core)
246
	 * 2. /wp-content/mu-plugins/gravityview-{locale}.mo
247
	 * 3. /wp-content/mu-plugins/languages/gravityview-{locale}.mo
248
	 * 4. /wp-content/plugins/gravityview/languages/gravityview-{locale}.mo
249
	 *
250
	 * @return void
251
	 */
252
	public function load_textdomain() {
253
254
		$domain = 'gravityview';
255
256
		// 1. /wp-content/languages/plugins/gravityview-{locale}.mo (loaded by WordPress Core)
257
		if ( is_textdomain_loaded( $domain ) ) {
258
			return;
259
		}
260
261
		// 2. /wp-content/languages/plugins/gravityview-{locale}.mo
262
		// 3. /wp-content/mu-plugins/plugins/languages/gravityview-{locale}.mo
263
		$loaded = load_muplugin_textdomain( $domain, '/languages/' );
264
265
		if ( $loaded ) {
266
			return;
267
		}
268
269
		// 4. /wp-content/plugins/gravityview/languages/gravityview-{locale}.mo
270
		$loaded = load_plugin_textdomain( $domain, false, $this->relpath( '/languages/' ) );
271
272
		if ( $loaded ) {
273
			return;
274
		}
275
276
		// Pre-4.6 loading
277
		// TODO: Remove when GV minimum version is WordPress 4.6.0
278
		$locale = apply_filters( 'plugin_locale', ( ( function_exists('get_user_locale') && is_admin() ) ? get_user_locale() : get_locale() ), 'gravityview' );
279
280
		$loaded = load_textdomain( 'gravityview', sprintf( '%s/%s-%s.mo', $this->dir('languages'), $domain, $locale ) );
281
282
		if( $loaded ) {
283
			return;
284
		}
285
286
		gravityview()->log->error( sprintf( 'Unable to load textdomain for %s locale.', $locale ) );
287
	}
288
289
	/**
290
	 * Register hooks that are fired when the plugin is activated and deactivated.
291
	 *
292
	 * @return void
293
	 */
294
	public function register_activation_hooks() {
295
		register_activation_hook( $this->dir( 'gravityview.php' ), array( $this, 'activate' ) );
296
		register_deactivation_hook( $this->dir( 'gravityview.php' ), array( $this, 'deactivate' ) );
297
	}
298
299
	/**
300
	 * Plugin activation function.
301
	 *
302
	 * @internal
303
	 * @return void
304
	 */
305 1
	public function activate() {
306 1
		gravityview();
307
308 1
		if ( ! $this->is_compatible() ) {
309
			return;
310
		}
311
312
		/** Register the gravityview post type upon WordPress core init. */
313 1
		require_once $this->dir( 'future/includes/class-gv-view.php' );
314 1
		View::register_post_type();
315
316
		/** Add the entry rewrite endpoint. */
317 1
		require_once $this->dir( 'future/includes/class-gv-entry.php' );
318 1
		Entry::add_rewrite_endpoint();
319
320
		/** Flush all URL rewrites. */
321 1
		flush_rewrite_rules();
322
323 1
		update_option( 'gv_version', self::$version );
324
325
		/** Add the transient to redirect to configuration page. */
326 1
		set_transient( '_gv_activation_redirect', true, 60 );
327
328
		/** Clear settings transient. */
329 1
		delete_transient( 'gravityview_edd-activate_valid' );
330
331 1
		\GravityView_Roles_Capabilities::get_instance()->add_caps();
332 1
	}
333
334
	/**
335
	 * Plugin deactivation function.
336
	 *
337
	 * @internal
338
	 * @return void
339
	 */
340
	public function deactivate() {
341
		flush_rewrite_rules();
342
	}
343
344
	/**
345
	 * Retrieve an absolute path within the GravityView plugin directory.
346
	 *
347
	 * @api
348
	 * @since 2.0
349
	 *
350
	 * @param string $path Optional. Append this extra path component.
351
	 * @return string The absolute path to the plugin directory.
352
	 */
353 114
	public function dir( $path = '' ) {
354 114
		return wp_normalize_path( GRAVITYVIEW_DIR . ltrim( $path, '/' ) );
355
	}
356
357
	/**
358
	 * Retrieve a relative path to the GravityView plugin directory from the WordPress plugin directory
359
	 *
360
	 * @api
361
	 * @since 2.2.3
362
	 *
363
	 * @param string $path Optional. Append this extra path component.
364
	 * @return string The relative path to the plugin directory from the plugin directory.
365
	 */
366 1
	public function relpath( $path = '' ) {
367
368 1
		$dirname = trailingslashit( dirname( plugin_basename( GRAVITYVIEW_FILE ) ) );
369
370 1
		return wp_normalize_path( $dirname . ltrim( $path, '/' ) );
371
	}
372
373
	/**
374
	 * Retrieve a URL within the GravityView plugin directory.
375
	 *
376
	 * @api
377
	 * @since 2.0
378
	 *
379
	 * @param string $path Optional. Extra path appended to the URL.
380
	 * @return string The URL to this plugin, with trailing slash.
381
	 */
382 1
	public function url( $path = '/' ) {
383 1
		return plugins_url( $path, $this->dir( 'gravityview.php' ) );
384
	}
385
386
	/**
387
	 * Is everything compatible with this version of GravityView?
388
	 *
389
	 * @api
390
	 * @since 2.0
391
	 *
392
	 * @return bool
393
	 */
394 5
	public function is_compatible() {
395
		return
396 5
			$this->is_compatible_php()
397 5
			&& $this->is_compatible_wordpress()
398 5
			&& $this->is_compatible_gravityforms();
399
	}
400
401
	/**
402
	 * Is this version of GravityView compatible with the current version of PHP?
403
	 *
404
	 * @api
405
	 * @since 2.0
406
	 *
407
	 * @return bool true if compatible, false otherwise.
408
	 */
409 5
	public function is_compatible_php() {
410 5
		return version_compare( $this->get_php_version(), self::$min_php_version, '>=' );
411
	}
412
413
	/**
414
	 * Is this version of GravityView compatible with the future required version of PHP?
415
	 *
416
	 * @api
417
	 * @since 2.0
418
	 *
419
	 * @return bool true if compatible, false otherwise.
420
	 */
421
	public function is_compatible_future_php() {
422
		return version_compare( $this->get_php_version(), self::$future_min_php_version, '>=' );
423
	}
424
425
	/**
426
	 * Is this version of GravityView compatible with the current version of WordPress?
427
	 *
428
	 * @api
429
	 * @since 2.0
430
	 *
431
	 * @param string $version Version to check against; otherwise uses GV_MIN_WP_VERSION
432
	 *
433
	 * @return bool true if compatible, false otherwise.
434
	 */
435 5
	public function is_compatible_wordpress( $version = null ) {
436
437 5
		if( ! $version ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $version of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
438 5
			$version = self::$min_wp_version;
439
		}
440
441 5
		return version_compare( $this->get_wordpress_version(), $version, '>=' );
442
	}
443
444
	/**
445
	 * Is this version of GravityView compatible with the current version of Gravity Forms?
446
	 *
447
	 * @api
448
	 * @since 2.0
449
	 *
450
	 * @return bool true if compatible, false otherwise (or not active/installed).
451
	 */
452 5
	public function is_compatible_gravityforms() {
453 5
		$version = $this->get_gravityforms_version();
454 5
		return $version ? version_compare( $version, self::$min_gf_version, '>=' ) : false;
455
	}
456
457
	/**
458
	 * Is this version of GravityView compatible with the future version of Gravity Forms?
459
	 *
460
	 * @api
461
	 * @since 2.0
462
	 *
463
	 * @return bool true if compatible, false otherwise (or not active/installed).
464
	 */
465
	public function is_compatible_future_gravityforms() {
466
		$version = $this->get_gravityforms_version();
467
		return $version ? version_compare( $version, self::$future_min_gf_version, '>=' ) : false;
468
	}
469
470
	/**
471
	 * Retrieve the current PHP version.
472
	 *
473
	 * Overridable with GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE during testing.
474
	 *
475
	 * @return string The version of PHP.
476
	 */
477 4
	private function get_php_version() {
478 4
		return ! empty( $GLOBALS['GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE'] ) ?
479 4
			$GLOBALS['GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE'] : phpversion();
480
	}
481
482
	/**
483
	 * Retrieve the current WordPress version.
484
	 *
485
	 * Overridable with GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE during testing.
486
	 *
487
	 * @return string The version of WordPress.
488
	 */
489 4
	private function get_wordpress_version() {
490 4
		return ! empty( $GLOBALS['GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE'] ) ?
491 4
			$GLOBALS['GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE'] : $GLOBALS['wp_version'];
492
	}
493
494
	/**
495
	 * Retrieve the current Gravity Forms version.
496
	 *
497
	 * Overridable with GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE during testing.
498
	 *
499
	 * @return string|null The version of Gravity Forms or null if inactive.
500
	 */
501 4
	private function get_gravityforms_version() {
502 4
		if ( ! class_exists( '\GFCommon' ) || ! empty( $GLOBALS['GRAVITYVIEW_TESTS_GF_INACTIVE_OVERRIDE'] ) ) {
503
			gravityview()->log->error( 'Gravity Forms is inactive or not installed.' );
504
			return null;
505
		}
506
507 4
		return ! empty( $GLOBALS['GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE'] ) ?
508 4
			$GLOBALS['GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE'] : \GFCommon::$version;
509
	}
510
511
	/**
512
	 * Feature support detection.
513
	 *
514
	 * @param string $feature Feature name. Check FEATURE_* class constants.
515
	 *
516
	 * @return boolean
517
	 */
518 194
	public function supports( $feature ) {
519 194
		if ( ! is_null( $supports = apply_filters( "gravityview/plugin/feature/$feature", null ) ) ) {
520
			return $supports;
521
		}
522
523
		switch ( $feature ):
524 194
				case self::FEATURE_GFQUERY:
525 74
					return class_exists( '\GF_Query' );
526 194
				case self::FEATURE_JOINS:
527 194
				case self::FEATURE_UNIONS:
528 181
					return apply_filters( 'gravityview/query/class', false ) === '\GF_Patched_Query';
529 194
				case self::FEATURE_REST:
530 194
					return class_exists( '\WP_REST_Controller' );
531
			default:
532
				return false;
533
		endswitch;
534
	}
535
536
	/**
537
	 * Delete GravityView Views, settings, roles, caps, etc.
538
	 *
539
	 * @return void
540
	 */
541
	public function uninstall() {
542
		global $wpdb;
543
544
		$suppress = $wpdb->suppress_errors();
545
546
		/**
547
		 * Posts.
548
		 */
549
		$items = get_posts( array(
550
			'post_type' => 'gravityview',
551
			'post_status' => 'any',
552
			'numberposts' => -1,
553
			'fields' => 'ids'
554
		) );
555
556
		foreach ( $items as $item ) {
557
			wp_delete_post( $item, true );
558
		}
559
560
		/**
561
		 * Meta.
562
		 */
563
		$tables = array();
564
565
		if ( version_compare( \GravityView_GFFormsModel::get_database_version(), '2.3-dev-1', '>=' ) ) {
566
			$tables []= \GFFormsModel::get_entry_meta_table_name();
567
		}
568
		$tables []= \GFFormsModel::get_lead_meta_table_name();
569
570
		foreach ( $tables as $meta_table ) {
571
			$sql = "
572
				DELETE FROM $meta_table
573
				WHERE (
574
					`meta_key` = 'is_approved'
575
				);
576
			";
577
			$wpdb->query( $sql );
578
		}
579
580
		/**
581
		 * Notes.
582
		 */
583
		$tables = array();
584
585
		if ( version_compare( \GravityView_GFFormsModel::get_database_version(), '2.3-dev-1', '>=' ) && method_exists( 'GFFormsModel', 'get_entry_notes_table_name' ) ) {
586
			$tables[] = \GFFormsModel::get_entry_notes_table_name();
587
		}
588
589
		$tables[] = \GFFormsModel::get_lead_notes_table_name();
590
591
		$disapproved = __('Disapproved the Entry for GravityView', 'gravityview');
592
		$approved = __('Approved the Entry for GravityView', 'gravityview');
593
594
		$suppress = $wpdb->suppress_errors();
595
		foreach ( $tables as $notes_table ) {
596
			$sql = $wpdb->prepare( "
597
				DELETE FROM $notes_table
598
				WHERE (
599
					`note_type` = 'gravityview' OR
600
					`value` = %s OR
601
					`value` = %s
602
				);
603
			", $approved, $disapproved );
604
			$wpdb->query( $sql );
605
		}
606
607
		$wpdb->suppress_errors( $suppress );
608
609
		/**
610
		 * Capabilities.
611
		 */
612
		\GravityView_Roles_Capabilities::get_instance()->remove_caps();
613
614
		/**
615
		 * Options.
616
		 */
617
		delete_option( 'gravityview_cache_blacklist' );
618
		delete_option( 'gv_version_upgraded_from' );
619
		delete_transient( 'gravityview_edd-activate_valid' );
620
		delete_transient( 'gravityview_edd-deactivate_valid' );
621
		delete_transient( 'gravityview_dismissed_notices' );
622
		delete_site_transient( 'gravityview_related_plugins' );
623
	}
624
625
	private function __clone() { }
626
627
	private function __wakeup() { }
628
}
629