Forum Replies Created

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter graindepixel

    (@graindepixel)

    After a few hours of struggling, and given my inexperience, I tried using AI. Good chat, GPT, and Gemini wasted a lot of my time, but Claude found a solution in just a few prompts. I don’t know if it’s a permanent solution, but it works, or at least it’s a temporary fix. Please feel free to give me corrections and instructions, especially to avoid creating more problems.

    <?php
    /**
    * Sécurisation et attachement automatique des images de galerie
    * Pour Frontend Admin + ACF
    */

    // 1. Limiter les types de fichiers uploadables dans la galerie
    add_filter('acf/upload_prefilter/name=galerie', 'restrict_gallery_file_types', 5, 3);
    function restrict_gallery_file_types($errors, $file, $field) {
    // Types MIME autorisés pour les images
    $allowed_types = array(
    'image/jpeg',
    'image/jpg',
    'image/png',
    'image/gif',
    'image/webp'
    );

    // Vérifier le type MIME du fichier
    if (!in_array($file['type'], $allowed_types)) {
    $errors[] = 'Seules les images (JPEG, PNG, GIF, WebP) sont autorisées.';
    }

    // Vérification supplémentaire avec wp_check_filetype
    $filetype = wp_check_filetype($file['name']);
    if (!in_array($filetype['type'], $allowed_types)) {
    $errors[] = 'Le type de fichier n\'est pas autorisé.';
    }

    return $errors;
    }

    // 2. Hook qui s'exécute quand ACF uploade un fichier
    add_filter('acf/upload_prefilter/name=galerie', 'set_gallery_image_parent', 10, 3);
    function set_gallery_image_parent($errors, $file, $field) {
    // Vérifier le nonce pour la sécurité
    if (!isset($_POST['_acfnonce']) || !wp_verify_nonce($_POST['_acfnonce'], 'acf_nonce')) {
    return $errors;
    }

    // Récupérer le post_id depuis les données du formulaire
    $post_id = 0;
    if (isset($_POST['post_id'])) {
    $post_id = intval($_POST['post_id']);
    } elseif (isset($_POST['acff']['post_id'])) {
    $post_id = intval($_POST['acff']['post_id']);
    }

    // Vérifier que le post existe et que l'utilisateur a les permissions
    if ($post_id > 0) {
    $post = get_post($post_id);

    // Vérifier que le post existe
    if (!$post) {
    return $errors;
    }

    // Vérifier les capacités de l'utilisateur
    if (!current_user_can('edit_post', $post_id)) {
    return $errors;
    }

    // Stocker temporairement le post_id pour l'utiliser après l'upload
    add_filter('acf/upload_prefilter', function($errors) use ($post_id) {
    global $temp_post_id_for_gallery;
    $temp_post_id_for_gallery = $post_id;
    return $errors;
    });
    }

    return $errors;
    }

    // 3. Hook qui s'exécute après l'upload pour attacher l'image
    add_filter('acf/update_value/name=galerie', 'attach_uploaded_gallery_images', 10, 3);
    function attach_uploaded_gallery_images($value, $post_id, $field) {
    // Vérifier que la valeur n'est pas vide
    if (empty($value) || !is_array($value)) {
    return $value;
    }

    // Vérifier que le post_id est valide
    if (!is_numeric($post_id) || $post_id <= 0) {
    return $value;
    }

    // Vérifier que le post existe
    $post = get_post($post_id);
    if (!$post) {
    return $value;
    }

    // Vérifier les permissions de l'utilisateur
    if (!current_user_can('edit_post', $post_id)) {
    return $value;
    }

    // Parcourir et attacher les images
    foreach ($value as $image_id) {
    // Valider que c'est bien un ID numérique
    if (!is_numeric($image_id) || $image_id <= 0) {
    continue;
    }

    $image_id = intval($image_id);

    // Vérifier que l'image existe et est bien une pièce jointe
    if (get_post_type($image_id) !== 'attachment') {
    continue;
    }

    // Vérifier que c'est bien une image (pas un PDF ou autre)
    $mime_type = get_post_mime_type($image_id);
    if (strpos($mime_type, 'image/') !== 0) {
    continue;
    }

    // Vérifier que l'utilisateur peut éditer cette image
    if (!current_user_can('edit_post', $image_id)) {
    continue;
    }

    // Attacher l'image au post
    wp_update_post(array(
    'ID' => $image_id,
    'post_parent' => $post_id
    ));
    }

    return $value;
    }

    Hopefully it will be viable 😮

    Thread Starter graindepixel

    (@graindepixel)

    Dara….. You’re my hero of the day! I didn’t need a filter factory, and your plugin is just perfect, plus it’s packed with features for users. I dreamed of it, and Dara made it happen. A thousand sincere thanks; it will help us enormously with adoptions. The more user-friendly it is, the more likely we are to motivate people. A huge thank you. And thank you to Reddit, where I found the reference for this plugin ^_^

    And thank you for the quick response; it’s priceless.

    Thread Starter graindepixel

    (@graindepixel)

    C’est revenu dans l’ordre, à rien y comprendre 😮

    Thread Starter graindepixel

    (@graindepixel)

    First of all, thank you for the quality of the response, it is very, very complete. The 777 in emission was just the time to try, I immediately returned to 644.

    I am a fan of prime mover because often the other solutions have all caused me problems. PM is the only plugin that is as flexible.

    I managed after numerous attempts to find a solution thanks to an old backup of my wp-config.php that I simply replaced. And that unblocked everything.

    I still do not understand why the other sites are causing problems. But in my opinion I am good to redo a wp config.php on each one to unblock everything (if of course the problem comes from there).

    Thanks again for the qualitative response (sorry my english so bad :p )

Viewing 4 replies - 1 through 4 (of 4 total)