Completed
Push — develop ( 1c83ad...4f9882 )
by Zack
26:36 queued 12:16
created

Plugin::is_compatible_wordpress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 10
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 1
		return is_multisite() && ( function_exists( 'is_plugin_active_for_network' ) && is_plugin_active_for_network( 'gravityview/gravityview.php' ) );
142
	}
143
144
	/**
145
	 * Include more legacy stuff.
146
	 *
147
	 * @param boolean $force Whether to force the includes.
148
	 *
149
	 * @return void
150
	 */
151
	public function include_legacy_frontend( $force = false ) {
152
153
		if ( gravityview()->request->is_admin() && ! $force ) {
154
			return;
155
		}
156
157
		include_once $this->dir( 'includes/class-gravityview-image.php' );
158
		include_once $this->dir( 'includes/class-template.php' );
159
		include_once $this->dir( 'includes/class-api.php' );
160
		include_once $this->dir( 'includes/class-frontend-views.php' );
161
		include_once $this->dir( 'includes/class-gravityview-change-entry-creator.php' );
162
163
		/**
164
		 * @action `gravityview_include_frontend_actions` Triggered after all GravityView frontend files are loaded
165
		 *
166
		 * @deprecated Use `gravityview/loaded` along with \GV\Request::is_admin(), etc.
167
		 *
168
		 * Nice place to insert extensions' frontend stuff
169
		 */
170
		do_action( 'gravityview_include_frontend_actions' );
171
	}
172
173
	/**
174
	 * Load more legacy core files.
175
	 *
176
	 * @return void
177
	 */
178
	public function include_legacy_core() {
179
180
		if ( ! class_exists( '\GravityView_Extension' ) ) {
181
			include_once $this->dir( 'includes/class-gravityview-extension.php' );
182
		}
183
184
		// Load fields
185
		include_once $this->dir( 'includes/fields/class-gravityview-fields.php' );
186
		include_once $this->dir( 'includes/fields/class-gravityview-field.php' );
187
188
		// Load all field files automatically
189
		foreach ( glob( $this->dir( 'includes/fields/class-gravityview-field*.php' ) ) as $gv_field_filename ) {
190
			include_once $gv_field_filename;
191
		}
192
193
		include_once $this->dir( 'includes/class-gravityview-entry-approval-status.php' );
194
		include_once $this->dir( 'includes/class-gravityview-entry-approval.php' );
195
196
		include_once $this->dir( 'includes/class-gravityview-entry-notes.php' );
197
		include_once $this->dir( 'includes/load-plugin-and-theme-hooks.php' );
198
199
		// Load Extensions
200
		// @todo: Convert to a scan of the directory or a method where this all lives
201
		include_once $this->dir( 'includes/extensions/edit-entry/class-edit-entry.php' );
202
		include_once $this->dir( 'includes/extensions/delete-entry/class-delete-entry.php' );
203
		include_once $this->dir( 'includes/extensions/entry-notes/class-gravityview-field-notes.php' );
204
205
		// Load WordPress Widgets
206
		include_once $this->dir( 'includes/wordpress-widgets/register-wordpress-widgets.php' );
207
208
		// Load GravityView Widgets
209
		include_once $this->dir( 'includes/widgets/register-gravityview-widgets.php' );
210
211
		// Add oEmbed
212
		include_once $this->dir( 'includes/class-api.php' );
213
		include_once $this->dir( 'includes/class-oembed.php' );
214
215
		// Add logging
216
		include_once $this->dir( 'includes/class-gravityview-logging.php' );
217
218
		include_once $this->dir( 'includes/class-ajax.php' );
219
		include_once $this->dir( 'includes/class-gravityview-html-elements.php' );
220
		include_once $this->dir( 'includes/class-frontend-views.php' );
221
		include_once $this->dir( 'includes/class-gravityview-admin-bar.php' );
222
		include_once $this->dir( 'includes/class-gravityview-entry-list.php' );
223
		include_once $this->dir( 'includes/class-gravityview-merge-tags.php'); /** @since 1.8.4 */
224
		include_once $this->dir( 'includes/class-data.php' );
225
		include_once $this->dir( 'includes/class-gravityview-shortcode.php' );
226
		include_once $this->dir( 'includes/class-gravityview-entry-link-shortcode.php' );
227
		include_once $this->dir( 'includes/class-gvlogic-shortcode.php' );
228
		include_once $this->dir( 'includes/presets/register-default-templates.php' );
229
230
		if ( class_exists( '\GFFormsModel' ) ) {
231
			include_once $this->dir( 'includes/class-gravityview-gfformsmodel.php' );
232
		}
233
	}
