From bd4fdd51ef81561d06cdb2de183a1295bb3bd3b1 Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 2 Feb 2024 10:56:55 +0100 Subject: [PATCH 1/2] :sparkles: Allow Role labels to not be translated --- src/Framework/Wordpress/Role.php | 72 ++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 17 deletions(-) diff --git a/src/Framework/Wordpress/Role.php b/src/Framework/Wordpress/Role.php index 8760f34..6f0276a 100644 --- a/src/Framework/Wordpress/Role.php +++ b/src/Framework/Wordpress/Role.php @@ -18,25 +18,24 @@ abstract class Role { /** * The role options. - * - * @var array */ protected array $options = []; + /** + * The role capabilities. + */ + protected array $caps = []; /** - * The role options. - * - * @var array + * The role default options, merged with options. */ - private array $defaults = [ + protected array $defaults = [ 'name' => '', 'label' => '', 'extends' => false, 'backoffice' => false, ]; - /** * The computed role options. * @@ -44,7 +43,6 @@ abstract class Role */ private $conf; - /** * The admin menu items the current role SHOULD NOT see. * @@ -53,6 +51,12 @@ abstract class Role */ protected array $hidden_menues = []; + /** + * Should translate the label. + * + * @since 2.5.0 + */ + protected bool $translate_labels = false; /** * i18n translation domain. @@ -62,7 +66,6 @@ abstract class Role */ protected string $i18n_domain = ''; - /** * i18n cpt default lang (format: 'en', 'fr'..). * Leave empty string to use the app default lang instead. @@ -73,7 +76,6 @@ abstract class Role */ protected string $i18n_base_lang = ''; - /** * Role constructor. Ensure mandatory options are set. * @@ -104,7 +106,7 @@ public function generateCaps() if ($this->conf->extends && is_string($this->conf->extends)) { $extends = get_role($this->conf->extends); - + if (!$extends) { throw new RoleNotFoundException( sprintf("ObjectPress : The extended role `%s` was not found.", $this->conf->extends) @@ -128,10 +130,11 @@ public function generateCaps() protected function register() { $caps = $this->generateCaps(); + $label = $this->translate_labels ? __($this->conf->label, $this->i18n_domain) : $this->conf->label; $role = get_role($this->conf->name); if (!$role) { - add_role($this->conf->name, __($this->conf->label, $this->i18n_domain), $caps); + add_role($this->conf->name, $label, $caps); } else { if ($role->capabilities !== $caps) { foreach ($role->capabilities as $name => $value) { @@ -146,7 +149,6 @@ protected function register() $this->removeAdminMenuItems(); } - /** * Remove some menu items from admin menu for this specific role. * @@ -181,7 +183,6 @@ protected function removeAdminMenuItems() /* */ /********************************/ - /** * Custom post type init (registration) * @@ -197,17 +198,54 @@ public function boot() $this->register(); } + public function removeCaps(string | array $caps) + { + if (!($role = get_role($this->conf->name))) { + throw new RoleNotFoundException( + sprintf("ObjectPress : The role `%s` was not found.", $this->conf->name) + ); + } + + foreach ((array) $caps as $cap) { + $role->remove_cap($cap); + } + } - public function addCaps() + public function addCaps(array $caps) { - // TODO + if (!($role = get_role($this->conf->name))) { + throw new RoleNotFoundException( + sprintf("ObjectPress : The role `%s` was not found.", $this->conf->name) + ); + } + + foreach ($caps as $name => $value) { + $role->add_cap($name, $value); + } + } + + public function replaceCaps(array $caps) + { + if (!($role = get_role($this->conf->name))) { + throw new RoleNotFoundException( + sprintf("ObjectPress : The role `%s` was not found.", $this->conf->name) + ); + } + + foreach ($role->capabilities as $name => $value) { + $role->remove_cap($name); + } + + foreach ($caps as $name => $value) { + $role->add_cap($name, $value); + } } public function getName() { return $this->conf->name; } - + public function getLabel() { return $this->conf->label; From a7ccca1ab0400dddc9c3fb18ca74c8d91bcdfe83 Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 23 Feb 2024 15:52:17 +0100 Subject: [PATCH 2/2] :sparkles: Support Terms in the PolylangTranslatable concern --- .../Models/Concerns/PolylangTranslatable.php | 65 +++++++++++++++++-- 1 file changed, 59 insertions(+), 6 deletions(-) diff --git a/src/Framework/Models/Concerns/PolylangTranslatable.php b/src/Framework/Models/Concerns/PolylangTranslatable.php index 65a553c..dc46dd2 100644 --- a/src/Framework/Models/Concerns/PolylangTranslatable.php +++ b/src/Framework/Models/Concerns/PolylangTranslatable.php @@ -21,6 +21,22 @@ trait PolylangTranslatable * @return string|null */ public function getLanguageAttribute() + { + if ($this instanceof \AmphiBee\Eloquent\Model\Post) { + return $this->getPostLanguageInfo(); + } + + if ($this instanceof \AmphiBee\Eloquent\Model\Term) { + return $this->getTermLanguageInfo(); + } + } + + /** + * Get the post language. + * + * @return string|null + */ + protected function getPostLanguageInfo() { $taxo = $this->taxonomies ->where('taxonomy', 'language') @@ -28,7 +44,26 @@ public function getLanguageAttribute() return $taxo ? $taxo->term->slug : null; } - + + /** + * Get the term language. + * + * @return string|null + */ + protected function getTermLanguageInfo() + { + $app = ObjectPress::app(); + + # No supported lang plugin detected + if (!$app->bound(LanguageDriver::class)) { + return; + } + + $driver = $app->make(LanguageDriver::class); + + return $driver->getTermLang($this->id); + } + /** * Set the post language. * @@ -45,10 +80,18 @@ public function setLanguageAttribute($value) } $driver = $app->make(LanguageDriver::class); - $driver->setPostLang($this->id, $value); + + if ($this instanceof \AmphiBee\Eloquent\Model\Post) { + $driver->setPostLang($this->id, $value); + } + + if ($this instanceof \AmphiBee\Eloquent\Model\Term) { + $driver->setTermLang($this->id, $value); + } + $this->refresh(); } - + /** * Get the post translation in the asked language. * @@ -58,6 +101,7 @@ public function setLanguageAttribute($value) public function translation(string $lang) { $app = ObjectPress::app(); + $id = null; # No supported lang plugin detected if (!$app->bound(LanguageDriver::class)) { @@ -70,13 +114,22 @@ public function translation(string $lang) if ($lang == 'current') { $lang = $driver->getCurrentLang(); } - + # Get the default/primary lang slug if ($lang == 'default') { $lang = $driver->getPrimaryLang(); } - $id = $driver->getPostIn($this->id, $lang); - return $id ? static::find($id) : null; + if ($this instanceof \AmphiBee\Eloquent\Model\Post) { + $id = $driver->getPostIn($this->id, $lang); + } + + if ($this instanceof \AmphiBee\Eloquent\Model\Term) { + $id = $driver->getTermIn($this->id, $lang); + } + + return $id + ? static::find($id) + : null; } }