Edit File by line
/home/barbar84/www/wp-inclu...
File: media.php
return false;
[1500] Fix | Delete
}
[1501] Fix | Delete
[1502] Fix | Delete
// Setup the default 'sizes' attribute.
[1503] Fix | Delete
$sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $width );
[1504] Fix | Delete
[1505] Fix | Delete
/**
[1506] Fix | Delete
* Filters the output of 'wp_calculate_image_sizes()'.
[1507] Fix | Delete
*
[1508] Fix | Delete
* @since 4.4.0
[1509] Fix | Delete
*
[1510] Fix | Delete
* @param string $sizes A source size value for use in a 'sizes' attribute.
[1511] Fix | Delete
* @param string|int[] $size Requested image size. Can be any registered image size name, or
[1512] Fix | Delete
* an array of width and height values in pixels (in that order).
[1513] Fix | Delete
* @param string|null $image_src The URL to the image file or null.
[1514] Fix | Delete
* @param array|null $image_meta The image meta data as returned by wp_get_attachment_metadata() or null.
[1515] Fix | Delete
* @param int $attachment_id Image attachment ID of the original image or 0.
[1516] Fix | Delete
*/
[1517] Fix | Delete
return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id );
[1518] Fix | Delete
}
[1519] Fix | Delete
[1520] Fix | Delete
/**
[1521] Fix | Delete
* Determines if the image meta data is for the image source file.
[1522] Fix | Delete
*
[1523] Fix | Delete
* The image meta data is retrieved by attachment post ID. In some cases the post IDs may change.
[1524] Fix | Delete
* For example when the website is exported and imported at another website. Then the
[1525] Fix | Delete
* attachment post IDs that are in post_content for the exported website may not match
[1526] Fix | Delete
* the same attachments at the new website.
[1527] Fix | Delete
*
[1528] Fix | Delete
* @since 5.5.0
[1529] Fix | Delete
*
[1530] Fix | Delete
* @param string $image_location The full path or URI to the image file.
[1531] Fix | Delete
* @param array $image_meta The attachment meta data as returned by 'wp_get_attachment_metadata()'.
[1532] Fix | Delete
* @param int $attachment_id Optional. The image attachment ID. Default 0.
[1533] Fix | Delete
* @return bool Whether the image meta is for this image file.
[1534] Fix | Delete
*/
[1535] Fix | Delete
function wp_image_file_matches_image_meta( $image_location, $image_meta, $attachment_id = 0 ) {
[1536] Fix | Delete
$match = false;
[1537] Fix | Delete
[1538] Fix | Delete
// Ensure the $image_meta is valid.
[1539] Fix | Delete
if ( isset( $image_meta['file'] ) && strlen( $image_meta['file'] ) > 4 ) {
[1540] Fix | Delete
// Remove quiery args if image URI.
[1541] Fix | Delete
list( $image_location ) = explode( '?', $image_location );
[1542] Fix | Delete
[1543] Fix | Delete
// Check if the relative image path from the image meta is at the end of $image_location.
[1544] Fix | Delete
if ( strrpos( $image_location, $image_meta['file'] ) === strlen( $image_location ) - strlen( $image_meta['file'] ) ) {
[1545] Fix | Delete
$match = true;
[1546] Fix | Delete
} else {
[1547] Fix | Delete
// Retrieve the uploads sub-directory from the full size image.
[1548] Fix | Delete
$dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
[1549] Fix | Delete
[1550] Fix | Delete
if ( $dirname ) {
[1551] Fix | Delete
$dirname = trailingslashit( $dirname );
[1552] Fix | Delete
}
[1553] Fix | Delete
[1554] Fix | Delete
if ( ! empty( $image_meta['original_image'] ) ) {
[1555] Fix | Delete
$relative_path = $dirname . $image_meta['original_image'];
[1556] Fix | Delete
[1557] Fix | Delete
if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) {
[1558] Fix | Delete
$match = true;
[1559] Fix | Delete
}
[1560] Fix | Delete
}
[1561] Fix | Delete
[1562] Fix | Delete
if ( ! $match && ! empty( $image_meta['sizes'] ) ) {
[1563] Fix | Delete
foreach ( $image_meta['sizes'] as $image_size_data ) {
[1564] Fix | Delete
$relative_path = $dirname . $image_size_data['file'];
[1565] Fix | Delete
[1566] Fix | Delete
if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) {
[1567] Fix | Delete
$match = true;
[1568] Fix | Delete
break;
[1569] Fix | Delete
}
[1570] Fix | Delete
}
[1571] Fix | Delete
}
[1572] Fix | Delete
}
[1573] Fix | Delete
}
[1574] Fix | Delete
[1575] Fix | Delete
/**
[1576] Fix | Delete
* Filters whether an image path or URI matches image meta.
[1577] Fix | Delete
*
[1578] Fix | Delete
* @since 5.5.0
[1579] Fix | Delete
*
[1580] Fix | Delete
* @param bool $match Whether the image relative path from the image meta
[1581] Fix | Delete
* matches the end of the URI or path to the image file.
[1582] Fix | Delete
* @param string $image_location Full path or URI to the tested image file.
[1583] Fix | Delete
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
[1584] Fix | Delete
* @param int $attachment_id The image attachment ID or 0 if not supplied.
[1585] Fix | Delete
*/
[1586] Fix | Delete
return apply_filters( 'wp_image_file_matches_image_meta', $match, $image_location, $image_meta, $attachment_id );
[1587] Fix | Delete
}
[1588] Fix | Delete
[1589] Fix | Delete
/**
[1590] Fix | Delete
* Determines an image's width and height dimensions based on the source file.
[1591] Fix | Delete
*
[1592] Fix | Delete
* @since 5.5.0
[1593] Fix | Delete
*
[1594] Fix | Delete
* @param string $image_src The image source file.
[1595] Fix | Delete
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
[1596] Fix | Delete
* @param int $attachment_id Optional. The image attachment ID. Default 0.
[1597] Fix | Delete
* @return array|false Array with first element being the width and second element being the height,
[1598] Fix | Delete
* or false if dimensions cannot be determined.
[1599] Fix | Delete
*/
[1600] Fix | Delete
function wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id = 0 ) {
[1601] Fix | Delete
$dimensions = false;
[1602] Fix | Delete
[1603] Fix | Delete
// Is it a full size image?
[1604] Fix | Delete
if (
[1605] Fix | Delete
isset( $image_meta['file'] ) &&
[1606] Fix | Delete
strpos( $image_src, wp_basename( $image_meta['file'] ) ) !== false
[1607] Fix | Delete
) {
[1608] Fix | Delete
$dimensions = array(
[1609] Fix | Delete
(int) $image_meta['width'],
[1610] Fix | Delete
(int) $image_meta['height'],
[1611] Fix | Delete
);
[1612] Fix | Delete
}
[1613] Fix | Delete
[1614] Fix | Delete
if ( ! $dimensions && ! empty( $image_meta['sizes'] ) ) {
[1615] Fix | Delete
$src_filename = wp_basename( $image_src );
[1616] Fix | Delete
[1617] Fix | Delete
foreach ( $image_meta['sizes'] as $image_size_data ) {
[1618] Fix | Delete
if ( $src_filename === $image_size_data['file'] ) {
[1619] Fix | Delete
$dimensions = array(
[1620] Fix | Delete
(int) $image_size_data['width'],
[1621] Fix | Delete
(int) $image_size_data['height'],
[1622] Fix | Delete
);
[1623] Fix | Delete
[1624] Fix | Delete
break;
[1625] Fix | Delete
}
[1626] Fix | Delete
}
[1627] Fix | Delete
}
[1628] Fix | Delete
[1629] Fix | Delete
/**
[1630] Fix | Delete
* Filters the 'wp_image_src_get_dimensions' value.
[1631] Fix | Delete
*
[1632] Fix | Delete
* @since 5.7.0
[1633] Fix | Delete
*
[1634] Fix | Delete
* @param array|false $dimensions Array with first element being the width
[1635] Fix | Delete
* and second element being the height, or
[1636] Fix | Delete
* false if dimensions could not be determined.
[1637] Fix | Delete
* @param string $image_src The image source file.
[1638] Fix | Delete
* @param array $image_meta The image meta data as returned by
[1639] Fix | Delete
* 'wp_get_attachment_metadata()'.
[1640] Fix | Delete
* @param int $attachment_id The image attachment ID. Default 0.
[1641] Fix | Delete
*/
[1642] Fix | Delete
return apply_filters( 'wp_image_src_get_dimensions', $dimensions, $image_src, $image_meta, $attachment_id );
[1643] Fix | Delete
}
[1644] Fix | Delete
[1645] Fix | Delete
/**
[1646] Fix | Delete
* Adds 'srcset' and 'sizes' attributes to an existing 'img' element.
[1647] Fix | Delete
*
[1648] Fix | Delete
* @since 4.4.0
[1649] Fix | Delete
*
[1650] Fix | Delete
* @see wp_calculate_image_srcset()
[1651] Fix | Delete
* @see wp_calculate_image_sizes()
[1652] Fix | Delete
*
[1653] Fix | Delete
* @param string $image An HTML 'img' element to be filtered.
[1654] Fix | Delete
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
[1655] Fix | Delete
* @param int $attachment_id Image attachment ID.
[1656] Fix | Delete
* @return string Converted 'img' element with 'srcset' and 'sizes' attributes added.
[1657] Fix | Delete
*/
[1658] Fix | Delete
function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) {
[1659] Fix | Delete
// Ensure the image meta exists.
[1660] Fix | Delete
if ( empty( $image_meta['sizes'] ) ) {
[1661] Fix | Delete
return $image;
[1662] Fix | Delete
}
[1663] Fix | Delete
[1664] Fix | Delete
$image_src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : '';
[1665] Fix | Delete
list( $image_src ) = explode( '?', $image_src );
[1666] Fix | Delete
[1667] Fix | Delete
// Return early if we couldn't get the image source.
[1668] Fix | Delete
if ( ! $image_src ) {
[1669] Fix | Delete
return $image;
[1670] Fix | Delete
}
[1671] Fix | Delete
[1672] Fix | Delete
// Bail early if an image has been inserted and later edited.
[1673] Fix | Delete
if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) &&
[1674] Fix | Delete
strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) {
[1675] Fix | Delete
[1676] Fix | Delete
return $image;
[1677] Fix | Delete
}
[1678] Fix | Delete
[1679] Fix | Delete
$width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0;
[1680] Fix | Delete
$height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0;
[1681] Fix | Delete
[1682] Fix | Delete
if ( $width && $height ) {
[1683] Fix | Delete
$size_array = array( $width, $height );
[1684] Fix | Delete
} else {
[1685] Fix | Delete
$size_array = wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id );
[1686] Fix | Delete
if ( ! $size_array ) {
[1687] Fix | Delete
return $image;
[1688] Fix | Delete
}
[1689] Fix | Delete
}
[1690] Fix | Delete
[1691] Fix | Delete
$srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );
[1692] Fix | Delete
[1693] Fix | Delete
if ( $srcset ) {
[1694] Fix | Delete
// Check if there is already a 'sizes' attribute.
[1695] Fix | Delete
$sizes = strpos( $image, ' sizes=' );
[1696] Fix | Delete
[1697] Fix | Delete
if ( ! $sizes ) {
[1698] Fix | Delete
$sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
[1699] Fix | Delete
}
[1700] Fix | Delete
}
[1701] Fix | Delete
[1702] Fix | Delete
if ( $srcset && $sizes ) {
[1703] Fix | Delete
// Format the 'srcset' and 'sizes' string and escape attributes.
[1704] Fix | Delete
$attr = sprintf( ' srcset="%s"', esc_attr( $srcset ) );
[1705] Fix | Delete
[1706] Fix | Delete
if ( is_string( $sizes ) ) {
[1707] Fix | Delete
$attr .= sprintf( ' sizes="%s"', esc_attr( $sizes ) );
[1708] Fix | Delete
}
[1709] Fix | Delete
[1710] Fix | Delete
// Add the srcset and sizes attributes to the image markup.
[1711] Fix | Delete
return preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $attr . ' />', $image );
[1712] Fix | Delete
}
[1713] Fix | Delete
[1714] Fix | Delete
return $image;
[1715] Fix | Delete
}
[1716] Fix | Delete
[1717] Fix | Delete
/**
[1718] Fix | Delete
* Determines whether to add the `loading` attribute to the specified tag in the specified context.
[1719] Fix | Delete
*
[1720] Fix | Delete
* @since 5.5.0
[1721] Fix | Delete
* @since 5.7.0 Now returns `true` by default for `iframe` tags.
[1722] Fix | Delete
*
[1723] Fix | Delete
* @param string $tag_name The tag name.
[1724] Fix | Delete
* @param string $context Additional context, like the current filter name
[1725] Fix | Delete
* or the function name from where this was called.
[1726] Fix | Delete
* @return bool Whether to add the attribute.
[1727] Fix | Delete
*/
[1728] Fix | Delete
function wp_lazy_loading_enabled( $tag_name, $context ) {
[1729] Fix | Delete
// By default add to all 'img' and 'iframe' tags.
[1730] Fix | Delete
// See https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-loading
[1731] Fix | Delete
// See https://html.spec.whatwg.org/multipage/iframe-embed-object.html#attr-iframe-loading
[1732] Fix | Delete
$default = ( 'img' === $tag_name || 'iframe' === $tag_name );
[1733] Fix | Delete
[1734] Fix | Delete
/**
[1735] Fix | Delete
* Filters whether to add the `loading` attribute to the specified tag in the specified context.
[1736] Fix | Delete
*
[1737] Fix | Delete
* @since 5.5.0
[1738] Fix | Delete
*
[1739] Fix | Delete
* @param bool $default Default value.
[1740] Fix | Delete
* @param string $tag_name The tag name.
[1741] Fix | Delete
* @param string $context Additional context, like the current filter name
[1742] Fix | Delete
* or the function name from where this was called.
[1743] Fix | Delete
*/
[1744] Fix | Delete
return (bool) apply_filters( 'wp_lazy_loading_enabled', $default, $tag_name, $context );
[1745] Fix | Delete
}
[1746] Fix | Delete
[1747] Fix | Delete
/**
[1748] Fix | Delete
* Filters specific tags in post content and modifies their markup.
[1749] Fix | Delete
*
[1750] Fix | Delete
* Modifies HTML tags in post content to include new browser and HTML technologies
[1751] Fix | Delete
* that may not have existed at the time of post creation. These modifications currently
[1752] Fix | Delete
* include adding `srcset`, `sizes`, and `loading` attributes to `img` HTML tags, as well
[1753] Fix | Delete
* as adding `loading` attributes to `iframe` HTML tags.
[1754] Fix | Delete
* Future similar optimizations should be added/expected here.
[1755] Fix | Delete
*
[1756] Fix | Delete
* @since 5.5.0
[1757] Fix | Delete
* @since 5.7.0 Now supports adding `loading` attributes to `iframe` tags.
[1758] Fix | Delete
*
[1759] Fix | Delete
* @see wp_img_tag_add_width_and_height_attr()
[1760] Fix | Delete
* @see wp_img_tag_add_srcset_and_sizes_attr()
[1761] Fix | Delete
* @see wp_img_tag_add_loading_attr()
[1762] Fix | Delete
* @see wp_iframe_tag_add_loading_attr()
[1763] Fix | Delete
*
[1764] Fix | Delete
* @param string $content The HTML content to be filtered.
[1765] Fix | Delete
* @param string $context Optional. Additional context to pass to the filters.
[1766] Fix | Delete
* Defaults to `current_filter()` when not set.
[1767] Fix | Delete
* @return string Converted content with images modified.
[1768] Fix | Delete
*/
[1769] Fix | Delete
function wp_filter_content_tags( $content, $context = null ) {
[1770] Fix | Delete
if ( null === $context ) {
[1771] Fix | Delete
$context = current_filter();
[1772] Fix | Delete
}
[1773] Fix | Delete
[1774] Fix | Delete
$add_img_loading_attr = wp_lazy_loading_enabled( 'img', $context );
[1775] Fix | Delete
$add_iframe_loading_attr = wp_lazy_loading_enabled( 'iframe', $context );
[1776] Fix | Delete
[1777] Fix | Delete
if ( ! preg_match_all( '/<(img|iframe)\s[^>]+>/', $content, $matches, PREG_SET_ORDER ) ) {
[1778] Fix | Delete
return $content;
[1779] Fix | Delete
}
[1780] Fix | Delete
[1781] Fix | Delete
// List of the unique `img` tags found in $content.
[1782] Fix | Delete
$images = array();
[1783] Fix | Delete
[1784] Fix | Delete
// List of the unique `iframe` tags found in $content.
[1785] Fix | Delete
$iframes = array();
[1786] Fix | Delete
[1787] Fix | Delete
foreach ( $matches as $match ) {
[1788] Fix | Delete
list( $tag, $tag_name ) = $match;
[1789] Fix | Delete
[1790] Fix | Delete
switch ( $tag_name ) {
[1791] Fix | Delete
case 'img':
[1792] Fix | Delete
if ( preg_match( '/wp-image-([0-9]+)/i', $tag, $class_id ) ) {
[1793] Fix | Delete
$attachment_id = absint( $class_id[1] );
[1794] Fix | Delete
[1795] Fix | Delete
if ( $attachment_id ) {
[1796] Fix | Delete
// If exactly the same image tag is used more than once, overwrite it.
[1797] Fix | Delete
// All identical tags will be replaced later with 'str_replace()'.
[1798] Fix | Delete
$images[ $tag ] = $attachment_id;
[1799] Fix | Delete
break;
[1800] Fix | Delete
}
[1801] Fix | Delete
}
[1802] Fix | Delete
$images[ $tag ] = 0;
[1803] Fix | Delete
break;
[1804] Fix | Delete
case 'iframe':
[1805] Fix | Delete
$iframes[ $tag ] = 0;
[1806] Fix | Delete
break;
[1807] Fix | Delete
}
[1808] Fix | Delete
}
[1809] Fix | Delete
[1810] Fix | Delete
// Reduce the array to unique attachment IDs.
[1811] Fix | Delete
$attachment_ids = array_unique( array_filter( array_values( $images ) ) );
[1812] Fix | Delete
[1813] Fix | Delete
if ( count( $attachment_ids ) > 1 ) {
[1814] Fix | Delete
/*
[1815] Fix | Delete
* Warm the object cache with post and meta information for all found
[1816] Fix | Delete
* images to avoid making individual database calls.
[1817] Fix | Delete
*/
[1818] Fix | Delete
_prime_post_caches( $attachment_ids, false, true );
[1819] Fix | Delete
}
[1820] Fix | Delete
[1821] Fix | Delete
foreach ( $images as $image => $attachment_id ) {
[1822] Fix | Delete
$filtered_image = $image;
[1823] Fix | Delete
[1824] Fix | Delete
// Add 'width' and 'height' attributes if applicable.
[1825] Fix | Delete
if ( $attachment_id > 0 && false === strpos( $filtered_image, ' width=' ) && false === strpos( $filtered_image, ' height=' ) ) {
[1826] Fix | Delete
$filtered_image = wp_img_tag_add_width_and_height_attr( $filtered_image, $context, $attachment_id );
[1827] Fix | Delete
}
[1828] Fix | Delete
[1829] Fix | Delete
// Add 'srcset' and 'sizes' attributes if applicable.
[1830] Fix | Delete
if ( $attachment_id > 0 && false === strpos( $filtered_image, ' srcset=' ) ) {
[1831] Fix | Delete
$filtered_image = wp_img_tag_add_srcset_and_sizes_attr( $filtered_image, $context, $attachment_id );
[1832] Fix | Delete
}
[1833] Fix | Delete
[1834] Fix | Delete
// Add 'loading' attribute if applicable.
[1835] Fix | Delete
if ( $add_img_loading_attr && false === strpos( $filtered_image, ' loading=' ) ) {
[1836] Fix | Delete
$filtered_image = wp_img_tag_add_loading_attr( $filtered_image, $context );
[1837] Fix | Delete
}
[1838] Fix | Delete
[1839] Fix | Delete
if ( $filtered_image !== $image ) {
[1840] Fix | Delete
$content = str_replace( $image, $filtered_image, $content );
[1841] Fix | Delete
}
[1842] Fix | Delete
}
[1843] Fix | Delete
[1844] Fix | Delete
foreach ( $iframes as $iframe => $attachment_id ) {
[1845] Fix | Delete
$filtered_iframe = $iframe;
[1846] Fix | Delete
[1847] Fix | Delete
// Add 'loading' attribute if applicable.
[1848] Fix | Delete
if ( $add_iframe_loading_attr && false === strpos( $filtered_iframe, ' loading=' ) ) {
[1849] Fix | Delete
$filtered_iframe = wp_iframe_tag_add_loading_attr( $filtered_iframe, $context );
[1850] Fix | Delete
}
[1851] Fix | Delete
[1852] Fix | Delete
if ( $filtered_iframe !== $iframe ) {
[1853] Fix | Delete
$content = str_replace( $iframe, $filtered_iframe, $content );
[1854] Fix | Delete
}
[1855] Fix | Delete
}
[1856] Fix | Delete
[1857] Fix | Delete
return $content;
[1858] Fix | Delete
}
[1859] Fix | Delete
[1860] Fix | Delete
/**
[1861] Fix | Delete
* Adds `loading` attribute to an `img` HTML tag.
[1862] Fix | Delete
*
[1863] Fix | Delete
* @since 5.5.0
[1864] Fix | Delete
*
[1865] Fix | Delete
* @param string $image The HTML `img` tag where the attribute should be added.
[1866] Fix | Delete
* @param string $context Additional context to pass to the filters.
[1867] Fix | Delete
* @return string Converted `img` tag with `loading` attribute added.
[1868] Fix | Delete
*/
[1869] Fix | Delete
function wp_img_tag_add_loading_attr( $image, $context ) {
[1870] Fix | Delete
// Images should have source and dimension attributes for the `loading` attribute to be added.
[1871] Fix | Delete
if ( false === strpos( $image, ' src="' ) || false === strpos( $image, ' width="' ) || false === strpos( $image, ' height="' ) ) {
[1872] Fix | Delete
return $image;
[1873] Fix | Delete
}
[1874] Fix | Delete
[1875] Fix | Delete
/**
[1876] Fix | Delete
* Filters the `loading` attribute value to add to an image. Default `lazy`.
[1877] Fix | Delete
*
[1878] Fix | Delete
* Returning `false` or an empty string will not add the attribute.
[1879] Fix | Delete
* Returning `true` will add the default value.
[1880] Fix | Delete
*
[1881] Fix | Delete
* @since 5.5.0
[1882] Fix | Delete
*
[1883] Fix | Delete
* @param string|bool $value The `loading` attribute value. Returning a falsey value will result in
[1884] Fix | Delete
* the attribute being omitted for the image. Default 'lazy'.
[1885] Fix | Delete
* @param string $image The HTML `img` tag to be filtered.
[1886] Fix | Delete
* @param string $context Additional context about how the function was called or where the img tag is.
[1887] Fix | Delete
*/
[1888] Fix | Delete
$value = apply_filters( 'wp_img_tag_add_loading_attr', 'lazy', $image, $context );
[1889] Fix | Delete
[1890] Fix | Delete
if ( $value ) {
[1891] Fix | Delete
if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) {
[1892] Fix | Delete
$value = 'lazy';
[1893] Fix | Delete
}
[1894] Fix | Delete
[1895] Fix | Delete
return str_replace( '<img', '<img loading="' . esc_attr( $value ) . '"', $image );
[1896] Fix | Delete
}
[1897] Fix | Delete
[1898] Fix | Delete
return $image;
[1899] Fix | Delete
}
[1900] Fix | Delete
[1901] Fix | Delete
/**
[1902] Fix | Delete
* Adds `width` and `height` attributes to an `img` HTML tag.
[1903] Fix | Delete
*
[1904] Fix | Delete
* @since 5.5.0
[1905] Fix | Delete
*
[1906] Fix | Delete
* @param string $image The HTML `img` tag where the attribute should be added.
[1907] Fix | Delete
* @param string $context Additional context to pass to the filters.
[1908] Fix | Delete
* @param int $attachment_id Image attachment ID.
[1909] Fix | Delete
* @return string Converted 'img' element with 'width' and 'height' attributes added.
[1910] Fix | Delete
*/
[1911] Fix | Delete
function wp_img_tag_add_width_and_height_attr( $image, $context, $attachment_id ) {
[1912] Fix | Delete
$image_src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : '';
[1913] Fix | Delete
list( $image_src ) = explode( '?', $image_src );
[1914] Fix | Delete
[1915] Fix | Delete
// Return early if we couldn't get the image source.
[1916] Fix | Delete
if ( ! $image_src ) {
[1917] Fix | Delete
return $image;
[1918] Fix | Delete
}
[1919] Fix | Delete
[1920] Fix | Delete
/**
[1921] Fix | Delete
* Filters whether to add the missing `width` and `height` HTML attributes to the img tag. Default `true`.
[1922] Fix | Delete
*
[1923] Fix | Delete
* Returning anything else than `true` will not add the attributes.
[1924] Fix | Delete
*
[1925] Fix | Delete
* @since 5.5.0
[1926] Fix | Delete
*
[1927] Fix | Delete
* @param bool $value The filtered value, defaults to `true`.
[1928] Fix | Delete
* @param string $image The HTML `img` tag where the attribute should be added.
[1929] Fix | Delete
* @param string $context Additional context about how the function was called or where the img tag is.
[1930] Fix | Delete
* @param int $attachment_id The image attachment ID.
[1931] Fix | Delete
*/
[1932] Fix | Delete
$add = apply_filters( 'wp_img_tag_add_width_and_height_attr', true, $image, $context, $attachment_id );
[1933] Fix | Delete
[1934] Fix | Delete
if ( true === $add ) {
[1935] Fix | Delete
$image_meta = wp_get_attachment_metadata( $attachment_id );
[1936] Fix | Delete
$size_array = wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id );
[1937] Fix | Delete
[1938] Fix | Delete
if ( $size_array ) {
[1939] Fix | Delete
$hw = trim( image_hwstring( $size_array[0], $size_array[1] ) );
[1940] Fix | Delete
return str_replace( '<img', "<img {$hw}", $image );
[1941] Fix | Delete
}
[1942] Fix | Delete
}
[1943] Fix | Delete
[1944] Fix | Delete
return $image;
[1945] Fix | Delete
}
[1946] Fix | Delete
[1947] Fix | Delete
/**
[1948] Fix | Delete
* Adds `srcset` and `sizes` attributes to an existing `img` HTML tag.
[1949] Fix | Delete
*
[1950] Fix | Delete
* @since 5.5.0
[1951] Fix | Delete
*
[1952] Fix | Delete
* @param string $image The HTML `img` tag where the attribute should be added.
[1953] Fix | Delete
* @param string $context Additional context to pass to the filters.
[1954] Fix | Delete
* @param int $attachment_id Image attachment ID.
[1955] Fix | Delete
* @return string Converted 'img' element with 'loading' attribute added.
[1956] Fix | Delete
*/
[1957] Fix | Delete
function wp_img_tag_add_srcset_and_sizes_attr( $image, $context, $attachment_id ) {
[1958] Fix | Delete
/**
[1959] Fix | Delete
* Filters whether to add the `srcset` and `sizes` HTML attributes to the img tag. Default `true`.
[1960] Fix | Delete
*
[1961] Fix | Delete
* Returning anything else than `true` will not add the attributes.
[1962] Fix | Delete
*
[1963] Fix | Delete
* @since 5.5.0
[1964] Fix | Delete
*
[1965] Fix | Delete
* @param bool $value The filtered value, defaults to `true`.
[1966] Fix | Delete
* @param string $image The HTML `img` tag where the attribute should be added.
[1967] Fix | Delete
* @param string $context Additional context about how the function was called or where the img tag is.
[1968] Fix | Delete
* @param int $attachment_id The image attachment ID.
[1969] Fix | Delete
*/
[1970] Fix | Delete
$add = apply_filters( 'wp_img_tag_add_srcset_and_sizes_attr', true, $image, $context, $attachment_id );
[1971] Fix | Delete
[1972] Fix | Delete
if ( true === $add ) {
[1973] Fix | Delete
$image_meta = wp_get_attachment_metadata( $attachment_id );
[1974] Fix | Delete
return wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id );
[1975] Fix | Delete
}
[1976] Fix | Delete
[1977] Fix | Delete
return $image;
[1978] Fix | Delete
}
[1979] Fix | Delete
[1980] Fix | Delete
/**
[1981] Fix | Delete
* Adds `loading` attribute to an `iframe` HTML tag.
[1982] Fix | Delete
*
[1983] Fix | Delete
* @since 5.7.0
[1984] Fix | Delete
*
[1985] Fix | Delete
* @param string $iframe The HTML `iframe` tag where the attribute should be added.
[1986] Fix | Delete
* @param string $context Additional context to pass to the filters.
[1987] Fix | Delete
* @return string Converted `iframe` tag with `loading` attribute added.
[1988] Fix | Delete
*/
[1989] Fix | Delete
function wp_iframe_tag_add_loading_attr( $iframe, $context ) {
[1990] Fix | Delete
// Iframes with fallback content (see `wp_filter_oembed_result()`) should not be lazy-loaded because they are
[1991] Fix | Delete
// visually hidden initially.
[1992] Fix | Delete
if ( false !== strpos( $iframe, ' data-secret="' ) ) {
[1993] Fix | Delete
return $iframe;
[1994] Fix | Delete
}
[1995] Fix | Delete
[1996] Fix | Delete
// Iframes should have source and dimension attributes for the `loading` attribute to be added.
[1997] Fix | Delete
if ( false === strpos( $iframe, ' src="' ) || false === strpos( $iframe, ' width="' ) || false === strpos( $iframe, ' height="' ) ) {
[1998] Fix | Delete
return $iframe;
[1999] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function