1

I am posting this in Wordpress because I am working to get the featured images of all the plugins in the wordpress repository.

http://plugins.svn.wordpress.org contains the images I want.

BUT, the extension for some files are .png and of some are .jpg

So, I want to show those images in my wordpress plugin using img src HTML tag.

But, how to determine whether the image extension is .jpg and .png and get the results accordingly?

I have already read about pathinfo and other PHP functions but those only tell us the extension of the image specified.

To make it more clear:

$image_extension = "http://plugins.svn.wordpress.org/" .  $plugin_slug .    
"/assets/" . "banner-772x250" . (and here the file extension, either .png or .jpg);

$plugin_slug refers to the slug of the plugin that I got from wordpress plugin API based on what the user inputs in the search bar.

For eg:, if I search Wordpress SEO by yoast in the above search bar (in the plugin), then I get its slug from Wordpress.org Plugin API, but I don't know whether the featured image's extension is .png or .jpg,

So, I want to get that extension info and then print it in $image_extension so that the featured image (banner) of that plugin shows up.

I hope I am clear in explaining my problem.

If anyone can help then please...

2
  • Hotlinking is not exactly a nice way to save money at the cost of an open source organization. Rethink your approach completely please.
    – fuxia
    May 7, 2015 at 8:09
  • @toscho This is not a commercial plugin nor I am uploading it in the plugins directory of wordpress. This is just for testing purposes on my localhost and hence I asked this question.
    – Paras Shah
    May 7, 2015 at 9:59

1 Answer 1

0

You could send a HEAD request for the allowed resources until you hit a status 200, cache the results (post meta, an option, whatever), and show the best match.

The allowed banner sizes and formats are:

'banner-1544x500.png',
'banner-772x250.png',
'banner-1544x500.jpg',
'banner-772x250.jpg',

The allowed icon sizes and formats:

'icon.svg',
'icon-256x256.jpg',
'icon-128x128.png',

Sending a HEAD request can be done either by using wp_remote_head() or by using cURL.

So let's just abstract that away:

namespace Wpse\Http;

interface Remote_Resource
{
    /**
     * @param  string $url
     * @return bool
     */
    public function exists( $url );
}

final class Curl_Remote_Resource implements Remote_Resource
{
    /**
     * @param  string $url
     * @return bool
     */
    public function exists( $url )
    {
        $status = `curl -s -o /dev/null -I -w "%{http_code}" $url`;

        return 200 === (int) $status;
    }
}

Now we need just a class to use that check against the different image variations:

/**
 * Find banners and icon for a plugin hosted on wordpress.org
 *
 *
 * @version 2015.08.09
 * @author  toscho
 * @license MIT
 */
final class Wp_Org_Assets
{
    /**
     * @see https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/
     *
     * @var array
     */
    private $banners = [
        'banner-1544x500.png',
        'banner-772x250.png',
        'banner-1544x500.jpg',
        'banner-772x250.jpg',
    ];
    /**
     * @see https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/
     *
     * @var array
     */
    private $icons = [
        // Do not embed unknown SVG in your HTML. Use the `<img>` element instead!
        'icon.svg',
        'icon-256x256.jpg',
        'icon-128x128.png',
    ];

    /**
     * @var string
     */
    private $url;

    /**
     * @var \Wpse\Http\Remote_Resource
     */
    private $remote;

    /**
     * This constructor is private, use one of the static "named constructors" instead.
     *
     * @see http://verraes.net/2014/06/named-constructors-in-php/
     * @param string                     $slug Plugin slug like "multilingual-press"
     * @param \Wpse\Http\Remote_Resource $remote
     */
    private function __construct( $slug, Remote_Resource $remote )
    {
        $this->url    = "https://plugins.svn.wordpress.org/$slug/assets";
        $this->remote = $remote;
    }

    /**
     * @param string                     $slug Plugin slug like "multilingual-press"
     * @param \Wpse\Http\Remote_Resource $remote
     * @return static
     */
    public static function from_slug( $slug, Remote_Resource $remote )
    {
        return new static( $slug, $remote );
    }

    /**
     * @param string                     $url Plugin URL like "https://wordpress.org/plugins/multilingual-press/"
     * @param \Wpse\Http\Remote_Resource $remote
     * @return static
     */
    public static function from_main_url( $url, Remote_Resource $remote )
    {
        $matches = [];
        preg_match( '~/plugins/([^/]*)/~', $url, $matches );

        return new static( $matches[ 1 ], $remote );
    }

    /**
     * @param string                     $url SVN URL like "https://plugins.svn.wordpress.org/multilingual-press/"
     * @param \Wpse\Http\Remote_Resource $remote
     * @return static
     */
    public static function from_svn_url( $url, Remote_Resource $remote )
    {
        $matches = [];
        preg_match( '~svn\.wordpress\.org/([^/]*)/~', $url, $matches );

        return new static( $matches[ 1 ], $remote );
    }

    /**
     * @return string
     */
    public function banner()
    {
        return $this->find( $this->banners );
    }

    /**
     * @return string
     */
    public function icon()
    {
        return $this->find( $this->icons );
    }

    /**
     * @param array $list
     * @return string
     */
    private function find( array $list )
    {
        foreach ( $list as $file )
            if ( $this->remote->exists( "$this->url/$file" ) )
                return "$this->url/$file";

        return '';
    }
}

And then we can use these classes in a few lines of code:

$assets = Wp_Org_Assets::from_main_url( 'https://wordpress.org/plugins/multilingual-press/', new Curl_Remote_Resource );
$files  = [
    'banner' => $assets->banner(),
    'icon'   => $assets->icon()
];

foreach ( $files as $name => $url )
{
    if ( $url )
        printf(
            '%1$s: <a href="%2$s"><code>%2$s</code></a><br><img style="max-width: 100%%" src="%2$s"><br>',
            $name,
            $url
        );
    else
        printf(
            'No %s found.<br>',
            $name
        );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.