bascule des statuts en custom tax et ajout d'un formulaire de proposition de maj

This commit is contained in:
Matt Marcha 2021-02-13 02:04:14 +01:00
parent 7f52e388f4
commit d86d9634f3
3 changed files with 418 additions and 11 deletions

132
comments.php Executable file
View file

@ -0,0 +1,132 @@
<?php
/**
* The template file for displaying the comments and comment form for the
* Twenty Twenty theme.
*
* @package WordPress
* @subpackage Twenty_Twenty
* @since Twenty Twenty 1.0
*/
/*
* If the current post is protected by a password and
* the visitor has not yet entered the password we will
* return early without loading the comments.
*/
if ( post_password_required() ) {
return;
}
if ( $comments ) {
?>
<div class="comments" id="comments">
<?php
$comments_number = absint( get_comments_number() );
?>
<div class="comments-header section-inner small max-percentage">
<h2 class="comment-reply-title">
<?php
if ( ! have_comments() ) {
_e( 'Leave a comment', 'twentytwenty' );
} elseif ( 1 === $comments_number ) {
/* translators: %s: Post title. */
printf( _x( 'Une suggestion de mise à jour', 'comments title', 'twentytwenty' ), get_the_title() );
} else {
printf(
/* translators: 1: Number of comments, 2: Post title. */
_nx(
'%1$s suggestion de mise à jour',
'%1$s suggestions de mise à jour',
$comments_number,
'comments title',
'twentytwenty'
),
number_format_i18n( $comments_number ),
get_the_title()
);
}
?>
</h2><!-- .comments-title -->
</div><!-- .comments-header -->
<div class="comments-inner section-inner thin max-percentage">
<?php
wp_list_comments(
array(
'callback' => "sf_comment_template",
'avatar_size' => 120,
'style' => 'div',
)
);
$comment_pagination = paginate_comments_links(
array(
'echo' => false,
'end_size' => 0,
'mid_size' => 0,
'next_text' => __( 'Newer Comments', 'twentytwenty' ) . ' <span aria-hidden="true">&rarr;</span>',
'prev_text' => '<span aria-hidden="true">&larr;</span> ' . __( 'Older Comments', 'twentytwenty' ),
)
);
if ( $comment_pagination ) {
$pagination_classes = '';
// If we're only showing the "Next" link, add a class indicating so.
if ( false === strpos( $comment_pagination, 'prev page-numbers' ) ) {
$pagination_classes = ' only-next';
}
?>
<nav class="comments-pagination pagination<?php echo $pagination_classes; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static output ?>" aria-label="<?php esc_attr_e( 'Comments', 'twentytwenty' ); ?>">
<?php echo wp_kses_post( $comment_pagination ); ?>
</nav>
<?php
}
?>
</div><!-- .comments-inner -->
</div><!-- comments -->
<?php
}
if ( comments_open() || pings_open() ) {
if ( $comments ) {
echo '<hr class="styled-separator is-style-wide" aria-hidden="true" />';
}
comment_form(
array(
'class_form' => 'section-inner thin max-percentage',
'title_reply_before' => '<h2 id="reply-title" class="comment-reply-title">',
'title_reply_after' => '</h2>',
)
);
} elseif ( is_single() ) {
if ( $comments ) {
echo '<hr class="styled-separator is-style-wide" aria-hidden="true" />';
}
?>
<!--<div class="comment-respond" id="respond">
<p class="comments-closed"><?php _e( 'Comments are closed.', 'twentytwenty' ); ?></p>
</div>--><!-- #respond -->
<?php
}

View file

@ -108,4 +108,21 @@ jQuery( document ).ready(function($) {
$(".search-container #measure-search").trigger("keyup"); $(".search-container #measure-search").trigger("keyup");
}); });
/*** Rien à voir avec la choucroute : on déplace le chap commentaire en bas ****/
if ($( "#commentform" ).length ) {
$('#commentform .comment-form-comment').insertBefore($('#commentform .comment-form-author')).show();
//Et on remplit le champ commentaire si il a été laissé vide
$('#commentform #submit').click(function (){
if($('#commentform .comment-form-comment').val().length === 0) {
$('#commentform .comment-form-comment textarea').val("Aucun commentaire");
}
});
}
}); });

View file

