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

GravityView_Image::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Generic class for generating image tag
4
 */
5
class GravityView_Image {
6
7
	var $alt;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $alt.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
8
	var $src;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $src.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
9
	var $title;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $title.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
10
	var $width;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $width.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
11
	var $height;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $height.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
12
13
	/**
14
	 * HTML class attribute.
15
	 * @var string
16
	 */
17
	var $class = NULL;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $class.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
18
19
	/**
20
	 * HTML style attribute
21
	 * @var string
22
	 */
23
	var $style = NULL;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $style.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
24
25
	/**
26
 	 * String representing size of image - Choose from "full", "medium", "thumb", "tiny"
27
	 * @var string
28
	 */
29
	var $size = NULL;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $size.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
30
31
	/**
32
	 * Use PHP getimagesize function to auto-calculate image size?
33
	 * @var boolean
34
	 */
35
	var $getimagesize = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $getimagesize.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
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;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $validate_src.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
42
43
	/**
44
	 * Handle being treated as a string by returning the HTML
45
	 * @return string HTML of image
46
	 */
47
	function __toString() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
48
		return $this->html();
49
	}
50
51
52
	function __construct( $atts = array() ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
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;
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
87
88
		if ( !$this->validate_src ) { return true; }
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
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' ));
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
97
98
		return isset( $info['extension'] ) && in_array( strtolower( $info['extension'] ), $image_exts);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
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 ) {
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
107
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
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 ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
115
116
				$image_size = @getimagesize( $this->src );
0 ignored issues
show
Coding Style introduced by
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
Coding Style introduced by
Silencing errors is discouraged
Loading history...
121
				}
122
123
				if ( !empty( $image_size ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
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),
0 ignored issues
show
introduced by
No space after opening parenthesis of array is bad style
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
137
					'small' => array('width' => 100, 'height' => 75),
0 ignored issues
show
introduced by
No space after opening parenthesis of array is bad style
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
138
					'medium' => array('width' => 250, 'height' => 188),
0 ignored issues
show
introduced by
No space after opening parenthesis of array is bad style
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
139
					'large' => array('width' => 448, 'height' => 336),
0 ignored issues
show
introduced by
No space after opening parenthesis of array is bad style
Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
140
				) );
141
142
				switch( $this->size ) {
143
					case 'tiny':
144
						extract($image_sizes['tiny']);
0 ignored issues
show
introduced by
extract() usage is highly discouraged, due to the complexity and unintended issues it might cause.
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
145
						break;
146
					case 'small':
147
					case 's':
148
					case 'thumb':
149
						extract($image_sizes['small']);
0 ignored issues
show
introduced by
extract() usage is highly discouraged, due to the complexity and unintended issues it might cause.
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
150
						break;
151
					case 'm':
152
					case 'medium':
153
						extract($image_sizes['medium']);
0 ignored issues
show
introduced by
extract() usage is highly discouraged, due to the complexity and unintended issues it might cause.
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
154
						break;
155
					case 'large':
156
					case 'l':
157
						extract($image_sizes['large']);
0 ignored issues
show
introduced by
extract() usage is highly discouraged, due to the complexity and unintended issues it might cause.
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
158
						break;
159
					default:
160
						// Verify that the passed sizes are integers.
161
						$width = !empty( $width ) ? intval( $width ) : intval( $this->width );
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
162
						$height = !empty( $height ) ? intval( $height ) : intval( $this->height );
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
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