Skip to content

Commit

Permalink
update requirements check for php extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
pjv committed Jun 11, 2023
1 parent a0d4f74 commit 93f8c29
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
54 changes: 44 additions & 10 deletions classes/class-nostrtium-requirements-check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 '<div class="error">';
echo '<p>The &#8220;' . esc_html($this->title) . '&#8221; plugin cannot run on PHP versions older than ' . $this->php . '. Please contact your host and ask them to upgrade.</p>';
echo '</div>';
}

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 '<div class="error">';
echo '<p>The &#8220;' . esc_html($this->title) . '&#8221; plugin cannot run without these missing php extensions: ' . $missing . '.</p>';
echo '</div>';
}

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 '<div class="error">';
echo '<p>The &#8220;' . esc_html($this->title) . '&#8221; plugin cannot run on WordPress versions older than ' . $this->wp . '. Please update WordPress.</p>';
echo '</div>';
}
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 '<div class="error">';
echo '<p>The &#8220;' . esc_html($this->title) . '&#8221; plugin cannot run without the wp-content/uploads directory being writeable.</p>';
Expand Down
16 changes: 8 additions & 8 deletions nostrtium.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';
Expand Down
7 changes: 6 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand Down Expand Up @@ -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).
Expand Down

0 comments on commit 93f8c29

Please sign in to comment.