name = 'oembed'; $this->label = __( 'oEmbed', 'acf' ); $this->category = 'content'; $this->description = __( 'An interactive component for embedding videos, images, tweets, audio and other content by making use of the native WordPress oEmbed functionality.', 'acf' ); $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-oembed.png'; $this->doc_url = acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/oembed/', 'docs', 'field-type-selection' ); $this->defaults = array( 'width' => '', 'height' => '', ); $this->width = 640; $this->height = 390; $this->supports = array( 'escaping_html' => true, // The OEmbed field only produces html safe content from format_value. ); // extra add_action( 'wp_ajax_acf/fields/oembed/search', array( $this, 'ajax_query' ) ); add_action( 'wp_ajax_nopriv_acf/fields/oembed/search', array( $this, 'ajax_query' ) ); } /** * This function will prepare the field for input * * @type function * @date 14/2/17 * @since 5.5.8 * * @param $field (array) * @return (int) */ function prepare_field( $field ) { // defaults if ( ! $field['width'] ) { $field['width'] = $this->width; } if ( ! $field['height'] ) { $field['height'] = $this->height; } // return return $field; } /** * Attempts to fetch the HTML for the provided URL using oEmbed. * * @date 24/01/2014 * @since 5.0.0 * * @param string $url The URL that should be embedded. * @param integer|string $width Optional maxwidth value passed to the provider URL. * @param integer|string $height Optional maxheight value passed to the provider URL. * @param array $args Optional. Additional arguments merged into the oEmbed request (e.g. 'discover'). * @return string|false The embedded HTML on success, false on failure. */ function wp_oembed_get( $url = '', $width = 0, $height = 0, $args = array() ) { $embed = false; $res = array_merge( array( 'width' => $width, 'height' => $height, ), $args ); if ( function_exists( 'wp_oembed_get' ) ) { $embed = wp_oembed_get( $url, $res ); } // try shortcode if ( ! $embed ) { global $wp_embed; /** * Mirror our discover restriction onto WP_Embed::shortcode(), * which otherwise forces discover=true via embed_oembed_discover. */ $force_discover_off = isset( $args['discover'] ) && false === $args['discover']; if ( $force_discover_off ) { add_filter( 'embed_oembed_discover', '__return_false', PHP_INT_MAX ); } $embed = $wp_embed->shortcode( $res, $url ); if ( $force_discover_off ) { remove_filter( 'embed_oembed_discover', '__return_false', PHP_INT_MAX ); } } return $embed; } /** * Returns AJAX results for the oEmbed field. * * @since 5.0.0 * * @return void */ public function ajax_query() { $args = acf_request_args( array( 'nonce' => '', 'field_key' => '', ) ); if ( ! acf_verify_ajax( $args['nonce'], $args['field_key'], true, 'oembed' ) ) { die(); } wp_send_json( $this->get_ajax_query( $_POST ) ); } /** * This function will return an array of data formatted for use in a select2 AJAX response * * @type function * @date 15/10/2014 * @since 5.0.9 * * @param $options (array) * @return (array) */ function get_ajax_query( $args = array() ) { // defaults $args = acf_parse_args( $args, array( 's' => '', 'field_key' => '', ) ); // load field $field = acf_get_field( $args['field_key'] ); if ( ! $field ) { return false; } // prepare field to correct width and height $field = $this->prepare_field( $field ); /** * Filters whether URL discovery is permitted on the AJAX oEmbed preview path. * * Discovery is restricted by default to users with the edit_posts capability, * limiting unauthenticated and subscriber-tier callers to WordPress's * registered oEmbed provider allowlist. Saved values and admin save-time * rendering are unaffected. * * @since 6.8.3 * * @param bool $allow_discovery Whether discovery is permitted. Default: true for users with edit_posts, false otherwise. * @param array $field The oEmbed field array. */ $allow_discovery = (bool) apply_filters( 'acf/fields/oembed/allow_discovery', current_user_can( 'edit_posts' ), $field ); // vars $response = array( 'url' => $args['s'], 'html' => $this->wp_oembed_get( $args['s'], $field['width'], $field['height'], array( 'discover' => $allow_discovery ) ), ); // return return $response; } /** * Renders the oEmbed field. * * @since 3.6 * * @param array $field The field settings array. * @return void */ public function render_field( $field ) { $atts = array( 'class' => 'acf-oembed', 'data-nonce' => wp_create_nonce( 'acf_field_' . $this->name . '_' . $field['key'] ), ); if ( $field['value'] ) { $atts['class'] .= ' has-value'; } ?>