Beispiel #1
0
 /**
  * Multi field: Image dimensions fields to enter width and height.
  * @param array $args
  * @param string $prefix
  * @return mixed|void
  * @since 1.5.2
  */
 function themify_image_dimensions_field($args = array(), $prefix = 'image')
 {
     if (themify_is_image_script_disabled()) {
         $height_field_before = '<span class="show_if_enabled_img_php">';
         $height_field_after = '</span>';
     } else {
         $height_field_before = '';
         $height_field_after = '';
     }
     $defaults = array('type' => 'multi', 'name' => $prefix . '_dimensions', 'title' => __('Image Dimension', 'themify'), 'meta' => array('fields' => array(array('name' => $prefix . '_width', 'label' => __('width', 'themify'), 'description' => '', 'type' => 'textbox', 'meta' => array('size' => 'small'), 'before' => '', 'after' => ''), array('name' => $prefix . '_height', 'label' => __('height', 'themify'), 'type' => 'textbox', 'meta' => array('size' => 'small'), 'before' => $height_field_before, 'after' => $height_field_after)), 'description' => __('Enter height = 0 to disable vertical cropping with image script enabled', 'themify'), 'before' => '', 'after' => '', 'separator' => ''));
     $field = wp_parse_args($args, $defaults);
     return apply_filters('themify_image_dimensions_field', $field);
 }
/**
 * 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' => ''));
    /**
     * 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 = '';
    // 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');
            }
        }
        if ('' == $width) {
            $width = $themify->width;
        }
        if ('' == $height && 0 != $height) {
            $height = $themify->height;
        }
    } 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'];
    }
    // No image was defined, parse content to find the first image.
    if (empty($img_url) && 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 = "<img src=\"{$img_url}\"";
            if ($width) {
                $out .= " width=\"{$width}\"";
            }
            if ($height) {
                $out .= " height=\"{$height}\"";
            }
            if (!empty($args['class'])) {
                $out .= " class=\"{$args['class']}\"";
            }
            $out .= ' alt="';
            $out .= !empty($args['alt']) ? $args['alt'] : the_title_attribute('echo=0');
            $out .= '" />';
        }
        $out = $args['before'] . $out . $args['after'];
    } else {
        $out = '';
    }
    return $out;
}