web-dev-qa-db-fra.com

Comment vérifier si une image en pièce jointe existe avant de la télécharger

J'essaie de télécharger par programme des images sur WP en vérifiant si une image du même nom existe déjà. Je le fais depuis un plugin, dans le panneau d'administration via add_submenu_page().

J'utilise foreach() pour télécharger 2 images par publication, et je leur donne un nom basé sur le slug de ma publication, mais une seule d'entre elles est correctement trouvée dans WP et omise, l'autre est toujours re-téléchargée. à WP même s'il existe.

Voici le code pertinent:

// sample vars
$slug = 'my-post-slug';
$card['img'] = 'http://example.com/img1.jpg';
$card['imgGold'] = 'http://example.com/img2.jpg';
// end sample vars

$images = '';
$images[] = $card['img'];
$images[] = $card['imgGold'];
foreach ( $images as $url ) {

    // check if current url is for regular or gold image
    if ( $card['imgGold'] == $url ) {
        $desc = $slug .'-gold';
    } else {
        $desc = $slug;
    }

    // check if attachment already exists
    $attachment_args = array(
        'posts_per_page' => 1,
        'post_type'      => 'attachment',
        'name'           => $desc
    );
    $attachment_check = new Wp_Query( $attachment_args );

    // if attachment exists, reuse and update data
    if ( $attachment_check->have_posts() ) {

        echo 'Attachment <strong>'. $desc .'</strong> found, omitting download...<br>';

        // do stuff..

    // if attachment doesn't exist fetch it from url and save it
    } else {

        echo 'Attachment <strong>'. $desc .'</strong> not found, downloading...<br>';

        // handle image upload from url and assign to post
        $src = media_sideload_image( $url, $post_id, $desc, 'src' );

        // add post meta
        if ( $card['imgGold'] == $url  ) {
            add_post_meta( $post_id, 'imgGold', $src );
        } else {
            add_post_meta( $post_id, 'img', $src );
        }

    } // end attachment exists

} // end foreach image

L'image d'or est celle qui fonctionne comme prévu. Le standard est toujours re-uploadé. Aucune idée pourquoi cependant, toute aide appréciée, merci!

2
Yogensia

OK j'ai enfin trouvé le problème! J'essayais d'assigner le même slug à la pièce jointe et à la publication parente, et apparemment, WordPress n'en aura pas. Le "nom" dans la médiathèque de la pièce jointe était correct, mais la limace réelle obtenait un -2 à la fin.

Voici le code corrigé, simplement en rendant les slugs d’image uniques :

// sample vars
$slug = 'my-post-slug';
$card['img'] = 'http://example.com/img1.jpg';
$card['imgGold'] = 'http://example.com/img2.jpg';
// end sample vars

$images = '';
$images[] = $card['img'];
$images[] = $card['imgGold'];
foreach ( $images as $url ) {

    // check if current url is for regular or gold image
    if ( $card['imgGold'] == $url ) {
        $desc = $slug .'-img-gold';
    } else {
        $desc = $slug .'-img';
    }

    // check if attachment already exists
    $attachment_args = array(
        'posts_per_page' => 1,
        'post_type'      => 'attachment',
        'name'           => $desc
    );
    $attachment_check = new Wp_Query( $attachment_args );

    // if attachment exists, reuse and update data
    if ( $attachment_check->have_posts() ) {

        echo 'Attachment <strong>'. $desc .'</strong> found, omitting download...<br>';

        // do stuff..

    // if attachment doesn't exist fetch it from url and save it
    } else {

        echo 'Attachment <strong>'. $desc .'</strong> not found, downloading...<br>';

        // handle image upload from url and assign to post
        $src = media_sideload_image( $url, $post_id, $desc, 'src' );

        // add post meta
        if ( $card['imgGold'] == $url  ) {
            add_post_meta( $post_id, 'imgGold', $src );
        } else {
            add_post_meta( $post_id, 'img', $src );
        }

    } // end attachment exists

} // end foreach image
2
Yogensia