admin plugins backend structure #96

This commit is contained in:
Leo Germani 2018-08-29 14:57:44 -03:00 committed by Mateus Machado Luna
parent af2ce16dfb
commit 1406ee8f35
10 changed files with 118 additions and 58 deletions

View File

@ -0,0 +1,9 @@
<?php
/**
* @see \Tainacan\Admin_Hooks->register()
*/
function register_admin_hook( $context, $callback, $position = 'end-left' ) {
$admin_hooks = \Tainacan\Admin_Hooks::get_instance();
return $admin_hooks->register( $context, $callback, $position );
}

View File

@ -3,10 +3,10 @@
namespace Tainacan;
class AdminHooks {
class Admin_Hooks {
public $registered_plugins = [];
private $registered_hooks = [];
private static $instance = null;
@ -19,19 +19,40 @@ class AdminHooks {
}
private function __construct() {
add_action('init', [$this, 'init']);
}
function init() {
do_action('tainacan-register-admin-hooks');
do_action('tainacan-register-meta');
}
public function get_available_positions() {
return apply_filters('tainacan-admin-hooks-positions', ['begin-left', 'begin-right', 'end-left', 'end-right']);
}
public function get_available_contexts() {
return apply_filters('tainacan-admin-hooks-contexts', ['collection', 'metadatum', 'item', 'taxonomy', 'term', 'filter']);
}
public function get_registered_hooks() {
return $this->registered_hooks;
}
/**
*
* @param string $context The context to add the hook to (collection, metadatum, item, taxonomy, term or filter)
* @param string $position The position inside the page to hook. (begin, end, begin-left, begin-right, end-left, end-right)
* @param callable $callback The callback that will output the form HTML
*/
public function register( $context, $position, $callback ) {
public function register( $context, $callback, $position = 'end-left' ) {
$contexts = $this->get_available_contexts();
$positions = $this->get_available_positions();
if ( !in_array($context, $contexts) || !in_array($position, $positions) ) {
return false;
}
$result = call_user_func($callback);
if (is_string($result)){
$this->registered_hooks[$context][$position][] = $result;
@ -39,19 +60,7 @@ class AdminHooks {
}
return false;
}
/**
*
* @param string $context The context to add the metahook to (collection, metadatum, item, taxonomy, term or filter)
* @param string $meta_key The unique name of a meta where information about the entity will be saved using add_post_meta()
*/
public function register_meta( $context, $meta_key ) {
if (is_string($meta_key)){
$this->registered_meta[$context][] = $meta_key;
return true;
}
return false;
}
}
}

View File

@ -205,45 +205,7 @@ class Admin {
$settings['i18n']['helpers_label'][$class->get_component()] = $class->get_form_labels();
}
$settings['form_hooks'] = [
'form-collection' => [
'begin-left' => [
'<input type="text" name="collection-background-color" value="blue" /><input type="text" name="collection-background-color1" value="blue" />',
'<input type="text" name="collection-background-color2" value="blue" />'
],
'end-left' => ['<input type="text" name="collection-color" value="red" />'],
'begin-right' => ['<input type="text" name="collection-border-color" value="black" />'],
'end-right' => ['<input type="text" name="collection-highlight-color" value="green" />']
],
'form-item' => [
'begin-left' => ['<input type="text" name="item-background-color" value="blue" />'],
'end-left' => ['<input type="text" name="item-color" value="red" />'],
'begin-right' => ['<input type="text" name="item-border-color" value="black" />'],
'end-right' => ['<input type="text" name="item-highlight-color" value="green" />']
],
'view-item' => [
'begin-left' => ['<p>blue</p>'],
'end-left' => ['<p>black</p>'],
'begin-right' => ['<p>red</p>'],
'end-right' => ['<p>green</p>'],
],
'form-taxonomy' => [
'begin' => ['<input type="text" name="taxonomy-background-color" value="blue" />'],
'end' => ['<input type="text" name="taxonomy-color" value="red" />']
],
'form-term' => [
'begin' => ['<input type="text" name="term-background-color" value="blue" />'],
'end' => ['<input type="text" name="term-color" value="red" />']
],
'form-metadatum' => [
'begin' => ['<input type="text" name="metadatum-background-color" value="blue" />'],
'end' => ['<input type="text" name="metadatum-color" value="red" />']
],
'form-filter' => [
'begin' => ['<input type="text" name="filter-background-color" value="blue" />'],
'end' => ['<input type="text" name="filter-color" value="red" />']
]
];
$settings['form_hooks'] = Admin_Hooks::get_instance()->get_registered_hooks();
return $settings;

View File

@ -203,7 +203,18 @@ class REST_Collections_Controller extends REST_Controller {
$item_arr['total_items']['private'] = $total_items->private;
}
$extra_metadata = apply_filters('tainacan-api-response-collection-meta', []);
/**
* Use this filter to add additional post_meta to the api response
* Use the $request object to get the context of the request and other variables
* For example, id context is edit, you may want to add your meta or not.
*
* Also take care to do any permissions verification before exposing the data
*/
$extra_metadata = apply_filters('tainacan-api-response-collection-meta', [], $request);
foreach ($extra_metadata as $extra_meta) {
$item_arr[$extra_meta] = get_post_meta($item_arr['id'], $extra_meta, true);
}
return $item_arr;
}

View File

@ -331,6 +331,19 @@ class REST_Filters_Controller extends REST_Controller {
$item_arr['filter_type_object'] = $item->get_filter_type_object() ? $item->get_filter_type_object()->_toArray() : $item->get_filter_type_object();
/**
* Use this filter to add additional post_meta to the api response
* Use the $request object to get the context of the request and other variables
* For example, id context is edit, you may want to add your meta or not.
*
* Also take care to do any permissions verification before exposing the data
*/
$extra_metadata = apply_filters('tainacan-api-response-filter-meta', [], $request);
foreach ($extra_metadata as $extra_meta) {
$item_arr[$extra_meta] = get_post_meta($item_arr['id'], $extra_meta, true);
}
return $item_arr;
}

View File

@ -183,6 +183,19 @@ class REST_Items_Controller extends REST_Controller {
$item_arr['url'] = get_permalink( $item_arr['id'] );
$item_arr['exposer_urls'] = \Tainacan\Exposers\Exposers::get_exposer_urls(get_rest_url(null, "{$this->namespace}/{$this->rest_base}/{$item->get_id()}/"));
/**
* Use this filter to add additional post_meta to the api response
* Use the $request object to get the context of the request and other variables
* For example, id context is edit, you may want to add your meta or not.
*
* Also take care to do any permissions verification before exposing the data
*/
$extra_metadata = apply_filters('tainacan-api-response-item-meta', [], $request);
foreach ($extra_metadata as $extra_meta) {
$item_arr[$extra_meta] = get_post_meta($item_arr['id'], $extra_meta, true);
}
return $item_arr;
}

View File

@ -321,6 +321,19 @@ class REST_Metadata_Controller extends REST_Controller {
$item_arr['enabled'] = $item->get_enabled_for_collection();
}
/**
* Use this filter to add additional post_meta to the api response
* Use the $request object to get the context of the request and other variables
* For example, id context is edit, you may want to add your meta or not.
*
* Also take care to do any permissions verification before exposing the data
*/
$extra_metadata = apply_filters('tainacan-api-response-metadatum-meta', [], $request);
foreach ($extra_metadata as $extra_meta) {
$item_arr[$extra_meta] = get_post_meta($item_arr['id'], $extra_meta, true);
}
return $item_arr;
}

