GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
preflight_check.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @license GPL-2.0-or-later
4  *
5  * Modified by gravityview on 13-January-2023 using Strauss.
6  * @see https://github.com/BrianHenryIE/strauss
7  */
8 
10 
11 require_once __DIR__ . '/Helpers/Core.php';
12 
13 /**
14  * Determines if a plugin should load.
15  *
16  * @since 1.0.3
17  * @since 1.0.4 Repurposed as a catch-all method that performs multiple checks
18  *
19  * @param string $plugin_file Absolute path to the main plugin file.
20  *
21  * @return bool
22  */
23 function should_load( $plugin_file ) {
24  return ! is_disabled_via_url( $plugin_file ) || ! meets_min_php_version_requirement( $plugin_file );
25 }
26 
27 /**
28  * Checks if loading is disabled via a URL parameter.
29  * Adding ?gk_disable_loading to the URL will prevent the loading of all plugins that include Foundation.
30  * Adding ?gk_disable_loading=plugin-text-domain to the URL will prevent the loading of specific plugin(s) that includes Foundation (multiple text domains can be comma-separated).
31  *
32  * @since 1.0.4
33  *
34  * @param string $plugin_file Absolute path to the main plugin file.
35  *
36  * @return bool
37  */
38 function is_disabled_via_url( $plugin_file ) {
39  // Limit check to admin & login pages.
40  if ( ! is_admin() && strpos( $_SERVER['PHP_SELF'], 'wp-login.php' ) === false ) {
41  return false;
42  }
43 
44  $plugin_data = Helpers\Core::get_plugin_data( $plugin_file );
45 
46  $cookie = 'gk_disable_loading';
47  $cookie_expiry_time = MINUTE_IN_SECONDS;
48 
49  $_is_disabled = function ( $plugin_text_domains ) use ( $plugin_data ) {
50  if ( 'all' === $plugin_text_domains ) {
51  return true;
52  }
53 
54  foreach ( explode( ',', $plugin_text_domains ) as $text_domain ) {
55  if ( $plugin_data['TextDomain'] === $text_domain ) {
56  return true;
57  }
58  }
59 
60  return false;
61  };
62 
63  if ( isset( $_COOKIE[ $cookie ] ) ) {
64  if ( isset( $_GET['gk_enable_loading'] ) ) {
65  setcookie( $cookie, false, time() - $cookie_expiry_time );
66 
67  return false;
68  }
69 
70  return $_is_disabled( $_COOKIE[ $cookie ] );
71  }
72 
73  $disable_loading = isset( $_GET['gk_disable_loading'] ) ? $_GET['gk_disable_loading'] : null;
74 
75  if ( is_null( $disable_loading ) ) {
76  return false;
77  }
78 
79  setcookie( $cookie, $disable_loading ?: 'all', time() + $cookie_expiry_time );
80 
81  return $_is_disabled( $disable_loading );
82 }
83 
84 /**
85  * Checks if the minimum PHP version requirement is met.
86  *
87  * @param string $plugin_file Absolute path to the main plugin file.
88  * @param string $min_php_version (optional) Minimum PHP version. Default: 5.6.4.
89  * @param bool $show_notice (optional) Display error notice. Default: true.
90  *
91  * @return bool
92  */
93 function meets_min_php_version_requirement( $plugin_file, $min_php_version = '5.6.4', $show_notice = true ) {
94  $plugin_data = Helpers\Core::get_plugin_data( $plugin_file );
95 
96  $meets_requirement = (bool) version_compare( phpversion(), $min_php_version, '>=' );
97 
98  if ( ! $show_notice ) {
99  return $meets_requirement;
100  }
101 
102  if ( ! $meets_requirement ) {
103  $notice = strtr(
104  esc_html_x( '[plugin] requires PHP [version] or newer.', 'Placeholders inside [] are not to be translated.', 'gk-gravityview' ),
105  [
106  '[plugin]' => $plugin_data['Name'],
107  '[version]' => $min_php_version
108  ]
109  );
110 
111  if ( 'cli' === php_sapi_name() ) {
112  printf( $notice );
113  } else {
114  add_action( 'admin_notices', function () use ( $notice ) {
115  echo "<div class='error' style='padding: 1.25em 0 1.25em 1em;'>$notice</div>";
116  } );
117  }
118  }
119 
120  return $meets_requirement;
121 }
GPL-2.0-or-later
Definition: Core.php:9
is_disabled_via_url( $plugin_file)
Checks if loading is disabled via a URL parameter.
static get_plugin_data( $plugin_file, $markup=true, $translate=true)
Wrapper for WP&#39;s get_plugin_data() function.
meets_min_php_version_requirement( $plugin_file, $min_php_version='5.6.4', $show_notice=true)
Checks if the minimum PHP version requirement is met.
should_load( $plugin_file)
Determines if a plugin should load.