180 lines
6.6 KiB
PHP
180 lines
6.6 KiB
PHP
<?php
|
||
/**
|
||
* Plugin Name: Elementor Category Grid Widget
|
||
* Description: Custom Elementor widget to display a grid of post categories with images.
|
||
* Version: 1.2.0
|
||
* Author: MrRaph_
|
||
* Text Domain: category-grid-widget
|
||
* Requires Plugins: elementor
|
||
* Elementor tested up to: 3.25.0
|
||
* Elementor Pro tested up to: 3.25.0
|
||
*/
|
||
|
||
if ( ! defined( 'ABSPATH' ) ) {
|
||
exit; // Sécurité : empêche l'accès direct.
|
||
}
|
||
|
||
/**
|
||
* Charger les scripts Media Uploader et localiser les données JS
|
||
* sur les écrans d'ajout ET d'édition de catégorie.
|
||
*/
|
||
function ccgw_enqueue_media_uploader( $hook_suffix ) {
|
||
// On ne charge que sur edit-tags.php (création) OU term.php (édition)
|
||
if ( ! in_array( $hook_suffix, [ 'edit-tags.php', 'term.php' ], true ) ) {
|
||
return;
|
||
}
|
||
// Et seulement pour la taxonomie 'category'
|
||
if ( empty( $_GET['taxonomy'] ) || 'category' !== $_GET['taxonomy'] ) {
|
||
return;
|
||
}
|
||
|
||
// Charge la librairie media de WP
|
||
wp_enqueue_media();
|
||
|
||
// Ton script JS
|
||
wp_enqueue_script(
|
||
'ccgw-category-image',
|
||
plugin_dir_url( __FILE__ ) . 'admin/js/category-image.js',
|
||
[ 'jquery' ],
|
||
'1.2.0',
|
||
true
|
||
);
|
||
|
||
// Variable JS
|
||
wp_localize_script(
|
||
'ccgw-category-image',
|
||
'ccgw_data',
|
||
[
|
||
'title' => esc_js( __( 'Sélectionner une image de catégorie', 'category-grid-widget' ) ),
|
||
'button' => esc_js( __( 'Sélectionner', 'category-grid-widget' ) ),
|
||
]
|
||
);
|
||
}
|
||
add_action( 'admin_enqueue_scripts', 'ccgw_enqueue_media_uploader' );
|
||
|
||
// 1) Déclarez la colonne "Image" dans le tableau des catégories
|
||
add_filter( 'manage_edit-category_columns', function( $columns ) {
|
||
$new = [];
|
||
foreach ( $columns as $key => $label ) {
|
||
$new[ $key ] = $label;
|
||
if ( 'name' === $key ) {
|
||
// après la colonne "Nom", on insère notre colonne "Image"
|
||
$new['category_image'] = esc_html__( 'Image', 'category-grid-widget' );
|
||
}
|
||
}
|
||
return $new;
|
||
} );
|
||
|
||
// 2) Remplissez la colonne "Image"
|
||
add_filter( 'manage_category_custom_column', function( $out, $column, $term_id ) {
|
||
if ( 'category_image' === $column ) {
|
||
$thumb_id = get_term_meta( $term_id, 'thumbnail_id', true );
|
||
if ( $thumb_id ) {
|
||
// Affiche la version 'thumbnail' de l'image
|
||
return wp_get_attachment_image( $thumb_id, 'thumbnail', false, [
|
||
'style' => 'max-width:60px;height:auto;'
|
||
] );
|
||
}
|
||
}
|
||
return $out;
|
||
}, 10, 3 );
|
||
|
||
// 3) Un peu de CSS pour la largeur de colonne et la mise en forme
|
||
add_action( 'admin_head-edit-tags.php', function() {
|
||
echo '<style>
|
||
.fixed .column-category_image { width: 80px; }
|
||
.column-category_image img { display: block; margin: 4px auto; }
|
||
</style>';
|
||
} );
|
||
|
||
/**
|
||
* Affiche le champ d'upload dans le formulaire de création de catégorie.
|
||
*/
|
||
function ccgw_category_image_field( $taxonomy ) { ?>
|
||
<div class="form-field term-group">
|
||
<label for="category-image-id"><?php esc_html_e( 'Image de la catégorie', 'category-grid-widget' ); ?></label>
|
||
<input type="hidden" id="category-image-id" name="category-image-id" value="">
|
||
<div id="category-image-wrapper"></div>
|
||
<p>
|
||
<button type="button" class="button button-secondary ccgw-upload-image"><?php esc_html_e( 'Choisir une image', 'category-grid-widget' ); ?></button>
|
||
<button type="button" class="button button-secondary ccgw-remove-image"><?php esc_html_e( 'Supprimer l’image', 'category-grid-widget' ); ?></button>
|
||
</p>
|
||
</div>
|
||
<?php }
|
||
add_action( 'category_add_form_fields', 'ccgw_category_image_field', 10, 2 );
|
||
|
||
/**
|
||
* Affiche le champ d'upload dans le formulaire d'édition de catégorie.
|
||
*/
|
||
function ccgw_category_image_field_edit( $term, $taxonomy ) {
|
||
$image_id = get_term_meta( $term->term_id, 'thumbnail_id', true );
|
||
$image_url = $image_id ? wp_get_attachment_thumb_url( $image_id ) : '';
|
||
?>
|
||
<tr class="form-field term-group-wrap">
|
||
<th scope="row">
|
||
<label for="category-image-id"><?php esc_html_e( 'Image de la catégorie', 'category-grid-widget' ); ?></label>
|
||
</th>
|
||
<td>
|
||
<input type="hidden" id="category-image-id" name="category-image-id" value="<?php echo esc_attr( $image_id ); ?>">
|
||
<div id="category-image-wrapper">
|
||
<?php if ( $image_url ) : ?>
|
||
<img src="<?php echo esc_url( $image_url ); ?>" style="max-width:100px; height:auto;">
|
||
<?php endif; ?>
|
||
</div>
|
||
<p>
|
||
<button type="button" class="button button-secondary ccgw-upload-image"><?php esc_html_e( 'Choisir une image', 'category-grid-widget' ); ?></button>
|
||
<button type="button" class="button button-secondary ccgw-remove-image"><?php esc_html_e( 'Supprimer l’image', 'category-grid-widget' ); ?></button>
|
||
</p>
|
||
</td>
|
||
</tr>
|
||
<?php }
|
||
add_action( 'category_edit_form_fields', 'ccgw_category_image_field_edit', 10, 2 );
|
||
|
||
/**
|
||
* Sauvegarde le term meta 'thumbnail_id' pour la catégorie.
|
||
*/
|
||
function ccgw_save_category_image( $term_id ) {
|
||
if ( isset( $_POST['category-image-id'] ) ) {
|
||
$image_id = intval( $_POST['category-image-id'] );
|
||
if ( $image_id ) {
|
||
update_term_meta( $term_id, 'thumbnail_id', $image_id );
|
||
} else {
|
||
delete_term_meta( $term_id, 'thumbnail_id' );
|
||
}
|
||
}
|
||
}
|
||
add_action( 'created_category', 'ccgw_save_category_image', 10, 2 );
|
||
add_action( 'edited_category', 'ccgw_save_category_image', 10, 2 );
|
||
|
||
/**
|
||
* Enregistrer le widget "Grille de Catégories" pour Elementor.
|
||
*
|
||
* Inclut le fichier de la classe du widget et enregistre le widget auprès du manager d'Elementor.
|
||
*
|
||
* @param \Elementor\Widgets_Manager $widgets_manager Le gestionnaire de widgets d'Elementor.
|
||
*/
|
||
function register_category_grid_widget( $widgets_manager ) {
|
||
// Inclure le fichier de la classe du widget.
|
||
require_once __DIR__ . '/widgets/category-grid-widget.php';
|
||
|
||
// Enregistrer la classe du widget auprès d'Elementor.
|
||
$widgets_manager->register( new \Elementor_Category_Grid_Widget() );
|
||
}
|
||
add_action( 'elementor/widgets/register', 'register_category_grid_widget' );
|
||
|
||
/**
|
||
* Enqueue styles front-end pour le widget.
|
||
*/
|
||
function ccgw_enqueue_front_styles() {
|
||
// Ne charger que si Elementor est actif
|
||
if ( defined( 'ELEMENTOR_VERSION' ) ) {
|
||
wp_enqueue_style(
|
||
'ccgw-category-grid-style',
|
||
plugin_dir_url( __FILE__ ) . 'css/style.css',
|
||
[],
|
||
'1.2.0'
|
||
);
|
||
}
|
||
}
|
||
add_action( 'wp_enqueue_scripts', 'ccgw_enqueue_front_styles' );
|