234
235
	/**
236
	 * Load the translations.
237
	 *
238
	 * Order of look-ups:
239
	 *
240
	 * 1. /wp-content/languages/plugins/gravityview-{locale}.mo (loaded by WordPress Core)
241
	 * 2. /wp-content/mu-plugins/gravityview-{locale}.mo
242
	 * 3. /wp-content/mu-plugins/languages/gravityview-{locale}.mo
243
	 * 4. /wp-content/plugins/gravityview/languages/gravityview-{locale}.mo
244
	 *
245
	 * @return void
246
	 */
247
	public function load_textdomain() {
248
249
		$domain = 'gravityview';
250
251
		// 1. /wp-content/languages/plugins/gravityview-{locale}.mo (loaded by WordPress Core)
252
		if ( is_textdomain_loaded( $domain ) ) {
253
			return;
254
		}
255
256
		// 2. /wp-content/languages/plugins/gravityview-{locale}.mo
257
		// 3. /wp-content/mu-plugins/plugins/languages/gravityview-{locale}.mo
258
		$loaded = load_muplugin_textdomain( $domain, '/languages/' );
259
260
		if ( $loaded ) {
261
			return;
262
		}
263
264
		// 4. /wp-content/plugins/gravityview/languages/gravityview-{locale}.mo
265
		$loaded = load_plugin_textdomain( $domain, false, $this->relpath( '/languages/' ) );
266
267
		if ( $loaded ) {
268
			return;
269
		}
270
271
		// Pre-4.6 loading
272
		// TODO: Remove when GV minimum version is WordPress 4.6.0
273
		$locale = apply_filters( 'plugin_locale', ( ( function_exists('get_user_locale') && is_admin() ) ? get_user_locale() : get_locale() ), 'gravityview' );
274
275
		$loaded = load_textdomain( 'gravityview', sprintf( '%s/%s-%s.mo', $this->dir('languages'), $domain, $locale ) );
276
277
		if( $loaded ) {
278
			return;
279
		}
280
281
		gravityview()->log->error( sprintf( 'Unable to load textdomain for %s locale.', $locale ) );
282
	}
283
284
	/**
285
	 * Register hooks that are fired when the plugin is activated and deactivated.
286
	 *
287
	 * @return void
288
	 */
289
	public function register_activation_hooks() {
290
		register_activation_hook( $this->dir( 'gravityview.php' ), array( $this, 'activate' ) );
291
		register_deactivation_hook( $this->dir( 'gravityview.php' ), array( $this, 'deactivate' ) );
292
	}
293
294
	/**
295
	 * Plugin activation function.
296
	 *
297
	 * @internal
298
	 * @return void
299
	 */
300 1
	public function activate() {
301 1
		gravityview();
302
303 1
		if ( ! $this->is_compatible() ) {
304
			return;
305
		}
306
307
		/** Register the gravityview post type upon WordPress core init. */
308 1
		require_once $this->dir( 'future/includes/class-gv-view.php' );
309 1
		View::register_post_type();
310
311
		/** Add the entry rewrite endpoint. */
312 1
		require_once $this->dir( 'future/includes/class-gv-entry.php' );
313 1
		Entry::add_rewrite_endpoint();
314
315
		/** Flush all URL rewrites. */
316 1
		flush_rewrite_rules();
317
318 1
		update_option( 'gv_version', self::$version );
319
320
		/** Add the transient to redirect to configuration page. */
321 1
		set_transient( '_gv_activation_redirect', true, 60 );
322
323
		/** Clear settings transient. */
324 1
		delete_transient( 'gravityview_edd-activate_valid' );
325
326 1
		\GravityView_Roles_Capabilities::get_instance()->add_caps();
327 1
	}
328
329
	/**
330
	 * Plugin deactivation function.
331
	 *
332
	 * @internal
333
	 * @return void
334
	 */
335
	public function deactivate() {
336
		flush_rewrite_rules();
337
	}
338
339
	/**
340
	 * Retrieve an absolute path within the GravityView plugin directory.
341
	 *
342
	 * @api
343
	 * @since 2.0
344
	 *
345
	 * @param string $path Optional. Append this extra path component.
346
	 * @return string The absolute path to the plugin directory.
347
	 */
348 109
	public function dir( $path = '' ) {
349 109
		return wp_normalize_path( GRAVITYVIEW_DIR . ltrim( $path, '/' ) );
350
	}
351
352
	/**
353
	 * Retrieve a relative path to the GravityView plugin directory from the WordPress plugin directory
354
	 *
355
	 * @api
356
	 * @since 2.2.3
357
	 *
358
	 * @param string $path Optional. Append this extra path component.
359
	 * @return string The relative path to the plugin directory from the plugin directory.
360
	 */
