PluginProbe
Embed Privacy / trunk
Embed Privacy vtrunk
1.14.0 1.13.0 trunk 0.1 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.10.0 1.10.1 1.10.10 1.10.2 1.10.3 1.10.4 1.10.5 1.10.6 1.10.7 1.10.8 1.10.9 1.11.0 1.11.1 1.11.2 All 68 releases
embed-privacy / inc / admin / class-field.php

class-field.php in Embed Privacy trunk, at inc/admin/class-field.php

235 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace epiphyt\Embed_Privacy\admin;
3
4 /**
5 * Functionality for a single admin field.
6 *
7 * @author Epiphyt
8 * @license GPL2
9 * @package epiphyt\Embed_Privacy
10 * @since 1.10.0
11 */
12 final class Field {
13 /**
14 * Get a field.
15 *
16 * @param array $attributes Field attributes
17 * @param int $post_id Optional post ID
18 */
19 public static function get( array $attributes, $post_id = 0 ) {
20 $attributes = \wp_parse_args( $attributes, [
21 'classes' => 'regular-text',
22 'description' => '',
23 'name' => '',
24 'option_type' => 'meta',
25 'single' => true,
26 'title' => '',
27 'type' => 'text',
28 'validation' => '',
29 ] );
30
31 if ( empty( $attributes['name'] ) || empty( $attributes['title'] ) ) {
32 return;
33 }
34
35 if ( $attributes['option_type'] === 'meta' ) {
36 $current_value = (string) \get_post_meta( $post_id, $attributes['name'], $attributes['single'] );
37 }
38 else {
39 $current_value = (string) \get_option( $attributes['name'] );
40 }
41
42 switch ( $attributes['type'] ) {
43 case 'checkbox':
44 case 'radio':
45 self::get_choice( $attributes, $current_value );
46 break;
47 default:
48 self::get_text( $attributes, $current_value );
49 break;
50 }
51 }
52
53 /**
54 * Get the markup of a field description.
55 *
56 * @since 1.13.1
57 *
58 * @param array $attributes Field attributes
59 * @return string The description markup or an empty string
60 */
61 private static function get_description( array $attributes ) {
62 if ( empty( $attributes['description'] ) ) {
63 return '';
64 }
65
66 $validation = ! empty( $attributes['validation'] ) ? $attributes['validation'] : '';
67
68 if ( empty( $validation ) ) {
69 $description = \esc_html( $attributes['description'] );
70 }
71 else if ( $validation === 'allow-links' ) {
72 $description = \wp_kses( $attributes['description'], [
73 'a' => [
74 'href' => true,
75 'rel' => true,
76 'target' => true,
77 ],
78 ] );
79 }
80 else {
81 return '';
82 }
83
84 return '<p class="description" id="' . \esc_attr( self::get_description_id( $attributes ) ) . '">' . $description . '</p>';
85 }
86
87 /**
88 * Get the ID a field description is referenced by.
89 *
90 * @since 1.13.1
91 *
92 * @param array $attributes Field attributes
93 * @return string The description ID
94 */
95 private static function get_description_id( array $attributes ) {
96 return $attributes['name'] . '-description';
97 }
98
99 /**
100 * Get the aria-describedby attribute of a field, if it has a description.
101 *
102 * @since 1.13.1
103 *
104 * @param array $attributes Field attributes
105 * @return string The attribute or an empty string
106 */
107 private static function get_described_by( array $attributes ) {
108 if ( empty( self::get_description( $attributes ) ) ) {
109 return '';
110 }
111
112 return ' aria-describedby="' . \esc_attr( self::get_description_id( $attributes ) ) . '"';
113 }
114
115 /**
116 * Get a choice field (checkbox or radio button).
117 *
118 * @param array $attributes Field attributes
119 * @param mixed $current_value Current value
120 */
121 public static function get_choice( array $attributes, $current_value ) {
122 if ( empty( $attributes['value'] ) ) {
123 $attributes['value'] = 'yes';
124 }
125
126 \ob_start();
127 ?>
128 <label for="<?php echo \esc_attr( $attributes['name'] ); ?>"><input type="<?php echo \esc_attr( $attributes['type'] ); ?>" name="<?php echo \esc_attr( $attributes['name'] ); ?>" id="<?php echo \esc_attr( $attributes['name'] ); ?>" value="<?php echo \esc_attr( $attributes['value'] ); ?>" class="<?php echo \esc_attr( $attributes['classes'] ); ?>"<?php \checked( $current_value, $attributes['value'] ); ?><?php echo self::get_described_by( $attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>> <?php echo \esc_html( $attributes['title'] ); ?></label>
129 <?php
130 echo self::get_description( $attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
131 $input = \ob_get_clean();
132
133 if ( $attributes['option_type'] === 'option' ) {
134 echo $input; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
135
136 return;
137 }
138 ?>
139 <tr>
140 <th scope="row"></th>
141 <td>
142 <?php echo $input; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
143 </td>
144 </tr>
145 <?php
146 }
147
148 /**
149 * Get an image field.
150 *
151 * @param int $post_id Post ID
152 * @param array $attributes An array with attributes
153 */
154 public static function get_image( $post_id, array $attributes ) {
155 $attributes = \wp_parse_args( $attributes, [
156 'classes' => '',
157 'description' => '',
158 'name' => '',
159 'single' => true,
160 'title' => '',
161 ] );
162
163 if ( empty( $attributes['name'] ) || empty( $attributes['title'] ) ) {
164 return;
165 }
166
167 $attributes['value'] = (string) \get_post_meta( $post_id, $attributes['name'], $attributes['single'] );
168 $image = \wp_get_attachment_image( (int) $attributes['value'] );
169 $label_id = $attributes['name'] . '-label';
170 ?>
171 <tr>
172 <th scope="row">
173 <span id="<?php echo \esc_attr( $label_id ); ?>"><?php echo \esc_html( $attributes['title'] ); ?></span>
174 </th>
175 <td class="embed-privacy-image-item" role="group" aria-labelledby="<?php echo \esc_attr( $label_id ); ?>">
176 <input type="hidden" name="<?php echo \esc_attr( $attributes['name'] ); ?>" value="<?php echo \esc_attr( $attributes['value'] ); ?>" class="embed-privacy-image-input">
177
178 <div class="embed-privacy-image-input-container<?php echo ! empty( $attributes['value'] ) ? ' embed-privacy-hidden' : ''; ?>">
179 <button type="button" class="button button-secondary embed-privacy-image-upload"<?php echo self::get_described_by( $attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>><?php \esc_html_e( 'Upload or choose file', 'embed-privacy' ); ?></button>
180 </div>
181
182 <div class="embed-privacy-image-container<?php echo empty( $attributes['value'] ) ? ' embed-privacy-hidden' : ''; ?>">
183 <?php echo $image; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
184 <button type="button" class="dashicons dashicons-no embed-privacy-icon embed-privacy-remove-image">
185 <span class="screen-reader-text"><?= \esc_attr__( 'Remove image', 'embed-privacy' ); ?></span>
186 </button>
187 </div>
188
189 <?php echo self::get_description( $attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
190
191 <p class="screen-reader-text embed-privacy-image-status" role="status"></p>
192 </td>
193 </tr>
194 <?php
195 }
196
197 /**
198 * Get a text field.
199 *
200 * @param array $attributes Field attributes
201 * @param mixed $current_value Current value
202 */
203 public static function get_text( array $attributes, $current_value ) {
204 \ob_start();
205 ?>
206 <input type="<?php echo \esc_attr( $attributes['type'] ); ?>" name="<?php echo \esc_attr( $attributes['name'] ); ?>" id="<?php echo \esc_attr( $attributes['name'] ); ?>" value="<?php echo \esc_attr( $current_value ); ?>" class="<?php echo \esc_attr( $attributes['classes'] ); ?>"<?php echo self::get_described_by( $attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
207 <?php
208 echo self::get_description( $attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
209 $input = \ob_get_clean();
210
211 if ( $attributes['option_type'] === 'option' ) {
212 // unlike a choice field, a text field has no label wrapping it, and
213 // the settings section provides none either
214 \printf(
215 '<label for="%1$s">%2$s</label> ',
216 \esc_attr( $attributes['name'] ),
217 \esc_html( $attributes['title'] )
218 );
219 echo $input; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
220
221 return;
222 }
223 ?>
224 <tr>
225 <th scope="row">
226 <label for="<?php echo \esc_attr( $attributes['name'] ); ?>"><?php echo \esc_html( $attributes['title'] ); ?></label>
227 </th>
228 <td>
229 <?php echo $input; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
230 </td>
231 </tr>
232 <?php
233 }
234 }
235