changes core author metadata to user metadata #354

This commit is contained in:
vnmedeiros 2020-04-17 09:29:11 -03:00
parent 48977e8852
commit b5d486961e
17 changed files with 118 additions and 181 deletions

View File

@ -63,9 +63,6 @@ $repos = [
[
'name' => 'get_core_description_metadatum',
],
[
'name' => 'get_core_author_metadatum',
],
[
'name' => 'fetch_all_metadatum_values',
],

View File

@ -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"

View File

@ -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}

View File

@ -25,7 +25,6 @@ class Dublin_Core extends Mapper {
],
'dc:creator' => [
'label' => 'Creator',
'core_metadatum' => 'author'
],
'dc:date' => [
'label' => 'Date',

View File

@ -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 );
}
}

View File

@ -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 )

View File

@ -1,71 +0,0 @@
<?php
namespace Tainacan\Metadata_Types;
use Tainacan\Entities\Metadatum;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class Core_Author extends Metadata_Type {
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->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);
}
}

View File

@ -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

View File

@ -45,6 +45,7 @@ export default {
data() {
return {
users: [],
defaultAuthor: 'no',
isLoadingCurrentUser: false,
isFetchingUsers: false,
userId: null,

View File

@ -0,0 +1,97 @@
<?php
namespace Tainacan\Metadata_Types;
use Tainacan\Entities\Metadatum;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class TainacanMetadatumType
*/
class User extends Metadata_Type {
function __construct(){
// call metadatum type constructor
parent::__construct();
$this->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('
<div>
<div class="control is-clearfix">
user
</div>
</div>
');
}
/**
* 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);
}
}

View File

@ -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);

View File

@ -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(

View File

@ -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);

View File

@ -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));
}

View File

@ -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());

View File

@ -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) );

View File

@ -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() ) );
}