creating on activate plugin the core fields (repository)

This commit is contained in:
Eduardo humberto 2018-02-15 16:29:33 -02:00
parent 707500f716
commit 6437dd9939
5 changed files with 134 additions and 2 deletions

View File

@ -152,6 +152,15 @@ class Field extends Entity {
function get_field_options(){
return $this->get_mapped_property('field_type_options');
}
/**
* Return the if the field may be deleted
*
* @return string
*/
function get_can_delete(){
return $this->get_mapped_property('can_delete');
}
/**
* Set the field name
@ -272,6 +281,16 @@ class Field extends Entity {
$this->set_mapped_property('field_type', ( is_object( $value ) ) ? get_class( $value ) : $value ) ; // Encode to avoid backslaches removal
}
/**
* Set can delete
*
* @param [string] $value
* @return void
*/
function set_can_delete( $value ){
$this->set_mapped_property('can_delete', $value);
}
// helpers
/**

View File

@ -14,6 +14,16 @@ class Fields extends Repository {
protected $default_metadata = 'default';
public $field_types = [];
/**
* Register specific hooks for field repository
*/
function __construct() {
parent::__construct();
add_action('tainacan_activated', array(&$this, 'register_core_fields'));
add_action('wp_trash_post', array( &$this, 'disable_delete_core_fields' ) );
add_action('before_delete_post', array( &$this, 'disable_delete_core_fields' ) );
}
public function get_map() {
return apply_filters('tainacan-get-map-'.$this->get_name(), [
@ -131,6 +141,15 @@ class Fields extends Repository {
'description'=> __('The collection ID', 'tainacan'),
//'validation' => ''
],
'can_delete' => [
'map' => 'meta',
'title' => __('Can delete', 'tainacan'),
'type' => 'string',
'description'=> __('The field can be deleted', 'tainacan'),
'on_error' => __('Can delete is invalid', 'tainacan'),
'validation' => v::stringType()->in(['yes', 'no']), // yes or no. It cant be multiple if its collection_key
'default' => 'yes'
],
]);
}
@ -266,6 +285,8 @@ class Fields extends Repository {
* @throws \Exception
*/
public function fetch_by_collection(Entities\Collection $collection, $args = [], $output = null){
$this->register_core_fields();
$collection_id = $collection->get_id();
//get parent collections
@ -391,4 +412,73 @@ class Fields extends Repository {
return $this->field_types;
}
/**
* verify and, if is not registered, insert the default fields
*/
public function register_core_fields(){
$update_option = [];
$core_fields = get_option('tainacan_core_fields');
if( $core_fields ) {
return $core_fields;
}
// TODO: create a better way to retrieve this data
$data_core_fields = [
'core_title' => [
'name' => 'Title',
'description' => 'title',
'collection_id' => 'default',
'field_type' => 'Tainacan\Field_Types\Core_Title',
'can_delete' => 'no',
'status' => 'publish'
],
'core_description' => [
'name' => 'Description',
'description' => 'description',
'collection_id' => 'default',
'field_type' => 'Tainacan\Field_Types\Core_Description',
'can_delete' => 'no',
'status' => 'publish'
]
];
foreach ( $data_core_fields as $index => $data_core_field ) {
if( !$core_fields || !isset($core_fields[$index]) ){
$field = new Entities\Field();
foreach ($data_core_field as $attribute => $value) {
$set_ = 'set_' . $attribute;
$field->$set_( $value );
}
if ($field->validate()) {
$field = $this->insert($field);
$update_option[$index] = $field->get_id();
} else {
throw new \ErrorException('The entity wasn\'t validated.' . print_r( $field->get_errors(), true));
}
} else if( isset($core_fields[$index]) ) {
$update_option[$index] = $core_fields[$index];
}
}
update_option('tainacan_core_fields', $update_option);
return $update_option;
}
/**
* block user from remove core fields
*
* @param $post_id The post ID which is deleting
* @throws \ErrorException
*/
public function disable_delete_core_fields( $post_id ){
$core_fields = get_option('tainacan_core_fields');
if ( $core_fields && in_array( $post_id, $core_fields ) ) {
throw new \ErrorException('Core fields cannot be deleted.');
}
}
}

View File

@ -25,6 +25,13 @@ function tnc_enable_dev_wp_interface() {
//return defined('TNC_ENABLE_DEV_WP_INTERFACE') && true === TNC_ENABLE_DEV_WP_INTERFACE ? true : false;
}
// fire actions right after plugin is activate
function tainacan_activate() {
do_action( 'tainacan_activated' );
}
register_activation_hook( __FILE__, 'tainacan_activate' );
// TODO move it somewhere else?
require_once('admin/class-tainacan-admin.php');
global $Tainacan_Admin;

View File

@ -164,7 +164,21 @@ class Fields extends TAINACAN_UnitTestCase {
);
$retrieve_metadata = $Tainacan_Fields->fetch_by_collection( $collection_son, [], 'OBJECT' );
$this->assertEquals( 4, sizeof( $retrieve_metadata ) );
// should return 6
$this->assertEquals( 6, sizeof( $retrieve_metadata ) );
}
function test_core_fields(){
global $Tainacan_Fields;
$core_fields_ids = $Tainacan_Fields->register_core_fields();
$this->expectException(\ErrorException::class);
if( $core_fields_ids ){
foreach( $core_fields_ids as $core_field_id ){
wp_trash_post( $core_field_id );
}
}
}
/**

View File

@ -225,7 +225,9 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
$item_metadatas = $Tainacan_Item_Metadata->fetch($i, 'OBJECT');
$this->assertTrue(is_array($item_metadatas));
$this->assertEquals(1, sizeof($item_metadatas));
// notice for repository fields
$this->assertEquals(3, sizeof($item_metadatas));
$this->assertEquals('metadado', $item_metadatas[0]->get_field()->get_name());
}