gravityview /
GravityView
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | namespace GV; |
||
| 3 | |||
| 4 | /** If this file is called directly, abort. */ |
||
| 5 | if ( ! defined( 'GRAVITYVIEW_DIR' ) ) |
||
| 6 | die(); |
||
| 7 | |||
| 8 | /** |
||
| 9 | * The GravityView WordPress plugin class. |
||
| 10 | * |
||
| 11 | * Contains functionality related to GravityView being |
||
| 12 | * a WordPress plugin and doing WordPress pluginy things. |
||
| 13 | * |
||
| 14 | * Accessible via gravityview()->plugin |
||
| 15 | */ |
||
| 16 | final class Plugin { |
||
| 17 | /** |
||
| 18 | * @var string The plugin version. |
||
| 19 | * |
||
| 20 | * @api |
||
| 21 | * @since future |
||
| 22 | */ |
||
| 23 | public $version = 'future'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string Minimum WordPress version. |
||
| 27 | * |
||
| 28 | * GravityView requires at least this version of WordPress to function properly. |
||
| 29 | */ |
||
| 30 | private static $min_wp_version = '4.0'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string Minimum Gravity Forms version. |
||
| 34 | * |
||
| 35 | * GravityView requires at least this version of Gravity Forms to function properly. |
||
| 36 | */ |
||
| 37 | private static $min_gf_version = '1.9.14'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string Minimum PHP version. |
||
| 41 | * |
||
| 42 | * GravityView requires at least this version of PHP to function properly. |
||
| 43 | */ |
||
| 44 | private static $min_php_version = '5.3.0'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string|bool Minimum future PHP version. |
||
| 48 | * |
||
| 49 | * GravityView will require this version of PHP soon. False if no future PHP version changes are planned. |
||
| 50 | */ |
||
| 51 | private static $future_min_php_version = false; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string|bool Minimum future Gravity Forms version. |
||
| 55 | * |
||
| 56 | * GravityView will require this version of Gravity Forms soon. False if no future Gravity Forms version changes are planned. |
||
| 57 | */ |
||
| 58 | private static $future_min_gf_version = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var \GV\Plugin The \GV\Plugin static instance. |
||
| 62 | */ |
||
| 63 | private static $__instance = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Get the global instance of \GV\Plugin. |
||
| 67 | * |
||
| 68 | * @return \GV\Plugin The global instance of GravityView Plugin. |
||
| 69 | */ |
||
| 70 | public static function get() { |
||
| 71 | if ( ! self::$__instance instanceof self ) |
||
| 72 | self::$__instance = new self; |
||
| 73 | return self::$__instance; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Bootstrap. |
||
| 78 | * |
||
| 79 | * @return void |
||
| 80 | */ |
||
| 81 | private function __construct() { |
||
| 82 | $this->init(); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Initialize all plugin hooks, constants, settings, etc. |
||
| 87 | * |
||
| 88 | * @return void |
||
| 89 | */ |
||
| 90 | private function init() { |
||
| 91 | /** |
||
| 92 | * Stop all further functionality from loading if the WordPress |
||
| 93 | * plugin is incompatible with the current environment. |
||
| 94 | */ |
||
| 95 | if ( ! $this->is_compatible() ) |
||
|
0 ignored issues
–
show
|
|||
| 96 | return; |
||
| 97 | |||
| 98 | /** Register hooks that are fired when the plugin is activated and deactivated. */ |
||
| 99 | register_activation_hook( $this->dir( 'gravityview.php' ), array( $this, 'activate' ) ); |
||
| 100 | register_deactivation_hook( $this->dir( 'gravityview.php' ), array( $this, 'deactivate' ) ); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Plugin activation function. |
||
| 105 | * |
||
| 106 | * @internal |
||
| 107 | * @return void |
||
| 108 | */ |
||
| 109 | public function activate() { |
||
| 110 | flush_rewrite_rules(); |
||
| 111 | |||
| 112 | update_option( 'gv_version', \GravityView_Plugin::version ); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Plugin deactivation function. |
||
| 117 | * |
||
| 118 | * @internal |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | public function deactivate() { |
||
| 122 | flush_rewrite_rules(); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Retrieve an absolute path within the Gravity Forms plugin directory. |
||
| 127 | * |
||
| 128 | * @api |
||
| 129 | * @since future |
||
| 130 | * |
||
| 131 | * @param string $path Optional. Append this extra path component. |
||
| 132 | * @return string The absolute path to the plugin directory. |
||
| 133 | */ |
||
| 134 | public function dir( $path = '' ) { |
||
| 135 | return GRAVITYVIEW_DIR . ltrim( $path, '/' ); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Retrieve a URL within the Gravity Forms plugin directory. |
||
| 140 | * |
||
| 141 | * @api |
||
| 142 | * @since future |
||
| 143 | * |
||
| 144 | * @param string $path Optional. Extra path appended to the URL. |
||
| 145 | * @return The URL to this plugin, with trailing slash. |
||
| 146 | */ |
||
| 147 | public function url( $path = '/' ) { |
||
| 148 | return plugins_url( $path, $this->dir( 'gravityview.php' ) ); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Is everything compatible with this version of GravityView? |
||
| 153 | * |
||
| 154 | * @api |
||
| 155 | * @since future |
||
| 156 | * |
||
| 157 | * @return bool |
||
| 158 | */ |
||
| 159 | public function is_compatible() { |
||
| 160 | return |
||
| 161 | $this->is_compatible_php() |
||
| 162 | && $this->is_compatible_wordpress() |
||
| 163 | && $this->is_compatible_gravityforms(); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Is this version of GravityView compatible with the current version of PHP? |
||
| 168 | * |
||
| 169 | * @api |
||
| 170 | * @since future |
||
| 171 | * |
||
| 172 | * @return bool true if compatible, false otherwise. |
||
| 173 | */ |
||
| 174 | public function is_compatible_php() { |
||
| 175 | return version_compare( $this->get_php_version(), self::$min_php_version, '>=' ); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Is this version of GravityView compatible with the current version of WordPress? |
||
| 180 | * |
||
| 181 | * @api |
||
| 182 | * @since future |
||
| 183 | * |
||
| 184 | * @return bool true if compatible, false otherwise. |
||
| 185 | */ |
||
| 186 | public function is_compatible_wordpress() { |
||
| 187 | return version_compare( $this->get_wordpress_version(), self::$min_wp_version, '>=' ); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Is this version of GravityView compatible with the current version of Gravity Forms? |
||
| 192 | * |
||
| 193 | * @api |
||
| 194 | * @since future |
||
| 195 | * |
||
| 196 | * @return bool true if compatible, false otherwise (or not active/installed). |
||
| 197 | */ |
||
| 198 | public function is_compatible_gravityforms() { |
||
| 199 | try { |
||
| 200 | return version_compare( $this->get_gravityforms_version(), self::$min_gf_version, '>=' ); |
||
| 201 | } catch ( \ErrorException $e ) { |
||
| 202 | return false; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Retrieve the current PHP version. |
||
| 208 | * |
||
| 209 | * Overridable with GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE during testing. |
||
| 210 | * |
||
| 211 | * @return string The version of PHP. |
||
| 212 | */ |
||
| 213 | private function get_php_version() { |
||
| 214 | return !empty( $GLOBALS['GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE'] ) ? |
||
| 215 | $GLOBALS['GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE'] : phpversion(); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Retrieve the current WordPress version. |
||
| 220 | * |
||
| 221 | * Overridable with GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE during testing. |
||
| 222 | * |
||
| 223 | * @return string The version of WordPress. |
||
| 224 | */ |
||
| 225 | private function get_wordpress_version() { |
||
| 226 | return !empty( $GLOBALS['GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE'] ) ? |
||
| 227 | $GLOBALS['GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE'] : $GLOBALS['wp_version']; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Retrieve the current Gravity Forms version. |
||
| 232 | * |
||
| 233 | * Overridable with GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE during testing. |
||
| 234 | * |
||
| 235 | * @throws \ErrorException If the Gravity Forms plugin is not active or installed (E_ERROR severity) |
||
| 236 | * @return string The version of Gravity Forms. |
||
| 237 | */ |
||
| 238 | private function get_gravityforms_version() { |
||
| 239 | if ( !class_exists( '\GFCommon' ) || !empty( $GLOBALS['GRAVITYVIEW_TESTS_GF_INACTIVE_OVERRIDE'] ) ) |
||
| 240 | throw new \ErrorException( __( 'Gravity Forms is inactive or not installed.', 'gravityview' ) ); |
||
| 241 | |||
| 242 | return !empty( $GLOBALS['GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE'] ) ? |
||
| 243 | $GLOBALS['GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE'] : \GFCommon::$version; |
||
| 244 | } |
||
| 245 | |||
| 246 | private function __clone() { } |
||
| 247 | |||
| 248 | private function __wakeup() { } |
||
| 249 | } |
||
| 250 |
Adding braces to control structures avoids accidental mistakes as your code changes: