Solves conflict on FieldsList.

This commit is contained in:
mateuswetah 2018-03-12 09:04:00 -03:00
commit 9071e8656b
6 changed files with 141 additions and 10 deletions

View File

@ -4,7 +4,7 @@ namespace Tainacan;
use Tainacan\Repositories\Repository; use Tainacan\Repositories\Repository;
class Capabilities { class Capabilities {
protected $defaults = [ public $defaults = [
"tainacan-collection"=> [ "tainacan-collection"=> [
"administrator"=> [ "administrator"=> [
"delete_posts", "delete_posts",
@ -275,18 +275,73 @@ class Capabilities {
* Register hooks * Register hooks
*/ */
function __construct() { function __construct() {
add_action('init', array(&$this, 'init'), 11);
add_action('tainacan-insert-tainacan-collection', array(&$this, 'new_collection')); add_action('tainacan-insert-tainacan-collection', array(&$this, 'new_collection'));
add_action('tainacan-add-collection-moderators', array(&$this, 'add_moderators'), 10, 2); add_action('tainacan-add-collection-moderators', array(&$this, 'add_moderators'), 10, 2);
add_action('tainacan-remove-collection-moderators', array(&$this, 'remove_moderators'), 10, 2); add_action('tainacan-remove-collection-moderators', array(&$this, 'remove_moderators'), 10, 2);
add_filter( 'gettext_with_context', array(&$this, 'translate_user_roles'), 10, 4 );
// Dummy calls for translation.
_x('Tainacan Author', 'User role', 'tainacan');
_x('Tainacan Contributor', 'User role', 'tainacan');
_x('Tainacan Editor', 'User role', 'tainacan');
} }
/** /**
* Update post_type caps using WordPress basic roles * Tainacan default roles
* @param string $name //capability name *
* These are roles relative to the native WordPress roles. They will have
* the same capabilities of their relatives native roles in what is concerned to
* tainacan activities, but will have no other WordPress capabilities.
*
* @return array Tainacan roles
*/
private function get_tainacan_roles() {
$tainacan_roles = [
'editor' => [
'slug' => 'tainacan-editor',
'display_name' => 'Tainacan Editor'
],
'contributor' => [
'slug' => 'tainacan-contributor',
'display_name' => 'Tainacan Contributor'
],
'author' => [
'slug' => 'tainacan-author',
'display_name' => 'Tainacan Author'
],
];
return $tainacan_roles;
}
/**
* Callback to gettext_with_context hook to translate custom ueser roles.
*
* Since user roles are stored in the database, we have to translate them on the fly
* using translate_user_role() function.
*
* @see https://wordpress.stackexchange.com/questions/141551/how-to-auto-translate-custom-user-roles
*/
public function translate_user_roles( $translations, $text, $context, $domain ) {
$plugin_domain = 'tainacan';
$roles_names = array_map(function($role) {
return $role['display_name'];
}, $this->get_tainacan_roles());
if ( $context === 'User role' && in_array( $text, $roles_names ) && $domain !== $plugin_domain ) {
return translate_with_gettext_context( $text, $context, $plugin_domain );
}
return $translations;
}
/**
* Update post_type caps using WordPress basic roles and register tainacan roles
*/ */
public function init() { public function init() {
$defaults_caps = apply_filters('tainacan-defaults-capabilities', $this->defaults); $defaults_caps = apply_filters('tainacan-defaults-capabilities', $this->defaults);
@ -305,8 +360,27 @@ class Capabilities {
foreach ($caps as $cap) { foreach ($caps as $cap) {
$role->add_cap($entity_cap->$cap); $role->add_cap($entity_cap->$cap);
} }
$tainacan_roles = $this->get_tainacan_roles();
if (array_key_exists($role_name, $tainacan_roles)) {
$tainacan_role = add_role( $tainacan_roles[$role_name]['slug'], $tainacan_roles[$role_name]['display_name'] );
if(!is_object($tainacan_role)) {
$tainacan_role = get_role($tainacan_roles[$role_name]['slug']);
}
if(!is_object($tainacan_role)) {
throw new \Exception(sprintf('Role "%s" not found', $tainacan_roles[$role_name]['slug']));
}
foreach ($caps as $cap) {
$tainacan_role->add_cap($entity_cap->$cap);
}
}
} }
} }
global $Tainacan_Collections; global $Tainacan_Collections;
$collections = $Tainacan_Collections->fetch([], 'OBJECT'); $collections = $Tainacan_Collections->fetch([], 'OBJECT');
foreach ($collections as $collection) { foreach ($collections as $collection) {

View File

@ -35,7 +35,7 @@ class Date extends Field_Type {
public function form(){ public function form(){
$approx_date = $this->get_option('approximate_date'); $approx_date = $this->get_option('approximate_date');
?> ?>
<tr> <!--tr>
<td> <td>
<label><?php echo __('Approximate Date','tainacan'); ?></label><br/> <label><?php echo __('Approximate Date','tainacan'); ?></label><br/>
<small><?php echo __('Allow format approximate date','tainacan'); ?></small> <small><?php echo __('Allow format approximate date','tainacan'); ?></small>
@ -43,7 +43,7 @@ class Date extends Field_Type {
<td> <td>
<?php Helpers\HtmlHelpers::radio_field( $approx_date, 'field_type_options[approximate_date]' ) ?> <?php Helpers\HtmlHelpers::radio_field( $approx_date, 'field_type_options[approximate_date]' ) ?>
</td> </td>
</tr> </tr-->
<?php <?php
} }
} }

View File

@ -1,7 +1,5 @@
<?php <?php
use Tainacan\Capabilities;
const ENTITIES_DIR = __DIR__ . '/entities/'; const ENTITIES_DIR = __DIR__ . '/entities/';
const FIELD_TYPES_DIR = __DIR__ . '/field-types/'; const FIELD_TYPES_DIR = __DIR__ . '/field-types/';
const FILTER_TYPES_DIR = __DIR__ . '/filter-types/'; const FILTER_TYPES_DIR = __DIR__ . '/filter-types/';
@ -109,6 +107,4 @@ $Tainacan_Terms = new \Tainacan\Repositories\Terms();
global $Tainacan_Logs; global $Tainacan_Logs;
$Tainacan_Logs = new \Tainacan\Repositories\Logs(); $Tainacan_Logs = new \Tainacan\Repositories\Logs();
new Capabilities();
?> ?>

View File

@ -25,6 +25,10 @@ function tnc_enable_dev_wp_interface() {
//return defined('TNC_ENABLE_DEV_WP_INTERFACE') && true === TNC_ENABLE_DEV_WP_INTERFACE ? true : false; //return defined('TNC_ENABLE_DEV_WP_INTERFACE') && true === TNC_ENABLE_DEV_WP_INTERFACE ? true : false;
} }
global $Tainacan_Capabilities;
$Tainacan_Capabilities = new \Tainacan\Capabilities();
register_activation_hook( __FILE__, array( $Tainacan_Capabilities, 'init' ) );
// TODO move it somewhere else? // TODO move it somewhere else?
require_once('admin/class-tainacan-admin.php'); require_once('admin/class-tainacan-admin.php');
global $Tainacan_Admin; global $Tainacan_Admin;

View File

@ -22,6 +22,9 @@ require_once $_tests_dir . '/includes/functions.php';
*/ */
function _manually_load_plugin() { function _manually_load_plugin() {
require dirname( dirname( __FILE__ ) ) . '/src/tainacan.php'; require dirname( dirname( __FILE__ ) ) . '/src/tainacan.php';
add_action('plugins_loaded', function() {
do_action('activate_' . substr(dirname( dirname( __FILE__ ) ), 1) . '/src/tainacan.php');
});
} }
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

View File

@ -0,0 +1,54 @@
<?php
namespace Tainacan\Tests;
use Tainacan\Repositories\Repository;
/**
* Class DefaultCapabilities
*
* @package Test_Tainacan
*/
/**
* Sample test case.
*/
class DefaultCapabilities extends TAINACAN_UnitTestCase {
function test_capabilities_present() {
global $Tainacan_Capabilities;
$c = new \Tainacan\Entities\Collection();
$caps = $c->get_capabilities();
$defaults_caps = $Tainacan_Capabilities->defaults;
foreach ($defaults_caps as $post_type => $wp_append_roles) {
if($post_type == 'tainacan-items') continue;
$entity = Repository::get_entity_by_post_type($post_type);
$entity_cap = $entity->get_capabilities();
foreach ($wp_append_roles as $role_name => $caps) {
$role = get_role($role_name);
$new_user = $this->factory()->user->create(array( 'role' => $role_name ));
wp_set_current_user($new_user);
foreach ($caps as $cap) {
$this->assertTrue(current_user_can($entity_cap->$cap), "$role_name does not have capability {$entity_cap->$cap}" );
}
$new_user = $this->factory()->user->create(array( 'role' => 'tainacan-' . $role_name ));
wp_set_current_user($new_user);
foreach ($caps as $cap) {
$this->assertTrue(current_user_can($entity_cap->$cap), "tainacan-$role_name does not have capability {$entity_cap->$cap}");
}
}
}
}
}