/** * Returns a 'srcset' attribute. * * @since 2.1.0 * @deprecated 3.0.0 Use 'wp_get_attachment_image_srcset()' * @see 'wp_get_attachment_image_sizes()' * @see 'wp_calculate_image_srcset()' * * @param int $id Image attachment ID. * @param array|string $size Image size. Accepts any valid image size, or an array of width and height * values in pixels (in that order). Default 'medium'. * @return string|bool A full 'srcset' string or false. */ function tevkori_get_srcset_string($id, $size = 'medium') { _deprecated_function(__FUNCTION__, '3.0.0', 'wp_get_attachment_image_srcset()'); if (has_filter('tevkori_srcset_array')) { $srcset_value = tevkori_get_srcset($id, $size); return $srcset_value ? 'srcset="' . $srcset_value . '"' : false; } else { $srcset_value = wp_get_attachment_image_srcset($id, $size); return $srcset_value ? 'srcset="' . $srcset_value . '"' : false; } }
/** * Ajax handler for updating the srcset when an image is changed in the editor. * * @since 2.3.0 * * @return string A sourcest value. */ function tevkori_ajax_srcset() { // Bail early if no post ID is passed. if (empty($_POST['postID'])) { return; } $postID = (int) $_POST['postID']; if (!$postID) { return; } // Grab the image size being passed from the AJAX request. $size = empty($_POST['size']) ? $_POST['size'] : ''; // Get the srcset value for our image. $srcset = tevkori_get_srcset($postID, $size); // For AJAX requests, we echo the result and then die. wp_send_json($srcset); }
/** * @expectedDeprecated tevkori_get_srcset * @expectedDeprecated tevkori_get_srcset_array */ function test_tevkori_get_srcset_single_srcset() { // Make an image. $id = self::$large_id; /* * In our tests, thumbnails would only return a single srcset candidate, * in which case we don't bother returning a srcset array. */ $this->assertTrue(1 === count(tevkori_get_srcset_array($id, 'thumbnail'))); $this->assertFalse(tevkori_get_srcset($id, 'thumbnail')); }
/** * Filter to add 'srcset' and 'sizes' attributes to post thumbnails and gallery images. * * @see wp_get_attachment_image_attributes * @return array Attributes for image. */ function tevkori_filter_attachment_image_attributes($attr, $attachment, $size) { if (!isset($attr['srcset'])) { $srcset = tevkori_get_srcset($attachment->ID, $size); // Set the 'srcset' attribute if one was returned. if ($srcset) { $attr['srcset'] = $srcset; if (!isset($attr['sizes'])) { $sizes = tevkori_get_sizes($attachment->ID, $size); // Set the 'sizes' attribute if sizes were returned. if ($sizes) { $attr['sizes'] = $sizes; } } } } return $attr; }
function test_tevkori_get_srcset_single_srcset() { // make an image $id = $this->_test_img(); // In our tests, thumbnails would only return a single srcset candidate, // in which case we don't bother returning a srcset array. $sizes = tevkori_get_srcset($id, 'thumbnail'); $this->assertTrue(1 === count(tevkori_get_srcset_array($id, 'thumbnail'))); $this->assertFalse(tevkori_get_srcset($id, 'thumbnail')); }