Image::get_default_image()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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