Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully handle multiple installed copies of the Gutenberg plugin #1020

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 5 additions & 0 deletions gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
* @package gutenberg
*/

require_once dirname( __FILE__ ) . '/lib/check-duplicate-plugins.php';
if ( defined( 'GUTENBERG_MULTIPLE_COPIES' ) && GUTENBERG_MULTIPLE_COPIES ) {
return;
Copy link
Member

Choose a reason for hiding this comment

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

Interested to find some weird repercussions of return used outside functions like this:

http://php.net/manual/en/function.return.php#112515

I suppose it's not a problem here because the functions aren't truly part of the same file. Perhaps fragile all the same though, if ever we were to add any functions to this file, since we'll encounter the same problem meant to be addressed by these changes (duplicate function declaration).

Better solution might be to wrap the require_once with the condition instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

Our plugin entry point should do as little as possible, only constants and requires and perhaps some very simple checks like in this PR.

}

require_once dirname( __FILE__ ) . '/lib/blocks.php';
require_once dirname( __FILE__ ) . '/lib/client-assets.php';
require_once dirname( __FILE__ ) . '/lib/i18n.php';
Expand Down
28 changes: 28 additions & 0 deletions lib/check-duplicate-plugins.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Make sure we only have one copy of the Gutenberg plugin active.
*
* @package gutenberg
*/

if ( ! defined( 'ABSPATH' ) ) {
die( 'Silence is golden.' );
}

if ( function_exists( 'the_gutenberg_project' ) ) {
if ( ! defined( 'GUTENBERG_MULTIPLE_COPIES' ) ) {
/**
* Displays an admin notice indicating a problem with the plugin.
*/
function gutenberg_multiple_copies_admin_notice() {
echo '<div class="notice notice-error"><p>';
_e(
'You have multiple copies of the Gutenberg plugin active. Please deactivate or delete all but one copy.',
'gutenberg'
);
echo '</p></div>';
}
add_action( 'admin_notices', 'gutenberg_multiple_copies_admin_notice' );
define( 'GUTENBERG_MULTIPLE_COPIES', true );
}
}