From 93f8c29753e0c94cb000acd80b47ca3f603e8838 Mon Sep 17 00:00:00 2001 From: pjv Date: Sun, 11 Jun 2023 07:48:57 -0500 Subject: [PATCH] update requirements check for php extensions --- README.md | 1 + .../class-nostrtium-requirements-check.php | 54 +++++++++++++++---- nostrtium.php | 16 +++--- readme.txt | 7 ++- 4 files changed, 59 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index cbfbc98..83d354f 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Please test and report issues here. Some of the included libraries have relatively recent dependency requirements so you will need the following in your WordPress platform: * PHP 8.1+ * php-gmp module must be installed ([Installation on Ubuntu](https://computingforgeeks.com/how-to-install-php-on-ubuntu-linux-system/)) +* php-bcmath module must be installed ([Installation on Ubuntu](https://www.itsolutionstuff.com/post/ubuntu-php-bcmath-extension-install-commands-exampleexample.html)) * WordPress 6.0+ * Writable uploads directory (on activation, the plugin writes a cryptographic keyfile to a storage directory) diff --git a/classes/class-nostrtium-requirements-check.php b/classes/class-nostrtium-requirements-check.php index 58fe06a..cddc043 100644 --- a/classes/class-nostrtium-requirements-check.php +++ b/classes/class-nostrtium-requirements-check.php @@ -8,6 +8,7 @@ class Nostrtium_Requirements_Check { // args private $title = ''; private $php = '8.1'; + private $extensions = []; private $wp = '6.0'; private $dir = ''; private $file; @@ -20,62 +21,95 @@ public function __construct($args) { } } } + public function passes() { - $passes = $this->php_passes() && $this->wp_passes() && $this->dir_passes(); + $passes = $this->php_passes() && $this->extensions_passes() && $this->wp_passes() && $this->dir_passes(); if (!$passes) { add_action('admin_notices', [$this, 'deactivate']); } return $passes; } + public function deactivate() { if (isset($this->file)) { deactivate_plugins(plugin_basename($this->file)); } } + private function php_passes() { if ($this->__php_at_least($this->php)) { return true; } else { add_action('admin_notices', [$this, 'php_version_notice']); - return false; } } + private static function __php_at_least($min_version) { return version_compare(phpversion(), $min_version, '>='); } + public function php_version_notice() { echo '
'; echo '

The “' . esc_html($this->title) . '” plugin cannot run on PHP versions older than ' . $this->php . '. Please contact your host and ask them to upgrade.

'; echo '
'; } + + private function extensions_passes() { + $loaded_extensions = get_loaded_extensions(); + $passes = true; + $missing = ""; + + foreach ($this->extensions as $extension) { + if (!in_array($extension, $loaded_extensions)) { + $passes = false; + $missing .= $extension . ", "; + } + } + + if ($passes) { + return true; + } else { + add_action('admin_notices', [$this, 'extensions_notice', $missing]); + return false; + } + } + + public function extensions_notice($missing) { + echo '
'; + echo '

The “' . esc_html($this->title) . '” plugin cannot run without these missing php extensions: ' . $missing . '.

'; + echo '
'; + } + private function wp_passes() { if ($this->__wp_at_least($this->wp)) { return true; } else { add_action('admin_notices', [$this, 'wp_version_notice']); - return false; } } + private static function __wp_at_least($min_version) { return version_compare(get_bloginfo('version'), $min_version, '>='); } + public function wp_version_notice() { echo '
'; echo '

The “' . esc_html($this->title) . '” plugin cannot run on WordPress versions older than ' . $this->wp . '. Please update WordPress.

'; echo '
'; } - private function dir_passes() { - if (is_writable($this->dir)) { - return true; - } else { - add_action('admin_notices', [$this, 'dir_writeable_notice']); - return false; - } + private function dir_passes() { + if (is_writable($this->dir)) { + return true; + } else { + add_action('admin_notices', [$this, 'dir_writeable_notice']); + return false; } + } + public function dir_writeable_notice() { echo '
'; echo '

The “' . esc_html($this->title) . '” plugin cannot run without the wp-content/uploads directory being writeable.

'; diff --git a/nostrtium.php b/nostrtium.php index 222e010..1118b9d 100644 --- a/nostrtium.php +++ b/nostrtium.php @@ -8,7 +8,7 @@ * Author URI: https://github.com/pjv * Text Domain: nostrtium * Domain Path: /languages - * Version: 0.7.1 + * Version: 0.7.2 * Requires at least 6.0 * Requires PHP 8.1 * License Unlicense @@ -21,18 +21,18 @@ exit; } -define('PJV_NOSTRTIUM_VERSION', '0.7.1'); +define('PJV_NOSTRTIUM_VERSION', '0.7.2'); define('PJV_NOSTRTIUM_DIR', plugin_dir_path(__FILE__)); define('PJV_NOSTRTIUM_DEFAULT_USER_ROLE', 'edit_posts'); define('PJV_NOSTRTIUM_STORAGE', wp_upload_dir()['basedir'] . '/nostrtium_' . md5(LOGGED_IN_SALT) . '/'); require_once PJV_NOSTRTIUM_DIR . 'classes/class-nostrtium-requirements-check.php'; -$pjv_nostrtium_requirements_check = new Nostrtium_Requirements_Check([ - 'title' => 'Nostrtium', - 'php' => '8.1', - 'wp' => '6.0', - 'dir' => wp_upload_dir()['basedir'], - 'file' => __FILE__, +$pjv_nostrtium_requirements_check = new Nostrtium_Requirements_Check(['title' => 'Nostrtium', + 'php' => '8.1', + 'extensions' => ['gmp', 'bcmath'], + 'wp' => '6.0', + 'dir' => wp_upload_dir()['basedir'], + 'file' => __FILE__, ]); if ($pjv_nostrtium_requirements_check->passes()) { require_once PJV_NOSTRTIUM_DIR . 'vendor/autoload.php'; diff --git a/readme.txt b/readme.txt index b58d08f..75f8db0 100644 --- a/readme.txt +++ b/readme.txt @@ -5,7 +5,7 @@ Tags: social media, nostr Requires at least: 6.0 Requires PHP: 8.1 Tested up to: 6.2.2 -Stable tag: 0.7.0 +Stable tag: 0.7.2 License: Unlicense License URI: https://unlicense.org @@ -30,6 +30,7 @@ Development is taking place on [github](https://github.com/pjv/nostrtium) and th Some of the included libraries have relatively recent dependency requirements so you will need the following in your WordPress platform: * PHP 8.1+ * php-gmp module must be installed ([Installation on Ubuntu](https://computingforgeeks.com/how-to-install-php-on-ubuntu-linux-system/)) +* php-bcmath module must be installed ([Installation on Ubuntu](https://www.itsolutionstuff.com/post/ubuntu-php-bcmath-extension-install-commands-exampleexample.html)) * WordPress 6.0+ * Writable uploads directory (on activation, the plugin writes a cryptographic keyfile to a storage directory) @@ -65,6 +66,10 @@ The manual installation method involves downloading the plugin and then uploadin == Changelog == += 0.7.2 = +* Update requirements text and requirements check +* Fix missing variable declaration + = 0.7.1 = * Auto-post only on first publication - not updates of old posts. * Auto post only of type 'post' (currently not pages or custom post types).