PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.9.1
Secure Custom Fields v6.9.1
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
FlexibleContent 2 months ago class-acf-field-accordion.php 2 months ago class-acf-field-button-group.php 2 months ago class-acf-field-checkbox.php 2 days ago class-acf-field-clone.php 2 months ago class-acf-field-color_picker.php 2 months ago class-acf-field-date_picker.php 2 months ago class-acf-field-date_time_picker.php 2 months ago class-acf-field-email.php 2 months ago class-acf-field-file.php 2 months ago class-acf-field-flexible-content.php 1 week ago class-acf-field-gallery.php 3 weeks ago class-acf-field-google-map.php 2 months ago class-acf-field-group.php 2 months ago class-acf-field-icon_picker.php 7 months ago class-acf-field-image.php 2 months ago class-acf-field-link.php 2 months ago class-acf-field-message.php 1 year ago class-acf-field-nav-menu.php 1 week ago class-acf-field-number.php 2 months ago class-acf-field-oembed.php 3 weeks ago class-acf-field-output.php 1 year ago class-acf-field-page_link.php 3 weeks ago class-acf-field-password.php 2 months ago class-acf-field-post_object.php 3 weeks ago class-acf-field-radio.php 2 days ago class-acf-field-range.php 2 months ago class-acf-field-relationship.php 3 weeks ago class-acf-field-repeater.php 3 weeks ago class-acf-field-select.php 2 days ago class-acf-field-separator.php 1 year ago class-acf-field-tab.php 1 year ago class-acf-field-taxonomy.php 3 weeks ago class-acf-field-text.php 3 weeks ago class-acf-field-textarea.php 3 weeks ago class-acf-field-time_picker.php 2 months ago class-acf-field-true_false.php 2 months ago class-acf-field-url.php 3 weeks ago class-acf-field-user.php 3 weeks ago class-acf-field-wysiwyg.php 2 months ago class-acf-field.php 2 months ago class-acf-repeater-table.php 1 year ago index.php 1 year ago
class-acf-field-oembed.php
445 lines
1 <?php
2
3 if ( ! class_exists( 'acf_field_oembed' ) ) :
4 class acf_field_oembed extends acf_field {
5
6 /**
7 *
8 * Oembed width.
9 *
10 * @var int $width
11 */
12 public $width = 0;
13 /**
14 * Oembed height.
15 *
16 * @var int $height
17 */
18 public $height = 0;
19
20 /**
21 * This function will setup the field type data
22 *
23 * @type function
24 * @date 5/03/2014
25 * @since ACF 5.0.0.0.0
26 *
27 * @param n/a
28 * @return n/a
29 */
30 function initialize() {
31
32 // vars
33 $this->name = 'oembed';
34 $this->label = __( 'oEmbed', 'secure-custom-fields' );
35 $this->category = 'content';
36 $this->description = __( 'An interactive component for embedding videos, images, tweets, audio and other content by making use of the native WordPress oEmbed functionality.', 'secure-custom-fields' );
37 $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-oembed.png';
38 $this->doc_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/oembed/';
39 $this->tutorial_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/oembed/oembed-tutorial/';
40 $this->defaults = array(
41 'width' => '',
42 'height' => '',
43 );
44 $this->width = 640;
45 $this->height = 390;
46 $this->supports = array(
47 'escaping_html' => true, // The OEmbed field only produces html safe content from format_value.
48 );
49
50 // extra
51 add_action( 'wp_ajax_acf/fields/oembed/search', array( $this, 'ajax_query' ) );
52 add_action( 'wp_ajax_nopriv_acf/fields/oembed/search', array( $this, 'ajax_query' ) );
53 }
54
55
56 /**
57 * This function will prepare the field for input
58 *
59 * @type function
60 * @date 14/2/17
61 * @since ACF 5.5.8.5.8
62 *
63 * @param $field (array)
64 * @return array
65 */
66 function prepare_field( $field ) {
67
68 // defaults
69 if ( ! $field['width'] ) {
70 $field['width'] = $this->width;
71 }
72 if ( ! $field['height'] ) {
73 $field['height'] = $this->height;
74 }
75
76 // return
77 return $field;
78 }
79
80 /**
81 * Attempts to fetch the HTML for the provided URL using oEmbed.
82 *
83 * @date 24/01/2014
84 * @since ACF 5.0.0.0.0
85 *
86 * @param string $url The URL that should be embedded.
87 * @param integer|string $width Optional maxwidth value passed to the provider URL.
88 * @param integer|string $height Optional maxheight value passed to the provider URL.
89 * @param array $args Optional. Additional arguments merged into the oEmbed request.
90 * @return string|false The embedded HTML on success, false on failure.
91 */
92 public function wp_oembed_get( $url = '', $width = 0, $height = 0, $args = array() ) {
93 $embed = false;
94 $res = array_merge(
95 array(
96 'width' => $width,
97 'height' => $height,
98 ),
99 $args
100 );
101
102 if ( function_exists( 'wp_oembed_get' ) ) {
103 $embed = wp_oembed_get( $url, $res );
104 }
105
106 // try shortcode
107 if ( ! $embed ) {
108 global $wp_embed;
109
110 // WP_Embed::shortcode() otherwise forces discovery on through this filter.
111 $force_discover_off = isset( $args['discover'] ) && false === $args['discover'];
112 if ( $force_discover_off ) {
113 add_filter( 'embed_oembed_discover', '__return_false', PHP_INT_MAX );
114 }
115
116 $embed = $wp_embed->shortcode( $res, $url );
117
118 if ( $force_discover_off ) {
119 remove_filter( 'embed_oembed_discover', '__return_false', PHP_INT_MAX );
120 }
121 }
122
123 return $embed;
124 }
125
126 /**
127 * Returns AJAX results for the oEmbed field.
128 *
129 * @since ACF 5.0.0.0.0
130 *
131 * @return void
132 */
133 public function ajax_query() {
134 $args = acf_request_args(
135 array(
136 'nonce' => '',
137 'field_key' => '',
138 )
139 );
140
141 if ( ! acf_verify_ajax( $args['nonce'], $args['field_key'], true, 'oembed' ) ) {
142 die();
143 }
144
145 wp_send_json( $this->get_ajax_query( $_POST ) );
146 }
147
148 /**
149 * This function will return an array of data formatted for use in a select2 AJAX response
150 *
151 * @type function
152 * @date 15/10/2014
153 * @since ACF 5.0.9.0.9
154 *
155 * @param $options (array)
156 * @return (array)
157 */
158 function get_ajax_query( $args = array() ) {
159
160 // defaults
161 $args = acf_parse_args(
162 $args,
163 array(
164 's' => '',
165 'field_key' => '',
166 )
167 );
168
169 // load field
170 $field = acf_get_field( $args['field_key'] );
171 if ( ! $field ) {
172 return false;
173 }
174
175 // prepare field to correct width and height
176 $field = $this->prepare_field( $field );
177
178 /**
179 * Filters whether URL discovery is permitted on the AJAX oEmbed preview path.
180 *
181 * Discovery is restricted by default to users with the edit_posts capability,
182 * limiting unauthenticated and subscriber-tier callers to WordPress's
183 * registered oEmbed provider allowlist. Saved values and admin save-time
184 * rendering are unaffected.
185 *
186 * @since SCF 6.8.6
187 *
188 * @param bool $allow_discovery Whether discovery is permitted.
189 * @param array $field The oEmbed field array.
190 */
191 $allow_discovery = (bool) apply_filters( 'acf/fields/oembed/allow_discovery', current_user_can( 'edit_posts' ), $field );
192
193 // vars
194 $response = array(
195 'url' => $args['s'],
196 'html' => $this->wp_oembed_get( $args['s'], $field['width'], $field['height'], array( 'discover' => $allow_discovery ) ),
197 );
198
199 // return
200 return $response;
201 }
202
203
204 /**
205 * Renders the oEmbed field.
206 *
207 * @since ACF 3.6 3.6
208 *
209 * @param array $field The field settings array.
210 * @return void
211 */
212 public function render_field( $field ) {
213 $atts = array(
214 'class' => 'acf-oembed',
215 'data-nonce' => wp_create_nonce( 'acf_field_' . $this->name . '_' . $field['key'] ),
216 );
217
218 if ( $field['value'] ) {
219 $atts['class'] .= ' has-value';
220 }
221
222 ?>
223 <div <?php echo acf_esc_attrs( $atts ); ?>>
224
225 <?php
226 acf_hidden_input(
227 array(
228 'class' => 'input-value',
229 'name' => $field['name'],
230 'value' => $field['value'],
231 )
232 );
233 ?>
234
235 <div class="title">
236 <?php
237 acf_text_input(
238 array(
239 'class' => 'input-search',
240 'value' => $field['value'],
241 'placeholder' => __( 'Enter URL', 'secure-custom-fields' ),
242 'autocomplete' => 'off',
243 )
244 );
245 ?>
246 <div class="acf-actions -hover">
247 <a data-name="clear-button" href="#" class="acf-icon -cancel grey"></a>
248 </div>
249 </div>
250
251 <div class="canvas">
252 <div class="canvas-media">
253 <?php
254 if ( $field['value'] ) {
255 echo $this->wp_oembed_get( $field['value'], $field['width'], $field['height'] ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_ombed_get generates HTML safe output.
256 }
257 ?>
258 </div>
259 <i class="acf-icon -picture hide-if-value"></i>
260 </div>
261
262 </div>
263 <?php
264 }
265
266
267 /**
268 * Create extra options for your field. This is rendered when editing a field.
269 * The value of $field['name'] can be used (like bellow) to save extra data to the $field
270 *
271 * @param $field - an array holding all the field's data
272 *
273 * @type action
274 * @since ACF 3.6 3.6
275 * @date 23/01/13
276 */
277 function render_field_settings( $field ) {
278 acf_render_field_setting(
279 $field,
280 array(
281 'label' => __( 'Embed Size', 'secure-custom-fields' ),
282 'type' => 'text',
283 'name' => 'width',
284 'prepend' => __( 'Width', 'secure-custom-fields' ),
285 'append' => 'px',
286 'placeholder' => $this->width,
287 )
288 );
289
290 acf_render_field_setting(
291 $field,
292 array(
293 'label' => __( 'Embed Size', 'secure-custom-fields' ),
294 'type' => 'text',
295 'name' => 'height',
296 'prepend' => __( 'Height', 'secure-custom-fields' ),
297 'append' => 'px',
298 'placeholder' => $this->height,
299 '_append' => 'width',
300 )
301 );
302 }
303
304 /**
305 * This filter is applied to the $value after it is loaded from the db and before it is returned to the template.
306 *
307 * @type filter
308 * @since ACF 3.6 3.6
309 *
310 * @param mixed $value The value which was loaded from the database.
311 * @param mixed $post_id The $post_id from which the value was loaded.
312 * @param array $field The field array holding all the field options.
313 * @return mixed the modified value
314 */
315 public function format_value( $value, $post_id, $field ) {
316 // bail early if no value
317 if ( empty( $value ) ) {
318 return $value;
319 }
320
321 // prepare field to correct width and height
322 $field = $this->prepare_field( $field );
323
324 // get oembed
325 $value = $this->wp_oembed_get( $value, $field['width'], $field['height'] );
326
327 // return
328 return $value;
329 }
330
331 /**
332 * Return the schema array for the REST API.
333 *
334 * @param array $field
335 * @return array
336 */
337 public function get_rest_schema( array $field ) {
338 $schema = parent::get_rest_schema( $field );
339 $schema['format'] = 'uri';
340
341 return $schema;
342 }
343
344 /**
345 * Returns an array of JSON-LD Property output types that are supported by this field type.
346 *
347 * @since 6.8
348 *
349 * @return string[]
350 */
351 public function get_jsonld_output_types(): array {
352 return array( 'VideoObject', 'AudioObject', 'MediaObject' );
353 }
354
355 /**
356 * Formats the field value for JSON-LD output.
357 *
358 * @since 6.8.0
359 *
360 * @param mixed $value The value of the field (URL).
361 * @param integer|string $post_id The ID of the post.
362 * @param array $field The field array.
363 * @return mixed
364 */
365 public function format_value_for_jsonld( $value, $post_id, $field ) {
366 if ( empty( $value ) ) {
367 return null;
368 }
369
370 // Get output format with fallback.
371 $output_format = $field['schema_output_format'] ?? '';
372 if ( empty( $output_format ) ) {
373 $property = $field['schema_property'] ?? '';
374 $output_format = \SCF\AI\GEO\Schema::get_default_output_format( $this->name, $property );
375 }
376
377 // Default to VideoObject if no format determined.
378 if ( empty( $output_format ) ) {
379 $output_format = 'VideoObject';
380 }
381
382 // Get oEmbed data for richer output.
383 $oembed_data = _wp_oembed_get_object()->get_data( $value, array() );
384
385 $result = array(
386 '@type' => $output_format,
387 'url' => $value,
388 );
389
390 if ( $oembed_data ) {
391 // Add name/title.
392 if ( ! empty( $oembed_data->title ) ) {
393 $result['name'] = $oembed_data->title;
394 }
395
396 // Add thumbnail.
397 if ( ! empty( $oembed_data->thumbnail_url ) ) {
398 $result['thumbnailUrl'] = $oembed_data->thumbnail_url;
399 }
400
401 // Add provider information.
402 if ( ! empty( $oembed_data->provider_name ) ) {
403 $result['publisher'] = array(
404 '@type' => 'Organization',
405 'name' => $oembed_data->provider_name,
406 );
407 if ( ! empty( $oembed_data->provider_url ) ) {
408 $result['publisher']['url'] = $oembed_data->provider_url;
409 }
410 }
411
412 // Add dimensions for video.
413 if ( 'VideoObject' === $output_format || 'video' === ( $oembed_data->type ?? '' ) ) {
414 if ( ! empty( $oembed_data->width ) ) {
415 $result['width'] = (int) $oembed_data->width;
416 }
417 if ( ! empty( $oembed_data->height ) ) {
418 $result['height'] = (int) $oembed_data->height;
419 }
420 }
421
422 // Add author if available.
423 if ( ! empty( $oembed_data->author_name ) ) {
424 $author = array(
425 '@type' => 'Person',
426 'name' => $oembed_data->author_name,
427 );
428 if ( ! empty( $oembed_data->author_url ) ) {
429 $author['url'] = $oembed_data->author_url;
430 }
431 $result['author'] = $author;
432 }
433 }
434
435 return $result;
436 }
437 }
438
439
440 // initialize
441 acf_register_field_type( 'acf_field_oembed' );
442 endif; // class_exists check
443
444 ?>
445