Solving errors

This commit is contained in:
andre2ar 2018-03-21 09:20:00 -03:00
parent 7555878a8f
commit e8835841a6
27 changed files with 163 additions and 78 deletions

View File

@ -7,8 +7,19 @@ namespace Tainacan;
class Admin {
private $menu_slug = 'tainacan_admin';
public function __construct() {
private static $instance = null;
public static function getInstance()
{
if(!isset(self::$instance))
{
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
add_action( 'admin_menu', array(&$this, 'add_admin_menu') );
add_filter( 'admin_body_class', array(&$this, 'admin_body_class') );
@ -66,7 +77,13 @@ class Admin {
}
function admin_page() {
global $TAINACAN_BASE_URL, $Tainacan_Collections, $Tainacan_Fields, $Tainacan_Filters, $Tainacan_Items, $Tainacan_Taxonomies;
global $TAINACAN_BASE_URL;
$Tainacan_Collections = \Tainacan\Repositories\Collections::getInstance();
$Tainacan_Fields = \Tainacan\Repositories\Fields::getInstance();
$Tainacan_Filters = \Tainacan\Repositories\Filters::getInstance();
$Tainacan_Items = \Tainacan\Repositories\Items::getInstance();
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::getInstance();
// TODO move it to a separate file and start the Vue project
echo "<div id='tainacan-admin-app'></div>";

View File

@ -270,11 +270,22 @@ class Capabilities {
]
],
];
private static $instance = null;
public static function getInstance()
{
if(!isset(self::$instance))
{
self::$instance = new self();
}
return self::$instance;
}
/**
* Register hooks
*/
function __construct() {
private function __construct() {
add_action('tainacan-insert-tainacan-collection', array(&$this, 'new_collection'));
add_action('tainacan-add-collection-moderators', array(&$this, 'add_moderators'), 10, 2);

View File

@ -9,7 +9,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
*/
class Collection extends Entity {
/**
/**
* {@inheritDoc}
* @see \Tainacan\Entities\Entity::post_type
* @var string
@ -20,7 +20,7 @@ class Collection extends Entity {
* @see \Tainacan\Entities\Entity::repository
* @var string
*/
protected $repository = 'Tainacan_Collections';
protected $repository = 'Collections';
/**
* Prefix used to create the db_identifier
@ -61,9 +61,9 @@ class Collection extends Entity {
* @return \WP_Post_Type|\WP_Error
*/
function register_collection_item_post_type() {
global ${$this->repository};
$repository = $this->get_repository();
$cpt_labels = ${$this->repository}->get_cpt_labels();
$cpt_labels = $repository->get_cpt_labels();
$cpt_labels['menu_name'] = $this->get_name();
@ -351,7 +351,7 @@ class Collection extends Entity {
* @throws \Exception
*/
function get_fields() {
$Tainacan_Fields = new \Tainacan\Repositories\Fields();
$Tainacan_Fields = \Tainacan\Repositories\Fields::getInstance();
return $Tainacan_Fields->fetch_by_collection( $this, [], 'OBJECT' );
}

View File

@ -124,7 +124,15 @@ class Entity {
}
}
}
public function get_repository()
{
$namespace = '\Tainacan\Repositories\\'.$this->repository;
$repository = $namespace::getInstance();
return $repository;
}
/**
* return the value for a mapped property
* @param string $prop id of property
@ -135,8 +143,9 @@ class Entity {
return $this->$prop;
}
//prop is not set at object, try to get from database
global ${$this->repository};
return ${$this->repository}->get_mapped_property($this, $prop);
$repository = $this->get_repository();
return $repository->get_mapped_property($this, $prop);
}
/**
@ -166,9 +175,9 @@ class Entity {
* @return boolean
*/
public function validate() {
global ${$this->repository};
$map = ${$this->repository}->get_map();
$repository = $this->get_repository();
$map = $repository->get_map();
$is_valid = true;
@ -190,8 +199,8 @@ class Entity {
* @return boolean
*/
public function validate_prop($prop) {
global ${$this->repository};
$map = ${$this->repository}->get_map();
$repository = $this->get_repository();
$map = $repository->get_map();
$mapped = $map[$prop];
$is_valid = true;
@ -289,8 +298,8 @@ class Entity {
}
public function __toArray(){
global ${$this->repository};
$map = ${$this->repository}->get_map();
$repository = $this->get_repository();
$map = $repository->get_map();
$attributes = [];
foreach($map as $prop => $content) {
@ -310,8 +319,8 @@ class Entity {
* @return bool
*/
public function can_read($user = null) {
global ${$this->repository};
return ${$this->repository}->can_read($this, $user);
$repository = $this->get_repository();
return $repository->can_read($this, $user);
}
/**
@ -320,8 +329,8 @@ class Entity {
* @return bool
*/
public function can_edit($user = null) {
global ${$this->repository};
return ${$this->repository}->can_edit($this, $user);
$repository = $this->get_repository();
return $repository->can_edit($this, $user);
}
/**
@ -330,8 +339,8 @@ class Entity {
* @return bool
*/
public function can_delete($user = null) {
global ${$this->repository};
return ${$this->repository}->can_delete($this, $user);
$repository = $this->get_repository();
return $repository->can_delete($this, $user);
}
/**
@ -340,8 +349,8 @@ class Entity {
* @return bool
*/
public function can_publish($user = null) {
global ${$this->repository};
return ${$this->repository}->can_publish($this, $user);
$repository = $this->get_repository();
return $repository->can_publish($this, $user);
}
/**
@ -370,8 +379,8 @@ class Entity {
* @return array
*/
public function diff($which = 0) {
global ${$this->repository};
return ${$this->repository}->diff($which, $this);
$repository = $this->get_repository();
return $repository->diff($which, $this);
}
}

View File

@ -20,7 +20,7 @@ class Field extends Entity {
* @see \Tainacan\Entities\Entity::repository
* @var string
*/
protected $repository = 'Tainacan_Fields';
protected $repository = 'Fields';
public function __toString(){
return 'Hello, my name is '. $this->get_name();

View File

@ -17,7 +17,7 @@ class Filter extends Entity {
* @see \Tainacan\Entities\Entity::repository
* @var string
*/
protected $repository = 'Tainacan_Filters';
protected $repository = 'Filters';
public function __toString(){
return 'Hello, my name is '. $this->get_name();

View File

@ -14,7 +14,7 @@ class Item_Metadata_Entity extends Entity {
* @see \Tainacan\Entities\Entity::repository
* @var string
*/
protected $repository = 'Tainacan_Item_Metadata';
protected $repository = 'Item_Metadata';
function __construct(Item $item, Field $field) {
@ -187,7 +187,7 @@ class Item_Metadata_Entity extends Entity {
}
if ($this->is_collection_key()) {
$Tainacan_Items = new \Tainacan\Repositories\Items();
$Tainacan_Items = \Tainacan\Repositories\Items::getInstance();
$test = $Tainacan_Items->fetch([
'meta_query' => [

View File

@ -14,7 +14,7 @@ class Item extends Entity {
* @see \Tainacan\Entities\Entity::repository
* @var string
*/
protected $repository = 'Tainacan_Items';
protected $repository = 'Items';
/**
* {@inheritDoc}

View File

@ -14,7 +14,7 @@ class Log extends Entity {
* @see \Tainacan\Entities\Entity::repository
* @var string
*/
protected $repository = 'Tainacan_Logs';
protected $repository = 'Logs';
public function __construct($which=0) {
parent::__construct($which);
@ -226,7 +226,8 @@ class Log extends Entity {
* @see \Tainacan\Repositories\Logs::approve
*/
public function approve() {
global ${$this->repository};
return ${$this->repository}->approve($this);
$repository = $this->get_repository();
return $repository->approve($this);
}
}

View File

@ -29,7 +29,7 @@ class Taxonomy extends Entity {
* @see \Tainacan\Entities\Entity::repository
* @var string
*/
protected $repository = 'Tainacan_Taxonomies';
protected $repository = 'Taxonomies';
public function __toString(){
return 'Hello, my name is '. $this->get_name();

View File

@ -14,7 +14,7 @@ class Term extends Entity {
* @see \Tainacan\Entities\Entity::repository
* @var string
*/
protected $repository = 'Tainacan_Terms';
protected $repository = 'Terms';
/**
* Term constructor.

View File

@ -105,7 +105,7 @@ class Item_Metadata extends Repository {
*/
public function fetch($object, $output = null ){
if($object instanceof Entities\Item){
global $Tainacan_Items, $Tainacan_Fields;
$Tainacan_Fields = \Tainacan\Repositories\Fields::getInstance();
$collection = $object->get_collection();

View File

@ -125,7 +125,8 @@ class Items extends Repository {
*/
public function register_post_type() {
global $Tainacan_Collections, $Tainacan_Taxonomies;
$Tainacan_Collections = \Tainacan\Repositories\Collections::getInstance();
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::getInstance();
$collections = $Tainacan_Collections->fetch( [], 'OBJECT' );
$taxonomies = $Tainacan_Taxonomies->fetch( [], 'OBJECT' );

View File

@ -383,7 +383,14 @@ abstract class Repository {
throw new \Exception( 'Collection object not found for this post' );
}
} else {
global $Tainacan_Collections, $Tainacan_Fields, $Tainacan_Item_Metadata, $Tainacan_Filters, $Tainacan_Taxonomies, $Tainacan_Terms, $Tainacan_Logs;
$Tainacan_Collections = \Tainacan\Repositories\Collections::getInstance();
$Tainacan_Filters = \Tainacan\Repositories\Filters::getInstance();
$Tainacan_Logs = \Tainacan\Repositories\Logs::getInstance();
$Tainacan_Fields = \Tainacan\Repositories\Fields::getInstance();
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::getInstance();
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::getInstance();
$Tainacan_Terms = \Tainacan\Repositories\Terms::getInstance();
$tnc_globals = [
$Tainacan_Collections,
$Tainacan_Fields,
@ -421,7 +428,14 @@ abstract class Repository {
return $Tainacan_Items;
} else {
global $Tainacan_Collections, $Tainacan_Fields, $Tainacan_Item_Metadata, $Tainacan_Filters, $Tainacan_Taxonomies, $Tainacan_Terms, $Tainacan_Logs;
$Tainacan_Collections = \Tainacan\Repositories\Collections::getInstance();
$Tainacan_Fields = \Tainacan\Repositories\Fields::getInstance();
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::getInstance();
$Tainacan_Filters = \Tainacan\Repositories\Filters::getInstance();
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::getInstance();
$Tainacan_Terms = \Tainacan\Repositories\Terms::getInstance();
$Tainacan_Logs = \Tainacan\Repositories\Logs::getInstance();
$tnc_globals = [
$Tainacan_Collections,
$Tainacan_Fields,

View File

@ -8,8 +8,20 @@ class DevInterface {
var $repositories = [];
var $has_errors = false;
public function __construct() {
private static $instance = null;
public static function getInstance()
{
if(!isset(self::$instance))
{
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
add_action('add_meta_boxes', array(&$this, 'register_metaboxes'));
add_action('save_post', array(&$this, 'save_post'), 10, 2);
@ -17,7 +29,11 @@ class DevInterface {
add_filter('post_type_link', array(&$this, 'permalink_filter'), 10, 3);
global $Tainacan_Collections, $Tainacan_Filters, $Tainacan_Logs, $Tainacan_Fields, $Tainacan_Taxonomies;
$Tainacan_Collections = \Tainacan\Repositories\Collections::getInstance();
$Tainacan_Filters = \Tainacan\Repositories\Filters::getInstance();
$Tainacan_Logs = \Tainacan\Repositories\Logs::getInstance();
$Tainacan_Fields = \Tainacan\Repositories\Fields::getInstance();
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::getInstance();
$repositories = [$Tainacan_Collections, $Tainacan_Filters, $Tainacan_Logs, $Tainacan_Fields, $Tainacan_Taxonomies];
@ -214,8 +230,10 @@ class DevInterface {
function metadata_metabox() {
global $Tainacan_Collections, $Tainacan_Item_Metadata, $pagenow, $typenow, $post;
global $typenow, $post;
$Tainacan_Collections = \Tainacan\Repositories\Collections::getInstance();
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::getInstance();
$collections = $Tainacan_Collections->fetch([], 'OBJECT');
// get current collection
@ -284,7 +302,9 @@ class DevInterface {
}
function metadata_components_metabox() {
global $Tainacan_Collections, $Tainacan_Item_Metadata, $pagenow, $typenow, $post;
global $typenow, $post;
$Tainacan_Collections = \Tainacan\Repositories\Collections::getInstance();
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::getInstance();
$collections = $Tainacan_Collections->fetch([], 'OBJECT');
@ -488,8 +508,12 @@ class DevInterface {
// Check if post type is an item from a collection
// TODO: there should ve a method in the repository to find this out
// or I could try to initialize an entity and find out what type it is
global $Tainacan_Collections, $Tainacan_Items, $Tainacan_Fields, $Tainacan_Item_Metadata;
$Tainacan_Collections = \Tainacan\Repositories\Collections::getInstance();
$Tainacan_Fields = \Tainacan\Repositories\Fields::getInstance();
$Tainacan_Items = \Tainacan\Repositories\Items::getInstance();
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::getInstance();
$collections = $Tainacan_Collections->fetch([], 'OBJECT');
$cpts = [];
foreach($collections as $col) {

View File

@ -224,7 +224,9 @@ abstract class Importer {
* @return Tainacan\Entities\Item Item inserted
*/
public function insert( $index, $processed_item ){
global $Tainacan_Items, $Tainacan_Item_Metadata, $Tainacan_Fields;
$Tainacan_Fields = \Tainacan\Repositories\Fields::getInstance();
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::getInstance();
$Tainacan_Items = \Tainacan\Repositories\Items::getInstance();
$isUpdate = ( is_array( $this->processed_items ) && isset( $this->processed_items[ $index ] ) )
? $this->processed_items[ $index ] : 0;

View File

@ -19,20 +19,18 @@ require_once(CLASSES_DIR . 'tainacan-creator.php');
require_once(API_DIR . 'tainacan-rest-creator.php');
require_once('dev-interface/class-tainacan-dev-interface.php');
$Tainacan_Dev_interface = new \Tainacan\DevInterface\DevInterface();
$Tainacan_Dev_interface = \Tainacan\DevInterface\DevInterface::getInstance();
function tnc_enable_dev_wp_interface() {
//return defined('TNC_ENABLE_DEV_WP_INTERFACE') && true === TNC_ENABLE_DEV_WP_INTERFACE ? true : false;
/*return defined('TNC_ENABLE_DEV_WP_INTERFACE') && true === TNC_ENABLE_DEV_WP_INTERFACE ? true : false;*/
}
global $Tainacan_Capabilities;
$Tainacan_Capabilities = new \Tainacan\Capabilities();
$Tainacan_Capabilities = \Tainacan\Capabilities::getInstance();
register_activation_hook( __FILE__, array( $Tainacan_Capabilities, 'init' ) );
// TODO move it somewhere else?
require_once('admin/class-tainacan-admin.php');
global $Tainacan_Admin;
$Tainacan_Admin = new \Tainacan\Admin();
$Tainacan_Admin = \Tainacan\Admin::getInstance();
function tainacan_load_plugin_textdomain() {
load_plugin_textdomain( 'tainacan', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );

View File

@ -55,7 +55,7 @@ class Entity_Factory {
}
$this->entity = new $this->entity_type();
$this->repository = new $this->repository_type();
$this->repository = $this->repository_type::getInstance();
if($publish) {
$this->entity->set_status('publish');

View File

@ -53,7 +53,6 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_UnitApiTestCase {
}
public function test_create_suggestion_item_metadata_in_a_collection(){
global $Tainacan_Fields, $Tainacan_Item_Metadata;
extract($this->create_meta_requirements());
@ -125,7 +124,6 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_UnitApiTestCase {
function test_create_anonymous_suggestion_item_metadata_in_a_collection() {
extract($this->create_meta_requirements());
global $Tainacan_Fields, $Tainacan_Item_Metadata;
$item__metadata_json = json_encode([
'values' => 'TestValues_metadado',

View File

@ -17,8 +17,10 @@ class CategoryFieldTypes extends TAINACAN_UnitTestCase {
function test_category_field_types() {
global $Tainacan_Item_Metadata, $Tainacan_Items, $Tainacan_Fields, $Tainacan_Terms;
$Tainacan_Fields = \Tainacan\Repositories\Fields::getInstance();
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::getInstance();
$Tainacan_Items = \Tainacan\Repositories\Items::getInstance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
@ -131,8 +133,8 @@ class CategoryFieldTypes extends TAINACAN_UnitTestCase {
}
function test_relate_taxonomy() {
global $Tainacan_Item_Metadata, $Tainacan_Items, $Tainacan_Fields, $Tainacan_Terms, $Tainacan_Taxonomies;
$Tainacan_Fields = \Tainacan\Repositories\Fields::getInstance();
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::getInstance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',

View File

@ -17,8 +17,9 @@ class CoreFieldTypes extends TAINACAN_UnitTestCase {
function test_core_field_types() {
global $Tainacan_Item_Metadata, $Tainacan_Items;
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::getInstance();
$Tainacan_Items = \Tainacan\Repositories\Items::getInstance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',

View File

@ -17,7 +17,7 @@ class DefaultCapabilities extends TAINACAN_UnitTestCase {
function test_capabilities_present() {
global $Tainacan_Capabilities;
$Tainacan_Capabilities = \Tainacan\Capabilities::getInstance();
$c = new \Tainacan\Entities\Collection();
$caps = $c->get_capabilities();

View File

@ -202,7 +202,8 @@ class Fields extends TAINACAN_UnitTestCase {
*
*/
function test_ordenation_fields(){
global $Tainacan_Collections, $Tainacan_Fields;
$Tainacan_Collections = \Tainacan\Repositories\Collections::getInstance();
$Tainacan_Fields = \Tainacan\Repositories\Fields::getInstance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',

View File

@ -37,7 +37,8 @@ class ImporterTests extends TAINACAN_UnitTestCase {
* @group importer
*/
public function test_file_csv () {
global $Tainacan_Fields, $Tainacan_Items;
$Tainacan_Items = \Tainacan\Repositories\Items::getInstance();
$Tainacan_Fields = \Tainacan\Repositories\Fields::getInstance();
$csv_importer = new Importer\CSV();
$id = $csv_importer->get_id();

View File

@ -17,8 +17,9 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
* Teste da insercao de um metadado simples sem o tipo
*/
function test_add() {
global $Tainacan_Fields, $Tainacan_Item_Metadata;
$Tainacan_Fields = \Tainacan\Repositories\Fields::getInstance();
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::getInstance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
@ -68,7 +69,8 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
* Teste da insercao de um metadado simples com o tipo
*/
function teste_required(){
global $Tainacan_Fields, $Tainacan_Item_Metadata;
$Tainacan_Fields = \Tainacan\Repositories\Fields::getInstance();
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::getInstance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
@ -121,7 +123,8 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
}
function teste_collection_key(){
global $Tainacan_Fields, $Tainacan_Item_Metadata;
$Tainacan_Fields = \Tainacan\Repositories\Fields::getInstance();
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::getInstance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',

View File

@ -134,8 +134,9 @@ class PrivateObjects extends TAINACAN_UnitTestCase {
$new_contributor_user = $this->factory()->user->create(array( 'role' => 'contributor' ));
wp_set_current_user($new_contributor_user);
global $Tainacan_Items, $Tainacan_Collections;
$Tainacan_Items = \Tainacan\Repositories\Items::getInstance();
$Tainacan_Collections = \Tainacan\Repositories\Collections::getInstance();
$items = $Tainacan_Items->fetch([], $collection);
$this->assertEquals(2, $items->found_posts, 'items of a public collections should be visible');

View File

@ -40,7 +40,8 @@ class Taxonomies extends TAINACAN_UnitTestCase {
}
function test_add_term_taxonomy(){
global $Tainacan_Taxonomies, $Tainacan_Terms;
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::getInstance();
$Tainacan_Terms = \Tainacan\Repositories\Terms::getInstance();
$taxonomy = $this->tainacan_entity_factory->create_entity(
'taxonomy',