/** * Render the single post view HTML markup for all the suitable featured images in the given post. * * @since 1.0. * * @param int $post_id The id of the post to get the featured images from. * @param string $size1 The image size to use for a single featured image. * @param string $size2 The image size to use for two featured images. * @param array $args Array of arguments. * @return string The HTML markup. */ function oxford_get_the_single_featured_images($post_id = 0, $size1 = 'oxford_large', $size2 = 'oxford_medium', $args = array()) { $defaults = array('show_caption' => true, 'link_to_post' => false); $args = wp_parse_args($args, $defaults); $html = ''; $caption = ''; // Sanitize and validate the post id if (0 === absint($post_id)) { $post_id = get_the_ID(); } // Get the selected image ids $image_ids = oxford_get_featured_image_ids($post_id); if (!empty($image_ids)) { $error_link = oxford_is_wpcom() ? 'http://theme.wordpress.com/themedoc/oxford/' : get_edit_post_link() . '#featured-images-help'; $error_message = sprintf(_n('<strong>Admin:</strong> The featured image is not wide enough to display here.<br> Please make sure that your image is at least <strong>900 pixels wide</strong>. %s', '<strong>Admin:</strong> This featured image is not wide enough to display here.<br> Please make sure that both of your images are at least <strong>450 pixels wide</strong>. %s', count($image_ids), 'oxford'), sprintf('<a href="%1$s">%2$s</a>', esc_url($error_link), __('Learn more.', 'oxford'))); // One image if (1 === count($image_ids)) { $html .= '<div class="selected-images">'; $image_id = absint(array_shift($image_ids)); $image_html = wp_get_attachment_image($image_id, $size1, false, array('class' => 'selected-images-only')); if ('' !== $image_html) { if (true === $args['link_to_post']) { $html .= '<a href="' . esc_url(get_permalink($post_id)) . '">' . $image_html . '</a>'; } else { $html .= $image_html; } if (true === $args['show_caption'] && has_excerpt($image_id)) { $caption .= '<span class="selected-images-caption">' . get_post($image_id)->post_excerpt . '</span>'; } } else { if (current_user_can('edit_posts')) { // Image is not wide enough $html .= '<div class="selected-images-none">'; $html .= $error_message; $html .= '</div>'; } } $html .= $caption . '</div>'; return $html; } // Two images if (count($image_ids) > 1) { // Sanitize image ids $image_id_1 = absint(array_shift($image_ids)); $image_id_2 = absint(array_shift($image_ids)); // Get image dimensions $s2 = oxford_get_image_size_dimensions($size2); $image_src_1 = image_get_intermediate_size($image_id_1, false === $s2 ? $size2 : array($s2['width'], $s2['height'])); $image_src_2 = image_get_intermediate_size($image_id_2, false === $s2 ? $size2 : array($s2['width'], $s2['height'])); // Get height adjustments $ratio = 50; $image_style_1 = ''; $image_style_2 = ''; if (isset($image_src_1['height']) && isset($image_src_2['height'])) { if ($image_src_1['height'] < $image_src_2['height']) { $ratio = floor($image_src_1['height'] / $image_src_1['width'] * 50); $image_style_2 = 'top: -' . ($image_src_2['height'] - $image_src_1['height']) / 2 / $image_src_1['height'] * 100 . '%;'; } else { $ratio = floor($image_src_2['height'] / $image_src_2['width'] * 50); $image_style_1 = 'top: -' . ($image_src_1['height'] - $image_src_2['height']) / 2 / $image_src_2['height'] * 100 . '%;'; } } // Get image markup $image_html_1 = wp_get_attachment_image($image_id_1, $size2, false, array('class' => 'selected-images-one', 'style' => $image_style_1)); $image_html_2 = wp_get_attachment_image($image_id_2, $size2, false, array('class' => 'selected-images-two', 'style' => $image_style_2)); // Outer container $html .= '<div class="selected-images">'; // Inner container $html .= '<div class="selected-images-holder" style="height: 0; padding-bottom: ' . $ratio . '%;">'; // First image if ('' !== $image_html_1) { if (true === $args['link_to_post']) { $html .= '<a href="' . esc_url(get_permalink($post_id)) . '">' . $image_html_1 . '</a>'; } else { $html .= $image_html_1; } if (true === $args['show_caption'] && has_excerpt($image_id_1)) { $caption .= '<span class="selected-images-caption selected-images-caption-1" data-position="' . __('Left:', 'oxford') . '">' . get_post($image_id_1)->post_excerpt . '</span>'; } } else { if (current_user_can('edit_posts')) { // Image is not wide enough $html .= '<div class="selected-images-one selected-images-none">'; $html .= $error_message; $html .= '</div>'; } } // Second image if ('' !== $image_html_2) { if (true === $args['link_to_post']) { $html .= '<a href="' . esc_url(get_permalink($post_id)) . '">' . $image_html_2 . '</a>'; } else { $html .= $image_html_2; } if (true === $args['show_caption'] && has_excerpt($image_id_2)) { $caption .= '<span class="selected-images-caption selected-images-caption-2" data-position="' . __('Right:', 'oxford') . '">' . get_post($image_id_2)->post_excerpt . '</span>'; } } else { if (current_user_can('edit_posts')) { // Image is not wide enough $html .= '<div class="selected-images-two selected-images-none">'; $html .= $error_message; $html .= '</div>'; } } $html .= '</div>' . $caption . '</div>'; return $html; } } return $html; }
/** * Check if a particular size of an image from the media library meets minimum dimension requirements. * * @since 1.0. * * @param int $image_id The image id. * @param array $min_size The minimum width and height of the image. * @param string $image_size The defined image size to use when analyzing the image. * @return array|bool Image src, width, and height if true, otherwise false. */ function oxford_is_image_large_enough($image_id, $min_size = array(), $image_size = 'large') { if (empty($min_size)) { return false; } // Get the actual minimum dimensions necessary with the current aspect ratio $imagedata = wp_get_attachment_metadata($image_id); $size = oxford_get_image_size_dimensions($image_size); $dims = image_resize_dimensions($imagedata['width'], $imagedata['height'], $size['width'], $size['height']); if (false !== $dims) { // Try intermediate size first $image = image_get_intermediate_size($image_id, array($dims[4], $dims[5])); // It's large enough. if ($image['width'] >= $min_size[0] && $image['height'] >= $min_size[1]) { return $image; } } // Then try the original image if ($imagedata['width'] >= $min_size[0] && $imagedata['height'] >= $min_size[1]) { return $imagedata; } // Not large enough. return false; }