Skip to content

Commit

Permalink
Add support to metaboxes in page templates - Version bump to 3.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Parziale committed Oct 3, 2019
1 parent 9427643 commit 91be4ca
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Aeria/Aeria.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
class Aeria extends Container
{
public const VERSION = '3.0.3';
public const VERSION = '3.0.4';
/**
* Constructs the Aeria container
*
Expand Down
61 changes: 47 additions & 14 deletions Aeria/Meta/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Aeria\RenderEngine\RenderEngine;
/**
* Meta is in charge of creating, saving and rendering metaboxes
*
*
* @category Meta
* @package Aeria
* @author Jacopo Martinelli <jacopo.martinelli@caffeina.com>
Expand Down Expand Up @@ -41,7 +41,7 @@ public function getValidationStructure() : array
*
* @param array $config the Meta configs
* @param array $sections the sections configuration
* @param RenderEngine $render_service the service in charge of rendering HTML
* @param RenderEngine $render_service the service in charge of rendering HTML
*
* @return array the field's values, an array containing the gallery's children
*
Expand All @@ -50,17 +50,44 @@ public function getValidationStructure() : array
*/
public function create($config, $sections, $render_service)
{
global $wp_meta_boxes;
$this->validate($config);
$this->sections = $sections;
add_meta_box(
'aeria-' . $config['id'],
$config['title'],
Meta::class . '::renderHTML',
isset($config['post_type']) ? $config['post_type'] : 'post',
isset($config['context']) ? $config['context'] : 'advanced',
isset($config['priority']) ? $config['priority'] : 'default',
["config" => $config,"sections"=> $sections, "render_service" => $render_service]
);

$context = isset($config['context']) ? $config['context'] : 'advanced';
$priority = isset($config['priority']) ? $config['priority'] : 'default';
$templates = isset($config['templates']) ? $config['templates'] : [];

if(isset($config['post_type']) && count($config['post_type']) === 0){
$post_types = false;
} else if (!isset($config['post_type'])){
$post_types = 'post';
} else {
$post_types = $config['post_type'];
}
if($post_types !== false){
add_meta_box(
'aeria-' . $config['id'],
$config['title'],
Meta::class . '::renderHTML',
$post_types,
$context,
$priority,
["config" => $config,"sections"=> $sections, "render_service" => $render_service]
);
}

if (!isset($wp_meta_boxes[get_post_type()][$context][$priority]['aeria-' . $config['id']]) && in_array(get_page_template_slug(), $templates)){
add_meta_box(
'aeria-' . $config['id'],
$config['title'],
Meta::class . '::renderHTML',
get_post_type(),
$context,
$priority,
["config" => $config,"sections"=> $sections, "render_service" => $render_service]
);
}
}
/**
* Validates the meta configuration
Expand All @@ -83,7 +110,7 @@ private function validate($conf)
/**
* Returns the required fields to get a WP nonce
*
* @param WP_Post $post current post object
* @param WP_Post $post current post object
*
* @return array the nonce fields
*
Expand Down Expand Up @@ -148,14 +175,20 @@ static function renderHTML($post, $extra)
public static function save($metabox, $new_values, $validator_service, $query_service, $sections, $render_service)
{
return function ($post_id,$post) use ($metabox, $new_values, $validator_service, $query_service, $sections, $render_service) {
$templates = isset($metabox['templates']) ? $metabox['templates'] : [];
$context = isset($metabox['context']) ? $metabox['context'] : 'advanced';
$priority = isset($metabox['priority']) ? $metabox['priority'] : 'default';
// Since this function is triggered when a post is created too, I'm gonna skip it in that case. I'm gonna skip it even if the post_Type is not supported.
if ($new_values==[] || !in_array($post->post_type, $metabox["post_type"])) {
if ($new_values==[]
|| !(in_array($post->post_type, $metabox["post_type"]) || in_array(get_page_template_slug(), $templates))
|| !isset($new_values['update_aeria_meta'])
) {
return $post_id;
}

$nonceIDs = static::nonceIDs($post);
$post_type_object = get_post_type_object($new_values['post_type']);

// TODO: Check if current post type is supported: in the 1.0, the supported fields were hardcoded to ['post']
if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
|| (empty($new_values['post_ID']) || empty($new_values['post_type']))
|| (!isset($new_values['post_ID']) || $post_id != $new_values['post_ID'])
Expand Down
2 changes: 1 addition & 1 deletion aeria.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Plugin Name: Aeria
* Plugin URI: https://github.com/caffeinalab/aeria
* Description: Aeria is a modular, lightweight, fast WordPress Application development kit.
* Version: 3.0.3
* Version: 3.0.4
* Author: Caffeina
* Author URI: https://caffeina.com
* Text Domain: aeria
Expand Down

0 comments on commit 91be4ca

Please sign in to comment.