Skip to content

Commit

Permalink
Move the ACF requirements to the plugin (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luehrsen authored Oct 10, 2023
1 parent 0795367 commit f48aec2
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 12 deletions.
49 changes: 49 additions & 0 deletions plugin/inc/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
function plugin() {
static $plugin = null;

/**
* Check if the requirements for the current plugin are met.
* If the requirements are not met, we might get severe errors. Therefore, we
* return null and do not initialize the plugin.
*/
if ( ! plugin_requirements_are_met() ) {
return null;
}

if ( null === $plugin ) {
/**
* The main plugin component.
Expand Down Expand Up @@ -45,3 +54,43 @@ function plugin_container() {

return $container;
}

/**
* Check if the requirements for the current plugin are met.
*
* @return bool True if requirements are met, false otherwise.
*/
function plugin_requirements_are_met() {
/**
* Advanced Custom Fields Pro is required.
*/
if ( ! function_exists( '\acf_add_options_page' ) ) {
return false;
}

return true;
}

/**
* Display an admin notice if the requirements are not met.
*/
function plugin_requirements_notice__error() {
if ( plugin_requirements_are_met() ) {
return;
}

?>
<div class="notice notice-error">
<p>
<?php
printf(
/* translators: %s: Plugin name. */
esc_html__( 'The requirements for the %s plugin are not met. Please install all requirements.', 'lhpbp' ),
'<strong>' . esc_html__( 'LHPBP Plugin', 'lhpbp' ) . '</strong>'
);
?>
</p>
</div>
<?php
}
add_action( 'admin_notices', '\WpMunich\lhpbp\plugin\plugin_requirements_notice__error' );
3 changes: 2 additions & 1 deletion plugin/lhpbpp.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

use function WpMunich\lhpbp\plugin\plugin;
use function WpMunich\lhpbp\plugin\plugin_requirements_are_met;

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
Expand All @@ -41,7 +42,7 @@
call_user_func( 'WpMunich\lhpbp\plugin\plugin' );

// Initialize the plugin update checker.
if ( class_exists( 'Puc_v4_Factory' ) ) {
if ( class_exists( 'Puc_v4_Factory' ) && plugin_requirements_are_met() ) {
Puc_v4_Factory::buildUpdateChecker(
'https://www.luehrsen-heinrich.de/updates/?action=get_metadata&slug=' . plugin()->get_plugin_slug(),
__FILE__, // Full path to the main plugin file or functions.php.
Expand Down
26 changes: 24 additions & 2 deletions schemas/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,31 @@
"px",
"em",
"rem",
"vh",
"%",
"vw",
"%"
"svw",
"lvw",
"dvw",
"vh",
"svh",
"lvh",
"dvh",
"vi",
"svi",
"lvi",
"dvi",
"vb",
"svb",
"lvb",
"dvb",
"vmin",
"svmin",
"lvmin",
"dvmin",
"vmax",
"svmax",
"lvmax",
"dvmax"
],
"default": "rem"
}
Expand Down
12 changes: 3 additions & 9 deletions theme/inc/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function theme() {

/**
* Check if the requirements for the current theme are met.
* The only requirement for the theme is the accompanying plugin.
*
* @return bool True if requirements are met, false otherwise.
*/
Expand All @@ -48,13 +49,6 @@ function theme_requirements_are_met() {
return false;
}

/**
* The Advanced Custom Fields plugin is required.
*/
if ( ! function_exists( 'get_field' ) ) {
return false;
}

return true;
}

Expand All @@ -63,7 +57,7 @@ function theme_requirements_are_met() {
*/
function requirements_template() {
if ( ! theme_requirements_are_met() ) {
wp_die( 'The requirements for this theme are not met.' );
wp_die( 'The requirements for this theme are not met. Please install and activate the accompanying plugin.' );
}
}
add_action( 'template_redirect', '\WpMunich\lhpbp\theme\requirements_template' );
Expand All @@ -77,7 +71,7 @@ function theme_requirements_notice__error() {
}

$class = 'notice notice-error';
$message = 'The requirements for this theme are not met.';
$message = 'The requirements for this theme are not met. Please install and activate the accompanying plugin.';

printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) );
}
Expand Down

0 comments on commit f48aec2

Please sign in to comment.