cabrerahector / wordpress-popular-posts

WordPress Popular Posts - A highly customizable WordPress widget that displays your most popular posts.
https://wordpress.org/plugins/wordpress-popular-posts/
GNU General Public License v2.0
280 stars 82 forks source link

Custom Field Images: add support for ACF Image fields #258

Closed cabrerahector closed 4 years ago

cabrerahector commented 4 years ago

Is your feature request related to a problem? Please describe.

As of version 5.1.0, the Custom Field option as a image source to generate/retrieve the post thumbnail (see How does WordPress Popular Posts pick my posts' thumbnails?) expects to receive a valid image URL. If the custom field stores other type of data (eg. ACF image fields can return either an attachment ID or an array) WordPress Popular Posts won't display the thumbnail (naturally, it expects an actual URL.) See: Pick images from custom fields.

Describe the solution you'd like

The plugin could check the provided value and if it's a number then attempt to fetch an image url via wp_get_attachment_image_src() for example.

Image.php, around line 186:

if ( ! $thumb_url || ! $this->is_image_url($thumb_url) ) {
    // Is this an attachment ID instead of an image URL?
    $thumb_url = $thumb_url && Helper::is_number($thumb_url) ? wp_get_attachment_image_src($thumb_url, 'full') : null;
    $thumb_url = is_array($thumb_url) ? $thumb_url[0] : null;
}

if ( $thumb_url ) {
    /**
        * Filters CSS classes assigned to the thumbnail
        *
        * @since   5.0.0
        * @param   string  Original ALT attribute
        * @param   int     The post ID
        * @return  string  The new ALT attribute
        */
    $alt = apply_filters(
        'wpp_thumbnail_alt_attribute',
        '',
        $post_object->id
    );
}

Image.php, around line 259:

if ( ! $thumb_url || ! $this->is_image_url($thumb_url) ) {
    // Is this an attachment ID instead of an image URL?
    // If so, try to fetch the image
    $thumb_url = $thumb_url && Helper::is_number($thumb_url) ? wp_get_attachment_image_src($thumb_url, 'full') : null;
    $thumb_url = is_array($thumb_url) ? $thumb_url[0] : null;
}

if ( $thumb_url && $this->is_image_url($thumb_url) ) {
    $file_path = $this->url_to_path($thumb_url, $post_object->id);
}