image::input()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 5
eloc 9
c 3
b 0
f 1
nc 16
nop 0
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 5
rs 8.8571
1
<?php
2
/**
3
 * UIX Image Control
4
 *
5
 * @package   controls
6
 * @author    David Cramer
7
 * @license   GPL-2.0+
8
 * @link
9
 * @copyright 2016 David Cramer
10
 */
11
12
namespace uix\ui\control;
13
14
/**
15
 * Image picker field
16
 *
17
 * @since 3.0.0
18
 */
19
class image extends \uix\ui\control {
20
21
	/**
22
	 * The type of object
23
	 *
24
	 * @since       1.0.0
25
	 * @access      public
26
	 * @var         string
27
	 */
28
	public $type = 'image';
29
30
	/**
31
	 * Checks if the current control is active.
32
	 *
33
	 * @since  1.0.0
34
	 * @access public
35
	 */
36
	public function is_active() {
37
		$active = parent::is_active();
38
39
		if ( true === $active ) {
40
			wp_enqueue_media();
41
		}
42
43
		return $active;
44
	}
45
46
	/**
47
	 * Define core UIX scripts - override to register core ( common scripts for
48
	 * uix type )
49
	 *
50
	 * @since  1.0.0
51
	 * @access public
52
	 */
53 1
	public function set_assets() {
54
55 1
		$this->assets['style']['image-control']  = $this->url . 'assets/controls/image/css/image' . UIX_ASSET_DEBUG . '.css';
56 1
		$this->assets['script']['image-control'] = [
57 1
			'src'       => $this->url . 'assets/controls/image/js/image' . UIX_ASSET_DEBUG . '.js',
58
			'in_footer' => true,
59
		];
60 1
		parent::set_assets();
61 1
	}
62
63
	/**
64
	 * Returns the main input field for rendering
65
	 *
66
	 * @since  1.0.0
67
	 * @see    \uix\ui\uix
68
	 * @access public
69
	 * @return string Input field HTML string
70
	 */
71 1
	public function input() {
72
73 1
		$output       = '<input type="hidden" value="' . esc_attr( $this->get_value() ) . '" ' . $this->build_attributes() . '>';
74 1
		$button_text  = isset( $this->struct['add_label'] ) ? $this->struct['add_label'] : __( 'Select Image', 'uix' );
75 1
		$preview_size = isset( $this->struct['preview_size'] ) ? $this->struct['preview_size'] : 'medium';
76 1
		$type         = isset( $this->struct['type'] ) ? $this->struct['type'] : 'id';
77 1
		$size         = isset( $this->struct['size'] ) ? $this->struct['size'] : 'full';
78 1
		$output      .= $this->preview( $preview_size );
79
		$output      .= '<button type="button" data-size="' . esc_attr( $size ) . '" data-type="' . esc_attr( $type ) . '" data-preview-size="' . esc_attr( $preview_size ) . '" data-target="' . esc_attr( $this->id() ) . '" class="button button-small uix-image-control-button">' . esc_html( $button_text ) . '</button>';
80 1
81 1
		return $output;
82
	}
83 1
84
	/**
85
	 * Returns code needed for the preview image.
86
	 *
87
	 * @since  3.0.0
88
	 * @see    \uix\ui\uix
89
	 * @access public
90
	 *
91
	 * @param string $preview_size The size of the preview image.
92
	 *
93
	 * @return string HTML string of the preview.
94
	 */
95
	public function preview( $preview_size ) {
96 1
		$output = '<div class="uix-image-control-wrapper" id="' . esc_attr( $this->id() ) . '-wrap">';
97 1
		if ( null !== $this->get_value() ) {
98 1
			$output .= $this->get_preview( $preview_size );
99 1
		}
100
		$output .= '</div>';
101
102
		return $output;
103 1
	}
104
105
	/**
106
	 * Render the Control preview.
107
	 *
108
	 * @since  1.0.0
109
	 * @access public
110
	 *
111
	 * @param string $preview_size The size of the preview.
112
	 *
113
	 * @return string HTML of rendered preview.
114
	 */
115
	private function get_preview( $preview_size ) {
116
		$url    = $this->get_url( $preview_size );
117
		$return = null;
118
		if ( ! empty( $url ) ) {
119
			$return = '<img src="' . esc_attr( $url ) . '" class="uix-image-control-preview"><a href="#" class="uix-image-control-remove" data-target="' . $this->id() . '"><span class="dashicons dashicons-no"></span></a>';
120
		}
121
122
		return $return;
123
	}
124
125
	/**
126
	 * Get the URL of the image of the control.
127
	 *
128
	 * @since  3.0.0
129
	 * @access public
130
	 *
131
	 * @param string $size The size of image to get url for.
132
	 *
133
	 * @return string URL to the requested image.
134
	 */
135
	public function get_url( $size ) {
136
		$url = null;
137
		if ( isset( $this->struct['return_type'] ) && 'url' === $this->struct['return_type'] ) {
138
			$url = $this->get_value();
139
		} else {
140
			$data = wp_get_attachment_image_src( $this->get_value(), $size );
141
			if ( ! empty( $data ) ) {
142
				$url = $data[0];
143
			}
144
		}
145
146
		return $url;
147
	}
148
}
149