feat: add a flag system

Fixes #6
This commit is contained in:
Kazhnuz 2021-01-03 14:45:01 +01:00
parent 84bb19f592
commit a0bd93a44e
2 changed files with 84 additions and 13 deletions

View file

@ -26,28 +26,88 @@
* Permet d'afficher les niveaux de post pour les post features * Permet d'afficher les niveaux de post pour les post features
*/ */
// 1.1 - Enregistrement de la taxonomy pour le flag
register_taxonomy( register_taxonomy(
'niveau', 'flag',
'post', 'post',
array( array(
'label' => 'Niveau', 'label' => 'Flag',
'labels' => array( 'labels' => array(
'name' => 'Niveaux', 'name' => 'Flags',
'singular_name' => 'Niveaux', 'singular_name' => 'Flag',
'all_items' => 'Tous les niveaux', 'all_items' => 'Tous les flags',
'edit_item' => 'Éditer le niveau', 'edit_item' => 'Éditer le flag',
'view_item' => 'Voir le niveau', 'view_item' => 'Voir le flag',
'update_item' => 'Mettre à jour le niveau', 'update_item' => 'Mettre à jour le flag',
'add_new_item' => 'Ajouter un niveau', 'add_new_item' => 'Ajouter un flag',
'new_item_name' => 'Nouveau niveau', 'new_item_name' => 'Nouveau flag',
'search_items' => 'Rechercher parmi les niveaux', 'search_items' => 'Rechercher parmi les flags',
'popular_items' => 'Niveaux les plus utilisés' 'popular_items' => 'Flags les plus utilisés'
), ),
'hierarchical' => false 'hierarchical' => false
) )
); );
register_taxonomy_for_object_type( 'niveau', 'post' ); register_taxonomy_for_object_type( 'flag', 'post' );
// 1.2 - Ajout/Modif du niveau du flag
function flags_taxonomy_custom_fields($tag) {
// Check for existing taxonomy meta for the term you're editing
$t_id = $tag->term_id; // Get the ID of the term you're editing
$term_meta = get_option( "taxonomy_term_$t_id" ); // Do the check
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="niveau"><?php _e('Niveau du flag'); ?></label>
</th>
<td>
<input type="text" name="term_meta[niveau]" id="term_meta[niveau]" size="25" style="width:60%;" value="<?php echo $term_meta['niveau'] ? $term_meta['niveau'] : ''; ?>"><br />
<span class="description"><?php _e('Le niveau du flag (info, warning, danger…)'); ?></span>
</td>
</tr>
<?php
}
function flags_taxonomy_custom_fields_add( $taxonomy ) {
// Check for existing taxonomy meta for the term you're editing
$t_id = $tag->term_id; // Get the ID of the term you're editing
$term_meta = get_option( "taxonomy_term_$t_id" ); // Do the check
?>
<div class="form-field">
<label for="term_meta[niveau]"><?php _e('Niveau du flag'); ?></label>
<input type="text" name="term_meta[niveau]" id="term_meta[niveau]" />
<p><?php _e('Le niveau du flag (info, warning, danger…)'); ?>.</p>
</div>
<?php
}
add_action( 'post_tag_edit_form_fields', 'kspace_edit_term_fields', 10, 2 );
// 1.3 - Sauvegarde du niveau du flag
function save_taxonomy_custom_fields( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_term_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ){
if ( isset( $_POST['term_meta'][$key] ) ){
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
//save the option array
update_option( "taxonomy_term_$t_id", $term_meta );
}
}
// Ajout des fields
add_action( 'flag_edit_form_fields', 'flags_taxonomy_custom_fields', 10, 2 );
add_action( 'flag_add_form_fields', 'flags_taxonomy_custom_fields_add' );
add_action( 'created_flag', 'save_taxonomy_custom_fields' );
add_action( 'edited_flag', 'save_taxonomy_custom_fields', 10, 2 );
/* 2. Réseaux sociaux /* 2. Réseaux sociaux
* *

View file

@ -14,6 +14,17 @@
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?> <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<?php $flags = get_the_terms($post->ID, 'flag');
if ($flags) {
foreach( $flags as $flag ) {
$term_meta = get_option( "taxonomy_term_$flag->term_id" );
echo "<div class='toast toast-" . $term_meta['niveau'] . "'>";
echo $flag->description;
echo "</div>";
}
} ?>
<article class="article container-article" id="post-<?php the_ID(); ?>"> <article class="article container-article" id="post-<?php the_ID(); ?>">
<h1 class="page-title"><?php the_title(); ?></h1> <h1 class="page-title"><?php the_title(); ?></h1>