Completed
Push — develop ( ddf92f...b59ba1 )
by David
11:43 queued 05:32
created

WL_Metabox_Field_sameas::html_input()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Metaboxes: sameAs Field Metabox.
4
 *
5
 * @since      3.0.0
6
 * @package    Wordlift
7
 * @subpackage Wordlift/admin/WL_Metabox
8
 */
9
10
/**
11
 * Define the {@link WL_Metabox_Field_sameas} class.
12
 *
13
 * @since      3.0.0
14
 * @package    Wordlift
15
 * @subpackage Wordlift/admin/WL_Metabox
16
 */
17
class WL_Metabox_Field_sameas extends WL_Metabox_Field {
18
19
	/**
20
	 * @inheritdoc
21
	 */
22
	public function __construct( $args ) {
23
		parent::__construct( $args['sameas'] );
24
	}
25
26
	/**
27
	 * @inheritdoc
28
	 */
29
	public function save_data( $values ) {
30
		// The autocomplete select may send JSON arrays in input values.
31
		mb_regex_encoding( 'UTF-8' );
32
		$merged = array_reduce( (array) $values, function ( $carry, $item ) {
33
			return array_merge( $carry, mb_split( "\x{2063}", wp_unslash( $item ) ) );
34
		}, array() );
35
36
		parent::save_data( $merged );
37
	}
38
39
	/**
40
	 * @inheritdoc
41
	 */
42
	public function sanitize_data_filter( $value ) {
43
44
		// Call our sanitizer helper.
45
		return Wordlift_Sanitizer::sanitize_url( $value );
46
	}
47
48
	/**
49
	 * @inheritdoc
50
	 */
51
	protected function get_heading_html() {
52
53
		// Add the select html fragment after the heading.
54
		return parent::get_heading_html()
55
			   . $this->get_select_html();
56
	}
57
58
	/**
59
	 * Get the select html fragment.
60
	 *
61
	 * @since 3.15.0
62
	 * @return string The html fragment.
63
	 */
64
	private function get_select_html() {
65
		// Return an element where the new Autocomplete Select will attach to.
66
		return '<p>'
67
			   . esc_html__( 'Use the search below to link this entity with equivalent entities in the linked data cloud.', 'wordlift' )
68
			   . '<div id="wl-metabox-field-sameas"></div></p>';
69
	}
70
71
	/**
72
	 * @inheritdoc
73
	 */
74
	protected function get_add_button_html( $count ) {
75
76
		$placeholder = esc_attr_x( 'Type here the URL of an equivalent entity from another dataset.', 'sameAs metabox input', 'wordlift' );
77
78
		return
79
			"<label for='$this->meta_name'>"
80
			. esc_html__( 'If you already know the URL of the entity that you would like to link, add it in the field below.', 'wordlift' )
81
			. '</label>'
82
			. '<div class="wl-input-wrapper">'
83
			. "<input type='text' id='$this->meta_name' name='wl_metaboxes[$this->meta_name][]' placeholder='$placeholder' style='width:88%' />"
84
			. '<button class="button wl-remove-input wl-button" type="button">Remove</button>'
85
			. '</div>'
86
			. parent::get_add_button_html( $count );
87
	}
88
89
	/**
90
	 * @inheritdoc
91
	 */
92
	protected function get_stored_values_html( &$count ) {
93
94
		return '<p>'
95
			   . parent::get_stored_values_html( $count )
96
			   . '</p>';
97
	}
98
99
	/**
100
	 * @inheritdoc
101
	 */
102
	public function html() {
103
104
		// Open main <div> for the Field.
105
		$html = $this->html_wrapper_open();
106
107
		// Label.
108
		$html .= $this->get_heading_html();
109
110
		// print nonce.
111
		$html .= $this->html_nonce();
112
113
		// print data loaded from DB.
114
		$count = 0;
115
116
		// If cardinality allows it, print button to add new values.
117
		$html .= $this->get_add_button_html( $count );
118
119
		$html .= $this->get_stored_values_html( $count );
120
121
		// Close the HTML wrapper.
122
		$html .= $this->html_wrapper_close();
123
124
		return $html;
125
	}
126
127
	/**
128
	 * @inheritdoc
129
	 */
130
	public function html_input( $value ) {
131
		$html = <<<EOF
132
			<div class="wl-input-wrapper">
133
				<input type="text" readonly="readonly" id="$this->meta_name" name="wl_metaboxes[$this->meta_name][]" value="$value" style="width:88%" />
134
				<button class="button wl-remove-input wl-button" type="button">Remove</button>
135
			</div>
136
EOF;
137
138
		return $html;
139
	}
140
141
}
142