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

@ -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;
@ -302,7 +304,9 @@ class Collections extends Repository {
$trashed = new Entities\Collection( wp_trash_post( $collection_id ) );
if ( $trashed ) {
do_action( 'tainacan-trashed', $trashed, [], false, false, true );
$this->logs_repository->insert_log( $trashed, [], false, false, true );
do_action( 'tainacan-trashed', $trashed );
}
return $trashed;
@ -363,6 +367,7 @@ class Collections extends Repository {
if ( is_numeric( $id ) ) {
return (int) $id;
}
return false;
}

View File

@ -1,11 +1,13 @@
<?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 = [];
@ -98,6 +100,7 @@ class Filters extends Repository {
/**
* 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() {
@ -196,7 +199,9 @@ class Filters extends Repository {
$deleted = new Entities\Filter( wp_delete_post( $filter_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;
@ -211,7 +216,9 @@ class Filters extends Repository {
$trashed = new Entities\Filter( wp_trash_post( $filter_id ) );
if ( $trashed ) {
do_action('tainacan-trashed', $trashed, [], false, false, true);
$this->logs_repository->insert_log( $trashed, [], false, false, true );
do_action( 'tainacan-trashed', $trashed );
}
return $trashed;
@ -231,6 +238,7 @@ class Filters extends Repository {
*
* @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 ) {
@ -256,6 +264,7 @@ class Filters extends Repository {
$args = apply_filters( 'tainacan_fetch_args', $args, 'filters' );
$wp_query = new \WP_Query( $args );
return $this->fetch_output( $wp_query, $output );
}
}
@ -300,6 +309,7 @@ class Filters extends Repository {
* 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' ) {
@ -322,6 +332,7 @@ class Filters extends Repository {
* 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 ) {
@ -393,6 +404,7 @@ class Filters extends Repository {
*
* @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 ) {
@ -428,8 +440,7 @@ class Filters extends Repository {
$result_ordinate = array_merge( $result_ordinate, $not_ordinate );
return $result_ordinate;
}
// if the result is a wp query object
} // if the result is a wp query object
else {
$posts = $result->posts;
$result_ordinate = [];
@ -453,6 +464,7 @@ class Filters extends Repository {
return $result;
}
}
return $result;
}
}

View File

@ -1,6 +1,7 @@
<?php
namespace Tainacan\Repositories;
use Tainacan\Entities;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
@ -101,6 +102,8 @@ class Item_Metadata extends Repository {
}
}
$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 );
}
@ -124,14 +127,16 @@ class Item_Metadata extends Repository {
*
* @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
@ -176,7 +181,8 @@ class Item_Metadata extends Repository {
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 );
}
}
}
@ -190,11 +196,13 @@ class Item_Metadata extends Repository {
$current_value = get_metadata_by_mid( 'post', $item_metadata->get_parent_meta_id() );
if (is_object($current_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 ) ) {
$current_value[] = $meta_id;
@ -259,6 +267,7 @@ class Item_Metadata extends Repository {
$item = $item_metadata->get_item();
$get_method = 'get_' . $metadata_type->get_related_mapped_prop();
return $item->$get_method();
} elseif ( $metadata_type->get_primitive_type() == 'term' ) {
@ -279,9 +288,10 @@ class Item_Metadata extends Repository {
if ( $unique ) {
$terms = reset( $terms );
if (false !== $terms)
if ( false !== $terms ) {
$terms = new Entities\Term( $terms );
}
}
if ( is_array( $terms ) ) {
$terms_array = [];
@ -339,12 +349,12 @@ 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 = [];
@ -353,7 +363,7 @@ class Item_Metadata extends Repository {
$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 );
$return_value[ $metadatum->get_id() ] = new Entities\Item_Metadata_Entity( $item, $metadatum, $id, $compound_meta_id );
}
}
@ -363,14 +373,22 @@ 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 ) {
@ -379,7 +397,9 @@ class Item_Metadata extends Repository {
/**
* 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 ) {

View File

@ -144,7 +144,14 @@ class Items extends Repository {
$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;
@ -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;
@ -309,7 +318,9 @@ class Items extends Repository {
$trashed = new Entities\Item( wp_trash_post( $item_id ) );
if ( $trashed ) {
do_action( 'tainacan-trashed', $trashed, [], false, false, true );
$this->logs_repository->insert_log( $trashed, [], false, false, true );
do_action( 'tainacan-trashed', $trashed );
}
return $trashed;
@ -320,6 +331,7 @@ class Items extends Repository {
*
* @param $where
* @param $wp_query
*
* @return string
*/
public function title_in_posts_where( $where, $wp_query ) {
@ -338,6 +350,7 @@ class Items extends Repository {
$where .= ' ' . $post_title_in['relation'] . '( ( ' . implode( ' OR ', $quotes ) . ' ) AND ' .
$status . ' AND ' . $type . ' )';
}
return $where;
}
@ -346,6 +359,7 @@ class Items extends Repository {
*
* @param $where
* @param $wp_query
*
* @return string
*/
public function content_in_posts_where( $where, $wp_query ) {
@ -364,6 +378,7 @@ class Items extends Repository {
$where .= ' ' . $post_content_in['relation'] . '( ( ' . implode( ' OR ', $quotes ) . ' ) AND ' .
$status . ' AND ' . $type . ' )';
}
return $where;
}
@ -371,6 +386,7 @@ class Items extends Repository {
* 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 ) {
@ -397,6 +413,7 @@ class Items extends Repository {
$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() );
return $thumb_id;
}
@ -415,6 +432,7 @@ class Items extends Repository {
$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 );
return $thumb_id;
}
}
@ -429,6 +447,7 @@ class Items extends Repository {
*
* @param Entities\Item $updated_item
* @param array $attributes The paramaters sent to the API
*
* @return void
*/
public function hook_api_updated_item( Entities\Item $updated_item, $attributes ) {

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 );
}
@ -208,8 +205,11 @@ 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 );
@ -368,7 +368,7 @@ class Logs extends Repository {
$collection = $object->get_collection();
if ( $collection ) {
$parent = '(parent '. $collection->get_name() .')';
$parent = '(parent: ' . $collection->get_name() . ')';
} else {
$parent = '(on repository level)';
}

View File

@ -1,11 +1,13 @@
<?php
namespace Tainacan\Repositories;
use Tainacan\Entities;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
use \Respect\Validation\Validator as v;
/**
* Class Metadata
*/
@ -200,6 +202,7 @@ class Metadata extends Repository {
/**
* 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() {
@ -328,6 +331,7 @@ class Metadata extends Repository {
$args = apply_filters( 'tainacan_fetch_args', $args, 'metadata' );
$wp_query = new \WP_Query( $args );
return $this->fetch_output( $wp_query, $output );
}
}
@ -342,6 +346,7 @@ class Metadata extends Repository {
* @param array $args WP_Query args || int $args the item id
*
* @return Array array of IDs;
* @throws \Exception
*/
public function fetch_ids( $args = [] ) {
@ -450,6 +455,7 @@ class Metadata extends Repository {
* @param \WP_Query|array $result Response from method fetch
* @param Entities\Collection $collection
* @param bool $include_disabled Wether to include disabled metadata in the results or not
*
* @return array or WP_Query ordinate
*/
public function order_result( $result, Entities\Collection $collection, $include_disabled = false ) {
@ -485,8 +491,7 @@ class Metadata extends Repository {
$result_ordinate = array_merge( $result_ordinate, $not_ordinate );
return $result_ordinate;
}
// if the result is a wp query object
} // if the result is a wp query object
else {
$posts = $result->posts;
$result_ordinate = [];
@ -510,11 +515,13 @@ class Metadata extends Repository {
return $result;
}
}
return $result;
}
/**
* @param \Tainacan\Entities\Metadatum $metadatum
*
* @return \Tainacan\Entities\Metadatum
* {@inheritDoc}
* @see \Tainacan\Repositories\Repository::insert()
@ -524,6 +531,7 @@ class Metadata extends Repository {
$new_metadatum = parent::insert( $metadatum );
$this->update_taxonomy_metadatum( $new_metadatum );
return $new_metadatum;
}
@ -548,7 +556,9 @@ class Metadata extends Repository {
$deleted = new Entities\Metadatum( wp_delete_post( $metadatum_id, true ) );
if ( $deleted ) {
do_action( 'tainacan-deleted', $deleted, [], false, true );
$this->logs_repository->insert_log( $deleted, [], false, true );
do_action( 'tainacan-deleted', $deleted );
}
return $deleted;
@ -566,7 +576,9 @@ class Metadata extends Repository {
$trashed = new Entities\Metadatum( wp_trash_post( $metadatum_id ) );
if ( $trashed ) {
do_action( 'tainacan-trashed', $trashed, [], false, false, true );
$this->logs_repository->insert_log( $trashed, [], false, false, true );
do_action( 'tainacan-trashed', $trashed );
}
return $trashed;
@ -580,6 +592,7 @@ class Metadata extends Repository {
* NAME - return an Array of the names of metadatum types registered
*
* @param $output string CLASS | NAME
*
* @return array of Entities\Metadata_Types\Metadata_Type classes path name
*/
public function fetch_metadata_types( $output = 'CLASS' ) {
@ -801,6 +814,7 @@ class Metadata extends Repository {
if ( is_array( $results ) && sizeof( $results ) == 1 && $results[0] instanceof \Tainacan\Entities\Metadatum ) {
return $results[0];
}
return false;
}
@ -827,6 +841,7 @@ class Metadata extends Repository {
if ( is_array( $results ) && sizeof( $results ) == 1 && $results[0] instanceof \Tainacan\Entities\Metadatum ) {
return $results[0];
}
return false;
}
@ -848,6 +863,7 @@ class Metadata extends Repository {
if ( $metadatum->validate() ) {
$metadatum = $this->insert( $metadatum );
return $metadatum->get_id();
} else {
throw new \ErrorException( 'The entity wasn\'t validated.' . print_r( $metadatum->get_errors(), true ) );

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
*
@ -164,9 +178,12 @@ abstract class Repository {
// TODO: Logs for header image insert and update
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 );
}

View File

@ -1,12 +1,14 @@
<?php
namespace Tainacan\Repositories;
use Tainacan\Entities;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
use \Respect\Validation\Validator as v;
/**
* Class Tainacan_Taxonomies
*/
@ -15,10 +17,8 @@ class Taxonomies extends Repository {
private static $instance = null;
public static function get_instance()
{
if(!isset(self::$instance))
{
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
@ -77,6 +77,7 @@ class Taxonomies extends Repository {
/**
* 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() {
@ -149,6 +150,7 @@ class Taxonomies extends Repository {
*
* @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 ) {
@ -175,6 +177,7 @@ class Taxonomies extends Repository {
$args = apply_filters( 'tainacan_fetch_args', $args, 'taxonomies' );
$wp_query = new \WP_Query( $args );
return $this->fetch_output( $wp_query, $output );
}
}
@ -206,7 +209,9 @@ class Taxonomies extends Repository {
return $deleted;
}
do_action( 'tainacan-deleted', $deleted, [], false, true );
$this->logs_repository->insert_log( $deleted, [], false, true );
do_action( 'tainacan-deleted', $deleted );
return $deleted;
}
@ -223,7 +228,9 @@ class Taxonomies extends Repository {
return $trashed;
}
do_action('tainacan-trashed', $trashed, [], false, false, true );
$this->logs_repository->insert_log( $trashed, [], false, false, true );
do_action( 'tainacan-trashed', $trashed );
return $trashed;
}

View File

@ -1,11 +1,13 @@
<?php
namespace Tainacan\Repositories;
use Tainacan\Entities;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
use \Respect\Validation\Validator as v;
/**
* Class Tainacan_Terms
*/
@ -99,6 +101,7 @@ class Terms extends Repository {
$defaults = parent::get_default_properties( $map );
//its uses the term_id and not id
unset( $defaults['id'] );
return $defaults;
}
@ -159,6 +162,7 @@ class Terms extends Repository {
}
// 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 );
@ -177,6 +181,7 @@ class Terms extends Repository {
*
* @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 = [] ) {
@ -218,6 +223,7 @@ class Terms extends Repository {
$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 );
@ -245,7 +251,9 @@ class Terms extends Repository {
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 );
do_action( 'tainacan-deleted', $deleted_term_tainacan );
}
return $deleted;
@ -256,7 +264,9 @@ class Terms extends Repository {
*
* @return mixed|void
*/
public function trash($term_id){}
public function register_post_type() { }
public function trash( $term_id ) {
}
public function register_post_type() {
}
}