Version en ingles y español
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Polylang-Pro
|
||||
*/
|
||||
|
||||
namespace WP_Syntex\Polylang_Pro\Modules\Capabilities;
|
||||
|
||||
use WP_User;
|
||||
use PLL_Base;
|
||||
use PLL_Model;
|
||||
use PLL_Language;
|
||||
use WP_Syntex\Polylang\Capabilities\Capabilities;
|
||||
use WP_Syntex\Polylang\Capabilities\User\NOOP;
|
||||
use WP_Syntex\Polylang\Capabilities\User\User_Interface;
|
||||
use WP_Syntex\Polylang_Pro\Modules\Capabilities\Mapper\Term;
|
||||
use WP_Syntex\Polylang_Pro\Modules\Capabilities\Mapper\Post;
|
||||
use WP_Syntex\Polylang_Pro\Modules\Capabilities\Mapper\Internal;
|
||||
use WP_Syntex\Polylang_Pro\Modules\Capabilities\User\Translator;
|
||||
use WP_Syntex\Polylang_Pro\Modules\Capabilities\Mapper\Do_Not_Allow;
|
||||
use WP_Syntex\Polylang_Pro\Modules\Capabilities\Mapper\Abstract_Mapper;
|
||||
use WP_Syntex\Polylang_Pro\Modules\Capabilities\Mapper\Machine_Translation;
|
||||
use WP_Syntex\Polylang_Pro\Modules\Capabilities\Create\Post as Create_Post;
|
||||
use WP_Syntex\Polylang_Pro\Modules\Capabilities\Create\Term as Create_Term;
|
||||
|
||||
/**
|
||||
* Class to orchestrate the capabilities mappers.
|
||||
*
|
||||
* @since 3.8
|
||||
*/
|
||||
class Advanced_Capabilities {
|
||||
/**
|
||||
* @var Abstract_Mapper
|
||||
*/
|
||||
private $mapper_chain;
|
||||
|
||||
/**
|
||||
* @var Create_Post
|
||||
*/
|
||||
public $create_post;
|
||||
|
||||
/**
|
||||
* @var Create_Term
|
||||
*/
|
||||
public $create_term;
|
||||
|
||||
/**
|
||||
* @var PLL_Model
|
||||
*/
|
||||
private $model;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param PLL_Base $polylang Polylang instance.
|
||||
*/
|
||||
public function __construct( PLL_Base $polylang ) {
|
||||
$this->model = $polylang->model;
|
||||
$this->create_post = (
|
||||
new Create_Post(
|
||||
$polylang->model,
|
||||
$polylang->request,
|
||||
property_exists( $polylang, 'pref_lang' ) ? $polylang->pref_lang : null,
|
||||
property_exists( $polylang, 'curlang' ) ? $polylang->curlang : null
|
||||
)
|
||||
)->init();
|
||||
$this->create_term = (
|
||||
new Create_Term(
|
||||
$polylang->model,
|
||||
$polylang->request,
|
||||
property_exists( $polylang, 'pref_lang' ) ? $polylang->pref_lang : null,
|
||||
property_exists( $polylang, 'curlang' ) ? $polylang->curlang : null
|
||||
)
|
||||
)->init();
|
||||
|
||||
$this->mapper_chain = new Do_Not_Allow();
|
||||
$this->mapper_chain
|
||||
->set_next( new Post( $polylang->model, $polylang->sync_post_model ) )
|
||||
->set_next( new Term( $polylang->model ) )
|
||||
->set_next( new Machine_Translation() )
|
||||
->set_next( new Internal( Capabilities::LANGUAGES ) )
|
||||
->set_next( new Internal( Capabilities::TRANSLATIONS ) );
|
||||
|
||||
add_filter( 'map_meta_cap', array( $this, 'map' ), 10, 4 );
|
||||
add_filter( 'pll_admin_preferred_language', array( $this, 'filter_preferred_language' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps a capability to the primitive capabilities required of the given user.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string[] $caps Primitive capabilities required of the user.
|
||||
* @param string $cap Capability being checked.
|
||||
* @param int $user_id The user ID.
|
||||
* @param array $args Adds context to the capability check, typically
|
||||
* starting with an object ID.
|
||||
* @return string[] Updated primitive capabilities required of the user.
|
||||
*/
|
||||
public function map( $caps, $cap, $user_id, $args ) {
|
||||
if ( ! is_array( $caps ) || ! is_string( $cap ) || ! is_numeric( $user_id ) || ! is_array( $args ) ) {
|
||||
// Type safety.
|
||||
return $caps;
|
||||
}
|
||||
|
||||
return $this->mapper_chain->map(
|
||||
$caps,
|
||||
$cap,
|
||||
Capabilities::get_user( new WP_User( $user_id ) ),
|
||||
$args
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the preferred language to ensure it's authorized for translators.
|
||||
* - If the preferred language is authorized, keeps it.
|
||||
* - Otherwise, returns the first authorized language.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param PLL_Language|false $pref_lang The preferred language.
|
||||
* @return PLL_Language|false The authorized preferred language, or false if none available.
|
||||
*/
|
||||
public function filter_preferred_language( $pref_lang ) {
|
||||
if ( $pref_lang instanceof PLL_Language ) {
|
||||
$user = Capabilities::get_user();
|
||||
if ( $user->can_translate( $pref_lang ) ) {
|
||||
return $pref_lang;
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, return the first authorized language.
|
||||
$authorized_languages = $this->model->languages->filter( 'translator' )->get_list();
|
||||
// If no language can be found, return false as `PLL_Model::get_default_language()` or `PLL_Model::get_language()`.
|
||||
return ! empty( $authorized_languages ) ? reset( $authorized_languages ) : false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Polylang-Pro
|
||||
*/
|
||||
|
||||
namespace WP_Syntex\Polylang_Pro\Modules\Capabilities\Create;
|
||||
|
||||
use PLL_Model;
|
||||
use PLL_Language;
|
||||
use WP_Syntex\Polylang\REST\Request;
|
||||
use WP_Syntex\Polylang\Capabilities\Capabilities;
|
||||
use WP_Syntex\Polylang\Capabilities\Create\Post as Create_Post;
|
||||
|
||||
/**
|
||||
* Class to check if the user has the capabilities to create a post in the contextual language.
|
||||
*/
|
||||
class Post {
|
||||
/**
|
||||
* Create_Post instance.
|
||||
*
|
||||
* @var Create_Post
|
||||
*/
|
||||
private $create_post;
|
||||
|
||||
/**
|
||||
* PLL_Model instance.
|
||||
*
|
||||
* @var PLL_Model
|
||||
*/
|
||||
private $model;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param PLL_Model $model The model instance.
|
||||
* @param Request $request The request instance.
|
||||
* @param PLL_Language|null $pref_lang The preferred language.
|
||||
* @param PLL_Language|null $curlang The current language.
|
||||
*/
|
||||
public function __construct( PLL_Model $model, Request $request, ?PLL_Language $pref_lang, ?PLL_Language $curlang ) {
|
||||
$this->create_post = new Create_Post( $model, $request, $pref_lang, $curlang );
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the capabilities checker.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function init(): self {
|
||||
add_filter( 'wp_insert_post_empty_content', array( $this, 'check_capabilities' ), 0, 2 );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the user has the capabilities to create a post in the contextual language.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param bool $maybe_empty Whether the post should be considered "empty". Unused.
|
||||
* @param array $postarr Array of post data.
|
||||
*
|
||||
* @return bool|never Untouched `$maybe_empty` parameter value. Die if the user does not have the capabilities.
|
||||
*/
|
||||
public function check_capabilities( $maybe_empty, $postarr ) {
|
||||
if ( ! empty( $postarr['ID'] ) || ! $this->model->post->is_translated_object_type( $postarr['post_type'] ) ) {
|
||||
return $maybe_empty;
|
||||
}
|
||||
|
||||
Capabilities::get_user()->can_translate_or_die(
|
||||
$this->create_post->get_language()
|
||||
);
|
||||
|
||||
return $maybe_empty;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Polylang-Pro
|
||||
*/
|
||||
|
||||
namespace WP_Syntex\Polylang_Pro\Modules\Capabilities\Create;
|
||||
|
||||
use PLL_Model;
|
||||
use PLL_Language;
|
||||
use WP_Syntex\Polylang\REST\Request;
|
||||
use WP_Syntex\Polylang\Capabilities\Capabilities;
|
||||
use WP_Syntex\Polylang\Capabilities\Create\Term as Create_Term;
|
||||
|
||||
/**
|
||||
* Class to check if the user has the capabilities to create a term in the contextual language.
|
||||
*/
|
||||
class Term {
|
||||
/**
|
||||
* Create_Term instance.
|
||||
*
|
||||
* @var Create_Term
|
||||
*/
|
||||
private $create_term;
|
||||
|
||||
/**
|
||||
* PLL_Model instance.
|
||||
*
|
||||
* @var PLL_Model
|
||||
*/
|
||||
private $model;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param PLL_Model $model The model instance.
|
||||
* @param Request $request The request instance.
|
||||
* @param PLL_Language|null $pref_lang The preferred language.
|
||||
* @param PLL_Language|null $curlang The current language.
|
||||
*/
|
||||
public function __construct( PLL_Model $model, Request $request, ?PLL_Language $pref_lang, ?PLL_Language $curlang ) {
|
||||
$this->create_term = new Create_Term( $model, $request, $pref_lang, $curlang );
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the capabilities checker.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function init(): self {
|
||||
add_filter( 'pre_insert_term', array( $this, 'check_capabilities' ), 0, 2 );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the user has the capabilities to create a term in the contextual language.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string|\WP_Error $term The term name to add, or a `WP_Error` object if there's an error. Unused.
|
||||
* @param string $taxonomy Taxonomy slug.
|
||||
*
|
||||
* @return string|\WP_Error|never The term name to add, or a `WP_Error` object if there's an error. Die if the user does not have the capabilities.
|
||||
*/
|
||||
public function check_capabilities( $term, $taxonomy ) {
|
||||
if ( is_int( $term ) || ! $this->model->term->is_translated_object_type( $taxonomy ) ) {
|
||||
return $term;
|
||||
}
|
||||
|
||||
Capabilities::get_user()->can_translate_or_die(
|
||||
$this->create_term->get_language()
|
||||
);
|
||||
|
||||
return $term;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Polylang-Pro
|
||||
*/
|
||||
|
||||
namespace WP_Syntex\Polylang_Pro\Modules\Capabilities;
|
||||
|
||||
use PLL_Language;
|
||||
use WP_Syntex\Polylang\Capabilities\Capabilities;
|
||||
use WP_Syntex\Polylang\Model\Languages_Proxy_Interface;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Class allowing to filter the list of languages, depending on the current user's capabilities.
|
||||
*
|
||||
* @since 3.8
|
||||
*/
|
||||
class Languages_Proxy implements Languages_Proxy_Interface {
|
||||
/**
|
||||
* Returns the proxy's key.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @phpstan-return non-falsy-string
|
||||
*/
|
||||
public function key(): string {
|
||||
return 'translator';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of available languages after passing it through this proxy.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param PLL_Language[] $languages List of languages to filter.
|
||||
* @return PLL_Language[]
|
||||
*/
|
||||
public function filter( array $languages ): array {
|
||||
$user = Capabilities::get_user();
|
||||
|
||||
if ( ! $user->is_translator() ) {
|
||||
return $languages;
|
||||
}
|
||||
|
||||
return array_filter( $languages, array( $user, 'can_translate' ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Polylang-Pro
|
||||
*/
|
||||
|
||||
namespace WP_Syntex\Polylang_Pro\Modules\Capabilities\Mapper;
|
||||
|
||||
use WP_Syntex\Polylang\Capabilities\User\User_Interface;
|
||||
|
||||
/**
|
||||
* Abstract class for capabilities mappers implementing the Chain of Responsibility pattern.
|
||||
*
|
||||
* @since 3.8
|
||||
*/
|
||||
abstract class Abstract_Mapper {
|
||||
/**
|
||||
* @var Abstract_Mapper|null
|
||||
*/
|
||||
protected $next;
|
||||
|
||||
/**
|
||||
* Maps a capability to the primitive capabilities required of the given user.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string[] $caps Primitive capabilities required of the user.
|
||||
* @param string $cap Capability being checked.
|
||||
* @param User_Interface $user The user object.
|
||||
* @param array $args Adds context to the capability check, typically starting with an object ID.
|
||||
* @return string[] Updated primitive capabilities required of the user.
|
||||
*/
|
||||
public function map( array $caps, string $cap, User_Interface $user, array $args ): array {
|
||||
if ( in_array( 'do_not_allow', $caps, true ) ) {
|
||||
return $caps;
|
||||
}
|
||||
|
||||
if ( $this->next ) {
|
||||
return $this->next->map( $caps, $cap, $user, $args );
|
||||
}
|
||||
|
||||
return $caps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the next mapper to be used.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param Abstract_Mapper $next_mapper The next mapper to be used.
|
||||
* @return Abstract_Mapper The next mapper to be used.
|
||||
*/
|
||||
public function set_next( Abstract_Mapper $next_mapper ): Abstract_Mapper {
|
||||
$this->next = $next_mapper;
|
||||
|
||||
return $next_mapper;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Polylang-Pro
|
||||
*/
|
||||
|
||||
namespace WP_Syntex\Polylang_Pro\Modules\Capabilities\Mapper;
|
||||
|
||||
/**
|
||||
* Concrete implementation of the `Abstract_Mapper` class.
|
||||
* It is used to break early the chain of mappers when a `do_not_allow` capability is present.
|
||||
*
|
||||
* @since 3.8
|
||||
*/
|
||||
class Do_Not_Allow extends Abstract_Mapper {}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Polylang-Pro
|
||||
*/
|
||||
|
||||
namespace WP_Syntex\Polylang_Pro\Modules\Capabilities\Mapper;
|
||||
|
||||
use WP_Syntex\Polylang\Capabilities\User\User_Interface;
|
||||
|
||||
/**
|
||||
* Class to manage our custom capabilities that are mapped to `manage_options`.
|
||||
*
|
||||
* @since 3.8
|
||||
*/
|
||||
class Internal extends Abstract_Mapper {
|
||||
/**
|
||||
* The capability mapped to `manage_options`.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $capability;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string $capability The capability mapped to `manage_options`.
|
||||
*/
|
||||
public function __construct( string $capability ) {
|
||||
$this->capability = $capability;
|
||||
}
|
||||
/**
|
||||
* Maps a capability to the primitive capabilities required of the given user.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string[] $caps Primitive capabilities required of the user.
|
||||
* @param string $cap Capability being checked.
|
||||
* @param User_Interface $user The user object.
|
||||
* @param array $args Adds context to the capability check, typically starting with an object ID.
|
||||
* @return string[] Updated primitive capabilities required of the user.
|
||||
*/
|
||||
public function map( array $caps, string $cap, User_Interface $user, array $args ): array {
|
||||
if ( $this->capability !== $cap ) {
|
||||
return parent::map( $caps, $cap, $user, $args );
|
||||
}
|
||||
|
||||
if ( $user->has_cap( 'manage_options' ) ) {
|
||||
// Polylang already replaced the custom capability by `manage_options`.
|
||||
return $caps;
|
||||
}
|
||||
|
||||
// Non-administrators must have the custom capability.
|
||||
$caps = array_diff( $caps, array( 'manage_options', $this->capability ) );
|
||||
$caps[] = $this->capability;
|
||||
|
||||
return $caps;
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Polylang-Pro
|
||||
*/
|
||||
|
||||
namespace WP_Syntex\Polylang_Pro\Modules\Capabilities\Mapper;
|
||||
|
||||
use WP_Syntex\Polylang\Capabilities\User\User_Interface;
|
||||
|
||||
/**
|
||||
* Class to map machine translation capabilities.
|
||||
*
|
||||
* @since 3.8
|
||||
*/
|
||||
class Machine_Translation extends Abstract_Mapper {
|
||||
/**
|
||||
* The capability allowing to use machine translation.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public const CAPABILITY = 'machine_translate';
|
||||
|
||||
/**
|
||||
* Maps a capability to the primitive capabilities required of the given user.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string[] $caps Primitive capabilities required of the user.
|
||||
* @param string $cap Capability being checked.
|
||||
* @param User_Interface $user The user object.
|
||||
* @param array $args Adds context to the capability check, typically starting with an object ID.
|
||||
* @return string[] Updated primitive capabilities required of the user.
|
||||
*/
|
||||
public function map( array $caps, string $cap, User_Interface $user, array $args ): array {
|
||||
if ( self::CAPABILITY !== $cap ) {
|
||||
return parent::map( $caps, $cap, $user, $args );
|
||||
}
|
||||
|
||||
if ( ! pll_get_constant( 'PLL_ENABLE_MACHINE_TRANSLATION_CAPABILITIES' ) ) {
|
||||
// Not enabled: remove `machine_translate`. The list should be empty.
|
||||
return array_diff( $caps, array( self::CAPABILITY ) );
|
||||
}
|
||||
|
||||
if ( $user->has_cap( 'manage_options' ) ) {
|
||||
// Enabled, but administrators are not required to have this capability.
|
||||
return array_diff( $caps, array( self::CAPABILITY ) );
|
||||
}
|
||||
|
||||
return $caps;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Polylang-Pro
|
||||
*/
|
||||
|
||||
namespace WP_Syntex\Polylang_Pro\Modules\Capabilities\Mapper;
|
||||
|
||||
use Closure;
|
||||
use WP_Post;
|
||||
use PLL_Model;
|
||||
use WP_Post_Type;
|
||||
use PLL_Sync_Post_Model;
|
||||
use WP_Syntex\Polylang\Capabilities\User\User_Interface;
|
||||
|
||||
/**
|
||||
* Class to map edit and delete post type capabilities.
|
||||
*
|
||||
* @since 3.8
|
||||
*/
|
||||
class Post extends Abstract_Mapper {
|
||||
/**
|
||||
* List of all post type capabilities with predefined primitive capabilities.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
private $capabilities = array(
|
||||
'edit_post',
|
||||
'delete_post',
|
||||
'edit_post_meta',
|
||||
'delete_post_meta',
|
||||
'add_post_meta',
|
||||
);
|
||||
|
||||
/**
|
||||
* List of "delete" post type capabilities with predefined primitive capabilities.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
private $delete_post_capabilities = array(
|
||||
'delete_post',
|
||||
);
|
||||
|
||||
/**
|
||||
* PLL_Model instance.
|
||||
*
|
||||
* @var PLL_Model
|
||||
*/
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Sync post model instance.
|
||||
*
|
||||
* @var PLL_Sync_Post_Model
|
||||
*/
|
||||
private $sync_post_model;
|
||||
|
||||
/**
|
||||
* Used to prevent an infinite loop in `map()`, and improve performance.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $processing_sync = array();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param PLL_Model $model Model instance.
|
||||
* @param PLL_Sync_Post_Model $sync_post_model Sync post model instance.
|
||||
*/
|
||||
public function __construct( PLL_Model $model, PLL_Sync_Post_Model $sync_post_model ) {
|
||||
$this->model = $model;
|
||||
$this->sync_post_model = $sync_post_model;
|
||||
|
||||
array_map(
|
||||
array( $this, 'add_capabilities' ),
|
||||
get_post_types( array(), 'objects' ),
|
||||
);
|
||||
|
||||
add_action( 'registered_post_type', Closure::fromCallable( array( $this, 'load_capabilities' ) ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps a capability to the primitive capabilities required of the given user.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string[] $caps Primitive capabilities required of the user.
|
||||
* @param string $cap Capability being checked.
|
||||
* @param User_Interface $user The user object.
|
||||
* @param array $args Adds context to the capability check, typically starting with an object ID.
|
||||
* @return string[] Updated primitive capabilities required of the user.
|
||||
*/
|
||||
public function map( array $caps, string $cap, User_Interface $user, array $args ): array {
|
||||
if ( empty( $args[0] ) ) {
|
||||
// No additional arguments.
|
||||
return parent::map( $caps, $cap, $user, $args );
|
||||
}
|
||||
|
||||
if ( ! $user->is_translator() ) {
|
||||
return parent::map( $caps, $cap, $user, $args );
|
||||
}
|
||||
|
||||
if ( ! in_array( $cap, $this->capabilities, true ) ) {
|
||||
return parent::map( $caps, $cap, $user, $args );
|
||||
}
|
||||
|
||||
// Accepts a post ID or a WP_Post as WordPress does, the latter being undocumented.
|
||||
$post = get_post( $args[0] );
|
||||
|
||||
if ( ! $post instanceof WP_Post ) {
|
||||
// Wrong type of argument, or the post doesn't exist.
|
||||
return parent::map( $caps, $cap, $user, $args );
|
||||
}
|
||||
|
||||
$key_suffix = sprintf( '|%d|%s', $user->get_id(), $cap );
|
||||
|
||||
if ( ! empty( $this->processing_sync[ $post->ID . $key_suffix ] ) ) {
|
||||
// Already processing this post in the `foreach` loop at the end of this method.
|
||||
return parent::map( $caps, $cap, $user, $args );
|
||||
}
|
||||
|
||||
if ( ! $this->model->post->is_translated_object_type( $post->post_type ) ) {
|
||||
return parent::map( $caps, $cap, $user, $args );
|
||||
}
|
||||
|
||||
$language = $this->model->post->get_language( $post->ID );
|
||||
|
||||
if ( ! $language || ! $user->can_translate( $language ) ) {
|
||||
$caps[] = 'do_not_allow';
|
||||
return $caps;
|
||||
}
|
||||
|
||||
if ( in_array( $cap, $this->delete_post_capabilities, true ) ) {
|
||||
// No need to deal with synchronization.
|
||||
return parent::map( $caps, $cap, $user, $args );
|
||||
}
|
||||
|
||||
$synchros = $this->sync_post_model->get( $post->ID );
|
||||
|
||||
if ( empty( $synchros ) ) {
|
||||
return parent::map( $caps, $cap, $user, $args );
|
||||
}
|
||||
|
||||
/*
|
||||
* Check all languages at once:
|
||||
* - Much faster here than in the `foreach` loop below.
|
||||
* - This allows to stop here if the user doesn't have all the required language capabilities (no need to check
|
||||
* anything else in this case).
|
||||
*/
|
||||
if ( ! $user->can_translate_all( array_keys( $synchros ) ) ) {
|
||||
$caps[] = 'do_not_allow';
|
||||
return $caps;
|
||||
}
|
||||
|
||||
/*
|
||||
* Make sure this post is not synchronized with one that the user can't edit (for whatever reason that is not
|
||||
* related to language capabilities).
|
||||
*/
|
||||
foreach ( $synchros as $tr_id ) {
|
||||
if ( (int) $tr_id === $post->ID ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tr_key = $tr_id . $key_suffix;
|
||||
|
||||
$this->processing_sync[ $tr_key ] = true;
|
||||
|
||||
if ( ! $user->has_cap( $cap, (int) $tr_id ) ) {
|
||||
unset( $this->processing_sync[ $tr_key ] );
|
||||
$caps[] = 'do_not_allow';
|
||||
return $caps;
|
||||
}
|
||||
|
||||
unset( $this->processing_sync[ $tr_key ] );
|
||||
}
|
||||
|
||||
return parent::map( $caps, $cap, $user, $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the post type capabilities.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string $post_type Post type. Unused.
|
||||
* @param WP_Post_Type $post_type_object Post type object.
|
||||
* @return void
|
||||
*/
|
||||
private function load_capabilities( $post_type, $post_type_object ): void {
|
||||
$post_type_object instanceof WP_Post_Type && $this->add_capabilities( $post_type_object );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the capabilities for a post type.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param WP_Post_Type $post_type_object Post type object.
|
||||
* @return void
|
||||
*/
|
||||
private function add_capabilities( WP_Post_Type $post_type_object ): void {
|
||||
if ( ! $this->model->post->is_translated_object_type( $post_type_object->name ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->delete_post_capabilities = array_merge(
|
||||
$this->delete_post_capabilities,
|
||||
array( $post_type_object->cap->delete_post )
|
||||
);
|
||||
$this->capabilities = array_merge(
|
||||
$this->capabilities,
|
||||
$this->delete_post_capabilities,
|
||||
array( $post_type_object->cap->edit_post )
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Polylang-Pro
|
||||
*/
|
||||
|
||||
namespace WP_Syntex\Polylang_Pro\Modules\Capabilities\Mapper;
|
||||
|
||||
use Closure;
|
||||
use WP_Term;
|
||||
use PLL_Model;
|
||||
use WP_Taxonomy;
|
||||
use WP_Syntex\Polylang\Capabilities\User\User_Interface;
|
||||
|
||||
/**
|
||||
* Class to map taxonomy capabilities.
|
||||
*
|
||||
* @since 3.8
|
||||
*/
|
||||
class Term extends Abstract_Mapper {
|
||||
/**
|
||||
* List of taxonomy capabilities with predefined primitive capabilities.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
private $capabilities = array(
|
||||
'edit_term',
|
||||
'delete_term',
|
||||
'edit_term_meta',
|
||||
'delete_term_meta',
|
||||
'add_term_meta',
|
||||
);
|
||||
|
||||
/**
|
||||
* PLL_Model instance.
|
||||
*
|
||||
* @var PLL_Model
|
||||
*/
|
||||
private $model;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param PLL_Model $model Model instance.
|
||||
*/
|
||||
public function __construct( PLL_Model $model ) {
|
||||
$this->model = $model;
|
||||
|
||||
array_map(
|
||||
array( $this, 'add_capabilities' ),
|
||||
get_taxonomies( array(), 'objects' ),
|
||||
);
|
||||
|
||||
add_action( 'registered_taxonomy', Closure::fromCallable( array( $this, 'load_capabilities' ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps a capability to the primitive capabilities required of the given user.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string[] $caps Primitive capabilities required of the user.
|
||||
* @param string $cap Capability being checked.
|
||||
* @param User_Interface $user The user object.
|
||||
* @param array $args Adds context to the capability check, typically starting with an object ID.
|
||||
* @return string[] Updated primitive capabilities required of the user.
|
||||
*/
|
||||
public function map( array $caps, string $cap, User_Interface $user, array $args ): array {
|
||||
if ( empty( $args[0] ) || ! is_numeric( $args[0] ) ) {
|
||||
// No additional arguments or non-numeric.
|
||||
return parent::map( $caps, $cap, $user, $args );
|
||||
}
|
||||
|
||||
if ( ! $user->is_translator() ) {
|
||||
return parent::map( $caps, $cap, $user, $args );
|
||||
}
|
||||
|
||||
if ( ! in_array( $cap, $this->capabilities, true ) ) {
|
||||
return parent::map( $caps, $cap, $user, $args );
|
||||
}
|
||||
|
||||
$term = get_term( (int) $args[0] );
|
||||
if ( ! $term instanceof WP_Term || ! $this->model->term->is_translated_object_type( $term->taxonomy ) ) {
|
||||
return parent::map( $caps, $cap, $user, $args );
|
||||
}
|
||||
|
||||
$language = $this->model->term->get_language( $term->term_id );
|
||||
if ( $language && $user->can_translate( $language ) ) {
|
||||
return parent::map( $caps, $cap, $user, $args );
|
||||
}
|
||||
|
||||
$caps[] = 'do_not_allow';
|
||||
|
||||
return $caps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the taxonomy capabilities.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string $taxonomy Taxonomy name.
|
||||
* @return void
|
||||
*/
|
||||
private function load_capabilities( $taxonomy ): void {
|
||||
$taxonomy_object = get_taxonomy( $taxonomy );
|
||||
if ( ! $taxonomy_object ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->add_capabilities( $taxonomy_object );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the capabilities for a taxonomy.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param WP_Taxonomy $taxonomy_object Taxonomy object.
|
||||
* @return void
|
||||
*/
|
||||
private function add_capabilities( WP_Taxonomy $taxonomy_object ): void {
|
||||
if ( ! $this->model->term->is_translated_object_type( $taxonomy_object->name ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->capabilities = array_merge(
|
||||
$this->capabilities,
|
||||
array( $taxonomy_object->cap->edit_terms, $taxonomy_object->cap->delete_terms )
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Polylang-Pro
|
||||
*/
|
||||
|
||||
namespace WP_Syntex\Polylang_Pro\Modules\Capabilities\User;
|
||||
|
||||
use WP_User;
|
||||
use WP_Syntex\Polylang\Capabilities\User\NOOP;
|
||||
use WP_Syntex\Polylang\Capabilities\User\User_Interface;
|
||||
use WP_Syntex\Polylang\Capabilities\User\Creator_Interface;
|
||||
|
||||
/**
|
||||
* A class that returns the correct user instance based on the user capabilities.
|
||||
* Completely disables the user decoration.
|
||||
*
|
||||
* @since 3.8
|
||||
*/
|
||||
class Creator implements Creator_Interface {
|
||||
/**
|
||||
* @var User_Interface
|
||||
*/
|
||||
private ?User_Interface $instance = null;
|
||||
|
||||
/**
|
||||
* Creates and returns the user.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param WP_User $user The user to decorate.
|
||||
* @return User_Interface The user instance.
|
||||
*/
|
||||
public function get( WP_User $user ): User_Interface {
|
||||
if ( $this->instance && $user->ID === $this->instance->get_id() ) {
|
||||
return $this->instance;
|
||||
}
|
||||
|
||||
/*
|
||||
* Voluntarily checking `WP_user::$allcaps` instead of `WP_user::has_cap()` to avoid infinite loop with the `map_meta_cap` filter.
|
||||
* Checking `manage_options` is appropriate because it allows both super admins and admins.
|
||||
*/
|
||||
if ( ! empty( $user->allcaps['manage_options'] ) ) {
|
||||
$this->instance = new NOOP( $user );
|
||||
|
||||
return $this->instance;
|
||||
}
|
||||
|
||||
$this->instance = new Translator( $user );
|
||||
|
||||
return $this->instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Polylang-Pro
|
||||
*/
|
||||
|
||||
namespace WP_Syntex\Polylang_Pro\Modules\Capabilities\User;
|
||||
|
||||
use WP_User;
|
||||
use PLL_Language;
|
||||
use WP_Syntex\Polylang\Model\Languages;
|
||||
use WP_Syntex\Polylang\Capabilities\User\User_Interface;
|
||||
|
||||
/**
|
||||
* A decorator for `WP_User` that adds translation management capabilities.
|
||||
*
|
||||
* @since 3.8
|
||||
*/
|
||||
class Translator implements User_Interface {
|
||||
/**
|
||||
* @var WP_User
|
||||
*/
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* @var string[]|null
|
||||
*/
|
||||
private $language_caps;
|
||||
|
||||
/**
|
||||
* @var bool[]
|
||||
*/
|
||||
private $can_translate = array();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param WP_User $user An instance of `WP_User`.
|
||||
*/
|
||||
public function __construct( WP_User $user ) {
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the user ID.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function get_id(): int {
|
||||
return $this->user->ID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells if the user is a translator (has a translator capability).
|
||||
* Note: returns `true` if the user has a capability for a language that doesn't exist anymore. This is intentional,
|
||||
* to prevent the user to suddenly have the rights to translate in all languages while it wasn't allowed until then.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_translator(): bool {
|
||||
return ! empty( $this->get_language_caps() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells if the user can translate to the given language.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param PLL_Language $language A language object.
|
||||
* @return bool
|
||||
*/
|
||||
public function can_translate( PLL_Language $language ): bool {
|
||||
if ( isset( $this->can_translate[ $language->slug ] ) ) {
|
||||
return $this->can_translate[ $language->slug ];
|
||||
}
|
||||
|
||||
if ( ! $this->is_translator() ) {
|
||||
$this->can_translate[ $language->slug ] = true;
|
||||
} else {
|
||||
$this->can_translate[ $language->slug ] = $this->user->has_cap( "translate_{$language->slug}" );
|
||||
}
|
||||
|
||||
return $this->can_translate[ $language->slug ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells if the user can translate to all the given languages.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param array $languages List of language slugs.
|
||||
* @return bool
|
||||
*
|
||||
* @phpstan-param array<non-empty-string> $languages
|
||||
*/
|
||||
public function can_translate_all( array $languages ): bool {
|
||||
if ( ! $this->is_translator() ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ( $languages as $language_slug ) {
|
||||
if ( ! isset( $this->can_translate[ $language_slug ] ) ) {
|
||||
$this->can_translate[ $language_slug ] = $this->user->has_cap( "translate_{$language_slug}" );
|
||||
}
|
||||
if ( ! $this->can_translate[ $language_slug ] ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells if the user has the specified capability.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param string $capability Capability name.
|
||||
* @param mixed ...$args Optional further parameters, typically starting with an object ID.
|
||||
* @return bool
|
||||
*/
|
||||
public function has_cap( $capability, ...$args ): bool {
|
||||
return $this->user->has_cap( $capability, ...$args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the preferred language of the user.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @return string The preferred language slug, empty string if no preferred language is found.
|
||||
*/
|
||||
public function get_preferred_language_slug(): string {
|
||||
$language_caps = $this->get_language_caps();
|
||||
|
||||
if ( empty( $language_caps ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Arbitrarily use the first language cap.
|
||||
$language_cap = reset( $language_caps );
|
||||
|
||||
return str_replace( 'translate_', '', $language_cap );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the language capabilities of the user.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_language_caps(): array {
|
||||
if ( isset( $this->language_caps ) ) {
|
||||
return $this->language_caps;
|
||||
}
|
||||
|
||||
$this->language_caps = (array) preg_grep( '/^translate_' . Languages::INNER_SLUG_PATTERN . '$/', array_keys( $this->user->allcaps ) );
|
||||
|
||||
return $this->language_caps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the current user has the rights to assign a language to an object and dies if not.
|
||||
*
|
||||
* @since 3.8
|
||||
*
|
||||
* @param PLL_Language $language The language to assign.
|
||||
* @return void|never Dies if the user does not have the rights, does nothing otherwise.
|
||||
*/
|
||||
public function can_translate_or_die( PLL_Language $language ): void {
|
||||
if ( ! $this->can_translate( $language ) ) {
|
||||
/* translators: %s is a language name */
|
||||
wp_die( esc_html( sprintf( __( 'Sorry, you are not allowed to edit content in %s.', 'polylang-pro' ), $language->name ) ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Polylang-Pro
|
||||
*/
|
||||
|
||||
namespace WP_Syntex\Polylang_Pro\Modules\Capabilities;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
if ( $polylang->model->has_languages() ) {
|
||||
require_once POLYLANG_PRO_DIR . '/src/modules/sync-post/load.php'; // Make sure `$polylang->sync_post_model` is set.
|
||||
|
||||
$polylang->model->languages->register_proxy( new Languages_Proxy() );
|
||||
$polylang->advanced_capabilities = new Advanced_Capabilities( $polylang );
|
||||
}
|
||||
Reference in New Issue
Block a user