Skip to content

Commit

Permalink
v0.22
Browse files Browse the repository at this point in the history
  • Loading branch information
GiorgioKM committed Jun 28, 2017
1 parent fcf9d56 commit 3a1a27d
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 10 deletions.
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
Changelog
=========

#### v0.22 - 28 Giugno 2017
* Aggiunta la revisione sui custom fields

#### v0.21 - 27 Giugno 2017
* Ora il metodo 'returnAllMeta()' è richiamabile anche lato backend
* Aggiunto nuovo metodo 'getMetaKey($namePostMeta = false)', che ritorna l'esatta metakey salvata sul DB. Se non viene passato nessun valore, ritorneranno tutti i metakey.
* Modificato il parametro 'required'. Ora è possibile passargli una funzione anonima per il controllo personalizzato del campo. In caso di errore, deve ritornare il messaggio da stampare.
* Aggiunto nuovo metodo 'getMetaKey($namePostMeta = false)', che ritorna l'esatta metakey salvata sul DB. Se non viene passato nessun valore, ritorneranno tutti i metakey
* Modificato il parametro 'required'. Ora è possibile passargli una funzione anonima per il controllo personalizzato del campo. In caso di errore, deve ritornare il messaggio da stampare

#### v0.20 - 20 Giugno 2017
* Aggiunto nuovo campo input radio (è possibile inserire più radio in un unico campo)
Expand All @@ -25,7 +28,7 @@
#### v0.18 - 7 Giugno 2017
* Classe rinominata in 'MetaBoxesHandler'
* Novità: i metabox verranno creati automaticamente dalla classe con il metodo 'add()'
* Cambiato il nome del metodo 'addMetaFields()' in 'add()'. Quando verrà lanciato creerà direttamente il metabox con la lista dei campi richiesti.
* Cambiato il nome del metodo 'addMetaFields()' in 'add()'. Quando verrà lanciato creerà direttamente il metabox con la lista dei campi richiesti
* Il metodo 'returnAllMeta()' ora funzionerà soltanto lato frontend. Nel backend non occorrerà più utilizzarlo
* Riscritti alcuni metodi per un utilizzo migliore della classe
* Risolto un problema nello script JS della classe
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
MetaBoxesHandler
================

