/** * Returns the post image, either from Themify Custom Panel fields or from WordPress Featured Image. * @param string $args Format string. * @return string String with <img> tag and optional content prepended and/or appended */ function themify_get_image($args) { global $themify; /** * List of parameters * @var array */ $args = wp_parse_args($args, array('id' => '', 'src' => '', 'class' => '', 'ignore' => '', 'w' => '', 'h' => '', 'before' => '', 'after' => '', 'alt' => '', 'crop' => true, 'setting' => '', 'field_name' => 'post_image,image,wp_thumb,feature_image', 'urlonly' => false, 'image_size' => '', 'image_meta' => false, 'f_image' => false)); /** * Post ID for single, query or archive views. * Page ID is stored separately in $themify->page_id. * @var string */ $post_id = get_the_ID(); /** * URL of the image to use * @var string */ $img_url = ''; /** * Image width * @var string */ $width = ''; /** * Image height * @var string */ $height = ''; /** * Alt text of the attachment * @var string */ $img_alt = ''; // If ignore is set, just use width and height passed if (!empty($args['ignore']) && !themify_is_image_script_disabled() || isset($themify->is_shortcode) && $themify->is_shortcode) { if (!empty($args['w'])) { $width = $args['w']; } if (!empty($args['h'])) { $height = $args['h']; } } elseif (in_the_loop()) { // Main query area if (is_single()) { // Single Entry if (!($width = get_post_meta($post_id, 'image_width', true))) { if (!($width = themify_get('setting-default_' . get_post_type() . '_single_image_post_width'))) { $width = themify_get("setting-{$args['setting']}_width"); } } if (!($height = get_post_meta($post_id, 'image_height', true))) { if (!($height = themify_get('setting-default_' . get_post_type() . '_single_image_post_height'))) { $height = themify_get("setting-{$args['setting']}_height"); } } } elseif (themify_is_query_page()) { // Query pages like Query Posts or Query Portfolios $query_post_type = isset($themify->query_post_type) && 'post' != $themify->query_post_type ? $themify->query_post_type . '_' : ''; if (!($width = get_post_meta($themify->page_id, $query_post_type . 'image_width', true))) { if (!($width = themify_get('setting-default_' . get_post_type() . '_index_image_post_width'))) { $width = themify_get('setting-image_post_width'); } } if (!($height = get_post_meta($themify->page_id, $query_post_type . 'image_height', true))) { if (!($height = themify_get('setting-default_' . get_post_type() . '_index_image_post_height'))) { $height = themify_get('setting-image_post_height'); } } } elseif (is_archive() || is_tax() || is_search() || is_home()) { // Category, Tag, Author, Date || Custom Taxonomy || Search if (!($width = themify_get('setting-default_' . get_post_type() . '_index_image_post_width'))) { $width = themify_get('setting-image_post_width'); } if (!($height = themify_get('setting-default_' . get_post_type() . '_index_image_post_height'))) { $height = themify_get('setting-image_post_height'); } } // Catch height before width so we can check if user entered something for width. if (('' === $height || is_null($height)) && ('' === $width || is_null($width))) { $height = $themify->height; } if ('' === $width || is_null($width)) { $width = $themify->width; } } else { if (!($width = get_post_meta($post_id, 'image_width', true))) { if (!empty($args['h'])) { $height = $args['h']; } } if (!($height = get_post_meta($post_id, 'image_height', true))) { if (!empty($args['w'])) { $width = $args['w']; } } } if (themify_is_image_script_disabled()) { // Use WP standard image sizes if (!empty($args['image_size'])) { // If image_size parameter is set $feature_size = $args['image_size']; } elseif (isset($themify->image_size) && !empty($themify->image_size)) { // or if Themify::image_size is set $feature_size = $themify->image_size; } else { if (in_the_loop() && (!isset($themify->is_shortcode) || !$themify->is_shortcode)) { // Main query area if (is_single()) { $feature_size = get_post_meta($post_id, 'feature_size', true); if (empty($feature_size) || 'blank' == $feature_size) { $feature_size = themify_get('setting-image_post_single_feature_size'); } } elseif (themify_is_query_page()) { $feature_size = get_post_meta($themify->page_id, $query_post_type . 'feature_size_page', true); if (empty($feature_size) || 'blank' == $feature_size) { $feature_size = themify_get('setting-image_post_feature_size'); } } elseif (is_archive() || is_tax() || is_search() || is_home()) { $feature_size = themify_get('setting-image_post_feature_size'); } } } if (!isset($feature_size) || 'blank' == $feature_size) { $feature_size = themify_get('setting-global_feature_size'); if (empty($feature_size) || 'blank' == $feature_size) { $feature_size = apply_filters('themify_global_feature_size', 'large'); } } if (empty($args['src'])) { // Set URL to use for final output. $img_url = themify_image_url(false, $feature_size); } else { $img_url = $args['src']; } } else { // Use Image Script if (empty($args['src'])) { if (has_post_thumbnail()) { $img_url = themify_image_url(); } else { foreach (explode(',', $args['field_name']) as $field) { if ($img_url = get_post_meta($post_id, trim($field), true)) { break; } } } } else { $img_url = $args['src']; } if (0 === $height) { $args['crop'] = false; } // Set URL to use for final output. $temp = themify_do_img($img_url, $width, $height, (bool) $args['crop']); $img_url = $temp['url']; // Get alt text by attachment id if it was returned. if (isset($temp['attachment_id'])) { $img_alt = get_post_meta($temp['attachment_id'], '_wp_attachment_image_alt', true); } } // No image was defined, parse content to find the first image. if (empty($img_url) && ($args['f_image'] || themify_check('setting-auto_featured_image'))) { $content = get_the_content(); foreach (array('img', 'embed', 'iframe') as $tag) { $count = substr_count($content, '<' . $tag); if ($count >= 1) { $start = strpos($content, '<' . $tag, 0); $pos = substr($content, $start); $end = strpos($pos, '>'); $temp = themify_prep_image(substr($pos, 0, $end + 1)); $ext = explode('.', $temp['src']); $ext = strtolower($ext[count($ext) - 1]); if (strpos($temp['src'], '.') && ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'gif' || $ext == 'png')) { $auto_image_url = isset($temp['src']) ? $temp['src'] : ''; $args['class'] .= isset($temp['class']) ? ' ' . $temp['class'] : ''; $args['alt'] = $temp['alt']; if (themify_is_image_script_disabled()) { $upload_dir = wp_upload_dir(); $img_url = themify_image_url(false, $feature_size, themify_get_attachment_id_from_url($auto_image_url, $upload_dir['baseurl'])); if (empty($img_url)) { $img_url = esc_url($auto_image_url); } } elseif ($temp = themify_do_img($auto_image_url, $width, $height, (bool) $args['crop'])) { $img_url = $temp['url']; } break; } } } } if (!empty($img_url)) { if ($args['urlonly']) { $out = $img_url; } else { // Build final image $out = ''; if ($args['image_meta'] == true) { $out .= "<meta itemprop=\"image\" content=\"{$img_url}\"></meta>"; } $out .= "<img src=\"{$img_url}\""; if ($width) { $out .= " width=\"{$width}\""; } if ($height) { $out .= " height=\"{$height}\""; } if (!empty($args['class'])) { $out .= " class=\"{$args['class']}\""; } // If alt was passed as parameter, use it. Otherwise use alt text by attachment id if it was fetched or post title. if (!empty($args['alt'])) { $out_alt = $args['alt']; } elseif (!empty($img_alt)) { $out_alt = $img_alt; } else { $out_alt = the_title_attribute('echo=0'); } $out .= ' alt="' . esc_attr($out_alt) . '" />'; } $out = $args['before'] . $out . $args['after']; } else { $out = ''; } return $out; }
/** * Returns the post image, either from Themify Custom Panel fields or from WordPress Featured Image. * @param string $args Format string. * @return string String with <img> tag and optional content prepended and/or appended */ function themify_get_image($args) { global $post, $image_width, $image_height, $themify; $post_query_category = $themify->query_category; $data = themify_get_data(); parse_str($args, $options); foreach ($options as $key => $val) { ${$key} = $val; } if (!isset($setting)) { $setting = ''; } if (!isset($before)) { $before = ''; } if (!isset($after)) { $after = ''; } if (!isset($width)) { $width = ''; } if (!isset($height)) { $height = ''; } if (!isset($class)) { $class = ''; } if (!isset($id)) { $id = ''; } if (!isset($ignore)) { $ignore = ''; } if (!isset($alt) || '' == $alt) { $alt = isset($post) && is_object($post) ? get_the_title($post->ID) : ''; } if (isset($crop) && (false == $crop || 'false' == $crop)) { $crop = false; } else { $crop = true; } $check = false; //$ignore = (!$ignore || !isset($ignore) || $ignore == "") ? false : true; if (!isset($src) || '' == $src) { if (isset($field_name) && $field_name != '') { $fields = explode(",", $field_name); foreach ($fields as $field) { $field = trim($field); if ($field == 'wp_thumb' && function_exists('has_post_thumbnail')) { if (has_post_thumbnail() && !$check) { $temp = themify_prep_image(get_the_post_thumbnail($post->ID)); $src = isset($temp['src']) ? $temp['src'] : ''; $class .= isset($temp['class']) ? ' ' . $temp['class'] : ''; if (isset($temp['alt']) && '' == $alt && $temp['alt'] != '') { $alt = $temp['alt']; } $check = true; } } else { if (get_post_meta($post->ID, $field, true) != '' && get_post_meta($post->ID, $field, true) && !$check) { $src = get_post_meta($post->ID, $field, true); $check = true; } } } } else { $fields = array('Feature Image', 'feature_image', 'Post Image', 'post_image', 'image'); foreach ($fields as $field) { if (isset($post) && is_object($post) && get_post_meta($post->ID, $field, true) != '' && !$check) { $src = get_post_meta($post->ID, $field, true); $check = true; } } if (function_exists('has_post_thumbnail')) { if (has_post_thumbnail() && !$check) { $temp = themify_prep_image(get_the_post_thumbnail()); $src = isset($temp['src']) ? $temp['src'] : ''; $class .= isset($temp['class']) ? ' ' . $temp['class'] : ''; if (isset($temp['alt']) && '' == $alt && $temp['alt'] != '') { $alt = $temp['alt']; } $check = true; } } if (!$check) { $tags = array('img', 'embed', 'iframe'); $content = get_the_content(); foreach ($tags as $tag) { $count = substr_count($content, '<' . $tag); if ($count >= 1 && !$check) { $start = strpos($content, '<' . $tag, 0); $pos = substr($content, $start); $end = strpos($pos, '>'); $temp = themify_prep_image(substr($pos, 0, $end + 1)); $ext = explode('.', $temp['src']); $ext = strtolower($ext[count($ext) - 1]); if (strpos($temp['src'], '.') && ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'gif' || $ext == 'png')) { $check = true; $src = isset($temp['src']) ? $temp['src'] : ''; $class .= isset($temp['class']) ? $temp['class'] : ''; if (isset($temp['alt']) && '' == $alt && $temp['alt'] != '') { $alt = $temp['alt']; } } } } } } } // Get width/height from custom fields in single post $cfwidth = isset($post) && is_object($post) ? get_post_meta($post->ID, 'image_width', true) : ''; $cfheight = isset($post) && is_object($post) ? get_post_meta($post->ID, 'image_height', true) : ''; // Set width if (in_the_loop() && (!isset($ignore) || !$ignore)) { if (is_home() || is_archive() || is_search()) { if (isset($data["setting-{$setting}_width"]) && $data["setting-{$setting}_width"] != '' && $setting != '') { // Use value defined in default index or single post layout $width = $data["setting-" . $setting . "_width"]; } elseif (isset($image_width) && $image_width != '' && !$ignore) { $width = $image_width; } elseif ($cfwidth) { $width = $cfwidth; } elseif (isset($w) && $w != '') { $width = $w; } } if ($post_query_category != '') { // Use value defined in query category page if (isset($themify->post_image_width) && '' != $themify->post_image_width) { $width = $themify->post_image_width; } else { $width = $w; } } if (is_single()) { // Use value defined in single post custom panel if ($cfwidth) { $width = $cfwidth; } elseif (isset($data["setting-{$setting}_width"]) && $data["setting-{$setting}_width"] != '' && $setting != '') { $width = $data["setting-{$setting}_width"]; } elseif (isset($w) && $w != '') { $width = $w; } } } elseif (isset($image_width) && $image_width != '' && !$ignore) { $width = $image_width; } elseif (!in_the_loop() && isset($data["setting-{$setting}_width"]) && $data["setting-{$setting}_width"] != '' && $setting != '') { $width = $data["setting-" . $setting . "_width"]; } else { if (isset($w) && $w != '') { $width = $w; } } //Set height if (in_the_loop() && (!isset($ignore) || !$ignore)) { if (is_home() || is_archive() || is_search()) { if (isset($data["setting-{$setting}_height"]) && $data["setting-{$setting}_height"] != '' && $setting != '') { // Use value defined in default index or single post layout $height = $data["setting-" . $setting . "_height"]; } elseif (isset($image_height) && $image_height != '' && !$ignore) { $height = $image_height; //echo 'height in image_height != "" && isset(image_height) !ignore ' . $height; } else { if (isset($data['setting-img_settings_vertical_crop_option']) && 'no' == $data['setting-img_settings_vertical_crop_option'] || 0 == $cfheight) { $height = ''; } elseif ($cfheight) { $height = $cfheight; } elseif (isset($themify->height)) { $height = $themify->height; } else { if (isset($h) && $h != '') { $height = $h; } } //echo 'height in home archive search ' . $height; } } if ($post_query_category != '') { // Use value defined in query category page if (isset($themify->post_image_height) && '' != $themify->post_image_height) { $height = $themify->post_image_height; } else { $height = $h; } //echo 'height in qc page ' . $height; } if (is_single()) { // Use value defined in single post custom panel if ($cfheight) { $height = $cfheight; } elseif (isset($data["setting-{$setting}_height"]) && $data["setting-{$setting}_height"] != '' && $setting != '') { $height = $data["setting-{$setting}_height"]; } else { if (isset($data['setting-img_settings_vertical_crop_option']) && 'no' == $data['setting-img_settings_vertical_crop_option'] || 0 == $cfheight) { $height = ''; } if (isset($themify->height)) { $height = $themify->height; } else { if (isset($h) && $h != '') { $height = $h; } } } } } elseif (isset($image_height) && $image_height != '' && !$ignore) { $height = $image_height; } elseif (!in_the_loop() && isset($data["setting-{$setting}_height"]) && $data["setting-{$setting}_height"] != '' && $setting != '') { $height = $data["setting-{$setting}_height"]; } else { if (isset($data['setting-img_settings_vertical_crop_option']) && 'no' == $data['setting-img_settings_vertical_crop_option']) { $height = ''; } else { if (isset($h) && $h != '') { $height = $h; } } } // If height is 0, the variable is removed if (0 == $height) { unset($height); } // Set custom classes if specified if (isset($class) && $class != '') { $class = "class='" . $class . "'"; } if (isset($id) && $id != '') { $id = "id='" . $id . "'"; } $p_height = isset($height) && $height != '' ? "height='" . $height . "'" : ""; $p_width = isset($width) && $width != '' ? "width='" . $width . "'" : ""; /** * User size selection: one of WP Featured Image sizes, image script or blank * @var string $feature_size * @since 1.1.5 */ global $feature_size_page; $base_feature_size = apply_filters('themify_global_feature_size', 'large'); //check if global size is set if (themify_check('setting-img_settings_use') && '' != themify_get('setting-global_feature_size')) { //set feature size $feature_size = themify_get('setting-global_feature_size'); } elseif ('' == themify_get('setting-global_feature_size')) { $feature_size = $base_feature_size; } //echo '<br/>global/default feature ',$feature_size; if (is_front_page()) { if ('' != themify_get('setting-image_post_feature_size') && 'blank' != themify_get('setting-image_post_feature_size')) { //override size with default index layout size $feature_size = themify_get('setting-image_post_feature_size'); //echo '<br/>is home, using default index layout ',$feature_size; } else { //nothing in default index layout size //$feature_size = 'medium'; //echo '<br/>is home, nothing in default index layout, using ',$feature_size; } } if (is_singular() && in_the_loop()) { //echo '<p>is singular</p>'; //check if post size is set if ('' != themify_get('feature_size')) { //override global size with post's custom field size $feature_size = themify_get('feature_size'); //echo '<br/>singular, size selected in post drop down ',$feature_size; } elseif ('' != themify_get('setting-image_post_single_feature_size') && 'blank' != themify_get('setting-image_post_single_feature_size')) { //override global size with post's custom field size $feature_size = themify_get('setting-image_post_single_feature_size'); //echo '<br/>singular, nothing selected in post drop down, using single post layout ',$feature_size; } } //are we on a page? is query category size set? if ('' != $post_query_category) { //echo '<p>is query category</p>'; if ('') { $feature_size = ''; } if ('' != themify_get('setting-image_post_feature_size') && 'blank' != themify_get('setting-image_post_feature_size')) { //override custom field size with default index layout size $feature_size = themify_get('setting-image_post_feature_size'); //echo '<br/>default index layout ',$feature_size; } if ('' != $feature_size_page && 'blank' != $feature_size_page && isset($feature_size_page)) { //override custom field size with query category size $feature_size = $feature_size_page; } //echo '<br/>query category page ',$feature_size_page; } //are we on an archive page if (is_archive() && (!isset($post_query_category) || '' == $post_query_category)) { //echo '<p>is archive</p>'; if (themify_check('setting-img_settings_use')) { //set feature size if ('' != themify_get('setting-global_feature_size')) { $feature_size = themify_get('setting-global_feature_size'); } elseif ('' == $feature_size) { $feature_size = 'medium'; } if ('' != themify_get('setting-image_post_feature_size') && 'blank' != themify_get('setting-image_post_feature_size')) { $feature_size = themify_get('setting-image_post_feature_size'); } } //echo '<br/>archive ',$feature_size; } /** * For Themify custom post types slider, menu and highlights */ $thecpt = isset($post) && is_object($post) ? get_post_type($post->ID) : ''; $iscpt = in_array($thecpt, themify_specific_post_types()); if ($iscpt) { global $feature_size_cpt; //echo '<br/>is a Themify custom post type'; if (!isset($feature_size_cpt)) { if ('' != themify_get('setting-global_feature_size')) { //echo '<br/>using global featured image size'; $feature_size = themify_get('setting-global_feature_size'); } elseif ('' == themify_get('setting-global_feature_size')) { //echo '<br/>global featured image size, use medium'; $feature_size = $base_feature_size; } if ('' != themify_get('feature_size')) { //override global size with post's custom field size $feature_size = themify_get('feature_size'); //echo '<br/>custom post type, size selected in cpt drop down ',$feature_size; } } else { //echo '<br/>is custom post type query category'; //for pages like Rezo, that have an additional drop down menu $feature_size = $feature_size_cpt; //echo '<br/>custom post type, size selected in cpt Query drop down ',$feature_size; } } /* end themify custom post types */ //if the call has been made outside the loop if (!in_the_loop()) { $feature_size = 'thumbnail'; } //if an image_size parameter was passed, for example if (isset($image_size)) { $feature_size = $image_size; } //echo '<br/>Final size to be used is ' , $feature_size; //we have a path so let's proceed to show our image if (isset($src) && $src != '' && $src) { //is image script enabled in theme settings and user did not selected a Featured Image size? if (!isset($data['setting-img_settings_use']) || '' == $data['setting-img_settings_use'] || !$data['setting-img_settings_use']) { // Filter $src so it can be manipulated $src = apply_filters('themify_image_script_src', $src); if (!isset($height)) { $height = ''; } // If no large size is available, we simply use original one if (function_exists('themify_do_img')) { $timg = themify_do_img(null, $src, $width, $height, $crop); } else { $timg = array('url' => $src); } return stripslashes($before) . "<img src='{$timg['url']}' alt='{$alt}' {$class} {$id} {$p_width} {$p_height} />" . stripslashes($after); } elseif (!in_the_loop()) { return stripslashes($before) . "<img src='{$src}' alt='{$alt}' {$class} {$id} {$p_width} {$p_height} />" . stripslashes($after); } else { //image script is disabled in theme settings or user selected a Featured Image size //check if user entered certain dimensions $feature_width = themify_get('image_width'); $feature_height = themify_get('image_height'); //user did not selected a size if ('' == $feature_size || !has_post_thumbnail()) { $id = isset($id) ? $id : ''; $class = isset($class) ? $class : ''; $before = isset($before) ? $before : ''; $after = isset($after) ? $after : ''; //output original image, useful for backwards compatibility return stripslashes($before) . "<img src='" . $src . "' alt='" . $alt . "' {$id} {$class} {$p_width} {$p_height} />" . stripslashes($after); } else { //user selected a size, so let's assume he set up an image as featured image /** * Store post thumbnail properties * [0] => url [1] => width [2] => height * @var array $thumb */ $thumb = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), $feature_size); //echo '<p>WP Feature image: '. $feature_size .'</p>'; /** * Start by clearing dimensions to avoid the math formula * * For a singular post/page * if dimensions are set in custom panel, use them * else if dimensions are set in setting single post, use them * else do nothing * * For a query category page * if dimensions are set in custom panel, use them * else do nothing * * For an archive page * if dimensions are set in setting index layout, use them * else do nothing * * For a custom post type * if dimensions are set in custom panel, use them * else do nothing * */ // Clear dimensions to start if (!isset($ignore) || !$ignore) { $p_width = 'width="' . $themify->width . '"'; $p_height = 'height="' . $themify->height . '"'; } // For singular views if (is_singular() && (!isset($ignore) || !$ignore)) { if (isset($feature_width)) { $p_width = 'width="' . $feature_width . '"'; } elseif (isset($data["setting-{$setting}_width"]) && '' != $data["setting-{$setting}_width"] && '' != $setting) { $p_width = 'width="' . $data["setting-{$setting}_width"] . '"'; } if (isset($feature_height)) { $p_height = 'height="' . $feature_height . '"'; } elseif (isset($data["setting-{$setting}_height"]) && '' != $data["setting-{$setting}_height"] && '' != $setting) { $p_height = 'height="' . $data["setting-{$setting}_height"] . '"'; } } // For query pages if ((!isset($ignore) || !$ignore) && isset($post_query_category)) { global $feature_size_query_page; $queryw = get_post_meta($feature_size_query_page->ID, 'image_width', true); $queryh = get_post_meta($feature_size_query_page->ID, 'image_height', true); if ('' != $queryw) { $p_width = 'width="' . $queryw . '"'; } if ('' != $queryh) { $p_height = 'height="' . $queryh . '"'; } } // For archive pages and home archive if ((!isset($ignore) || !$ignore) && (is_home() || is_archive())) { if (isset($data["setting-{$setting}_width"]) && '' != $data["setting-{$setting}_width"] && '' != $setting) { $p_width = 'width="' . $data["setting-{$setting}_width"] . '"'; } if (isset($data["setting-{$setting}_height"]) && '' != $data["setting-{$setting}_height"] && '' != $setting) { $p_height = 'height="' . $data["setting-{$setting}_height"] . '"'; } } // For custom post types if ((!isset($ignore) || !$ignore) && $iscpt) { // is it a custom post type query page? if (isset($feature_size_cpt)) { global $feature_size_query_page; $cptw = get_post_meta($feature_size_query_page->ID, $thecpt . '_image_width', true); $cpth = get_post_meta($feature_size_query_page->ID, $thecpt . '_image_height', true); } else { $cptw = get_post_meta($post->ID, 'image_width', true); $cpth = get_post_meta($post->ID, 'image_height', true); } if ('' != $cptw) { $p_width = 'width="' . $cptw . '"'; } if ('' != $cpth) { $p_height = 'height="' . $cpth . '"'; } } if ('height="0"' == $p_height) { $p_height = ''; } $id = isset($id) ? $id : ''; $class = isset($class) ? $class : ''; $before = isset($before) ? $before : ''; $after = isset($after) ? $after : ''; return stripslashes($before) . "<img src='{$thumb['0']}' alt='{$alt}' {$id} {$class} {$p_width} {$p_height} />" . stripslashes($after); } } } }