Exemplo n.º 1
0
/**
 * View function.
 * 
 * @param type $params 
 */
function wpcf_fields_image_view($params)
{
    $output = '';
    $alt = false;
    $title = false;
    $class = array();
    // Get image data
    $image_data = wpcf_fields_image_get_data($params['field_value']);
    // Display error to admin only
    if (!empty($image_data['error'])) {
        if (current_user_can('administrator')) {
            return '<div style="padding:10px;background-color:Red;color:#FFFFFF;">' . 'Types: ' . $image_data['error'] . '</div>';
        }
        return $params['field_value'];
    }
    if (!$image_data['is_outsider'] && !$image_data['is_dir_writable']) {
        if (current_user_can('administrator')) {
            return '<div style="padding:10px;background-color:Red;color:#FFFFFF;">' . 'Types: ' . sprintf(__('Directory %s not writable', 'wpcf'), $image_data['abspath']) . '</div>';
        }
        return $params['field_value'];
    }
    // Set alt
    if (isset($params['alt'])) {
        $alt = $params['alt'];
    }
    // Set title
    if (isset($params['title'])) {
        $title = $params['title'];
    }
    // Set attachment class
    if (!empty($params['size'])) {
        $class[] = 'attachment-' . $params['size'];
    }
    // Set align class
    if (!empty($params['align']) && $params['align'] != 'none') {
        $class[] = 'align' . $params['align'];
    }
    // Pre-configured size (use WP function)
    if ($image_data['is_attachment'] && !empty($params['size'])) {
        $output = wp_get_attachment_image($image_data['is_attachment'], $params['size'], false, array('class' => implode(' ', $class), 'alt' => $alt, 'title' => $title));
        $output = wpcf_frontend_wrap_field_value($params['field'], $output, $params);
        $output = wpcf_frontend_wrap_field($params['field'], $output, $params);
    } else {
        // Custom size
        $width = !empty($params['width']) ? intval($params['width']) : null;
        $height = !empty($params['height']) ? intval($params['height']) : null;
        $crop = !empty($params['proportional']) && $params['proportional'] == 'true' ? false : true;
        // Check if image is outsider
        if (!$image_data['is_outsider']) {
            $resized_image = wpcf_fields_image_resize_image($params['field_value'], $width, $height, 'relpath', false, $crop);
            if (!$resized_image) {
                $resized_image = $params['field_value'];
            }
        } else {
            $resized_image = $params['field_value'];
        }
        $output = '<img alt="';
        $output .= $alt !== false ? $alt : $resized_image;
        $output .= '" title="';
        $output .= $title !== false ? $title : $resized_image;
        $output .= '"';
        $output .= !empty($params['onload']) ? ' onload="' . $params['onload'] . '"' : '';
        $output .= !empty($class) ? ' class="' . implode(' ', $class) . '"' : '';
        $output .= ' src="' . $resized_image . '" />';
        $output = wpcf_frontend_wrap_field_value($params['field'], $output, $params);
        $output = wpcf_frontend_wrap_field($params['field'], $output, $params);
    }
    return $output;
}
Exemplo n.º 2
0
/**
 * View function.
 * 
 * @param type $params 
 */
