PluginProbe
Embed Privacy / 1.10.2
Embed Privacy v1.10.2
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 1.10.2, at inc/admin/class-field.php

197 lines 5.8 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 a choice field (checkbox or radio button).
55 *
56 * @param array $attributes Field attributes
57 * @param mixed $current_value Current value
58 */
59 public static function get_choice( array $attributes, $current_value ) {
60 if ( empty( $attributes['value'] ) ) {
61 $attributes['value'] = 'yes';
62 }
63
64 \ob_start();
65 ?>
66 <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 \esc_html( $attributes['title'] ); ?></label>
67 <?php if ( ! empty( $attributes['description'] ) ) : ?>
68 <p>
69 <?php
70 if ( empty( $attributes['validation'] ) ) {
71 echo \esc_html( $attributes['description'] );
72 }
73 else if ( $attributes['validation'] === 'allow-links' ) {
74 echo \wp_kses( $attributes['description'], [
75 'a' => [
76 'href' => true,
77 'rel' => true,
78 'target' => true,
79 ],
80 ] );
81 }
82 ?>
83 </p>
84 <?php
85 endif;
86 $input = \ob_get_clean();
87
88 if ( $attributes['option_type'] === 'option' ) {
89 echo $input; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
90
91 return;
92 }
93 ?>
94 <tr>
95 <th scope="row"></th>
96 <td>
97 <?php echo $input; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
98 </td>
99 </tr>
100 <?php
101 }
102
103 /**
104 * Get an image field.
105 *
106 * @param int $post_id Post ID
107 * @param array $attributes An array with attributes
108 */
109 public static function get_image( $post_id, array $attributes ) {
110 $attributes = \wp_parse_args( $attributes, [
111 'classes' => '',
112 'description' => '',
113 'name' => '',
114 'single' => true,
115 'title' => '',
116 ] );
117
118 if ( empty( $attributes['name'] ) || empty( $attributes['title'] ) ) {
119 return;
120 }
121
122 $attributes['value'] = (string) \get_post_meta( $post_id, $attributes['name'], $attributes['single'] );
123 $image = \wp_get_attachment_image( (int) $attributes['value'] );
124 ?>
125 <tr>
126 <th scope="row">
127 <label for="<?php echo \esc_attr( $attributes['name'] ); ?>"><?php echo \esc_html( $attributes['title'] ); ?></label>
128 </th>
129 <td class="embed-privacy-image-item">
130 <input type="hidden" name="<?php echo \esc_attr( $attributes['name'] ); ?>" value="<?php echo \esc_attr( $attributes['value'] ); ?>" class="embed-privacy-image-input">
131
132 <div class="embed-privacy-image-input-container<?php echo ! empty( $attributes['value'] ) ? ' embed-privacy-hidden' : ''; ?>">
133 <button type="button" class="button button-secondary embed-privacy-image-upload"><?php \esc_html_e( 'Upload or choose file', 'embed-privacy' ); ?></button>
134 </div>
135
136 <div class="embed-privacy-image-container<?php echo empty( $attributes['value'] ) ? ' embed-privacy-hidden' : ''; ?>">
137 <?php echo $image; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
138 <span class="dashicons dashicons-no embed-privacy-icon embed-privacy-remove-image"></span>
139 </div>
140
141 <?php if ( ! empty( $attributes['description'] ) ) : ?>
142 <p><?php echo \esc_html( $attributes['description'] ); ?></p>
143 <?php endif; ?>
144 </td>
145 </tr>
146 <?php
147 }
148
149 /**
150 * Get a text field.
151 *
152 * @param array $attributes Field attributes
153 * @param mixed $current_value Current value
154 */
155 public static function get_text( array $attributes, $current_value ) {
156 \ob_start();
157 ?>
158 <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'] ); ?>">
159 <?php if ( ! empty( $attributes['description'] ) ) : ?>
160 <p>
161 <?php
162 if ( empty( $attributes['validation'] ) ) {
163 echo \esc_html( $attributes['description'] );
164 }
165 else if ( $attributes['validation'] === 'allow-links' ) {
166 echo \wp_kses( $attributes['description'], [
167 'a' => [
168 'href' => true,
169 'rel' => true,
170 'target' => true,
171 ],
172 ] );
173 }
174 ?>
175 </p>
176 <?php
177 endif;
178 $input = \ob_get_clean();
179
180 if ( $attributes['option_type'] === 'option' ) {
181 echo $input; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
182
183 return;
184 }
185 ?>
186 <tr>
187 <th scope="row">
188 <label for="<?php echo \esc_attr( $attributes['name'] ); ?>"><?php echo \esc_html( $attributes['title'] ); ?></label>
189 </th>
190 <td>
191 <?php echo $input; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
192 </td>
193 </tr>
194 <?php
195 }
196 }
197