gutenberg-dependency-check.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. add_action( 'admin_notices', 'show_admin_messages' );
  3. function print_admin_message( $version ) {
  4. echo '<div id="message" class="error"><p><strong>';
  5. printf( __( 'The installed theme requires <a href="https://wordpress.org/plugins/gutenberg/">Gutenberg</a> version %s or higher.', 'livro' ), $version );
  6. echo '</strong></p></div>';
  7. }
  8. function show_admin_messages() {
  9. $metadata = file_get_contents( get_stylesheet_directory() . '/style.css' );
  10. preg_match( '/(?<=Requires Gutenberg:).+/', $metadata, $match );
  11. if ( 0 === sizeof( $match ) ) {
  12. return; // Gutenberg is not required
  13. }
  14. $version = trim( $match[0] );
  15. if ( ! defined( 'IS_GUTENBERG_PLUGIN' ) ) {
  16. print_admin_message( $version ); // Gutenberg is not activated
  17. return;
  18. }
  19. // Determine Gutenberg version from defined constant
  20. if ( defined( 'GUTENBERG_VERSION' ) ) {
  21. if ( version_compare( GUTENBERG_VERSION, $version ) < 0 ) {
  22. print_admin_message( $version );
  23. }
  24. return;
  25. }
  26. // We have confirmed that Gutenberg is installed and activated, however we cannot use the GUTENBERG_VERSION constant
  27. // (probably because we are in development mode)
  28. // we'll use the metadata from get_plugins() to determine the version of Gutenberg
  29. $plugins = get_plugins();
  30. foreach ( $plugins as $plugin ) {
  31. if ( 'Gutenberg' === $plugin['Name'] ) {
  32. if ( version_compare( trim( $plugin['Version'] ), $version ) < 0 ) {
  33. print_admin_message( $version );
  34. }
  35. return;
  36. }
  37. }
  38. // 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!
  39. }