GravityView  2.1.1
The best, easiest way to display Gravity Forms entries on your website.
class-gv-plugin.php
Go to the documentation of this file.
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 REST API functionality identifier.
86  */
87  const FEATURE_REST = 'rest_api';
88 
89  /**
90  * Get the global instance of \GV\Plugin.
91  *
92  * @return \GV\Plugin The global instance of GravityView Plugin.
93  */
94  public static function get() {
95  if ( ! self::$__instance instanceof self ) {
96  self::$__instance = new self;
97  }
98  return self::$__instance;
99  }
100 
101 
102  private function __construct() {
103  /**
104  * Load translations.
105  */
106  add_action( 'init', array( $this, 'load_textdomain' ) );
107 
108  /**
109  * Load some frontend-related legacy files.
110  */
111  add_action( 'gravityview/loaded', array( $this, 'include_legacy_frontend' ) );
112 
113  /**
114  * GFAddOn-backed settings, licensing.
115  */
116  add_action( 'plugins_loaded', array( $this, 'load_license_settings' ) );
117  }
118 
119  public function load_license_settings() {
120  require_once $this->dir( 'future/includes/class-gv-license-handler.php' );
121  require_once $this->dir( 'future/includes/class-gv-settings-addon.php' );
122  if ( class_exists( '\GV\Addon_Settings' ) ) {
123  $this->settings = new Addon_Settings();
124  include_once $this->dir( 'includes/class-gravityview-settings.php' );
125  } else {
126  gravityview()->log->notice( '\GV\Addon_Settings not loaded. Missing \GFAddOn.' );
127  }
128  }
129 
130  /**
131  * Check whether GravityView is network activated.
132  *
133  * @return bool Whether it's network activated or not.
134  */
135  public static function is_network_activated() {
136  return is_multisite() && ( function_exists( 'is_plugin_active_for_network' ) && is_plugin_active_for_network( 'gravityview/gravityview.php' ) );
137  }
138 
139  /**
140  * Include more legacy stuff.
141  *
142  * @param boolean $force Whether to force the includes.
143  *
144  * @return void
145  */
146  public function include_legacy_frontend( $force = false ) {
147 
148  if ( gravityview()->request->is_admin() && ! $force ) {
149  return;
150  }
151 
152  include_once $this->dir( 'includes/class-gravityview-image.php' );
153  include_once $this->dir( 'includes/class-template.php' );
154  include_once $this->dir( 'includes/class-api.php' );
155  include_once $this->dir( 'includes/class-frontend-views.php' );
156  include_once $this->dir( 'includes/class-gravityview-change-entry-creator.php' );
157 
158  /**
159  * @action `gravityview_include_frontend_actions` Triggered after all GravityView frontend files are loaded
160  *
161  * @deprecated Use `gravityview/loaded` along with \GV\Request::is_admin(), etc.
162  *
163  * Nice place to insert extensions' frontend stuff
164  */
165  do_action( 'gravityview_include_frontend_actions' );
166  }
167 
168  /**
169  * Load more legacy core files.
170  *
171  * @return void
172  */
173  public function include_legacy_core() {
174  // Load fields
175  include_once $this->dir( 'includes/fields/class-gravityview-fields.php' );
176  include_once $this->dir( 'includes/fields/class-gravityview-field.php' );
177 
178  // Load all field files automatically
179  foreach ( glob( $this->dir( 'includes/fields/class-gravityview-field*.php' ) ) as $gv_field_filename ) {
180  include_once $gv_field_filename;
181  }
182 
183  include_once $this->dir( 'includes/class-gravityview-entry-approval-status.php' );
184  include_once $this->dir( 'includes/class-gravityview-entry-approval.php' );
185 
186  include_once $this->dir( 'includes/class-gravityview-entry-notes.php' );
187  include_once $this->dir( 'includes/load-plugin-and-theme-hooks.php' );
188 
189  // Load Extensions
190  // @todo: Convert to a scan of the directory or a method where this all lives
191  include_once $this->dir( 'includes/extensions/edit-entry/class-edit-entry.php' );
192  include_once $this->dir( 'includes/extensions/delete-entry/class-delete-entry.php' );
193  include_once $this->dir( 'includes/extensions/entry-notes/class-gravityview-field-notes.php' );
194 
195  // Load WordPress Widgets
196  include_once $this->dir( 'includes/wordpress-widgets/register-wordpress-widgets.php' );
197 
198  // Load GravityView Widgets
199  include_once $this->dir( 'includes/widgets/register-gravityview-widgets.php' );
200 
201  // Add oEmbed
202  include_once $this->dir( 'includes/class-api.php' );
203  include_once $this->dir( 'includes/class-oembed.php' );
204 
205  // Add logging
206  include_once $this->dir( 'includes/class-gravityview-logging.php' );
207 
208  include_once $this->dir( 'includes/class-ajax.php' );
209  include_once $this->dir( 'includes/class-gravityview-html-elements.php' );
210  include_once $this->dir( 'includes/class-frontend-views.php' );
211  include_once $this->dir( 'includes/class-gravityview-admin-bar.php' );
212  include_once $this->dir( 'includes/class-gravityview-entry-list.php' );
213  include_once $this->dir( 'includes/class-gravityview-merge-tags.php'); /** @since 1.8.4 */
214  include_once $this->dir( 'includes/class-data.php' );
215  include_once $this->dir( 'includes/class-gravityview-shortcode.php' );
216  include_once $this->dir( 'includes/class-gravityview-entry-link-shortcode.php' );
217  include_once $this->dir( 'includes/class-gvlogic-shortcode.php' );
218  include_once $this->dir( 'includes/presets/register-default-templates.php' );
219 
220  if ( class_exists( '\GFFormsModel' ) ) {
221  include_once $this->dir( 'includes/class-gravityview-gfformsmodel.php' );
222  }
223 
224  if ( ! class_exists( '\GravityView_Extension' ) ) {
225  include_once $this->dir( 'includes/class-gravityview-extension.php' );
226  }
227  }
228 
229  /**
230  * Load the translations.
231  *
232  * @return void
233  */
234  public function load_textdomain() {
235  $loaded = load_plugin_textdomain( 'gravityview', false, $this->dir( 'languages' ) );
236 
237  if ( ! $loaded ) {
238  $loaded = load_muplugin_textdomain( 'gravityview', '/languages/' );
239  }
240  if ( ! $loaded ) {
241  $loaded = load_theme_textdomain( 'gravityview', '/languages/' );
242  }
243  if ( ! $loaded ) {
244 
245  $locale = get_locale();
246 
247  if ( function_exists('get_user_locale') && is_admin() ) {
248  $locale = get_user_locale();
249  }
250 
251  $locale = apply_filters( 'plugin_locale', $locale, 'gravityview' );
252  $mofile = $this->dir( 'languages' ) . '/gravityview-'. $locale .'.mo';
253  load_textdomain( 'gravityview', $mofile );
254  }
255  }
256 
257  /**
258  * Register hooks that are fired when the plugin is activated and deactivated.
259  *
260  * @return void
261  */
262  public function register_activation_hooks() {
263  register_activation_hook( $this->dir( 'gravityview.php' ), array( $this, 'activate' ) );
264  register_deactivation_hook( $this->dir( 'gravityview.php' ), array( $this, 'deactivate' ) );
265  }
266 
267  /**
268  * Plugin activation function.
269  *
270  * @internal
271  * @return void
272  */
273  public function activate() {
274  gravityview();
275 
276  /** Register the gravityview post type upon WordPress core init. */
277  require_once $this->dir( 'future/includes/class-gv-view.php' );
278  View::register_post_type();
279 
280  /** Add the entry rewrite endpoint. */
281  require_once $this->dir( 'future/includes/class-gv-entry.php' );
282  Entry::add_rewrite_endpoint();
283 
284  /** Flush all URL rewrites. */
285  flush_rewrite_rules();
286 
287  update_option( 'gv_version', self::$version );
288 
289  /** Add the transient to redirect to configuration page. */
290  set_transient( '_gv_activation_redirect', true, 60 );
291 
292  /** Clear settings transient. */
293  delete_transient( 'gravityview_edd-activate_valid' );
294 
296  }
297 
298  /**
299  * Plugin deactivation function.
300  *
301  * @internal
302  * @return void
303  */
304  public function deactivate() {
305  flush_rewrite_rules();
306  }
307 
308  /**
309  * Retrieve an absolute path within the Gravity Forms plugin directory.
310  *
311  * @api
312  * @since 2.0
313  *
314  * @param string $path Optional. Append this extra path component.
315  * @return string The absolute path to the plugin directory.
316  */
317  public function dir( $path = '' ) {
318  return GRAVITYVIEW_DIR . ltrim( $path, '/' );
319  }
320 
321  /**
322  * Retrieve a URL within the Gravity Forms plugin directory.
323  *
324  * @api
325  * @since 2.0
326  *
327  * @param string $path Optional. Extra path appended to the URL.
328  * @return string The URL to this plugin, with trailing slash.
329  */
330  public function url( $path = '/' ) {
331  return plugins_url( $path, $this->dir( 'gravityview.php' ) );
332  }
333 
334  /**
335  * Is everything compatible with this version of GravityView?
336  *
337  * @api
338  * @since 2.0
339  *
340  * @return bool
341  */
342  public function is_compatible() {
343  return
344  $this->is_compatible_php()
345  && $this->is_compatible_wordpress()
346  && $this->is_compatible_gravityforms();
347  }
348 
349  /**
350  * Is this version of GravityView compatible with the current version of PHP?
351  *
352  * @api
353  * @since 2.0
354  *
355  * @return bool true if compatible, false otherwise.
356  */
357  public function is_compatible_php() {
358  return version_compare( $this->get_php_version(), self::$min_php_version, '>=' );
359  }
360 
361  /**
362  * Is this version of GravityView compatible with the future required version of PHP?
363  *
364  * @api
365  * @since 2.0
366  *
367  * @return bool true if compatible, false otherwise.
368  */
369  public function is_compatible_future_php() {
370  return version_compare( $this->get_php_version(), self::$future_min_php_version, '>=' );
371  }
372 
373  /**
374  * Is this version of GravityView compatible with the current version of WordPress?
375  *
376  * @api
377  * @since 2.0
378  *
379  * @param string $version Version to check against; otherwise uses GV_MIN_WP_VERSION
380  *
381  * @return bool true if compatible, false otherwise.
382  */
383  public function is_compatible_wordpress( $version = null ) {
384 
385  if( ! $version ) {
386  $version = self::$min_wp_version;
387  }
388 
389  return version_compare( $this->get_wordpress_version(), $version, '>=' );
390  }
391 
392  /**
393  * Is this version of GravityView compatible with the current version of Gravity Forms?
394  *
395  * @api
396  * @since 2.0
397  *
398  * @return bool true if compatible, false otherwise (or not active/installed).
399  */
400  public function is_compatible_gravityforms() {
401  $version = $this->get_gravityforms_version();
402  return $version ? version_compare( $version, self::$min_gf_version, '>=' ) : false;
403  }
404 
405  /**
406  * Is this version of GravityView compatible with the future version of Gravity Forms?
407  *
408  * @api
409  * @since 2.0
410  *
411  * @return bool true if compatible, false otherwise (or not active/installed).
412  */
414  $version = $this->get_gravityforms_version();
415  return $version ? version_compare( $version, self::$future_min_gf_version, '>=' ) : false;
416  }
417 
418  /**
419  * Retrieve the current PHP version.
420  *
421  * Overridable with GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE during testing.
422  *
423  * @return string The version of PHP.
424  */
425  private function get_php_version() {
426  return ! empty( $GLOBALS['GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE'] ) ?
427  $GLOBALS['GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE'] : phpversion();
428  }
429 
430  /**
431  * Retrieve the current WordPress version.
432  *
433  * Overridable with GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE during testing.
434  *
435  * @return string The version of WordPress.
436  */
437  private function get_wordpress_version() {
438  return ! empty( $GLOBALS['GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE'] ) ?
439  $GLOBALS['GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE'] : $GLOBALS['wp_version'];
440  }
441 
442  /**
443  * Retrieve the current Gravity Forms version.
444  *
445  * Overridable with GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE during testing.
446  *
447  * @return string|null The version of Gravity Forms or null if inactive.
448  */
449  private function get_gravityforms_version() {
450  if ( ! class_exists( '\GFCommon' ) || ! empty( $GLOBALS['GRAVITYVIEW_TESTS_GF_INACTIVE_OVERRIDE'] ) ) {
451  gravityview()->log->error( 'Gravity Forms is inactive or not installed.' );
452  return null;
453  }
454 
455  return ! empty( $GLOBALS['GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE'] ) ?
456  $GLOBALS['GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE'] : \GFCommon::$version;
457  }
458 
459  /**
460  * Feature support detection.
461  *
462  * @param string $feature Feature name. Check FEATURE_* class constants.
463  *
464  * @return boolean
465  */
466  public function supports( $feature ) {
467  if ( ! is_null( $supports = apply_filters( "gravityview/plugin/feature/$feature", null ) ) ) {
468  return $supports;
469  }
470 
471  switch ( $feature ):
472  case self::FEATURE_GFQUERY:
473  case self::FEATURE_JOINS:
474  return class_exists( '\GF_Query' );
475  case self::FEATURE_REST:
476  return class_exists( '\WP_REST_Controller' );
477  default:
478  return false;
479  endswitch;
480  }
481 
482  /**
483  * Delete GravityView Views, settings, roles, caps, etc.
484  *
485  * @return void
486  */
487  public function uninstall() {
488  global $wpdb;
489 
490  $suppress = $wpdb->suppress_errors();
491 
492  /**
493  * Posts.
494  */
495  $items = get_posts( array(
496  'post_type' => 'gravityview',
497  'post_status' => 'any',
498  'numberposts' => -1,
499  'fields' => 'ids'
500  ) );
501 
502  foreach ( $items as $item ) {
503  wp_delete_post( $item, true );
504  }
505 
506  /**
507  * Meta.
508  */
509  $tables = array();
510 
511  if ( version_compare( \GravityView_GFFormsModel::get_database_version(), '2.3-dev-1', '>=' ) ) {
512  $tables []= \GFFormsModel::get_entry_meta_table_name();
513  }
514  $tables []= \GFFormsModel::get_lead_meta_table_name();
515 
516  foreach ( $tables as $meta_table ) {
517  $sql = "
518  DELETE FROM $meta_table
519  WHERE (
520  `meta_key` = 'is_approved'
521  );
522  ";
523  $wpdb->query( $sql );
524  }
525 
526  /**
527  * Notes.
528  */
529  $tables = array();
530 
531  if ( version_compare( \GravityView_GFFormsModel::get_database_version(), '2.3-dev-1', '>=' ) && method_exists( 'GFFormsModel', 'get_entry_notes_table_name' ) ) {
532  $tables[] = \GFFormsModel::get_entry_notes_table_name();
533  }
534 
535  $tables[] = \GFFormsModel::get_lead_notes_table_name();
536 
537  $disapproved = __('Disapproved the Entry for GravityView', 'gravityview');
538  $approved = __('Approved the Entry for GravityView', 'gravityview');
539 
540  $suppress = $wpdb->suppress_errors();
541  foreach ( $tables as $notes_table ) {
542  $sql = $wpdb->prepare( "
543  DELETE FROM $notes_table
544  WHERE (
545  `note_type` = 'gravityview' OR
546  `value` = %s OR
547  `value` = %s
548  );
549  ", $approved, $disapproved );
550  $wpdb->query( $sql );
551  }
552 
553  $wpdb->suppress_errors( $suppress );
554 
555  /**
556  * Capabilities.
557  */
559 
560  /**
561  * Options.
562  */
563  delete_option( 'gravityview_cache_blacklist' );
564  delete_option( 'gv_version_upgraded_from' );
565  delete_transient( 'gravityview_edd-activate_valid' );
566  delete_transient( 'gravityview_edd-deactivate_valid' );
567  delete_transient( 'gravityview_dismissed_notices' );
568  delete_site_transient( 'gravityview_related_plugins' );
569  }
570 
571  private function __clone() { }
572 
573  private function __wakeup() { }
574 }
const GRAVITYVIEW_DIR
"GRAVITYVIEW_DIR" "./" The absolute path to the plugin directory, with trailing slash ...
Definition: gravityview.php:40
deactivate()
Plugin deactivation function.
If this file is called directly, abort.
get_php_version()
Retrieve the current PHP version.
uninstall()
Delete GravityView Views, settings, roles, caps, etc.
is_compatible_future_gravityforms()
Is this version of GravityView compatible with the future version of Gravity Forms?
get_wordpress_version()
Retrieve the current WordPress version.
static is_network_activated()
Check whether GravityView is network activated.
is_compatible_wordpress( $version=null)
Is this version of GravityView compatible with the current version of WordPress?
is_compatible_gravityforms()
Is this version of GravityView compatible with the current version of Gravity Forms?
is_compatible_future_php()
Is this version of GravityView compatible with the future required version of PHP?
gravityview()
Definition: _stubs.php:26
get_gravityforms_version()
Retrieve the current Gravity Forms version.
include_legacy_core()
Load more legacy core files.
dir( $path='')
Retrieve an absolute path within the Gravity Forms plugin directory.
const GV_PLUGIN_VERSION(! defined( 'ABSPATH'))
Plugin Name: GravityView Plugin URI: https://gravityview.co Description: The best, easiest way to display Gravity Forms entries on your website.
Definition: gravityview.php:25
const GV_FUTURE_MIN_PHP_VERSION
Definition: gravityview.php:64
If this file is called directly, abort.
const GV_FUTURE_MIN_GF_VERSION
GravityView will soon require at least this version of Gravity Forms to function properly.
Definition: gravityview.php:70
supports( $feature)
Feature support detection.
register_activation_hooks()
Register hooks that are fired when the plugin is activated and deactivated.
is_compatible()
Is everything compatible with this version of GravityView?
include_legacy_frontend( $force=false)
Include more legacy stuff.
is_compatible_php()
Is this version of GravityView compatible with the current version of PHP?
static get_database_version()
Make sure the method exists, regardless of GF version.
activate()
Plugin activation function.
url( $path='/')
Retrieve a URL within the Gravity Forms plugin directory.
load_textdomain()
Load the translations.
const GV_MIN_GF_VERSION
GravityView requires at least this version of Gravity Forms to function properly. ...
Definition: gravityview.php:45
const GV_MIN_PHP_VERSION
GravityView requires at least this version of PHP to function properly.
Definition: gravityview.php:57
const GV_MIN_WP_VERSION
GravityView requires at least this version of WordPress to function properly.
Definition: gravityview.php:51