Now repository methods (delete, insert, update) call explicitly the insert_logs method (issue #89)

This commit is contained in:
weryques 2018-08-13 14:59:16 -03:00
parent fc9e62e585
commit 52848190d3
9 changed files with 1938 additions and 1832 deletions

View File

@ -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' )
@ -268,7 +268,7 @@ class Collections extends Repository {
$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 );
}
}

View File

@ -1,150 +1,153 @@
<?php
namespace Tainacan\Repositories;
use Tainacan\Entities;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
use \Respect\Validation\Validator as v;
class Filters extends Repository {
public $entities_type = '\Tainacan\Entities\Filter';
public $filters_types = [];
public $filters_types = [];
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();
}
protected function __construct() {
parent::__construct();
}
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
]
]);
}
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 [];
}
public function update( $object, $new_values = null ) {
return $this->insert( $object );
}
} elseif (is_array($args)) {
// TODO: get filters from parent collections
$args = array_merge([
'posts_per_page' => -1,
'post_status' => 'publish'
], $args);
/**
* 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 [];
}
$args = $this->parse_fetch_args($args);
} elseif ( is_array( $args ) ) {
// TODO: get filters from parent collections
$args = array_merge( [
'posts_per_page' => - 1,
'post_status' => 'publish'
], $args );
$args['post_type'] = Entities\Filter::get_post_type();
$args = $this->parse_fetch_args( $args );
$args = apply_filters('tainacan_fetch_args', $args, 'filters');
$args['post_type'] = Entities\Filter::get_post_type();
$wp_query = new \WP_Query($args);
return $this->fetch_output($wp_query, $output);
}
}
$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;
}
}

View File

@ -1,27 +1,28 @@
<?php
namespace Tainacan\Repositories;
use Tainacan\Entities;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
class Item_Metadata extends Repository {
protected function __construct() {
parent::__construct();
}
protected function __construct() {
parent::__construct();
}
public $entities_type = '\Tainacan\Entities\Item_Metadata_Entity';
public $entities_type = '\Tainacan\Entities\Item_Metadata_Entity';
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;
}
/**
* @param Entities\Entity $item_metadata
@ -29,48 +30,48 @@ class Item_Metadata extends Repository {
* @return Entities\Entity|Entities\Item_Metadata_Entity
* @throws \Exception
*/
public function insert($item_metadata) {
public function insert( $item_metadata ) {
if ( ! $item_metadata->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);
}
$diffs = $this->diff( $old, $item_metadata );
}
$unique = !$item_metadata->is_multiple();
$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 ( $unique ) {
if (is_int($item_metadata->get_meta_id())) {
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 {
@ -80,105 +81,110 @@ class Item_Metadata extends Repository {
* 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());
} 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();
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 ));
}
}
}
foreach ( $values as $value ) {
add_post_meta( $item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_id(), wp_slash( $value ) );
}
}
}
do_action('tainacan-insert', $item_metadata, $diffs, $is_update);
do_action('tainacan-insert-Item_Metadata_Entity', $item_metadata);
$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());
$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_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;
}
}
/**
* @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 );
}
}
}
}
@ -186,17 +192,19 @@ class Item_Metadata extends Repository {
*
* @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_object( $current_value ) ) {
$current_value = $current_value->meta_value;
}
if ( !is_array($current_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;
}
@ -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();
public function fetch( $object, $output = null, $args = [] ) {
if ( $object instanceof Entities\Item ) {
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$collection = $object->get_collection();
$collection = $object->get_collection();
if (!$collection instanceof Entities\Collection){
return [];
}
if ( ! $collection instanceof Entities\Collection ) {
return [];
}
$meta_list = $Tainacan_Metadata->fetch_by_collection($collection, $args, 'OBJECT' );
$meta_list = $Tainacan_Metadata->fetch_by_collection( $collection, $args, 'OBJECT' );
$return = [];
$return = [];
if (is_array($meta_list)) {
foreach ($meta_list as $meta) {
$return[] = new Entities\Item_Metadata_Entity($object, $meta);
}
}
if ( is_array( $meta_list ) ) {
foreach ( $meta_list as $meta ) {
$return[] = new Entities\Item_Metadata_Entity( $object, $meta );
}
}
return $return;
}else{
return [];
}
}
return $return;
} else {
return [];
}
}
/**
* Get the value for a Item metadatum.
@ -251,42 +259,44 @@ class Item_Metadata extends Repository {
* @return mixed
* @throws \Exception
*/
public function get_value(Entities\Item_Metadata_Entity $item_metadata) {
$unique = ! $item_metadata->is_multiple();
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();
$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();
$get_method = 'get_' . $metadata_type->get_related_mapped_prop();
} elseif ($metadata_type->get_primitive_type() == 'term') {
return $item->$get_method();
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;
}
} elseif ( $metadata_type->get_primitive_type() == 'term' ) {
$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_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;
@ -294,19 +304,19 @@ class Item_Metadata extends Repository {
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()),
$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)) {
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']);
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;
@ -319,19 +329,19 @@ class Item_Metadata extends Repository {
return $return_value;
} else {
if (is_int($item_metadata->get_meta_id())) {
} 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,21 +349,21 @@ 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 );
}
}
@ -363,26 +373,36 @@ class Item_Metadata extends Repository {
}
public function register_post_type() { }
public function register_post_type() {
}
public function get_map() { return []; }
public function get_default_properties($map) { return []; }
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' );
}
}

