$val ) { if ( str_starts_with( $key, 'pods_meta_' ) ) { $restore[ $key ] = is_array( $val ) ? array_map( 'wp_unslash', $val ) : wp_unslash( $val ); } } $error_text = is_wp_error( $error ) ? $error->get_error_message() : (string) $error; $restore['_msg'] = wp_strip_all_tags( $error_text ); $restore['_title'] = sanitize_text_field( wp_unslash( $_POST['post_title'] ?? '' ) ); set_transient( 'thalim_pods_restore_' . $post_id . '_' . $user_id, $restore, 10 * MINUTE_IN_SECONDS ); $GLOBALS['thalim_pods_error_post_id'] = $post_id; return false; // empêche wp_die() }, 10, 2 ); // Après le save : rediriger vers la page d'édition + annuler le statut si besoin add_filter( 'redirect_post_location', function ( $location ) { $post_id = $GLOBALS['thalim_pods_error_post_id'] ?? 0; if ( ! $post_id ) { return $location; } // Annuler le changement de statut vers publish si le post n'était pas encore publié $original = isset( $_POST['original_post_status'] ) ? sanitize_key( $_POST['original_post_status'] ) : ''; $post = get_post( $post_id ); if ( $post && in_array( $post->post_status, [ 'publish', 'future', 'pending' ], true ) && ! in_array( $original, [ 'publish', 'future', 'pending' ], true ) ) { global $wpdb; $wpdb->update( $wpdb->posts, [ 'post_status' => $original ?: 'draft' ], [ 'ID' => $post_id ], [ '%s' ], [ '%d' ] ); clean_post_cache( $post_id ); } return admin_url( 'post.php?post=' . $post_id . '&action=edit' ); }, 10 ); // Sur la page d'édition (GET) : lire le transient une seule fois → global → supprimer add_action( 'current_screen', function ( $screen ) { if ( $screen->base !== 'post' ) { return; } $post_id = isset( $_GET['post'] ) ? intval( $_GET['post'] ) : 0; if ( ! $post_id ) { return; } $user_id = get_current_user_id(); $key = 'thalim_pods_restore_' . $post_id . '_' . $user_id; $data = get_transient( $key ); if ( ! $data ) { return; } $GLOBALS['thalim_pods_restore'] = [ 'post_id' => $post_id, 'data' => $data, ]; delete_transient( $key ); } ); // Injecter les valeurs dans get_post_meta → Pods DFV les embarque dans son JSON React add_filter( 'get_post_metadata', function ( $value, $object_id, $meta_key, $single ) { $restore = $GLOBALS['thalim_pods_restore'] ?? null; if ( ! $restore || $restore['post_id'] !== (int) $object_id ) { return $value; } $pods_key = 'pods_meta_' . $meta_key; if ( ! isset( $restore['data'][ $pods_key ] ) ) { return $value; } $val = $restore['data'][ $pods_key ]; return $single ? $val : [ $val ]; }, 10, 4 ); // Restauration JS : titre + champs Pods select/pick via PodsDFV (même pattern que les modales) add_action( 'admin_footer', function () { $restore = $GLOBALS['thalim_pods_restore'] ?? null; if ( ! $restore ) { return; } $screen = get_current_screen(); if ( ! $screen || $screen->base !== 'post' ) { return; } $post_id = intval( $restore['post_id'] ); $title = $restore['data']['_title'] ?? ''; // Construire le map fieldName => value pour les champs Pods $fields = []; foreach ( $restore['data'] as $key => $val ) { if ( str_starts_with( $key, 'pods_meta_' ) ) { $fields[ substr( $key, 10 ) ] = $val; } } ?> base !== 'post' ) { return; } $msg = esc_html( $restore['data']['_msg'] ?? '' ); if ( $msg ) { echo '
' . $msg . '
Votre contenu a été restauré. Vérifiez les champs obligatoires avant de republier.