taxonomy WP). * `categorie` est volontairement absent (déjà synchronisé par Pods). */ function thalim_pod_tax_sync_fields(): array { return [ 'programmes_de_recherche' => 'programme_de_recherche', 'etiquettes' => 'post_tag', 'axes_thematiques' => 'axe_thematique', ]; } 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 ( ! 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; } $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. $native = wp_get_object_terms( $post_id, $taxonomy, [ 'fields' => 'ids' ] ); if ( is_wp_error( $native ) ) { continue; } $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 ); } } }