PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.0-beta2
Secure Custom Fields v6.4.0-beta2
6.9.5 6.9.4 6.9.3 6.9.2 6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / fields / class-acf-field-oembed.php
secure-custom-fields / includes / fields Last commit date
class-acf-field-accordion.php 1 year ago class-acf-field-button-group.php 1 year ago class-acf-field-checkbox.php 1 year ago class-acf-field-color_picker.php 1 year ago class-acf-field-date_picker.php 1 year ago class-acf-field-date_time_picker.php 1 year ago class-acf-field-email.php 1 year ago class-acf-field-file.php 1 year ago class-acf-field-google-map.php 1 year ago class-acf-field-group.php 1 year ago class-acf-field-icon_picker.php 1 year ago class-acf-field-image.php 1 year ago class-acf-field-link.php 1 year ago class-acf-field-message.php 1 year ago class-acf-field-number.php 1 year ago class-acf-field-oembed.php 1 year ago class-acf-field-output.php 1 year ago class-acf-field-page_link.php 1 year ago class-acf-field-password.php 1 year ago class-acf-field-post_object.php 1 year ago class-acf-field-radio.php 1 year ago class-acf-field-range.php 1 year ago class-acf-field-relationship.php 1 year ago class-acf-field-select.php 1 year ago class-acf-field-separator.php 1 year ago class-acf-field-tab.php 1 year ago class-acf-field-taxonomy.php 1 year ago class-acf-field-text.php 1 year ago class-acf-field-textarea.php 1 year ago class-acf-field-time_picker.php 1 year ago class-acf-field-true_false.php 1 year ago class-acf-field-url.php 1 year ago class-acf-field-user.php 1 year ago class-acf-field-wysiwyg.php 1 year ago class-acf-field.php 1 year ago index.php 1 year ago
class-acf-field-oembed.php
308 lines
1 <?php
2
3 if ( ! class_exists( 'acf_field_oembed' ) ) :
4 #[AllowDynamicProperties]
5 class acf_field_oembed extends acf_field {
6
7
8 /**
9 * This function will setup the field type data
10 *
11 * @type function
12 * @date 5/03/2014
13 * @since 5.0.0
14 *
15 * @param n/a
16 * @return n/a
17 */
18 function initialize() {
19
20 // vars
21 $this->name = 'oembed';
22 $this->label = __( 'oEmbed', 'acf' );
23 $this->category = 'content';
24 $this->description = __( 'An interactive component for embedding videos, images, tweets, audio and other content by making use of the native WordPress oEmbed functionality.', 'acf' );
25 $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-oembed.png';
26 $this->doc_url = 'https://www.advancedcustomfields.com/resources/oembed/';
27 $this->defaults = array(
28 'width' => '',
29 'height' => '',
30 );
31 $this->width = 640;
32 $this->height = 390;
33 $this->supports = array(
34 'escaping_html' => true, // The OEmbed field only produces html safe content from format_value.
35 );
36
37 // extra
38 add_action( 'wp_ajax_acf/fields/oembed/search', array( $this, 'ajax_query' ) );
39 add_action( 'wp_ajax_nopriv_acf/fields/oembed/search', array( $this, 'ajax_query' ) );
40 }
41
42
43 /**
44 * This function will prepare the field for input
45 *
46 * @type function
47 * @date 14/2/17
48 * @since 5.5.8
49 *
50 * @param $field (array)
51 * @return (int)
52 */
53 function prepare_field( $field ) {
54
55 // defaults
56 if ( ! $field['width'] ) {
57 $field['width'] = $this->width;
58 }
59 if ( ! $field['height'] ) {
60 $field['height'] = $this->height;
61 }
62
63 // return
64 return $field;
65 }
66
67 /**
68 * Attempts to fetch the HTML for the provided URL using oEmbed.
69 *
70 * @date 24/01/2014
71 * @since 5.0.0
72 *
73 * @param string $url The URL that should be embedded.
74 * @param integer|string $width Optional maxwidth value passed to the provider URL.
75 * @param integer|string $height Optional maxheight value passed to the provider URL.
76 * @return string|false The embedded HTML on success, false on failure.
77 */
78 function wp_oembed_get( $url = '', $width = 0, $height = 0 ) {
79 $embed = false;
80 $res = array(
81 'width' => $width,
82 'height' => $height,
83 );
84
85 if ( function_exists( 'wp_oembed_get' ) ) {
86 $embed = wp_oembed_get( $url, $res );
87 }
88
89 // try shortcode
90 if ( ! $embed ) {
91 global $wp_embed;
92 $embed = $wp_embed->shortcode( $res, $url );
93 }
94
95 return $embed;
96 }
97
98 /**
99 * Returns AJAX results for the oEmbed field.
100 *
101 * @since 5.0.0
102 *
103 * @return void
104 */
105 public function ajax_query() {
106 $args = acf_request_args(
107 array(
108 'nonce' => '',
109 'field_key' => '',
110 )
111 );
112
113 if ( ! acf_verify_ajax( $args['nonce'], $args['field_key'] ) ) {
114 die();
115 }
116
117 wp_send_json( $this->get_ajax_query( $_POST ) );
118 }
119
120 /**
121 * This function will return an array of data formatted for use in a select2 AJAX response
122 *
123 * @type function
124 * @date 15/10/2014
125 * @since 5.0.9
126 *
127 * @param $options (array)
128 * @return (array)
129 */
130 function get_ajax_query( $args = array() ) {
131
132 // defaults
133 $args = acf_parse_args(
134 $args,
135 array(
136 's' => '',
137 'field_key' => '',
138 )
139 );
140
141 // load field
142 $field = acf_get_field( $args['field_key'] );
143 if ( ! $field ) {
144 return false;
145 }
146
147 // prepare field to correct width and height
148 $field = $this->prepare_field( $field );
149
150 // vars
151 $response = array(
152 'url' => $args['s'],
153 'html' => $this->wp_oembed_get( $args['s'], $field['width'], $field['height'] ),
154 );
155
156 // return
157 return $response;
158 }
159
160
161 /**
162 * Renders the oEmbed field.
163 *
164 * @since 3.6
165 *
166 * @param array $field The field settings array.
167 * @return void
168 */
169 public function render_field( $field ) {
170 $atts = array(
171 'class' => 'acf-oembed',
172 'data-nonce' => wp_create_nonce( $field['key'] ),
173 );
174
175 if ( $field['value'] ) {
176 $atts['class'] .= ' has-value';
177 }
178
179 ?>
180 <div <?php echo acf_esc_attrs( $atts ); ?>>
181
182 <?php
183 acf_hidden_input(
184 array(
185 'class' => 'input-value',
186 'name' => $field['name'],
187 'value' => $field['value'],
188 )
189 );
190 ?>
191
192 <div class="title">
193 <?php
194 acf_text_input(
195 array(
196 'class' => 'input-search',
197 'value' => $field['value'],
198 'placeholder' => __( 'Enter URL', 'acf' ),
199 'autocomplete' => 'off',
200 )
201 );
202 ?>
203 <div class="acf-actions -hover">
204 <a data-name="clear-button" href="#" class="acf-icon -cancel grey"></a>
205 </div>
206 </div>
207
208 <div class="canvas">
209 <div class="canvas-media">
210 <?php
211 if ( $field['value'] ) {
212 echo $this->wp_oembed_get( $field['value'], $field['width'], $field['height'] ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_ombed_get generates HTML safe output.
213 }
214 ?>
215 </div>
216 <i class="acf-icon -picture hide-if-value"></i>
217 </div>
218
219 </div>
220 <?php
221 }
222
223
224 /**
225 * Create extra options for your field. This is rendered when editing a field.
226 * The value of $field['name'] can be used (like bellow) to save extra data to the $field
227 *
228 * @param $field - an array holding all the field's data
229 *
230 * @type action
231 * @since 3.6
232 * @date 23/01/13
233 */
234 function render_field_settings( $field ) {
235 acf_render_field_setting(
236 $field,
237 array(
238 'label' => __( 'Embed Size', 'acf' ),
239 'type' => 'text',
240 'name' => 'width',
241 'prepend' => __( 'Width', 'acf' ),
242 'append' => 'px',
243 'placeholder' => $this->width,
244 )
245 );
246
247 acf_render_field_setting(
248 $field,
249 array(
250 'label' => __( 'Embed Size', 'acf' ),
251 'type' => 'text',
252 'name' => 'height',
253 'prepend' => __( 'Height', 'acf' ),
254 'append' => 'px',
255 'placeholder' => $this->height,
256 '_append' => 'width',
257 )
258 );
259 }
260
261 /**
262 * This filter is appied to the $value after it is loaded from the db and before it is returned to the template.
263 *
264 * @type filter
265 * @since 3.6
266 *
267 * @param mixed $value The value which was loaded from the database.
268 * @param mixed $post_id The $post_id from which the value was loaded.
269 * @param array $field The field array holding all the field options.
270 * @return mixed the modified value
271 */
272 public function format_value( $value, $post_id, $field ) {
273 // bail early if no value
274 if ( empty( $value ) ) {
275 return $value;
276 }
277
278 // prepare field to correct width and height
279 $field = $this->prepare_field( $field );
280
281 // get oembed
282 $value = $this->wp_oembed_get( $value, $field['width'], $field['height'] );
283
284 // return
285 return $value;
286 }
287
288 /**
289 * Return the schema array for the REST API.
290 *
291 * @param array $field
292 * @return array
293 */
294 public function get_rest_schema( array $field ) {
295 $schema = parent::get_rest_schema( $field );
296 $schema['format'] = 'uri';
297
298 return $schema;
299 }
300 }
301
302
303 // initialize
304 acf_register_field_type( 'acf_field_oembed' );
305 endif; // class_exists check
306
307 ?>
308