Completed
Pull Request — master (#851)
by
unknown
02:58
created

TimberImage::height()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * If TimberPost is the class you're going to spend the most time, TimberImage is the class you're going to have the most fun with.
5
 * @example
6
 * ```php
7
 * $context = Timber::get_context();
8
 * $post = new TimberPost();
9
 * $context['post'] = $post;
10
 *
11
 * // lets say you have an alternate large 'cover image' for your post stored in a custom field which returns an image ID
12
 * $cover_image_id = $post->cover_image;
13
 * $context['cover_image'] = new TimberImage($cover_image_id);
14
 * Timber::render('single.twig', $context);
15
 * ```
16
 *
17
 * ```twig
18
 * <article>
19
 * 	<img src="{{cover_image.src}}" class="cover-image" />
20
 * 	<h1 class="headline">{{post.title}}</h1>
21
 * 	<div class="body">
22
 * 		{{post.content}}
23
 * 	</div>
24
 *
25
 * 	<img src="{{ Image(post.custom_field_with_image_id).src }}" alt="Another way to initialize images as TimberImages, but within Twig" />
26
 * </article>
27
 * ```
28
 *
29
 * ```html
30
 * <article>
31
 * 	<img src="http://example.org/wp-content/uploads/2015/06/nevermind.jpg" class="cover-image" />
32
 * 	<h1 class="headline">Now you've done it!</h1>
33
 * 	<div class="body">
34
 * 		Whatever whatever
35
 * 	</div>
36
 * 	<img src="http://example.org/wp-content/uploads/2015/06/kurt.jpg" alt="Another way to initialize images as TimberImages, but within Twig" />
37
 * </article>
38
 * ```
39
 */
40
class TimberImage extends TimberPost implements TimberCoreInterface {
41
42
	protected $_can_edit;
43
	protected $_dimensions;
44
	public $abs_url;
45
	/**
46
	 * @var string $object_type what does this class represent in WordPress terms?
47
	 */
48
	public $object_type = 'image';
49
	/**
50
	 * @var string $representation what does this class represent in WordPress terms?
51
	 */
52
	public static $representation = 'image';
53
	/**
54
	 * @var array of supported relative file types
55
	 */
56
	private $file_types = array('jpg', 'jpeg', 'png', 'svg', 'bmp', 'ico', 'gif', 'tiff', 'pdf');
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...
57
	/**
58
	 * @api
59
	 * @var string $file_loc the location of the image file in the filesystem (ex: `/var/www/htdocs/wp-content/uploads/2015/08/my-pic.jpg`)
60
	 */
61
	public $file_loc;
62
	public $file;
63
	public $sizes = array();
64
	/**
65
	 * @api
66
	 * @var string $caption the string stored in the WordPress database
67
	 */
68
	public $caption;
69
	/**
70
	 * @var $_wp_attached_file the file as stored in the WordPress database
71
	 */
72
	protected $_wp_attached_file;
73
74
	/**
75
	 * Creates a new TimberImage object
76
	 * @example
77
	 * ```php
78
	 * // You can pass it an ID number
79
	 * $myImage = new TimberImage(552);
80
	 *
81
	 * //Or send it a URL to an image
82
	 * $myImage = new TimberImage('http://google.com/logo.jpg');
83
	 * ```
84
	 * @param int|string $iid
85
	 */
86
	public function __construct($iid) {
87
		$this->init($iid);
0 ignored issues
show
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...
88
	}
89
90
	/**
91
	 * @return string the src of the file
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
92
	 */
93
	public function __toString() {
94
		if ( $this->get_src() ) {
0 ignored issues
show
Deprecated Code introduced by
The method TimberImage::get_src() has been deprecated with message: 0.21.9 use TimberImage::src

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
95
			return $this->get_src();
0 ignored issues
show
Deprecated Code introduced by
The method TimberImage::get_src() has been deprecated with message: 0.21.9 use TimberImage::src

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
96
		}
97
		return '';
98
	}
99
100
	/**
101
	 * Get a PHP array with pathinfo() info from the file
102
	 * @return array
103
	 */
104
	function get_pathinfo() {
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...
105
		return pathinfo($this->file);
0 ignored issues
show
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...
106
	}
107
108
	/**
109
	 * @internal
110
	 * @param string $dim
0 ignored issues
show
Documentation introduced by
Should the type for parameter $dim not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
111
	 * @return array|int
112
	 */
113
	protected function get_dimensions($dim = null) {
114
		if ( isset($this->_dimensions) ) {
115
			return $this->get_dimensions_loaded($dim);
0 ignored issues
show
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...
116
		}
117
		if ( file_exists($this->file_loc) && filesize($this->file_loc) ) {
0 ignored issues
show
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...
118
			list($width, $height) = getimagesize($this->file_loc);
0 ignored issues
show
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...
119
			$this->_dimensions = array();
120
			$this->_dimensions[0] = $width;
121
			$this->_dimensions[1] = $height;
122
			return $this->get_dimensions_loaded($dim);
0 ignored issues
show
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...
123
		}
124
	}
125
126
	/**
127
	 * @internal
128
	 * @param string|null $dim
129
	 * @return array|int
130
	 */
131
	protected function get_dimensions_loaded($dim) {
132
		if ( $dim === null ) {
133
			return $this->_dimensions;
134
		}
135
		if ( $dim == 'w' || $dim == 'width' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
136
			return $this->_dimensions[0];
137
		}
138
		if ( $dim == 'h' || $dim == 'height' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
139
			return $this->_dimensions[1];
140
		}
141
		return null;
142
	}
143
144
	/**
145
	 * @internal
146
	 * @param  int $iid the id number of the image in the WP database
147
	 */
148
	protected function get_image_info( $iid ) {
149
		$image_info = $iid;
150
		if (is_numeric($iid)) {
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
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...
introduced by
No space before closing parenthesis is prohibited
Loading history...
151
			$image_info = wp_get_attachment_metadata($iid);
0 ignored issues
show
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...
152
			if (!is_array($image_info)) {
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
Expected 1 space before "!"; 0 found
Loading history...
introduced by
Expected 1 space after "!"; 0 found
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...
introduced by
No space before closing parenthesis is prohibited
Loading history...
153
				$image_info = array();
154
			}
155
			$image_custom = get_post_custom($iid);
0 ignored issues
show
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...
156
			$basic = get_post($iid);
0 ignored issues
show
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...
157
			if ($basic) {
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
158
				if (isset($basic->post_excerpt)) {
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
159
					$this->caption = $basic->post_excerpt;
160
				}
161
				$image_custom = array_merge($image_custom, get_object_vars($basic));
0 ignored issues
show
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...
162
			}
163
			return array_merge($image_info, $image_custom);
0 ignored issues
show
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...
164
		}
165
		if (is_array($image_info) && isset($image_info['image'])) {
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
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...
introduced by
No space before closing parenthesis is prohibited
Loading history...
166
			return $image_info['image'];
167
		}
168
		if (is_object($image_info)) {
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
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...
introduced by
No space before closing parenthesis is prohibited
Loading history...
169
		   return get_object_vars($image_info);
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 3 tabs, found 2
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...
170
		}
171
		return $iid;
172
	}
173
174
	/**
175
	 * @internal
176
	 * @param  string $url for evaluation
177
	 * @return string with http/https corrected depending on what's appropriate for server
178
	 */
179
	protected static function _maybe_secure_url($url) {
180
		if ( is_ssl() && strpos($url, 'https') !== 0 && strpos($url, 'http') === 0 ) {
0 ignored issues
show
introduced by
Found "!== 0". Use Yoda Condition checks, you must
Loading history...
introduced by
Found "=== 0". Use Yoda Condition checks, you must
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...
181
			$url = 'https' . substr($url, strlen('http'));
0 ignored issues
show
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...
182
		}
183
		return $url;
184
	}
185
186
	public static function wp_upload_dir() {
187
		static $wp_upload_dir = false;
188
189
		if ( !$wp_upload_dir ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
190
			$wp_upload_dir = wp_upload_dir();
191
		}
192
193
		return $wp_upload_dir;
194
	}
195
196
	/**
197
	 * @internal
198
	 * @param int $iid
0 ignored issues
show
Documentation introduced by
Should the type for parameter $iid not be false|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
199
	 */
200
	function init( $iid = false ) {
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...
201
		if ( !is_numeric( $iid ) && is_string( $iid ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
202
			if (strstr($iid, '://')) {
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
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...
introduced by
No space before closing parenthesis is prohibited
Loading history...
203
				$this->init_with_url($iid);
0 ignored issues
show
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...
204
				return;
205
			}
206
			if ( strstr($iid, ABSPATH) ) {
0 ignored issues
show
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...
207
				$this->init_with_file_path($iid);
0 ignored issues
show
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...
208
				return;
209
			}
210
			
211
			$relative = false;
212
			$iid_lower = strtolower($iid);
0 ignored issues
show
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...
213
			foreach( $this->file_types as $type ) { if( strstr( $iid_lower, $type ) ) { $relative = true; break; } };
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
introduced by
Blank line found after control structure
Loading history...
214
			if ( $relative ) {
215
				$this->init_with_relative_path( $iid );
216
				return;
217
			}
218
		}
219
220
		$image_info = $this->get_image_info($iid);
0 ignored issues
show
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...
221
222
		$this->import($image_info);
0 ignored issues
show
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...
223
		$basedir = self::wp_upload_dir();
224
		$basedir = $basedir['basedir'];
225
		if ( isset($this->file) ) {
226
			$this->file_loc = $basedir . DIRECTORY_SEPARATOR . $this->file;
227
		} else if ( isset($this->_wp_attached_file) ) {
228
			$this->file = reset($this->_wp_attached_file);
0 ignored issues
show
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...
229
			$this->file_loc = $basedir . DIRECTORY_SEPARATOR . $this->file;
230
		}
231
		if ( isset($image_info['id']) ) {
232
			$this->ID = $image_info['id'];
233
		} else if ( is_numeric($iid) ) {
0 ignored issues
show
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...
234
			$this->ID = $iid;
0 ignored issues
show
Documentation Bug introduced by
It seems like $iid can also be of type integer or double. However, the property $ID is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
235
		}
236
		if ( isset($this->ID) ) {
237
			$custom = get_post_custom($this->ID);
0 ignored issues
show
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...
238
			foreach ($custom as $key => $value) {
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
239
				$this->$key = $value[0];
240
			}
241
		} else {
242
			if ( is_array($iid) || is_object($iid) ) {
0 ignored issues
show
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...
243
				TimberHelper::error_log('Not able to init in TimberImage with iid=');
0 ignored issues
show
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...
244
				TimberHelper::error_log($iid);
0 ignored issues
show
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...
245
			} else {
246
				TimberHelper::error_log('Not able to init in TimberImage with iid=' . $iid);
0 ignored issues
show
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...
247
			}
248
		}
249
	}
250
251
	/**
252
	 * @internal
253
	 * @param string $relative_path
254
	 */
255
	protected function init_with_relative_path( $relative_path ) {
256
		$this->abs_url = home_url( $relative_path );
257
		$file_path = TimberURLHelper::get_full_path( $relative_path );
258
		$this->file_loc = $file_path;
259
		$this->file = $file_path;
260
	}
261
262
	/**
263
	 * @internal
264
	 * @param string $file_path
265
	 */
266
	protected function init_with_file_path( $file_path ) {
267
		$url = TimberURLHelper::file_system_to_url( $file_path );
268
		$this->abs_url = $url;
269
		$this->file_loc = $file_path;
270
		$this->file = $file_path;
271
	}
272
273
	/**
274
	 * @internal
275
	 * @param string $url
276
	 */
277
	protected function init_with_url($url) {
278
		$this->abs_url = $url;
279
		if ( TimberURLHelper::is_local($url) ) {
0 ignored issues
show
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...
280
			$this->file = ABSPATH . TimberURLHelper::get_rel_url($url);
0 ignored issues
show
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...
281
			$this->file_loc = ABSPATH . TimberURLHelper::get_rel_url($url);
0 ignored issues
show
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...
282
		}
283
	}
284
285
	/**
286
	 * @api
287
	 * @example
288
	 * ```twig
289
	 * <img src="{{ image.src }}" alt="{{ image.alt }}" />
290
	 * ```
291
	 * ```html
292
	 * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" alt="W3 Checker told me to add alt text, so I am" />
293
	 * ```
294
	 * @return string alt text stored in WordPress
295
	 */
296
	public function alt() {
297
		$alt = trim(strip_tags(get_post_meta($this->ID, '_wp_attachment_image_alt', true)));
0 ignored issues
show
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...
298
		return $alt;
299
	}
300
301
	/**
302
	 * @api
303
	 * @example
304
	 * ```twig
305
	 * {% if post.thumbnail.aspect < 1 %}
306
	 *     {# handle vertical image #}
307
	 *     <img src="{{ post.thumbnail.src|resize(300, 500) }}" alt="A basketball player" />
308
	 * {% else %}
309
	 * 	   <img src="{{ post.thumbnail.src|resize(500) }}" alt="A sumo wrestler" />
310
	 * {% endif %}
311
	 * ```
312
	 * @return float
0 ignored issues
show
Documentation introduced by
Should the return type not be integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
313
	 */
314
	public function aspect() {
315
		$w = intval($this->width());
0 ignored issues
show
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...
316
		$h = intval($this->height());
0 ignored issues
show
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...
317
		return $w / $h;
318
	}
319
320
	/**
321
	 * @api
322
	 * @example
323
	 * ```twig
324
	 * <img src="{{ image.src }}" height="{{ image.height }}" />
325
	 * ```
326
	 * ```html
327
	 * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" height="900" />
328
	 * ```
329
	 * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be array|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
330
	 */
331
	public function height() {
332
		return $this->get_dimensions('height');
0 ignored issues
show
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...
333
	}
334
335
	/**
336
	 * Returns the link to an image attachment's Permalink page (NOT the link for the image itself!!)
337
	 * @api
338
	 * @example
339
	 * ```twig
340
	 * <a href="{{ image.link }}"><img src="{{ image.src }} "/></a>
341
	 * ```
342
	 * ```html
343
	 * <a href="http://example.org/my-cool-picture"><img src="http://example.org/wp-content/uploads/2015/whatever.jpg"/></a>
344
	 * ```
345
	 */
346
	public function link() {
347
		if ( strlen($this->abs_url) ) {
0 ignored issues
show
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...
348
			return $this->abs_url;
349
		}
350
		return get_permalink($this->ID);
0 ignored issues
show
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...
351
	}
352
353
	/**
354
	 * @api
355
	 * @return bool|TimberPost
0 ignored issues
show
Documentation introduced by
Should the return type not be false|object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
356
	 */
357
	public function parent() {
358
		if ( !$this->post_parent ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
359
			return false;
360
		}
361
		return new $this->PostClass($this->post_parent);
0 ignored issues
show
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...
362
	}
363
364
	/**
365
	 * @api
366
	 * @example
367
	 * ```twig
368
	 * <img src="{{ image.path }}" />
369
	 * ```
370
	 * ```html
371
	 * <img src="/wp-content/uploads/2015/08/pic.jpg" />
372
	 * ```
373
	 * @return  string the /relative/path/to/the/file
374
	 */
375
	public function path() {
376
		return TimberURLHelper::get_rel_path($this->src());
0 ignored issues
show
Bug introduced by
It seems like $this->src() targeting TimberImage::src() can also be of type boolean; however, TimberURLHelper::get_rel_path() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

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...
377
	}
378
379
	/**
380
	 * @param string $size a size known to WordPress (like "medium")
381
	 * @api
382
	 * @example
383
	 * ```twig
384
	 * <h1>{{post.title}}</h1>
385
	 * <img src="{{post.thumbnail.src}}" />
386
	 * ```
387
	 * ```html
388
	 * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" />
389
	 * ```
390
	 * @return bool|string
391
	 */
392
	public function src($size = '') {
393
		if ( isset($this->abs_url) ) {
394
			return $this->_maybe_secure_url($this->abs_url);
0 ignored issues
show
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...
395
		}
396
397
		if ( $size && is_string($size) && isset($this->sizes[$size]) ) {
0 ignored issues
show
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...
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
398
			$image = image_downsize($this->ID, $size);
0 ignored issues
show
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...
399
			return $this->_maybe_secure_url(reset($image));
0 ignored issues
show
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...
400
		}
401
402
		if ( !isset($this->file) && isset($this->_wp_attached_file) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
403
			$this->file = $this->_wp_attached_file;
404
		}
405
406
		if ( !isset($this->file) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
407
			return false;
408
		}
409
410
		$dir = self::wp_upload_dir();
411
		$base = $dir['baseurl'];
412
413
		$src = trailingslashit($this->_maybe_secure_url($base)) . $this->file;
0 ignored issues
show
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...
414
		$src = apply_filters('timber/image/src', $src, $this->ID);
0 ignored issues
show
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...
415
		return apply_filters('timber_image_src', $src, $this->ID);
0 ignored issues
show
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...
416
	}
417
418
	/**
419
	 * @deprecated use src() instead
420
	 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
421
	 */
422
	function url() {
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...
423
		return $this->get_src();
0 ignored issues
show
Deprecated Code introduced by
The method TimberImage::get_src() has been deprecated with message: 0.21.9 use TimberImage::src

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
424
	}
425
426
	/**
427
	 * @api
428
	 * @example
429
	 * ```twig
430
	 * <img src="{{ image.src }}" width="{{ image.width }}" />
431
	 * ```
432
	 * ```html
433
	 * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" width="1600" />
434
	 * ```
435
	 * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be array|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
436
	 */
437
	public function width() {
438
		return $this->get_dimensions('width');
0 ignored issues
show
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...
439
	}
440
441
442
	/**
443
	 * @deprecated 0.21.9 use TimberImage::width() instead
444
	 * @internal
445
	 * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be array|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
446
	 */
447
	function get_width() {
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...
448
		return $this->width();
449
	}
450
451
	/**
452
	 * @deprecated 0.21.9 use TimberImage::height() instead
453
	 * @internal
454
	 * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be array|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
455
	 */
456
	function get_height() {
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...
457
		return $this->height();
458
	}
459
460
	/**
461
	 * @deprecated 0.21.9 use TimberImage::src
462
	 * @internal
463
	 * @param string $size
464
	 * @return bool|string
465
	 */
466
	function get_src( $size = '' ) {
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...
467
		return $this->src( $size );
468
	}
469
470
	/**
471
	 * @deprecated 0.21.9 use TimberImage::path()
472
	 * @internal
473
	 * @return string
474
	 */
475
	function get_path() {
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...
476
		return $this->link();
477
	}
478
479
	/**
480
	 * @deprecated use src() instead
481
	 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
482
	 */
483
	function get_url() {
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...
484
		return $this->get_src();
0 ignored issues
show
Deprecated Code introduced by
The method TimberImage::get_src() has been deprecated with message: 0.21.9 use TimberImage::src

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
485
	}
486
487
	/**
488
	 * @internal
489
	 * @deprecated 0.21.8
490
	 * @return bool|TimberPost
0 ignored issues
show
Documentation introduced by
Should the return type not be false|object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
491
	 */
492
	function get_parent() {
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...
493
		return $this->parent();
494
	}
495
496
	/**
497
	 * @internal
498
	 * @deprecated 0.21.9
499
	 * @see TimberImage::alt
500
	 * @return string
501
	 */
502
	function get_alt() {
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...
503
		return $this->alt();
504
	}
505
506
}
507