This commit is contained in:
2024-05-20 15:37:46 +03:00
commit 00b7dbd0b7
10404 changed files with 3285853 additions and 0 deletions

View File

@ -0,0 +1,149 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Roles
*/
/**
* Abstract Role Manager template.
*/
abstract class WPSEO_Abstract_Role_Manager implements WPSEO_Role_Manager {
/**
* Registered roles.
*
* @var array
*/
protected $roles = [];
/**
* Registers a role.
*
* @param string $role Role to register.
* @param string $display_name Display name to use.
* @param string|null $template Optional. Role to base the new role on.
*
* @return void
*/
public function register( $role, $display_name, $template = null ) {
$this->roles[ $role ] = (object) [
'display_name' => $display_name,
'template' => $template,
];
}
/**
* Returns the list of registered roles.
*
* @return string[] List or registered roles.
*/
public function get_roles() {
return array_keys( $this->roles );
}
/**
* Adds the registered roles.
*
* @return void
*/
public function add() {
foreach ( $this->roles as $role => $data ) {
$capabilities = $this->get_capabilities( $data->template );
$capabilities = $this->filter_existing_capabilties( $role, $capabilities );
$this->add_role( $role, $data->display_name, $capabilities );
}
}
/**
* Removes the registered roles.
*
* @return void
*/
public function remove() {
$roles = array_keys( $this->roles );
array_map( [ $this, 'remove_role' ], $roles );
}
/**
* Returns the capabilities for the specified role.
*
* @param string $role Role to fetch capabilities from.
*
* @return array List of capabilities.
*/
protected function get_capabilities( $role ) {
if ( ! is_string( $role ) || empty( $role ) ) {
return [];
}
$wp_role = get_role( $role );
if ( ! $wp_role ) {
return [];
}
return $wp_role->capabilities;
}
/**
* Returns true if the capability exists on the role.
*
* @param WP_Role $role Role to check capability against.
* @param string $capability Capability to check.
*
* @return bool True if the capability is defined for the role.
*/
protected function capability_exists( WP_Role $role, $capability ) {
return ! array_key_exists( $capability, $role->capabilities );
}
/**
* Filters out capabilities that are already set for the role.
*
* This makes sure we don't override configurations that have been previously set.
*
* @param string $role The role to check against.
* @param array $capabilities The capabilities that should be set.
*
* @return array Capabilties that can be safely set.
*/
protected function filter_existing_capabilties( $role, array $capabilities ) {
if ( $capabilities === [] ) {
return $capabilities;
}
$wp_role = get_role( $role );
if ( ! $wp_role ) {
return $capabilities;
}
foreach ( $capabilities as $capability => $grant ) {
if ( $this->capability_exists( $wp_role, $capability ) ) {
unset( $capabilities[ $capability ] );
}
}
return $capabilities;
}
/**
* Adds a role to the system.
*
* @param string $role Role to add.
* @param string $display_name Name to display for the role.
* @param array $capabilities Capabilities to add to the role.
*
* @return void
*/
abstract protected function add_role( $role, $display_name, array $capabilities = [] );
/**
* Removes a role from the system.
*
* @param string $role Role to remove.
*
* @return void
*/
abstract protected function remove_role( $role );
}

View File

@ -0,0 +1,33 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Roles
*/
/**
* Role registration class.
*/
class WPSEO_Register_Roles implements WPSEO_WordPress_Integration {
/**
* Adds hooks.
*
* @return void
*/
public function register_hooks() {
add_action( 'wpseo_register_roles', [ $this, 'register' ] );
}
/**
* Registers the roles.
*
* @return void
*/
public function register() {
$role_manager = WPSEO_Role_Manager_Factory::get();
$role_manager->register( 'wpseo_manager', 'SEO Manager', 'editor' );
$role_manager->register( 'wpseo_editor', 'SEO Editor', 'editor' );
}
}

View File

