Completed
Push — develop ( 4659b8...3ce645 )
by Zack
16:21
created

includes/class-gravityview-image.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Generic class for generating image tag
4
 */
5
class GravityView_Image {
6
7
	var $alt;
8
	var $src;
9
	var $title;
10
	var $width;
11
	var $height;
12
13
	/**
14
	 * HTML class attribute.
15
	 * @var string
16
	 */
17
	var $class = NULL;
18
19
	/**
20
	 * HTML style attribute
21
	 * @var string
22
	 */
23
	var $style = NULL;
24
25
	/**
26
 	 * String representing size of image - Choose from "full", "medium", "thumb", "tiny"
27
	 * @var string
28
	 */
29
	var $size = NULL;
30
31
	/**
32
	 * Use PHP getimagesize function to auto-calculate image size?
33
	 * @var boolean
34
	 */
35
	var $getimagesize = false;
36
37
	/**
38
	 * Check the src to make sure it looks like a valid image URL
39
	 * @var boolean
40
	 */
41
	var $validate_src = true;
42
43
	/**
44
	 * Handle being treated as a string by returning the HTML
45
	 * @return string HTML of image
46
	 */
47
	function __toString() {
48
		return $this->html();
49
	}
50
51
52
	function __construct( $atts = array() ) {
53
54
		$defaults = array(
55
			'width' => $this->width,
56
			'height' => $this->height,
57
			'alt' => $this->alt,
58
			'title' => $this->title,
59
			'size' => $this->size,
60
			'src' => $this->src,
61
			'class' => $this->class,
62
			'getimagesize' => false,
63
			'validate_src' => true
64
		);
65
66
		$atts = wp_parse_args( $atts, $defaults );
67
68
		foreach( $atts as $key => $val ) {
69
			$this->{$key} = $val;
70
		}
71
72
		$this->class = !empty( $this->class ) ? esc_attr( implode( ' ', (array)$this->class ) ) : $this->class;
73
74
		$this->set_image_size();
75
76
	}
77
78
	/**
79
	 * Verify that the src URL matches image patterns.
80
	 *
81
	 * Yes, images can not have extensions, but this is a basic check. To disable this,
82
	 * set `validate_src` to false when instantiating the object.
83
	 *
84
	 * @return boolean     True: matches pattern; False: does not match pattern.
85
	 */
86
	function validate_image_src() {
87
88
		if ( !$this->validate_src ) { return true; }
89
90
		$info = pathinfo( $this->src );
91
92
		/**
93
		 * @filter `gravityview_image_extensions` Extensions that GravityView recognizes as valid images to be shown in an `img` tag
94
		 * @param array $image_exts Default: `['jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico']`
95
		 */
96
		$image_exts = apply_filters( 'gravityview_image_extensions', array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico' ));
97
98
		return isset( $info['extension'] ) && in_array( strtolower( $info['extension'] ), $image_exts);
99
	}
100
101
	/**
102
	 * Get default widths and heights for image size.
103
	 *
104
	 * @return void
105
	 */
106
	public function set_image_size( $string = NULL, $width = NULL, $height = NULL ) {
107
108
109
		// If there is no width or height passed
110
		if ( empty( $width ) || empty( $height ) ) {
111
112
			// And there is no string size passed
113
			// 		And we want to get the image size using PHP
114
			if ( empty( $string ) && !empty( $this->getimagesize ) ) {
115
116
				$image_size = @getimagesize( $this->src );
0 ignored issues
show
Silencing errors is discouraged
Loading history...
117
118
				// If it didn't return a response, it may be a HTTPS/SSL error
119
				if ( empty( $image_size[0] ) ) {
120
					$image_size = @getimagesize( set_url_scheme( $this->src, 'http' ) );
0 ignored issues
show
Silencing errors is discouraged
Loading history...
121
				}
122
123
				if ( !empty( $image_size ) ) {
124
					list( $width, $height ) = $image_size;
125
				}
126
127
			}
128
			// Otherwise, we calculate based on the string size value
129
			else {
130
131
				/**
132
				 * @filter `gravityview_image_sizes` Modify the image size presets used by GravityView_Image class
133
				 * @param array $image_sizes Array of image sizes with the key being the size slug, and the value being an array with `width` and `height` defined, in pixels
134
				 */
135
				$image_sizes = apply_filters( 'gravityview_image_sizes', array(
136
					'tiny' => array('width' => 40, 'height' => 30),
137
					'small' => array('width' => 100, 'height' => 75),
138
					'medium' => array('width' => 250, 'height' => 188),
139
					'large' => array('width' => 448, 'height' => 336),
140
				) );
141
142
				switch( $this->size ) {
143
					case 'tiny':
144
						extract($image_sizes['tiny']);
145
						break;
146
					case 'small':
147
					case 's':
148
					case 'thumb':
149
						extract($image_sizes['small']);
150
						break;
151
					case 'm':
152
					case 'medium':
153
						extract($image_sizes['medium']);
154
						break;
155
					case 'large':
156
					case 'l':
157
						extract($image_sizes['large']);
158
						break;
159
					default:
160
						// Verify that the passed sizes are integers.
161
						$width = !empty( $width ) ? intval( $width ) : intval( $this->width );
162
						$height = !empty( $height ) ? intval( $height ) : intval( $this->height );
163
				}
164
165
			}
166
167
		}
168
169
		$this->width = $width;
170
		$this->height = $height;
171
	}
172
173
	/**
174
	 * Return the HTML tag for the image
175
	 * @return string HTML of the image
176
	 */
177
	public function html() {
178
179
		$html = '';
180
181
		if ( $this->validate_image_src() && ! empty( $this->src ) ) {
182
			$atts = '';
183
			foreach ( array( 'width', 'height', 'alt', 'title', 'class' ) as $attr ) {
184
185
				if ( empty( $this->{$attr} ) ) { continue; }
186
187
				$atts .= sprintf( ' %s="%s"', $attr, esc_attr( $this->{$attr} ) );
188
			}
189
190
			$html = sprintf( '<img src="%s"%s />', esc_url_raw( $this->src ), $atts );
191
		}
192
193
		/**
194
		 * @filter `gravityview_image_html` Filter the HTML image output
195
		 * @param string $html the generated image html
196
		 * @param GravityView_Image $this The current image object
197
		 */
198
		return apply_filters( 'gravityview_image_html', $html, $this );
199
	}
200
}
201