Refactoring : sécurité (XSS), découpage en modules inc/* et js/admin/*, IDs résolus par slug, perf (caches, cron Gravatar, assets auto-hébergés), tests
This commit is contained in:
52
inc/seance-helpers.php
Normal file
52
inc/seance-helpers.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* Séances de séminaire (cat « seance-de-seminaire ») :
|
||||
* résolution mémoïsée séance → séminaire parent, et redirection des
|
||||
* permaliens de séance vers le parent avec ancre #seance-{ID}.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Renvoie l'ID du séminaire parent (publié) qui liste cette séance dans son
|
||||
* champ Pods `seances`, ou 0. Mémoïsé par requête : la même résolution est
|
||||
* utilisée par les cards, l'agenda, l'AJAX et la redirection.
|
||||
*/
|
||||
function thalim_get_seance_parent_id( int $seance_id ): int {
|
||||
static $cache = [];
|
||||
if ( ! isset( $cache[ $seance_id ] ) ) {
|
||||
global $wpdb;
|
||||
$cache[ $seance_id ] = (int) $wpdb->get_var( $wpdb->prepare(
|
||||
"SELECT pm.post_id FROM {$wpdb->postmeta} pm
|
||||
JOIN {$wpdb->posts} p ON p.ID = pm.post_id
|
||||
WHERE pm.meta_key = 'seances' AND pm.meta_value = %s
|
||||
AND p.post_status = 'publish'
|
||||
LIMIT 1",
|
||||
(string) $seance_id
|
||||
) );
|
||||
}
|
||||
return $cache[ $seance_id ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Lien public d'une séance : permalien du séminaire parent + #seance-{ID},
|
||||
* ou permalien de la séance elle-même si aucun parent n'est trouvé.
|
||||
*/
|
||||
function thalim_get_seance_link( int $seance_id ): string {
|
||||
$parent_id = thalim_get_seance_parent_id( $seance_id );
|
||||
return $parent_id
|
||||
? get_permalink( $parent_id ) . '#seance-' . $seance_id
|
||||
: (string) get_permalink( $seance_id );
|
||||
}
|
||||
|
||||
// Séance de séminaire: redirect to parent séminaire with #seance-{ID} anchor
|
||||
add_action( 'template_redirect', function() {
|
||||
if ( ! is_single() ) return;
|
||||
if ( ! has_category( thalim_cat_id( 'seance' ) ) ) return;
|
||||
|
||||
$seance_id = get_the_ID();
|
||||
$parent_id = thalim_get_seance_parent_id( $seance_id );
|
||||
|
||||
if ( $parent_id ) {
|
||||
wp_redirect( get_permalink( $parent_id ) . '#seance-' . $seance_id, 301 );
|
||||
exit;
|
||||
}
|
||||
} );
|
||||
Reference in New Issue
Block a user