116 lines
3.4 KiB
PHP
116 lines
3.4 KiB
PHP
<?php
|
|
|
|
|
|
|
|
|
|
function dynamiques_revue_save_post($post_id)
|
|
{
|
|
$current_revue_ID = $post_id;
|
|
$post_type = get_post_type($post_id);
|
|
if ($post_type !== 'revues') return;
|
|
|
|
// Remove articles that are no longer associated with the current review
|
|
dynamiques_revue_empty_unmapped_articles($current_revue_ID);
|
|
|
|
// Link articles to the current review
|
|
dynamiques_revue_link_mapped_articles($current_revue_ID);
|
|
}
|
|
add_action('acf/save_post', 'dynamiques_revue_save_post', 20);
|
|
|
|
|
|
//*******************************************************************************************************/
|
|
// Compare the articles linked to the current review being saved and remove those that are no longer associated
|
|
//*******************************************************************************************************/
|
|
|
|
|
|
function dynamiques_revue_empty_unmapped_articles($current_revue_ID)
|
|
{
|
|
$articles = get_field('articles', $current_revue_ID);
|
|
if (!$articles) return;
|
|
|
|
// GET ALL ARTICLES RELATED TO THE REVUE AND CHECK IF THEY ARE HANDLED BY THE PARENT REVUE IN THE ARTICLE FIELD
|
|
|
|
$articles_related_to_revue = new WP_Query(array(
|
|
'post_type' => 'articles',
|
|
'meta_query' => array(
|
|
array(
|
|
'key' => 'related_revue',
|
|
'value' => $current_revue_ID,
|
|
'compare' => '=',
|
|
),
|
|
),
|
|
));
|
|
|
|
$articles_related_to_revue_posts = $articles_related_to_revue->posts;
|
|
|
|
|
|
$articles_related_to_revue_ids = array_map(function ($post) {
|
|
return $post->ID;
|
|
}, $articles_related_to_revue_posts);
|
|
|
|
$articles_ids = array_map(function ($article) {
|
|
return $article->ID;
|
|
}, $articles);
|
|
|
|
// GET THE ARTICLES THAT ARE NOT HANDLED BY THE PARENT REVUE IN THE ARTICLE FIELD
|
|
$unmapped_articles_ids = array_diff($articles_related_to_revue_ids, $articles_ids);
|
|
|
|
|
|
// EMPTY THE ARTICLE FIELD BECAUSE NOT USED BY THE PARENT REVUE
|
|
|
|
foreach ($unmapped_articles_ids as $article_id) {
|
|
update_field('related_revue', null, $article_id);
|
|
}
|
|
}
|
|
|
|
function dynamiques_revue_link_mapped_articles($current_revue_ID)
|
|
{
|
|
$articles = get_field('articles', $current_revue_ID);
|
|
if (!$articles) return;
|
|
|
|
foreach ($articles as $article) {
|
|
$article_id = $article->ID;
|
|
$related_revue = get_field('related_revue', $article_id);
|
|
update_field('related_revue', $current_revue_ID, $article_id);
|
|
}
|
|
}
|
|
|
|
|
|
//*******************************************************************************************************/
|
|
// Check if the issue number is already used by another revue
|
|
//*******************************************************************************************************/
|
|
|
|
add_filter('acf/validate_value/name=issue_number', 'dynamiques_revue_check_issue_number', 10, 4);
|
|
|
|
function dynamiques_revue_check_issue_number($valid, $value, $field, $input)
|
|
{
|
|
if (!$valid) {
|
|
return $valid;
|
|
}
|
|
|
|
// Récupère l'ID du post en cours d'édition
|
|
$current_revue_ID = isset($_POST['post_ID']) ? intval($_POST['post_ID']) : 0;
|
|
|
|
// Recherche d'autres revues avec le même numéro
|
|
$args = array(
|
|
'post_type' => 'revues',
|
|
'post_status' => 'any',
|
|
'posts_per_page' => 1,
|
|
'post__not_in' => array($current_revue_ID),
|
|
'meta_query' => array(
|
|
array(
|
|
'key' => 'issue_number',
|
|
'value' => $value,
|
|
'compare' => '=',
|
|
),
|
|
),
|
|
);
|
|
$query = new WP_Query($args);
|
|
|
|
if ($query->have_posts()) {
|
|
return 'Ce numéro de revue existe déjà.';
|
|
}
|
|
|
|
return $valid;
|
|
}
|