Skip to content

Commit da9130c

Browse files
authored
Merge pull request #3 from BeAPI/feature/multisite-checks
Various refactor of the plugin boostrap file
2 parents 43c00d0 + 67d379a commit da9130c

File tree

2 files changed

+51
-40
lines changed

2 files changed

+51
-40
lines changed

includes/Main.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

multisite-shared-blocks.php

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
* Network: true
1515
*/
1616

17+
namespace Beapi\MultisiteSharedBlocks;
18+
19+
use Beapi\MultisiteSharedBlocks\Admin\Main as AdminMain;
20+
use Beapi\MultisiteSharedBlocks\Blocks\SharedBlock;
21+
use Beapi\MultisiteSharedBlocks\Gutenberg\SharedBlocksAttributes;
22+
use Beapi\MultisiteSharedBlocks\Rest\Rest;
23+
1724
// Don't load directly
1825
if ( ! defined( 'ABSPATH' ) ) {
1926
die( '-1' );
@@ -33,29 +40,62 @@
3340
define( 'MULTISITE_SHARED_BLOCKS_DIR', plugin_dir_path( __FILE__ ) );
3441
define( 'MULTISITE_SHARED_BLOCKS_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
3542

43+
// Plugin only support multisite installation.
44+
if ( ! is_multisite() ) {
45+
add_action( 'admin_init', __NAMESPACE__ . '\\handle_multisite_support_issue' );
46+
47+
return;
48+
}
49+
3650
register_activation_hook( __FILE__, [ \Beapi\MultisiteSharedBlocks\Plugin::class, 'activate' ] );
3751

38-
add_action( 'plugins_loaded', 'init_multisite_shared_blocks_plugin' );
52+
add_action( 'plugins_loaded', __NAMESPACE__ . '\\init' );
3953
/**
4054
* Init the plugin
4155
*/
42-
function init_multisite_shared_blocks_plugin(): void {
56+
function init(): void {
4357

4458
// Register custom table and check for schema updates
45-
\Beapi\MultisiteSharedBlocks\Database::setup();
59+
Database::setup();
4660

47-
\Beapi\MultisiteSharedBlocks\Main::get_instance();
48-
\Beapi\MultisiteSharedBlocks\Gutenberg\SharedBlocksAttributes::get_instance();
49-
\Beapi\MultisiteSharedBlocks\Blocks\SharedBlock::get_instance();
50-
\Beapi\MultisiteSharedBlocks\Listener::get_instance();
51-
\Beapi\MultisiteSharedBlocks\Rest\Rest::get_instance();
52-
\Beapi\MultisiteSharedBlocks\Preview::get_instance();
61+
SharedBlocksAttributes::get_instance();
62+
SharedBlock::get_instance();
63+
Listener::get_instance();
64+
Rest::get_instance();
65+
Preview::get_instance();
5366

5467
if ( is_admin() ) {
55-
\Beapi\MultisiteSharedBlocks\Admin\Main::get_instance();
68+
AdminMain::get_instance();
5669
}
5770

5871
if ( defined( 'WP_CLI' ) && WP_CLI ) {
59-
\WP_CLI::add_command( 'multisite-shared-blocks', \Beapi\MultisiteSharedBlocks\Cli::class );
72+
\WP_CLI::add_command( 'multisite-shared-blocks', Cli::class );
73+
}
74+
}
75+
76+
/**
77+
* Handle multisite support issue.
78+
*
79+
* Deactivate the plugin and display an admin notices to users if they are able to manage plugins.
80+
*
81+
* @return void
82+
*/
83+
function handle_multisite_support_issue(): void {
84+
if ( wp_doing_ajax() || ! current_user_can( 'activate_plugins' ) ) {
85+
return;
6086
}
87+
88+
// Deactivate plugin
89+
deactivate_plugins( __FILE__ );
90+
unset( $_GET['activate'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended
91+
92+
// Display admin notices
93+
add_action(
94+
'admin_notices',
95+
function () {
96+
echo '<div class="notice error">';
97+
echo sprintf( '<p>%s</p>', esc_html__( 'Multisite Shared Blocks require a multisite installation to work.', 'multisite-shared-blocks' ) );
98+
echo '</div>';
99+
}
100+
);
61101
}

0 commit comments

Comments
 (0)