GravityView  2.1.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 Katz Web Services, Inc.
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_wordpress() ) {
235 
236  self::$notices['wp_version'] = array(
237  'class' => 'error',
238  '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>' ),
239  'cap' => 'update_core',
240  'dismiss' => 'wp_version',
241  );
242 
243  return false;
244  }
245 
246  return true;
247  }
248 
249 
250  /**
251  * Check if Gravity Forms plugin is active and show notice if not.
252  *
253  * @since 1.12
254  *
255  * @access public
256  * @return boolean True: checks have been passed; GV is fine to run; False: checks have failed, don't continue loading
257  */
258  public static function check_gravityforms() {
259 
260  // Bypass other checks: if the class exists
261  if( class_exists( 'GFCommon' ) ) {
262 
263  // Does the version meet future requirements?
264  if( true === version_compare( GFCommon::$version, GV_FUTURE_MIN_GF_VERSION, ">=" ) ) {
265  return true;
266  }
267 
268  // Does it meet minimum requirements?
269  $meets_minimum = gravityview()->plugin->is_compatible_gravityforms();
270 
271  if( $meets_minimum ) {
272  /* translators: first placeholder is the future required version of Gravity Forms. The second placeholder is the current version of Gravity Forms. */
273  $title = __( 'In the future, GravityView will require Gravity Forms Version %s or newer.', 'gravityview' );
274  $version = GV_FUTURE_MIN_GF_VERSION;
275  $class = 'notice-warning';
276  } else {
277  /* translators: the placeholder is the required version of Gravity Forms. */
278  $title = __( 'GravityView requires Gravity Forms Version %s or newer.', 'gravityview' );
279  $version = GV_MIN_GF_VERSION;
280  $class = 'error';
281  }
282 
283  $message = '<h3>' . esc_html( sprintf( $title, $version ) ) . '</h3>';
284 
285  /* translators: the placeholder is the current version of Gravity Forms. */
286  $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>';
287 
288  /* translators: In this context, "get" means purchase */
289  $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>';
290 
291 
292  // Show the notice even if the future version requirements aren't met
293  self::$notices['gf_version'] = array(
294  'class' => $class,
295  'message' => $message,
296  'cap' => 'update_plugins',
297  'dismiss' => 'gf_version_' . $version,
298  );
299 
300  // Return false if the plugin is not compatible, true if meets minimum
301  return $meets_minimum;
302  }
303 
304  $gf_status = self::get_plugin_status( 'gravityforms/gravityforms.php' );
305 
306  /**
307  * The plugin is activated and yet somehow GFCommon didn't get picked up...
308  * OR
309  * It's the Network Admin and we just don't know whether the sites have GF activated themselves.
310  */
311  if( true === $gf_status || is_network_admin() ) {
312  return true;
313  }
314 
315  // If GFCommon doesn't exist, assume GF not active
316  $return = false;
317 
318  switch( $gf_status ) {
319  case 'inactive':
320 
321  // Required for multisite
322  if( ! function_exists('wp_create_nonce') ) {
323  require_once ABSPATH . WPINC . '/pluggable.php';
324  }
325 
326  // Otherwise, throws an error on activation & deactivation "Use of undefined constant LOGGED_IN_COOKIE"
327  if( is_multisite() ) {
328  wp_cookie_constants();
329  }
330 
331  $return = false;
332 
333  $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">';
334 
335  self::$notices['gf_inactive'] = array(
336  'class' => 'error',
337  '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>' ),
338  'cap' => 'activate_plugins',
339  'dismiss' => 'gf_inactive',
340  );
341 
342  break;
343  default:
344  self::$notices['gf_installed'] = array(
345  'class' => 'error',
346  '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>'),
347  'cap' => 'install_plugins',
348  'dismiss' => 'gf_installed',
349  );
350  break;
351  }
352 
353  return $return;
354  }
355 
356  /**
357  * Check for potential conflicts and let users know about common issues.
358  *
359  * @return void
360  */
361  private static function check_gf_directory() {
362 
363  if( class_exists( 'GFDirectory' ) ) {
364  self::$notices['gf_directory'] = array(
365  'class' => 'error is-dismissible',
366  'title' => __('Potential Conflict', 'gravityview' ),
367  'message' => __( 'GravityView and Gravity Forms Directory are both active. This may cause problems. If you experience issues, disable the Gravity Forms Directory plugin.', 'gravityview' ),
368  'dismiss' => 'gf_directory',
369  'cap' => 'activate_plugins',
370  );
371  }
372 
373  }
374 
375  /**
376  * Check if specified plugin is active, inactive or not installed
377  *
378  * @access public
379  * @static
380  * @param string $location (default: '')
381  * @return boolean|string True: plugin is active; False: plugin file doesn't exist at path; 'inactive' it's inactive
382  */
383  public static function get_plugin_status( $location = '' ) {
384 
385  if( ! function_exists('is_plugin_active') ) {
386  include_once( ABSPATH . '/wp-admin/includes/plugin.php' );
387  }
388 
389  if( is_network_admin() && is_plugin_active_for_network( $location ) ) {
390  return true;
391  }
392 
393  if( !is_network_admin() && is_plugin_active( $location ) ) {
394  return true;
395  }
396 
397  if(
398  !file_exists( trailingslashit( WP_PLUGIN_DIR ) . $location ) &&
399  !file_exists( trailingslashit( WPMU_PLUGIN_DIR ) . $location )
400  ) {
401  return false;
402  }
403 
404  return 'inactive';
405  }
406 
407 }
408 
insert_admin_notices( $notices=array())
Add the compatibility notices to the other admin notices.
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_FUTURE_MIN_PHP_VERSION
Definition: gravityview.php:64
const GV_FUTURE_MIN_GF_VERSION
GravityView will soon require at least this version of Gravity Forms to function properly.
Definition: gravityview.php:70
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:57
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:51
$title