remove collection moderators from backend #274

This commit is contained in:
leogermani 2019-11-05 11:17:41 -03:00
parent 78c910a4a4
commit 33afc168a6
4 changed files with 42 additions and 159 deletions

View File

@ -403,21 +403,21 @@ return apply_filters( 'tainacan-admin-i18n', [
'label_previous_page' => __( 'Previous page', 'tainacan' ), 'label_previous_page' => __( 'Previous page', 'tainacan' ),
'label_page' => __( 'Page', 'tainacan' ), 'label_page' => __( 'Page', 'tainacan' ),
'label_current_page' => __( 'Current page', 'tainacan' ), 'label_current_page' => __( 'Current page', 'tainacan' ),
'label_shrink_menu' => __( 'Shrink menu', 'tainacan' ), 'label_shrink_menu' => __( 'Shrink menu', 'tainacan' ),
'label_document_uploaded' => __( 'Document uploaded', 'tainacan' ), 'label_document_uploaded' => __( 'Document uploaded', 'tainacan' ),
'label_repository_filter' => __( 'Repository filter', 'tainacan' ), 'label_repository_filter' => __( 'Repository filter', 'tainacan' ),
'label_repository_metadatum' => __( 'Repository metadatum', 'tainacan' ), 'label_repository_metadatum' => __( 'Repository metadatum', 'tainacan' ),
'label_collection_filter' => __( 'Collection filter', 'tainacan' ), 'label_collection_filter' => __( 'Collection filter', 'tainacan' ),
'label_collection_metadatum' => __( 'Collection metadatum', 'tainacan' ), 'label_collection_metadatum' => __( 'Collection metadatum', 'tainacan' ),
'label_collection_capabilities' => __( 'Collection capabilities', 'tainacan' ), 'label_collection_capabilities' => __( 'Collection capabilities', 'tainacan' ),
'label_recover_from_trash' => __( 'Recover from trash', 'tainacan' ), 'label_recover_from_trash' => __( 'Recover from trash', 'tainacan' ),
'label_show_children_terms' => __( 'Show children terms', 'tainacan' ), 'label_show_children_terms' => __( 'Show children terms', 'tainacan' ),
'label_begin_slide_transition' => __( 'Begin slide transition', 'tainacan' ), 'label_begin_slide_transition' => __( 'Begin slide transition', 'tainacan' ),
'label_pause_slide_transition' => __( 'Pause slide transition', 'tainacan' ), 'label_pause_slide_transition' => __( 'Pause slide transition', 'tainacan' ),
'label_next_group_slides' => __( 'Next group of slides', 'tainacan' ), 'label_next_group_slides' => __( 'Next group of slides', 'tainacan' ),
'label_previous_group_slides' => __( 'Previous group of slides', 'tainacan' ), 'label_previous_group_slides' => __( 'Previous group of slides', 'tainacan' ),
'label_plugin_home_page' => __( 'Plugin home page', 'tainacan' ), 'label_plugin_home_page' => __( 'Plugin home page', 'tainacan' ),
'label_wordpress_admin_page' => __( 'WordPress Admin Page', 'tainacan' ), 'label_wordpress_admin_page' => __( 'WordPress Admin Page', 'tainacan' ),
'label_view_all_%s_collections' => __( 'View all %s collections', 'tainacan' ), 'label_view_all_%s_collections' => __( 'View all %s collections', 'tainacan' ),
'label_view_collections_list' => __( 'View collections list', 'tainacan' ), 'label_view_collections_list' => __( 'View collections list', 'tainacan' ),
'label_comparator' => __( 'Comparator', 'tainacan' ), 'label_comparator' => __( 'Comparator', 'tainacan' ),
@ -440,7 +440,6 @@ return apply_filters( 'tainacan-admin-i18n', [
'label_year' => __( 'Year', 'tainacan' ), 'label_year' => __( 'Year', 'tainacan' ),
'label_related_to' => __( 'Related to', 'tainacan' ), 'label_related_to' => __( 'Related to', 'tainacan' ),
'label_associated_roles' => __( 'Associated roles', 'tainacan' ), 'label_associated_roles' => __( 'Associated roles', 'tainacan' ),
// Instructions. More complex sentences to guide user and placeholders // Instructions. More complex sentences to guide user and placeholders
'instruction_delete_selected_collections' => __( 'Delete selected collections', 'tainacan' ), 'instruction_delete_selected_collections' => __( 'Delete selected collections', 'tainacan' ),
'instruction_delete_selected_items' => __( 'Delete selected items', 'tainacan' ), 'instruction_delete_selected_items' => __( 'Delete selected items', 'tainacan' ),
@ -489,7 +488,7 @@ return apply_filters( 'tainacan-admin-i18n', [
'instruction_type_value_year' => __( 'Type year value', 'tainacan' ), 'instruction_type_value_year' => __( 'Type year value', 'tainacan' ),
'instruction_select_the_amount_of_copies' => __( 'Select the amount of copies of the item that you want to create', 'tainacan'), 'instruction_select_the_amount_of_copies' => __( 'Select the amount of copies of the item that you want to create', 'tainacan'),
'instruction_select_a_interval' => __( 'Select an interval', 'tainacan'), 'instruction_select_a_interval' => __( 'Select an interval', 'tainacan'),
// Info. Other feedback to user. // Info. Other feedback to user.
'info_items_tab_all' => __( 'Every published item, including those visible only to editors.', 'tainacan' ), 'info_items_tab_all' => __( 'Every published item, including those visible only to editors.', 'tainacan' ),
@ -515,22 +514,22 @@ return apply_filters( 'tainacan-admin-i18n', [
'info_search_criteria' => __( 'Advanced Search Criteria', 'tainacan' ), 'info_search_criteria' => __( 'Advanced Search Criteria', 'tainacan' ),
'info_name_is_required' => __( 'Name is required.', 'tainacan' ), 'info_name_is_required' => __( 'Name is required.', 'tainacan' ),
'info_no_collection_created' => __( 'No collection was created in this repository.', 'tainacan' ), 'info_no_collection_created' => __( 'No collection was created in this repository.', 'tainacan' ),
'info_no_items_publish' => __( 'No public items found.', 'tainacan' ), 'info_no_items_publish' => __( 'No public items found.', 'tainacan' ),
'info_no_items_private' => __( 'No private items found.', 'tainacan' ), 'info_no_items_private' => __( 'No private items found.', 'tainacan' ),
'info_no_items_draft' => __( 'No draft items found.', 'tainacan' ), 'info_no_items_draft' => __( 'No draft items found.', 'tainacan' ),
'info_no_items_trash' => __( 'No items found on trash.', 'tainacan' ), 'info_no_items_trash' => __( 'No items found on trash.', 'tainacan' ),
'info_no_collections_publish' => __( 'No public collections found.', 'tainacan' ), 'info_no_collections_publish' => __( 'No public collections found.', 'tainacan' ),
'info_no_collections_private' => __( 'No private collections found.', 'tainacan' ), 'info_no_collections_private' => __( 'No private collections found.', 'tainacan' ),
'info_no_collections_draft' => __( 'No draft collections found.', 'tainacan' ), 'info_no_collections_draft' => __( 'No draft collections found.', 'tainacan' ),
'info_no_collections_trash' => __( 'No collections found on trash.', 'tainacan' ), 'info_no_collections_trash' => __( 'No collections found on trash.', 'tainacan' ),
'info_no_taxonomies_publish' => __( 'No public taxonomies found.', 'tainacan' ), 'info_no_taxonomies_publish' => __( 'No public taxonomies found.', 'tainacan' ),
'info_no_taxonomies_private' => __( 'No private taxonomies found.', 'tainacan' ), 'info_no_taxonomies_private' => __( 'No private taxonomies found.', 'tainacan' ),
'info_no_taxonomies_draft' => __( 'No draft taxonomies found.', 'tainacan' ), 'info_no_taxonomies_draft' => __( 'No draft taxonomies found.', 'tainacan' ),
'info_no_taxonomies_trash' => __( 'No taxonomies found on trash.', 'tainacan' ), 'info_no_taxonomies_trash' => __( 'No taxonomies found on trash.', 'tainacan' ),
'info_no_taxonomy_created' => __( 'No taxonomy was created in this repository.', 'tainacan' ), 'info_no_taxonomy_created' => __( 'No taxonomy was created in this repository.', 'tainacan' ),
'info_no_terms_created_on_taxonomy' => __( 'No term was created for this taxonomy.', 'tainacan' ), 'info_no_terms_created_on_taxonomy' => __( 'No term was created for this taxonomy.', 'tainacan' ),
'info_no_terms_found' => __( 'No term was found here', 'tainacan' ), 'info_no_terms_found' => __( 'No term was found here', 'tainacan' ),
@ -699,6 +698,6 @@ return apply_filters( 'tainacan-admin-i18n', [
'datepicker_short_friday' => __( 'F', 'tainacan' ), 'datepicker_short_friday' => __( 'F', 'tainacan' ),
/* translators: This refers to the short label that will appear on datepickers for Saturday */ /* translators: This refers to the short label that will appear on datepickers for Saturday */
'datepicker_short_saturday' => __( 'Sa', 'tainacan' ) 'datepicker_short_saturday' => __( 'Sa', 'tainacan' )
] ); ] );
?> ?>

View File

@ -25,7 +25,7 @@ class REST_Collections_Controller extends REST_Controller {
parent::__construct(); parent::__construct();
add_action('init', array(&$this, 'init_objects'), 11); add_action('init', array(&$this, 'init_objects'), 11);
} }
/** /**
* Initialize objects after post_type register * Initialize objects after post_type register
*/ */
@ -59,7 +59,7 @@ class REST_Collections_Controller extends REST_Controller {
'callback' => array($this, 'get_item'), 'callback' => array($this, 'get_item'),
'permission_callback' => array($this, 'get_item_permissions_check'), 'permission_callback' => array($this, 'get_item_permissions_check'),
'args' => $this->get_wp_query_params(), 'args' => $this->get_wp_query_params(),
), ),
array( array(
'methods' => \WP_REST_Server::EDITABLE, 'methods' => \WP_REST_Server::EDITABLE,
@ -165,29 +165,10 @@ class REST_Collections_Controller extends REST_Controller {
$item_arr = $item->_toArray(); $item_arr = $item->_toArray();
if ( $request['context'] === 'edit' ) { if ( $request['context'] === 'edit' ) {
$moderators_ids = $item_arr['moderators_ids'];
$moderators = [];
foreach ($moderators_ids as $id){
$user_data = get_userdata($id);
if($user_data){
$user['name'] = $user_data->display_name;
//$user['roles'] = $user_data->roles;
$user['id'] = $user_data->ID;
$moderators[] = $user;
}
}
$item_arr['moderators'] = $moderators;
$item_arr['current_user_can_edit'] = $item->can_edit(); $item_arr['current_user_can_edit'] = $item->can_edit();
$item_arr['current_user_can_delete'] = $item->can_delete(); $item_arr['current_user_can_delete'] = $item->can_delete();
} }
unset($item_arr['moderators_ids']);
} else { } else {
$attributes_to_filter = $request['fetch_only']; $attributes_to_filter = $request['fetch_only'];
@ -206,12 +187,12 @@ class REST_Collections_Controller extends REST_Controller {
$item_arr['current_user_can_edit'] = $item->can_edit(); $item_arr['current_user_can_edit'] = $item->can_edit();
$item_arr['current_user_can_delete'] = $item->can_delete(); $item_arr['current_user_can_delete'] = $item->can_delete();
} }
$item_arr['url'] = get_permalink( $item_arr['id'] ); $item_arr['url'] = get_permalink( $item_arr['id'] );
} }
$total_items = wp_count_posts( $item->get_db_identifier(), 'readable' ); $total_items = wp_count_posts( $item->get_db_identifier(), 'readable' );
if (isset($total_items->publish) || if (isset($total_items->publish) ||
isset($total_items->private) || isset($total_items->private) ||
isset($total_items->trash) || isset($total_items->trash) ||
@ -227,7 +208,7 @@ class REST_Collections_Controller extends REST_Controller {
* Use this filter to add additional post_meta to the api response * Use this filter to add additional post_meta to the api response
* Use the $request object to get the context of the request and other variables * Use the $request object to get the context of the request and other variables
* For example, id context is edit, you may want to add your meta or not. * For example, id context is edit, you may want to add your meta or not.
* *
* Also take care to do any permissions verification before exposing the data * Also take care to do any permissions verification before exposing the data
*/ */
$extra_metadata = apply_filters('tainacan-api-response-collection-meta', [], $request); $extra_metadata = apply_filters('tainacan-api-response-collection-meta', [], $request);
@ -235,7 +216,7 @@ class REST_Collections_Controller extends REST_Controller {
foreach ($extra_metadata as $extra_meta) { foreach ($extra_metadata as $extra_meta) {
$item_arr[$extra_meta] = get_post_meta($item_arr['id'], $extra_meta, true); $item_arr[$extra_meta] = get_post_meta($item_arr['id'], $extra_meta, true);
} }
return $item_arr; return $item_arr;
} }
@ -273,7 +254,7 @@ class REST_Collections_Controller extends REST_Controller {
} }
return $collection->can_read(); return $collection->can_read();
} }
return false; return false;
} }
@ -295,7 +276,7 @@ class REST_Collections_Controller extends REST_Controller {
'collection' => $body 'collection' => $body
], 400); ], 400);
} }
$this->collection = new Collection(); $this->collection = new Collection();
try { try {
@ -308,7 +289,7 @@ class REST_Collections_Controller extends REST_Controller {
$collection = $this->collections_repository->insert( $prepared_post ); $collection = $this->collections_repository->insert( $prepared_post );
$response = $this->prepare_item_for_response($collection, $request); $response = $this->prepare_item_for_response($collection, $request);
do_action('tainacan-api-collection-created', $response, $request); do_action('tainacan-api-collection-created', $response, $request);
return new \WP_REST_Response($response, 201); return new \WP_REST_Response($response, 201);
@ -475,14 +456,14 @@ class REST_Collections_Controller extends REST_Controller {
public function get_endpoint_args_for_item_schema( $method = null ) { public function get_endpoint_args_for_item_schema( $method = null ) {
$endpoint_args = []; $endpoint_args = [];
if($method === \WP_REST_Server::READABLE) { if($method === \WP_REST_Server::READABLE) {
$endpoint_args['name'] = array( $endpoint_args['name'] = array(
'description' => __('Limits the result set to collections with a specific name'), 'description' => __('Limits the result set to collections with a specific name'),
'type' => 'string', 'type' => 'string',
); );
$endpoint_args = array_merge( $endpoint_args = array_merge(
$endpoint_args, $endpoint_args,
parent::get_wp_query_params(), parent::get_wp_query_params(),
parent::get_fetch_only_param(), parent::get_fetch_only_param(),
parent::get_meta_queries_params() parent::get_meta_queries_params()
@ -505,29 +486,25 @@ class REST_Collections_Controller extends REST_Controller {
return $endpoint_args; return $endpoint_args;
} }
function get_schema() { function get_schema() {
$schema = [ $schema = [
'$schema' => 'http://json-schema.org/draft-04/schema#', '$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'collection', 'title' => 'collection',
'type' => 'object' 'type' => 'object'
]; ];
$main_schema = parent::get_repository_schema( $this->collections_repository ); $main_schema = parent::get_repository_schema( $this->collections_repository );
$permissions_schema = parent::get_permissions_schema(); $permissions_schema = parent::get_permissions_schema();
// transformation done in $this->prepare_item_for_response()
$main_schema['moderators'] = $main_schema['moderators_ids'];
$main_schema['moderators']['contex'] = 'edit';
$schema['properties'] = array_merge( $schema['properties'] = array_merge(
parent::get_base_properties_schema(), parent::get_base_properties_schema(),
$main_schema, $main_schema,
$permissions_schema $permissions_schema
); );
return $schema; return $schema;
} }
} }

View File

@ -33,7 +33,6 @@ class Collection extends Entity {
$cover_page_id, $cover_page_id,
$header_image_id, $header_image_id,
$header_image, $header_image,
$moderators_ids,
$comment_status, $comment_status,
$allow_comments; $allow_comments;
@ -451,15 +450,6 @@ class Collection extends Entity {
return $this->get_mapped_property( 'filters_order' ); return $this->get_mapped_property( 'filters_order' );
} }
/**
* Get collection moderators ids
*
* @return array
*/
function get_moderators_ids() {
return $this->get_mapped_property( 'moderators_ids' );
}
/** /**
* Get collection DB identifier * Get collection DB identifier
* *
@ -708,27 +698,6 @@ class Collection extends Entity {
$this->set_mapped_property( 'header_image_id', $value ); $this->set_mapped_property( 'header_image_id', $value );
} }
/**
* Set collection moderators ids
*
* @param [string] $value
*
* @return void
*/
function set_moderators_ids( $value ) {
if(!is_array($value)) {
if(empty($value)) {
$value = [];
} else {
throw new \Exception('moderators_ids must be a array of users ids');
}
}
// make sure you never have duplicated moderators
$value = array_unique($value);
$this->set_mapped_property( 'moderators_ids', $value );
}
/** /**
* Sets if comments are allowed for the current Collection. * Sets if comments are allowed for the current Collection.
* *
@ -747,59 +716,6 @@ class Collection extends Entity {
$this->set_mapped_property('allow_comments', $value ); $this->set_mapped_property('allow_comments', $value );
} }
// Moderators methods
/**
* Add a moderator ID to the moderators_ids list
*
* @param int $user_id The user ID to be added
*
* @return boolean Wether the ID was added or not. (if it already existed in the list it returns false)
*/
function add_moderator_id( $user_id ) {
if ( is_integer( $user_id ) ) {
$current_moderators = $this->get_moderators_ids();
if ( ! in_array( $user_id, $current_moderators ) ) {
$current_moderators[] = $user_id;
$this->set_moderators_ids( $current_moderators );
return true;
}
}
return false;
}
/**
* Remove a moderator ID to the moderators_ids list
*
* @param int $user_id The user ID to be removed
*
* @return boolean Wether the ID was added or not. (if it did not exist in the list it returns false)
*/
function remove_moderator_id( $user_id ) {
if ( is_integer( $user_id ) ) {
$current_moderators = $this->get_moderators_ids();
if ( ( $key = array_search( $user_id, $current_moderators ) ) !== false ) {
unset( $current_moderators[ $key ] );
$this->set_moderators_ids( $current_moderators );
return true;
}
}
return false;
}
/**
* TODO implement the following methods to handle moderators_ids
*
* set_moderators
* get_moderators
* (the same as moderators_ids but gets and sets WP_User objects)
*
*/
/** /**
* Validate Collection * Validate Collection
* *

View File

@ -174,15 +174,6 @@ class Collections extends Repository {
//'validation' => v::numeric(), //'validation' => v::numeric(),
'default' => '' 'default' => ''
], ],
// deprecated
'moderators_ids' => [
'map' => 'meta_multi',
'title' => __( 'Moderators', 'tainacan' ),
'type' => 'array/object/string',
'items' => [ 'type' => 'array/string/integer/object' ],
'description' => __( 'Moderators of this collection', 'tainacan' ),
'validation' => ''
],
'_thumbnail_id' => [ '_thumbnail_id' => [
'map' => 'meta', 'map' => 'meta',
'title' => __( 'Thumbnail', 'tainacan' ), 'title' => __( 'Thumbnail', 'tainacan' ),
@ -293,7 +284,7 @@ class Collections extends Repository {
* to learn all args accepted in the $args parameter (@see https://developer.wordpress.org/reference/classes/wp_query/) * to learn all args accepted in the $args parameter (@see https://developer.wordpress.org/reference/classes/wp_query/)
* You can also use a mapped property, such as name and description, as an argument and it will be mapped to the * You can also use a mapped property, such as name and description, as an argument and it will be mapped to the
* appropriate WP_Query argument * appropriate WP_Query argument
* *
* If a number is passed to $args, it will return a \Tainacan\Entities\Collection object. But if the post is not found or * If a number is passed to $args, it will return a \Tainacan\Entities\Collection object. But if the post is not found or
* does not match the entity post type, it will return an empty array * does not match the entity post type, it will return an empty array
* *
@ -366,15 +357,15 @@ class Collections extends Repository {
$Tainacan_Metadata->register_core_metadata( $collection ); $Tainacan_Metadata->register_core_metadata( $collection );
if ( $this->old_collection instanceof Entities\Collection && if ( $this->old_collection instanceof Entities\Collection &&
$this->old_collection->get_parent() != $collection->get_parent() && $this->old_collection->get_parent() != $collection->get_parent() &&
$this->old_core_title instanceof Entities\Metadatum && $this->old_core_title instanceof Entities\Metadatum &&
$this->old_core_description instanceof Entities\Metadatum $this->old_core_description instanceof Entities\Metadatum
) { ) {
$Tainacan_Metadata->maybe_update_core_metadata_meta_keys( $collection, $this->old_collection, $this->old_core_title, $this->old_core_description ); $Tainacan_Metadata->maybe_update_core_metadata_meta_keys( $collection, $this->old_collection, $this->old_core_title, $this->old_core_description );
} }
} }
}
}