231 lines
8.0 KiB
PHP
231 lines
8.0 KiB
PHP
<?php
|
||
/**
|
||
* Plugin Name: Category Grid Widget for Elementor
|
||
* Plugin URI: https://git.mrraph.fr/WordPress/elementor-category-grid-widget-for-elementor
|
||
* Description: Responsive article category grid with image for Elementor.
|
||
* Version: 1.4.0
|
||
* Author: MrRaph_
|
||
* Author URI: https://mrraph.photo
|
||
* Requires at least: 5.8
|
||
* Requires PHP: 7.0
|
||
* Text Domain: category-grid-widget-for-elementor
|
||
* Requires Plugins: elementor
|
||
* License: GPLv2 or later
|
||
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
|
||
*/
|
||
|
||
|
||
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)
|
||
{
|
||
// phpcs:disable WordPress.Security.NonceVerification.Recommended
|
||
$taxonomy = filter_input( INPUT_GET, 'taxonomy', FILTER_SANITIZE_STRING );
|
||
$taxonomy = sanitize_key( (string) $taxonomy );
|
||
// phpcs:enable WordPress.Security.NonceVerification.Recommended
|
||
|
||
if ( ! in_array( $hook_suffix, [ 'edit-tags.php', 'term.php' ], true ) ) {
|
||
return;
|
||
}
|
||
if ( 'category' !== $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.4.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-for-elementor')),
|
||
'button' => esc_js(__('Sélectionner', 'category-grid-widget-for-elementor')),
|
||
]
|
||
);
|
||
}
|
||
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-for-elementor');
|
||
}
|
||
}
|
||
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-for-elementor'); ?></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-for-elementor'); ?></button>
|
||
<button type="button"
|
||
class="button button-secondary ccgw-remove-image"><?php esc_html_e('Supprimer l’image', 'category-grid-widget-for-elementor'); ?></button>
|
||
</p>
|
||
<?php wp_nonce_field('ccgw_save_category_image', 'ccgw_category_image_nonce'); ?>
|
||
</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-for-elementor'); ?></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): ?>
|
||
<?php
|
||
echo wp_get_attachment_image(
|
||
$image_id,
|
||
'thumbnail',
|
||
false,
|
||
['style' => 'max-width:60px;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-for-elementor'); ?></button>
|
||
<button type="button"
|
||
class="button button-secondary ccgw-remove-image"><?php esc_html_e('Supprimer l’image', 'category-grid-widget-for-elementor'); ?></button>
|
||
</p>
|
||
</td>
|
||
<?php wp_nonce_field('ccgw_save_category_image', 'ccgw_category_image_nonce'); ?>
|
||
</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,
|
||
* en vérifiant le nonce et en sanitisant adéquatement.
|
||
*/
|
||
function ccgw_save_category_image($term_id)
|
||
{
|
||
$nonce = isset( $_POST['ccgw_category_image_nonce'] )
|
||
? sanitize_text_field( wp_unslash( $_POST['ccgw_category_image_nonce'] ))
|
||
: '';
|
||
// $nonce = sanitize_text_field( $raw_nonce );
|
||
|
||
// 1) Vérifier la présence du nonce
|
||
if (empty($nonce)) {
|
||
return;
|
||
}
|
||
|
||
// 2) Vérifier le nonce
|
||
if (!wp_verify_nonce($nonce, 'ccgw_save_category_image')) {
|
||
return;
|
||
}
|
||
|
||
// 3) Vérifier la capacité de l’utilisateur
|
||
if (!current_user_can('manage_categories')) {
|
||
return;
|
||
}
|
||
|
||
// 4) Récupérer et sanitiszer l’ID d’image
|
||
if (isset($_POST['category-image-id'])) {
|
||
$image_id = intval(wp_unslash($_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.4.0'
|
||
);
|
||
}
|
||
}
|
||
add_action('wp_enqueue_scripts', 'ccgw_enqueue_front_styles');
|