您可以发送HEAD请求allowed resources 直到你达到状态200
, cache 结果(post meta、选项等),并显示最佳匹配。
允许的横幅大小和格式为:
\'banner-1544x500.png\',
\'banner-772x250.png\',
\'banner-1544x500.jpg\',
\'banner-772x250.jpg\',
允许的图标大小和格式:
\'icon.svg\',
\'icon-256x256.jpg\',
\'icon-128x128.png\',
发送HEAD请求可以通过使用
wp_remote_head()
或者使用cURL。
让我们把它抽象出来:
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;
}
}
现在,我们只需要一个类来针对不同的图像变化使用该检查:
/**
* 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 \'\';
}
}
然后我们可以在几行代码中使用这些类:
$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
);
}