Completed
Push — master ( bc6754...ed64c2 )
by
unknown
07:19
created

Image::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
namespace Classy\Models;
4
5
use Classy\Basis;
6
7
class Image extends Basis {
8
9
	/**
10
	 * Current image id
11
	 * @var int
12
	 */
13
	public $ID;
14
15
	/**
16
	 * Main constructor function. Requires image id
17
	 * @param int $pid
18
	 */
19
	public function __construct( $pid = null ) {
20
21
		// Checks if image with this id exists
22
		if ( null !== $pid && wp_get_attachment_image_src( $pid ) ) {
23
24
			$this->ID = $pid;
25
26
		} else {
27
28
			$this->ID = 0;
29
30
		}
31
	}
32
33
	/**
34
	 * Returns default image url
35
	 * @return string
36
	 */
37
	public static function get_default_image() {
38
39
		// You can put here any url
40
		return CLASSY_THEME_DIR . '/assets/noimage.png';
41
42
	}
43
44
	/**
45
	 * Returns image url. In case image ID is 0 or not set returns default image
46
	 * @param  string $size
47
	 * @return string
48
	 */
49
	public function src( $size = 'medium' ) {
50
51
		if ( $this->ID ) {
52
53
			$thumb = wp_get_attachment_image_src( $this->ID, $size );
54
55
			return $thumb[0];
56
57
		} else {
58
59
			return self::get_default_image();
60
61
		}
62
	}
63
}
64