Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Add a notice when requirements aren’t met #75

Merged
merged 4 commits into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions gutenberg-ramp.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
include __DIR__ . '/inc/class-gutenberg-ramp.php';
include __DIR__ . '/inc/class-gutenberg-ramp-criteria.php';
include __DIR__ . '/inc/admin/class-gutenberg-ramp-post-type-settings-ui.php';
include __DIR__ . '/inc/admin/class-gutenberg-ramp-compatibility-check.php';

/**
* This function allows themes to specify Gutenberg loading critera.
Expand Down Expand Up @@ -111,6 +112,23 @@ function ramp_for_gutenberg_load_gutenberg( $criteria = false ) {
gutenberg_ramp_load_gutenberg( $criteria );
}

/**
* @return bool|string
*/
function gutenberg_ramp_get_validated_gutenberg_load_path() {

$gutenberg_plugin_path = apply_filters( 'gutenberg_ramp_gutenberg_load_path', WP_PLUGIN_DIR . '/gutenberg/gutenberg.php' );

if ( empty( $gutenberg_plugin_path )
|| 0 !== validate_file( $gutenberg_plugin_path )
|| ! file_exists( $gutenberg_plugin_path )
) {
return false;
}

return $gutenberg_plugin_path;
}

/**
* Ramp expects Gutenberg to be active
* This function is going to load Gutenberg plugin if it's not already active
Expand All @@ -129,25 +147,26 @@ function gutenberg_ramp_require_gutenberg() {

// perform any actions required before loading gutenberg
do_action( 'gutenberg_ramp_before_load_gutenberg' );
$gutenberg_include = apply_filters( 'gutenberg_ramp_gutenberg_load_path', WP_PLUGIN_DIR . '/gutenberg/gutenberg.php' );

if ( false === $gutenberg_include
|| 0 !== validate_file( $gutenberg_include )
|| ! file_exists( $gutenberg_include )
) {
return false;
$validated_load_path = gutenberg_ramp_get_validated_gutenberg_load_path();
if ( false !== $validated_load_path ) {
include_once $validated_load_path;
}

include_once $gutenberg_include;
return true;

}

/**
* Rquire Gutenberg Plugin
* Require Gutenberg Plugin
*/
gutenberg_ramp_require_gutenberg();

if ( Gutenberg_Ramp_Compatibility_Check::should_check_compatibility() ) {
$ramp_compatibility = new Gutenberg_Ramp_Compatibility_Check();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we skip loading the rest of Ramp if the versions don't match?

Copy link
Member Author

@pyronaur pyronaur Nov 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary. If Ramp isn't compatible - it doesn't do anything apart from attaching itself to 2 filters that will never be called, but it still does allow you to interact with ramp helper the you would normally.

I'm hesitant to change this because this is an edge case we're talking about. Refactoring the plugin to activate on a condition just because of an edge seems like a bit of overkill.

Ramp checks for whether it should be checking for compatibility at all, and if it should - it checks the compatibility and adds a notice - leaving the rest of the Ramp behavior as it was previously. It's simple, easy, and it works.

add_action( 'admin_init', [ $ramp_compatibility, 'maybe_display_notice' ] );
}

/**
* Initialize Gutenberg Ramp
*/
Expand Down
69 changes: 69 additions & 0 deletions inc/admin/class-gutenberg-ramp-compatibility-check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

class Gutenberg_Ramp_Compatibility_Check {

/**
* Gutenberg Compatibility should only be checked in WordPress < 5.0,
* because Core 5.0 and Gutenberg Plugin < 4.1.0 are not compatible anyway,
* and Gutenberg_Ramp_Compatibility_Check is only needed to check for Gutenberg Plugin < 3.5.0
*/
public static function should_check_compatibility() {

return apply_filters( 'gutenberg_ramp_should_check_compatibility', version_compare( $GLOBALS['wp_version'], '5.0', '<' ) );
}

public static function should_display_message() {

$gutenberg_version = static::get_gutenberg_version();

/**
* If no Gutenberg version is found, don't display a notice
* Because this class only cares about a specific condition ( GB < 3.5 )
*/
if ( false === $gutenberg_version ) {
return false;
}

return version_compare( $gutenberg_version, '3.5', '<' );
}

public static function get_gutenberg_version() {

$gutenberg_plugin_path = gutenberg_ramp_get_validated_gutenberg_load_path();
if ( false === $gutenberg_plugin_path ) {
return false;
}

$gutenberg_data = get_plugin_data( $gutenberg_plugin_path, false, false );

if ( empty( $gutenberg_data['Version'] ) ) {
return false;
}

return $gutenberg_data['Version'];
}


// The backup sanity check, in case the plugin is activated in a weird way,
// or the versions change after activation.
public function maybe_display_notice() {

if ( self::should_display_message() ) {
add_action( 'admin_notices', [ $this, 'display_notice' ] );
}
}

public function display_notice() {

?>
<div class="notice notice-error is-dismissible">
<p>
<strong> <?php esc_html_e( 'Gutenberg Ramp functionality is disabled' ); ?></strong> <br/>
<?php esc_html_e( 'The version of Gutenberg you have installed is not compatible with Gutenberg Ramp. To restore Ramp functionality, please upgrade to Gutenberg 4.1 (or newer) or WordPress 5.0 (or newer).', 'gutenberg-ramp' ) ?>
</p>
</div>
<?php

}
}