Completed
Push — master ( 049abe...7733bc )
by Andrew
10s
created

ClassyImage::src()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 6
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 14
rs 9.4285
1
<?php
2
3
class ClassyImage extends ClassyBasis {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
5
	/**
6
	 * Current image id
7
	 * @var int
8
	 */
9
	public $ID;
10
11
	/**
12
	 * Main constructor function. Requires image id
13
	 * @param int $pid
14
	 */
15
	public function __construct( $pid = null ) {
16
17
		// Checks if image with this id exists
18
		if ( $pid && wp_get_attachment_image_src( $pid ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pid of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
19
20
			$this->ID = $pid;
21
22
		} else {
23
24
			$this->ID = 0;
25
26
		}
27
	}
28
29
	/**
30
	 * Returns default image url
31
	 * @return string
32
	 */
33
	public static function get_default_image() {
34
35
		// You can put here any url
36
		return THEME_DIR . '/assets/noimage.png';
37
38
	}
39
40
	/**
41
	 * Returns image url. In case image ID is 0 or not set returns default image
42
	 * @param  string $size
43
	 * @return string
44
	 */
45
	public function src( $size = 'medium' ) {
46
47
		if ( $this->ID ) {
48
49
			$thumb = wp_get_attachment_image_src( $this->ID, $size );
50
51
			return $thumb[0];
52
53
		} else {
54
55
			return self::get_default_image();
56
57
		}
58
	}
59
}
60