FEATURE introducing component
This commit is contained in:
parent
7e6f5eef21
commit
65825221d5
57
includes/article.php
Normal file
57
includes/article.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
|
||||
function dynamiques_article_save_post($post_id)
|
||||
{
|
||||
$current_article_ID = $post_id;
|
||||
$post_type = get_post_type($post_id);
|
||||
if ($current_article_ID && $post_type !== 'articles') return;
|
||||
|
||||
$related_revue_ID = get_field('related_revue', $current_article_ID);
|
||||
|
||||
dynamiques_article_remove_articles_from_all_revues($current_article_ID);
|
||||
|
||||
if ($related_revue_ID && is_numeric($related_revue_ID) && $related_revue_ID !== '') {
|
||||
dynamiques_article_include_article_in_revue_articles_array($current_article_ID, $related_revue_ID);
|
||||
}
|
||||
}
|
||||
add_action('acf/save_post', 'dynamiques_article_save_post', 20);
|
||||
|
||||
|
||||
function dynamiques_article_include_article_in_revue_articles_array($current_article_ID, $related_revue_ID)
|
||||
{
|
||||
$current_article = get_post($current_article_ID);
|
||||
$related_revues_articles = get_field('articles', $related_revue_ID);
|
||||
if (!is_array($related_revues_articles)) {
|
||||
$related_revues_articles = [];
|
||||
}
|
||||
$related_revues_articles[] = $current_article;
|
||||
|
||||
|
||||
update_field('articles', $related_revues_articles, $related_revue_ID);
|
||||
}
|
||||
|
||||
|
||||
function dynamiques_article_remove_articles_from_all_revues($current_article_ID)
|
||||
{
|
||||
$revues_containing_article = new WP_Query(array(
|
||||
'post_type' => 'revues',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'articles',
|
||||
'value' => '"' . $current_article_ID . '"',
|
||||
'compare' => 'LIKE',
|
||||
),
|
||||
),
|
||||
));
|
||||
|
||||
foreach ($revues_containing_article->posts as $revue) {
|
||||
$revue_ID = $revue->ID;
|
||||
$revue_articles = get_field('articles', $revue_ID);
|
||||
|
||||
$revue_articles = array_filter($revue_articles, function ($article) use ($current_article_ID) {
|
||||
return (is_object($article) && isset($article->ID)) ? $article->ID != $current_article_ID : $article != $current_article_ID;
|
||||
});
|
||||
update_field('articles', $revue_articles, $revue_ID);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user