361 1
	public function relpath( $path = '' ) {
362
363 1
		$dirname = trailingslashit( dirname( plugin_basename( GRAVITYVIEW_FILE ) ) );
364
365 1
		return wp_normalize_path( $dirname . ltrim( $path, '/' ) );
366
	}
367
368
	/**
369
	 * Retrieve a URL within the GravityView plugin directory.
370
	 *
371
	 * @api
372
	 * @since 2.0
373
	 *
374
	 * @param string $path Optional. Extra path appended to the URL.
375
	 * @return string The URL to this plugin, with trailing slash.
376
	 */
377 1
	public function url( $path = '/' ) {
378 1
		return plugins_url( $path, $this->dir( 'gravityview.php' ) );
379
	}
380
381
	/**
382
	 * Is everything compatible with this version of GravityView?
383
	 *
384
	 * @api
385
	 * @since 2.0
386
	 *
387
	 * @return bool
388
	 */
389 4
	public function is_compatible() {
390
		return
391 4
			$this->is_compatible_php()
392 4
			&& $this->is_compatible_wordpress()
393 4
			&& $this->is_compatible_gravityforms();
394
	}
395
396
	/**
397
	 * Is this version of GravityView compatible with the current version of PHP?
398
	 *
399
	 * @api
400
	 * @since 2.0
401
	 *
402
	 * @return bool true if compatible, false otherwise.
403
	 */
404 4
	public function is_compatible_php() {
405 4
		return version_compare( $this->get_php_version(), self::$min_php_version, '>=' );
406
	}
407
408
	/**
409
	 * Is this version of GravityView compatible with the future required version of PHP?
410
	 *
411
	 * @api
412
	 * @since 2.0
413
	 *
414
	 * @return bool true if compatible, false otherwise.
415
	 */
416
	public function is_compatible_future_php() {
417
		return version_compare( $this->get_php_version(), self::$future_min_php_version, '>=' );
418
	}
419
420
	/**
421
	 * Is this version of GravityView compatible with the current version of WordPress?
422
	 *
423
	 * @api
424
	 * @since 2.0
425
	 *
426
	 * @param string $version Version to check against; otherwise uses GV_MIN_WP_VERSION
427
	 *
428
	 * @return bool true if compatible, false otherwise.
429
	 */
430 4
	public function is_compatible_wordpress( $version = null ) {
431
432 4
		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...
433 4
			$version = self::$min_wp_version;
434
		}
435
436 4
		return version_compare( $this->get_wordpress_version(), $version, '>=' );
437
	}
438
439
	/**
440
	 * Is this version of GravityView compatible with the current version of Gravity Forms?
441
	 *
442
	 * @api
443
	 * @since 2.0
444
	 *
445
	 * @return bool true if compatible, false otherwise (or not active/installed).
446
	 */
447 4
	public function is_compatible_gravityforms() {
448 4
		$version = $this->get_gravityforms_version();
449 4
		return $version ? version_compare( $version, self::$min_gf_version, '>=' ) : false;
450
	}
451
452
	/**
453
	 * Is this version of GravityView compatible with the future version of Gravity Forms?
454
	 *
455
	 * @api
456
	 * @since 2.0
457
	 *
458
	 * @return bool true if compatible, false otherwise (or not active/installed).
459
	 */
460
	public function is_compatible_future_gravityforms() {
461
		$version = $this->get_gravityforms_version();
462
		return $version ? version_compare( $version, self::$future_min_gf_version, '>=' ) : false;
463
	}
464
465
	/**
466
	 * Retrieve the current PHP version.
467
	 *
468
	 * Overridable with GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE during testing.
469
	 *
470
	 * @return string The version of PHP.
471
	 */
472 3
	private function get_php_version() {
473 3
		return ! empty( $GLOBALS['GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE'] ) ?
474 3
			$GLOBALS['GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE'] : phpversion();
475
	}
476
477
	/**
478
	 * Retrieve the current WordPress version.
479
	 *
480
	 * Overridable with GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE during testing.
481
	 *
482
	 * @return string The version of WordPress.
483
	 */
484 3
	private function get_wordpress_version() {
485 3
		return ! empty( $GLOBALS['GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE'] ) ?
486 3
			$GLOBALS['GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE'] : $GLOBALS['wp_version'];
487
	}
488
489
	/**
490
	 * Retrieve the current Gravity Forms version.
491
	 *
492
	 * Overridable with GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE during testing.
493
	 *
494
	 * @return string|null The version of Gravity Forms or null if inactive.
495
	 */
