. */
$attributes = ( !empty( $attr['id'] ) ? ' id="' . esc_attr( $attr['id'] ) . '"' : '' );
$attributes .= ' class="wp-caption ' . esc_attr( $attr['align'] ) . '"';
$attributes .= ' style="width: ' . esc_attr( $attr['width'] ) . 'px"';
/* Open the caption
. */
$output = '
';
/* Allow shortcodes for the content the caption was created for. */
$output .= do_shortcode( $content );
/* Append the caption text. */
$output .= '
' . $attr['caption'] . '
';
/* Close the caption
. */
$output .= '
';
/* Return the formatted, clean caption. */
return $output;
}
add_filter( 'img_caption_shortcode', 'franz_cleaner_caption', 10, 3 );
/**
* Check if current post has custom page layout
*
* @return boolean
*/
function franz_has_custom_layout() {
global $post;
if ( ! function_exists( 'fjs_load_textdomain' ) ) return false;
if ( empty( $post ) ) return false;
if ( ! is_singular() ) return false;
if ( get_post_meta( $post->ID, 'panels_data', true ) ) return true;
else return false;
}
/**
* Output the custom layout if the current post has one
*
* @return boolean
*/
function franz_do_custom_layout() {
if ( franz_has_custom_layout() ) {
the_content();
get_footer();
exit();
}
}
add_action( 'franz_before_content', 'franz_do_custom_layout', 1000 );
/**
* Check if there is usable image in the post
*/
function franz_has_post_image( $post_id = '' ){
/* Get the post ID if not provided */
if ( ! $post_id ) $post_id = get_the_ID();
if ( has_post_thumbnail( $post_id ) ) return true;
if ( get_attached_media( 'image', $post_id ) ) return true;
if ( get_post_gallery( $post_id, false ) ) return true;
return false;
}
/**
* Get the best available post image based on requested size
*/
function franz_get_post_image( $size = 'thumbnail', $post_id = '' ){
/* Get the requested dimension */
$size = apply_filters( 'franz_post_image_size', $size, $post_id );
if ( ! is_array( $size ) ) {
$dimension = franz_get_image_sizes( $size );
if ( $size ) {
$width = $dimension['width'];
$height = $dimension['height'];
}
} else {
$width = $size[0];
$height = $size[1];
}
/* Get the post ID if not provided */
if ( ! $post_id ) $post_id = get_the_ID();
/* Get and return the cached result if available */
$cached_images = get_post_meta( $post_id, '_franz_post_images', true );
if ( $cached_images ) {
if ( array_key_exists( $width . 'x' . $height, $cached_images ) ) return $cached_images[$width . 'x' . $height];
} else {
$cached_images = array();
}
$images = array();
$image_ids = array();
/* Check if the post has a featured image */
if ( has_post_thumbnail( $post_id ) ) {
$image_id = get_post_thumbnail_id( $post_id );
$image = wp_get_attachment_image_src( $image_id, $size );
if ( $image ) {
$images[] = array(
'id' => $image_id,
'featured' => true,
'url' => $image[0],
'width' => $image[1],
'height' => $image[2],
'aspect_ratio' => $image[1] / $image[2]
);
$image_ids[] = $image_id;
}
}
/* Get other images uploaded to the post */
$media = get_attached_media( 'image', $post_id );
if ( $media ) {
foreach ( $media as $image ) {
$image_id = $image->ID;
$image = wp_get_attachment_image_src( $image_id, $size );
if ( $image ) {
$images[] = array(
'id' => $image_id,
'featured' => false,
'url' => $image[0],
'width' => $image[1],
'height' => $image[2],
'aspect_ratio' => $image[1] / $image[2]
);
$image_ids[] = $image_id;
}
}
}
/* Get the images from galleries in the post */
$galleries = get_post_galleries( $post_id, false );
if ( $galleries ) {
foreach ( $galleries as $gallery ) {
$gallery_images = explode( ',', $gallery['ids'] );
foreach ( $gallery_images as $image_id ) {
if ( in_array( $image_id, $image_ids ) ) continue;
$image = wp_get_attachment_image_src( $image_id, $size );
if ( $image ) {
$images[] = array(
'id' => $image_id,
'featured' => false,
'url' => $image[0],
'width' => $image[1],
'height' => $image[2],
'aspect_ratio' => $image[1] / $image[2]
);
$image_ids[] = $image_id;
}
}
}
}
/* Score the images for best match to the requested size */
$weight = array(
'dimension' => 1.5,
'aspect_ratio' => 1,
'featured_image'=> 1
);
$target_aspect = $width / $height;
foreach ( $images as $key => $image ) {
$score = 0.0;
/* Aspect ratio */
if ( $image['aspect_ratio'] > $target_aspect ) $score += ( $target_aspect / $image['aspect_ratio'] ) * $weight['aspect_ratio'];
else $score += ( $image['aspect_ratio'] / $target_aspect ) * $weight['aspect_ratio'];
/* Dimension: ( width ratio + height ratio ) / 2 */
$dim_score = min( array( ( $image['width'] / $width ), 1 ) ) + min( array( ( $image['height'] / $height ), 1 ) );
$score += ( $dim_score / 2 ) * $weight['dimension'];
/* Featured image */
if ( $image['featured'] ) $score += $weight['featured_image'];
$images[$key]['score'] = $score;
}
/* Sort the images based on the score */
usort( $images, 'franz_sort_array_key_score' );
$images = apply_filters( 'franz_get_post_image', $images, $size, $post_id );
if ( $images ) {
$cached_images = array_merge( $cached_images, array( $width . 'x' . $height => $images[0] ) );
update_post_meta( $post_id, '_franz_post_images', $cached_images );
return $images[0];
} else {
$cached_images = array_merge( $cached_images, array( $width . 'x' . $height => false ) );
update_post_meta( $post_id, '_franz_post_images', $cached_images );
return false;
}
}
/**
* Clear the post image cache when post is updated
*/
function franz_clear_post_image_cache( $post_id ){
if ( wp_is_post_revision( $post_id ) ) return;
delete_post_meta( $post_id, '_franz_post_images' );
}
add_action( 'save_post', 'franz_clear_post_image_cache' );
/**
* Display the post's image
*/
function franz_the_post_image( $size = 'post-thumbnail', $attr = '' ) {
$post_id = get_the_ID();
$image = franz_get_post_image( $size, $post_id );
if ( $image ) {
do_action( 'begin_fetch_post_thumbnail_html', $post_id, $image['id'], $size );
$html = wp_get_attachment_image( $image['id'], $size, false, $attr );
do_action( 'end_fetch_post_thumbnail_html', $post_id, $image['id'], $size );
} else {
$html = '';
}
echo apply_filters( 'post_thumbnail_html', $html, $post_id, $image['id'], $size, $attr );
}
if ( ! function_exists( 'franz_featured_image' ) ) :
/**
* Display the featured image in single post pages
*/
function franz_featured_image() {
global $franz_settings;
$has_featured_image = true;
if ( $franz_settings['hide_featured_image'] ) $has_featured_image = false;
if ( ! has_post_thumbnail() ) $has_featured_image = false;
else {
/* Check if featured image size is at least as wide as the content area width */
global $content_width;
$featured_image_id = get_post_thumbnail_id();
$featured_image = wp_get_attachment_metadata( $featured_image_id );
if ( $featured_image['width'] < $content_width ) $has_featured_image = false;
}
if ( $has_featured_image ) :
?>
post_excerpt ) {
?>
post_excerpt; ?>