View File

@ -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,7 +259,7 @@ 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 );
@ -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,85 +314,92 @@ 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;
}
// 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;
}
/**
* 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;
}
/**
* 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);
$thumb_id = apply_filters( 'tainacan-get-thumbnail-id-from-document', null, $item );
if (!is_null($thumb_id)) {
if ( ! is_null( $thumb_id ) ) {
return $thumb_id;
}
if ( empty($item->get_document()) ) {
if ( empty( $item->get_document() ) ) {
return null;
}
@ -392,11 +408,12 @@ class Items extends Repository {
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;
}
@ -407,14 +424,15 @@ class Items extends Repository {
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;
}
}
@ -428,18 +446,19 @@ class Items extends Repository {
* 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 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())
) {
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);
$thumb_id = $this->get_thumbnail_id_from_document( $updated_item );
if (!is_null($thumb_id)) {
if ( ! is_null( $thumb_id ) ) {
set_post_thumbnail( $updated_item->get_id(), (int) $thumb_id );
}

View File

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

File diff suppressed because it is too large Load Diff

View File

@ -17,10 +17,18 @@ 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,12 +114,12 @@ 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() );
@ -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,25 +216,25 @@ 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 ) );
}
@ -335,8 +352,8 @@ 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'];
}
}
@ -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 ) {
@ -563,16 +580,16 @@ abstract class Repository {
*
* @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');
$results = $this->fetch( $args, 'OBJECT' );
if (is_array($results) && sizeof($results) > 0 && $results[0] instanceof \Tainacan\Entities\Entity) {
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;

View File

@ -1,251 +1,258 @@
<?php
namespace Tainacan\Repositories;
use Tainacan\Entities;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
use \Respect\Validation\Validator as v;
/**
* Class Tainacan_Taxonomies
*/
class Taxonomies extends Repository {
public $entities_type = '\Tainacan\Entities\Taxonomy';
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_action('tainacan-taxonomy-removed-from-collection', array($this, 'removed_collection'), 10, 2);
add_action('tainacan-taxonomy-added-to-collection', array($this, 'added_collection'), 10, 2);
add_action( 'tainacan-taxonomy-removed-from-collection', array( $this, 'removed_collection' ), 10, 2 );
add_action( 'tainacan-taxonomy-added-to-collection', array( $this, 'added_collection' ), 10, 2 );
}
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' => ''
],
]);
}
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();
$new_taxonomy = parent::insert( $taxonomy );
$new_taxonomy->tainacan_register_taxonomy();
// return a brand new object
return $new_taxonomy;
}
// return a brand new object
return $new_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 ) {
/**
* 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
// 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)) {
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 = array_merge([
'posts_per_page' => -1,
], $args);
$args = array_merge( [
'posts_per_page' => - 1,
], $args );
$args = $this->parse_fetch_args($args);
$args = $this->parse_fetch_args( $args );
$args['post_type'] = Entities\Taxonomy::get_post_type();
$args['post_type'] = Entities\Taxonomy::get_post_type();
$args = apply_filters('tainacan_fetch_args', $args, 'taxonomies');
$args = apply_filters( 'tainacan_fetch_args', $args, 'taxonomies' );
$wp_query = new \WP_Query($args);
return $this->fetch_output($wp_query, $output);
}
}
$wp_query = new \WP_Query( $args );
public function update($object, $new_values = null){
return $this->insert($object);
}
return $this->fetch_output( $wp_query, $output );
}
}
public function delete($taxonomy_id){
$taxonomy_name = $this->fetch($taxonomy_id)->get_db_identifier();
public function update( $object, $new_values = null ) {
return $this->insert( $object );
}
/* TODO: Investigate the cause of taxonomies aren't been registered
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;
}
do_action( 'tainacan-trashed', $trashed );
public function added_collection($taxonomy_id, $collection) {
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 );
}
}
}

View File

@ -1,19 +1,21 @@
<?php
namespace Tainacan\Repositories;
use Tainacan\Entities;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
use \Respect\Validation\Validator as v;
/**
* Class Tainacan_Terms
*/
* Class Tainacan_Terms
*/
class Terms extends Repository {
public $entities_type = '\Tainacan\Entities\Term';
private static $instance = null;
private static $instance = null;
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
@ -27,80 +29,81 @@ class Terms extends Repository {
parent::__construct();
}
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_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;
}
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 trash( $term_id ) {
}
public function register_post_type() { }
public function register_post_type() {
}
}