Taxonomies Pods : retirer le terme natif quand il est décoché
Le hook de synchronisation ne faisait qu'ajouter des termes natifs : une annonce décochée d'un programme, d'un axe ou d'une étiquette restait listée sur l'index du terme, alors que l'admin et la page de l'annonce ne l'affichaient plus. Le champ Pods est désormais lu avant et après la sauvegarde pour ne retirer que les termes qui viennent d'être décochés ; un terme natif qui n'a jamais figuré dans le champ n'est pas touché.
This commit is contained in:
+76
-20
@@ -16,11 +16,14 @@
|
||||
* trou est côté sauvegarde admin. Ce hook comble ce trou pour toutes les
|
||||
* origines de save.
|
||||
*
|
||||
* Stratégie : APPEND-ONLY. On n'ajoute que les termes présents dans le champ
|
||||
* Pods mais absents de la taxonomie native. On ne retire jamais de terme —
|
||||
* ainsi aucun risque de détruire des assignations posées par ailleurs (ex.
|
||||
* post_tag issus du matching HAL). La contrepartie (un terme retiré du champ
|
||||
* Pods reste en taxonomie native) est un moindre mal, très rare ici.
|
||||
* Stratégie : COMPARAISON AVANT/APRÈS. Le champ Pods est lu avant sa sauvegarde
|
||||
* (priorité 1, Pods écrit en priorité 10) et après (priorité 99) :
|
||||
* - un terme présent dans le champ mais absent de la taxonomie native est ajouté ;
|
||||
* - un terme qui était dans le champ et n'y est plus (décoché) est retiré.
|
||||
* On ne retire donc que ce qui vient d'être décoché. Un terme natif qui n'a
|
||||
* jamais figuré dans le champ Pods (ex. ancien import) n'est pas touché.
|
||||
* Contrepartie : pour nettoyer un terme natif resté orphelin (décoché avant ce
|
||||
* correctif), il faut le recocher, enregistrer, le décocher, enregistrer.
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
@@ -39,37 +42,90 @@ function thalim_pod_tax_sync_fields(): array {
|
||||
];
|
||||
}
|
||||
|
||||
add_action( 'save_post', 'thalim_snapshot_pods_taxonomy_fields', 1, 2 );
|
||||
add_action( 'save_post', 'thalim_sync_pods_taxonomy_relations', 99, 2 );
|
||||
|
||||
/**
|
||||
* Le post est-il concerné par la synchronisation ?
|
||||
*/
|
||||
function thalim_pod_tax_sync_applies( $post_id, $post ): bool {
|
||||
return $post instanceof WP_Post
|
||||
&& 'post' === $post->post_type
|
||||
&& ! wp_is_post_revision( $post_id )
|
||||
&& ! wp_is_post_autosave( $post_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Valeurs d'un champ Pods (une ligne postmeta par term_id).
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
function thalim_pod_tax_field_ids( $post_id, string $field ): array {
|
||||
return array_values( array_unique( array_filter(
|
||||
array_map( 'intval', (array) get_post_meta( $post_id, $field ) )
|
||||
) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Calcule les termes natifs à ajouter et à retirer (fonction pure).
|
||||
*
|
||||
* @param int[] $before Champ Pods avant la sauvegarde.
|
||||
* @param int[] $after Champ Pods après la sauvegarde.
|
||||
* @param int[] $native Termes natifs actuellement assignés.
|
||||
* @return array{add: int[], remove: int[]}
|
||||
*/
|
||||
function thalim_pod_tax_sync_diff( array $before, array $after, array $native ): array {
|
||||
return [
|
||||
'add' => array_values( array_diff( $after, $native ) ),
|
||||
'remove' => array_values( array_intersect( array_diff( $before, $after ), $native ) ),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Mémorise le contenu des champs avant que Pods ne les réécrive.
|
||||
* En cas de sauvegarde imbriquée, on garde l'état de la sauvegarde la plus externe.
|
||||
*/
|
||||
function thalim_snapshot_pods_taxonomy_fields( $post_id, $post ) {
|
||||
if ( ! thalim_pod_tax_sync_applies( $post_id, $post ) || isset( $GLOBALS['thalim_pod_tax_before'][ $post_id ] ) ) {
|
||||
return;
|
||||
}
|
||||
foreach ( thalim_pod_tax_sync_fields() as $field => $taxonomy ) {
|
||||
$GLOBALS['thalim_pod_tax_before'][ $post_id ][ $field ] = thalim_pod_tax_field_ids( $post_id, $field );
|
||||
}
|
||||
}
|
||||
|
||||
function thalim_sync_pods_taxonomy_relations( $post_id, $post ) {
|
||||
if ( ! ( $post instanceof WP_Post ) || 'post' !== $post->post_type ) {
|
||||
return;
|
||||
}
|
||||
if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
|
||||
if ( ! thalim_pod_tax_sync_applies( $post_id, $post ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$snapshot = $GLOBALS['thalim_pod_tax_before'][ $post_id ] ?? [];
|
||||
unset( $GLOBALS['thalim_pod_tax_before'][ $post_id ] );
|
||||
|
||||
foreach ( thalim_pod_tax_sync_fields() as $field => $taxonomy ) {
|
||||
if ( ! taxonomy_exists( $taxonomy ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Valeurs du champ Pods (source de vérité) : une ligne postmeta par term_id.
|
||||
$ids = array_values( array_unique( array_filter(
|
||||
array_map( 'intval', (array) get_post_meta( $post_id, $field ) )
|
||||
) ) );
|
||||
if ( empty( $ids ) ) {
|
||||
$after = thalim_pod_tax_field_ids( $post_id, $field );
|
||||
// Sans état antérieur connu, on ne déduit aucun retrait.
|
||||
$before = $snapshot[ $field ] ?? $after;
|
||||
if ( empty( $before ) && empty( $after ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Termes déjà assignés en taxonomie native.
|
||||
$current = wp_get_object_terms( $post_id, $taxonomy, [ 'fields' => 'ids' ] );
|
||||
$current = is_wp_error( $current ) ? [] : array_map( 'intval', $current );
|
||||
$native = wp_get_object_terms( $post_id, $taxonomy, [ 'fields' => 'ids' ] );
|
||||
if ( is_wp_error( $native ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// N'ajouter que les manquants (append), ne jamais retirer.
|
||||
$missing = array_diff( $ids, $current );
|
||||
if ( ! empty( $missing ) ) {
|
||||
wp_set_object_terms( $post_id, array_values( $missing ), $taxonomy, true );
|
||||
$diff = thalim_pod_tax_sync_diff( $before, $after, array_map( 'intval', $native ) );
|
||||
if ( ! empty( $diff['add'] ) ) {
|
||||
wp_set_object_terms( $post_id, $diff['add'], $taxonomy, true );
|
||||
}
|
||||
if ( ! empty( $diff['remove'] ) ) {
|
||||
wp_remove_object_terms( $post_id, $diff['remove'], $taxonomy );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,6 +79,14 @@ check('rôles exclus', thalim_excluded_role_ids(), [600, 598]);
|
||||
echo "== thalim_get_active_pinned_ids ==\n";
|
||||
check('catégorie 0 → []', thalim_get_active_pinned_ids(0), []);
|
||||
|
||||
echo "== thalim_pod_tax_sync_diff ==\n";
|
||||
check('coché : ajout', thalim_pod_tax_sync_diff([], [5], []), ['add' => [5], 'remove' => []]);
|
||||
check('tout décoché : retrait', thalim_pod_tax_sync_diff([5], [], [5]), ['add' => [], 'remove' => [5]]);
|
||||
check('décoché partiel', thalim_pod_tax_sync_diff([5, 6], [5], [5, 6]), ['add' => [], 'remove' => [6]]);
|
||||
check('natif hors champ conservé', thalim_pod_tax_sync_diff([5], [5], [5, 9]), ['add' => [], 'remove' => []]);
|
||||
check('orphelin sans coche conservé', thalim_pod_tax_sync_diff([], [], [9]), ['add' => [], 'remove' => []]);
|
||||
check('décoché déjà absent du natif', thalim_pod_tax_sync_diff([5], [], []), ['add' => [], 'remove' => []]);
|
||||
|
||||
echo "== thalim_get_seance_parent_id ==\n";
|
||||
global $wpdb;
|
||||
$seance_id = (int) $wpdb->get_var(
|
||||
|
||||
Reference in New Issue
Block a user