我需要与默认WordPress相同的功能wp_calculate_image_srcset()
方法返回srcset的字符串:
image-300x200.jpg 300w, image-600x400.jpg 600w, ...
但它应该返回一个数组而不是字符串,其结构类似于:$image_sizes = array(
array(
\'url\' => \'image-300x200.jpg\',
\'width\' => 300,
\'height\' => 200
),
array(
\'url\' => \'image-600x400.jpg\',
\'width\' => 600,
\'height\' => 400
),
array(
\'url\' => \'image.jpg\',
\'width\' => 1200,
\'height\' => 800
)
);
我知道我可以解析字符串本身,但我想知道它是否安全?我正在制作的插件是公共的,可以在任何服务器上与其他可能修改此字符串的插件一起使用。Source of wp_calculate_image_srcset
看起来很复杂,并且使用了一些“\\u private”方法。
这个wp_calculate_image_srcset
也在使用max_srcset_image_width
过滤器(限制图像的最大大小),我需要以某种方式避免它。remove\\u filter->execute method->add\\u filter是跳过它的唯一方法吗?
Edit: 我最终还是这样做了(如果这是个坏主意,请告诉我):
class MyClass {
public $image_items;
function __construct() {
add_filter(\'wp_calculate_image_srcset\', array($this, \'filter_my_srcset\'), 10, 5);
wp_calculate_image_srcset( ... );
remove_filter(\'wp_calculate_image_srcset\', array($this, \'filter_my_srcset\'));
// print_r( $this->image_items );
}
public function filter_my_srcset( $source, ... ) {
$this->image_items = $source;
return $source;
}
}