function wpcf_fields_image_view($params)
{
    $output = '';
    $alt = false;
    $title = false;
    $class = array();
    // Get image data
    $image_data = wpcf_fields_image_get_data($params['field_value']);
    // Display error to admin only
    if (!empty($image_data['error'])) {
        if (current_user_can('administrator')) {
            return '<div style="padding:10px;background-color:Red;color:#FFFFFF;">' . 'Types: ' . $image_data['error'] . '</div>';
        }
        return $params['field_value'];
    }
    //if (!$image_data['is_outsider']) {
    //    if (current_user_can('administrator')) {
    //        return '<div style="padding:10px;background-color:Red;color:#FFFFFF;">'
    //                . 'Types: ' . sprintf(__('Directory %s not writable', 'wpcf'),
    //                        $image_data['abspath']) . '</div>';
    //    }
    //    return $params['field_value'];
    //}
    // Set alt
    if (isset($params['alt'])) {
        $alt = $params['alt'];
    }
    // Set title
    if (isset($params['title'])) {
        $title = $params['title'];
    }
    // Set attachment class
    if (!empty($params['size'])) {
        $class[] = 'attachment-' . $params['size'];
    }
    // Set align class
    if (!empty($params['align']) && $params['align'] != 'none') {
        $class[] = 'align' . $params['align'];
    }
    // Pre-configured size (use WP function)
    if ($image_data['is_attachment'] && !empty($params['size'])) {
        $output = wp_get_attachment_image($image_data['is_attachment'], $params['size'], false, array('class' => implode(' ', $class), 'alt' => $alt, 'title' => $title));
    } else {
        // Custom size
        $width = !empty($params['width']) ? intval($params['width']) : null;
        $height = !empty($params['height']) ? intval($params['height']) : null;
        $crop = !empty($params['proportional']) && $params['proportional'] == 'true' ? false : true;
        // Check if image is outsider
        if (!$image_data['is_outsider']) {
            $resized_image = wpcf_fields_image_resize_image($params['field_value'], $width, $height, 'relpath', false, $crop);
            if (!$resized_image) {
                $resized_image = $params['field_value'];
            } else {
                // Add to library
                $image_abspath = wpcf_fields_image_resize_image($params['field_value'], $width, $height, 'abspath', false, $crop);
                $add_to_library = wpcf_get_settings('add_resized_images_to_library');
                if ($add_to_library) {
                    global $wpdb;
                    $attachment_exists = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts}\r\r\n    WHERE post_type = 'attachment' AND guid=%s", $resized_image));
                    if (empty($attachment_exists)) {
                        // Add as attachment
                        $wp_filetype = wp_check_filetype(basename($image_abspath), null);
                        $attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($image_abspath)), 'post_content' => '', 'post_status' => 'inherit', 'guid' => $resized_image);
                        global $post;
                        $attach_id = wp_insert_attachment($attachment, $image_abspath, $post->ID);
                        // you must first include the image.php file
                        // for the function wp_generate_attachment_metadata() to work
                        require_once ABSPATH . "wp-admin" . '/includes/image.php';
                        $attach_data = wp_generate_attachment_metadata($attach_id, $image_abspath);
                        wp_update_attachment_metadata($attach_id, $attach_data);
                    }
                }
            }
        } else {
            $resized_image = $params['field_value'];
        }
        $output = '<img alt="';
        $output .= $alt !== false ? $alt : $resized_image;
        $output .= '" title="';
        $output .= $title !== false ? $title : $resized_image;
        $output .= '"';
        $output .= !empty($params['onload']) ? ' onload="' . $params['onload'] . '"' : '';
        $output .= !empty($class) ? ' class="' . implode(' ', $class) . '"' : '';
        $output .= ' src="' . $resized_image . '" />';
    }
    return $output;
}
Exemplo n.º 3
0
/**
 * View function.
 * 
 * @param type $params 
 */
