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-clone.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-flexible-content.php
1 year ago
class-acf-field-gallery.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-repeater.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
class-acf-repeater-table.php
1 year ago
index.php
1 year ago
class-acf-field-oembed.php
309 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 ACF 5.0.0.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', 'secure-custom-fields' ); |
| 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.', 'secure-custom-fields' ); |
| 25 | $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-oembed.png'; |
| 26 | $this->doc_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/oembed/'; |
| 27 | $this->tutorial_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/oembed/oembed-tutorial/'; |
| 28 | $this->defaults = array( |
| 29 | 'width' => '', |
| 30 | 'height' => '', |
| 31 | ); |
| 32 | $this->width = 640; |
| 33 | $this->height = 390; |
| 34 | $this->supports = array( |
| 35 | 'escaping_html' => true, // The OEmbed field only produces html safe content from format_value. |
| 36 | ); |
| 37 | |
| 38 | // extra |
| 39 | add_action( 'wp_ajax_acf/fields/oembed/search', array( $this, 'ajax_query' ) ); |
| 40 | add_action( 'wp_ajax_nopriv_acf/fields/oembed/search', array( $this, 'ajax_query' ) ); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * This function will prepare the field for input |
| 46 | * |
| 47 | * @type function |
| 48 | * @date 14/2/17 |
| 49 | * @since ACF 5.5.8.5.8 |
| 50 | * |
| 51 | * @param $field (array) |
| 52 | * @return (int) |
| 53 | */ |
| 54 | function prepare_field( $field ) { |
| 55 | |
| 56 | // defaults |
| 57 | if ( ! $field['width'] ) { |
| 58 | $field['width'] = $this->width; |
| 59 | } |
| 60 | if ( ! $field['height'] ) { |
| 61 | $field['height'] = $this->height; |
| 62 | } |
| 63 | |
| 64 | // return |
| 65 | return $field; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Attempts to fetch the HTML for the provided URL using oEmbed. |
| 70 | * |
| 71 | * @date 24/01/2014 |
| 72 | * @since ACF 5.0.0.0.0 |
| 73 | * |
| 74 | * @param string $url The URL that should be embedded. |
| 75 | * @param integer|string $width Optional maxwidth value passed to the provider URL. |
| 76 | * @param integer|string $height Optional maxheight value passed to the provider URL. |
| 77 | * @return string|false The embedded HTML on success, false on failure. |
| 78 | */ |
| 79 | function wp_oembed_get( $url = '', $width = 0, $height = 0 ) { |
| 80 | $embed = false; |
| 81 | $res = array( |
| 82 | 'width' => $width, |
| 83 | 'height' => $height, |
| 84 | ); |
| 85 | |
| 86 | if ( function_exists( 'wp_oembed_get' ) ) { |
| 87 | $embed = wp_oembed_get( $url, $res ); |
| 88 | } |
| 89 | |
| 90 | // try shortcode |
| 91 | if ( ! $embed ) { |
| 92 | global $wp_embed; |
| 93 | $embed = $wp_embed->shortcode( $res, $url ); |
| 94 | } |
| 95 | |
| 96 | return $embed; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns AJAX results for the oEmbed field. |
| 101 | * |
| 102 | * @since ACF 5.0.0.0.0 |
| 103 | * |
| 104 | * @return void |
| 105 | */ |
| 106 | public function ajax_query() { |
| 107 | $args = acf_request_args( |
| 108 | array( |
| 109 | 'nonce' => '', |
| 110 | 'field_key' => '', |
| 111 | ) |
| 112 | ); |
| 113 | |
| 114 | if ( ! acf_verify_ajax( $args['nonce'], $args['field_key'] ) ) { |
| 115 | die(); |
| 116 | } |
| 117 | |
| 118 | wp_send_json( $this->get_ajax_query( $_POST ) ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * This function will return an array of data formatted for use in a select2 AJAX response |
| 123 | * |
| 124 | * @type function |
| 125 | * @date 15/10/2014 |
| 126 | * @since ACF 5.0.9.0.9 |
| 127 | * |
| 128 | * @param $options (array) |
| 129 | * @return (array) |
| 130 | */ |
| 131 | function get_ajax_query( $args = array() ) { |
| 132 | |
| 133 | // defaults |
| 134 | $args = acf_parse_args( |
| 135 | $args, |
| 136 | array( |
| 137 | 's' => '', |
| 138 | 'field_key' => '', |
| 139 | ) |
| 140 | ); |
| 141 | |
| 142 | // load field |
| 143 | $field = acf_get_field( $args['field_key'] ); |
| 144 | if ( ! $field ) { |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | // prepare field to correct width and height |
| 149 | $field = $this->prepare_field( $field ); |
| 150 | |
| 151 | // vars |
| 152 | $response = array( |
| 153 | 'url' => $args['s'], |
| 154 | 'html' => $this->wp_oembed_get( $args['s'], $field['width'], $field['height'] ), |
| 155 | ); |
| 156 | |
| 157 | // return |
| 158 | return $response; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /** |
| 163 | * Renders the oEmbed field. |
| 164 | * |
| 165 | * @since ACF 3.6 3.6 |
| 166 | * |
| 167 | * @param array $field The field settings array. |
| 168 | * @return void |
| 169 | */ |
| 170 | public function render_field( $field ) { |
| 171 | $atts = array( |
| 172 | 'class' => 'acf-oembed', |
| 173 | 'data-nonce' => wp_create_nonce( $field['key'] ), |
| 174 | ); |
| 175 | |
| 176 | if ( $field['value'] ) { |
| 177 | $atts['class'] .= ' has-value'; |
| 178 | } |
| 179 | |
| 180 | ?> |
| 181 | <div <?php echo acf_esc_attrs( $atts ); ?>> |
| 182 | |
| 183 | <?php |
| 184 | acf_hidden_input( |
| 185 | array( |
| 186 | 'class' => 'input-value', |
| 187 | 'name' => $field['name'], |
| 188 | 'value' => $field['value'], |
| 189 | ) |
| 190 | ); |
| 191 | ?> |
| 192 | |
| 193 | <div class="title"> |
| 194 | <?php |
| 195 | acf_text_input( |
| 196 | array( |
| 197 | 'class' => 'input-search', |
| 198 | 'value' => $field['value'], |
| 199 | 'placeholder' => __( 'Enter URL', 'secure-custom-fields' ), |
| 200 | 'autocomplete' => 'off', |
| 201 | ) |
| 202 | ); |
| 203 | ?> |
| 204 | <div class="acf-actions -hover"> |
| 205 | <a data-name="clear-button" href="#" class="acf-icon -cancel grey"></a> |
| 206 | </div> |
| 207 | </div> |
| 208 | |
| 209 | <div class="canvas"> |
| 210 | <div class="canvas-media"> |
| 211 | <?php |
| 212 | if ( $field['value'] ) { |
| 213 | echo $this->wp_oembed_get( $field['value'], $field['width'], $field['height'] ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_ombed_get generates HTML safe output. |
| 214 | } |
| 215 | ?> |
| 216 | </div> |
| 217 | <i class="acf-icon -picture hide-if-value"></i> |
| 218 | </div> |
| 219 | |
| 220 | </div> |
| 221 | <?php |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /** |
| 226 | * Create extra options for your field. This is rendered when editing a field. |
| 227 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 228 | * |
| 229 | * @param $field - an array holding all the field's data |
| 230 | * |
| 231 | * @type action |
| 232 | * @since ACF 3.6 3.6 |
| 233 | * @date 23/01/13 |
| 234 | */ |
| 235 | function render_field_settings( $field ) { |
| 236 | acf_render_field_setting( |
| 237 | $field, |
| 238 | array( |
| 239 | 'label' => __( 'Embed Size', 'secure-custom-fields' ), |
| 240 | 'type' => 'text', |
| 241 | 'name' => 'width', |
| 242 | 'prepend' => __( 'Width', 'secure-custom-fields' ), |
| 243 | 'append' => 'px', |
| 244 | 'placeholder' => $this->width, |
| 245 | ) |
| 246 | ); |
| 247 | |
| 248 | acf_render_field_setting( |
| 249 | $field, |
| 250 | array( |
| 251 | 'label' => __( 'Embed Size', 'secure-custom-fields' ), |
| 252 | 'type' => 'text', |
| 253 | 'name' => 'height', |
| 254 | 'prepend' => __( 'Height', 'secure-custom-fields' ), |
| 255 | 'append' => 'px', |
| 256 | 'placeholder' => $this->height, |
| 257 | '_append' => 'width', |
| 258 | ) |
| 259 | ); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * This filter is appied to the $value after it is loaded from the db and before it is returned to the template. |
| 264 | * |
| 265 | * @type filter |
| 266 | * @since ACF 3.6 3.6 |
| 267 | * |
| 268 | * @param mixed $value The value which was loaded from the database. |
| 269 | * @param mixed $post_id The $post_id from which the value was loaded. |
| 270 | * @param array $field The field array holding all the field options. |
| 271 | * @return mixed the modified value |
| 272 | */ |
| 273 | public function format_value( $value, $post_id, $field ) { |
| 274 | // bail early if no value |
| 275 | if ( empty( $value ) ) { |
| 276 | return $value; |
| 277 | } |
| 278 | |
| 279 | // prepare field to correct width and height |
| 280 | $field = $this->prepare_field( $field ); |
| 281 | |
| 282 | // get oembed |
| 283 | $value = $this->wp_oembed_get( $value, $field['width'], $field['height'] ); |
| 284 | |
| 285 | // return |
| 286 | return $value; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Return the schema array for the REST API. |
| 291 | * |
| 292 | * @param array $field |
| 293 | * @return array |
| 294 | */ |
| 295 | public function get_rest_schema( array $field ) { |
| 296 | $schema = parent::get_rest_schema( $field ); |
| 297 | $schema['format'] = 'uri'; |
| 298 | |
| 299 | return $schema; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | |
| 304 | // initialize |
| 305 | acf_register_field_type( 'acf_field_oembed' ); |
| 306 | endif; // class_exists check |
| 307 | |
| 308 | ?> |
| 309 |