![Build Status](https://img.shields.io/badge/build-v0.21-green.svg?style=flat)
![Build Status](https://img.shields.io/badge/build-v0.22-green.svg?style=flat)

È un utility per Wordpress per la gestione automatizzata di metabox da utilizzare con un custom post type, sia lato backend che frontend.

Expand All @@ -23,15 +23,18 @@ Crediti
|Tipo|Descrizione|
|:---|---:|
|@autore|Giorgio Suadoni|
|@versione|0.21|
|@data ultimo aggiornamento|27 Giugno 2017|
|@versione|0.22|
|@data ultimo aggiornamento|28 Giugno 2017|
|@data prima versione|20 Maggio 2017|



Changelog
---------

#### v0.22 - 28 Giugno 2017
* Aggiunta la revisione sui custom fields

#### v0.21 - 27 Giugno 2017
* Ora il metodo 'returnAllMeta()' è richiamabile anche lato backend
* Aggiunto nuovo metodo 'getMetaKey($namePostMeta = false)', che ritorna l'esatta metakey salvata sul DB. Se non viene passato nessun valore, ritorneranno tutti i metakey.
Expand Down
4 changes: 2 additions & 2 deletions dist/MetaBoxesHandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*
* È un utility per Wordpress per la gestione automatizzata di metabox da utilizzare con un custom post type, sia lato backend che frontend.
*
* @versione 0.21
* @data ultimo aggiornamento 27 Giugno 2017
* @versione 0.22
* @data ultimo aggiornamento 28 Giugno 2017
* @data prima versione 20 Maggio 2017
*
* @autore Giorgio Suadoni
Expand Down
172 changes: 170 additions & 2 deletions dist/includes/BaseMBH.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ abstract public function returnAllMeta($includeAllPosts);
/**
* Costruttore.
*
* @aggiornamento v0.14
* @aggiornamento v0.22 Integrazione con le revisioni
* @dalla v0.1
*
* @accesso pubblico
Expand All @@ -83,11 +83,135 @@ public function __construct($lang = false) {
$this->_setLang($lang);

$this->adminNotes = new adminNotes;

if (is_admin()) {
add_action('wp_print_scripts', function() {
wp_deregister_script('autosave');
});

add_filter('_wp_post_revision_fields', array($this, 'wp_post_revision_fields'));
add_action('wp_restore_post_revision', array($this, 'wp_restore_post_revision'), 10, 2);
}
}

/*
* Richiamato da 'add_filter'. Metodo che rimpiazza quello predefinito di wordpress.
*
* Determina quali campi di post devono essere salvati nelle revisioni.
*
* @dalla v0.22
*
* @accesso pubblico
*/
public function wp_post_revision_fields($returnPost) {
global $post, $pagenow;

$allowed = false;

if ($pagenow == 'admin-ajax.php' && isset($_POST['action']) && $_POST['action'] == 'get-revision-diffs')
$post_type = get_post_type($_POST['post_id']);
else
$post_type = $post->post_type;

if ($post_type != $this->postType)
return $returnPost;

$allMetaKey = $this->_processFieldSettings(function($settings, $args) {
$postMetas = $settings['name'];

if (!is_array($postMetas)) {
$tmp = $postMetas;

$postMetas = array();

$postMetas[] = $tmp;
}

foreach ($postMetas as $namePostMeta) {
$args[$namePostMeta] = $settings['label'];

add_filter('_wp_post_revision_field_'. $namePostMeta, array($this, 'wp_post_revision_field'), 10, 3);
}

return $args;
});

$returnPost = array_merge($returnPost, $allMetaKey);

return $returnPost;
}

/*
* Richiamato da 'add_filter'. Metodo che rimpiazza quello predefinito di wordpress.
*
* Questo filtro caricherà il valore per il campo specificato e lo restituisce per il rendering
*
* @dalla v0.22
*
* @accesso pubblico
*/
public function wp_post_revision_field($value, $fieldName, $postObj = null) {
global $revision, $post;

$meta = get_metadata('post', $postObj->ID, $this->postType, true);

return (isset($meta[$fieldName]) ? $meta[$fieldName] : '');
}

/*
* Richiamato da 'add_filter'. Metodo che rimpiazza quello predefinito di wordpress.
*
* Ripristina un post alla revisione specificata.
*
* @dalla v0.22
*
* @accesso pubblico
*/
public function wp_restore_post_revision($post_id, $revision_id) {
global $post;

if ($post->post_type != $this->postType)
return;

$revision = get_post($revision_id);

$metaLoad = get_metadata('post', $revision->ID, $this->postType, true);

$args = $this->_processFieldSettings(function($settings, $args) use ($metaLoad) {
$postMetas = $settings['name'];

if (!is_array($postMetas)) {
$tmp = $postMetas;

$postMetas = array();

$postMetas[] = $tmp;
}

foreach ($postMetas as $namePostMeta) {
if (!isset($settings['save-unique']) || isset($settings['save-unique']) && !$settings['save-unique'])
$args['metaSave'][$namePostMeta] = (isset($metaLoad[$namePostMeta]) ? $metaLoad[$namePostMeta] : '');
else {
$singleMetaLoad = get_metadata('post', $revision->ID, $this->postType .'_'. $namePostMeta .'_single', true);

if (false !== $singleMetaLoad)
update_post_meta($post_id, $this->postType .'_'. $namePostMeta .'_single', $singleMetaLoad);
else
delete_post_meta($post_id, $this->postType .'_'. $namePostMeta .'_single');
}
}

return $args;
});

if (is_array($args['metaSave']) && count($args['metaSave']))
update_post_meta($post_id, $this->postType, $args['metaSave']);
}

/**
* Richiama questo metodo quando viene aggiunta l'azione di salvataggio del post.
*
* @aggiornamento v0.22 Integrazione con le revisioni
* @dalla v0.18
*
* @accesso pubblico
Expand All @@ -98,7 +222,31 @@ public function mbhSaveActionWP($post_id) {

if ($post->post_type != $this->postType || in_array($_GET['action'], array('trash', 'untrash')))
return;


if ($_GET['action'] == 'restore') {
if ($parent_id = wp_is_post_revision($post_id)) {
$parent = get_post($parent_id);

if ($parent->post_type == $this->postType) {
$meta = get_post_meta($parent->ID, $this->postType, true);
$allMetaKey = $this->_getMetaKeyFromNamePostMeta();

foreach ($allMetaKey as $k => $v) {
if (!in_array($k, array_keys($meta)))
$meta[$k] = get_post_meta($parent->ID, $v, true);
}

if (false !== $meta)
add_metadata('post', $post_id, $this->postType, $meta);

return;
}

}

return;
}

$__POST = array_map('stripslashes_deep', $_POST);

if ($fieldRequired = $this->_checkRequiredFieldsInPOST($__POST))
Expand Down Expand Up @@ -149,6 +297,26 @@ public function mbhSaveActionWP($post_id) {
if (is_array($args['metaSave']) && count($args['metaSave']))
update_post_meta($post->ID, $this->postType, $args['metaSave']);
}

if ($parent_id = wp_is_post_revision($post_id)) {
$parent = get_post($parent_id);

if ($parent->post_type == $this->postType) {
$meta = get_post_meta($parent->ID, $this->postType, true);
$allMetaKey = $this->_getMetaKeyFromNamePostMeta();

foreach ($allMetaKey as $k => $v) {
if (!in_array($k, array_keys($meta)))
$meta[$k] = get_post_meta($parent->ID, $v, true);
}

if (false !== $meta)
add_metadata('post', $post_id, $this->postType, $meta);

return;
}

}
}


Expand Down

0 comments on commit 3a1a27d

Please sign in to comment.