From b5d486961e91f1900d74c2f8c4c543f0f350307b Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Fri, 17 Apr 2020 09:29:11 -0300 Subject: [PATCH] changes core author metadata to user metadata #354 --- docs/generate-repo-descriptions.php | 3 - .../entities/class-tainacan-collection.php | 13 +-- src/classes/entities/class-tainacan-item.php | 11 --- .../mappers/class-tainacan-dublin-core.php | 1 - .../class-tainacan-collections.php | 6 +- .../repositories/class-tainacan-metadata.php | 54 +---------- .../class-tainacan-core-author.php | 71 -------------- .../class-tainacan-metadata-type-helper.php | 1 + .../{core-author/Author.vue => user/User.vue} | 1 + .../user/class-tainacan-user.php | 97 +++++++++++++++++++ src/views/admin/js/main.js | 4 +- tests/test-api-items.php | 2 +- tests/test-api-metadata.php | 2 +- tests/test-capabilities.php | 8 +- tests/test-core-metadatum-types.php | 17 +--- tests/test-item-metadata.php | 2 +- tests/test-metadata.php | 6 +- 17 files changed, 118 insertions(+), 181 deletions(-) delete mode 100644 src/views/admin/components/metadata-types/core-author/class-tainacan-core-author.php rename src/views/admin/components/metadata-types/{core-author/Author.vue => user/User.vue} (98%) create mode 100644 src/views/admin/components/metadata-types/user/class-tainacan-user.php diff --git a/docs/generate-repo-descriptions.php b/docs/generate-repo-descriptions.php index 4988cd3ac..1861170ec 100644 --- a/docs/generate-repo-descriptions.php +++ b/docs/generate-repo-descriptions.php @@ -63,9 +63,6 @@ $repos = [ [ 'name' => 'get_core_description_metadatum', ], - [ - 'name' => 'get_core_author_metadatum', - ], [ 'name' => 'fetch_all_metadatum_values', ], diff --git a/src/classes/entities/class-tainacan-collection.php b/src/classes/entities/class-tainacan-collection.php index b9cbb99f0..0848a34c8 100644 --- a/src/classes/entities/class-tainacan-collection.php +++ b/src/classes/entities/class-tainacan-collection.php @@ -494,7 +494,7 @@ class Collection extends Entity { } /** - * Get the two core metadata of the collection (title, description and author) + * Get the two core metadata of the collection (title and description) * * @return array[\Tainacan\Entities\Metadatum] */ @@ -527,17 +527,6 @@ class Collection extends Entity { return $repo->get_core_description_metadatum($this); } - /** - * Get the Core Author Metadatum of the collection - * - * @return array[\Tainacan\Entities\Metadatum] - */ - function get_core_author_metadatum() { - $repo = \Tainacan\Repositories\Metadata::get_instance(); - - return $repo->get_core_author_metadatum($this); - } - /** * Checks if comments are allowed for the current Collection. * @return string "open"|"closed" diff --git a/src/classes/entities/class-tainacan-item.php b/src/classes/entities/class-tainacan-item.php index dc5712ba7..494a542fb 100644 --- a/src/classes/entities/class-tainacan-item.php +++ b/src/classes/entities/class-tainacan-item.php @@ -384,17 +384,6 @@ class Item extends Entity { $this->set_mapped_property('comment_status', $value); } - /** - * Define the author id - * - * @param [integer] $value - * - * @return void - */ - function set_author_id( $value ) { - $this->set_mapped_property( 'author_id', $value ); - } - /** * * {@inheritDoc} diff --git a/src/classes/mappers/class-tainacan-dublin-core.php b/src/classes/mappers/class-tainacan-dublin-core.php index 08beb6716..6563765ae 100644 --- a/src/classes/mappers/class-tainacan-dublin-core.php +++ b/src/classes/mappers/class-tainacan-dublin-core.php @@ -25,7 +25,6 @@ class Dublin_Core extends Mapper { ], 'dc:creator' => [ 'label' => 'Creator', - 'core_metadatum' => 'author' ], 'dc:date' => [ 'label' => 'Date', diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index aaae01c57..2d556dfa5 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -348,7 +348,6 @@ class Collections extends Repository { $this->old_collection = $this->fetch( $collection->get_id() ); $this->old_core_title = $collection->get_core_title_metadatum(); $this->old_core_description = $collection->get_core_description_metadatum(); - $this->old_core_author = $collection->get_core_author_metadatum(); } @@ -361,10 +360,9 @@ class Collections extends Repository { if ( $this->old_collection instanceof Entities\Collection && $this->old_collection->get_parent() != $collection->get_parent() && $this->old_core_title instanceof Entities\Metadatum && - $this->old_core_description instanceof Entities\Metadatum && - $this->old_core_author 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, $this->old_core_author); + $Tainacan_Metadata->maybe_update_core_metadata_meta_keys( $collection, $this->old_collection, $this->old_core_title, $this->old_core_description ); } } diff --git a/src/classes/repositories/class-tainacan-metadata.php b/src/classes/repositories/class-tainacan-metadata.php index a896d3df7..415b6ffe8 100644 --- a/src/classes/repositories/class-tainacan-metadata.php +++ b/src/classes/repositories/class-tainacan-metadata.php @@ -19,8 +19,7 @@ class Metadata extends Repository { public $core_metadata = [ 'Tainacan\Metadata_Types\Core_Title', - 'Tainacan\Metadata_Types\Core_Description', - 'Tainacan\Metadata_Types\Core_Author' + 'Tainacan\Metadata_Types\Core_Description' ]; private static $instance = null; @@ -666,7 +665,7 @@ class Metadata extends Repository { * @return void * @throws \Exception */ - public function maybe_update_core_metadata_meta_keys( Entities\Collection $collection_new, Entities\Collection $collection_old, Entities\Metadatum $old_title_metadatum, Entities\Metadatum $old_description_metadatum, Entities\Metadatum $old_author_metadatum ) { + public function maybe_update_core_metadata_meta_keys( Entities\Collection $collection_new, Entities\Collection $collection_old, Entities\Metadatum $old_title_metadatum, Entities\Metadatum $old_description_metadatum ) { global $wpdb; @@ -678,13 +677,10 @@ class Metadata extends Repository { wp_delete_post( $old_description_metadatum->get_id(), true ); update_post_meta( $old_title_metadatum->get_id(), 'metadata_type', 'to_delete', $old_title_metadatum->get_metadata_type() ); wp_delete_post( $old_title_metadatum->get_id(), true ); - update_post_meta( $old_author_metadatum->get_id(), 'metadata_type', 'to_delete', $old_author_metadatum->get_metadata_type() ); - wp_delete_post( $old_author_metadatum->get_id(), true ); } $new_title_metadatum = $collection_new->get_core_title_metadatum(); $new_description_metadatum = $collection_new->get_core_description_metadatum(); - $new_author_metadatum = $collection_new->get_core_author_metadatum(); $sql_statement = $wpdb->prepare( "UPDATE $wpdb->postmeta @@ -710,18 +706,6 @@ class Metadata extends Repository { $res = $wpdb->query( $sql_statement ); - $sql_statement = $wpdb->prepare( - "UPDATE $wpdb->postmeta - SET meta_key = %s - WHERE meta_key = %s AND post_id IN ( - SELECT ID - FROM $wpdb->posts - WHERE post_type = %s - )", $new_author_metadatum->get_id(), $old_author_metadatum->get_id(), $item_post_type - ); - - $res = $wpdb->query( $sql_statement ); - wp_cache_flush(); } @@ -749,14 +733,6 @@ class Metadata extends Repository { 'status' => 'publish', 'display' => 'yes', ], - 'core_author' => [ - 'name' => 'Author', - 'description' => 'Author', - 'collection_id' => $collection->get_id(), - 'metadata_type' => 'Tainacan\Metadata_Types\Core_Author', - 'status' => 'private', - 'display' => 'no', - ] ]; } @@ -920,32 +896,6 @@ class Metadata extends Repository { return false; } - /** - * Get the Core Author Metadatum for a collection - * - * @param Entities\Collection $collection - * - * @return \Tainacan\Entities\Metadatum The Core Author Metadatum - * @throws \Exception - */ - public function get_core_author_metadatum( Entities\Collection $collection ) { - - $results = $this->fetch_by_collection( $collection, [ - 'meta_query' => [ - [ - 'key' => 'metadata_type', - 'value' => 'Tainacan\Metadata_Types\Core_Author', - ] - ], - 'posts_per_page' => 1 - ] ); - - if ( is_array( $results ) && sizeof( $results ) == 1 && $results[0] instanceof \Tainacan\Entities\Metadatum ) { - return $results[0]; - } - - return false; - } /** * create a metadatum entity and insert by an associative array ( attribute => value ) diff --git a/src/views/admin/components/metadata-types/core-author/class-tainacan-core-author.php b/src/views/admin/components/metadata-types/core-author/class-tainacan-core-author.php deleted file mode 100644 index e75de85c9..000000000 --- a/src/views/admin/components/metadata-types/core-author/class-tainacan-core-author.php +++ /dev/null @@ -1,71 +0,0 @@ -set_primitive_type('user'); - $this->set_core(true); - $this->set_related_mapped_prop('author_id'); - $this->set_component('tainacan-author'); - $this->set_name( __('Core Author', 'tainacan') ); - $this->set_description( __('The "Core Author" is a compulsory metadata automatically created for all collections by default.', 'tainacan') ); - } - - /** - * generate the metadata for this metadatum type - */ - public function form(){ - - } - - /** - * Core author metadatum type is stored as the item author - * - * Lets validate it as the item title - * - * @param TainacanEntitiesItem_Metadata_Entity $item_metadata - * @return bool Valid or not - * - * Quarantine - Core metadata should be validated as any other metadata - * and item title is no longer mandatory - * public function validate(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) { } - */ - - public function validate_options( Metadatum $metadatum ) { - if ( !in_array($metadatum->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) ) - return true; - - if ( $metadatum->get_multiple() != 'no') { - return ['multiple' => __('Core Metadata can not accept multiple values', 'tainacan')]; - } - - if ($metadatum->get_required() != 'no') { - return ['multiple' => __('Core Author Metadata is required', 'tainacan')]; - } - - return true; - } - - /** - * Get the value as a HTML string - * @return string - */ - public function get_value_as_html(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) { - $value = $item_metadata->get_value(); - $name = get_the_author_meta( 'display_name', $value ); - return apply_filters("tainacan-item-get-author-name", $name, $this); - } - -} \ No newline at end of file diff --git a/src/views/admin/components/metadata-types/metadata-type-helper/class-tainacan-metadata-type-helper.php b/src/views/admin/components/metadata-types/metadata-type-helper/class-tainacan-metadata-type-helper.php index 601bdfe6b..f073f36da 100644 --- a/src/views/admin/components/metadata-types/metadata-type-helper/class-tainacan-metadata-type-helper.php +++ b/src/views/admin/components/metadata-types/metadata-type-helper/class-tainacan-metadata-type-helper.php @@ -40,6 +40,7 @@ class Metadata_Type_Helper { $this->Tainacan_Metadata->register_metadata_type('Tainacan\Metadata_Types\Relationship'); $this->Tainacan_Metadata->register_metadata_type('Tainacan\Metadata_Types\Taxonomy'); $this->Tainacan_Metadata->register_metadata_type('Tainacan\Metadata_Types\Compound'); + $this->Tainacan_Metadata->register_metadata_type('Tainacan\Metadata_Types\User'); // the priority should see less than on function // `load_admin_page()` of class `Admin` in file /src/views/class-tainacan-admin.php diff --git a/src/views/admin/components/metadata-types/core-author/Author.vue b/src/views/admin/components/metadata-types/user/User.vue similarity index 98% rename from src/views/admin/components/metadata-types/core-author/Author.vue rename to src/views/admin/components/metadata-types/user/User.vue index 9c094a688..4868f8ba4 100644 --- a/src/views/admin/components/metadata-types/core-author/Author.vue +++ b/src/views/admin/components/metadata-types/user/User.vue @@ -45,6 +45,7 @@ export default { data() { return { users: [], + defaultAuthor: 'no', isLoadingCurrentUser: false, isFetchingUsers: false, userId: null, diff --git a/src/views/admin/components/metadata-types/user/class-tainacan-user.php b/src/views/admin/components/metadata-types/user/class-tainacan-user.php new file mode 100644 index 000000000..0d6b3ea2d --- /dev/null +++ b/src/views/admin/components/metadata-types/user/class-tainacan-user.php @@ -0,0 +1,97 @@ +set_primitive_type('user'); + $this->set_component('tainacan-user'); + $this->set_name( __('User', 'tainacan') ); + $this->set_description( __('A registered user on wordpress', 'tainacan') ); + $this->set_preview_template(' +
+
+ user +
+
+ '); + } + + /** + * generate the metadata for this metadatum type + */ + public function form() { + + } + + function user_exists($user) { + // if( !is_int($user) ) + // return username_exists($user) !== false; + + global $wpdb; + // Check cache: + if (wp_cache_get($user, 'users')) return true; + // Check database: + if ($wpdb->get_var($wpdb->prepare("SELECT EXISTS (SELECT 1 FROM $wpdb->users WHERE ID = %d)", $user))) + return true; + return false; + } + + /** + * User metadatum type is stored as id of WP user + * + * @param TainacanEntitiesItem_Metadata_Entity $item_metadata + * @return bool Valid or not + * + */ + public function validate(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) { + $value = $item_metadata->get_value(); + if ( is_array($values) ) { + foreach($values as $user) { + if ( !$this->user_exists($user) ) { + $this->add_error( sprintf(__('User does not exist %s.', 'tainacan'), $user ) ); + return false; + } + } + } else { + if ( !$this->user_exists($value) ) { + $this->add_error( sprintf( __('User does not exist %s.', 'tainacan'), $user ) ); + return false; + } + } + return true; + } + + public function validate_options( Metadatum $metadatum ) { + if ( !in_array($metadatum->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) ) { + return true; + } + + if ( empty( $this->get_option('default_author') ) || !in_array ( $this->get_option('default_author'), ['yes', 'no'] ) ) { + return ['default_author' => __('Invalid config default author','tainacan')]; + } + return true; + } + + /** + * Get the value as a HTML string + * @return string + */ + public function get_value_as_html(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) { + $value = $item_metadata->get_value(); + $name = get_the_author_meta( 'display_name', $value ); + return apply_filters("tainacan-item-get-author-name", $name, $this); + } + +} \ No newline at end of file diff --git a/src/views/admin/js/main.js b/src/views/admin/js/main.js index 19dd9f5fa..9386edf94 100644 --- a/src/views/admin/js/main.js +++ b/src/views/admin/js/main.js @@ -20,7 +20,7 @@ import Date from '../components/metadata-types/date/Date.vue'; import Relationship from '../components/metadata-types/relationship/Relationship.vue'; import Taxonomy from '../components/metadata-types/taxonomy/Taxonomy.vue'; import Compound from '../components/metadata-types/compound/Compound.vue'; -import Author from '../components/metadata-types/core-author/Author.vue'; +import User from '../components/metadata-types/user/User.vue'; import FormRelationship from '../components/metadata-types/relationship/FormRelationship.vue'; import FormTaxonomy from '../components/metadata-types/taxonomy/FormTaxonomy.vue'; @@ -95,7 +95,7 @@ Vue.component('tainacan-date', Date); Vue.component('tainacan-relationship', Relationship); Vue.component('tainacan-taxonomy', Taxonomy); Vue.component('tainacan-compound', Compound); -Vue.component('tainacan-author', Author); +Vue.component('tainacan-user', User); /* Metadata Option forms */ Vue.component('tainacan-form-relationship', FormRelationship); diff --git a/tests/test-api-items.php b/tests/test-api-items.php index fa10aede5..8c83cb29f 100644 --- a/tests/test-api-items.php +++ b/tests/test-api-items.php @@ -380,7 +380,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { $data = $response->get_data()['items']; $this->assertEquals( 2, sizeof($data) ); - $this->assertEquals( 6, sizeof($data[0]['metadata']) ); + $this->assertEquals( 5, sizeof($data[0]['metadata']) ); // Fetch only as admin $request = new \WP_REST_Request( diff --git a/tests/test-api-metadata.php b/tests/test-api-metadata.php index 3d2d5e73d..06525444c 100644 --- a/tests/test-api-metadata.php +++ b/tests/test-api-metadata.php @@ -825,7 +825,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $response = $this->server->dispatch($request); $data = $response->get_data(); - $this->assertEquals(5, count($data['metadata_order'])); + $this->assertEquals(4, count($data['metadata_order'])); wp_logout(); wp_set_current_user(0); diff --git a/tests/test-capabilities.php b/tests/test-capabilities.php index d9db8b010..0c38b8d5a 100644 --- a/tests/test-capabilities.php +++ b/tests/test-capabilities.php @@ -510,17 +510,17 @@ class Capabilities extends TAINACAN_UnitTestCase { $current_user = $this->subscriber2; // force update current user object with new capabilities $meta = tainacan_metadata()->fetch_by_collection($this->public_collection); - $this->AssertEquals(6, sizeof($meta)); + $this->AssertEquals(5, sizeof($meta)); $meta = tainacan_metadata()->fetch_ids_by_collection($this->public_collection); - $this->AssertEquals(6, sizeof($meta)); + $this->AssertEquals(5, sizeof($meta)); $this->subscriber2->add_cap( 'tnc_rep_read_private_metadata' ); $current_user = $this->subscriber2; // force update current user object with new capabilities $meta = tainacan_metadata()->fetch_by_collection($this->public_collection); - $this->AssertEquals(7, sizeof($meta)); + $this->AssertEquals(6, sizeof($meta)); $meta = tainacan_metadata()->fetch_ids_by_collection($this->public_collection); - $this->AssertEquals(7, sizeof($meta)); + $this->AssertEquals(6, sizeof($meta)); } diff --git a/tests/test-core-metadatum-types.php b/tests/test-core-metadatum-types.php index b82ffede8..c73c070e5 100644 --- a/tests/test-core-metadatum-types.php +++ b/tests/test-core-metadatum-types.php @@ -34,9 +34,6 @@ class CoreMetadatumTypes extends TAINACAN_UnitTestCase { $metadatumDescription = $collection->get_core_description_metadatum(); - $metadatumAuthor = $collection->get_core_author_metadatum(); - - $i = $this->tainacan_entity_factory->create_entity( 'item', array( @@ -91,9 +88,6 @@ class CoreMetadatumTypes extends TAINACAN_UnitTestCase { $this->assertEquals('changed description', $checkMeta[0]->get_description()); - // author - $item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatumAuthor); - } function test_validate_required_title() { @@ -200,7 +194,7 @@ class CoreMetadatumTypes extends TAINACAN_UnitTestCase { $core_metadata = $collection->get_core_metadata(); - $this->assertEquals(3, sizeof($core_metadata)); + $this->assertEquals(2, sizeof($core_metadata)); $this->assertNotEquals('Tainacan\Metadata_Types\Text', $core_metadata[0]->get_metadata_type()); $this->assertNotEquals('Tainacan\Metadata_Types\Text', $core_metadata[1]->get_metadata_type()); @@ -260,7 +254,6 @@ class CoreMetadatumTypes extends TAINACAN_UnitTestCase { 'title' => 'Son of son', 'description' => 'Desc of son of son', 'collection' => $collection_son, - 'author_id' => get_current_user_id(), 'status' => 'publish' ), true @@ -278,13 +271,7 @@ class CoreMetadatumTypes extends TAINACAN_UnitTestCase { 'Desc of son of son' ); - $item_metadatum_author = $this->tainacan_item_metadata_factory->create_item_metadata( - $collection_son_item, - $core_metadata_son[2], - get_current_user_id() - ); - - $this->assertEquals($core_metadata_son[0]->get_id(), $item_metadatum_desc->get_metadatum()->get_id()); + $this->assertEquals($core_metadata_son[0]->get_id(), $item_metadatum_desc->get_metadatum()->get_id()); $this->assertEquals($core_metadata_son[1]->get_id(), $item_metadatum_title->get_metadatum()->get_id()); $this->assertNotEquals($core_metadata_parent[0]->get_id(), $item_metadatum_desc->get_metadatum()->get_id()); diff --git a/tests/test-item-metadata.php b/tests/test-item-metadata.php index abd2a1461..9badf85e3 100644 --- a/tests/test-item-metadata.php +++ b/tests/test-item-metadata.php @@ -240,7 +240,7 @@ class Item_Metadata extends TAINACAN_UnitTestCase { $this->assertTrue(is_array($item_metadatas)); // notice for repository metadata - $this->assertEquals(4, sizeof($item_metadatas)); + $this->assertEquals(3, sizeof($item_metadatas)); //first 2 metadata are repository metadata $this->assertTrue( in_array('metadado', $names) ); diff --git a/tests/test-metadata.php b/tests/test-metadata.php index a76841066..43a47d73b 100644 --- a/tests/test-metadata.php +++ b/tests/test-metadata.php @@ -165,8 +165,8 @@ class Metadata extends TAINACAN_UnitTestCase { $retrieve_metadata_ids = $Tainacan_Metadata->fetch_ids_by_collection( $collection_son, [] ); // should return 6 - $this->assertEquals( 7, sizeof( $retrieve_metadata ) ); - $this->assertEquals( 7, sizeof( $retrieve_metadata_ids ) ); + $this->assertEquals( 6, sizeof( $retrieve_metadata ) ); + $this->assertEquals( 6, sizeof( $retrieve_metadata_ids ) ); } /** @@ -197,7 +197,7 @@ class Metadata extends TAINACAN_UnitTestCase { */ function test_metadata_metadata_type(){ $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - $this->assertEquals( 8, sizeof( $Tainacan_Metadata->fetch_metadata_types() ) ); + $this->assertEquals( 9, sizeof( $Tainacan_Metadata->fetch_metadata_types() ) ); }