Added a dependency check for Gutenberg (#5718)

Co-authored-by: Jeff Ong <jonger4@gmail.com>
This commit is contained in:
Jason Crist 2022-03-23 12:24:51 -04:00 committed by GitHub
parent e03bb1c149
commit 0840ea6e52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 0 deletions

View file

@ -64,3 +64,5 @@ add_action( 'wp_enqueue_scripts', 'livro_styles' );
// Add block patterns
require get_template_directory() . '/inc/block-patterns.php';
require get_template_directory() . '/inc/gutenberg-dependency-check.php';

View file

@ -0,0 +1,45 @@
<?php
add_action( 'admin_notices', 'show_admin_messages' );
function print_admin_message( $version ) {
echo '<div id="message" class="error"><p><strong>';
printf( __( 'The installed theme requires <a href="https://wordpress.org/plugins/gutenberg/">Gutenberg</a> version %s or higher.', 'livro' ), $version );
echo '</strong></p></div>';
}
function show_admin_messages() {
$metadata = file_get_contents( get_stylesheet_directory() . '/style.css' );
preg_match( '/(?<=Requires Gutenberg:).+/', $metadata, $match );
if ( 0 === sizeof( $match ) ) {
return; // Gutenberg is not required
}
$version = trim( $match[0] );
if ( ! defined( 'IS_GUTENBERG_PLUGIN' ) ) {
print_admin_message( $version ); // Gutenberg is not activated
return;
}
// Determine Gutenberg version from defined constant
if ( defined( 'GUTENBERG_VERSION' ) ) {
if ( version_compare( GUTENBERG_VERSION, $version ) < 0 ) {
print_admin_message( $version );
}
return;
}
// We have confirmed that Gutenberg is installed and activated, however we cannot use the GUTENBERG_VERSION constant
// (probably because we are in development mode)
// we'll use the metadata from get_plugins() to determine the version of Gutenberg
$plugins = get_plugins();
foreach ( $plugins as $plugin ) {
if ( 'Gutenberg' === $plugin['Name'] ) {
if ( version_compare( trim( $plugin['Version'] ), $version ) < 0 ) {
print_admin_message( $version );
}
return;
}
}
// We weren't able to confirm the version, however we do know that it's installed and activated so we'll just hope for the best!
}

View file

@ -7,6 +7,7 @@ Description: Livro is a simple theme designed to evoke the calm feeling you get
Requires at least: 5.8
Tested up to: 5.8.3
Requires PHP: 5.6
Requires Gutenberg: 12.8
Version: 1.0.9
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html