GravityView  2.10.1
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-compatibility.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Handle issues with plugin and version compatibility
4  *
5  * @package GravityView
6  * @license GPL2+
7  * @author GravityView <[email protected]>
8  * @link http://gravityview.co
9  * @copyright Copyright 2015, Katz Web Services, Inc.
10  *
11  * @since 1.12
12  */
13 
14 /**
15  * Handle GravityView compatibility notices and fallback shortcodes
16  * @since 1.12
17  */
19 
20  /**
21  * @var GravityView_Compatibility
22  */
23  static public $instance = null;
24 
25  /**
26  * @var bool Is Gravity Forms version valid and is Gravity Forms loaded?
27  */
28  static public $valid_gravity_forms = false;
29 
30  /**
31  * @var bool Is the WordPress installation compatible?
32  */
33  static public $valid_wordpress = false;
34 
35  /**
36  * @var bool Is the server's PHP version compatible?
37  */
38  static public $valid_php = false;
39 
40  /**
41  * @var array Holder for notices to be displayed in frontend shortcodes if not valid GF
42  */
43  static private $notices = array();
44 
45  function __construct() {
46 
47  self::$valid_gravity_forms = self::check_gravityforms();
48 
49  self::$valid_wordpress = self::check_wordpress();
50 
51  self::$valid_php = self::check_php();
52 
53  self::check_gf_directory();
54 
55  $this->add_hooks();
56  }
57 
58  function add_hooks() {
59 
60  add_filter( 'gravityview/admin/notices', array( $this, 'insert_admin_notices' ) );
61 
62  $this->add_fallback_shortcode();
63  }
64 
65  /**
66  * Add the compatibility notices to the other admin notices
67  * @param array $notices
68  *
69  * @return array
70  */
71  function insert_admin_notices( $notices = array() ) {
72  return array_merge( $notices, self::$notices );
73  }
74 
75  /**
76  * @return GravityView_Compatibility
77  */
78  public static function getInstance() {
79  if( self::$instance ) {
80  return self::$instance;
81  }
82  return new self;
83  }
84 
85  /**
86  * Is everything compatible with this version of GravityView?
87  *
88  * @deprecated 1.19.4
89  * @see \GV\Plugin::is_compatible() accessible via gravityview()->plugin->is_compatible()
90  *
91  * @return bool
92  */
93  public static function is_valid() {
94  return gravityview()->plugin->is_compatible();
95  }
96 
97  /**
98  * Is the version of WordPress compatible?
99  * @since 1.12
100  *
101  * @deprecated 1.19.4
102  * @see \GV\Plugin::is_compatible_wordpress() accessible via gravityview()->plugin->is_compatible_wordpress()
103  */
104  private static function is_valid_wordpress() {
105  return gravityview()->plugin->is_compatible_wordpress();
106  }
107 
108  /**
109  * @since 1.12
110  *
111  * @deprecated 1.19.4
112  * @see \GV\Plugin::is_compatible_gravityforms() accessible via gravityview()->plugin->is_compatible_gravityforms()
113  *
114  * @return bool
115  */
116  private static function is_valid_gravity_forms() {
117  return gravityview()->plugin->is_compatible_gravityforms();
118  }
119 
120  /**
121  * @since 1.12
122  *
123  * @deprecated 1.19.4
124  * @see \GV\Plugin::is_compatible_php() accessible via gravityview()->plugin->is_compatible_php()
125  *
126  * @return bool
127  */
128  private static function is_valid_php() {
129  return gravityview()->plugin->is_compatible_php();
130  }
131 
132  /**
133  * @since 1.12
134  * @return bool
135  */
136  private function add_fallback_shortcode() {
137 
138  // If Gravity Forms doesn't exist or is outdated, load the admin view class to
139  // show the notice, but not load any post types or process shortcodes.
140  // Without Gravity Forms, there is no GravityView. Beautiful, really.
141  if( ! self::is_valid() ) {
142 
143  // If the plugin's not loaded, might as well hide the shortcode for people.
144  add_shortcode( 'gravityview', array( $this, '_shortcode_gf_notice') );
145 
146  }
147  }
148 
149  /**
150  * Get admin notices
151  * @since 1.12
152  * @return array
153  */
154  public static function get_notices() {
155  return self::$notices;
156  }
157 
158  /**
159  * @since 1.9.2 in gravityview.php
160  * @since 1.12
161  *
162  * @param array $atts
163  * @param null $content
164  * @param string $shortcode
165  *
166  * @return null|string NULL returned if user can't activate plugins. Notice shown with a warning that GF isn't supported.
167  */
168  public function _shortcode_gf_notice( $atts = array(), $content = null, $shortcode = 'gravityview' ) {
169 
170  if( ! GVCommon::has_cap( 'activate_plugins' ) ) {
171  return null;
172  }
173 
174  $notices = self::get_notices();
175 
176  $message = '<div style="border:1px solid red; padding: 15px;"><p style="text-align:center;"><em>' . esc_html__( 'You are seeing this notice because you are an administrator. Other users of the site will see nothing.', 'gravityview') . '</em></p>';
177  foreach( (array)$notices as $notice ) {
178  $message .= wpautop( $notice['message'] );
179  }
180  $message .= '</div>';
181 
182  return $message;
183 
184  }
185 
186  /**
187  * Is the version of PHP compatible?
188  *
189  * @since 1.12
190  * @since 1.19.2 Shows a notice if it's compatible with future PHP version requirements
191  *
192  * @return boolean
193  */
194  public static function check_php() {
195 
196  if ( ! gravityview()->plugin->is_compatible_php() ) {
197 
198  self::$notices['php_version'] = array(
199  'class' => 'error',
200  'message' => sprintf( __( "%sGravityView requires PHP Version %s or newer.%s \n\nYou're using Version %s. Please ask your host to upgrade your server's PHP.", 'gravityview' ), '<h3>', GV_MIN_PHP_VERSION, "</h3>\n\n", '<span style="font-family: Consolas, Courier, monospace;">'.phpversion().'</span>' ),
201  'cap' => 'manage_options',
202  'dismiss' => 'php_version',
203  );
204 
205  return false;
206  }
207 
208  if ( ! gravityview()->plugin->is_compatible_future_php() ) {
209 
210  // Show the notice on every update. Yes, annoying, but not as annoying as a plugin breaking.
211  $key = sprintf('php_%s_%s', GV_FUTURE_MIN_PHP_VERSION, GravityView_Plugin::version );
212 
213  self::$notices[ $key ] = array(
214  'class' => 'error',
215  'message' => sprintf( __( "%sGravityView will soon require PHP Version %s.%s \n\nYou're using Version %s. Please ask your host to upgrade your server's PHP.", 'gravityview' ), '<h3>', GV_FUTURE_MIN_PHP_VERSION, "</h3>\n\n", '<span style="font-family: Consolas, Courier, monospace;">'.phpversion().'</span>' ),
216  'cap' => 'manage_options',
217  'dismiss' => $key,
218  );
219 
220  }
221 
222  return true;
223  }
224 
225  /**
226  * Is WordPress compatible?
227  *
228  * @since 1.12
229  * @return boolean
230  */
231  public static function check_wordpress() {
232  global $wp_version;
233 
234  if ( gravityview()->plugin->is_compatible_future_wordpress() ) {
235  return true;
236  }
237 
238  if ( ! gravityview()->plugin->is_compatible_wordpress() ) {
239 
240  self::$notices['wp_version'] = array(
241  'class' => 'error',
242  'message' => sprintf( __( "%sGravityView requires WordPress %s or newer.%s \n\nYou're using Version %s. Please upgrade your WordPress installation.", 'gravityview' ), '<h3>', GV_MIN_WP_VERSION, "</h3>\n\n", '<span style="font-family: Consolas, Courier, monospace;">' . $wp_version . '</span>' ),
243  'cap' => 'update_core',
244  'dismiss' => 'wp_version',
245  );
246 
247  return false;
248  }
249 
250  // Show the notice on every update. Yes, annoying, but not as annoying as a plugin breaking.
251  $key = sprintf( 'wp_%s_%s', GV_FUTURE_MIN_WP_VERSION, GV_PLUGIN_VERSION );
252 
253  self::$notices[ $key ] = array(
254  'class' => 'notice-warning',
255  'message' => sprintf( __( "%sGravityView will soon require WordPress %s%s \n\nYou're using Version %s. Please upgrade your WordPress installation.", 'gravityview' ), '<h3>', GV_FUTURE_MIN_WP_VERSION, "</h3>\n\n", '<span style="font-family: Consolas, Courier, monospace;">' . $wp_version . '</span>' ),
256  'cap' => 'update_core',
257  'dismiss' => $key,
258  );
259 
260  return true;
261  }
262 
263 
264  /**
265  * Check if Gravity Forms plugin is active and show notice if not.
266  *
267  * @since 1.12
268  *
269  * @return boolean True: checks have been passed; GV is fine to run; False: checks have failed, don't continue loading
270  */
271  public static function check_gravityforms() {
272 
273  // Bypass other checks: if the class exists
274  if( class_exists( 'GFCommon' ) ) {
275 
276  // Does the version meet future requirements?
277  if( true === gravityview()->plugin->is_compatible_future_gravityforms() ) {
278  return true;
279  }
280 
281  // Does it meet minimum requirements?
282  $meets_minimum = gravityview()->plugin->is_compatible_gravityforms();
283 
284  if( $meets_minimum ) {
285  /* translators: first placeholder is the future required version of Gravity Forms. The second placeholder is the current version of Gravity Forms. */
286  $title = __( 'In the future, GravityView will require Gravity Forms Version %s or newer.', 'gravityview' );
287  $version = GV_FUTURE_MIN_GF_VERSION;
288  $class = 'notice-warning';
289  } else {
290  /* translators: the placeholder is the required version of Gravity Forms. */
291  $title = __( 'GravityView requires Gravity Forms Version %s or newer.', 'gravityview' );
292  $version = GV_MIN_GF_VERSION;
293  $class = 'error';
294  }
295 
296  $message = '<h3>' . esc_html( sprintf( $title, $version ) ) . '</h3>';
297 
298  /* translators: the placeholder is the current version of Gravity Forms. */
299  $message .= '<p>' . sprintf( esc_html__( "You're using Version %s. Please update your Gravity Forms or purchase a license.", 'gravityview' ), '<span style="font-family: Consolas, Courier, monospace;">'.GFCommon::$version.'</span>' ) . '</p>';
300 
301  /* translators: In this context, "get" means purchase */
302  $message .= '<p><a href="https://gravityview.co/gravityforms/" class="button button-secondary button-large button-hero">' . esc_html__( 'Get the Latest Gravity Forms', 'gravityview' ) . '</a></p>';
303 
304 
305  // Show the notice even if the future version requirements aren't met
306  self::$notices['gf_version'] = array(
307  'class' => $class,
308  'message' => $message,
309  'cap' => 'update_plugins',
310  'dismiss' => 'gf_version_' . $version,
311  );
312 
313  // Return false if the plugin is not compatible, true if meets minimum
314  return $meets_minimum;
315  }
316 
317  $gf_status = self::get_plugin_status( 'gravityforms/gravityforms.php' );
318 
319  /**
320  * The plugin is activated and yet somehow GFCommon didn't get picked up...
321  * OR
322  * It's the Network Admin and we just don't know whether the sites have GF activated themselves.
323  */
324  if( true === $gf_status || is_network_admin() ) {
325  return true;
326  }
327 
328  // If GFCommon doesn't exist, assume GF not active
329  $return = false;
330 
331  switch( $gf_status ) {
332  case 'inactive':
333 
334  // Required for multisite
335  if( ! function_exists('wp_create_nonce') ) {
336  require_once ABSPATH . WPINC . '/pluggable.php';
337  }
338 
339  // Otherwise, throws an error on activation & deactivation "Use of undefined constant LOGGED_IN_COOKIE"
340  if( is_multisite() ) {
341  wp_cookie_constants();
342  }
343 
344  $return = false;
345 
346  $button = function_exists('is_network_admin') && is_network_admin() ? '<strong><a href="#gravity-forms">' : '<strong><a href="'. wp_nonce_url( admin_url( 'plugins.php?action=activate&plugin=gravityforms/gravityforms.php' ), 'activate-plugin_gravityforms/gravityforms.php') . '" class="button button-large">';
347 
348  self::$notices['gf_inactive'] = array(
349  'class' => 'error',
350  'message' => sprintf( __( '%sGravityView requires Gravity Forms to be active. %sActivate Gravity Forms%s to use the GravityView plugin.', 'gravityview' ), '<h3>', "</h3>\n\n". $button, '</a></strong>' ),
351  'cap' => 'activate_plugins',
352  'dismiss' => 'gf_inactive',
353  );
354 
355  break;
356  default:
357  self::$notices['gf_installed'] = array(
358  'class' => 'error',
359  'message' => sprintf( __( '%sGravityView requires Gravity Forms to be installed in order to run properly. %sGet Gravity Forms%s - starting at $59%s%s', 'gravityview' ), '<h3>', "</h3>\n\n".'<a href="https://gravityview.co/gravityforms/" class="button button-secondary button-large button-hero">' , '<em>', '</em>', '</a>'),
360  'cap' => 'install_plugins',
361  'dismiss' => 'gf_installed',
362  );
363  break;
364  }
365 
366  return $return;
367  }
368 
369  /**
370  * Check for potential conflicts and let users know about common issues.
371  *
372  * @return void
373  */
374  private static function check_gf_directory() {
375 
376  if( class_exists( 'GFDirectory' ) ) {
377  self::$notices['gf_directory'] = array(
378  'class' => 'error is-dismissible',
379  'title' => __('Potential Conflict', 'gravityview' ),
380  'message' => __( 'GravityView and Gravity Forms Directory are both active. This may cause problems. If you experience issues, disable the Gravity Forms Directory plugin.', 'gravityview' ),
381  'dismiss' => 'gf_directory',
382  'cap' => 'activate_plugins',
383  );
384  }
385 
386  }
387 
388  /**
389  * Check if specified plugin is active, inactive or not installed
390  *
391  * @param string $location (default: '')
392  *
393  * @return boolean|string True: plugin is active; False: plugin file doesn't exist at path; 'inactive' it's inactive
394  */
395  public static function get_plugin_status( $location = '' ) {
396 
397  if( ! function_exists('is_plugin_active') ) {
398  include_once( ABSPATH . '/wp-admin/includes/plugin.php' );
399  }
400 
401  if( is_network_admin() && is_plugin_active_for_network( $location ) ) {
402  return true;
403  }
404 
405  if( ! is_network_admin() && is_plugin_active( $location ) ) {
406  return true;
407  }
408 
409  if(
410  !file_exists( trailingslashit( WP_PLUGIN_DIR ) . $location ) &&
411  !file_exists( trailingslashit( WPMU_PLUGIN_DIR ) . $location )
412  ) {
413  return false;
414  }
415 
416  return 'inactive';
417  }
418 
419 }
420 
insert_admin_notices( $notices=array())
Add the compatibility notices to the other admin notices.
const GV_FUTURE_MIN_WP_VERSION
GravityView will soon require at least this version of WordPress to function properly.
Definition: gravityview.php:63
static check_wordpress()
Is WordPress compatible?
_shortcode_gf_notice( $atts=array(), $content=null, $shortcode='gravityview')
$class
static is_valid()
Is everything compatible with this version of GravityView?
gravityview()
Definition: _stubs.php:26
if(empty( $field_settings['content'])) $content
Definition: custom.php:37
static get_notices()
Get admin notices.
static check_gf_directory()
Check for potential conflicts and let users know about common issues.
static check_php()
Is the version of PHP compatible?
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:76
const GV_FUTURE_MIN_GF_VERSION
GravityView will soon require at least this version of Gravity Forms to function properly.
Definition: gravityview.php:51
static get_plugin_status( $location='')
Check if specified plugin is active, inactive or not installed.
static check_gravityforms()
Check if Gravity Forms plugin is active and show notice if not.
static is_valid_wordpress()
Is the version of WordPress compatible?
static has_cap( $caps='', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()
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:69
Handle GravityView compatibility notices and fallback shortcodes.
const GV_MIN_WP_VERSION
GravityView requires at least this version of WordPress to function properly.
Definition: gravityview.php:57
$title