PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.8.6
Secure Custom Fields v6.8.6
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
FlexibleContent 4 months ago class-acf-field-accordion.php 4 months ago class-acf-field-button-group.php 4 months ago class-acf-field-checkbox.php 4 months ago class-acf-field-clone.php 4 months ago class-acf-field-color_picker.php 4 months ago class-acf-field-date_picker.php 4 months ago class-acf-field-date_time_picker.php 4 months ago class-acf-field-email.php 4 months ago class-acf-field-file.php 4 months ago class-acf-field-flexible-content.php 4 months ago class-acf-field-gallery.php 4 months ago class-acf-field-google-map.php 4 months ago class-acf-field-group.php 4 months ago class-acf-field-icon_picker.php 9 months ago class-acf-field-image.php 4 months ago class-acf-field-link.php 4 months ago class-acf-field-message.php 1 year ago class-acf-field-nav-menu.php 1 year ago class-acf-field-number.php 4 months ago class-acf-field-oembed.php 3 months ago class-acf-field-output.php 1 year ago class-acf-field-page_link.php 4 months ago class-acf-field-password.php 4 months ago class-acf-field-post_object.php 4 months ago class-acf-field-radio.php 4 months ago class-acf-field-range.php 4 months ago class-acf-field-relationship.php 4 months ago class-acf-field-repeater.php 4 months ago class-acf-field-select.php 4 months ago class-acf-field-separator.php 1 year ago class-acf-field-tab.php 1 year ago class-acf-field-taxonomy.php 4 months ago class-acf-field-text.php 4 months ago class-acf-field-textarea.php 4 months ago class-acf-field-time_picker.php 4 months ago class-acf-field-true_false.php 4 months ago class-acf-field-url.php 4 months ago class-acf-field-user.php 4 months ago class-acf-field-wysiwyg.php 4 months ago class-acf-field.php 4 months ago class-acf-repeater-table.php 1 year ago index.php 1 year ago
class-acf-field-oembed.php
422 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 (int)
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 * @return string|false The embedded HTML on success, false on failure.
90 */
91 function wp_oembed_get( $url = '', $width = 0, $height = 0 ) {
92 $embed = false;
93 $res = array(
94 'width' => $width,
95 'height' => $height,
96 );
97
98 if ( function_exists( 'wp_oembed_get' ) ) {
99 $embed = wp_oembed_get( $url, $res );
100 }
101
102 // try shortcode
103 if ( ! $embed ) {
104 global $wp_embed;
105 $embed = $wp_embed->shortcode( $res, $url );
106 }
107
108 return $embed;
109 }
110
111 /**
112 * Returns AJAX results for the oEmbed field.
113 *
114 * @since ACF 5.0.0.0.0
115 *
116 * @return void
117 */
118 public function ajax_query() {
119 $args = acf_request_args(
120 array(
121 'nonce' => '',
122 'field_key' => '',
123 )
124 );
125
126 if ( ! acf_verify_ajax( $args['nonce'], $args['field_key'], true ) || ! current_user_can( 'edit_posts' ) ) {
127 if ( ! is_user_logged_in() ) {
128 _doing_it_wrong(
129 __METHOD__,
130 esc_html__( 'The oEmbed AJAX search endpoint now requires an authenticated user with the edit_posts capability. Unauthenticated access via wp_ajax_nopriv_acf/fields/oembed/search is deprecated and will be removed in a future release.', 'secure-custom-fields' ),
131 '6.8.5'
132 );
133 }
134 wp_send_json_error();
135 }
136
137 wp_send_json( $this->get_ajax_query( $_POST ) );
138 }
139
140 /**
141 * This function will return an array of data formatted for use in a select2 AJAX response
142 *
143 * @type function
144 * @date 15/10/2014
145 * @since ACF 5.0.9.0.9
146 *
147 * @param $options (array)
148 * @return (array)
149 */
150 function get_ajax_query( $args = array() ) {
151
152 // defaults
153 $args = acf_parse_args(
154 $args,
155 array(
156 's' => '',
157 'field_key' => '',
158 )
159 );
160
161 // load field
162 $field = acf_get_field( $args['field_key'] );
163 if ( ! $field ) {
164 return false;
165 }
166
167 // prepare field to correct width and height
168 $field = $this->prepare_field( $field );
169
170 // vars
171 $response = array(
172 'url' => $args['s'],
173 'html' => $this->wp_oembed_get( $args['s'], $field['width'], $field['height'] ),
174 );
175
176 // return
177 return $response;
178 }
179
180
181 /**
182 * Renders the oEmbed field.
183 *
184 * @since ACF 3.6 3.6
185 *
186 * @param array $field The field settings array.
187 * @return void
188 */
189 public function render_field( $field ) {
190 $atts = array(
191 'class' => 'acf-oembed',
192 'data-nonce' => wp_create_nonce( 'acf_field_' . $this->name . '_' . $field['key'] ),
193 );
194
195 if ( $field['value'] ) {
196 $atts['class'] .= ' has-value';
197 }
198
199 ?>
200 <div <?php echo acf_esc_attrs( $atts ); ?>>
201
202 <?php
203 acf_hidden_input(
204 array(
205 'class' => 'input-value',
206 'name' => $field['name'],
207 'value' => $field['value'],
208 )
209 );
210 ?>
211
212 <div class="title">
213 <?php
214 acf_text_input(
215 array(
216 'class' => 'input-search',
217 'value' => $field['value'],
218 'placeholder' => __( 'Enter URL', 'secure-custom-fields' ),
219 'autocomplete' => 'off',
220 )
221 );
222 ?>
223 <div class="acf-actions -hover">
224 <a data-name="clear-button" href="#" class="acf-icon -cancel grey"></a>
225 </div>
226 </div>
227
228 <div class="canvas">
229 <div class="canvas-media">
230 <?php
231 if ( $field['value'] ) {
232 echo $this->wp_oembed_get( $field['value'], $field['width'], $field['height'] ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_ombed_get generates HTML safe output.
233 }
234 ?>
235 </div>
236 <i class="acf-icon -picture hide-if-value"></i>
237 </div>
238
239 </div>
240 <?php
241 }
242
243
244 /**
245 * Create extra options for your field. This is rendered when editing a field.
246 * The value of $field['name'] can be used (like bellow) to save extra data to the $field
247 *
248 * @param $field - an array holding all the field's data
249 *
250 * @type action
251 * @since ACF 3.6 3.6
252 * @date 23/01/13
253 */
254 function render_field_settings( $field ) {
255 acf_render_field_setting(
256 $field,
257 array(
258 'label' => __( 'Embed Size', 'secure-custom-fields' ),
259 'type' => 'text',
260 'name' => 'width',
261 'prepend' => __( 'Width', 'secure-custom-fields' ),
262 'append' => 'px',
263 'placeholder' => $this->width,
264 )
265 );
266
267 acf_render_field_setting(
268 $field,
269 array(
270 'label' => __( 'Embed Size', 'secure-custom-fields' ),
271 'type' => 'text',
272 'name' => 'height',
273 'prepend' => __( 'Height', 'secure-custom-fields' ),
274 'append' => 'px',
275 'placeholder' => $this->height,
276 '_append' => 'width',
277 )
278 );
279 }
280
281 /**
282 * This filter is applied to the $value after it is loaded from the db and before it is returned to the template.
283 *
284 * @type filter
285 * @since ACF 3.6 3.6
286 *
287 * @param mixed $value The value which was loaded from the database.
288 * @param mixed $post_id The $post_id from which the value was loaded.
289 * @param array $field The field array holding all the field options.
290 * @return mixed the modified value
291 */
292 public function format_value( $value, $post_id, $field ) {
293 // bail early if no value
294 if ( empty( $value ) ) {
295 return $value;
296 }
297
298 // prepare field to correct width and height
299 $field = $this->prepare_field( $field );
300
301 // get oembed
302 $value = $this->wp_oembed_get( $value, $field['width'], $field['height'] );
303
304 // return
305 return $value;
306 }
307
308 /**
309 * Return the schema array for the REST API.
310 *
311 * @param array $field
312 * @return array
313 */
314 public function get_rest_schema( array $field ) {
315 $schema = parent::get_rest_schema( $field );
316 $schema['format'] = 'uri';
317
318 return $schema;
319 }
320
321 /**
322 * Returns an array of JSON-LD Property output types that are supported by this field type.
323 *
324 * @since 6.8
325 *
326 * @return string[]
327 */
328 public function get_jsonld_output_types(): array {
329 return array( 'VideoObject', 'AudioObject', 'MediaObject' );
330 }
331
332 /**
333 * Formats the field value for JSON-LD output.
334 *
335 * @since 6.8.0
336 *
337 * @param mixed $value The value of the field (URL).
338 * @param integer|string $post_id The ID of the post.
339 * @param array $field The field array.
340 * @return mixed
341 */
342 public function format_value_for_jsonld( $value, $post_id, $field ) {
343 if ( empty( $value ) ) {
344 return null;
345 }
346
347 // Get output format with fallback.
348 $output_format = $field['schema_output_format'] ?? '';
349 if ( empty( $output_format ) ) {
350 $property = $field['schema_property'] ?? '';
351 $output_format = \SCF\AI\GEO\Schema::get_default_output_format( $this->name, $property );
352 }
353
354 // Default to VideoObject if no format determined.
355 if ( empty( $output_format ) ) {
356 $output_format = 'VideoObject';
357 }
358
359 // Get oEmbed data for richer output.
360 $oembed_data = _wp_oembed_get_object()->get_data( $value, array() );
361
362 $result = array(
363 '@type' => $output_format,
364 'url' => $value,
365 );
366
367 if ( $oembed_data ) {
368 // Add name/title.
369 if ( ! empty( $oembed_data->title ) ) {
370 $result['name'] = $oembed_data->title;
371 }
372
373 // Add thumbnail.
374 if ( ! empty( $oembed_data->thumbnail_url ) ) {
375 $result['thumbnailUrl'] = $oembed_data->thumbnail_url;
376 }
377
378 // Add provider information.
379 if ( ! empty( $oembed_data->provider_name ) ) {
380 $result['publisher'] = array(
381 '@type' => 'Organization',
382 'name' => $oembed_data->provider_name,
383 );
384 if ( ! empty( $oembed_data->provider_url ) ) {
385 $result['publisher']['url'] = $oembed_data->provider_url;
386 }
387 }
388
389 // Add dimensions for video.
390 if ( 'VideoObject' === $output_format || 'video' === ( $oembed_data->type ?? '' ) ) {
391 if ( ! empty( $oembed_data->width ) ) {
392 $result['width'] = (int) $oembed_data->width;
393 }
394 if ( ! empty( $oembed_data->height ) ) {
395 $result['height'] = (int) $oembed_data->height;
396 }
397 }
398
399 // Add author if available.
400 if ( ! empty( $oembed_data->author_name ) ) {
401 $author = array(
402 '@type' => 'Person',
403 'name' => $oembed_data->author_name,
404 );
405 if ( ! empty( $oembed_data->author_url ) ) {
406 $author['url'] = $oembed_data->author_url;
407 }
408 $result['author'] = $author;
409 }
410 }
411
412 return $result;
413 }
414 }
415
416
417 // initialize
418 acf_register_field_type( 'acf_field_oembed' );
419 endif; // class_exists check
420
421 ?>
422