Skip to content

Commit

Permalink
check php version in main.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Luiz Bills committed Aug 12, 2022
1 parent df948a1 commit 2f1c125
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"require": {
"php": ">=7.4",
"ext-mbstring": "*"
},
"require-dev": {},
Expand Down
3 changes: 1 addition & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 0 additions & 14 deletions core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
27 changes: 23 additions & 4 deletions main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
'<code>composer install</code>'
)
);
}
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;
Expand Down

0 comments on commit 2f1c125

Please sign in to comment.