496 3
	private function get_gravityforms_version() {
497 3
		if ( ! class_exists( '\GFCommon' ) || ! empty( $GLOBALS['GRAVITYVIEW_TESTS_GF_INACTIVE_OVERRIDE'] ) ) {
498
			gravityview()->log->error( 'Gravity Forms is inactive or not installed.' );
499
			return null;
500
		}
501
502 3
		return ! empty( $GLOBALS['GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE'] ) ?
503 3
			$GLOBALS['GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE'] : \GFCommon::$version;
504
	}
505
506
	/**
507
	 * Feature support detection.
508
	 *
509
	 * @param string $feature Feature name. Check FEATURE_* class constants.
510
	 *
511
	 * @return boolean
512
	 */
513 176
	public function supports( $feature ) {
514 176
		if ( ! is_null( $supports = apply_filters( "gravityview/plugin/feature/$feature", null ) ) ) {
515
			return $supports;
516
		}
517
518
		switch ( $feature ):
519 176
				case self::FEATURE_GFQUERY:
520 62
					return class_exists( '\GF_Query' );
521 176
				case self::FEATURE_JOINS:
522 176
				case self::FEATURE_UNIONS:
523 160
					return apply_filters( 'gravityview/query/class', false ) === '\GF_Patched_Query';
524 176
				case self::FEATURE_REST:
525 176
					return class_exists( '\WP_REST_Controller' );
526
			default:
527
				return false;
528
		endswitch;
529
	}
530
531
	/**
532
	 * Delete GravityView Views, settings, roles, caps, etc.
533
	 *
534
	 * @return void
535
	 */
536
	public function uninstall() {
537
		global $wpdb;
538
539
		$suppress = $wpdb->suppress_errors();
540
541
		/**
542
		 * Posts.
543
		 */
544
		$items = get_posts( array(
545
			'post_type' => 'gravityview',
546
			'post_status' => 'any',
547
			'numberposts' => -1,
548
			'fields' => 'ids'
549
		) );
550
551
		foreach ( $items as $item ) {
552
			wp_delete_post( $item, true );
553
		}
554
555
		/**
556
		 * Meta.
557
		 */
558
		$tables = array();
559
560
		if ( version_compare( \GravityView_GFFormsModel::get_database_version(), '2.3-dev-1', '>=' ) ) {
561
			$tables []= \GFFormsModel::get_entry_meta_table_name();
562
		}
563
		$tables []= \GFFormsModel::get_lead_meta_table_name();
564
565
		foreach ( $tables as $meta_table ) {
566
			$sql = "
567
				DELETE FROM $meta_table
568
				WHERE (
569
					`meta_key` = 'is_approved'
570
				);
571
			";
572
			$wpdb->query( $sql );
573
		}
574
575
		/**
576
		 * Notes.
577
		 */
578
		$tables = array();
579
580
		if ( version_compare( \GravityView_GFFormsModel::get_database_version(), '2.3-dev-1', '>=' ) && method_exists( 'GFFormsModel', 'get_entry_notes_table_name' ) ) {
581
			$tables[] = \GFFormsModel::get_entry_notes_table_name();
582
		}
583
584
		$tables[] = \GFFormsModel::get_lead_notes_table_name();
585
586
		$disapproved = __('Disapproved the Entry for GravityView', 'gravityview');
587
		$approved = __('Approved the Entry for GravityView', 'gravityview');
588
589
		$suppress = $wpdb->suppress_errors();
590
		foreach ( $tables as $notes_table ) {
591
			$sql = $wpdb->prepare( "
592
				DELETE FROM $notes_table
593
				WHERE (
594
					`note_type` = 'gravityview' OR
595
					`value` = %s OR
596
					`value` = %s
597
				);
598
			", $approved, $disapproved );
599
			$wpdb->query( $sql );
600
		}
601
602
		$wpdb->suppress_errors( $suppress );
603
604
		/**
605
		 * Capabilities.
606
		 */
607
		\GravityView_Roles_Capabilities::get_instance()->remove_caps();
608
609
		/**
610
		 * Options.
611
		 */
612
		delete_option( 'gravityview_cache_blacklist' );
613
		delete_option( 'gv_version_upgraded_from' );
614
		delete_transient( 'gravityview_edd-activate_valid' );
615
		delete_transient( 'gravityview_edd-deactivate_valid' );
616
		delete_transient( 'gravityview_dismissed_notices' );
617
		delete_site_transient( 'gravityview_related_plugins' );
618
	}
619
620
	private function __clone() { }
621
622
	private function __wakeup() { }
623
}
624