cache par requête */ private static $ids = []; /** * ID du pod `post` (post_type _pods_pod), 0 si introuvable. */ public static function pod_id(): int { if (!isset(self::$ids['__pod'])) { global $wpdb; self::$ids['__pod'] = (int) $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} WHERE post_type = '_pods_pod' AND post_name = 'post' LIMIT 1" ); } return self::$ids['__pod']; } /** * ID d'un champ du pod `post` résolu par nom, 0 si introuvable. */ public static function field_id(string $field_name): int { if (!isset(self::$ids[$field_name])) { global $wpdb; self::$ids[$field_name] = (int) $wpdb->get_var($wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type = '_pods_field' AND post_name = %s AND post_parent = %d LIMIT 1", $field_name, self::pod_id() )); } return self::$ids[$field_name]; } /** * Écrit une relation multi-valeurs (terms ou users) façon Pods : * 1. wp_term_relationships (si $taxonomy fournie) * 2. lignes postmeta individuelles * 3. meta `_pods_{field}` sérialisée * 4. lignes wp_podsrel (weight = position), précédées d'un delete * pour ne jamais créer de doublons. * * @param int[] $related_ids IDs liés (term_ids ou user_ids) * @param string|null $taxonomy Taxonomy WP si le champ pointe des termes */ public static function set_relation(int $post_id, string $field_name, array $related_ids, ?string $taxonomy = null): void { $related_ids = array_values(array_unique(array_map('intval', $related_ids))); if (empty($related_ids)) { return; } global $wpdb; // 1. Relation native WP pour les taxonomies (append) if ($taxonomy !== null) { wp_set_object_terms($post_id, $related_ids, $taxonomy, true); } // 2. postmeta : une ligne par valeur (string, comme Pods) // Re-écrit l'ensemble pour rester cohérent avec _pods_* et podsrel. delete_post_meta($post_id, $field_name); foreach ($related_ids as $rid) { add_post_meta($post_id, $field_name, (string) $rid); } // 3. _pods_{field} : tableau d'entiers sérialisé update_post_meta($post_id, '_pods_' . $field_name, $related_ids); // 4. wp_podsrel $pod_id = self::pod_id(); $field_id = self::field_id($field_name); if (!$pod_id || !$field_id) { // Configuration Pods absente/inattendue : on n'écrit pas podsrel // (les meta posées ci-dessus suffisent au front), mais on trace. error_log(sprintf( '[thalim-hal-importer] Pods field "%s" introuvable (pod_id=%d) — wp_podsrel non écrit pour le post %d', $field_name, $pod_id, $post_id )); return; } $wpdb->delete( $wpdb->prefix . 'podsrel', ['pod_id' => $pod_id, 'field_id' => $field_id, 'item_id' => $post_id], ['%d', '%d', '%d'] ); foreach ($related_ids as $weight => $rid) { $wpdb->insert( $wpdb->prefix . 'podsrel', [ 'pod_id' => $pod_id, 'field_id' => $field_id, 'item_id' => $post_id, 'related_pod_id' => 0, 'related_field_id' => 0, 'related_item_id' => $rid, 'weight' => $weight, ], ['%d', '%d', '%d', '%d', '%d', '%d', '%d'] ); } } /** * Assigne LA catégorie d'un post (champ pick `categorie`) : catégorie WP * native (remplace) + stockage Pods. */ public static function set_categorie(int $post_id, int $cat_id): void { if (!$cat_id) { return; } wp_set_post_categories($post_id, [$cat_id]); global $wpdb; update_post_meta($post_id, 'categorie', $cat_id); update_post_meta($post_id, '_pods_categorie', [$cat_id]); $pod_id = self::pod_id(); $field_id = self::field_id('categorie'); if (!$pod_id || !$field_id) { error_log(sprintf( '[thalim-hal-importer] Pods field "categorie" introuvable — wp_podsrel non écrit pour le post %d', $post_id )); return; } $wpdb->delete( $wpdb->prefix . 'podsrel', ['pod_id' => $pod_id, 'field_id' => $field_id, 'item_id' => $post_id], ['%d', '%d', '%d'] ); $wpdb->insert( $wpdb->prefix . 'podsrel', [ 'pod_id' => $pod_id, 'field_id' => $field_id, 'item_id' => $post_id, 'related_pod_id' => 0, 'related_field_id' => 0, 'related_item_id' => $cat_id, 'weight' => 0, ], ['%d', '%d', '%d', '%d', '%d', '%d', '%d'] ); } }