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

Add hooks for create and delete sites. #7494

Closed
wants to merge 1 commit 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
14 changes: 13 additions & 1 deletion includes/Database_Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this runs on admin_init, it won't necessarily be available during activation or initialization (think WP-CLI context).

We could change the registration action to init though and call is_admin() in register().
activate() would then run the migration directly and not call register().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Again, this is what I get for developing something at 11pm without testing it.


/**
* The slug of database option.
Expand Down Expand Up @@ -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.
*
Expand Down
42 changes: 42 additions & 0 deletions includes/Infrastructure/Delete_Site.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Interface Delete_Site.
*
* @package Google\Web_Stories
* @copyright 2019 Alain Schlesser
* @license MIT
* @link https://www.mwpd.io/
*/

/**
* Original code modified for this project.
*
* @copyright 2021 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
*/

namespace Google\Web_Stories\Infrastructure;

/**
* Something that can be activated.
*
* By tagging a service with this interface, the system will automatically hook
* it up to the WordPress activation hook.
*
* This way, we can just add the simple interface marker and not worry about how
* to wire up the code to reach that part during the static activation hook.
*
* @since 1.8.0
* @internal
*/
interface Delete_Site {

/**
* Delete the service on new site ( Multisite only )
*
* @since 1.8.0
*
* @return void
*/
public function delete_site();
}
42 changes: 42 additions & 0 deletions includes/Infrastructure/Initialize_Site.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Interface Initialize_Site.
*
* @package Google\Web_Stories
* @copyright 2019 Alain Schlesser
* @license MIT
* @link https://www.mwpd.io/
*/

/**
* Original code modified for this project.
*
* @copyright 2021 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
*/

namespace Google\Web_Stories\Infrastructure;

/**
* Something that can be activated.
*
* By tagging a service with this interface, the system will automatically hook
* it up to the WordPress activation hook.
*
* This way, we can just add the simple interface marker and not worry about how
* to wire up the code to reach that part during the static activation hook.
*
* @since 1.8.0
* @internal
*/
interface Initialize_Site {

/**
* Initialize the service on new site ( Multisite only )
*
* @since 1.8.0
*
* @return void
*/
public function initialize_site();
}
34 changes: 34 additions & 0 deletions includes/Infrastructure/ServiceBasedPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,40 @@ public function deactivate( $network_wide ) {
rewrite_flush();
}

/**
* On new site creation ( Multisite only )
*
* @since 1.8.0
*
* @return void
*/
public function new_site() {
$this->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.
*
Expand Down
16 changes: 15 additions & 1 deletion includes/Story_Post_Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down
26 changes: 25 additions & 1 deletion includes/User/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -36,7 +38,7 @@
*
* @package Google\Web_Stories\User
*/
class Capabilities implements Activateable, Deactivateable {
class Capabilities implements Activateable, Deactivateable, Initialize_Site, Delete_Site {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this will have any effect, because Capabilities is not a service, and these interfaces are all meant to be used on services.

Since it`s not a service, there's no way the plugin can know it needs to instantiate it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I was looking into this last night. You are right as the unit tests failed. It is a nice pattern, so I would like to use it.

Copy link
Contributor

Choose a reason for hiding this comment

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

You should be able to just turn Capabilities into a Service without much work. Just make sure you check all the places where you're using it to ensure it still works, and keep in mind that it will be shared by default if retrieved through the Services container, so it might make sense to just build it as a shared service right away.

/**
* Activate the service.
*
Expand All @@ -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.
*
Expand Down
36 changes: 2 additions & 34 deletions includes/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 );
Expand All @@ -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();
}

Expand Down