@ -75,15 +75,19 @@ function generate_home_posts_list() {
$html .= "<div class='search-container'><input id='measure-search' type='text' name='filters-search' placeholder='Rechercher'><button type='reset' id='measure-search-reset'><span class='dashicons dashicons-no-alt'></span></button> </div>"; $html .= "<div class='search-container'><input id='measure-search' type='text' name='filters-search' placeholder='Rechercher'><button type='reset' id='measure-search-reset'><span class='dashicons dashicons-no-alt'></span></button> </div>";
foreach ($measures as $measure) { foreach ($measures as $measure) {
$status = get_the_category($measure->ID)[0]; $status = get_the_terms($measure->ID, 'post-status')[0];
$color = get_field('couleur', 'category_'.$status->term_id); $color = get_field('couleur', 'category_'.$status->term_id);
$link = get_field('proposition-url', $measure->ID); $link = get_field('proposition-url', $measure->ID);
$date = get_the_modified_date("d/m/Y à H:i", $measure); $date = get_the_modified_date("d/m/Y à H:i", $measure);
$cats = wp_get_object_terms($measure->ID, 'category', ['orderby'=>'parent', 'fields'=>'id=>name']);
$html .= "<article class='measure-wrapper measure-".$status->slug."' style='border-color: $color'>"; $html .= "<article class='measure-wrapper measure-".$status->slug."' style='border-color: $color'>";
$html .= "<h3 class='measure-title' style='background-color: $color'>". $measure->post_title ."</h3>"; $html .= "<h3 class='measure-title' style='background-color: $color'>". $measure->post_title ."</h3>";
$html .= "<div class='measure-content'>"; $html .= "<div class='measure-content'>";
$html .= "<p class='measure-meta' style='background-color: $color'>". $measure->post_excerpt ." <a href='$link' target='_blank' class='external'>Lire la mesure</a></p>"; $html .= "<div class='measure-meta' style='background-color: $color'>
<p class='measure-meta-categories ". implode(' ',array_keys($cats)) ."'>" . implode(' > ', $cats) . "</p>
<p class='measure-meta-desc'>". $measure->post_excerpt ." <a href='$link' target='_blank' class='external'>Lire la mesure</a></p>
</div>";
$html .= "<div class='status-desc' style='background-color: $color'><p>". $status->description . "</p></div>"; $html .= "<div class='status-desc' style='background-color: $color'><p>". $status->description . "</p></div>";
$html .= "<div class='content'>" . $measure->post_content . "</div>"; $html .= "<div class='content'>" . $measure->post_content . "</div>";
$html .= "<p class='measure-meta' style='background-color: $color'>Mise à jour le " . $date . "<br /> "; $html .= "<p class='measure-meta' style='background-color: $color'>Mise à jour le " . $date . "<br /> ";
@ -113,29 +117,29 @@ function generate_home_status_graph() {
// calculer le pourcentage sur le total de mesures // calculer le pourcentage sur le total de mesures
// créer un bloc html avec l data correspondante // créer un bloc html avec l data correspondante
$cats = ["validated", "partially-validated", "discussed", "danger", "rejected", "undiscussed"]; $stats = ["validated", "partially-validated", "discussed", "danger", "rejected", "undiscussed"];
$obj_total = 0; $obj_total = 0;
$html = ""; $html = "";
foreach ($cats as $cat) { foreach ($stats as $stat) {
$cat = get_category_by_slug($cat); $stat = get_term_by('slug', $stat, 'post-status');
$color = get_field('couleur', "category_" . $cat->term_id); $color = get_field('couleur', "category_" . $stat->term_id);
$percent = $cat->count * 100 / $total; $percent = $stat->count * 100 / $total;
$html .= "<div class='home-graph-item'>"; $html .= "<div class='home-graph-item'>";
$html .= "<h3 class='status-title' status='". $cat->slug ."'>" . $cat->name . "</h3>"; $html .= "<h3 class='status-title' status='". $stat->slug ."'>" . $stat->name . "</h3>";
$html .= "<div class='progress-bar-wrapper'>"; $html .= "<div class='progress-bar-wrapper'>";
$html .= "<div class='progress-bar' style='background-color: $color; width: $percent%; '></div>"; $html .= "<div class='progress-bar' style='background-color: $color; width: $percent%; '></div>";
$html .= "<div class='progress-bar bottom'></div>"; $html .= "<div class='progress-bar bottom'></div>";
$html .= "</div>"; $html .= "</div>";
$html .= "<p>" . $cat->count . "</p>"; $html .= "<p>" . $stat->count . "</p>";
$html .= "</div>"; $html .= "</div>";
if (in_array($cat->slug, ['validated', 'partially-validated'])) { if (in_array($stat->slug, ['validated', 'partially-validated'])) {
$obj_total += $cat->count; $obj_total += $stat->count;
} }
} }
@ -156,6 +160,260 @@ function generate_home_status_graph() {
add_shortcode('graph_mesures', 'generate_home_status_graph'); add_shortcode('graph_mesures', 'generate_home_status_graph');
if ( ! function_exists( 'status_taxonomy' ) ) {
// Création de la taxonomie "statuts"
function status_taxonomy() {
$labels = array(
'name' => 'Statuts',
'singular_name' => 'Statut',
'menu_name' => 'Statut',
'all_items' => 'Tous les statuts',
'parent_item' => 'Statut parent',
'parent_item_colon' => 'Statut parent :',
'new_item_name' => 'Nom du nouveau statut',
'add_new_item' => 'Ajouter un statut',
'edit_item' => 'Modifier le statut',
'update_item' => 'Mettre à jour le statut',
'view_item' => 'Voir le statut',
'separate_items_with_commas' => 'Séparer les statuts par une virugle',
'add_or_remove_items' => 'Ajouter ou supprimer des statuts',
'choose_from_most_used' => 'Choisir parmis les statuts les plus utilisés',
'popular_items' => 'Statuts les plus fréquents',
'search_items' => 'Chercher parmi les statuts',
'not_found' => 'Introuvable',
'no_terms' => 'Aucun statut',
'items_list' => 'Liste des statuts',
'items_list_navigation' => 'Navigation de la liste de statuts',
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'post-status', array( 'post' ), $args );
}
add_action( 'init', 'status_taxonomy', 0 );
}
/**
* On continue le foutoir du function.php avant refactoring
* ENREGISTREMENT DES PROPOSITIONS DE MISE À JOUR
*/
// Sur le papier c'est simple : on attribue au commentaire un au ID de post référent juste avant son enregistrement
function filter_sf_comment_referer( $comment_data ) {
// On vérifie que le champ ACF soit bien présent
if (isset($_POST['acf']['field_6026cfd4eaefc'])) {
// si oui on change l'ID de rattachement du commentaire
$comment_data['comment_post_ID'] = $_POST['acf']['field_6026cfd4eaefc'];
}
return $comment_data;
}
add_filter( 'preprocess_comment' , 'filter_sf_comment_referer', 10, 1);
// En pratique on se retrouve à devoir aussi modifier l'affichage du form des commentaires
add_filter( 'comment_form_default_fields', 'custom_comment_form_fields', 10 );
function custom_comment_form_fields( $fields ) {
// variables névéssaires à la suite
$req = get_option( 'require_name_email' );
$commenter = wp_get_current_commenter();
$html_req = ( $req ? " required='required'" : '' );
// le champ nom, on change juste le label
$fields['author'] =
sprintf('<p class="comment-form-author">%s %s</p>',
sprintf( '<label for="author">%s%s</label>', 'Prénom et initiale du nom', ( $req ? ' <span class="required">*</span>' : '' ) ),
sprintf( '<input id="author" name="author" type="text" value="%s" size="30" maxlength="245"%s />',
esc_attr( $commenter['comment_author'] ),
$html_req
)
);
//on vire l'url qui sert à rien
unset($fields['url']);
return $fields;
}
// On change quelques mentions
add_filter('comment_form_defaults', 'custom_comment_form_labels', 10);
function custom_comment_form_labels ($defaults) {
//Modification du titre
$defaults['title_reply'] = "Proposer la mise à jour d'une mesure.";
//Et du sous titre
$defaults['comment_notes_before'] = "Votre adresse e-mail ne sera pas publiée et gardée uniquement le temps de traiter votre proposition.<br /> Les champs obligatoires sont indiqués avec *";
return $defaults;
}
// Et la rediction
function sf_comment_redirect( $location ) {
$location = get_page_uri(740);
return $location;
}
add_filter( 'comment_post_redirect', 'sf_comment_redirect' );
// Enfin on ajoute un petit quelque chose aux commentaires vide pour les accepter quand même
function sf_accept_empty_comment($post_id, $author=null, $email=null) {
$comment = ( isset($_POST['comment']) ) ? trim($_POST['comment']) : null;
if (!$comment) {
$_POST['comment'] = "N/A";
}
}
add_action('pre_comment_on_post', 'sf_accept_empty_comment');
// Enfin on gère l'affichage
function sf_comment_template( $comment, $args, $depth ) {
?>
<article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
<footer class="comment-meta">
<div class="comment-author vcard">
<?php
$comment_author_url = get_comment_author_url( $comment );
$comment_author = get_comment_author( $comment );
$avatar = get_avatar( $comment, $args['avatar_size'] );
if ( 0 !== $args['avatar_size'] ) {
if ( empty( $comment_author_url ) ) {
echo wp_kses_post( $avatar );
} else {
printf( '<a href="%s" rel="external nofollow" class="url">', $comment_author_url ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped --Escaped in https://developer.wordpress.org/reference/functions/get_comment_author_url/
echo wp_kses_post( $avatar );
}
}
printf(
'<span class="fn">%1$s</span><span class="screen-reader-text says">%2$s</span>',
esc_html( $comment_author ),
__( 'says:', 'twentytwenty' )
);
if ( ! empty( $comment_author_url ) ) {
echo '</a>';
}
?>
</div><!-- .comment-author -->
<div class="comment-metadata">
<?php
/* translators: 1: Comment date, 2: Comment time. */
$comment_timestamp = sprintf( __( '%1$s at %2$s', 'twentytwenty' ), get_comment_date( '', $comment ), get_comment_time() );
printf(
'<a href="%s"><time datetime="%s" title="%s">%s</time></a>',
esc_url( get_comment_link( $comment, $args ) ),
get_comment_time( 'c' ),
esc_attr( $comment_timestamp ),
esc_html( $comment_timestamp )
);
if ( get_edit_comment_link() ) {
printf(
' <span aria-hidden="true">&bull;</span> <a class="comment-edit-link" href="%s">%s</a>',
esc_url( get_edit_comment_link() ),
__( 'Edit', 'twentytwenty' )
);
}
?>
</div><!-- .comment-metadata -->
</footer><!-- .comment-meta -->
<div class="comment-content entry-content">
<?php if( get_field('sf_maj_statut', $comment) ): ?>
<p class="sf-comment-status">Statut : <?php echo get_term_by('term_taxonomy_id', get_field('sf_maj_statut', $comment))->name; ?></p>
<?php endif; ?>
<?php if( get_field('sf_maj_texte_officiel', $comment) ): ?>
<p class="sf_comment_texte_officiel">Référence : <?php the_field('sf_maj_texte_officiel', $comment); ?></p>
<?php endif; ?>
<?php if( get_field('sf_maj_source', $comment) ): ?>
<p class="sf_comment_source"><a target="_blank" href="<?php the_field('sf_maj_source', $comment); ?>">Source</a></p>
<?php endif; ?>
<p>Commentaire :</p>
<?php
comment_text();
if ( '0' === $comment->comment_approved ) {
?>
<p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentytwenty' ); ?></p>
<?php
}
?>
</div><!-- .comment-content -->
<?php
$comment_reply_link = get_comment_reply_link(
array_merge(
$args,
array(
'add_below' => 'div-comment',
'depth' => $depth,
'max_depth' => $args['max_depth'],
'before' => '<span class="comment-reply">',
'after' => '</span>',
)
)
);
$by_post_author = twentytwenty_is_comment_by_post_author( $comment );
if ( $comment_reply_link || $by_post_author ) {
?>
<footer class="comment-footer-meta">
<?php
if ( $comment_reply_link ) {
echo $comment_reply_link; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Link is escaped in https://developer.wordpress.org/reference/functions/get_comment_reply_link/
}
if ( $by_post_author ) {
echo '<span class="by-post-author">' . __( 'By Post Author', 'twentytwenty' ) . '</span>';
}
?>
</footer>
<?php
}
?>
</article><!-- .comment-body -->
<hr />
<?php
}