From 52848190d33c32e3190d958a51f6ffcc17f01f32 Mon Sep 17 00:00:00 2001 From: weryques Date: Mon, 13 Aug 2018 14:59:16 -0300 Subject: [PATCH] Now repository methods (delete, insert, update) call explicitly the insert_logs method (issue #89) --- .../class-tainacan-collections.php | 87 +- .../repositories/class-tainacan-filters.php | 692 ++++----- .../class-tainacan-item-metadata.php | 444 +++--- .../repositories/class-tainacan-items.php | 399 +++--- .../repositories/class-tainacan-logs.php | 44 +- .../repositories/class-tainacan-metadata.php | 1234 +++++++++-------- .../class-tainacan-repository.php | 127 +- .../class-tainacan-taxonomies.php | 369 ++--- .../repositories/class-tainacan-terms.php | 374 ++--- 9 files changed, 1938 insertions(+), 1832 deletions(-) diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index f1d7f70e8..8eec98c6b 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -36,53 +36,53 @@ class Collections extends Repository { */ public function get_map() { return apply_filters( 'tainacan-get-map-' . $this->get_name(), [ - 'name' => [ + 'name' => [ 'map' => 'post_title', 'title' => __( 'Name', 'tainacan' ), 'type' => 'string', 'description' => __( 'The title of the collection', 'tainacan' ), 'validation' => v::stringType()->notEmpty(), ], - 'status' => [ + 'status' => [ 'map' => 'post_status', 'title' => __( 'Status', 'tainacan' ), 'type' => 'string', 'default' => '', 'description' => __( 'The current situation of the post', 'tainacan' ) ], - 'author_id' => [ + 'author_id' => [ 'map' => 'post_author', 'title' => __( 'Author ID', 'tainacan' ), 'type' => 'string', 'description' => __( 'The collection author\'s user ID (numeric string)', 'tainacan' ) ], - 'creation_date' => [ + 'creation_date' => [ 'map' => 'post_date', 'title' => __( 'Creation Date', 'tainacan' ), 'type' => 'string', 'description' => __( 'The collection creation date', 'tainacan' ) ], - 'modification_date' => [ + 'modification_date' => [ 'map' => 'post_modified', 'title' => __( 'Modification Date', 'tainacan' ), 'type' => 'string', 'description' => __( 'The collection modification date', 'tainacan' ) ], - 'order' => [ + 'order' => [ 'map' => 'menu_order', 'title' => __( 'Order', 'tainacan' ), 'type' => 'string', 'description' => __( 'Collection order. This metadata is used if collections are manually ordered.', 'tainacan' ), //'validation' => v::stringType(), ], - 'parent' => [ + 'parent' => [ 'map' => 'post_parent', 'title' => __( 'Parent Collection', 'tainacan' ), 'type' => 'integer', 'description' => __( 'Original collection from which features are inherited', 'tainacan' ), //'validation' => v::stringType(), ], - 'description' => [ + 'description' => [ 'map' => 'post_content', 'title' => __( 'Description', 'tainacan' ), 'type' => 'string', @@ -90,14 +90,14 @@ class Collections extends Repository { 'default' => '', //'validation' => v::stringType(), ], - 'slug' => [ + 'slug' => [ 'map' => 'post_name', 'title' => __( 'Slug', 'tainacan' ), 'type' => 'string', 'description' => __( 'An unique and sanitized string representation of the collection, used to build the collection URL. It must not contain any special characters or spaces.', 'tainacan' ), //'validation' => v::stringType(), ], - 'default_orderby' => [ + 'default_orderby' => [ 'map' => 'meta', 'title' => __( 'Default Order metadata', 'tainacan' ), 'type' => 'string', @@ -105,7 +105,7 @@ class Collections extends Repository { 'default' => 'name', //'validation' => v::stringType(), ], - 'default_order' => [ + 'default_order' => [ 'map' => 'meta', 'title' => __( 'Default order', 'tainacan' ), 'description' => __( 'Default order for items in this collection. ASC or DESC', 'tainacan' ), @@ -122,7 +122,7 @@ class Collections extends Repository { 'description' => __( 'List of collection properties that will be displayed in the table view', 'tainacan' ), //'validation' => v::stringType(), ], - 'default_view_mode' => [ + 'default_view_mode' => [ 'map' => 'meta', 'title' => __( 'Default view mode', 'tainacan' ), 'type' => 'string', @@ -130,13 +130,13 @@ class Collections extends Repository { 'default' => 'table', //'validation' => v::stringType(), ], - 'enabled_view_modes' => [ + 'enabled_view_modes' => [ 'map' => 'meta', 'title' => __( 'Enabled view modes', 'tainacan' ), 'type' => 'array', 'description' => __( 'Which visualization modes will be available for the public to choose from', 'tainacan' ), - 'default' => ['table', 'cards'], - 'items' => [ 'type' => 'string' ], + 'default' => [ 'table', 'cards' ], + 'items' => [ 'type' => 'string' ], //'validation' => v::stringType(), ], 'metadata_order' => [ @@ -147,7 +147,7 @@ class Collections extends Repository { 'description' => __( 'Collection metadata ordination', 'tainacan' ), //'validation' => v::stringType(), ], - 'filters_order' => [ + 'filters_order' => [ 'map' => 'meta', 'title' => __( 'Ordination filters', 'tainacan' ), 'type' => 'array/object/string', @@ -155,7 +155,7 @@ class Collections extends Repository { 'description' => __( 'Collection filters ordination', 'tainacan' ), //'validation' => v::stringType(), ], - 'enable_cover_page' => [ + 'enable_cover_page' => [ 'map' => 'meta', 'title' => __( 'Enable Cover Page', 'tainacan' ), 'type' => 'string', @@ -164,7 +164,7 @@ class Collections extends Repository { 'validation' => v::stringType()->in( [ 'yes', 'no' ] ), // yes or no 'default' => 'no' ], - 'cover_page_id' => [ + 'cover_page_id' => [ 'map' => 'meta', 'title' => __( 'Cover Page ID', 'tainacan' ), 'type' => 'string', @@ -173,7 +173,7 @@ class Collections extends Repository { //'validation' => v::numeric(), 'default' => '' ], - 'header_image_id' => [ + 'header_image_id' => [ 'map' => 'meta', 'title' => __( 'Header Image', 'tainacan' ), 'type' => 'string', @@ -182,7 +182,7 @@ class Collections extends Repository { //'validation' => v::numeric(), 'default' => '' ], - 'moderators_ids' => [ + 'moderators_ids' => [ 'map' => 'meta_multi', 'title' => __( 'Moderators', 'tainacan' ), 'type' => 'array/object/string', @@ -190,7 +190,7 @@ class Collections extends Repository { 'description' => __( 'To assign users as Moderators of this collection', 'tainacan' ), 'validation' => '' ], - '_thumbnail_id' => [ + '_thumbnail_id' => [ 'map' => 'meta', 'title' => __( 'Thumbnail', 'tainacan' ), 'description' => __( 'Squared reduced-size version of a picture that helps recognizing and organizing files', 'tainacan' ) @@ -262,13 +262,13 @@ class Collections extends Repository { public function insert( $collection ) { $this->pre_process( $collection ); - + $new_collection = parent::insert( $collection ); $this->handle_core_metadata( $new_collection ); $collection->register_collection_item_post_type(); - flush_rewrite_rules(false); // needed to activate items post type archive url + flush_rewrite_rules( false ); // needed to activate items post type archive url $this->update_moderators( $new_collection ); return $new_collection; @@ -287,7 +287,9 @@ class Collections extends Repository { $deleted = new Entities\Collection( wp_delete_post( $collection_id, true ) ); if ( $deleted ) { - do_action( 'tainacan-deleted', $deleted, [], false, true ); + $this->logs_repository->insert_log( $deleted, [], false, true ); + + do_action( 'tainacan-deleted', $deleted ); } return $deleted; @@ -301,8 +303,10 @@ class Collections extends Repository { public function trash( $collection_id ) { $trashed = new Entities\Collection( wp_trash_post( $collection_id ) ); - if($trashed) { - do_action( 'tainacan-trashed', $trashed, [], false, false, true ); + if ( $trashed ) { + $this->logs_repository->insert_log( $trashed, [], false, false, true ); + + do_action( 'tainacan-trashed', $trashed ); } return $trashed; @@ -332,7 +336,7 @@ class Collections extends Repository { } elseif ( is_array( $args ) ) { $args = array_merge( [ - 'posts_per_page' => -1, + 'posts_per_page' => - 1, ], $args ); $args = $this->parse_fetch_args( $args ); @@ -341,7 +345,7 @@ class Collections extends Repository { // TODO: Pegar coleções registradas via código - $args = apply_filters('tainacan_fetch_args', $args, 'collections'); + $args = apply_filters( 'tainacan_fetch_args', $args, 'collections' ); $wp_query = new \WP_Query( $args ); @@ -350,19 +354,20 @@ class Collections extends Repository { } public function fetch_by_db_identifier( $db_identifier ) { - if ( $id = $this->get_id_by_db_identifier($db_identifier) ) { - return $this->fetch($id); + if ( $id = $this->get_id_by_db_identifier( $db_identifier ) ) { + return $this->fetch( $id ); } } - - public function get_id_by_db_identifier($db_identifier) { + + public function get_id_by_db_identifier( $db_identifier ) { $prefix = \Tainacan\Entities\Collection::$db_identifier_prefix; - $sufix = \Tainacan\Entities\Collection::$db_identifier_sufix; - $id = str_replace($prefix, '', $db_identifier); - $id = str_replace($sufix, '', $id); - if (is_numeric($id)) { + $sufix = \Tainacan\Entities\Collection::$db_identifier_sufix; + $id = str_replace( $prefix, '', $db_identifier ); + $id = str_replace( $sufix, '', $id ); + if ( is_numeric( $id ) ) { return (int) $id; } + return false; } @@ -371,8 +376,8 @@ class Collections extends Repository { $current_moderators = $this->get_mapped_property( $collection, 'moderators_ids' ); $this->current_moderators = is_array( $current_moderators ) ? $current_moderators : []; - $this->old_collection = $this->fetch( $collection->get_id() ); - $this->old_core_title = $collection->get_core_title_metadatum(); + $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(); @@ -388,13 +393,13 @@ class Collections extends Repository { do_action( 'tainacan-remove-collection-moderators', $collection, $deleted ); } - function handle_core_metadata ( $collection ) { + function handle_core_metadata( $collection ) { $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); $Tainacan_Metadata->register_core_metadata( $collection ); - + if ( $this->old_collection instanceof Entities\Collection && $this->old_collection->get_parent() != $collection->get_parent() ) { - $Tainacan_Metadata->maybe_update_core_metadata_meta_keys( $collection, $this->old_collection, $this->old_core_title, $this->old_core_description ); + $Tainacan_Metadata->maybe_update_core_metadata_meta_keys( $collection, $this->old_collection, $this->old_core_title, $this->old_core_description ); } } diff --git a/src/classes/repositories/class-tainacan-filters.php b/src/classes/repositories/class-tainacan-filters.php index bc5b47bdb..8bcb3ab1c 100644 --- a/src/classes/repositories/class-tainacan-filters.php +++ b/src/classes/repositories/class-tainacan-filters.php @@ -1,150 +1,153 @@ get_name(), [ + 'name' => [ + 'map' => 'post_title', + 'title' => __( 'Name', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'Name of the filter', 'tainacan' ), + 'on_error' => __( 'The filter name should be a text value and not empty', 'tainacan' ), + 'validation' => v::stringType()->notEmpty(), + ], + 'order' => [ + 'map' => 'menu_order', + 'title' => __( 'Order', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'Filter order. This metadata is used if filters were manually ordered.', 'tainacan' ), + 'validation' => '' + ], + 'description' => [ + 'map' => 'post_content', + 'title' => __( 'Description', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The filter description', 'tainacan' ), + 'validation' => '', + 'default' => '', + ], + 'filter_type_options' => [ + 'map' => 'meta', + 'title' => __( 'Filter type options', 'tainacan' ), + 'type' => 'array/object/string', + 'items' => [ 'type' => 'array/string/integer/object' ], + 'description' => __( 'The filter type options', 'tainacan' ), + 'validation' => '' + ], + 'filter_type' => [ + 'map' => 'meta', + 'title' => __( 'Type', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The filter type', 'tainacan' ), + 'validation' => '' + ], + 'collection_id' => [ + 'map' => 'meta', + 'title' => __( 'Collection', 'tainacan' ), + 'type' => 'integer/string', + 'description' => __( 'The collection ID', 'tainacan' ), + 'validation' => '' + ], + 'color' => [ + 'map' => 'meta', + 'title' => __( 'Color', 'tainacan' ), + 'type' => 'integer', + 'description' => __( 'Filter color', 'tainacan' ), + 'validation' => '' + ], + 'metadatum' => [ + 'map' => 'meta', + 'title' => __( 'Metadata', 'tainacan' ), + 'type' => 'integer', + 'description' => __( 'Filter metadata', 'tainacan' ), + 'validation' => '' + ], + 'max_options' => [ + 'map' => 'meta', + 'title' => __( 'Max of options', 'tainacan' ), + 'type' => 'integer/string', + 'description' => __( 'The max number of options to be showed in filter sidebar.', 'tainacan' ), + 'validation' => '', + 'default' => 4 + ] + ] ); + } - 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 filter', 'tainacan' ), - 'on_error' => __( 'The filter name should be a text value and not empty', 'tainacan' ), - 'validation' => v::stringType()->notEmpty(), - ], - 'order' => [ - 'map' => 'menu_order', - 'title' => __( 'Order', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'Filter order. This metadata is used if filters were manually ordered.', 'tainacan' ), - 'validation' => '' - ], - 'description' => [ - 'map' => 'post_content', - 'title' => __( 'Description', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'The filter description', 'tainacan' ), - 'validation' => '', - 'default' => '', - ], - 'filter_type_options' => [ - 'map' => 'meta', - 'title' => __( 'Filter type options', 'tainacan' ), - 'type' => 'array/object/string', - 'items' => [ 'type' => 'array/string/integer/object' ], - 'description' => __( 'The filter type options', 'tainacan' ), - 'validation' => '' - ], - 'filter_type' => [ - 'map' => 'meta', - 'title' => __( 'Type', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'The filter type', 'tainacan' ), - 'validation' => '' - ], - 'collection_id' => [ - 'map' => 'meta', - 'title' => __( 'Collection', 'tainacan' ), - 'type' => 'integer/string', - 'description' => __( 'The collection ID', 'tainacan' ), - 'validation' => '' - ], - 'color' => [ - 'map' => 'meta', - 'title' => __( 'Color', 'tainacan' ), - 'type' => 'integer', - 'description' => __( 'Filter color', 'tainacan' ), - 'validation' => '' - ], - 'metadatum' => [ - 'map' => 'meta', - 'title' => __( 'Metadata', 'tainacan' ), - 'type' => 'integer', - 'description' => __( 'Filter metadata', 'tainacan' ), - 'validation' => '' - ], - 'max_options' => [ - 'map' => 'meta', - 'title' => __( 'Max of options', 'tainacan' ), - 'type' => 'integer/string', - 'description' => __( 'The max number of options to be showed in filter sidebar.', 'tainacan' ), - 'validation' => '', - 'default' => 4 - ] - ]); - } - /** * 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' => __('Filters', 'tainacan'), - 'singular_name' => __('Filter', 'tainacan'), - 'add_new' => __('Add new', 'tainacan'), - 'add_new_item' => __('Add new Filter', 'tainacan'), - 'edit_item' => __('Edit Filter', 'tainacan'), - 'new_item' => __('New Filter', 'tainacan'), - 'view_item' => __('View Filter', 'tainacan'), - 'search_items' => __('Search Filters', 'tainacan'), - 'not_found' => __('No Filters found ', 'tainacan'), - 'not_found_in_trash' => __('No Filters found in trash', 'tainacan'), - 'parent_item_colon' => __('Parent Filter:', 'tainacan'), - 'menu_name' => __('Filters', 'tainacan') - ); + 'name' => __( 'Filters', 'tainacan' ), + 'singular_name' => __( 'Filter', 'tainacan' ), + 'add_new' => __( 'Add new', 'tainacan' ), + 'add_new_item' => __( 'Add new Filter', 'tainacan' ), + 'edit_item' => __( 'Edit Filter', 'tainacan' ), + 'new_item' => __( 'New Filter', 'tainacan' ), + 'view_item' => __( 'View Filter', 'tainacan' ), + 'search_items' => __( 'Search Filters', 'tainacan' ), + 'not_found' => __( 'No Filters found ', 'tainacan' ), + 'not_found_in_trash' => __( 'No Filters found in trash', 'tainacan' ), + 'parent_item_colon' => __( 'Parent Filter:', 'tainacan' ), + 'menu_name' => __( 'Filters', '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\Metadatum::get_capability_type(), - 'supports' => [ - 'title', - 'editor', - 'page-attributes' - ] - ); - register_post_type(Entities\Filter::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\Metadatum::get_capability_type(), + 'supports' => [ + 'title', + 'editor', + 'page-attributes' + ] + ); + register_post_type( Entities\Filter::get_post_type(), $args ); + } /** @@ -192,267 +195,276 @@ class Filters extends Repository { * * @return Entities\Filter */ - public function delete($filter_id){ - $deleted = new Entities\Filter( wp_delete_post( $filter_id, true ) ); + public function delete( $filter_id ) { + $deleted = new Entities\Filter( wp_delete_post( $filter_id, true ) ); - if ( $deleted ) { - do_action( 'tainacan-deleted', $deleted, [], false, true ); - } + if ( $deleted ) { + $this->logs_repository->insert_log( $deleted, [], false, true ); - return $deleted; - } + do_action( 'tainacan-deleted', $deleted ); + } + + return $deleted; + } /** * @param $filter_id * * @return mixed|Entities\Filter */ - public function trash($filter_id){ - $trashed = new Entities\Filter(wp_trash_post($filter_id)); + public function trash( $filter_id ) { + $trashed = new Entities\Filter( wp_trash_post( $filter_id ) ); - if($trashed){ - do_action('tainacan-trashed', $trashed, [], false, false, true); - } + if ( $trashed ) { + $this->logs_repository->insert_log( $trashed, [], false, false, true ); - return $trashed; - } + do_action( 'tainacan-trashed', $trashed ); + } - public function update($object, $new_values = null){ - return $this->insert($object); - } + return $trashed; + } - /** - * fetch filter based on ID or WP_Query args - * - * Filters are stored as posts. Check WP_Query docs - * to learn all args accepted in the $args parameter (@see https://developer.wordpress.org/reference/classes/wp_query/) - * You can also use a mapped property, such as name and description, as an argument and it will be mapped to the - * appropriate WP_Query argument - * - * @param array $args WP_Query args || int $args the filter id - * @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values) - * @return \WP_Query|Array an instance of wp query OR array of entities; - */ - public function fetch($args = [], $output = null){ - if( is_numeric($args) ){ - $existing_post = get_post($args); - if ($existing_post instanceof \WP_Post) { - return new Entities\Filter($existing_post); - } else { - return []; - } - - } elseif (is_array($args)) { - // TODO: get filters from parent collections - $args = array_merge([ - 'posts_per_page' => -1, - 'post_status' => 'publish' - ], $args); - - $args = $this->parse_fetch_args($args); - - $args['post_type'] = Entities\Filter::get_post_type(); - - $args = apply_filters('tainacan_fetch_args', $args, 'filters'); + public function update( $object, $new_values = null ) { + return $this->insert( $object ); + } - $wp_query = new \WP_Query($args); - return $this->fetch_output($wp_query, $output); - } - } + /** + * fetch filter based on ID or WP_Query args + * + * Filters are stored as posts. Check WP_Query docs + * to learn all args accepted in the $args parameter (@see https://developer.wordpress.org/reference/classes/wp_query/) + * You can also use a mapped property, such as name and description, as an argument and it will be mapped to the + * appropriate WP_Query argument + * + * @param array $args WP_Query args || int $args the filter id + * @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values) + * + * @return \WP_Query|Array an instance of wp query OR array of entities; + */ + public function fetch( $args = [], $output = null ) { + if ( is_numeric( $args ) ) { + $existing_post = get_post( $args ); + if ( $existing_post instanceof \WP_Post ) { + return new Entities\Filter( $existing_post ); + } else { + return []; + } + + } elseif ( is_array( $args ) ) { + // TODO: get filters from parent collections + $args = array_merge( [ + 'posts_per_page' => - 1, + 'post_status' => 'publish' + ], $args ); + + $args = $this->parse_fetch_args( $args ); + + $args['post_type'] = Entities\Filter::get_post_type(); + + $args = apply_filters( 'tainacan_fetch_args', $args, 'filters' ); + + $wp_query = new \WP_Query( $args ); + + return $this->fetch_output( $wp_query, $output ); + } + } - /** - * register metadatum types class on array of types - * - * @param $class_name string | object The class name or the instance - */ - public function register_filter_type( $class_name ){ - if( is_object( $class_name ) ){ - $class_name = get_class( $class_name ); - } + /** + * register metadatum types class on array of types + * + * @param $class_name string | object The class name or the instance + */ + public function register_filter_type( $class_name ) { + if ( is_object( $class_name ) ) { + $class_name = get_class( $class_name ); + } - if(!in_array( $class_name, $this->filters_types)){ - $this->filters_types[] = $class_name; - } - } + if ( ! in_array( $class_name, $this->filters_types ) ) { + $this->filters_types[] = $class_name; + } + } - /** - * register metadatum types class on array of types - * - * @param $class_name string | object The class name or the instance - */ - public function deregister_filter_type( $class_name ){ - if (is_object($class_name)) { - $class_name = get_class($class_name); - } + /** + * register metadatum types class on array of types + * + * @param $class_name string | object The class name or the instance + */ + public function deregister_filter_type( $class_name ) { + if ( is_object( $class_name ) ) { + $class_name = get_class( $class_name ); + } - $key = array_search($class_name, $this->filters_types); - if ($key !== false) { - unset($this->filters_types[$key]); - } - } + $key = array_search( $class_name, $this->filters_types ); + if ( $key !== false ) { + unset( $this->filters_types[ $key ] ); + } + } - /** - * fetch all registered filter type classes - * - * Possible outputs are: - * CLASS (default) - returns the Class name of of filter types registered - * NAME - return an Array of the names of filter types registered - * - * @param $output string CLASS | NAME - * @return array of Entities\Filter_Types\Filter_Type classes path name - */ - public function fetch_filter_types( $output = 'CLASS'){ - $return = []; + /** + * fetch all registered filter type classes + * + * Possible outputs are: + * CLASS (default) - returns the Class name of of filter types registered + * NAME - return an Array of the names of filter types registered + * + * @param $output string CLASS | NAME + * + * @return array of Entities\Filter_Types\Filter_Type classes path name + */ + public function fetch_filter_types( $output = 'CLASS' ) { + $return = []; - do_action('register_filter_types'); + do_action( 'register_filter_types' ); - if( $output === 'NAME' ){ - foreach ($this->filters_types as $filter_type) { - $return[] = str_replace('Tainacan\Filter_Types\\','', $filter_type); - } + if ( $output === 'NAME' ) { + foreach ( $this->filters_types as $filter_type ) { + $return[] = str_replace( 'Tainacan\Filter_Types\\', '', $filter_type ); + } - return $return; - } + return $return; + } - return $this->filters_types; - } + return $this->filters_types; + } - /** - * fetch only supported filters for the type specified - * - * @param ( string || array ) $types Primitve types of metadatum ( float, string, int) - * @return array Filters supported by the primitive types passed in $types - */ - public function fetch_supported_filter_types($types){ - $filter_types = $this->fetch_filter_types(); - $supported_filter_types = []; + /** + * fetch only supported filters for the type specified + * + * @param ( string || array ) $types Primitve types of metadatum ( float, string, int) + * + * @return array Filters supported by the primitive types passed in $types + */ + public function fetch_supported_filter_types( $types ) { + $filter_types = $this->fetch_filter_types(); + $supported_filter_types = []; - foreach ( $filter_types as $filter_type){ - $filter = new $filter_type(); + foreach ( $filter_types as $filter_type ) { + $filter = new $filter_type(); - if( ( is_array( $types ) )){ - foreach ( $types as $single_type ) { - if( in_array( $single_type ,$filter->get_supported_types() )){ - $supported_filter_types[] = $filter; - } - } - }else if( in_array( $types ,$filter->get_supported_types() )){ - $supported_filter_types[] = $filter; - } - } + if ( ( is_array( $types ) ) ) { + foreach ( $types as $single_type ) { + if ( in_array( $single_type, $filter->get_supported_types() ) ) { + $supported_filter_types[] = $filter; + } + } + } else if ( in_array( $types, $filter->get_supported_types() ) ) { + $supported_filter_types[] = $filter; + } + } - return $supported_filter_types; - } + return $supported_filter_types; + } - /** - * fetch filters by collection, searches all filters available - * - * @param Entities\Collection $collection - * @param array $args WP_Query args plus disabled_metadata - * @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values) - * - * @return array Entities\Metadatum - * @throws \Exception - */ - public function fetch_by_collection(Entities\Collection $collection, $args = [], $output = null){ - $collection_id = $collection->get_id(); + /** + * fetch filters by collection, searches all filters available + * + * @param Entities\Collection $collection + * @param array $args WP_Query args plus disabled_metadata + * @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values) + * + * @return array Entities\Metadatum + * @throws \Exception + */ + 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 metadatum - //$parents[] = $this->get_default_metadata_attribute(); + //search for default metadatum + //$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; - }else{ - $args['meta_query'] = array( $meta_query ); - } + if ( isset( $args['meta_query'] ) ) { + $args['meta_query'][] = $meta_query; + } else { + $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, - * filters not ordinated appear on the end of the list - * - * - * @param $result Response from method fetch - * @param Entities\Collection $collection - * @return array or WP_Query ordinate - */ - public function order_result( $result, Entities\Collection $collection, $include_disabled = false ){ - $order = $collection->get_filters_order(); - if($order) { - $order = ( is_array($order) ) ? $order : unserialize($order); + /** + * Ordinate the result from fetch response if $collection has an ordination, + * filters not ordinated appear on the end of the list + * + * + * @param $result Response from method fetch + * @param Entities\Collection $collection + * + * @return array or WP_Query ordinate + */ + public function order_result( $result, Entities\Collection $collection, $include_disabled = false ) { + $order = $collection->get_filters_order(); + if ( $order ) { + $order = ( is_array( $order ) ) ? $order : unserialize( $order ); - if ( is_array($result) ){ - $result_ordinate = []; - $not_ordinate = []; + 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') ); + foreach ( $result as $item ) { + $id = $item->WP_Post->ID; + $index = array_search( $id, array_column( $order, 'id' ) ); - if( $index !== false ) { + if ( $index !== false ) { - // skipping metadata disabled if the arg is set - if ( ! $include_disabled && isset( $order[ $index ]['enabled'] ) && ! $order[ $index ]['enabled'] ) { - continue; - } + // skipping metadata disabled if the arg is set + if ( ! $include_disabled && isset( $order[ $index ]['enabled'] ) && ! $order[ $index ]['enabled'] ) { + continue; + } - $enable = ( isset( $order[ $index ]['enabled'] ) ) ? $order[ $index ]['enabled'] : true; - $item->set_enabled_for_collection( $enable ); + $enable = ( isset( $order[ $index ]['enabled'] ) ) ? $order[ $index ]['enabled'] : true; + $item->set_enabled_for_collection( $enable ); - $result_ordinate[$index] = $item; - } else { - $not_ordinate[] = $item; - } - } + $result_ordinate[ $index ] = $item; + } else { + $not_ordinate[] = $item; + } + } - ksort ( $result_ordinate ); - $result_ordinate = array_merge( $result_ordinate, $not_ordinate ); + 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 = []; + 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') ); + 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; - } - } + 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 ); + ksort( $result_ordinate ); + $result->posts = $result_ordinate; + $result->posts = array_merge( $result->posts, $not_ordinate ); - return $result; - } - } - return $result; - } + return $result; + } + } + + return $result; + } } \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-item-metadata.php b/src/classes/repositories/class-tainacan-item-metadata.php index aa98aacab..8984002e4 100644 --- a/src/classes/repositories/class-tainacan-item-metadata.php +++ b/src/classes/repositories/class-tainacan-item-metadata.php @@ -1,27 +1,28 @@ get_validated() ) { throw new \Exception( 'Entities must be validated before you can save them' ); // TODO: Throw Warning saying you must validate object before insert() } - $is_update = false; - $diffs = []; - if ( $item_metadata->get_id() ) { + $is_update = false; + $diffs = []; + if ( $item_metadata->get_id() ) { - if($item_metadata->get_status() === 'auto-draft') { - $is_update = false; - } else { - $is_update = true; - } + if ( $item_metadata->get_status() === 'auto-draft' ) { + $is_update = false; + } else { + $is_update = true; + } - $old = $item_metadata->get_repository()->fetch( $item_metadata->get_id() ); + $old = $item_metadata->get_repository()->fetch( $item_metadata->get_id() ); - $diffs = $this->diff($old, $item_metadata); - } - - $unique = !$item_metadata->is_multiple(); + $diffs = $this->diff( $old, $item_metadata ); + } + + $unique = ! $item_metadata->is_multiple(); $metadata_type = $item_metadata->get_metadatum()->get_metadata_type_object(); - - if ($metadata_type->get_core()) { - $this->save_core_metadatum_value($item_metadata); + + if ( $metadata_type->get_core() ) { + $this->save_core_metadatum_value( $item_metadata ); // Core metadata are also stored as regular metadata (in the code following below) // This is useful to create queries via filters, advanced search or APIs // if you can search for title and content with meta_query as if they were regular metadata - } - - if ($metadata_type->get_primitive_type() == 'term') { - $this->save_terms_metadatum_value($item_metadata); - } elseif ($metadata_type->get_primitive_type() == 'compound') { + } + + if ( $metadata_type->get_primitive_type() == 'term' ) { + $this->save_terms_metadatum_value( $item_metadata ); + } elseif ( $metadata_type->get_primitive_type() == 'compound' ) { // do nothing. Compound values are updated when its child metadata are updated return $item_metadata; } else { - if ($unique) { - - if (is_int($item_metadata->get_meta_id())) { + if ( $unique ) { + + if ( is_int( $item_metadata->get_meta_id() ) ) { update_metadata_by_mid( 'post', $item_metadata->get_meta_id(), wp_slash( $item_metadata->get_value() ) ); } else { - + /** * When we are adding a metadatum that is child of another, this means it is inside a compound metadatum * - * In that case, if the Item_Metadata object is not set with a meta_id, it means we want to create a new one + * In that case, if the Item_Metadata object is not set with a meta_id, it means we want to create a new one * and not update an existing. This is the case of a multiple compound metadatum. */ - if ( $item_metadata->get_metadatum()->get_parent() > 0 && is_null($item_metadata->get_meta_id()) ) { - $added_meta_id = add_post_meta($item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_id(), wp_slash( $item_metadata->get_value() ) ); - $added_compound = $this->add_compound_value($item_metadata, $added_meta_id); + if ( $item_metadata->get_metadatum()->get_parent() > 0 && is_null( $item_metadata->get_meta_id() ) ) { + $added_meta_id = add_post_meta( $item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_id(), wp_slash( $item_metadata->get_value() ) ); + $added_compound = $this->add_compound_value( $item_metadata, $added_meta_id ); } else { - update_post_meta($item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_id(), wp_slash( $item_metadata->get_value() ) ); + update_post_meta( $item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_id(), wp_slash( $item_metadata->get_value() ) ); } - + } - - } else { - delete_post_meta($item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_id()); - - if (is_array($item_metadata->get_value())){ - $values = $item_metadata->get_value(); - foreach ($values as $value){ - add_post_meta($item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_id(), wp_slash( $value )); - } - } - } + } else { + delete_post_meta( $item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_id() ); - do_action('tainacan-insert', $item_metadata, $diffs, $is_update); - do_action('tainacan-insert-Item_Metadata_Entity', $item_metadata); + if ( is_array( $item_metadata->get_value() ) ) { + $values = $item_metadata->get_value(); + + foreach ( $values as $value ) { + add_post_meta( $item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_id(), wp_slash( $value ) ); + } + } + } + + $this->logs_repository->insert_log( $item_metadata, $diffs, $is_update ); + + do_action( 'tainacan-insert', $item_metadata, $diffs, $is_update ); + do_action( 'tainacan-insert-Item_Metadata_Entity', $item_metadata ); } - $new_entity = new Entities\Item_Metadata_Entity($item_metadata->get_item(), $item_metadata->get_metadatum()); - - if (isset($added_compound) && is_int($added_compound)) { - $new_entity->set_parent_meta_id($added_compound); + $new_entity = new Entities\Item_Metadata_Entity( $item_metadata->get_item(), $item_metadata->get_metadatum() ); + + if ( isset( $added_compound ) && is_int( $added_compound ) ) { + $new_entity->set_parent_meta_id( $added_compound ); } - - if (isset($added_meta_id) && is_int($added_meta_id)) { - $new_entity->set_meta_id($added_meta_id); + + if ( isset( $added_meta_id ) && is_int( $added_meta_id ) ) { + $new_entity->set_meta_id( $added_meta_id ); } - - return $new_entity; - - } + + return $new_entity; + + } /** * @param $item_metadata_id * * @return mixed|void */ - public function delete($item_metadata_id){} + public function delete( $item_metadata_id ) { + } /** * @param $item_metadata_id * * @return mixed|void */ - public function trash($item_metadata_id){} + public function trash( $item_metadata_id ) { + } /** * @param Entities\Item_Metadata_Entity $item_metadata * * @throws \Exception */ - public function save_core_metadatum_value(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) { - $metadata_type = $item_metadata->get_metadatum()->get_metadata_type_object(); + public function save_core_metadatum_value( \Tainacan\Entities\Item_Metadata_Entity $item_metadata ) { + $metadata_type = $item_metadata->get_metadatum()->get_metadata_type_object(); - if ($metadata_type->get_core()) { - $item = $item_metadata->get_item(); - $set_method = 'set_' . $metadata_type->get_related_mapped_prop(); + if ( $metadata_type->get_core() ) { + $item = $item_metadata->get_item(); + $set_method = 'set_' . $metadata_type->get_related_mapped_prop(); - $value = $item_metadata->get_value(); - $item->$set_method( is_array( $value ) ? $value[0] : $value ); + $value = $item_metadata->get_value(); + $item->$set_method( is_array( $value ) ? $value[0] : $value ); - if ($item->validate_core_metadata()) { - $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); - $Tainacan_Items->insert($item); - } else { - throw new \Exception('Item metadata should be validated beforehand'); - } - } - } + if ( $item->validate_core_metadata() ) { + $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); + $Tainacan_Items->insert( $item ); + } else { + throw new \Exception( 'Item metadata should be validated beforehand' ); + } + } + } /** * @param $item_metadata * * @throws \Exception */ - public function save_terms_metadatum_value($item_metadata) { + public function save_terms_metadatum_value( $item_metadata ) { $metadata_type = $item_metadata->get_metadatum()->get_metadata_type_object(); - if ($metadata_type->get_primitive_type() == 'term') { + if ( $metadata_type->get_primitive_type() == 'term' ) { $new_terms = $item_metadata->get_value(); - $taxonomy = new Entities\Taxonomy( $metadata_type->get_option('taxonomy_id') ); + $taxonomy = new Entities\Taxonomy( $metadata_type->get_option( 'taxonomy_id' ) ); - if( $taxonomy ){ + if ( $taxonomy ) { $old = $item_metadata; - $success = wp_set_object_terms($item_metadata->get_item()->get_id(), $new_terms, $taxonomy->get_db_identifier() ); + $success = wp_set_object_terms( $item_metadata->get_item()->get_id(), $new_terms, $taxonomy->get_db_identifier() ); - if(!$success instanceof \WP_Error) { - $diffs = $this->diff($old, $item_metadata); + if ( ! $success instanceof \WP_Error ) { + $diffs = $this->diff( $old, $item_metadata ); - do_action( 'tainacan-insert', $item_metadata, $diffs, true ); - } - } + $this->logs_repository->insert_log( $item_metadata, $diffs, true ); + //do_action( 'tainacan-insert', $item_metadata, $diffs, true ); + } + } } } - + /** - * + * * @return null|ind the meta id of the created compound metadata */ - public function add_compound_value(Entities\Item_Metadata_Entity $item_metadata, $meta_id) { - + public function add_compound_value( Entities\Item_Metadata_Entity $item_metadata, $meta_id ) { + $current_value = get_metadata_by_mid( 'post', $item_metadata->get_parent_meta_id() ); - - if (is_object($current_value)) - $current_value = $current_value->meta_value; - - if ( !is_array($current_value) ) + + if ( is_object( $current_value ) ) { + $current_value = $current_value->meta_value; + } + + if ( ! is_array( $current_value ) ) { $current_value = []; - - if ( !in_array( $meta_id, $current_value ) ) { + } + + if ( ! in_array( $meta_id, $current_value ) ) { $current_value[] = $meta_id; } - + if ( $item_metadata->get_parent_meta_id() > 0 ) { update_metadata_by_mid( 'post', $item_metadata->get_parent_meta_id(), $current_value ); } elseif ( $item_metadata->get_metadatum()->get_parent() > 0 ) { return add_post_meta( $item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_parent(), $current_value ); } - - + + } /** @@ -217,31 +225,31 @@ class Item_Metadata extends Repository { * @return array * @throws \Exception */ - public function fetch($object, $output = null, $args = [] ){ - if($object instanceof Entities\Item){ - $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - - $collection = $object->get_collection(); - - if (!$collection instanceof Entities\Collection){ - return []; - } - - $meta_list = $Tainacan_Metadata->fetch_by_collection($collection, $args, '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, $args = [] ) { + if ( $object instanceof Entities\Item ) { + $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); + + $collection = $object->get_collection(); + + if ( ! $collection instanceof Entities\Collection ) { + return []; + } + + $meta_list = $Tainacan_Metadata->fetch_by_collection( $collection, $args, '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 metadatum. @@ -251,62 +259,64 @@ class Item_Metadata extends Repository { * @return mixed * @throws \Exception */ - public function get_value(Entities\Item_Metadata_Entity $item_metadata) { - $unique = ! $item_metadata->is_multiple(); - - $metadata_type = $item_metadata->get_metadatum()->get_metadata_type_object(); - if ($metadata_type->get_core()) { - $item = $item_metadata->get_item(); - - $get_method = 'get_' . $metadata_type->get_related_mapped_prop(); - return $item->$get_method(); - - } elseif ($metadata_type->get_primitive_type() == 'term') { + public function get_value( Entities\Item_Metadata_Entity $item_metadata ) { + $unique = ! $item_metadata->is_multiple(); - if( is_numeric( $metadata_type->get_option('taxonomy_id') ) ){ - $taxonomy = new Entities\Taxonomy( $metadata_type->get_option('taxonomy_id') ); - if( $taxonomy ){ - $taxonomy_slug = $taxonomy->get_db_identifier(); - } else { - return null; - } - } else { - return null; - } + $metadata_type = $item_metadata->get_metadatum()->get_metadata_type_object(); + if ( $metadata_type->get_core() ) { + $item = $item_metadata->get_item(); - $terms = wp_get_object_terms($item_metadata->get_item()->get_id(), $taxonomy_slug ); - - if ($unique) { - $terms = reset($terms); - - if (false !== $terms) - $terms = new Entities\Term($terms); + $get_method = 'get_' . $metadata_type->get_related_mapped_prop(); + + return $item->$get_method(); + + } elseif ( $metadata_type->get_primitive_type() == 'term' ) { + + if ( is_numeric( $metadata_type->get_option( 'taxonomy_id' ) ) ) { + $taxonomy = new Entities\Taxonomy( $metadata_type->get_option( 'taxonomy_id' ) ); + if ( $taxonomy ) { + $taxonomy_slug = $taxonomy->get_db_identifier(); + } else { + return null; + } + } else { + return null; } - if(is_array($terms)){ + $terms = wp_get_object_terms( $item_metadata->get_item()->get_id(), $taxonomy_slug ); + + if ( $unique ) { + $terms = reset( $terms ); + + if ( false !== $terms ) { + $terms = new Entities\Term( $terms ); + } + } + + if ( is_array( $terms ) ) { $terms_array = []; - foreach ($terms as $term){ - $terms_array[] = new Entities\Term($term); + foreach ( $terms as $term ) { + $terms_array[] = new Entities\Term( $term ); } return $terms_array; } - + return $terms; - - } elseif ($metadata_type->get_primitive_type() == 'compound') { - + + } elseif ( $metadata_type->get_primitive_type() == 'compound' ) { + global $wpdb; - $rows = $wpdb->get_results( - $wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_id()), + $rows = $wpdb->get_results( + $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_id() ), ARRAY_A ); - + $return_value = []; - - if (is_array($rows)) { - - foreach ($rows as $row) { - $value = $this->extract_compound_value(maybe_unserialize($row['meta_value']), $item_metadata->get_item(), $row['meta_id']); + + if ( is_array( $rows ) ) { + + foreach ( $rows as $row ) { + $value = $this->extract_compound_value( maybe_unserialize( $row['meta_value'] ), $item_metadata->get_item(), $row['meta_id'] ); if ( $unique ) { $return_value = $value; break; @@ -314,24 +324,24 @@ class Item_Metadata extends Repository { $return_value[] = $value; } } - + } - - return $return_value; - - } else { - if (is_int($item_metadata->get_meta_id())) { + + return $return_value; + + } else { + if ( is_int( $item_metadata->get_meta_id() ) ) { $value = get_metadata_by_mid( 'post', $item_metadata->get_meta_id() ); - if ( is_object($value) && isset( $value->meta_value) ) { + if ( is_object( $value ) && isset( $value->meta_value ) ) { return $value->meta_value; } } else { - return get_post_meta($item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_id(), $unique); + return get_post_meta( $item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_id(), $unique ); } - - } - - } + + } + + } /** * Transforms the array saved as meta_value with the IDs of post_meta saved as a value for compound metadata @@ -339,50 +349,60 @@ class Item_Metadata extends Repository { * * @param array $ids The array of post_meta ids * @param Entities\Item $item The item this post_meta is related to - * @param int $compund_meta_id the meta_id of the parent compound metadata + * @param $compound_meta_id * * @return array An array of Item_Metadata_Entity objects * @throws \Exception */ - private function extract_compound_value(array $ids, Entities\Item $item, $compund_meta_id) { - + private function extract_compound_value( array $ids, Entities\Item $item, $compound_meta_id ) { + $return_value = []; - - if (is_array($ids)) { - foreach ($ids as $id) { + + if ( is_array( $ids ) ) { + foreach ( $ids as $id ) { $post_meta_object = get_metadata_by_mid( 'post', $id ); - if ( is_object($post_meta_object) ) { - $metadatum = new Entities\Metadatum($post_meta_object->meta_key); - $return_value[$metadatum->get_id()] = new Entities\Item_Metadata_Entity( $item, $metadatum, $id, $compund_meta_id ); + if ( is_object( $post_meta_object ) ) { + $metadatum = new Entities\Metadatum( $post_meta_object->meta_key ); + $return_value[ $metadatum->get_id() ] = new Entities\Item_Metadata_Entity( $item, $metadatum, $id, $compound_meta_id ); } - + } } - + return $return_value; - + } - public function register_post_type() { } - - public function get_map() { return []; } - public function get_default_properties($map) { return []; } + public function register_post_type() { + } + + public function get_map() { + return []; + } + + public function get_default_properties( $map ) { + return []; + } /** * @param $object * + * @param null $new_values + * * @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 Metadatum 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 Metadatum 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 fca764e80..ef18d3c03 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -11,101 +11,101 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); class Items extends Repository { public $entities_type = '\Tainacan\Entities\Item'; - private static $instance = null; + private static $instance = null; - public static function get_instance() { - if(!isset(self::$instance)) { - self::$instance = new self(); - } + public static function get_instance() { + if ( ! isset( self::$instance ) ) { + self::$instance = new self(); + } - return self::$instance; - } + return self::$instance; + } - protected function __construct() { - parent::__construct(); - add_filter( 'posts_where', array(&$this, 'title_in_posts_where'), 10, 2 ); - add_filter( 'posts_where', array(&$this, 'content_in_posts_where'), 10, 2 ); - add_action( 'tainacan-api-item-updated', array(&$this, 'hook_api_updated_item'), 10, 2 ); - } + protected function __construct() { + parent::__construct(); + add_filter( 'posts_where', array( &$this, 'title_in_posts_where' ), 10, 2 ); + add_filter( 'posts_where', array( &$this, 'content_in_posts_where' ), 10, 2 ); + add_action( 'tainacan-api-item-updated', array( &$this, 'hook_api_updated_item' ), 10, 2 ); + } - public function get_map() { - return apply_filters( 'tainacan-get-map-' . $this->get_name(), [ - 'title' => [ - 'map' => 'post_title', - 'title' => __( 'Title', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'Title of the item', 'tainacan' ), - 'on_error' => __( 'The title should be a text value and not empty', 'tainacan' ), - //'validation' => v::stringType()->notEmpty(), - ], - 'status' => [ - 'map' => 'post_status', - 'title' => __( 'Status', 'tainacan' ), - 'type' => 'string', - 'default' => 'draft', - 'description' => __( 'The posts status', 'tainacan' ) - ], - 'description' => [ - 'map' => 'post_content', - 'title' => __( 'Description', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'The item description', 'tainacan' ), - 'default' => '', - 'validation' => '' - ], - 'collection_id' => [ - 'map' => 'meta', - 'title' => __( 'Collection', 'tainacan' ), - 'type' => 'integer', - 'description' => __( 'The collection ID', 'tainacan' ), - 'validation' => '' - ], - 'author_id' => [ - 'map' => 'post_author', - 'title' => __( 'Author', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'The item author\'s user ID (numeric string)', 'tainacan' ) - ], - 'creation_date' => [ - 'map' => 'post_date', - 'title' => __( 'Creation Date', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'The item creation date', 'tainacan' ) - ], - 'modification_date' => [ - 'map' => 'post_modified', - 'title' => __( 'Modification Date', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'The item modification date', 'tainacan' ) - ], - 'terms' => [ - 'map' => 'terms', - 'title' => __( 'Term IDs', 'tainacan' ), - 'type' => 'array', - 'description' => __( 'The item term IDs', 'tainacan' ), - ], - 'document_type' => [ - 'map' => 'meta', - 'title' => __( 'Document Type', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'The document type, can be a local attachment, an external URL or a text', 'tainacan' ), - 'on_error' => __( 'Invalid document type', 'tainacan' ), - 'validation' => v::stringType()->in( [ 'attachment', 'url', 'text', 'empty'] ), - 'default' => 'empty' - ], - 'document' => [ - 'map' => 'meta', - 'title' => __( 'Document', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'The document itself. An ID in case of attachment, an URL in case of link or a text in the case of text.', 'tainacan' ), - 'on_error' => __( 'Invalid document', 'tainacan' ), - 'default' => '' - ], - '_thumbnail_id' => [ - 'map' => 'meta', - 'title' => __( 'Thumbnail', 'tainacan' ), - 'description' => __( 'Squared reduced-size version of a picture that helps recognizing and organizing files', 'tainacan' ) - ] + public function get_map() { + return apply_filters( 'tainacan-get-map-' . $this->get_name(), [ + 'title' => [ + 'map' => 'post_title', + 'title' => __( 'Title', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'Title of the item', 'tainacan' ), + 'on_error' => __( 'The title should be a text value and not empty', 'tainacan' ), + //'validation' => v::stringType()->notEmpty(), + ], + 'status' => [ + 'map' => 'post_status', + 'title' => __( 'Status', 'tainacan' ), + 'type' => 'string', + 'default' => 'draft', + 'description' => __( 'The posts status', 'tainacan' ) + ], + 'description' => [ + 'map' => 'post_content', + 'title' => __( 'Description', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The item description', 'tainacan' ), + 'default' => '', + 'validation' => '' + ], + 'collection_id' => [ + 'map' => 'meta', + 'title' => __( 'Collection', 'tainacan' ), + 'type' => 'integer', + 'description' => __( 'The collection ID', 'tainacan' ), + 'validation' => '' + ], + 'author_id' => [ + 'map' => 'post_author', + 'title' => __( 'Author', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The item author\'s user ID (numeric string)', 'tainacan' ) + ], + 'creation_date' => [ + 'map' => 'post_date', + 'title' => __( 'Creation Date', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The item creation date', 'tainacan' ) + ], + 'modification_date' => [ + 'map' => 'post_modified', + 'title' => __( 'Modification Date', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The item modification date', 'tainacan' ) + ], + 'terms' => [ + 'map' => 'terms', + 'title' => __( 'Term IDs', 'tainacan' ), + 'type' => 'array', + 'description' => __( 'The item term IDs', 'tainacan' ), + ], + 'document_type' => [ + 'map' => 'meta', + 'title' => __( 'Document Type', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The document type, can be a local attachment, an external URL or a text', 'tainacan' ), + 'on_error' => __( 'Invalid document type', 'tainacan' ), + 'validation' => v::stringType()->in( [ 'attachment', 'url', 'text', 'empty' ] ), + 'default' => 'empty' + ], + 'document' => [ + 'map' => 'meta', + 'title' => __( 'Document', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The document itself. An ID in case of attachment, an URL in case of link or a text in the case of text.', 'tainacan' ), + 'on_error' => __( 'Invalid document', 'tainacan' ), + 'default' => '' + ], + '_thumbnail_id' => [ + 'map' => 'meta', + 'title' => __( 'Thumbnail', 'tainacan' ), + 'description' => __( 'Squared reduced-size version of a picture that helps recognizing and organizing files', 'tainacan' ) + ] ] ); } @@ -140,11 +140,18 @@ class Items extends Repository { */ public function register_post_type() { - $Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance(); - $Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance(); + $Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance(); + $Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance(); $collections = $Tainacan_Collections->fetch( [], 'OBJECT' ); - $taxonomies = $Tainacan_Taxonomies->fetch( ['status' => ['auto-draft', 'draft', 'publish', 'private']], 'OBJECT' ); + $taxonomies = $Tainacan_Taxonomies->fetch( [ + 'status' => [ + 'auto-draft', + 'draft', + 'publish', + 'private' + ] + ], 'OBJECT' ); if ( ! is_array( $collections ) ) { return; @@ -164,7 +171,7 @@ class Items extends Repository { } public function insert( $item ) { - return parent::insert($item); + return parent::insert( $item ); } /** @@ -252,13 +259,13 @@ class Items extends Repository { $args['post_type'] = $cpt; - $args = apply_filters('tainacan_fetch_args', $args, 'items'); + $args = apply_filters( 'tainacan_fetch_args', $args, 'items' ); $wp_query = new \WP_Query( $args ); return $this->fetch_output( $wp_query, $output ); } - + /** * fetch items IDs based on WP_Query args * @@ -275,9 +282,9 @@ class Items extends Repository { * @return Array array of IDs; */ public function fetch_ids( $args = [], $collections = [] ) { - + $args['fields'] = 'ids'; - + return $this->fetch( $args, $collections )->get_posts(); } @@ -294,7 +301,9 @@ class Items extends Repository { $deleted = new Entities\Item( wp_delete_post( $item_id, true ) ); if ( $deleted ) { - do_action( 'tainacan-deleted', $deleted, false, true ); + $this->logs_repository->insert_log( $deleted, [], false, true ); + + do_action( 'tainacan-deleted', $deleted ); } return $deleted; @@ -305,147 +314,157 @@ class Items extends Repository { * * @return mixed|Item */ - public function trash($item_id){ + public function trash( $item_id ) { $trashed = new Entities\Item( wp_trash_post( $item_id ) ); - if($trashed) { - do_action( 'tainacan-trashed', $trashed, [], false, false, true ); + if ( $trashed ) { + $this->logs_repository->insert_log( $trashed, [], false, false, true ); + + do_action( 'tainacan-trashed', $trashed ); } return $trashed; } - /** - * allow wp query filter post by array of titles - * - * @param $where - * @param $wp_query - * @return string - */ - public function title_in_posts_where( $where, $wp_query ) { - global $wpdb; - if ( $post_title_in = $wp_query->get( 'post_title_in' ) ) { - if(is_array( $post_title_in ) && isset( $post_title_in['value']) ){ - $quotes = []; - foreach ($post_title_in['value'] as $title) { - $quotes[] = " $wpdb->posts.post_title LIKE '%" . esc_sql( $wpdb->esc_like( $title ) ). "%'"; - } - } + /** + * allow wp query filter post by array of titles + * + * @param $where + * @param $wp_query + * + * @return string + */ + public function title_in_posts_where( $where, $wp_query ) { + global $wpdb; + if ( $post_title_in = $wp_query->get( 'post_title_in' ) ) { + if ( is_array( $post_title_in ) && isset( $post_title_in['value'] ) ) { + $quotes = []; + foreach ( $post_title_in['value'] as $title ) { + $quotes[] = " $wpdb->posts.post_title LIKE '%" . esc_sql( $wpdb->esc_like( $title ) ) . "%'"; + } + } - // retrieve only posts for the specified collection and status - $type = " $wpdb->posts.post_type = '" . $wp_query->get( 'post_type' )[0]."' "; - $status = " ( $wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private') "; - $where .= ' '.$post_title_in['relation'] . '( ( ' .implode(' OR ', $quotes ) . ' ) AND ' . - $status . ' AND ' . $type . ' )'; - } - return $where; - } + // retrieve only posts for the specified collection and status + $type = " $wpdb->posts.post_type = '" . $wp_query->get( 'post_type' )[0] . "' "; + $status = " ( $wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private') "; + $where .= ' ' . $post_title_in['relation'] . '( ( ' . implode( ' OR ', $quotes ) . ' ) AND ' . + $status . ' AND ' . $type . ' )'; + } - /** - * allow wp query filter post by array of content - * - * @param $where - * @param $wp_query - * @return string - */ - public function content_in_posts_where( $where, $wp_query ) { - global $wpdb; - if ( $post_content_in = $wp_query->get( 'post_content_in' ) ) { - if(is_array( $post_content_in ) && isset( $post_content_in['value']) ){ - $quotes = []; - foreach ($post_content_in['value'] as $title) { - $quotes[] = " $wpdb->posts.post_content LIKE '%" .esc_sql( $wpdb->esc_like( $title ) ). "%'"; - } - } + return $where; + } + + /** + * allow wp query filter post by array of content + * + * @param $where + * @param $wp_query + * + * @return string + */ + public function content_in_posts_where( $where, $wp_query ) { + global $wpdb; + if ( $post_content_in = $wp_query->get( 'post_content_in' ) ) { + if ( is_array( $post_content_in ) && isset( $post_content_in['value'] ) ) { + $quotes = []; + foreach ( $post_content_in['value'] as $title ) { + $quotes[] = " $wpdb->posts.post_content LIKE '%" . esc_sql( $wpdb->esc_like( $title ) ) . "%'"; + } + } + + // retrieve only posts for the specified collection and status + $type = " $wpdb->posts.post_type = '" . $wp_query->get( 'post_type' )[0] . "' "; + $status = " ( $wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private') "; + $where .= ' ' . $post_content_in['relation'] . '( ( ' . implode( ' OR ', $quotes ) . ' ) AND ' . + $status . ' AND ' . $type . ' )'; + } + + return $where; + } - // retrieve only posts for the specified collection and status - $type = " $wpdb->posts.post_type = '" . $wp_query->get( 'post_type' )[0]."' "; - $status = " ( $wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private') "; - $where .= ' '.$post_content_in['relation'] . '( ( ' .implode(' OR ', $quotes ) . ' ) AND ' . - $status . ' AND ' . $type . ' )'; - } - return $where; - } - /** * Get a default thumbnail ID from the item document. - * + * * @param EntitiesItem $item The item + * * @return int|null The thumbnail ID or null if it was not possible to find a thumbnail */ - public function get_thumbnail_id_from_document(Entities\Item $item) { + public function get_thumbnail_id_from_document( Entities\Item $item ) { /** * Hook to get thumbnail from document */ - $thumb_id = apply_filters('tainacan-get-thumbnail-id-from-document', null, $item); - - if (!is_null($thumb_id)) { + $thumb_id = apply_filters( 'tainacan-get-thumbnail-id-from-document', null, $item ); + + if ( ! is_null( $thumb_id ) ) { return $thumb_id; } - - if ( empty($item->get_document()) ) { + + if ( empty( $item->get_document() ) ) { return null; } - + if ( $item->get_document_type() == 'attachment' ) { if ( wp_attachment_is_image( $item->get_document() ) ) { return $item->get_document(); } else { - - $filepath = get_attached_file($item->get_document()); + + $filepath = get_attached_file( $item->get_document() ); $TainacanMedia = \Tainacan\Media::get_instance(); - $thumb_blob = $TainacanMedia->get_pdf_cover($filepath); + $thumb_blob = $TainacanMedia->get_pdf_cover( $filepath ); if ( $thumb_blob ) { - $thumb_id = $TainacanMedia->insert_attachment_from_blob($thumb_blob, basename($filepath) . '-cover.jpg', $item->get_id()); + $thumb_id = $TainacanMedia->insert_attachment_from_blob( $thumb_blob, basename( $filepath ) . '-cover.jpg', $item->get_id() ); + return $thumb_id; } - + } } elseif ( $item->get_document_type() == 'url' ) { - + $TainacanEmbed = \Tainacan\Embed::get_instance(); if ( $thumb_url = $TainacanEmbed->oembed_get_thumbnail( $item->get_document() ) ) { $meta_key = '_' . $thumb_url . '__thumb'; - - $existing_thumb = get_post_meta($item->get_id(), $meta_key, true); - + + $existing_thumb = get_post_meta( $item->get_id(), $meta_key, true ); + if ( is_numeric( $existing_thumb ) ) { return $existing_thumb; } else { $TainacanMedia = \Tainacan\Media::get_instance(); - $thumb_id = $TainacanMedia->insert_attachment_from_url($thumb_url, $item->get_id()); - update_post_meta($item->get_id(), $meta_key, $thumb_id); + $thumb_id = $TainacanMedia->insert_attachment_from_url( $thumb_url, $item->get_id() ); + update_post_meta( $item->get_id(), $meta_key, $thumb_id ); + return $thumb_id; } } - + } - + return $thumb_id; } - + /** * When updating an item document, set a default thumbnail to the item if it does not have one yet - * - * @param Entities\Item $updated_item - * @param array $attributes The paramaters sent to the API + * + * @param Entities\Item $updated_item + * @param array $attributes The paramaters sent to the API + * * @return void */ - public function hook_api_updated_item(Entities\Item $updated_item, $attributes) { - if ( array_key_exists('document', $attributes) - && empty($updated_item->get__thumbnail_id()) - && !empty($updated_item->get_document()) - ) { - - $thumb_id = $this->get_thumbnail_id_from_document($updated_item); - - if (!is_null($thumb_id)) { + public function hook_api_updated_item( Entities\Item $updated_item, $attributes ) { + if ( array_key_exists( 'document', $attributes ) + && empty( $updated_item->get__thumbnail_id() ) + && ! empty( $updated_item->get_document() ) + ) { + + $thumb_id = $this->get_thumbnail_id_from_document( $updated_item ); + + if ( ! is_null( $thumb_id ) ) { set_post_thumbnail( $updated_item->get_id(), (int) $thumb_id ); } - + } - + } - - + + } \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-logs.php b/src/classes/repositories/class-tainacan-logs.php index 2350f6823..1a30503b7 100644 --- a/src/classes/repositories/class-tainacan-logs.php +++ b/src/classes/repositories/class-tainacan-logs.php @@ -28,9 +28,6 @@ class Logs extends Repository { protected function __construct() { parent::__construct(); - add_action( 'tainacan-insert', array( $this, 'insert_log' ), 10, 5 ); - add_action( 'tainacan-deleted', array( $this, 'insert_log'), 10, 5 ); - add_action( 'tainacan-trashed', array( $this, 'insert_log'), 10, 5 ); add_action( 'add_attachment', array( $this, 'prepare_attachment_log_before_insert' ), 10 ); } @@ -193,14 +190,14 @@ class Logs extends Repository { } elseif ( is_array( $args ) ) { $args = array_merge( [ - 'posts_per_page' => -1, + 'posts_per_page' => - 1, ], $args ); $args = $this->parse_fetch_args( $args ); $args['post_type'] = Entities\Log::get_post_type(); - $args = apply_filters('tainacan_fetch_args', $args, 'logs'); + $args = apply_filters( 'tainacan_fetch_args', $args, 'logs' ); $wp_query = new \WP_Query( $args ); @@ -208,11 +205,14 @@ class Logs extends Repository { } } - public function delete( $object ){} - public function trash( $object ){} + public function delete( $object ) { + } + + public function trash( $object ) { + } public function update( $object, $new_values = null ) { - return $this->insert($object); + return $this->insert( $object ); } public function fetch_last() { @@ -238,14 +238,14 @@ class Logs extends Repository { $tainacan_post = Repository::get_entity_by_post( $post ); - if($tainacan_post) { + if ( $tainacan_post ) { // was added a normal attachment // get all attachments except the new $old_attachments = $tainacan_post->get_attachments( $post_ID ); foreach ( $old_attachments as $index => $a ) { - unset( $old_attachments[$index]['id'] ); + unset( $old_attachments[ $index ]['id'] ); } $new_attachments[] = [ @@ -312,19 +312,19 @@ class Logs extends Repository { } if ( $is_update ) { - $msn = $this->prepare_event_message($new_value, $name, $class_name, 'updated'); + $msn = $this->prepare_event_message( $new_value, $name, $class_name, 'updated' ); $description = $msn; - } elseif( $is_delete ){ + } elseif ( $is_delete ) { // was deleted - $msn = $this->prepare_event_message($new_value, $name, $class_name, 'deleted'); + $msn = $this->prepare_event_message( $new_value, $name, $class_name, 'deleted' ); $description = $msn; - } elseif( !empty($diffs) ) { + } elseif ( ! empty( $diffs ) ) { // was created - $msn = $this->prepare_event_message($new_value, $name, $class_name, 'created'); + $msn = $this->prepare_event_message( $new_value, $name, $class_name, 'created' ); $description = $msn; - } elseif( $is_trash ) { + } elseif ( $is_trash ) { // was trashed - $msn = $this->prepare_event_message($new_value, $name, $class_name, 'trashed'); + $msn = $this->prepare_event_message( $new_value, $name, $class_name, 'trashed' ); $description = $msn; } @@ -363,12 +363,12 @@ class Logs extends Repository { * * @return string */ - private function prepare_event_message($object, $name, $class_name, $action_message){ - if ( $object instanceof Entities\Metadatum || $object instanceof Entities\Item || $object instanceof Entities\Filter) { + private function prepare_event_message( $object, $name, $class_name, $action_message ) { + if ( $object instanceof Entities\Metadatum || $object instanceof Entities\Item || $object instanceof Entities\Filter ) { $collection = $object->get_collection(); if ( $collection ) { - $parent = '(parent '. $collection->get_name() .')'; + $parent = '(parent: ' . $collection->get_name() . ')'; } else { $parent = '(on repository level)'; } @@ -394,11 +394,11 @@ class Logs extends Repository { /** @var Entity $value * */ $value = $log->get_value(); - $value->set_status('publish'); // TODO check if publish the entity on approve + $value->set_status( 'publish' ); // TODO check if publish the entity on approve $repository = self::get_repository( $value ); - if($value->validate()) { + if ( $value->validate() ) { return $repository->insert( $value ); } } diff --git a/src/classes/repositories/class-tainacan-metadata.php b/src/classes/repositories/class-tainacan-metadata.php index 9fc6192d1..69f31cbba 100644 --- a/src/classes/repositories/class-tainacan-metadata.php +++ b/src/classes/repositories/class-tainacan-metadata.php @@ -1,11 +1,13 @@ get_name(), [ + 'name' => [ + 'map' => 'post_title', + 'title' => __( 'Name', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'Name of the metadata', '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 metadata', 'tainacan' ), + //'validation' => v::stringType(), + ], + 'order' => [ + 'map' => 'menu_order', + 'title' => __( 'Order', 'tainacan' ), + 'type' => 'string/integer', + 'description' => __( 'Metadata order. This metadata will be used if collections were manually ordered.', 'tainacan' ), + 'on_error' => __( 'The menu order should be a numeric value', 'tainacan' ), + //'validation' => v::numeric(), + ], + 'parent' => [ + 'map' => 'post_parent', + 'title' => __( 'Parent', 'tainacan' ), + 'type' => 'integer', + 'description' => __( 'Parent metadata', '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 metadata description', 'tainacan' ), + 'default' => '', + //'on_error' => __('The description should be a text value', 'tainacan'), + //'validation' => v::stringType()->notEmpty(), + ], + 'metadata_type' => [ + 'map' => 'meta', + 'title' => __( 'Type', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The metadata type', 'tainacan' ), + 'on_error' => __( 'Metadata type is empty', 'tainacan' ), + 'validation' => v::stringType()->notEmpty(), + ], + 'required' => [ + 'map' => 'meta', + 'title' => __( 'Required', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The metadata is required', 'tainacan' ), + 'on_error' => __( 'The metadata content 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' => __( 'Metadata 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 values for the metadata', 'tainacan' ), + 'on_error' => __( 'Invalid multiple metadata', '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 metadata', 'tainacan' ), + 'on_error' => __( 'This number of multiples metadata is not allowed', 'tainacan' ), + 'validation' => v::numeric()->positive(), + 'default' => 1 + ], + 'mask' => [ + 'map' => 'meta', + 'title' => __( 'Mask', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The mask to be used in the metadata', 'tainacan' ), + //'on_error' => __('Mask is invalid', 'tainacan'), + //'validation' => '' + ], + 'default_value' => [ + 'map' => 'meta', + 'title' => __( 'Default value', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The default value for the metadata', 'tainacan' ), + ], + 'metadata_type_options' => [ // not showed in form + 'map' => 'meta', + 'title' => __( 'Metadata type options', 'tainacan' ), + 'type' => 'array/object/string', + 'items' => [ 'type' => 'array/string/integer/object' ], + 'description' => __( 'Specific options for metadata 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' => __( 'Metadata Value Accepts Suggestions', 'tainacan' ), + 'type' => 'bool', + 'description' => __( 'Allow community to suggest different values for the metadata', 'tainacan' ), + 'default' => false, + 'validation' => v::boolType() + ], + 'exposer_mapping' => [ + 'map' => 'meta', + 'title' => __( 'Relationship metadata mapping', 'tainacan' ), + 'type' => 'array/object/string', + 'items' => [ 'type' => 'array/string/integer/object' ], + 'description' => __( 'The metadata mapping options. Metadata can be configured to match another type of data distribution.', 'tainacan' ), + 'on_error' => __( 'Invalid Metadata Mapping', 'tainacan' ), + //'validation' => v::arrayType(), + 'default' => [] + ], + 'display' => [ + 'map' => 'meta', + 'title' => __( 'Display', 'tainacan' ), + 'type' => __( 'string' ), + 'validation' => v::stringType()->in( [ 'yes', 'no', 'never' ] ), + 'description' => __( 'Display by default on listing or do not display or never display.', 'tainacan' ), + 'default' => 'yes' + ], + 'semantic_uri' => [ + 'map' => 'meta', + 'title' => __( 'The semantic metadatum description URI' ), + 'type' => __( 'url' ), + 'validation' => v::optional( v::url() ), + 'description' => __( 'The semantic metadatum description URI like: ', 'tainacan' ) . 'https://schema.org/URL', + 'default' => '' + ] + ] ); + } - } - - /** - * {@inheritDoc} - * @see \Tainacan\Repositories\Repository::get_map() - */ - 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 metadata', '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 metadata', 'tainacan' ), - //'validation' => v::stringType(), - ], - 'order' => [ - 'map' => 'menu_order', - 'title' => __( 'Order', 'tainacan' ), - 'type' => 'string/integer', - 'description' => __( 'Metadata order. This metadata will be used if collections were manually ordered.', 'tainacan' ), - 'on_error' => __( 'The menu order should be a numeric value', 'tainacan' ), - //'validation' => v::numeric(), - ], - 'parent' => [ - 'map' => 'post_parent', - 'title' => __( 'Parent', 'tainacan' ), - 'type' => 'integer', - 'description' => __( 'Parent metadata', '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 metadata description', 'tainacan' ), - 'default' => '', - //'on_error' => __('The description should be a text value', 'tainacan'), - //'validation' => v::stringType()->notEmpty(), - ], - 'metadata_type' => [ - 'map' => 'meta', - 'title' => __( 'Type', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'The metadata type', 'tainacan' ), - 'on_error' => __( 'Metadata type is empty', 'tainacan' ), - 'validation' => v::stringType()->notEmpty(), - ], - 'required' => [ - 'map' => 'meta', - 'title' => __( 'Required', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'The metadata is required', 'tainacan' ), - 'on_error' => __( 'The metadata content 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' => __( 'Metadata 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 values for the metadata', 'tainacan' ), - 'on_error' => __( 'Invalid multiple metadata', '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 metadata', 'tainacan' ), - 'on_error' => __( 'This number of multiples metadata is not allowed', 'tainacan' ), - 'validation' => v::numeric()->positive(), - 'default' => 1 - ], - 'mask' => [ - 'map' => 'meta', - 'title' => __( 'Mask', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'The mask to be used in the metadata', 'tainacan' ), - //'on_error' => __('Mask is invalid', 'tainacan'), - //'validation' => '' - ], - 'default_value' => [ - 'map' => 'meta', - 'title' => __( 'Default value', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'The default value for the metadata', 'tainacan' ), - ], - 'metadata_type_options' => [ // not showed in form - 'map' => 'meta', - 'title' => __( 'Metadata type options', 'tainacan' ), - 'type' => 'array/object/string', - 'items' => [ 'type' => 'array/string/integer/object' ], - 'description' => __( 'Specific options for metadata 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' => __( 'Metadata Value Accepts Suggestions', 'tainacan' ), - 'type' => 'bool', - 'description' => __( 'Allow community to suggest different values for the metadata', 'tainacan' ), - 'default' => false, - 'validation' => v::boolType() - ], - 'exposer_mapping' => [ - 'map' => 'meta', - 'title' => __( 'Relationship metadata mapping', 'tainacan' ), - 'type' => 'array/object/string', - 'items' => [ 'type' => 'array/string/integer/object' ], - 'description' => __( 'The metadata mapping options. Metadata can be configured to match another type of data distribution.', 'tainacan' ), - 'on_error' => __( 'Invalid Metadata Mapping', 'tainacan' ), - //'validation' => v::arrayType(), - 'default' => [] - ], - 'display' => [ - 'map' => 'meta', - 'title' => __( 'Display', 'tainacan' ), - 'type' => __( 'string' ), - 'validation' => v::stringType()->in( [ 'yes', 'no', 'never' ] ), - 'description' => __( 'Display by default on listing or do not display or never display.', 'tainacan' ), - 'default' => 'yes' - ], - 'semantic_uri' => [ - 'map' => 'meta', - 'title' => __( 'The semantic metadatum description URI' ), - 'type' => __( 'url' ), - 'validation' => v::optional(v::url()), - 'description' => __( 'The semantic metadatum description URI like: ', 'tainacan' ).'https://schema.org/URL', - 'default' => '' - ] - ] ); - } - /** * 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' => __('Metadata', 'tainacan'), - 'singular_name' => __('Metadata', 'tainacan'), - 'add_new' => __('Add new', 'tainacan'), - 'add_new_item' => __('Add new Metadata', 'tainacan'), - 'edit_item' => __('Edit Metadata', 'tainacan'), - 'new_item' => __('New Metadata', 'tainacan'), - 'view_item' => __('View Metadata', 'tainacan'), - 'search_items' => __('Search Metadata', 'tainacan'), - 'not_found' => __('No Metadata found ', 'tainacan'), - 'not_found_in_trash' => __('No Metadata found in trash', 'tainacan'), - 'parent_item_colon' => __('Parent Metadata:', 'tainacan'), - 'menu_name' => __('Metadata', 'tainacan') - ); + 'name' => __( 'Metadata', 'tainacan' ), + 'singular_name' => __( 'Metadata', 'tainacan' ), + 'add_new' => __( 'Add new', 'tainacan' ), + 'add_new_item' => __( 'Add new Metadata', 'tainacan' ), + 'edit_item' => __( 'Edit Metadata', 'tainacan' ), + 'new_item' => __( 'New Metadata', 'tainacan' ), + 'view_item' => __( 'View Metadata', 'tainacan' ), + 'search_items' => __( 'Search Metadata', 'tainacan' ), + 'not_found' => __( 'No Metadata found ', 'tainacan' ), + 'not_found_in_trash' => __( 'No Metadata found in trash', 'tainacan' ), + 'parent_item_colon' => __( 'Parent Metadata:', 'tainacan' ), + 'menu_name' => __( 'Metadata', '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\Metadatum::get_capability_type(), - 'supports' => [ - 'title', - 'editor', - 'page-attributes' - ] - ); - register_post_type(Entities\Metadatum::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\Metadatum::get_capability_type(), + 'supports' => [ + 'title', + 'editor', + 'page-attributes' + ] + ); + register_post_type( Entities\Metadatum::get_post_type(), $args ); + } - /** - * constant used in default metadatum in attribute collection_id - * - * @return string the value of constant - */ - public function get_default_metadata_attribute(){ - return $this->default_metadata; - } + /** + * constant used in default metadatum in attribute collection_id + * + * @return string the value of constant + */ + public function get_default_metadata_attribute() { + return $this->default_metadata; + } + + /** + * register metadatum types class on array of types + * + * @param $class_name string | object The class name or the instance + */ + public function register_metadata_type( $class_name ) { - /** - * register metadatum types class on array of types - * - * @param $class_name string | object The class name or the instance - */ - public function register_metadata_type( $class_name ){ - // TODO: we shoud not allow registration of metadatum types of retricted core metadatum types (e.g. compound, term) by plugins - - if( is_object( $class_name ) ){ - $class_name = get_class( $class_name ); - } - if(!in_array( $class_name, $this->metadata_types)){ - $this->metadata_types[] = $class_name; - } - } + if ( is_object( $class_name ) ) { + $class_name = get_class( $class_name ); + } - /** - * register metadatum types class on array of types - * - * @param $class_name string | object The class name or the instance - */ - public function unregister_metadata_type( $class_name ){ - if( is_object( $class_name ) ){ - $class_name = get_class( $class_name ); - } + if ( ! in_array( $class_name, $this->metadata_types ) ) { + $this->metadata_types[] = $class_name; + } + } - $key = array_search( $class_name, $this->metadata_types ); - if($key !== false){ - unset( $this->metadata_types[$key] ); - } - } + /** + * register metadatum types class on array of types + * + * @param $class_name string | object The class name or the instance + */ + public function unregister_metadata_type( $class_name ) { + if ( is_object( $class_name ) ) { + $class_name = get_class( $class_name ); + } + + $key = array_search( $class_name, $this->metadata_types ); + if ( $key !== false ) { + unset( $this->metadata_types[ $key ] ); + } + } /** @@ -306,32 +309,33 @@ class Metadata extends Repository { * @return Entities\Metadatum|\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\Metadatum($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\Metadatum( $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\Metadatum::get_post_type(); - - $args = apply_filters('tainacan_fetch_args', $args, 'metadata'); + $args = array_merge( [ + 'posts_per_page' => - 1, + ], $args ); + + $args = $this->parse_fetch_args( $args ); + + $args['post_type'] = Entities\Metadatum::get_post_type(); + + $args = apply_filters( 'tainacan_fetch_args', $args, 'metadata' ); + + $wp_query = new \WP_Query( $args ); + + return $this->fetch_output( $wp_query, $output ); + } + } - $wp_query = new \WP_Query($args); - return $this->fetch_output($wp_query, $output); - } - } - /** * fetch metadata IDs based on WP_Query args * @@ -342,17 +346,18 @@ class Metadata extends Repository { * @param array $args WP_Query args || int $args the item id * * @return Array array of IDs; + * @throws \Exception */ public function fetch_ids( $args = [] ) { - + $args['fields'] = 'ids'; - + return $this->fetch( $args )->get_posts(); } /** * fetch metadatum by collection, considering inheritance and order - * + * * @param Entities\Collection $collection * @param array $args WP_Query args plus disabled_metadata * @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values) @@ -360,172 +365,175 @@ class Metadata extends Repository { * @return array Entities\Metadatum * @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 metadatum - $parents[] = $this->get_default_metadata_attribute(); + //search for default metadatum + $parents[] = $this->get_default_metadata_attribute(); - $meta_query = array( - 'key' => 'collection_id', - 'value' => $parents, - 'compare' => 'IN', - ); - - $args = array_merge([ + $meta_query = array( + 'key' => 'collection_id', + 'value' => $parents, + 'compare' => 'IN', + ); + + $args = array_merge( [ 'parent' => 0 - ], $args); + ], $args ); - 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 - ); - } - /** * fetch metadata IDs by collection, considering inheritance - * + * * @param Entities\Collection|int $collection object or ID * @param array $args WP_Query args plus disabled_metadata * * @return array List of metadata IDs * @throws \Exception */ - public function fetch_ids_by_collection($collection, $args = []){ - - if ($collection instanceof Entities\Collection) { + public function fetch_ids_by_collection( $collection, $args = [] ) { + + if ( $collection instanceof Entities\Collection ) { $collection_id = $collection->get_id(); - } elseif (is_integer($collection)) { + } elseif ( is_integer( $collection ) ) { $collection_id = $collection; } else { - throw new \InvalidArgumentException('fetch_ids_by_collection expects paramater 1 to be a integer or a \Tainacan\Entities\Collection object. ' . gettype($collection) . ' given'); + throw new \InvalidArgumentException( 'fetch_ids_by_collection expects paramater 1 to be a integer or a \Tainacan\Entities\Collection object. ' . gettype( $collection ) . ' given' ); } - //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 metadatum - $parents[] = $this->get_default_metadata_attribute(); + //search for default metadatum + $parents[] = $this->get_default_metadata_attribute(); - $meta_query = array( - 'key' => 'collection_id', - 'value' => $parents, - 'compare' => 'IN', - ); - - $args = array_merge([ + $meta_query = array( + 'key' => 'collection_id', + 'value' => $parents, + 'compare' => 'IN', + ); + + $args = array_merge( [ 'parent' => 0 - ], $args); + ], $args ); - 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->fetch_ids( $args ); - } + return $this->fetch_ids( $args ); + } - /** - * Ordinate the result from fetch response if $collection has an ordination, - * metadata not ordinated appear on the end of the list - * - * - * @param \WP_Query|array $result Response from method fetch - * @param Entities\Collection $collection - * @param bool $include_disabled Wether to include disabled metadata 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_metadata_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 metadata disabled if the arg is set - if ( ! $include_disabled && isset( $order[ $index ]['enabled'] ) && ! $order[ $index ]['enabled'] ) { - continue; - } - - $enable = ( isset( $order[ $index ]['enabled'] ) ) ? $order[ $index ]['enabled'] : 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\Metadatum $metadatum - * @return \Tainacan\Entities\Metadatum - * {@inheritDoc} - * @see \Tainacan\Repositories\Repository::insert() - */ - public function insert($metadatum){ - $this->pre_update_taxonomy_metadatum($metadatum); - $new_metadatum = parent::insert($metadatum); + * Ordinate the result from fetch response if $collection has an ordination, + * metadata not ordinated appear on the end of the list + * + * + * @param \WP_Query|array $result Response from method fetch + * @param Entities\Collection $collection + * @param bool $include_disabled Wether to include disabled metadata 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_metadata_order(); + if ( $order ) { + $order = ( is_array( $order ) ) ? $order : unserialize( $order ); - $this->update_taxonomy_metadatum($new_metadatum); - return $new_metadatum; - } + 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 metadata disabled if the arg is set + if ( ! $include_disabled && isset( $order[ $index ]['enabled'] ) && ! $order[ $index ]['enabled'] ) { + continue; + } + + $enable = ( isset( $order[ $index ]['enabled'] ) ) ? $order[ $index ]['enabled'] : 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\Metadatum $metadatum + * + * @return \Tainacan\Entities\Metadatum + * {@inheritDoc} + * @see \Tainacan\Repositories\Repository::insert() + */ + public function insert( $metadatum ) { + $this->pre_update_taxonomy_metadatum( $metadatum ); + $new_metadatum = parent::insert( $metadatum ); + + $this->update_taxonomy_metadatum( $new_metadatum ); + + return $new_metadatum; + } /** * @param $object @@ -534,9 +542,9 @@ class Metadata 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 ); + } /** * @param $metadatum_id @@ -544,11 +552,13 @@ class Metadata extends Repository { * @return mixed|void * @throws \Exception */ - public function delete($metadatum_id){ + public function delete( $metadatum_id ) { $deleted = new Entities\Metadatum( wp_delete_post( $metadatum_id, true ) ); if ( $deleted ) { - do_action( 'tainacan-deleted', $deleted, [], false, true ); + $this->logs_repository->insert_log( $deleted, [], false, true ); + + do_action( 'tainacan-deleted', $deleted ); } return $deleted; @@ -560,43 +570,46 @@ class Metadata extends Repository { * @return mixed|Entities\Metadatum * @throws \Exception */ - public function trash($metadatum_id){ - $this->delete_taxonomy_metadatum($metadatum_id); + public function trash( $metadatum_id ) { + $this->delete_taxonomy_metadatum( $metadatum_id ); - $trashed = new Entities\Metadatum( wp_trash_post( $metadatum_id ) ); + $trashed = new Entities\Metadatum( wp_trash_post( $metadatum_id ) ); - if($trashed) { - do_action( 'tainacan-trashed', $trashed, [], false, false, true ); - } + if ( $trashed ) { + $this->logs_repository->insert_log( $trashed, [], false, false, true ); - return $trashed; - } + do_action( 'tainacan-trashed', $trashed ); + } - /** - * fetch all registered metadatum type classes - * - * Possible outputs are: - * CLASS (default) - returns the Class name of of metadatum types registered - * NAME - return an Array of the names of metadatum types registered - * - * @param $output string CLASS | NAME - * @return array of Entities\Metadata_Types\Metadata_Type classes path name - */ - public function fetch_metadata_types( $output = 'CLASS'){ - $return = []; + return $trashed; + } - do_action('register_metadata_types'); + /** + * fetch all registered metadatum type classes + * + * Possible outputs are: + * CLASS (default) - returns the Class name of of metadatum types registered + * NAME - return an Array of the names of metadatum types registered + * + * @param $output string CLASS | NAME + * + * @return array of Entities\Metadata_Types\Metadata_Type classes path name + */ + public function fetch_metadata_types( $output = 'CLASS' ) { + $return = []; - if( $output === 'NAME' ){ - foreach ($this->metadata_types as $metadata_type) { - $return[] = str_replace('Tainacan\Metadata_Types\\','', $metadata_type); - } + do_action( 'register_metadata_types' ); - return $return; - } + if ( $output === 'NAME' ) { + foreach ( $this->metadata_types as $metadata_type ) { + $return[] = str_replace( 'Tainacan\Metadata_Types\\', '', $metadata_type ); + } - return $this->metadata_types; - } + return $return; + } + + return $this->metadata_types; + } /** @@ -608,21 +621,21 @@ 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){ + 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; - $item_post_type = $collection_new->get_db_identifier(); + $item_post_type = $collection_new->get_db_identifier(); $parent_collection_id = $collection_new->get_parent(); - if ($parent_collection_id != 0 && $collection_old->get_parent() == 0) { + if ( $parent_collection_id != 0 && $collection_old->get_parent() == 0 ) { update_post_meta( $old_description_metadatum->get_id(), 'metadata_type', 'to_delete', $old_description_metadatum->get_metadata_type() ); - wp_delete_post( $old_description_metadatum->get_id(), true); + 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); + wp_delete_post( $old_title_metadatum->get_id(), true ); } - $new_title_metadatum = $collection_new->get_core_title_metadatum(); + $new_title_metadatum = $collection_new->get_core_title_metadatum(); $new_description_metadatum = $collection_new->get_core_description_metadatum(); $sql_statement = $wpdb->prepare( @@ -635,7 +648,7 @@ class Metadata extends Repository { )", $new_title_metadatum->get_id(), $old_title_metadatum->get_id(), $item_post_type ); - $res = $wpdb->query($sql_statement); + $res = $wpdb->query( $sql_statement ); $sql_statement = $wpdb->prepare( "UPDATE $wpdb->postmeta @@ -647,43 +660,43 @@ class Metadata extends Repository { )", $new_description_metadatum->get_id(), $old_description_metadatum->get_id(), $item_post_type ); - $res = $wpdb->query($sql_statement); + $res = $wpdb->query( $sql_statement ); wp_cache_flush(); - } + } /** * @param Entities\Collection $collection * * @return array */ - private function get_data_core_metadata(Entities\Collection $collection){ + private function get_data_core_metadata( Entities\Collection $collection ) { return $data_core_metadata = [ 'core_description' => [ - 'name' => 'Description', - 'description' => 'description', - 'collection_id' => $collection->get_id(), - 'metadata_type' => 'Tainacan\Metadata_Types\Core_Description', - 'status' => 'publish', - 'exposer_mapping' => [ + 'name' => 'Description', + 'description' => 'description', + 'collection_id' => $collection->get_id(), + 'metadata_type' => 'Tainacan\Metadata_Types\Core_Description', + 'status' => 'publish', + 'exposer_mapping' => [ 'dublin-core' => 'description' ] ], 'core_title' => [ - 'name' => 'Title', - 'description' => 'title', - 'collection_id' => $collection->get_id(), - 'metadata_type' => 'Tainacan\Metadata_Types\Core_Title', - 'status' => 'publish', - 'exposer_mapping' => [ + 'name' => 'Title', + 'description' => 'title', + 'collection_id' => $collection->get_id(), + 'metadata_type' => 'Tainacan\Metadata_Types\Core_Title', + 'status' => 'publish', + 'exposer_mapping' => [ 'dublin-core' => 'title' ] ] ]; - } + } /** * @param Entities\Collection $collection @@ -692,33 +705,33 @@ class Metadata extends Repository { * @throws \ErrorException * @throws \Exception */ - public function register_core_metadata( Entities\Collection $collection ){ + public function register_core_metadata( Entities\Collection $collection ) { - if ($collection->get_status() == 'auto-draft') { + if ( $collection->get_status() == 'auto-draft' ) { return; } - + $metadata = $collection->get_core_metadata(); - $data_core_metadata = $this->get_data_core_metadata($collection); + $data_core_metadata = $this->get_data_core_metadata( $collection ); - foreach ( $data_core_metadata as $index => $data_core_metadatum ) { - if( empty( $metadata ) ){ - $this->insert_array_metadatum( $data_core_metadatum ); - } else { - $exists = false; - foreach ( $metadata as $metadatum ){ - if ( $metadatum->get_metadata_type() === $data_core_metadatum['metadata_type'] ) { - $exists = true; - } - } + foreach ( $data_core_metadata as $index => $data_core_metadatum ) { + if ( empty( $metadata ) ) { + $this->insert_array_metadatum( $data_core_metadatum ); + } else { + $exists = false; + foreach ( $metadata as $metadatum ) { + if ( $metadatum->get_metadata_type() === $data_core_metadatum['metadata_type'] ) { + $exists = true; + } + } - if( !$exists ){ - $this->insert_array_metadatum( $data_core_metadatum ); - } - } + if ( ! $exists ) { + $this->insert_array_metadatum( $data_core_metadatum ); + } + } } - } + } /** * block user from remove core metadata @@ -729,13 +742,13 @@ class Metadata extends Repository { * @return null/bool * @throws \Exception */ - public function disable_delete_core_metadata( $before, $post ){ - $metadatum = $this->fetch( $post->ID ); + public function disable_delete_core_metadata( $before, $post ) { + $metadatum = $this->fetch( $post->ID ); - if ( $metadatum && in_array( $metadatum->get_metadata_type(), $this->core_metadata ) && is_numeric($metadatum->get_collection_id()) ) { - return false; - } - } + if ( $metadatum && in_array( $metadatum->get_metadata_type(), $this->core_metadata ) && is_numeric( $metadatum->get_collection_id() ) ) { + return false; + } + } /** * block user from remove core metadata ( if use wp_delete_post) @@ -748,13 +761,13 @@ class Metadata extends Repository { * @throws \Exception * @internal param The $post_id post ID which is deleting */ - public function force_delete_core_metadata( $before, $post, $force_delete ){ - $metadatum = $this->fetch( $post->ID ); + public function force_delete_core_metadata( $before, $post, $force_delete ) { + $metadatum = $this->fetch( $post->ID ); - if ( $metadatum && in_array( $metadatum->get_metadata_type(), $this->core_metadata ) && is_numeric($metadatum->get_collection_id()) ) { - return false; - } - } + if ( $metadatum && in_array( $metadatum->get_metadata_type(), $this->core_metadata ) && is_numeric( $metadatum->get_collection_id() ) ) { + return false; + } + } /** * returns all core items from a specific collection @@ -764,17 +777,17 @@ class Metadata extends Repository { * @return Array|\WP_Query * @throws \Exception */ - public function get_core_metadata( Entities\Collection $collection ){ - - return $this->fetch_by_collection($collection, [ + public function get_core_metadata( Entities\Collection $collection ) { + + return $this->fetch_by_collection( $collection, [ 'meta_query' => [ [ - 'key' => 'metadata_type', - 'value' => $this->core_metadata, + 'key' => 'metadata_type', + 'value' => $this->core_metadata, 'compare' => 'IN' ] ] - ], 'OBJECT'); + ], 'OBJECT' ); } @@ -788,19 +801,20 @@ class Metadata extends Repository { */ public function get_core_title_metadatum( Entities\Collection $collection ) { - $results = $this->fetch_by_collection($collection, [ - 'meta_query' => [ + $results = $this->fetch_by_collection( $collection, [ + 'meta_query' => [ [ - 'key' => 'metadata_type', + 'key' => 'metadata_type', 'value' => 'Tainacan\Metadata_Types\Core_Title', ] ], 'posts_per_page' => 1 - ], 'OBJECT'); + ], 'OBJECT' ); - if (is_array($results) && sizeof($results) == 1 && $results[0] instanceof \Tainacan\Entities\Metadatum) { + if ( is_array( $results ) && sizeof( $results ) == 1 && $results[0] instanceof \Tainacan\Entities\Metadatum ) { return $results[0]; } + return false; } @@ -814,19 +828,20 @@ class Metadata extends Repository { */ public function get_core_description_metadatum( Entities\Collection $collection ) { - $results = $this->fetch_by_collection($collection, [ - 'meta_query' => [ + $results = $this->fetch_by_collection( $collection, [ + 'meta_query' => [ [ - 'key' => 'metadata_type', + 'key' => 'metadata_type', 'value' => 'Tainacan\Metadata_Types\Core_Description', ] ], 'posts_per_page' => 1 - ], 'OBJECT'); + ], 'OBJECT' ); - if (is_array($results) && sizeof($results) == 1 && $results[0] instanceof \Tainacan\Entities\Metadatum) { + if ( is_array( $results ) && sizeof( $results ) == 1 && $results[0] instanceof \Tainacan\Entities\Metadatum ) { return $results[0]; } + return false; } @@ -839,23 +854,24 @@ class Metadata extends Repository { * @throws \ErrorException * @throws \Exception */ - public function insert_array_metadatum( $data ){ - $metadatum = new Entities\Metadatum(); - foreach ( $data as $attribute => $value ) { - $set_ = 'set_' . $attribute; - $metadatum->$set_( $value ); - } + public function insert_array_metadatum( $data ) { + $metadatum = new Entities\Metadatum(); + foreach ( $data as $attribute => $value ) { + $set_ = 'set_' . $attribute; + $metadatum->$set_( $value ); + } - if ( $metadatum->validate( )) { - $metadatum = $this->insert( $metadatum ); - return $metadatum->get_id(); - } else { - throw new \ErrorException('The entity wasn\'t validated.' . print_r( $metadatum->get_errors(), true)); - } - } + if ( $metadatum->validate() ) { + $metadatum = $this->insert( $metadatum ); + + return $metadatum->get_id(); + } else { + throw new \ErrorException( 'The entity wasn\'t validated.' . print_r( $metadatum->get_errors(), true ) ); + } + } - # TODO: Fetch all metadatum value for repository level + # TODO: Fetch all metadatum value for repository level /** * Fetch all values of a metadatum from a collection or repository @@ -871,7 +887,7 @@ class Metadata extends Repository { * @return array|null|object * @throws \Exception */ - public function fetch_all_metadatum_values($collection_id, $metadatum_id, $search = '', $offset = 0, $number = ''){ + public function fetch_all_metadatum_values( $collection_id, $metadatum_id, $search = '', $offset = 0, $number = '' ) { global $wpdb; // Clear the result cache @@ -880,65 +896,65 @@ class Metadata extends Repository { $metadatum = new Entities\Metadatum( $metadatum_id ); // handle core titles - if( strpos( $metadatum->get_metadata_type(), 'Core') !== false && $search){ - $collection = new Entities\Collection( $collection_id ); - $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); + if ( strpos( $metadatum->get_metadata_type(), 'Core' ) !== false && $search ) { + $collection = new Entities\Collection( $collection_id ); + $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); - if($number >= 1 && $offset >=0){ - $items = $Tainacan_Items->fetch( [ - 's' => $search, - 'offset' => $offset, - 'posts_per_page' => $number - ], $collection, 'OBJECT'); - } else { - $items = $Tainacan_Items->fetch( ['s' => $search], $collection, 'OBJECT'); - } + if ( $number >= 1 && $offset >= 0 ) { + $items = $Tainacan_Items->fetch( [ + 's' => $search, + 'offset' => $offset, + 'posts_per_page' => $number + ], $collection, 'OBJECT' ); + } else { + $items = $Tainacan_Items->fetch( [ 's' => $search ], $collection, 'OBJECT' ); + } - $return = []; + $return = []; - foreach ($items as $item) { - if( strpos( $metadatum->get_metadata_type(), 'Core_Title') !== false ){ - $title = $item->get_title(); + foreach ( $items as $item ) { + if ( strpos( $metadatum->get_metadata_type(), 'Core_Title' ) !== false ) { + $title = $item->get_title(); - if(!empty($search) && stristr($title, $search) !== false) { - $return[] = [ 'metadatum_id' => $metadatum_id, 'mvalue' => $title ]; - } elseif (empty($search)) { - $return[] = [ 'metadatum_id' => $metadatum_id, 'mvalue' => $title ]; - } - } else { - $description = $item->get_description(); + if ( ! empty( $search ) && stristr( $title, $search ) !== false ) { + $return[] = [ 'metadatum_id' => $metadatum_id, 'mvalue' => $title ]; + } elseif ( empty( $search ) ) { + $return[] = [ 'metadatum_id' => $metadatum_id, 'mvalue' => $title ]; + } + } else { + $description = $item->get_description(); - if(!empty($search) && stristr($description, $search) !== false) { - $return[] = [ 'metadatum_id' => $metadatum_id, 'mvalue' => $description ]; - } elseif (empty($search)) { - $return[] = [ 'metadatum_id' => $metadatum_id, 'mvalue' => $description ]; - } - } - } + if ( ! empty( $search ) && stristr( $description, $search ) !== false ) { + $return[] = [ 'metadatum_id' => $metadatum_id, 'mvalue' => $description ]; + } elseif ( empty( $search ) ) { + $return[] = [ 'metadatum_id' => $metadatum_id, 'mvalue' => $description ]; + } + } + } - return $return; - } + return $return; + } $item_post_type = "%%{$collection_id}_item"; - $collection = new Entities\Collection($collection_id); + $collection = new Entities\Collection( $collection_id ); $capabilities = $collection->get_capabilities(); $results = []; $search_query = ''; - if ($search) { + if ( $search ) { $search_param = '%' . $search . '%'; $search_query = $wpdb->prepare( "WHERE meta_value LIKE %s", $search_param ); } $pagination = ''; - if($offset >= 0 && $number >= 1){ - $pagination = $wpdb->prepare("LIMIT %d,%d", (int) $offset, (int) $number); + if ( $offset >= 0 && $number >= 1 ) { + $pagination = $wpdb->prepare( "LIMIT %d,%d", (int) $offset, (int) $number ); } // 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, @@ -948,9 +964,9 @@ class Metadata extends Repository { $post_statuses = get_post_stati( $args, 'names', 'and' ); - foreach ($post_statuses as $post_status) { + foreach ( $post_statuses as $post_status ) { - if($collection_id) { + if ( $collection_id ) { $sql_string = $wpdb->prepare( "SELECT DISTINCT metadatum_id, mvalue FROM ( @@ -984,22 +1000,22 @@ class Metadata extends Repository { $pre_result = $wpdb->get_results( $sql_string, ARRAY_A ); - if (!empty($pre_result)) { - foreach ($pre_result as $pre){ + if ( ! empty( $pre_result ) ) { + foreach ( $pre_result as $pre ) { $results[] = $pre; } } } - } 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 ) { - if($collection_id) { + if ( $collection_id ) { $sql_string = $wpdb->prepare( "SELECT DISTINCT metadatum_id, mvalue FROM ( @@ -1033,29 +1049,29 @@ class Metadata extends Repository { $pre_result = $wpdb->get_results( $sql_string, ARRAY_A ); - if (!empty($pre_result)) { - foreach ($pre_result as $pre){ + if ( ! empty( $pre_result ) ) { + foreach ( $pre_result as $pre ) { $results[] = $pre; } } } } - return $this->unique_multidimensional_array($results, 'mvalue'); + return $this->unique_multidimensional_array( $results, 'mvalue' ); } - + /** * Stores the value of the taxonomy_id option to use on update_taxonomy_metadatum method. * */ - private function pre_update_taxonomy_metadatum($metadatum) { + private function pre_update_taxonomy_metadatum( $metadatum ) { $metadata_type = $metadatum->get_metadata_type_object(); - $current_tax = ''; - if ($metadata_type->get_primitive_type() == 'term') { - - $options = $this->get_mapped_property($metadatum, 'metadata_type_options'); - $metadata_type->set_options($options); - $current_tax = $metadata_type->get_option('taxonomy_id'); + $current_tax = ''; + if ( $metadata_type->get_primitive_type() == 'term' ) { + + $options = $this->get_mapped_property( $metadatum, 'metadata_type_options' ); + $metadata_type->set_options( $options ); + $current_tax = $metadata_type->get_option( 'taxonomy_id' ); } $this->current_taxonomy = $current_tax; } @@ -1070,38 +1086,38 @@ class Metadata extends Repository { * * @return void [type] [description] */ - private function update_taxonomy_metadatum($metadatum) { + private function update_taxonomy_metadatum( $metadatum ) { $metadata_type = $metadatum->get_metadata_type_object(); - $new_tax = ''; - - if ($metadata_type->get_primitive_type() == 'term') { - $new_tax = $metadata_type->get_option('taxonomy_id'); + $new_tax = ''; + + if ( $metadata_type->get_primitive_type() == 'term' ) { + $new_tax = $metadata_type->get_option( 'taxonomy_id' ); } - - if ($new_tax != $this->current_taxonomy) { + + if ( $new_tax != $this->current_taxonomy ) { $collection = $metadatum->get_collection(); - if (!empty($this->current_taxonomy) && $collection) { - do_action('tainacan-taxonomy-removed-from-collection', $this->current_taxonomy, $collection); + if ( ! empty( $this->current_taxonomy ) && $collection ) { + do_action( 'tainacan-taxonomy-removed-from-collection', $this->current_taxonomy, $collection ); } - if (!empty($new_tax) && $collection) { - do_action('tainacan-taxonomy-added-to-collection', $new_tax, $collection); + if ( ! empty( $new_tax ) && $collection ) { + do_action( 'tainacan-taxonomy-added-to-collection', $new_tax, $collection ); } } } - - private function delete_taxonomy_metadatum($metadatum_id) { - $metadatum = $this->fetch($metadatum_id); + + private function delete_taxonomy_metadatum( $metadatum_id ) { + $metadatum = $this->fetch( $metadatum_id ); $metadata_type = $metadatum->get_metadata_type_object(); - if ($metadata_type->get_primitive_type() == 'term') { - $removed_tax = $metadata_type->get_option('taxonomy_id'); + if ( $metadata_type->get_primitive_type() == 'term' ) { + $removed_tax = $metadata_type->get_option( 'taxonomy_id' ); $collection = $metadatum->get_collection(); - if (!empty($removed_tax) && $collection) { + if ( ! empty( $removed_tax ) && $collection ) { do_action( 'tainacan-taxonomy-removed-from-collection', $removed_tax, $collection ); } } diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index a6c52493c..af43079ce 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -15,12 +15,20 @@ abstract class Repository { /** * If set to false, no logs will be generated upon insertion or update - * + * * use enable_logs() and disable_logs() to set the values + * * @var bool */ private $use_logs = true; + /** + * Instance of Repository Logs + * + * @var Repositories\Logs + */ + protected $logs_repository; + /** * Disable creation of logs while inerting and updating entities */ @@ -29,7 +37,7 @@ abstract class Repository { } /** - * Enable creation of logs while inerting and updating entities + * Enable creation of logs while inserting and updating entities * if it was disabled */ public function enable_logs() { @@ -41,9 +49,15 @@ abstract class Repository { */ protected function __construct() { add_action( 'init', array( &$this, 'register_post_type' ) ); + add_action( 'init', array( &$this, 'init_objects' ) ); + add_filter( 'tainacan-get-map-' . $this->get_name(), array( $this, 'get_default_properties' ) ); } + public function init_objects() { + $this->logs_repository = Repositories\Logs::get_instance(); + } + /** * return properties map * @@ -100,26 +114,26 @@ abstract class Repository { // TODO: Throw Warning saying you must validate object before insert() } - $is_update = false; - $old = ''; + $is_update = false; + $old = ''; $diffs = []; - - if ($this->use_logs) { + + if ( $this->use_logs ) { if ( $obj->get_id() ) { - + $old = $obj->get_repository()->fetch( $obj->get_id() ); - + if ( method_exists( $old, 'get_status' ) && $old->get_status() === 'auto-draft' ) { $is_update = false; } else { $is_update = true; } - + $diffs = $this->diff( $old, $obj ); } } - + $map = $this->get_map(); // First iterate through the native post properties @@ -163,10 +177,13 @@ abstract class Repository { } // TODO: Logs for header image insert and update - if ($this->use_logs) { - do_action( 'tainacan-insert', $obj, $diffs, $is_update ); - do_action( 'tainacan-insert-' . $obj->get_post_type(), $obj ); + if ( $this->use_logs ) { + $this->logs_repository->insert_log( $obj, $diffs, $is_update ); } + + do_action( 'tainacan-insert', $obj, $diffs, $is_update ); + do_action( 'tainacan-insert-' . $obj->get_post_type(), $obj ); + // return a brand new object return new $this->entities_type( $obj->WP_Post ); } @@ -190,8 +207,8 @@ abstract class Repository { if ( $map[ $prop ]['map'] == 'meta' ) { - if($prop === '_thumbnail_id'){ - $diffs = $this->insert_thumbnail($obj, $diffs); + if ( $prop === '_thumbnail_id' ) { + $diffs = $this->insert_thumbnail( $obj, $diffs ); return $diffs; } else { @@ -199,28 +216,28 @@ abstract class Repository { } } elseif ( $map[ $prop ]['map'] == 'meta_multi' ) { - $values = $obj->get_mapped_property( $prop ); + $values = $obj->get_mapped_property( $prop ); $current_values = get_post_meta( $obj->get_id(), $prop ); - - if (empty($values) || !is_array($values)) { + + if ( empty( $values ) || ! is_array( $values ) ) { $values = []; } - if (empty($current_values) || !is_array($current_values)) { + if ( empty( $current_values ) || ! is_array( $current_values ) ) { $current_values = []; } - + $deleted = array_diff( $current_values, $values ); $added = array_diff( $values, $current_values ); - - foreach ($deleted as $del) { + + foreach ( $deleted as $del ) { delete_post_meta( $obj->get_id(), $prop, $del ); } - - foreach ($added as $add) { + + foreach ( $added as $add ) { add_post_meta( $obj->get_id(), $prop, $this->maybe_add_slashes( $add ) ); } - + } return $diffs; @@ -333,14 +350,14 @@ abstract class Repository { } $args['meta_query'] = $meta_query; - + // Map orderby parameter - if ( isset($args['orderby']) ) { - if ( array_key_exists($args['orderby'], $map) ) { + if ( isset( $args['orderby'] ) ) { + if ( array_key_exists( $args['orderby'], $map ) ) { $args['orderby'] = $map[ $args['orderby'] ]['map']; } } - + return $args; @@ -463,9 +480,9 @@ abstract class Repository { * @return \Tainacan\Entities\Entity|boolean the entity for post_type, with data if $post is given or false */ public static function get_entity_by_post_type( $post_type, $post = 0 ) { - $prefix = substr( $post_type, 0, strlen( Entities\Collection::$db_identifier_prefix ) ); - $item_metadata = Repositories\Item_Metadata::get_instance(); - $item_metadata_entity = new $item_metadata->entities_type(null, null); + $prefix = substr( $post_type, 0, strlen( Entities\Collection::$db_identifier_prefix ) ); + $item_metadata = Repositories\Item_Metadata::get_instance(); + $item_metadata_entity = new $item_metadata->entities_type( null, null ); $item_metadata_post_type = $item_metadata_entity::get_post_type(); // Is it a collection Item? @@ -482,7 +499,7 @@ abstract class Repository { $Tainacan_Collections = Repositories\Collections::get_instance(); $Tainacan_Filters = Repositories\Filters::get_instance(); $Tainacan_Logs = Repositories\Logs::get_instance(); - $Tainacan_Metadata = Repositories\Metadata::get_instance(); + $Tainacan_Metadata = Repositories\Metadata::get_instance(); $Tainacan_Taxonomies = Repositories\Taxonomies::get_instance(); $Tainacan_Terms = Repositories\Terms::get_instance(); @@ -495,7 +512,7 @@ abstract class Repository { $Tainacan_Logs ]; foreach ( $tnc_globals as $tnc_repository ) { - $tnc_entity = new $tnc_repository->entities_type(); + $tnc_entity = new $tnc_repository->entities_type(); $entity_post_type = $tnc_entity::get_post_type(); if ( $entity_post_type == $post_type ) { @@ -525,7 +542,7 @@ abstract class Repository { return $Tainacan_Items; } else { $Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance(); - $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); + $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); $Tainacan_Filters = \Tainacan\Repositories\Filters::get_instance(); $Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance(); @@ -542,7 +559,7 @@ abstract class Repository { $Tainacan_Logs ]; foreach ( $tnc_globals as $tnc_repository ) { - $tnc_entity = new $tnc_repository->entities_type(); + $tnc_entity = new $tnc_repository->entities_type(); $entity_post_type = $tnc_entity::get_post_type(); if ( $entity_post_type == $post_type ) { @@ -556,23 +573,23 @@ abstract class Repository { /** * Fetch one Entity based on query args. - * + * * Note: Does not work with Item_Metadata Repository - * + * * @param array $args Query Args as expected by fetch - * + * * @return false|\Tainacan\Entities The entity or false if none was found */ - public function fetch_one($args) { - if ($this->get_name() == 'Item_Metadata') { + public function fetch_one( $args ) { + if ( $this->get_name() == 'Item_Metadata' ) { return false; } $args['posts_per_page'] = 1; - $results = $this->fetch($args, 'OBJECT'); - - if (is_array($results) && sizeof($results) > 0 && $results[0] instanceof \Tainacan\Entities\Entity) { + $results = $this->fetch( $args, 'OBJECT' ); + + if ( is_array( $results ) && sizeof( $results ) > 0 && $results[0] instanceof \Tainacan\Entities\Entity ) { return $results[0]; } @@ -730,17 +747,17 @@ abstract class Repository { * * @return array */ - function unique_multidimensional_array($array, $key) { + function unique_multidimensional_array( $array, $key ) { $temp_array = array(); - $i = 0; - $key_array = array(); + $i = 0; + $key_array = array(); - foreach($array as $val) { - if (!in_array($val[$key], $key_array)) { - $key_array[$i] = $val[$key]; - $temp_array[$i] = $val; + foreach ( $array as $val ) { + if ( ! in_array( $val[ $key ], $key_array ) ) { + $key_array[ $i ] = $val[ $key ]; + $temp_array[ $i ] = $val; } - $i++; + $i ++; } return $temp_array; @@ -758,7 +775,7 @@ abstract class Repository { public function diff( $old = 0, $new ) { $old_entity = null; - if ( $old === 0 || is_array($old) && count($old) == 0 ) { // self diff or other entity? + if ( $old === 0 || is_array( $old ) && count( $old ) == 0 ) { // self diff or other entity? $id = $new->get_id(); if ( ! empty( $id ) ) { // there is a repository entity? @@ -768,7 +785,7 @@ abstract class Repository { $old_entity = new $entity_type; // there is no saved entity, let compare with a new empty one } } else { - if($old->get_status() === 'auto-draft'){ + if ( $old->get_status() === 'auto-draft' ) { $entity_type = get_class( $new ); $old_entity = new $entity_type; } else { @@ -822,7 +839,7 @@ abstract class Repository { } } - unset($diff['id'], $diff['collection_id'], $diff['author_id'], $diff['creation_date'], $diff['_thumbnail_id']); + unset( $diff['id'], $diff['collection_id'], $diff['author_id'], $diff['creation_date'], $diff['_thumbnail_id'] ); $diff = apply_filters( 'tainacan-entity-diff', $diff, $new, $old ); return $diff; diff --git a/src/classes/repositories/class-tainacan-taxonomies.php b/src/classes/repositories/class-tainacan-taxonomies.php index 9b6cfe369..85bab3327 100644 --- a/src/classes/repositories/class-tainacan-taxonomies.php +++ b/src/classes/repositories/class-tainacan-taxonomies.php @@ -1,251 +1,258 @@ get_name(), [ - 'name' => [ - 'map' => 'post_title', - 'title' => __('Name', 'tainacan'), - 'type' => 'string', - 'description' => __('Name of the taxonomy', 'tainacan'), - 'on_error' => __('The taxonomy name should be a text value and should not be empty.', 'tainacan'), - 'validation' => v::stringType()->notEmpty(), - ], - 'description' => [ - 'map' => 'post_content', - 'title' => __('Description', 'tainacan'), - 'type' => 'string', - 'description' => __('The taxonomy description', 'tainacan'), - 'default' => '', - 'validation' => '' - ], - 'slug' => [ - 'map' => 'post_name', - 'title' => __('Slug', 'tainacan'), - 'type' => 'string', - 'description' => __('The taxonomy slug', 'tainacan'), - 'validation' => '' - ], - 'allow_insert' => [ - 'map' => 'meta', - 'title' => __('Allow insert', 'tainacan'), - 'type' => 'string', - 'description' => __('Allow/Deny the creation of new terms in the taxonomy', 'tainacan'), - 'on_error' => __('Invalid insertion, allowed values are ( yes/no )', 'tainacan'), - 'validation' => v::stringType()->in(['yes', 'no']), // yes or no - 'default' => 'yes' - ], - 'collections_ids' => [ - 'map' => 'meta_multi', - 'title' => __('Collections', 'tainacan'), - 'type' => 'string', - 'description' => __('The IDs of collection where the taxonomy is used', 'tainacan'), - 'validation' => '' - ], - ]); - } - + + 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 taxonomy', 'tainacan' ), + 'on_error' => __( 'The taxonomy name should be a text value and should not be empty.', 'tainacan' ), + 'validation' => v::stringType()->notEmpty(), + ], + 'description' => [ + 'map' => 'post_content', + 'title' => __( 'Description', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The taxonomy description', 'tainacan' ), + 'default' => '', + 'validation' => '' + ], + 'slug' => [ + 'map' => 'post_name', + 'title' => __( 'Slug', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The taxonomy slug', 'tainacan' ), + 'validation' => '' + ], + 'allow_insert' => [ + 'map' => 'meta', + 'title' => __( 'Allow insert', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'Allow/Deny the creation of new terms in the taxonomy', 'tainacan' ), + 'on_error' => __( 'Invalid insertion, allowed values are ( yes/no )', 'tainacan' ), + 'validation' => v::stringType()->in( [ 'yes', 'no' ] ), // yes or no + 'default' => 'yes' + ], + 'collections_ids' => [ + 'map' => 'meta_multi', + 'title' => __( 'Collections', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The IDs of collection where the taxonomy is used', 'tainacan' ), + 'validation' => '' + ], + ] ); + } + /** * 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' => __('Taxonomies', 'tainacan'), - 'singular_name' => __('Taxonomy', 'tainacan'), - 'add_new' => __('Add new', 'tainacan'), - 'add_new_item' => __('Add new Taxonomy', 'tainacan'), - 'edit_item' => __('Edit Taxonomy', 'tainacan'), - 'new_item' => __('New Taxonomy', 'tainacan'), - 'view_item' => __('View Taxonomy', 'tainacan'), - 'search_items' => __('Search Taxonomies', 'tainacan'), - 'not_found' => __('No Taxonomies found ', 'tainacan'), - 'not_found_in_trash' => __('No Taxonomies found in trash', 'tainacan'), - 'parent_item_colon' => __('Parent Taxonomy:', 'tainacan'), - 'menu_name' => __('Taxonomies', 'tainacan') - ); + 'name' => __( 'Taxonomies', 'tainacan' ), + 'singular_name' => __( 'Taxonomy', 'tainacan' ), + 'add_new' => __( 'Add new', 'tainacan' ), + 'add_new_item' => __( 'Add new Taxonomy', 'tainacan' ), + 'edit_item' => __( 'Edit Taxonomy', 'tainacan' ), + 'new_item' => __( 'New Taxonomy', 'tainacan' ), + 'view_item' => __( 'View Taxonomy', 'tainacan' ), + 'search_items' => __( 'Search Taxonomies', 'tainacan' ), + 'not_found' => __( 'No Taxonomies found ', 'tainacan' ), + 'not_found_in_trash' => __( 'No Taxonomies found in trash', 'tainacan' ), + 'parent_item_colon' => __( 'Parent Taxonomy:', 'tainacan' ), + 'menu_name' => __( 'Taxonomies', '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' => false, - 'exclude_from_search' => true, - 'has_archive' => false, - 'query_var' => true, - 'can_export' => true, - 'rewrite' => true, - 'map_meta_cap' => true, - 'capability_type' => Entities\Taxonomy::get_capability_type(), - 'supports' => [ - 'title', - 'editor', - 'page-attributes' - ] - ); - register_post_type(Entities\Taxonomy::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' => false, + 'exclude_from_search' => true, + 'has_archive' => false, + 'query_var' => true, + 'can_export' => true, + 'rewrite' => true, + 'map_meta_cap' => true, + 'capability_type' => Entities\Taxonomy::get_capability_type(), + 'supports' => [ + 'title', + 'editor', + 'page-attributes' + ] + ); + register_post_type( Entities\Taxonomy::get_post_type(), $args ); + } /** * @param Entities\Taxonomy $taxonomy * * @return Entities\Entity */ - public function insert($taxonomy) { + public function insert( $taxonomy ) { - $new_taxonomy = parent::insert($taxonomy); - $new_taxonomy->tainacan_register_taxonomy(); - - // return a brand new object - return $new_taxonomy; - } + $new_taxonomy = parent::insert( $taxonomy ); + $new_taxonomy->tainacan_register_taxonomy(); - /** - * fetch taxonomies based on ID or WP_Query args - * - * Taxonomies are stored as posts. Check WP_Query docs - * to learn all args accepted in the $args parameter (@see https://developer.wordpress.org/reference/classes/wp_query/) - * You can also use a mapped property, such as name and description, as an argument and it will be mapped to the - * appropriate WP_Query argument - * - * @param array $args WP_Query args | int $args the taxonomy id - * @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values) - * @return \WP_Query|Array an instance of wp query OR array of entities; - */ - public function fetch( $args = [], $output = null ) { - - // TODO: Pegar taxonomias registradas via código - - if( is_numeric($args) ){ - $existing_post = get_post($args); - if ($existing_post instanceof \WP_Post) { - return new Entities\Taxonomy($existing_post); - } else { - return []; - } - } elseif (is_array($args)) { + // return a brand new object + return $new_taxonomy; + } - $args = array_merge([ - 'posts_per_page' => -1, - ], $args); + /** + * fetch taxonomies based on ID or WP_Query args + * + * Taxonomies are stored as posts. Check WP_Query docs + * to learn all args accepted in the $args parameter (@see https://developer.wordpress.org/reference/classes/wp_query/) + * You can also use a mapped property, such as name and description, as an argument and it will be mapped to the + * appropriate WP_Query argument + * + * @param array $args WP_Query args | int $args the taxonomy id + * @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values) + * + * @return \WP_Query|Array an instance of wp query OR array of entities; + */ + public function fetch( $args = [], $output = null ) { - $args = $this->parse_fetch_args($args); + // TODO: Pegar taxonomias registradas via código - $args['post_type'] = Entities\Taxonomy::get_post_type(); + if ( is_numeric( $args ) ) { + $existing_post = get_post( $args ); + if ( $existing_post instanceof \WP_Post ) { + return new Entities\Taxonomy( $existing_post ); + } else { + return []; + } + } elseif ( is_array( $args ) ) { - $args = apply_filters('tainacan_fetch_args', $args, 'taxonomies'); - - $wp_query = new \WP_Query($args); - return $this->fetch_output($wp_query, $output); - } - } + $args = array_merge( [ + 'posts_per_page' => - 1, + ], $args ); - public function update($object, $new_values = null){ - return $this->insert($object); - } + $args = $this->parse_fetch_args( $args ); - public function delete($taxonomy_id){ - $taxonomy_name = $this->fetch($taxonomy_id)->get_db_identifier(); + $args['post_type'] = Entities\Taxonomy::get_post_type(); - /* TODO: Investigate the cause of taxonomies aren't been registered + $args = apply_filters( 'tainacan_fetch_args', $args, 'taxonomies' ); + + $wp_query = new \WP_Query( $args ); + + return $this->fetch_output( $wp_query, $output ); + } + } + + public function update( $object, $new_values = null ) { + return $this->insert( $object ); + } + + public function delete( $taxonomy_id ) { + $taxonomy_name = $this->fetch( $taxonomy_id )->get_db_identifier(); + + /* TODO: Investigate the cause of taxonomies aren't been registered * * This cause a 'invalid taxonomy' exception when try to delete permanently a taxonomy * * This condition is a temporary solution */ - if ( taxonomy_exists( $taxonomy_name ) ) { - $unregistered = unregister_taxonomy( $taxonomy_name ); + if ( taxonomy_exists( $taxonomy_name ) ) { + $unregistered = unregister_taxonomy( $taxonomy_name ); - if ( $unregistered instanceof \WP_Error ) { - return $unregistered; - } - } + if ( $unregistered instanceof \WP_Error ) { + return $unregistered; + } + } - $deleted = new Entities\Taxonomy( wp_delete_post( $taxonomy_id, true ) ); + $deleted = new Entities\Taxonomy( wp_delete_post( $taxonomy_id, true ) ); - if ( ! $deleted ) { - return $deleted; - } + if ( ! $deleted ) { + return $deleted; + } - do_action( 'tainacan-deleted', $deleted, [], false, true ); + $this->logs_repository->insert_log( $deleted, [], false, true ); - return $deleted; - } + do_action( 'tainacan-deleted', $deleted ); + + return $deleted; + } /** * @param $taxonomy_id * * @return mixed|Entities\Taxonomy */ - public function trash($taxonomy_id){ - $trashed = new Entities\Taxonomy(wp_trash_post($taxonomy_id)); + public function trash( $taxonomy_id ) { + $trashed = new Entities\Taxonomy( wp_trash_post( $taxonomy_id ) ); - if(!$trashed){ - return $trashed; - } + if ( ! $trashed ) { + return $trashed; + } - do_action('tainacan-trashed', $trashed, [], false, false, true ); + $this->logs_repository->insert_log( $trashed, [], false, false, true ); - return $trashed; - } - - public function added_collection($taxonomy_id, $collection) { + do_action( 'tainacan-trashed', $trashed ); + + return $trashed; + } + + public function added_collection( $taxonomy_id, $collection ) { $id = $taxonomy_id; - if (!empty($id) && is_numeric($id)) { - $tax = $this->fetch((int) $id); - $tax->add_collection_id($collection->get_id()); - if ($tax->validate()) { - $this->insert($tax); + if ( ! empty( $id ) && is_numeric( $id ) ) { + $tax = $this->fetch( (int) $id ); + $tax->add_collection_id( $collection->get_id() ); + if ( $tax->validate() ) { + $this->insert( $tax ); } } } - - public function removed_collection($taxonomy_id, $collection) { - $id = $taxonomy_id; - if (!empty($id) && is_numeric($id)) { - $tax = $this->fetch((int) $id); - $tax->remove_collection_id($collection->get_id()); - if ($tax->validate()) { - $this->insert($tax); + + public function removed_collection( $taxonomy_id, $collection ) { + $id = $taxonomy_id; + if ( ! empty( $id ) && is_numeric( $id ) ) { + $tax = $this->fetch( (int) $id ); + $tax->remove_collection_id( $collection->get_id() ); + if ( $tax->validate() ) { + $this->insert( $tax ); } } } diff --git a/src/classes/repositories/class-tainacan-terms.php b/src/classes/repositories/class-tainacan-terms.php index 1ae8084c0..45d0c7765 100644 --- a/src/classes/repositories/class-tainacan-terms.php +++ b/src/classes/repositories/class-tainacan-terms.php @@ -1,19 +1,21 @@ get_name(), [ - 'term_id' => [ - 'map' => 'term_id', - 'title' => __( 'ID', 'tainacan' ), - 'type' => 'integer', - 'description' => __( 'Unique identifier', 'tainacan' ), - //'validation' => '' - ], - 'name' => [ - 'map' => 'name', - 'title' => __( 'Name', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'Name of the term', 'tainacan' ), - 'on_error' => __( 'The name is empty', 'tainacan' ), - 'validation' => v::stringType()->notEmpty(), - ], - 'parent' => [ - 'map' => 'parent', - 'title' => __( 'Parent', 'tainacan' ), - 'type' => 'integer', - 'description' => __( 'The parent of the term', 'tainacan' ), - 'default' => 0, - 'validation' => '' - ], - 'description' => [ - 'map' => 'description', - 'title' => __( 'Description', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'The term description', 'tainacan' ), - 'default' => '', - 'validation' => '' - ], - 'taxonomy' => [ - 'map' => 'taxonomy', - 'title' => __( 'Taxonomy', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'The term taxonomy', 'tainacan' ), - 'on_error' => __( 'The taxonomy is empty', 'tainacan' ), - 'validation' => v::stringType()->notEmpty(), - ], - 'user' => [ - 'map' => 'termmeta', - 'title' => __( 'User', 'tainacan' ), - 'type' => 'integer', - 'description' => __( 'The term creator', 'tainacan' ), - 'on_error' => __( 'The user is empty or invalid', 'tainacan' ), - 'default' => get_current_user_id(), - 'validation' => v::numeric(), - ], - 'header_image_id' => [ - 'map' => 'termmeta', - 'title' => __( 'Header Image', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'The image to be used in term header', 'tainacan' ), - 'on_error' => __( 'Invalid image', 'tainacan' ), - //'validation' => v::numeric(), - 'default' => '' - ], - 'hide_empty' => [ - 'map' => 'hide_empty', - 'title' => __( 'Hide empty', 'tainacan' ), - 'type' => 'bool', - 'description' => __( 'Hide empty terms', 'tainacan' ) - ] - ] ); - } - - public function get_default_properties($map) { - $defaults = parent::get_default_properties($map); - //its uses the term_id and not id - unset($defaults['id']); - return $defaults; - } + public function get_map() { + return apply_filters( 'tainacan-get-map-' . $this->get_name(), [ + 'term_id' => [ + 'map' => 'term_id', + 'title' => __( 'ID', 'tainacan' ), + 'type' => 'integer', + 'description' => __( 'Unique identifier', 'tainacan' ), + //'validation' => '' + ], + 'name' => [ + 'map' => 'name', + 'title' => __( 'Name', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'Name of the term', 'tainacan' ), + 'on_error' => __( 'The name is empty', 'tainacan' ), + 'validation' => v::stringType()->notEmpty(), + ], + 'parent' => [ + 'map' => 'parent', + 'title' => __( 'Parent', 'tainacan' ), + 'type' => 'integer', + 'description' => __( 'The parent of the term', 'tainacan' ), + 'default' => 0, + 'validation' => '' + ], + 'description' => [ + 'map' => 'description', + 'title' => __( 'Description', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The term description', 'tainacan' ), + 'default' => '', + 'validation' => '' + ], + 'taxonomy' => [ + 'map' => 'taxonomy', + 'title' => __( 'Taxonomy', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The term taxonomy', 'tainacan' ), + 'on_error' => __( 'The taxonomy is empty', 'tainacan' ), + 'validation' => v::stringType()->notEmpty(), + ], + 'user' => [ + 'map' => 'termmeta', + 'title' => __( 'User', 'tainacan' ), + 'type' => 'integer', + 'description' => __( 'The term creator', 'tainacan' ), + 'on_error' => __( 'The user is empty or invalid', 'tainacan' ), + 'default' => get_current_user_id(), + 'validation' => v::numeric(), + ], + 'header_image_id' => [ + 'map' => 'termmeta', + 'title' => __( 'Header Image', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'The image to be used in term header', 'tainacan' ), + 'on_error' => __( 'Invalid image', 'tainacan' ), + //'validation' => v::numeric(), + 'default' => '' + ], + 'hide_empty' => [ + 'map' => 'hide_empty', + 'title' => __( 'Hide empty', 'tainacan' ), + 'type' => 'bool', + 'description' => __( 'Hide empty terms', 'tainacan' ) + ] + ] ); + } + + public function get_default_properties( $map ) { + $defaults = parent::get_default_properties( $map ); + //its uses the term_id and not id + unset( $defaults['id'] ); + + return $defaults; + } /** * @param Entities\Entity $term @@ -108,155 +111,162 @@ class Terms extends Repository { * @return Entities\Entity|Entities\Term * @throws \Exception */ - public function insert($term){ + public function insert( $term ) { $is_update = false; - $diffs = []; + $diffs = []; if ( $term->get_id() ) { $is_update = true; - $old = $this->fetch( $term->get_id(), $term->get_taxonomy() ); + $old = $this->fetch( $term->get_id(), $term->get_taxonomy() ); - $diffs = $this->diff($old, $term); + $diffs = $this->diff( $old, $term ); } - // First iterate through the native post properties - $map = $this->get_map(); - foreach ($map as $prop => $mapped) { - if ($mapped['map'] != 'termmeta') { - $term->WP_Term->{$mapped['map']} = $term->get_mapped_property($prop); - } - } + // First iterate through the native post properties + $map = $this->get_map(); + foreach ( $map as $prop => $mapped ) { + if ( $mapped['map'] != 'termmeta' ) { + $term->WP_Term->{$mapped['map']} = $term->get_mapped_property( $prop ); + } + } - // save post and get its ID - if(isset($term->WP_Term->term_id)){ + // save post and get its ID + if ( isset( $term->WP_Term->term_id ) ) { - $args = []; - foreach ($map as $prop => $mapped) { - if ($mapped['map'] != 'termmeta') { - $get_ = 'get_'. $prop; + $args = []; + foreach ( $map as $prop => $mapped ) { + if ( $mapped['map'] != 'termmeta' ) { + $get_ = 'get_' . $prop; - if( !empty($term->WP_Term->{$mapped['map']}) ){ - $args[$mapped['map']] = $term->$get_(); + if ( ! empty( $term->WP_Term->{$mapped['map']} ) ) { + $args[ $mapped['map'] ] = $term->$get_(); } - } - } + } + } - $term_saved = wp_update_term( $term->get_id(), $term->get_taxonomy(), $args); - } else { - $term_saved = wp_insert_term( $term->get_name(), $term->get_taxonomy(), [ - 'parent' => $term->get_parent(), - 'description' => $term->get_description(), - ] ); - } + $term_saved = wp_update_term( $term->get_id(), $term->get_taxonomy(), $args ); + } else { + $term_saved = wp_insert_term( $term->get_name(), $term->get_taxonomy(), [ + 'parent' => $term->get_parent(), + 'description' => $term->get_description(), + ] ); + } - // Now run through properties stored as postmeta - foreach ($map as $prop => $mapped) { - if ($mapped['map'] == 'termmeta') { - update_term_meta($term_saved['term_id'], $prop, wp_slash( $term->get_mapped_property($prop) )); - } - } + // Now run through properties stored as postmeta + foreach ( $map as $prop => $mapped ) { + if ( $mapped['map'] == 'termmeta' ) { + update_term_meta( $term_saved['term_id'], $prop, wp_slash( $term->get_mapped_property( $prop ) ) ); + } + } - // TODO: Log header image updates + // TODO: Log header image updates + $this->logs_repository->insert_log( $term, $diffs, $is_update ); - do_action('tainacan-insert', $term, $diffs, $is_update); - do_action('tainacan-insert-Term', $term); + do_action( 'tainacan-insert', $term, $diffs, $is_update ); + do_action( 'tainacan-insert-Term', $term ); - return new Entities\Term($term_saved['term_id'], $term->get_taxonomy()); - } + return new Entities\Term( $term_saved['term_id'], $term->get_taxonomy() ); + } - /** - * fetch terms based on ID or get terms args - * - * Terms are stored as WordPress regular terms. Check (@see https://developer.wordpress.org/reference/functions/get_terms/) get_terms() docs - * to learn all args accepted in the $args parameter - * - * The second paramater specifies from which taxonomies terms should be fetched. - * You can pass the Taxonomy ID or object, or an Array of IDs or taxonomies objects - * - * @param array $args WP_Query args || int $args the term id - * @param array $taxonomies Array Entities\Taxonomy || Array int terms IDs || int collection id || Entities\Taxonomy taxonomy object - * @return array of Entities\Term objects || Entities\Term - */ - public function fetch( $args = [], $taxonomies = []){ + /** + * fetch terms based on ID or get terms args + * + * Terms are stored as WordPress regular terms. Check (@see https://developer.wordpress.org/reference/functions/get_terms/) get_terms() docs + * to learn all args accepted in the $args parameter + * + * The second paramater specifies from which taxonomies terms should be fetched. + * You can pass the Taxonomy ID or object, or an Array of IDs or taxonomies objects + * + * @param array $args WP_Query args || int $args the term id + * @param array $taxonomies Array Entities\Taxonomy || Array int terms IDs || int collection id || Entities\Taxonomy taxonomy object + * + * @return array of Entities\Term objects || Entities\Term + */ + public function fetch( $args = [], $taxonomies = [] ) { - $Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance(); + $Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance(); - if (is_numeric($taxonomies)){ + if ( is_numeric( $taxonomies ) ) { $taxonomies = $Tainacan_Taxonomies->fetch( $taxonomies ); } - if ( $taxonomies instanceof Entities\Taxonomy ) { - $cpt = $taxonomies->get_db_identifier(); - } elseif (is_array( $taxonomies )) { - $cpt = []; + if ( $taxonomies instanceof Entities\Taxonomy ) { + $cpt = $taxonomies->get_db_identifier(); + } elseif ( is_array( $taxonomies ) ) { + $cpt = []; - foreach ($taxonomies as $taxonomy) { - if (is_numeric($taxonomy)){ - $taxonomy = $Tainacan_Taxonomies->fetch( $taxonomy ); - } - if ($taxonomy instanceof Entities\Taxonomy){ - $cpt[] = $taxonomy->get_db_identifier(); - } - } + foreach ( $taxonomies as $taxonomy ) { + if ( is_numeric( $taxonomy ) ) { + $taxonomy = $Tainacan_Taxonomies->fetch( $taxonomy ); + } + if ( $taxonomy instanceof Entities\Taxonomy ) { + $cpt[] = $taxonomy->get_db_identifier(); + } + } - } elseif (is_string($taxonomies) && is_numeric($args)){ // if taxonomy is taxonomy_db_identifier - $cpt = $taxonomies; + } elseif ( is_string( $taxonomies ) && is_numeric( $args ) ) { // if taxonomy is taxonomy_db_identifier + $cpt = $taxonomies; - $term = get_term_by('id', $args, $cpt); + $term = get_term_by( 'id', $args, $cpt ); - return new Entities\Term($term); - } + return new Entities\Term( $term ); + } - if(is_array( $args ) && !empty( $cpt ) ){ // if an array of arguments is - $terms = get_terms( $cpt, $args ); - $return = []; + if ( is_array( $args ) && ! empty( $cpt ) ) { // if an array of arguments is + $terms = get_terms( $cpt, $args ); + $return = []; - foreach ($terms as $term) { - $tainacan_term = new Entities\Term( $term ); - $tainacan_term->set_user( get_term_meta($tainacan_term->get_id() , 'user', true ) ); - $return[] = $tainacan_term; - } - return $return; - } elseif( is_numeric($args) && !empty($cpt) && !is_array( $cpt ) ){ // if an id is passed taxonomy cannot be an array - $wp_term = get_term_by('id', $args, $cpt); - $tainacan_term = new Entities\Term( $wp_term ); - $tainacan_term->set_user( get_term_meta($tainacan_term->get_id() , 'user', true ) ); + foreach ( $terms as $term ) { + $tainacan_term = new Entities\Term( $term ); + $tainacan_term->set_user( get_term_meta( $tainacan_term->get_id(), 'user', true ) ); + $return[] = $tainacan_term; + } - return $tainacan_term; - } else{ - return []; - } - } + return $return; + } elseif ( is_numeric( $args ) && ! empty( $cpt ) && ! is_array( $cpt ) ) { // if an id is passed taxonomy cannot be an array + $wp_term = get_term_by( 'id', $args, $cpt ); + $tainacan_term = new Entities\Term( $wp_term ); + $tainacan_term->set_user( get_term_meta( $tainacan_term->get_id(), 'user', true ) ); - public function update($object, $args = null){ - return $this->insert($object); - } + return $tainacan_term; + } else { + return []; + } + } + + public function update( $object, $args = null ) { + return $this->insert( $object ); + } /** * @param Array $delete_args has ['term_id', 'taxonomy'] * * @return bool|int|mixed|\WP_Error */ - public function delete($delete_args){ - $deleted = wp_delete_term($delete_args['term_id'], $delete_args['taxonomy']); + public function delete( $delete_args ) { + $deleted = wp_delete_term( $delete_args['term_id'], $delete_args['taxonomy'] ); - if($deleted) { - $deleted_term_tainacan = new Entities\Term($delete_args['term_id'], $delete_args['taxonomy']); + if ( $deleted ) { + $deleted_term_tainacan = new Entities\Term( $delete_args['term_id'], $delete_args['taxonomy'] ); - do_action( 'tainacan-deleted', $deleted_term_tainacan, [], false, true ); - } + $this->logs_repository->insert_log( $deleted_term_tainacan, [], false, true ); - return $deleted; - } + do_action( 'tainacan-deleted', $deleted_term_tainacan ); + } + + return $deleted; + } /** * @param $term_id * * @return mixed|void */ - public function trash($term_id){} - - public function register_post_type() { } + public function trash( $term_id ) { + } + + public function register_post_type() { + } } \ No newline at end of file