Skip to content

Commit

Permalink
Merge pull request #546 from 10up/fix/541-php-ver-check
Browse files Browse the repository at this point in the history
Add PHP version check for plugin.
  • Loading branch information
dkotter authored Jul 28, 2023
2 parents a8d37b4 + f7dec6b commit 5c79b02
Showing 1 changed file with 37 additions and 20 deletions.
57 changes: 37 additions & 20 deletions classifai.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,47 @@
*/

/**
* Require PHP version 7.4+ - throw an error if the plugin is activated on an older version.
* Get the minimum version of PHP required by this plugin.
*
* Note that this itself is only PHP5.3+ compatible because of the anonymous callback.
* @return string Minimum version required.
*/
register_activation_hook(
__FILE__,
function() {
if ( version_compare( PHP_VERSION, '7.4.0', '<' ) ) {
wp_die(
sprintf(
wp_kses(
/* translators: PHP Update guide URL */
__( 'ClassifAI requires PHP version 7.4. <a href="%s">Click here</a> to learn how to update your PHP version.', 'classifai' ),
array(
'a' => array( 'href' => array() ),
function classifai_minimum_php_requirement() {
return '7.4';
}

/**
* Whether PHP installation meets the minimum requirements
*
* @return bool True if meets minimum requirements, false otherwise.
*/
function classifai_site_meets_php_requirements() {
return version_compare( phpversion(), classifai_minimum_php_requirement(), '>=' );
}

// Ensuring our PHP version requirement is met first before loading plugin.
if ( ! classifai_site_meets_php_requirements() ) {
add_action(
'admin_notices',
function() {
?>
<div class="notice notice-error">
<p>
<?php
echo wp_kses_post(
sprintf(
/* translators: %s: Minimum required PHP version */
__( 'ClassifAI requires PHP version %s or later. Please upgrade PHP or disable the plugin.', 'classifai' ),
esc_html( classifai_minimum_php_requirement() )
)
),
esc_url( 'https://wordpress.org/support/update-php/' )
),
esc_html__( 'Error Activating', 'classifai' )
);
);
?>
</p>
</div>
<?php
}
}
);
);
return;
}

/**
* Small wrapper around PHP's define function. The defined constant is
Expand Down

0 comments on commit 5c79b02

Please sign in to comment.