PluginProbe
WP Ultimate Post Grid / 2.6.0
WP Ultimate Post Grid v2.6.0
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / addons / custom-templates / templates / post / image.php

image.php in WP Ultimate Post Grid 2.6.0, at addons/custom-templates/templates/post/image.php

179 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class WPUPG_Template_Post_Image extends WPUPG_Template_Block {
4
5 public $editorField = 'postImage';
6 public $thumbnail;
7 public $crop = false;
8
9 public function __construct( $type = 'post-image' )
10 {
11 parent::__construct( $type );
12 }
13
14 public function thumbnail( $thumbnail )
15 {
16 $this->thumbnail = $thumbnail;
17 return $this;
18 }
19
20 public function crop( $crop )
21 {
22 $this->crop = $crop;
23 return $this;
24 }
25
26 public function output( $post, $args = array() )
27 {
28 if( !$this->output_block( $post, $args ) ) return '';
29 if( !isset( $this->thumbnail ) ) $this->thumbnail = 'full';
30
31 // Post Image ID
32 $custom_image_id = get_post_meta( $post->ID, 'wpupg_custom_image_id', true );
33 if( $custom_image_id && get_post_type( $custom_image_id ) == 'attachment' ) {
34 $post_image_id = $custom_image_id;
35 } else {
36 $post_image_id = $post->post_type == 'attachment' ? $post->ID : get_post_thumbnail_id( $post->ID );
37 }
38
39 $thumb = $this->get_thumb( $post, $post_image_id, $this->thumbnail );
40 if(!$thumb) return ''; // No recipe image found
41
42 $image_url = $thumb[0];
43 if( is_null( $image_url ) ) return '';
44 $img_size_attributes = '';
45
46 // Check image size unless a specific thumbnail was specified
47 if( is_array( $this->thumbnail ) ) {
48 $new_width = $this->thumbnail[0];
49 $new_height = $this->thumbnail[1];
50
51 if( $thumb[1] && $thumb[2] ) { // Use image size if passed along
52 $width = $thumb[1];
53 $height = $thumb[2];
54 } else { // Or look it up for ourselves otherwise
55 $size = getimagesize( $image_url );
56 $width = $size[0];
57 $height = $size[1];
58 }
59
60 // Don't distort the image
61 $undistored_height = floor( $new_width * ( $height / $width ) );
62 $this->add_style( 'height', $undistored_height.'px' );
63
64 // Get correct thumbnail size
65 $correct_thumb = array(
66 $new_width,
67 $undistored_height
68 );
69
70 $img_size_attributes = ' width="' . $new_width . '" height="' . $undistored_height . '"';
71
72 $thumb = $this->get_thumb( $post, $post_image_id, $correct_thumb );
73 $image_url = $thumb[0];
74
75 // Cropping the image
76 if( $this->crop ) {
77 $this->add_style( 'overflow', 'hidden', 'outer' );
78 $this->add_style( 'max-width', $new_width.'px', 'outer' );
79 $this->add_style( 'max-height', $new_height.'px', 'outer' );
80
81 if( $new_height < $undistored_height ) {
82 $margin = -1 * ( 1 - $new_height / $undistored_height ) * 100/2;
83 $this->add_style( 'margin-top', $margin.'%' );
84 $this->add_style( 'margin-bottom', $margin.'%' );
85
86 $this->add_style( 'width', '100%' );
87 $this->add_style( 'height', 'auto' );
88 } elseif ( $new_height > $undistored_height ) {
89 // We need a larger image
90 $larger_width = $new_height * ($new_width / $undistored_height);
91 $larger_thumb = array(
92 $larger_width,
93 $new_height
94 );
95
96 $thumb = $this->get_thumb( $post, $post_image_id, $larger_thumb );
97 $image_url = $thumb[0];
98
99 $margin = ( $new_width - $larger_width ) / 2;
100 $this->add_style( 'margin-left', $margin.'px' );
101 $this->add_style( 'margin-right', $margin.'px' );
102 $this->add_style( 'width', $larger_width.'px' );
103 $this->add_style( 'max-width', $larger_width.'px' );
104 $this->add_style( 'height', $new_height.'px' );
105 }
106
107 $img_size_attributes = ' width="' . $new_width . '" height="' . $new_height . '"';
108 }
109 } else if( $this->thumbnail == 'full' ) {
110 // Get better thumbnail size based on max possible block size
111 $height = $args['max_height'] == 9999 ? 0 : $args['max_height'];
112 $correct_thumb = array(
113 $args['max_width'],
114 $height,
115 );
116
117 $thumb = $this->get_thumb( $post, $post_image_id, $correct_thumb );
118
119 if( isset( $thumb[1] ) && $thumb[1] >= $args['max_width'] ) {
120 $image_url = $thumb[0];
121 } else {
122 $correct_thumb = array(
123 $args['max_width'],
124 $args['max_height'],
125 );
126
127 $thumb = $this->get_thumb( $post, $post_image_id, $correct_thumb );
128
129 if( isset( $thumb[1] ) && $thumb[1] >= $args['max_width'] ) {
130 $image_url = $thumb[0];
131 }
132 }
133 }
134
135 $args['desktop'] = $args['desktop'] && $this->show_on_desktop;
136
137 $output = $this->before_output();
138
139 ob_start();
140 ?>
141 <div<?php echo $this->style( 'outer' ); ?>>
142 <img src="<?php echo $image_url; ?>" alt="<?php echo esc_attr( get_post_meta( $post_image_id, '_wp_attachment_image_alt', true) ); ?>"<?php echo $img_size_attributes . $this->style(); ?> />
143 </div>
144 <?php
145 $output .= ob_get_contents();
146 ob_end_clean();
147
148 return $this->after_output( $output, $post );
149 }
150
151 private function get_thumb( $post, $image_id, $size )
152 {
153 if( isset( $post->term ) && $post->term ) {
154 $custom_image = get_term_meta( $post->ID, 'wpupg_custom_image', true );
155
156 if( $custom_image ) {
157 $thumb = wp_get_attachment_image_src( intval( $custom_image ), $size );
158 } else {
159 $thumb = false;
160 }
161 } else {
162 $custom_image_url = get_post_meta( $post->ID, 'wpupg_custom_image', true );
163 $custom_image_size = $custom_image_url ? @getimagesize( $custom_image_url ) : array();
164
165 if( isset( $custom_image_size[0] ) && is_int( $custom_image_size[0] ) && isset( $custom_image_size[1] ) && is_int( $custom_image_size[1] ) ) {
166 $thumb = array(
167 $custom_image_url,
168 $custom_image_size[0],
169 $custom_image_size[1],
170 false,
171 );
172 } else {
173 $thumb = wp_get_attachment_image_src( $image_id, $size );
174 }
175 }
176
177 return $thumb;
178 }
179 }