From 2f1c125e2ac1721713b612e4c74eedaf933e6388 Mon Sep 17 00:00:00 2001 From: Luiz Bills Date: Fri, 12 Aug 2022 11:28:13 -0300 Subject: [PATCH] check php version in main.php --- composer.json | 1 - composer.lock | 3 +-- core/Config.php | 14 -------------- main.php | 27 +++++++++++++++++++++++---- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/composer.json b/composer.json index 909eb9f..fbdbad8 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,5 @@ { "require": { - "php": ">=7.4", "ext-mbstring": "*" }, "require-dev": {}, diff --git a/composer.lock b/composer.lock index 0920603..c22c123 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9c57cca5efcab303eb090a22d1773b6a", + "content-hash": "cd688039d84dca3bdea14a92f9dd4e43", "packages": [], "packages-dev": [], "aliases": [], @@ -13,7 +13,6 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=7.4", "ext-mbstring": "*" }, "platform-dev": [], diff --git a/core/Config.php b/core/Config.php index d2771a1..cc14350 100644 --- a/core/Config.php +++ b/core/Config.php @@ -41,20 +41,6 @@ public static function init ( $main_file ) { $data = \get_file_data( $main_file, [ 'Plugin Name', 'Version' ] ); self::$values[ 'NAME' ] = __( $data[0], 'your_text_domain' ); self::$values[ 'VERSION' ] = $data[1]; - - if ( \file_exists( $root . '/composer.json' ) ) { - $json_raw = \file_get_contents( $root . '/composer.json' ); - $composer = \json_decode( $json_raw ); - $php_version = null; - try { - $php_version = $composer ? $composer->require->php : false; - } catch ( \Throwable $e ) { - } - if ( $php_version ) { - $php_version = \preg_replace( '/[^0-9\.]/', '', $php_version ); - self::$values[ 'REQUIRED_PHP_VERSION' ] = $php_version; - } - } } public static function set ( $key, $value ) { diff --git a/main.php b/main.php index a566ff2..dc1baa2 100644 --- a/main.php +++ b/main.php @@ -32,12 +32,31 @@ // load_plugin_textdomain( 'your_text_domain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); try { + // Check PHP Version + $php_expected = '7.4'; + $php_current = PHP_VERSION; + if ( version_compare( $php_current, $php_expected, '<' ) ) { + throw new Error( + sprintf( + // translators: the %s are PHP versions + esc_html__( "This plugin requires PHP version %s or later (your server PHP version is %s)", 'your_text_domain' ), + $php_expected, esc_html( $php_current ) + ) + ); + } + // check composer autoload - $composer_autoload = __DIR__ . '/vendor/autoload.php'; - if ( ! file_exists( $composer_autoload ) ) { - throw new \Error( $composer_autoload . ' does not exist' ); + $autoload = __DIR__ . '/vendor/autoload.php'; + if ( ! file_exists( $autoload ) ) { + throw new Error( + sprintf( + // translators: %s is the `composer install` command + esc_html__( 'Missing Composer autoload. You need run %s.', 'your_text_domain' ), + 'composer install' + ) + ); } - include_once $composer_autoload; + include_once $autoload; } catch ( Throwable $e ) { return add_action( 'admin_notices', function () use ( $e ) { if ( ! current_user_can( 'install_plugins' ) ) return;