Completed
Push — develop ( 294427...9dd6e7 )
by David
01:58
created

image::get_preview()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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
	public function set_assets() {
54
55
		$this->assets['style']['image-control']  = $this->url . 'assets/controls/image/css/image' . UIX_ASSET_DEBUG . '.css';
56
		$this->assets['script']['image-control'] = [
57
			'src'       => $this->url . 'assets/controls/image/js/image' . UIX_ASSET_DEBUG . '.js',
58
			'in_footer' => true,
59
		];
60
		parent::set_assets();
61
	}
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
	public function input() {
72
73
		$output      = '<input type="hidden" value="' . esc_attr( $this->get_value() ) . '" ' . $this->build_attributes() . '>';
74
		$button_text = isset( $this->struct['add_label'] ) ? $this->struct['add_label'] : __( 'Select Image', 'uix' );
75
		$output      .= '<div class="uix-image-control-wrapper" id="' . esc_attr( $this->id() ) . '-wrap">';
76
		if ( null !== $this->get_value() ) {
77
			$output .= $this->get_preview();
78
		}
79
		$output .= '</div>';
80
		$output .= '<button type="button" data-target="' . esc_attr( $this->id() ) . '" class="button button-small uix-image-control-button">' . esc_html( $button_text ) . '</button>';
81
82
		return $output;
83
	}
84
85
	/**
86
	 * Render the Control preview.
87
	 *
88
	 * @since  1.0.0
89
	 * @access public
90
	 * @return string HTML of rendered preview
91
	 */
92
	private function get_preview() {
93
		$data   = wp_get_attachment_image_src( $this->get_value(), 'medium' );
94
		$return = null;
95
		if ( ! empty( $data ) ) {
96
			$return = '<img src="' . esc_attr( $data[0] ) . '" class="uix-image-control-preview"><a href="#" class="uix-image-control-remove" data-target="' . $this->id() . '"><span class="dashicons dashicons-no"></span></a>';
97
		}
98
99
		return $return;
100
101
	}
102
}
103