View File

@ -106,6 +106,19 @@ class REST_Taxonomies_Controller extends REST_Controller {
$item_arr = $this->filter_object_by_attributes($item, $attributes_to_filter);
}
/**
* Use this filter to add additional post_meta to the api response
* Use the $request object to get the context of the request and other variables
* For example, id context is edit, you may want to add your meta or not.
*
* Also take care to do any permissions verification before exposing the data
*/
$extra_metadata = apply_filters('tainacan-api-response-taxonomy-meta', [], $request);
foreach ($extra_metadata as $extra_meta) {
$item_arr[$extra_meta] = get_post_meta($item_arr['id'], $extra_meta, true);
}
return $item_arr;
}

View File

@ -277,6 +277,19 @@ class REST_Terms_Controller extends REST_Controller {
$item_arr = $this->filter_object_by_attributes($item, $attributes_to_filter);
}
/**
* Use this filter to add additional term_meta to the api response
* Use the $request object to get the context of the request and other variables
* For example, id context is edit, you may want to add your meta or not.
*
* Also take care to do any permissions verification before exposing the data
*/
$extra_metadata = apply_filters('tainacan-api-response-term-meta', [], $request);
foreach ($extra_metadata as $extra_meta) {
$item_arr[$extra_meta] = get_term_meta($item_arr['id'], $extra_meta, true);
}
return $item_arr;
}

View File

@ -131,6 +131,10 @@ $Tainacan_Embed = \Tainacan\Embed::get_instance();
require_once(__DIR__ . '/../admin/class-tainacan-admin.php');
$Tainacan_Admin = \Tainacan\Admin::get_instance();
require_once(__DIR__ . '/../admin/class-tainacan-admin-hooks.php');
require_once(__DIR__ . '/../admin/admin-hooks-functions.php');
$Tainacan_Admin_Hooks = \Tainacan\Admin_Hooks::get_instance();
require_once(__DIR__ . '/../theme-helper/class-tainacan-theme-helper.php');
require_once(__DIR__ . '/../theme-helper/template-tags.php');
$Tainacan_Theme_Helper = \Tainacan\Theme_Helper::get_instance();