diff --git a/includes/Database_Upgrader.php b/includes/Database_Upgrader.php index 349fa90f08e7..55e05f490eca 100644 --- a/includes/Database_Upgrader.php +++ b/includes/Database_Upgrader.php @@ -27,6 +27,7 @@ namespace Google\Web_Stories; use Google\Web_Stories\Infrastructure\Activateable; +use Google\Web_Stories\Infrastructure\Initialize_Site; use Google\Web_Stories\Infrastructure\Injector; use Google\Web_Stories\Infrastructure\Service; @@ -35,7 +36,7 @@ * * @package Google\Web_Stories */ -class Database_Upgrader extends Service_Base implements Activateable { +class Database_Upgrader extends Service_Base implements Activateable, Initialize_Site { /** * The slug of database option. @@ -111,6 +112,17 @@ public function activate( $network_wide ) { $this->register(); } + /** + * Initialize the service on new site ( Multisite only ) + * + * @since 1.8.0 + * + * @return void + */ + public function initialize_site() { + $this->register(); + } + /** * Get the action to use for registering the service. * diff --git a/includes/Infrastructure/Delete_Site.php b/includes/Infrastructure/Delete_Site.php new file mode 100644 index 000000000000..151300a2c467 --- /dev/null +++ b/includes/Infrastructure/Delete_Site.php @@ -0,0 +1,42 @@ +register_services(); + + foreach ( $this->service_container as $service ) { + if ( $service instanceof Initialize_Site ) { + $service->initialize_site(); + } + } + } + + /** + * On site deletion ( Multisite only ) + * + * @since 1.8.0 + * + * @return void + */ + public function delete_site() { + $this->register_services(); + + foreach ( $this->service_container as $service ) { + if ( $service instanceof Delete_Site ) { + $service->delete_site(); + } + } + } + /** * Register the plugin with the WordPress system. * diff --git a/includes/Story_Post_Type.php b/includes/Story_Post_Type.php index c6917a9e91f7..1b9b964f9d3a 100644 --- a/includes/Story_Post_Type.php +++ b/includes/Story_Post_Type.php @@ -27,6 +27,7 @@ namespace Google\Web_Stories; use Google\Web_Stories\Infrastructure\Deactivateable; +use Google\Web_Stories\Infrastructure\Initialize_Site; use Google\Web_Stories\REST_API\Stories_Controller; use WP_Post_Type; use WP_Rewrite; @@ -35,7 +36,7 @@ /** * Class Story_Post_Type. */ -class Story_Post_Type extends Service_Base implements Deactivateable { +class Story_Post_Type extends Service_Base implements Deactivateable, Initialize_Site { /** * The slug of the stories post type. @@ -140,6 +141,19 @@ public function register() { add_filter( 'bulk_post_updated_messages', [ $this, 'bulk_post_updated_messages' ], 10, 2 ); } + + /** + * Initialize the service on new site ( Multisite only ) + * + * @since 1.8.0 + * + * @return void + */ + public function initialize_site() { + $this->register(); + rewrite_flush(); + } + /** * Deactivate the service. * diff --git a/includes/User/Capabilities.php b/includes/User/Capabilities.php index 033c9dd9e873..7071fbefada3 100644 --- a/includes/User/Capabilities.php +++ b/includes/User/Capabilities.php @@ -28,6 +28,8 @@ use Google\Web_Stories\Infrastructure\Activateable; use Google\Web_Stories\Infrastructure\Deactivateable; +use Google\Web_Stories\Infrastructure\Delete_Site; +use Google\Web_Stories\Infrastructure\Initialize_Site; use Google\Web_Stories\Story_Post_Type; use WP_Role; @@ -36,7 +38,7 @@ * * @package Google\Web_Stories\User */ -class Capabilities implements Activateable, Deactivateable { +class Capabilities implements Activateable, Deactivateable, Initialize_Site, Delete_Site { /** * Activate the service. * @@ -57,6 +59,28 @@ public function deactivate( $network_wide ) { $this->remove_caps_from_roles(); } + /** + * Initialize the service on new site ( Multisite only ) + * + * @since 1.8.0 + * + * @return void + */ + public function initialize_site() { + $this->add_caps_to_roles(); + } + + /** + * Delete the service on new site ( Multisite only ) + * + * @since 1.8.0 + * + * @return void + */ + public function delete_site() { + $this->remove_caps_from_roles(); + } + /** * Adds story capabilities to default user roles. * diff --git a/includes/namespace.php b/includes/namespace.php index 021582431701..6d27f44891b6 100644 --- a/includes/namespace.php +++ b/includes/namespace.php @@ -30,32 +30,6 @@ use WP_Error; use WP_Site; -/** - * Run logic to setup a new site with web stories. - * - * @since 1.2.0 - * - * @return void - */ -function setup_new_site() { - $injector = Services::get_injector(); - if ( ! method_exists( $injector, 'make' ) ) { - return; - } - $story = $injector->make( Story_Post_Type::class ); - $story->register(); - // TODO Register cap to roles within class itself. - $capabilities = $injector->make( User\Capabilities::class ); - $capabilities->add_caps_to_roles(); - rewrite_flush(); - - // Not using Services::get(...) because the class is only registered on 'admin_init', which we might not be in here. - // TODO move this logic to Database_Upgrader class. - $database_upgrader = $injector->make( Database_Upgrader::class ); - $database_upgrader->register(); -} - - /** * Flush rewrites. * @@ -107,7 +81,7 @@ function new_site( $site ) { } $site_id = (int) $site->blog_id; switch_to_blog( $site_id ); - setup_new_site(); + get_plugin_instance()->new_site(); restore_current_blog(); } add_action( 'wp_initialize_site', __NAMESPACE__ . '\new_site', PHP_INT_MAX ); @@ -132,15 +106,9 @@ function remove_site( $error, $site ) { return; } - $injector = Services::get_injector(); - if ( ! method_exists( $injector, 'make' ) ) { - return; - } - $story = $injector->make( Story_Post_Type::class ); - $site_id = (int) $site->blog_id; switch_to_blog( $site_id ); - $story->remove_caps_from_roles(); + get_plugin_instance()->delete_site(); restore_current_blog(); }