@ -0,0 +1,27 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Roles
*/
/**
* Role Manager Factory.
*/
class WPSEO_Role_Manager_Factory {
/**
* Retrieves the Role manager to use.
*
* @return WPSEO_Role_Manager
*/
public static function get() {
static $manager = null;
if ( $manager === null ) {
$manager = new WPSEO_Role_Manager_WP();
}
return $manager;
}
}

View File

@ -0,0 +1,66 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Roles
*/
/**
* VIP implementation of the Role Manager.
*
* @deprecated 19.9
* @codeCoverageIgnore
*/
final class WPSEO_Role_Manager_VIP extends WPSEO_Abstract_Role_Manager {
/**
* Adds a role to the system.
*
* @deprecated 19.9
* @codeCoverageIgnore
*
* @param string $role Role to add.
* @param string $display_name Name to display for the role.
* @param array $capabilities Capabilities to add to the role.
*
* @return void
*/
protected function add_role( $role, $display_name, array $capabilities = [] ) {
_deprecated_function( __METHOD__, 'Yoast SEO 19.9' );
$enabled_capabilities = [];
$disabled_capabilities = [];
// Build lists of enabled and disabled capabilities.
foreach ( $capabilities as $capability => $grant ) {
if ( $grant ) {
$enabled_capabilities[] = $capability;
}
if ( ! $grant ) {
$disabled_capabilities[] = $capability;
}
}
wpcom_vip_add_role( $role, $display_name, $enabled_capabilities );
if ( $disabled_capabilities !== [] ) {
wpcom_vip_remove_role_caps( $role, $disabled_capabilities );
}
}
/**
* Removes a role from the system.
*
* @deprecated 19.9
* @codeCoverageIgnore
*
* @param string $role Role to remove.
*
* @return void
*/
protected function remove_role( $role ) {
_deprecated_function( __METHOD__, 'Yoast SEO 19.9' );
remove_role( $role );
}
}

View File

@ -0,0 +1,61 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Roles
*/
/**
* WordPress' default implementation of the Role Manager.
*/
final class WPSEO_Role_Manager_WP extends WPSEO_Abstract_Role_Manager {
/**
* Adds a role to the system.
*
* @param string $role Role to add.
* @param string $display_name Name to display for the role.
* @param array $capabilities Capabilities to add to the role.
*
* @return void
*/
protected function add_role( $role, $display_name, array $capabilities = [] ) {
$wp_role = get_role( $role );
if ( $wp_role ) {
foreach ( $capabilities as $capability => $grant ) {
$wp_role->add_cap( $capability, $grant );
}
return;
}
add_role( $role, $display_name, $capabilities );
}
/**
* Removes a role from the system.
*
* @param string $role Role to remove.
*
* @return void
*/
protected function remove_role( $role ) {
remove_role( $role );
}
/**
* Formats the capabilities to the required format.
*
* @param array $capabilities Capabilities to format.
* @param bool $enabled Whether these capabilities should be enabled or not.
*
* @return array Formatted capabilities.
*/
protected function format_capabilities( array $capabilities, $enabled = true ) {
// Flip keys and values.
$capabilities = array_flip( $capabilities );
// Set all values to $enabled.
return array_fill_keys( array_keys( $capabilities ), $enabled );
}
}

View File

@ -0,0 +1,44 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Roles
*/
/**
* Role Manager interface.
*/
interface WPSEO_Role_Manager {
/**
* Registers a role.
*
* @param string $role Role to register.
* @param string $display_name Display name to use.
* @param string|null $template Optional. Role to base the new role on.
*
* @return void
*/
public function register( $role, $display_name, $template = null );
/**
* Adds the registered roles.
*
* @return void
*/
public function add();
/**
* Removes the registered roles.
*
* @return void
*/
public function remove();
/**
* Returns the list of registered roles.
*
* @return string[] List or registered roles.
*/
public function get_roles();
}