function wpcf_fields_image_view($params)
{
    $output = '';
    $alt = false;
    $title = false;
    $class = array();
    $style = array();
    // Get image data
    $image_data = wpcf_fields_image_get_data($params['field_value']);
    //print_r($image_data);
    //print_r($params);
    // Display error to admin only
    if (!empty($image_data['error'])) {
        if (current_user_can('administrator')) {
            return '<div style="padding:10px;background-color:Red;color:#FFFFFF;">' . 'Types: ' . $image_data['error'] . '</div>';
        }
        return $params['field_value'];
    }
    // Set alt
    if (isset($params['alt'])) {
        $alt = $params['alt'];
    }
    // Set title
    if (isset($params['title'])) {
        $title = $params['title'];
    }
    // Set attachment class
    if (!empty($params['size'])) {
        $class[] = 'attachment-' . $params['size'];
    }
    // Set align class
    if (!empty($params['align']) && $params['align'] != 'none') {
        $class[] = 'align' . $params['align'];
    }
    if (!empty($params['class'])) {
        $class[] = $params['class'];
    }
    if (!empty($params['style'])) {
        $style[] = $params['style'];
    }
    // Pre-configured size (use WP function)
    if ($image_data['is_attachment'] && !empty($params['size'])) {
        //print_r('is_attachment');
        if (isset($params['url']) && $params['url'] == 'true') {
            //print_r('is_url');
            $image_url = wp_get_attachment_image_src($image_data['is_attachment'], $params['size']);
            if (!empty($image_url[0])) {
                $output = $image_url[0];
            } else {
                $output = $params['field_value'];
            }
        } else {
            //print_r('is_not_url');
            $output = wp_get_attachment_image($image_data['is_attachment'], $params['size'], false, array('class' => implode(' ', $class), 'style' => implode(' ', $style), 'alt' => $alt, 'title' => $title));
        }
    } else {
        // Custom size
        //print_r('custom_size');
        $width = !empty($params['width']) ? intval($params['width']) : null;
        $height = !empty($params['height']) ? intval($params['height']) : null;
        $crop = !empty($params['proportional']) && $params['proportional'] == 'true' ? false : true;
        //////////////////////////
        // If width and height are not set then check the size parameter.
        // This handles the case when the image is not an attachment.
        if (!$width && !$height && !empty($params['size'])) {
            //print_r('no_width_no_height_and_size');
            switch ($params['size']) {
                case 'thumbnail':
                    $width = get_option('thumbnail_size_w');
                    $height = get_option('thumbnail_size_h');
                    if (empty($params['proportional'])) {
                        $crop = get_option('thumbnail_crop');
                    }
                    break;
                case 'medium':
                    $width = get_option('medium_size_w');
                    $height = get_option('medium_size_h');
                    break;
                case 'large':
                    $width = get_option('large_size_w');
                    $height = get_option('large_size_h');
                    break;
            }
        }
        // Check if image is outsider
        if (!$image_data['is_outsider']) {
            //print_r('Not is_outsider');
            $resized_image = wpcf_fields_image_resize_image($params['field_value'], $width, $height, 'relpath', false, $crop);
            if (!$resized_image) {
                //print_r('Not resized image');
                $resized_image = $params['field_value'];
            } else {
                //print_r('resized image add to lib');
                // Add to library
                $image_abspath = wpcf_fields_image_resize_image($params['field_value'], $width, $height, 'abspath', false, $crop);
                $add_to_library = wpcf_get_settings('add_resized_images_to_library');
                if ($add_to_library) {
                    //print_r('add to lib');
                    global $wpdb;
                    $attachment_exists = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts}\r\n    WHERE post_type = 'attachment' AND guid=%s", $resized_image));
                    if (empty($attachment_exists)) {
                        // Add as attachment
                        $wp_filetype = wp_check_filetype(basename($image_abspath), null);
                        $attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($image_abspath)), 'post_content' => '', 'post_status' => 'inherit', 'guid' => $resized_image);
                        global $post;
                        $attach_id = wp_insert_attachment($attachment, $image_abspath, $post->ID);
                        // you must first include the image.php file
                        // for the function wp_generate_attachment_metadata() to work
                        require_once ABSPATH . "wp-admin" . '/includes/image.php';
                        $attach_data = wp_generate_attachment_metadata($attach_id, $image_abspath);
                        wp_update_attachment_metadata($attach_id, $attach_data);
                    }
                }
            }
        } else {
            //print_r('is_outsider');
            $resized_image = $params['field_value'];
        }
        if (isset($params['url']) && $params['url'] == 'true') {
            //print_r('return');
            return $resized_image;
        }
        //print_r('output');
        $output = '<img alt="';
        $output .= $alt !== false ? $alt : $resized_image;
        $output .= '" title="';
        $output .= $title !== false ? $title : $resized_image;
        $output .= '"';
        $output .= !empty($params['onload']) ? ' onload="' . $params['onload'] . '"' : '';
        $output .= !empty($class) ? ' class="' . implode(' ', $class) . '"' : '';
        $output .= !empty($style) ? ' style="' . implode(' ', $style) . '"' : '';
        $output .= ' src="' . $resized_image . '" />';
    }
    return $output;
}