From 868c917c90081bf0cb2105b6315c80cf4fc8ab95 Mon Sep 17 00:00:00 2001 From: weryques Date: Tue, 20 Mar 2018 13:59:10 -0300 Subject: [PATCH 1/5] Reformat code and create a test update core field status --- .../repositories/class-tainacan-fields.php | 1024 +++++++++-------- .../class-tainacan-item-metadata.php | 262 +++-- .../repositories/class-tainacan-items.php | 4 +- tests/test-core-field-types.php | 198 ++-- 4 files changed, 780 insertions(+), 708 deletions(-) diff --git a/src/classes/repositories/class-tainacan-fields.php b/src/classes/repositories/class-tainacan-fields.php index 32ad38686..09829b4f1 100644 --- a/src/classes/repositories/class-tainacan-fields.php +++ b/src/classes/repositories/class-tainacan-fields.php @@ -1,12 +1,14 @@ get_name(), [ - 'name' => [ - 'map' => 'post_title', - 'title' => __('Name', 'tainacan'), - 'type' => 'string', - 'description'=> __('Name of the field', 'tainacan'), - 'on_error' => __('The name should be a text value and not empty', 'tainacan'), - 'validation' => v::stringType()->notEmpty(), - ], - 'slug' => [ - 'map' => 'post_name', - 'title' => __('Slug', 'tainacan'), - 'type' => 'string', - 'description'=> __('A unique and santized string representation of the field', 'tainacan'), - //'validation' => v::stringType(), - ], - 'order' => [ - 'map' => 'menu_order', - 'title' => __('Order', 'tainacan'), - 'type' => 'string/integer', - 'description'=> __('Field order. Field used if collections are manually ordered', 'tainacan'), - 'on_error' => __('The menu order should be a numeric value', 'tainacan'), - //'validation' => v::numeric(), - ], - 'parent' => [ - 'map' => 'parent', - 'title' => __('Parent', 'tainacan'), - 'type' => 'integer', - 'description'=> __('Parent field', 'tainacan'), - 'default' => 0 - //'on_error' => __('The Parent should be numeric value', 'tainacan'), - //'validation' => v::numeric(), - ], - 'description' => [ - 'map' => 'post_content', - 'title' => __('Description', 'tainacan'), - 'type' => 'string', - 'description'=> __('The field description', 'tainacan'), - 'default' => '', - //'on_error' => __('The description should be a text value', 'tainacan'), - //'validation' => v::stringType()->notEmpty(), - ], - 'field_type' => [ - 'map' => 'meta', - 'title' => __('Type', 'tainacan'), - 'type' => 'string', - 'description'=> __('The field type', 'tainacan'), - 'on_error' => __('Field type is empty', 'tainacan'), - 'validation' => v::stringType()->notEmpty(), - ], - 'required' => [ - 'map' => 'meta', - 'title' => __('Required', 'tainacan'), - 'type' => 'string', - 'description'=> __('The field is required', 'tainacan'), - 'on_error' => __('Field required field is invalid', 'tainacan'), - 'validation' => v::stringType()->in(['yes', 'no']), // yes or no - 'default' => 'no' - ], - 'collection_key' => [ - 'map' => 'meta', - 'title' => __('Collection key', 'tainacan'), - 'type' => 'string', - 'description'=> __('Field value should not be repeated', 'tainacan'), - 'on_error' => __('Collection key is invalid', 'tainacan'), - 'validation' => v::stringType()->in(['yes', 'no']), // yes or no - 'default' => 'no' - ], - 'multiple' => [ - 'map' => 'meta', - 'title' => __('Multiple', 'tainacan'), - 'type' => 'string', - 'description'=> __('Allow multiple fields for the field', 'tainacan'), - 'on_error' => __('Multiple fields is invalid', 'tainacan'), - 'validation' => v::stringType()->in(['yes', 'no']), // yes or no. It cant be multiple if its collection_key - 'default' => 'no' - ], - 'cardinality' => [ - 'map' => 'meta', - 'title' => __('Cardinality', 'tainacan'), - 'type' => 'string/number', - 'description'=> __('Number of multiples possible fields', 'tainacan'), - 'on_error' => __('The number of fields not allowed', 'tainacan'), - 'validation' => v::numeric()->positive(), - 'default' => 1 - ], - 'privacy' => [ - 'map' => 'meta', - 'title' => __('Privacy', 'tainacan'), - 'type' => 'string', - 'description'=> __('The field should be omitted in item view', 'tainacan'), - 'on_error' => __('Privacy is invalid', 'tainacan'), - 'validation' => v::stringType()->in(['yes', 'no']), // yes or no. It cant be multiple if its collection_key - 'default' => 'no' - ], - 'mask' => [ - 'map' => 'meta', - 'title' => __('Mask', 'tainacan'), - 'type' => 'string', - 'description'=> __('The mask to be used in the field', 'tainacan'), - //'on_error' => __('Mask is invalid', 'tainacan'), - //'validation' => '' - ], - 'default_value' => [ - 'map' => 'meta', - 'title' => __('Default value', 'tainacan'), - 'type' => 'string', - 'description'=> __('The value default fot the field', 'tainacan'), - ], - 'field_type_options' => [ // not showed in form - 'map' => 'meta', - 'title' => __('Field Type options', 'tainacan'), - 'type' => 'array/object/string', - 'items' => ['type' => 'array/string/integer/object'], - 'description'=> __('Options specific for field type', 'tainacan'), - // 'validation' => '' - ], - 'collection_id' => [ // not showed in form - 'map' => 'meta', - 'title' => __('Collection', 'tainacan'), - 'type' => 'integer/string', - 'description'=> __('The collection ID', 'tainacan'), - //'validation' => '' - ], - 'accept_suggestion' => [ - 'map' => 'meta', - 'title' => __('Field Value Accepts Suggestions', 'tainacan'), - 'type' => 'bool', - 'description'=> __('Allow the community suggest a different values for that field', 'tainacan'), - 'default' => false, - 'validation' => v::boolType() - ], - 'can_delete' => [ - 'map' => 'meta', - 'title' => __('Can delete', 'tainacan'), - 'type' => 'string', - 'description'=> __('The field can be deleted', 'tainacan'), - 'on_error' => __('Can delete is invalid', 'tainacan'), - 'validation' => v::stringType()->in(['yes', 'no']), // yes or no. It cant be multiple if its collection_key - 'default' => 'yes' - ], - ]); - } - + 'Tainacan\Field_Types\Core_Title', + 'Tainacan\Field_Types\Core_Description' + ]; + + /** + * Register specific hooks for field repository + */ + function __construct() { + parent::__construct(); + add_filter( 'pre_trash_post', array( &$this, 'disable_delete_core_fields' ), 10, 2 ); + add_filter( 'pre_delete_post', array( &$this, 'force_delete_core_fields' ), 10, 3 ); + } + + public function get_map() { + return apply_filters( 'tainacan-get-map-' . $this->get_name(), [ + 'name' => [ + 'map' => 'post_title', + 'title' => __( 'Name', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'Name of the field', 'tainacan' ), + 'on_error' => __( 'The name should be a text value and not empty', 'tainacan' ), + 'validation' => v::stringType()->notEmpty(), + ], + 'slug' => [ + 'map' => 'post_name', + 'title' => __( 'Slug', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'A unique and santized string representation of the field', 'tainacan' ), + //'validation' => v::stringType(), + ], + 'order' => [ + 'map' => 'menu_order', + 'title' => __( 'Order', 'tainacan' ), + 'type' => 'string/integer', + 'description' => __( 'Field order. Field used if collections are manually ordered', 'tainacan' ), + 'on_error' => __( 'The menu order should be a numeric value', 'tainacan' ), + //'validation' => v::numeric(), + ], + 'parent' => [ + 'map' => 'parent', + 'title' => __( 'Parent', 'tainacan' ), + 'type' => 'integer', + 'description' => __( 'Parent field', 'tainacan' ), + 'default' => 0 + //'on_error' => __('The Parent should be numeric value', 'tainacan'), + //'validation' => v::numeric(), + ], + 'description' => [ + 'map' => 'post_content', + 'title' => __( 'Description', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The field description', 'tainacan' ), + 'default' => '', + //'on_error' => __('The description should be a text value', 'tainacan'), + //'validation' => v::stringType()->notEmpty(), + ], + 'field_type' => [ + 'map' => 'meta', + 'title' => __( 'Type', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The field type', 'tainacan' ), + 'on_error' => __( 'Field type is empty', 'tainacan' ), + 'validation' => v::stringType()->notEmpty(), + ], + 'required' => [ + 'map' => 'meta', + 'title' => __( 'Required', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The field is required', 'tainacan' ), + 'on_error' => __( 'Field required field is invalid', 'tainacan' ), + 'validation' => v::stringType()->in( [ 'yes', 'no' ] ), // yes or no + 'default' => 'no' + ], + 'collection_key' => [ + 'map' => 'meta', + 'title' => __( 'Collection key', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'Field value should not be repeated', 'tainacan' ), + 'on_error' => __( 'Collection key is invalid', 'tainacan' ), + 'validation' => v::stringType()->in( [ 'yes', 'no' ] ), // yes or no + 'default' => 'no' + ], + 'multiple' => [ + 'map' => 'meta', + 'title' => __( 'Multiple', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'Allow multiple fields for the field', 'tainacan' ), + 'on_error' => __( 'Multiple fields is invalid', 'tainacan' ), + 'validation' => v::stringType()->in( [ 'yes', 'no' ] ), + // yes or no. It cant be multiple if its collection_key + 'default' => 'no' + ], + 'cardinality' => [ + 'map' => 'meta', + 'title' => __( 'Cardinality', 'tainacan' ), + 'type' => 'string/number', + 'description' => __( 'Number of multiples possible fields', 'tainacan' ), + 'on_error' => __( 'The number of fields not allowed', 'tainacan' ), + 'validation' => v::numeric()->positive(), + 'default' => 1 + ], + 'privacy' => [ + 'map' => 'meta', + 'title' => __( 'Privacy', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The field should be omitted in item view', 'tainacan' ), + 'on_error' => __( 'Privacy is invalid', 'tainacan' ), + 'validation' => v::stringType()->in( [ 'yes', 'no' ] ), + // yes or no. It cant be multiple if its collection_key + 'default' => 'no' + ], + 'mask' => [ + 'map' => 'meta', + 'title' => __( 'Mask', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The mask to be used in the field', 'tainacan' ), + //'on_error' => __('Mask is invalid', 'tainacan'), + //'validation' => '' + ], + 'default_value' => [ + 'map' => 'meta', + 'title' => __( 'Default value', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The value default fot the field', 'tainacan' ), + ], + 'field_type_options' => [ // not showed in form + 'map' => 'meta', + 'title' => __( 'Field Type options', 'tainacan' ), + 'type' => 'array/object/string', + 'items' => [ 'type' => 'array/string/integer/object' ], + 'description' => __( 'Options specific for field type', 'tainacan' ), + // 'validation' => '' + ], + 'collection_id' => [ // not showed in form + 'map' => 'meta', + 'title' => __( 'Collection', 'tainacan' ), + 'type' => 'integer/string', + 'description' => __( 'The collection ID', 'tainacan' ), + //'validation' => '' + ], + 'accept_suggestion' => [ + 'map' => 'meta', + 'title' => __( 'Field Value Accepts Suggestions', 'tainacan' ), + 'type' => 'bool', + 'description' => __( 'Allow the community suggest a different values for that field', 'tainacan' ), + 'default' => false, + 'validation' => v::boolType() + ], + 'can_delete' => [ + 'map' => 'meta', + 'title' => __( 'Can delete', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The field can be deleted', 'tainacan' ), + 'on_error' => __( 'Can delete is invalid', 'tainacan' ), + 'validation' => v::stringType()->in( [ 'yes', 'no' ] ), + // yes or no. It cant be multiple if its collection_key + 'default' => 'yes' + ], + ] ); + } + /** * Get the labels for the custom post type of this repository + * * @return array Labels in the format expected by register_post_type() */ public function get_cpt_labels() { return array( - 'name' => __('Fields', 'tainacan'), - 'singular_name' => __('Field', 'tainacan'), - 'add_new' => __('Add new', 'tainacan'), - 'add_new_item' => __('Add new Field', 'tainacan'), - 'edit_item' => __('Edit Field', 'tainacan'), - 'new_item' => __('New Field', 'tainacan'), - 'view_item' => __('View Field', 'tainacan'), - 'search_items' => __('Search Field', 'tainacan'), - 'not_found' => __('No Field found ', 'tainacan'), - 'not_found_in_trash' => __('No Field found in trash', 'tainacan'), - 'parent_item_colon' => __('Parent Field:', 'tainacan'), - 'menu_name' => __('Fields', 'tainacan') - ); + 'name' => __( 'Fields', 'tainacan' ), + 'singular_name' => __( 'Field', 'tainacan' ), + 'add_new' => __( 'Add new', 'tainacan' ), + 'add_new_item' => __( 'Add new Field', 'tainacan' ), + 'edit_item' => __( 'Edit Field', 'tainacan' ), + 'new_item' => __( 'New Field', 'tainacan' ), + 'view_item' => __( 'View Field', 'tainacan' ), + 'search_items' => __( 'Search Field', 'tainacan' ), + 'not_found' => __( 'No Field found ', 'tainacan' ), + 'not_found_in_trash' => __( 'No Field found in trash', 'tainacan' ), + 'parent_item_colon' => __( 'Parent Field:', 'tainacan' ), + 'menu_name' => __( 'Fields', 'tainacan' ) + ); } - public function register_post_type() { - $labels = $this->get_cpt_labels(); - $args = array( - 'labels' => $labels, - 'hierarchical' => true, - //'supports' => array('title'), - //'taxonomies' => array(self::TAXONOMY), - 'public' => true, - 'show_ui' => tnc_enable_dev_wp_interface(), - 'show_in_menu' => tnc_enable_dev_wp_interface(), - //'menu_position' => 5, - //'show_in_nav_menus' => false, - 'publicly_queryable' => true, - 'exclude_from_search' => true, - 'has_archive' => true, - 'query_var' => true, - 'can_export' => true, - 'rewrite' => true, - 'map_meta_cap' => true, - 'capability_type' => Entities\Field::get_capability_type(), - 'supports' => [ - 'title', - 'editor', - 'page-attributes' - ] - ); - register_post_type(Entities\Field::get_post_type(), $args); - } + public function register_post_type() { + $labels = $this->get_cpt_labels(); + $args = array( + 'labels' => $labels, + 'hierarchical' => true, + //'supports' => array('title'), + //'taxonomies' => array(self::TAXONOMY), + 'public' => true, + 'show_ui' => tnc_enable_dev_wp_interface(), + 'show_in_menu' => tnc_enable_dev_wp_interface(), + //'menu_position' => 5, + //'show_in_nav_menus' => false, + 'publicly_queryable' => true, + 'exclude_from_search' => true, + 'has_archive' => true, + 'query_var' => true, + 'can_export' => true, + 'rewrite' => true, + 'map_meta_cap' => true, + 'capability_type' => Entities\Field::get_capability_type(), + 'supports' => [ + 'title', + 'editor', + 'page-attributes' + ] + ); + register_post_type( Entities\Field::get_post_type(), $args ); + } - /** - * constant used in default field in attribute collection_id - * - * @return string the value of constant - */ - public function get_default_metadata_attribute(){ - return $this->default_metadata; - } + /** + * constant used in default field in attribute collection_id + * + * @return string the value of constant + */ + public function get_default_metadata_attribute() { + return $this->default_metadata; + } - /** - * register field types class on array of types - * - * @param $class_name string | object The class name or the instance - */ - public function register_field_type( $class_name ){ - if( is_object( $class_name ) ){ - $class_name = get_class( $class_name ); - } + /** + * register field types class on array of types + * + * @param $class_name string | object The class name or the instance + */ + public function register_field_type( $class_name ) { + if ( is_object( $class_name ) ) { + $class_name = get_class( $class_name ); + } - if(!in_array( $class_name, $this->field_types)){ - $this->field_types[] = $class_name; - } - } + if ( ! in_array( $class_name, $this->field_types ) ) { + $this->field_types[] = $class_name; + } + } - /** - * register field types class on array of types - * - * @param $class_name string | object The class name or the instance - */ - public function unregister_field_type( $class_name ){ - if( is_object( $class_name ) ){ - $class_name = get_class( $class_name ); - } + /** + * register field types class on array of types + * + * @param $class_name string | object The class name or the instance + */ + public function unregister_field_type( $class_name ) { + if ( is_object( $class_name ) ) { + $class_name = get_class( $class_name ); + } - $key = array_search( $class_name, $this->field_types ); - if($key !== false){ - unset( $this->field_types[$key] ); - } - } + $key = array_search( $class_name, $this->field_types ); + if ( $key !== false ) { + unset( $this->field_types[ $key ] ); + } + } /** @@ -279,29 +286,30 @@ class Fields extends Repository { * @return Entities\Field|\WP_Query|Array an instance of wp query OR array of entities; * @throws \Exception */ - public function fetch( $args, $output = null ) { + public function fetch( $args, $output = null ) { - if( is_numeric($args) ){ - $existing_post = get_post($args); - if ($existing_post instanceof \WP_Post) { - return new Entities\Field($existing_post); - } else { - return []; - } - } elseif (is_array($args)) { + if ( is_numeric( $args ) ) { + $existing_post = get_post( $args ); + if ( $existing_post instanceof \WP_Post ) { + return new Entities\Field( $existing_post ); + } else { + return []; + } + } elseif ( is_array( $args ) ) { - $args = array_merge([ - 'posts_per_page' => -1, - ], $args); - - $args = $this->parse_fetch_args($args); - - $args['post_type'] = Entities\Field::get_post_type(); + $args = array_merge( [ + 'posts_per_page' => - 1, + ], $args ); - $wp_query = new \WP_Query($args); - return $this->fetch_output($wp_query, $output); - } - } + $args = $this->parse_fetch_args( $args ); + + $args['post_type'] = Entities\Field::get_post_type(); + + $wp_query = new \WP_Query( $args ); + + return $this->fetch_output( $wp_query, $output ); + } + } /** * fetch field by collection, searches all field available @@ -313,123 +321,126 @@ class Fields extends Repository { * @return array Entities\Field * @throws \Exception */ - public function fetch_by_collection(Entities\Collection $collection, $args = [], $output = null){ - $collection_id = $collection->get_id(); + public function fetch_by_collection( Entities\Collection $collection, $args = [], $output = null ) { + $collection_id = $collection->get_id(); - //get parent collections - $parents = get_post_ancestors( $collection_id ); + //get parent collections + $parents = get_post_ancestors( $collection_id ); - //insert the actual collection - $parents[] = $collection_id; + //insert the actual collection + $parents[] = $collection_id; - //search for default field - $parents[] = $this->get_default_metadata_attribute(); + //search for default field + $parents[] = $this->get_default_metadata_attribute(); - $meta_query = array( - 'key' => 'collection_id', - 'value' => $parents, - 'compare' => 'IN', - ); + $meta_query = array( + 'key' => 'collection_id', + 'value' => $parents, + 'compare' => 'IN', + ); - if( isset( $args['meta_query'] ) ){ - $args['meta_query'][] = $meta_query; - } elseif(is_array($args)){ - $args['meta_query'] = array( $meta_query ); - } + if ( isset( $args['meta_query'] ) ) { + $args['meta_query'][] = $meta_query; + } elseif ( is_array( $args ) ) { + $args['meta_query'] = array( $meta_query ); + } - return $this->order_result( - $this->fetch( $args, $output ), - $collection, - isset( $args['include_disabled'] ) ? $args['include_disabled'] : false - ); - } + return $this->order_result( + $this->fetch( $args, $output ), + $collection, + isset( $args['include_disabled'] ) ? $args['include_disabled'] : false + ); + } - /** - * Ordinate the result from fetch response if $collection has an ordination, - * fields not ordinated appear on the end of the list - * - * - * @param $result Response from method fetch - * @param Entities\Collection $collection - * @param bool $include_disabled Wether to include disabled fields in the results or not - * @return array or WP_Query ordinate - */ - public function order_result( $result, Entities\Collection $collection, $include_disabled = false ){ - $order = $collection->get_fields_order(); - if($order) { - $order = ( is_array($order) ) ? $order : unserialize($order); - - if ( is_array($result) ){ - $result_ordinate = []; - $not_ordinate = []; - - foreach ( $result as $item ) { - $id = $item->WP_Post->ID; - $index = array_search ( $id , array_column( $order , 'id') ); - - if( $index !== false ) { - - // skipping fields disabled if the arg is set - if( !$include_disabled && isset( $order[$index]['enable'] ) && !$order[$index]['enable'] ) { - continue; - } - - $enable = ( isset( $order[$index]['enable'] )) ? $order[$index]['enable'] : true; - $item->set_enabled_for_collection($enable); - - $result_ordinate[$index] = $item; - } else { - $not_ordinate[] = $item; - } - } - - ksort ( $result_ordinate ); - $result_ordinate = array_merge( $result_ordinate, $not_ordinate ); - - return $result_ordinate; - } - // if the result is a wp query object - else { - $posts = $result->posts; - $result_ordinate = []; - $not_ordinate = []; - - foreach ( $posts as $item ) { - $id = $item->ID; - $index = array_search ( $id , array_column( $order , 'id') ); - - if( $index !== false ){ - $result_ordinate[$index] = $item; - } else { - $not_ordinate[] = $item; - } - } - - ksort ( $result_ordinate ); - $result->posts = $result_ordinate; - $result->posts = array_merge( $result->posts, $not_ordinate ); - - return $result; - } - } - return $result; - } - /** - * @param \Tainacan\Entities\Field $field - * @return \Tainacan\Entities\Field - * {@inheritDoc} - * @see \Tainacan\Repositories\Repository::insert() - */ - public function insert($field){ - global $Tainacan_Fields; + * Ordinate the result from fetch response if $collection has an ordination, + * fields not ordinated appear on the end of the list + * + * + * @param $result Response from method fetch + * @param Entities\Collection $collection + * @param bool $include_disabled Wether to include disabled fields in the results or not + * + * @return array or WP_Query ordinate + */ + public function order_result( $result, Entities\Collection $collection, $include_disabled = false ) { + $order = $collection->get_fields_order(); + if ( $order ) { + $order = ( is_array( $order ) ) ? $order : unserialize( $order ); - $this->pre_update_category_field($field); - $new_field = parent::insert($field); + if ( is_array( $result ) ) { + $result_ordinate = []; + $not_ordinate = []; - $this->update_category_field($new_field); - return $new_field; - } + foreach ( $result as $item ) { + $id = $item->WP_Post->ID; + $index = array_search( $id, array_column( $order, 'id' ) ); + + if ( $index !== false ) { + + // skipping fields disabled if the arg is set + if ( ! $include_disabled && isset( $order[ $index ]['enable'] ) && ! $order[ $index ]['enable'] ) { + continue; + } + + $enable = ( isset( $order[ $index ]['enable'] ) ) ? $order[ $index ]['enable'] : true; + $item->set_enabled_for_collection( $enable ); + + $result_ordinate[ $index ] = $item; + } else { + $not_ordinate[] = $item; + } + } + + ksort( $result_ordinate ); + $result_ordinate = array_merge( $result_ordinate, $not_ordinate ); + + return $result_ordinate; + } // if the result is a wp query object + else { + $posts = $result->posts; + $result_ordinate = []; + $not_ordinate = []; + + foreach ( $posts as $item ) { + $id = $item->ID; + $index = array_search( $id, array_column( $order, 'id' ) ); + + if ( $index !== false ) { + $result_ordinate[ $index ] = $item; + } else { + $not_ordinate[] = $item; + } + } + + ksort( $result_ordinate ); + $result->posts = $result_ordinate; + $result->posts = array_merge( $result->posts, $not_ordinate ); + + return $result; + } + } + + return $result; + } + + /** + * @param \Tainacan\Entities\Field $field + * + * @return \Tainacan\Entities\Field + * {@inheritDoc} + * @see \Tainacan\Repositories\Repository::insert() + */ + public function insert( $field ) { + global $Tainacan_Fields; + + $this->pre_update_category_field( $field ); + $new_field = parent::insert( $field ); + + $this->update_category_field( $new_field ); + + return $new_field; + } /** * @param $object @@ -438,40 +449,42 @@ class Fields extends Repository { * @return mixed|string|Entities\Entity * @throws \Exception */ - public function update($object, $new_values = null){ - return $this->insert($object); - } + public function update( $object, $new_values = null ) { + return $this->insert( $object ); + } + + public function delete( $field_id ) { + $this->delete_category_field( $field_id ); - public function delete($field_id){ - $this->delete_category_field($field_id); return new Entities\Field( wp_trash_post( $field_id ) ); - } + } - /** - * fetch all registered field type classes - * - * Possible outputs are: - * CLASS (default) - returns the Class name of of field types registered - * NAME - return an Array of the names of field types registered - * - * @param $output string CLASS | NAME - * @return array of Entities\Field_Types\Field_Type classes path name - */ - public function fetch_field_types( $output = 'CLASS'){ - $return = []; + /** + * fetch all registered field type classes + * + * Possible outputs are: + * CLASS (default) - returns the Class name of of field types registered + * NAME - return an Array of the names of field types registered + * + * @param $output string CLASS | NAME + * + * @return array of Entities\Field_Types\Field_Type classes path name + */ + public function fetch_field_types( $output = 'CLASS' ) { + $return = []; - do_action('register_field_types'); + do_action( 'register_field_types' ); - if( $output === 'NAME' ){ - foreach ($this->field_types as $field_type) { - $return[] = str_replace('Tainacan\Field_Types\\','', $field_type); - } + if ( $output === 'NAME' ) { + foreach ( $this->field_types as $field_type ) { + $return[] = str_replace( 'Tainacan\Field_Types\\', '', $field_type ); + } - return $return; - } + return $return; + } - return $this->field_types; - } + return $this->field_types; + } /** * @param Entities\Collection $collection @@ -480,49 +493,49 @@ class Fields extends Repository { * @throws \ErrorException * @throws \Exception */ - public function register_core_fields( Entities\Collection $collection ){ + public function register_core_fields( Entities\Collection $collection ) { - $fields = $this->get_core_fields( $collection ); + $fields = $this->get_core_fields( $collection ); - // TODO: create a better way to retrieve this data - $data_core_fields = [ - 'core_description' => [ - 'name' => 'Description', - 'description' => 'description', - 'collection_id' => $collection->get_id(), - 'field_type' => 'Tainacan\Field_Types\Core_Description', - 'status' => 'publish' - ], - 'core_title' => [ - 'name' => 'Title', - 'description' => 'title', - 'collection_id' => $collection->get_id(), - 'field_type' => 'Tainacan\Field_Types\Core_Title', - 'status' => 'publish' - ] - ]; + // TODO: create a better way to retrieve this data + $data_core_fields = [ + 'core_description' => [ + 'name' => 'Description', + 'description' => 'description', + 'collection_id' => $collection->get_id(), + 'field_type' => 'Tainacan\Field_Types\Core_Description', + 'status' => 'publish' + ], + 'core_title' => [ + 'name' => 'Title', + 'description' => 'title', + 'collection_id' => $collection->get_id(), + 'field_type' => 'Tainacan\Field_Types\Core_Title', + 'status' => 'publish' + ] + ]; - if( $collection->get_parent() !== 0 ){ - return false; - } + if ( $collection->get_parent() !== 0 ) { + return false; + } - foreach ( $data_core_fields as $index => $data_core_field ) { - if( empty( $fields ) ){ - $this->insert_array_field( $data_core_field ); - } else { - $exists = false; - foreach ( $fields as $field ){ - if ( $field->get_field_type() === $data_core_field['field_type'] ) { - $exists = true; - } - } + foreach ( $data_core_fields as $index => $data_core_field ) { + if ( empty( $fields ) ) { + $this->insert_array_field( $data_core_field ); + } else { + $exists = false; + foreach ( $fields as $field ) { + if ( $field->get_field_type() === $data_core_field['field_type'] ) { + $exists = true; + } + } - if( !$exists ){ - $this->insert_array_field( $data_core_field ); - } - } - } - } + if ( ! $exists ) { + $this->insert_array_field( $data_core_field ); + } + } + } + } /** * block user from remove core fields @@ -533,13 +546,13 @@ class Fields extends Repository { * @return null/bool * @throws \Exception */ - public function disable_delete_core_fields( $before, $post ){ - $field = $this->fetch( $post->ID ); + public function disable_delete_core_fields( $before, $post ) { + $field = $this->fetch( $post->ID ); - if ( $field && in_array( $field->get_field_type(), $this->core_fields ) && is_numeric($field->get_collection_id()) ) { - return false; - } - } + if ( $field && in_array( $field->get_field_type(), $this->core_fields ) && is_numeric( $field->get_collection_id() ) ) { + return false; + } + } /** * block user from remove core fields ( if use wp_delete_post) @@ -552,13 +565,13 @@ class Fields extends Repository { * @throws \Exception * @internal param The $post_id post ID which is deleting */ - public function force_delete_core_fields( $before, $post, $force_delete ){ - $field = $this->fetch( $post->ID ); + public function force_delete_core_fields( $before, $post, $force_delete ) { + $field = $this->fetch( $post->ID ); - if ( $field && in_array( $field->get_field_type(), $this->core_fields ) && is_numeric($field->get_collection_id()) ) { - return false; - } - } + if ( $field && in_array( $field->get_field_type(), $this->core_fields ) && is_numeric( $field->get_collection_id() ) ) { + return false; + } + } /** * returns all core items from a specific collection @@ -568,26 +581,26 @@ class Fields extends Repository { * @return Array|\WP_Query * @throws \Exception */ - public function get_core_fields( Entities\Collection $collection ){ - $args = []; + public function get_core_fields( Entities\Collection $collection ) { + $args = []; - $meta_query = array( - array( - 'key' => 'collection_id', - 'value' => $collection->get_id(), - 'compare' => 'IN', - ), - array( - 'key' => 'field_type', - 'value' => $this->core_fields, - 'compare' => 'IN', - ) - ); + $meta_query = array( + array( + 'key' => 'collection_id', + 'value' => $collection->get_id(), + 'compare' => '=', + ), + array( + 'key' => 'field_type', + 'value' => $this->core_fields, + 'compare' => 'IN', + ) + ); - $args['meta_query'] = $meta_query; + $args['meta_query'] = $meta_query; - return $this->fetch( $args, 'OBJECT' ); - } + return $this->fetch( $args, 'OBJECT' ); + } /** * create a field entity and insert by an associative array ( attribute => value ) @@ -598,20 +611,21 @@ class Fields extends Repository { * @throws \ErrorException * @throws \Exception */ - public function insert_array_field( $data ){ - $field = new Entities\Field(); - foreach ( $data as $attribute => $value ) { - $set_ = 'set_' . $attribute; - $field->$set_( $value ); - } + public function insert_array_field( $data ) { + $field = new Entities\Field(); + foreach ( $data as $attribute => $value ) { + $set_ = 'set_' . $attribute; + $field->$set_( $value ); + } - if ( $field->validate( )) { - $field = $this->insert( $field ); - return $field->get_id(); - } else { - throw new \ErrorException('The entity wasn\'t validated.' . print_r( $field->get_errors(), true)); - } - } + if ( $field->validate() ) { + $field = $this->insert( $field ); + + return $field->get_id(); + } else { + throw new \ErrorException( 'The entity wasn\'t validated.' . print_r( $field->get_errors(), true ) ); + } + } /** * Fetch all values of a field from a collection in all it collection items @@ -621,7 +635,7 @@ class Fields extends Repository { * * @return array|null|object */ - public function fetch_all_field_values($collection_id, $field_id){ + public function fetch_all_field_values( $collection_id, $field_id ) { global $wpdb; // Clear the result cache @@ -629,13 +643,13 @@ class Fields extends Repository { $item_post_type = "%%{$collection_id}_item"; - $collection = new Entities\Collection($collection_id); + $collection = new Entities\Collection( $collection_id ); $capabilities = $collection->get_capabilities(); $results = []; // If no has logged user or actual user can not read private posts - if(get_current_user_id() === 0 || !current_user_can( $capabilities->read_private_posts)) { + if ( get_current_user_id() === 0 || ! current_user_can( $capabilities->read_private_posts ) ) { $args = [ 'exclude_from_search' => false, 'public' => true, @@ -645,7 +659,7 @@ class Fields extends Repository { $post_statuses = get_post_stati( $args, 'names', 'and' ); - foreach ($post_statuses as $post_status) { + foreach ( $post_statuses as $post_status ) { $sql_string = $wpdb->prepare( "SELECT item_id, field_id, mvalue FROM ( @@ -662,18 +676,18 @@ class Fields extends Repository { ); $pre_result = $wpdb->get_results( $sql_string, ARRAY_A ); - if (!empty($pre_result)) { + if ( ! empty( $pre_result ) ) { $results[] = $pre_result; } } - } elseif ( current_user_can( $capabilities->read_private_posts) ) { + } elseif ( current_user_can( $capabilities->read_private_posts ) ) { $args = [ 'exclude_from_search' => false, ]; $post_statuses = get_post_stati( $args, 'names', 'and' ); - foreach ($post_statuses as $post_status) { + foreach ( $post_statuses as $post_status ) { $sql_string = $wpdb->prepare( "SELECT item_id, field_id, mvalue FROM ( @@ -691,7 +705,7 @@ class Fields extends Repository { $pre_result = $wpdb->get_results( $sql_string, ARRAY_A ); - if (!empty($pre_result)) { + if ( ! empty( $pre_result ) ) { $results[] = $pre_result; } } @@ -699,58 +713,60 @@ class Fields extends Repository { return $results; } - + /** * Stores the value of the taxonomy_id option to use on update_category_field method. * */ - private function pre_update_category_field($field) { - $field_type = $field->get_field_type_object(); + private function pre_update_category_field( $field ) { + $field_type = $field->get_field_type_object(); $current_tax = ''; - if ($field_type->get_primitive_type() == 'term') { - - $options = $this->get_mapped_property($field, 'field_type_options'); - $field_type->set_options($options); - $current_tax = $field_type->get_option('taxonomy_id'); + if ( $field_type->get_primitive_type() == 'term' ) { + + $options = $this->get_mapped_property( $field, 'field_type_options' ); + $field_type->set_options( $options ); + $current_tax = $field_type->get_option( 'taxonomy_id' ); } $this->current_taxonomy = $current_tax; } - + /** * Triggers hooks when saving a Category Field, indicating wich taxonomy was added or removed from a collection. * * This is used by Taxonomies repository to update the collections_ids property of the taxonomy as * a field type category is inserted or removed - * + * * @param [type] $field [description] + * * @return [type] [description] */ - private function update_category_field($field) { + private function update_category_field( $field ) { $field_type = $field->get_field_type_object(); - $new_tax = ''; - - if ($field_type->get_primitive_type() == 'term') { - $new_tax = $field_type->get_option('taxonomy_id'); + $new_tax = ''; + + if ( $field_type->get_primitive_type() == 'term' ) { + $new_tax = $field_type->get_option( 'taxonomy_id' ); } - - if ($new_tax != $this->current_taxonomy) { - if (!empty($this->current_taxonomy)) { - do_action('tainacan-taxonomy-removed-from-collection', $this->current_taxonomy, $field->get_collection()); + + if ( $new_tax != $this->current_taxonomy ) { + if ( ! empty( $this->current_taxonomy ) ) { + do_action( 'tainacan-taxonomy-removed-from-collection', $this->current_taxonomy, $field->get_collection() ); } - if (!empty($new_tax)) { - do_action('tainacan-taxonomy-added-to-collection', $new_tax, $field->get_collection()); + if ( ! empty( $new_tax ) ) { + do_action( 'tainacan-taxonomy-added-to-collection', $new_tax, $field->get_collection() ); } - + } } - - private function delete_category_field($field_id) { - $field = $this->fetch($field_id); + + private function delete_category_field( $field_id ) { + $field = $this->fetch( $field_id ); $field_type = $field->get_field_type_object(); - if ($field_type->get_primitive_type() == 'term') { - $removed_tax = $field_type->get_option('taxonomy_id'); - if (!empty($removed_tax)) - do_action('tainacan-taxonomy-removed-from-collection', $removed_tax, $field->get_collection()); + if ( $field_type->get_primitive_type() == 'term' ) { + $removed_tax = $field_type->get_option( 'taxonomy_id' ); + if ( ! empty( $removed_tax ) ) { + do_action( 'tainacan-taxonomy-removed-from-collection', $removed_tax, $field->get_collection() ); + } } } } diff --git a/src/classes/repositories/class-tainacan-item-metadata.php b/src/classes/repositories/class-tainacan-item-metadata.php index 5641089b4..8dae87ba3 100644 --- a/src/classes/repositories/class-tainacan-item-metadata.php +++ b/src/classes/repositories/class-tainacan-item-metadata.php @@ -1,77 +1,78 @@ is_multiple(); - + public function insert( $item_metadata ) { + + $unique = ! $item_metadata->is_multiple(); + $field_type = $item_metadata->get_field()->get_field_type_object(); - if ($field_type->get_core()) { - $this->save_core_field_value($item_metadata); - } elseif ($field_type->get_primitive_type() == 'term') { - $this->save_terms_field_value($item_metadata); + if ( $field_type->get_core() ) { + $this->save_core_field_value( $item_metadata ); + } elseif ( $field_type->get_primitive_type() == 'term' ) { + $this->save_terms_field_value( $item_metadata ); } else { - if ($unique) { - update_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $item_metadata->get_value() ) ); - } else { - delete_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id()); - - if (is_array($item_metadata->get_value())){ - $values = $item_metadata->get_value(); + if ( $unique ) { + update_post_meta( $item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $item_metadata->get_value() ) ); + } else { + delete_post_meta( $item_metadata->item->get_id(), $item_metadata->field->get_id() ); - foreach ($values as $value){ - add_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $value )); - } - } - } + if ( is_array( $item_metadata->get_value() ) ) { + $values = $item_metadata->get_value(); + + foreach ( $values as $value ) { + add_post_meta( $item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $value ) ); + } + } + } } - - - - do_action('tainacan-insert', $item_metadata); - do_action('tainacan-insert-Item_Metadata_Entity', $item_metadata); - return new Entities\Item_Metadata_Entity($item_metadata->get_item(), $item_metadata->get_field()); - } + + do_action( 'tainacan-insert', $item_metadata ); + do_action( 'tainacan-insert-Item_Metadata_Entity', $item_metadata ); + + return new Entities\Item_Metadata_Entity( $item_metadata->get_item(), $item_metadata->get_field() ); + } /** * @param $item_metadata * * @return mixed|void */ - public function delete($item_metadata){} + public function delete( $item_metadata ) { + } - public function save_core_field_value(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) { - $field_type = $item_metadata->get_field()->get_field_type_object(); - if ($field_type->get_core()) { - $item = $item_metadata->get_item(); - $set_method = 'set_' . $field_type->get_related_mapped_prop(); - $value = $item_metadata->get_value(); - $item->$set_method( is_array( $value ) ? $value[0] : $value ); - if ($item->validate_core_fields()) { - global $Tainacan_Items; - $Tainacan_Items->insert($item); - } else { - throw new \Exception('Item metadata should be validated beforehand'); - } - } - } - - public function save_terms_field_value($item_metadata) { + public function save_core_field_value( \Tainacan\Entities\Item_Metadata_Entity $item_metadata ) { $field_type = $item_metadata->get_field()->get_field_type_object(); - if ($field_type->get_primitive_type() == 'term') { + if ( $field_type->get_core() ) { + $item = $item_metadata->get_item(); + $set_method = 'set_' . $field_type->get_related_mapped_prop(); + $value = $item_metadata->get_value(); + $item->$set_method( is_array( $value ) ? $value[0] : $value ); + if ( $item->validate_core_fields() ) { + global $Tainacan_Items; + $Tainacan_Items->update( $item ); + } else { + throw new \Exception( 'Item metadata should be validated beforehand' ); + } + } + } + + public function save_terms_field_value( $item_metadata ) { + $field_type = $item_metadata->get_field()->get_field_type_object(); + if ( $field_type->get_primitive_type() == 'term' ) { $new_terms = $item_metadata->get_value(); - $taxonomy = new Entities\Taxonomy( $field_type->get_option('taxonomy_id') ); - if( $taxonomy ){ - wp_set_object_terms($item_metadata->get_item()->get_id(), $new_terms, $taxonomy->get_db_identifier() ); - } + $taxonomy = new Entities\Taxonomy( $field_type->get_option( 'taxonomy_id' ) ); + if ( $taxonomy ) { + wp_set_object_terms( $item_metadata->get_item()->get_id(), $new_terms, $taxonomy->get_db_identifier() ); + } } @@ -85,78 +86,87 @@ class Item_Metadata extends Repository { * @return array * @throws \Exception */ - public function fetch($object, $output = null ){ - if($object instanceof Entities\Item){ - global $Tainacan_Items, $Tainacan_Fields; - - $collection = $object->get_collection(); - - if (!$collection instanceof Entities\Collection){ - return []; - } - - $meta_list = $Tainacan_Fields->fetch_by_collection($collection, [], 'OBJECT' ); - - $return = []; - - if (is_array($meta_list)) { - foreach ($meta_list as $meta) { - $return[] = new Entities\Item_Metadata_Entity($object, $meta); - } - } - - return $return; - }else{ - return []; - } - } + public function fetch( $object, $output = null ) { + if ( $object instanceof Entities\Item ) { + global $Tainacan_Items, $Tainacan_Fields; - /** - * Get the value for a Item field. - * - * @param Entities\Item_Metadata_Entity $item_metadata - * @return mixed - */ - public function get_value(Entities\Item_Metadata_Entity $item_metadata) { - $unique = ! $item_metadata->is_multiple(); - - $field_type = $item_metadata->get_field()->get_field_type_object(); - if ($field_type->get_core()) { - $item = $item_metadata->get_item(); - - $get_method = 'get_' . $field_type->get_related_mapped_prop(); - return $item->$get_method(); - - } elseif ($field_type->get_primitive_type() == 'term') { + $collection = $object->get_collection(); - if( is_numeric( $field_type->get_option('taxonomy_id') ) ){ - $taxonomy = new Entities\Taxonomy( $field_type->get_option('taxonomy_id') ); - if( $taxonomy ){ - $taxonomy_slug = $taxonomy->get_db_identifier(); - } else { - return null; - } - } else { - return null; - } + if ( ! $collection instanceof Entities\Collection ) { + return []; + } + + $meta_list = $Tainacan_Fields->fetch_by_collection( $collection, [], 'OBJECT' ); + + $return = []; + + if ( is_array( $meta_list ) ) { + foreach ( $meta_list as $meta ) { + $return[] = new Entities\Item_Metadata_Entity( $object, $meta ); + } + } + + return $return; + } else { + return []; + } + } + + /** + * Get the value for a Item field. + * + * @param Entities\Item_Metadata_Entity $item_metadata + * + * @return mixed + */ + public function get_value( Entities\Item_Metadata_Entity $item_metadata ) { + $unique = ! $item_metadata->is_multiple(); + + $field_type = $item_metadata->get_field()->get_field_type_object(); + if ( $field_type->get_core() ) { + $item = $item_metadata->get_item(); + + $get_method = 'get_' . $field_type->get_related_mapped_prop(); + + return $item->$get_method(); + + } elseif ( $field_type->get_primitive_type() == 'term' ) { + + if ( is_numeric( $field_type->get_option( 'taxonomy_id' ) ) ) { + $taxonomy = new Entities\Taxonomy( $field_type->get_option( 'taxonomy_id' ) ); + if ( $taxonomy ) { + $taxonomy_slug = $taxonomy->get_db_identifier(); + } else { + return null; + } + } else { + return null; + } + + $terms = wp_get_object_terms( $item_metadata->get_item()->get_id(), $taxonomy_slug ); + + if ( $unique ) { + $terms = reset( $terms ); + } - $terms = wp_get_object_terms($item_metadata->get_item()->get_id(), $taxonomy_slug ); - - if ($unique) - $terms = reset($terms); - return $terms; - - } else { - return get_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), $unique); - } - - } - public function register_post_type() { } - - public function get_map() { return []; } - public function get_default_properties($map) { return []; } + } else { + return get_post_meta( $item_metadata->item->get_id(), $item_metadata->field->get_id(), $unique ); + } + + } + + public function register_post_type() { + } + + public function get_map() { + return []; + } + + public function get_default_properties( $map ) { + return []; + } /** * @param $object @@ -164,15 +174,17 @@ class Item_Metadata extends Repository { * @return mixed */ public function update( $object, $new_values = null ) { - return $this->insert($object); + return $this->insert( $object ); } - /** - * Suggest a value to be inserted as a item Field value, return a pending log - * @param Entities\Item_Metadata_Entity $item_metadata - * @return Entities\Log - */ - public function suggest($item_metadata) { - return Entities\Log::create(false, '', $item_metadata, null, 'pending'); + /** + * Suggest a value to be inserted as a item Field value, return a pending log + * + * @param Entities\Item_Metadata_Entity $item_metadata + * + * @return Entities\Log + */ + public function suggest( $item_metadata ) { + return Entities\Log::create( false, '', $item_metadata, null, 'pending' ); } } diff --git a/src/classes/repositories/class-tainacan-items.php b/src/classes/repositories/class-tainacan-items.php index 7adffe57a..07f09b2a6 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -145,12 +145,12 @@ class Items extends Repository { // iterate through the native post properties foreach ( $map as $prop => $mapped ) { - if ( $mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi' && $mapped['map'] != 'terms' && $mapped['map'] != 'thumbnail_id' ) { + if ( $mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi' && $mapped['map'] != 'terms' ) { $item->WP_Post->{$mapped['map']} = $item->get_mapped_property( $prop ); } } - // save post and geet its ID + // save post and get its ID $item->WP_Post->post_type = $cpt; //$item->WP_Post->post_status = 'publish'; diff --git a/tests/test-core-field-types.php b/tests/test-core-field-types.php index b9ee0cf7e..25f2f6876 100644 --- a/tests/test-core-field-types.php +++ b/tests/test-core-field-types.php @@ -8,88 +8,132 @@ namespace Tainacan\Tests; * @package Test_Tainacan */ -use Tainacan\Entities; +class CoreFieldTypes extends TAINACAN_UnitApiTestCase { -/** - * Sample test case. - */ -class CoreFieldTypes extends TAINACAN_UnitTestCase { - - function test_core_field_types() { - - global $Tainacan_Item_Metadata, $Tainacan_Items; - - $collection = $this->tainacan_entity_factory->create_entity( + function test_core_field_types() { + + global $Tainacan_Item_Metadata, $Tainacan_Items; + + $collection = $this->tainacan_entity_factory->create_entity( 'collection', array( - 'name' => 'test', + 'name' => 'test', ), true ); - - $field = $this->tainacan_entity_factory->create_entity( - 'field', - array( - 'name' => 'metadado', - 'description' => 'title', - 'collection' => $collection, - 'field_type' => 'Tainacan\Field_Types\Core_Title' - ), - true - ); - - $fieldDescription = $this->tainacan_entity_factory->create_entity( - 'field', - array( - 'name' => 'metadado_desc', - 'description' => 'description', - 'collection' => $collection, - 'field_type' => 'Tainacan\Field_Types\Core_Description' - ), - true - ); - - - $i = $this->tainacan_entity_factory->create_entity( - 'item', - array( - 'title' => 'item test', - 'description' => 'adasdasdsa', - 'collection' => $collection - ), - true - ); - - - $item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($i, $field); - $item_metadata->set_value('changed title'); - $item_metadata->validate(); - - $Tainacan_Item_Metadata->insert($item_metadata); - - $checkItem = $Tainacan_Items->fetch($i->get_id()); - - $this->assertEquals('changed title', $checkItem->get_title()); - - $check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($checkItem, $field); - $this->assertEquals('changed title', $check_item_metadata->get_value()); - - - // description - $item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($i, $fieldDescription); - $item_metadata->set_value('changed description'); - $item_metadata->validate(); - - $Tainacan_Item_Metadata->insert($item_metadata); - - $checkItem = $Tainacan_Items->fetch($i->get_id()); - - $this->assertEquals('changed description', $checkItem->get_description()); - - $check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($checkItem, $fieldDescription); - $this->assertEquals('changed description', $check_item_metadata->get_value()); - - } - + + $field = $this->tainacan_entity_factory->create_entity( + 'field', + array( + 'name' => 'metadado', + 'description' => 'title', + 'collection' => $collection, + 'field_type' => 'Tainacan\Field_Types\Core_Title' + ), + true + ); + + $fieldDescription = $this->tainacan_entity_factory->create_entity( + 'field', + array( + 'name' => 'metadado_desc', + 'description' => 'description', + 'collection' => $collection, + 'field_type' => 'Tainacan\Field_Types\Core_Description' + ), + true + ); + + + $i = $this->tainacan_entity_factory->create_entity( + 'item', + array( + 'title' => 'item test', + 'description' => 'adasdasdsa', + 'collection' => $collection + ), + true + ); + + + $item_metadata = new \Tainacan\Entities\Item_Metadata_Entity( $i, $field ); + $item_metadata->set_value( 'changed title' ); + $item_metadata->validate(); + + $Tainacan_Item_Metadata->insert( $item_metadata ); + + $checkItem = $Tainacan_Items->fetch( $i->get_id() ); + + $this->assertEquals( 'changed title', $checkItem->get_title() ); + + $check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity( $checkItem, $field ); + $this->assertEquals( 'changed title', $check_item_metadata->get_value() ); + + + // description + $item_metadata = new \Tainacan\Entities\Item_Metadata_Entity( $i, $fieldDescription ); + $item_metadata->set_value( 'changed description' ); + $item_metadata->validate(); + + $Tainacan_Item_Metadata->insert( $item_metadata ); + + $checkItem = $Tainacan_Items->fetch( $i->get_id() ); + + $this->assertEquals( 'changed description', $checkItem->get_description() ); + + $check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity( $checkItem, $fieldDescription ); + $this->assertEquals( 'changed description', $check_item_metadata->get_value() ); + + } + + protected function get_fields( $collection ) { + $request_fields = new \WP_REST_Request( 'GET', $this->namespace . '/collection/' . $collection->get_id() . '/fields' ); + + $response = $this->server->dispatch( $request_fields ); + + $data = $response->get_data(); + + return $data; + } + + public function test_update_core_fields_status() { + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'test', + ), + true + ); + + // GET + $data1 = self::get_fields( $collection ); + + // PATCH + $field_id = $data1[0]['id']; + + $body = json_encode( array( + 'status' => 'private' + ) ); + + $request = new \WP_REST_Request( 'PATCH', $this->namespace . '/collection/' . $collection->get_id() . '/fields/' . $field_id ); + $request->set_body( $body ); + + $response = $this->server->dispatch( $request ); + + $data2 = $response->get_data(); + $this->assertEquals('private', $data2['status']); + + // GET + $data3 = self::get_fields( $collection ); + + $this->assertCount(2, $data3); + + $statuses = [$data3[0]['status'], $data3[1]['status']]; + + $this->assertContains('private', $statuses); + $this->assertContains('publish', $statuses); + + } + } \ No newline at end of file From c60902a035cc5830e5a5aa5c93198a3df94eec2c Mon Sep 17 00:00:00 2001 From: weryques Date: Tue, 20 Mar 2018 14:14:27 -0300 Subject: [PATCH 2/5] Build watch for macOS --- build-watch.sh | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/build-watch.sh b/build-watch.sh index 4977df4db..698d272ee 100755 --- a/build-watch.sh +++ b/build-watch.sh @@ -4,8 +4,16 @@ echo 'Watching changes on src/' -while inotifywait -qqr src -e create,move,modify,delete; do +current_OS=`uname` + +# For macOS (Darwin) is needed fsevents-tools installed (you can use homebrew install fsevents-tools) +if [ $current_OS == "Darwin" ]; then echo - echo 'Change detected, running build' - ./build.sh -done \ No newline at end of file + notifyloop src ./build.sh +else + while inotifywait -qqr src -e create,move,modify,delete; do + echo + echo 'Change detected, running build' + ./build.sh + done +fi \ No newline at end of file From ed9bc12471d61e8b1919c7660024ed4da535bd99 Mon Sep 17 00:00:00 2001 From: Eduardo humberto Date: Tue, 20 Mar 2018 15:08:10 -0300 Subject: [PATCH 3/5] allow create new terms in field type category --- src/admin/tainacan-admin-i18n.php | 3 + .../field-types/category/AddNewTerm.vue | 125 ++++++++++++++++++ src/classes/field-types/category/Category.vue | 43 ++++-- .../field-types/category/CategoryCheckbox.vue | 3 +- .../field-types/category/CategoryRadio.vue | 3 +- .../category/CategorySelectbox.vue | 54 ++++++++ .../field-types/category/CategoryTaginput.vue | 27 ++-- 7 files changed, 235 insertions(+), 23 deletions(-) create mode 100644 src/classes/field-types/category/AddNewTerm.vue create mode 100644 src/classes/field-types/category/CategorySelectbox.vue diff --git a/src/admin/tainacan-admin-i18n.php b/src/admin/tainacan-admin-i18n.php index c26f2fbe9..076fefe38 100644 --- a/src/admin/tainacan-admin-i18n.php +++ b/src/admin/tainacan-admin-i18n.php @@ -113,6 +113,8 @@ return [ 'label_collection_items' => __('Collection Items', 'tainacan'), 'label_collection_fields' => __('Collection Fields', 'tainacan'), 'label_collection_filters' => __('Collection Filters', 'tainacan'), + 'label_parent_term' => __('Parent Term', 'tainacan'), + 'label_add_new_term' => __('Add New Term', 'tainacan'), // Instructions. More complex sentences to guide user and placeholders 'instruction_dragndrop_fields_collection' => __('Drag and drop Fields here to Collection.', 'tainacan'), @@ -126,6 +128,7 @@ return [ 'instruction_select_a_filter_type' => __('Select a filter type:', 'tainacan'), // Info. Other feedback to user. + 'info_name_is_required' => __('Name is required.', 'tainacan'), 'info_no_collection_created' => __('No collection was created in this repository.', 'tainacan'), 'info_no_item_created' => __('No item was created in this collection.', 'tainacan'), 'info_error_deleting_collection' => __('Error on deleting collection.', 'tainacan'), diff --git a/src/classes/field-types/category/AddNewTerm.vue b/src/classes/field-types/category/AddNewTerm.vue new file mode 100644 index 000000000..20daa78b7 --- /dev/null +++ b/src/classes/field-types/category/AddNewTerm.vue @@ -0,0 +1,125 @@ + + + \ No newline at end of file diff --git a/src/classes/field-types/category/Category.vue b/src/classes/field-types/category/Category.vue index 4a6d2de83..20df30da4 100644 --- a/src/classes/field-types/category/Category.vue +++ b/src/classes/field-types/category/Category.vue @@ -1,16 +1,29 @@ \ No newline at end of file + + + \ No newline at end of file diff --git a/src/classes/field-types/category/CategoryCheckbox.vue b/src/classes/field-types/category/CategoryCheckbox.vue index 48e145cf3..5cd61a38e 100644 --- a/src/classes/field-types/category/CategoryCheckbox.vue +++ b/src/classes/field-types/category/CategoryCheckbox.vue @@ -1,6 +1,6 @@ diff --git a/src/classes/field-types/category/CategoryRadio.vue b/src/classes/field-types/category/CategoryRadio.vue index 0c61184ec..bb6b09afb 100644 --- a/src/classes/field-types/category/CategoryRadio.vue +++ b/src/classes/field-types/category/CategoryRadio.vue @@ -1,6 +1,6 @@ diff --git a/src/classes/field-types/category/CategorySelectbox.vue b/src/classes/field-types/category/CategorySelectbox.vue new file mode 100644 index 000000000..3b7b4ba80 --- /dev/null +++ b/src/classes/field-types/category/CategorySelectbox.vue @@ -0,0 +1,54 @@ + + + \ No newline at end of file diff --git a/src/classes/field-types/category/CategoryTaginput.vue b/src/classes/field-types/category/CategoryTaginput.vue index 65a704cb0..2e061d554 100644 --- a/src/classes/field-types/category/CategoryTaginput.vue +++ b/src/classes/field-types/category/CategoryTaginput.vue @@ -5,6 +5,7 @@ rounded icon="magnify" :allowNew="allowNew" + @input="emitChange" v-model="selected" :data="labels" field="label" @@ -22,19 +23,6 @@ } }, watch: { - selected( val ){ - this.selected = val; - let results = []; - for( let term of val ){ - if( term.value ){ - results.push( term.value ); - } else { - results.push( term ); - } - } - this.$emit('input', results); - this.$emit('blur'); - }, terms( val ){ this.selectedValues(); } @@ -74,6 +62,19 @@ } this.selected = selected; } + }, + emitChange(){ + let val = this.selected; + let results = []; + for( let term of val ){ + if( term.value ){ + results.push( term.value ); + } else { + results.push( term ); + } + } + this.$emit('input', results); + this.$emit('blur'); } } } From c75140b2f3a99d0d148b3a036b4bd1754154a199 Mon Sep 17 00:00:00 2001 From: Eduardo humberto Date: Tue, 20 Mar 2018 15:13:10 -0300 Subject: [PATCH 4/5] remove web component from dev interface --- src/dev-interface/class-tainacan-dev-interface.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/dev-interface/class-tainacan-dev-interface.php b/src/dev-interface/class-tainacan-dev-interface.php index 4d0142184..5d5cf062b 100644 --- a/src/dev-interface/class-tainacan-dev-interface.php +++ b/src/dev-interface/class-tainacan-dev-interface.php @@ -107,14 +107,6 @@ class DevInterface { ); - add_meta_box( - $col->get_db_identifier() . '_metadata_js', - __('Field Components', 'tainacan'), - array(&$this, 'metadata_components_metabox'), - $col->get_db_identifier(), //post type - 'normal' - - ); } From 4401ac95b96de0587d9e7fc778a0900f28e9c502 Mon Sep 17 00:00:00 2001 From: Eduardo humberto Date: Tue, 20 Mar 2018 16:03:15 -0300 Subject: [PATCH 5/5] fix errors on create term in field type category --- src/classes/field-types/category/AddNewTerm.vue | 7 ++++--- src/classes/field-types/category/Category.vue | 4 +++- src/classes/field-types/category/CategoryCheckbox.vue | 5 +++++ src/classes/field-types/category/CategoryRadio.vue | 11 ++++++----- .../field-types/category/CategorySelectbox.vue | 5 +++++ 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/classes/field-types/category/AddNewTerm.vue b/src/classes/field-types/category/AddNewTerm.vue index 20daa78b7..7539ae8fd 100644 --- a/src/classes/field-types/category/AddNewTerm.vue +++ b/src/classes/field-types/category/AddNewTerm.vue @@ -89,14 +89,16 @@ .then( res => { instance.name = ''; instance.parent = 0; + if( res.data && res.data.term_id || res.term_id ){ let term_id = ( res.term_id ) ? res.term_id : res.data.term_id; let val = this.value; + if( !Array.isArray( val ) && this.field.field.multiple === 'no' ){ axios.patch(`/item/${this.item_id}/metadata/${this.field_id}`, { values: term_id, }).then( res => { - instance.$emit('newTerm', true); + instance.$emit('newTerm', term_id); }) } else { val = ( val ) ? val : []; @@ -104,10 +106,9 @@ axios.patch(`/item/${this.item_id}/metadata/${this.field_id}`, { values: val, }).then( res => { - instance.$emit('newTerm', true); + instance.$emit('newTerm', val); }) } - } }); } diff --git a/src/classes/field-types/category/Category.vue b/src/classes/field-types/category/Category.vue index 20df30da4..1da5d2d58 100644 --- a/src/classes/field-types/category/Category.vue +++ b/src/classes/field-types/category/Category.vue @@ -125,7 +125,9 @@ this.$emit('input', this.inputValue); this.$emit('blur'); }, - reload(){ + reload( val ){ + this.valueComponent = val; + this.terms = []; this.getTermsFromTaxonomy(); this.getTermsId(); diff --git a/src/classes/field-types/category/CategoryCheckbox.vue b/src/classes/field-types/category/CategoryCheckbox.vue index 5cd61a38e..fda2e17ab 100644 --- a/src/classes/field-types/category/CategoryCheckbox.vue +++ b/src/classes/field-types/category/CategoryCheckbox.vue @@ -28,6 +28,11 @@ checked: [] } }, + watch: { + value( val ){ + this.checked = val; + } + }, props: { options: { type: Array diff --git a/src/classes/field-types/category/CategoryRadio.vue b/src/classes/field-types/category/CategoryRadio.vue index bb6b09afb..676cd8d30 100644 --- a/src/classes/field-types/category/CategoryRadio.vue +++ b/src/classes/field-types/category/CategoryRadio.vue @@ -19,13 +19,14 @@