add wp-rocket

This commit is contained in:
nguyen dung
2022-02-18 19:09:35 +07:00
parent 39b8cb3612
commit 3110d00ee7
927 changed files with 271703 additions and 2 deletions

View File

@@ -0,0 +1,168 @@
<?php
namespace WP_Rocket\Engine\Capabilities;
use WP_Rocket\Engine\Activation\ActivationInterface;
use WP_Rocket\Engine\Deactivation\DeactivationInterface;
class Manager implements ActivationInterface, DeactivationInterface {
/**
* List of WP Rocket capabilities
*
* @var array
*/
private $capabilities = [
'rocket_manage_options',
'rocket_purge_cache',
'rocket_purge_posts',
'rocket_purge_terms',
'rocket_purge_users',
'rocket_purge_opcache',
'rocket_purge_cloudflare_cache',
'rocket_purge_sucuri_cache',
'rocket_preload_cache',
'rocket_regenerate_critical_css',
];
/**
* Gets the WP Rocket capabilities
*
* @since 3.4
*
* @return array
*/
private function get_capabilities() {
return $this->capabilities;
}
/**
* Performs these actions during the plugin activation
*
* @return void
*/
public function activate() {
add_action( 'rocket_activation', [ $this, 'add_rocket_capabilities' ] );
}
/**
* Performs these actions during the plugin deactivation
*
* @return void
*/
public function deactivate() {
add_action( 'rocket_deactivation', [ $this, 'remove_rocket_capabilities' ] );
}
/**
* Add WP Rocket capabilities to the administrator role
*
* @since 3.4
*
* @return void
*/
public function add_rocket_capabilities() {
$role = $this->get_administrator_role_object();
if ( is_null( $role ) ) {
return;
}
foreach ( $this->get_capabilities() as $cap ) {
$role->add_cap( $cap );
}
}
/**
* Remove WP Rocket capabilities from the administrator role
*
* @since 3.4
*
* @return void
*/
public function remove_rocket_capabilities() {
$role = $this->get_administrator_role_object();
if ( is_null( $role ) ) {
return;
}
foreach ( $this->get_capabilities() as $cap ) {
$role->remove_cap( $cap );
}
}
/**
* Sets the capability for the options page.
*
* @since 3.4
*
* @param string $capability The capability used for the page, which is manage_options by default.
* @return string
*/
public function required_capability( $capability ) {
return 'rocket_manage_options';
}
/**
* Add WP Rocket capabilities to User Role Editor
*
* @since 3.4
*
* @param array $caps Array of existing capabilities.
* @return array
*/
public function add_caps_to_ure( $caps ) {
foreach ( $this->get_capabilities() as $cap ) {
$caps[ $cap ] = [
'custom',
'wp_rocket',
];
}
return $caps;
}
/**
* Add WP Rocket as a group in User Role Editor
*
* @since 3.4
*
* @param array $groups Array of existing groups.
* @return array
*/
public function add_group_to_ure( $groups ) {
$groups['wp_rocket'] = [
'caption' => esc_html( 'WP Rocket' ),
'parent' => 'custom',
'level' => 2,
];
return $groups;
}
/**
* Adds WP Rocket capabilities on plugin upgrade
*
* @since 3.6.3
*
* @param string $wp_rocket_version Latest WP Rocket version.
* @param string $actual_version Installed WP Rocket version.
* @return void
*/
public function add_capabilities_on_upgrade( $wp_rocket_version, $actual_version ) {
if ( version_compare( $actual_version, '3.4.0.1', '<' ) ) {
$this->add_rocket_capabilities();
}
}
/**
* Returns the object for the administrator roll
*
* @since 3.6.3
*
* @return WP_Role|null
*/
private function get_administrator_role_object() {
return get_role( 'administrator' );
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace WP_Rocket\Engine\Capabilities;
use WP_Rocket\Engine\Container\ServiceProvider\AbstractServiceProvider;
/**
* Service Provider for capabilities
*
* @since 3.6.3
*/
class ServiceProvider extends AbstractServiceProvider {
/**
* The provides array is a way to let the container
* know that a service is provided by this service
* provider. Every service that is registered via
* this service provider must have an alias added
* to this array or it will be ignored.
*
* @var array
*/
protected $provides = [
'capabilities_manager',
'capabilities_subscriber',
];
/**
* Registers the option array in the container
*
* @return void
*/
public function register() {
$this->getContainer()->add( 'capabilities_manager', 'WP_Rocket\Engine\Capabilities\Manager' );
$this->getContainer()->share( 'capabilities_subscriber', 'WP_Rocket\Engine\Capabilities\Subscriber' )
->withArgument( $this->getContainer()->get( 'capabilities_manager' ) );
}
}

View File

@@ -0,0 +1,94 @@
<?php
namespace WP_Rocket\Engine\Capabilities;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Manage WP Rocket custom capabilities
*
* @since 3.4
*/
class Subscriber implements Subscriber_Interface {
/**
* Capabilities manager instance
*
* @var Manager
*/
private $capabilities;
/**
* Instantiate the subscriber
*
* @param Manager $capabilities Capabilities manager instance.
*/
public function __construct( Manager $capabilities ) {
$this->capabilities = $capabilities;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @since 3.4
*
* @return array
*/
public static function get_subscribed_events() {
$slug = rocket_get_constant( 'WP_ROCKET_PLUGIN_SLUG' );
return [
"option_page_capability_{$slug}" => 'required_capability',
'ure_built_in_wp_caps' => 'add_caps_to_ure',
'ure_capabilities_groups_tree' => 'add_group_to_ure',
'wp_rocket_upgrade' => [ 'add_capabilities_on_upgrade', 12, 2 ],
];
}
/**
* Sets the capability for the options page.
*
* @since 3.4
*
* @param string $capability The capability used for the page, which is manage_options by default.
* @return string
*/
public function required_capability( $capability ) {
return $this->capabilities->required_capability( $capability );
}
/**
* Adds WP Rocket capabilities to User Role Editor
*
* @since 3.4
*
* @param array $caps Array of existing capabilities.
* @return array
*/
public function add_caps_to_ure( $caps ) {
return $this->capabilities->add_caps_to_ure( $caps );
}
/**
* Adds WP Rocket as a group in User Role Editor
*
* @since 3.4
*
* @param array $groups Array of existing groups.
* @return array
*/
public function add_group_to_ure( $groups ) {
return $this->capabilities->add_group_to_ure( $groups );
}
/**
* Adds WP Rocket capabilities on plugin upgrade
*
* @since 3.6.3
*
* @param string $wp_rocket_version Latest WP Rocket version.
* @param string $actual_version Installed WP Rocket version.
* @return void
*/
public function add_capabilities_on_upgrade( $wp_rocket_version, $actual_version ) {
$this->capabilities->add_capabilities_on_upgrade( $wp_rocket_version, $actual_version );
}
}