class-acf-field-oembed.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. if ( ! class_exists( 'acf_field_oembed' ) ) :
  3. class acf_field_oembed extends acf_field {
  4. /*
  5. * __construct
  6. *
  7. * This function will setup the field type data
  8. *
  9. * @type function
  10. * @date 5/03/2014
  11. * @since 5.0.0
  12. *
  13. * @param n/a
  14. * @return n/a
  15. */
  16. function initialize() {
  17. // vars
  18. $this->name = 'oembed';
  19. $this->label = __( 'oEmbed', 'acf' );
  20. $this->category = 'content';
  21. $this->defaults = array(
  22. 'width' => '',
  23. 'height' => '',
  24. );
  25. $this->width = 640;
  26. $this->height = 390;
  27. // extra
  28. add_action( 'wp_ajax_acf/fields/oembed/search', array( $this, 'ajax_query' ) );
  29. add_action( 'wp_ajax_nopriv_acf/fields/oembed/search', array( $this, 'ajax_query' ) );
  30. }
  31. /*
  32. * prepare_field
  33. *
  34. * This function will prepare the field for input
  35. *
  36. * @type function
  37. * @date 14/2/17
  38. * @since 5.5.8
  39. *
  40. * @param $field (array)
  41. * @return (int)
  42. */
  43. function prepare_field( $field ) {
  44. // defaults
  45. if ( ! $field['width'] ) {
  46. $field['width'] = $this->width;
  47. }
  48. if ( ! $field['height'] ) {
  49. $field['height'] = $this->height;
  50. }
  51. // return
  52. return $field;
  53. }
  54. /**
  55. * Attempts to fetch the HTML for the provided URL using oEmbed.
  56. *
  57. * @date 24/01/2014
  58. * @since 5.0.0
  59. *
  60. * @param string $url The URL that should be embedded.
  61. * @param int|string $width Optional maxwidth value passed to the provider URL.
  62. * @param int|string $height Optional maxheight value passed to the provider URL.
  63. * @return string|false The embedded HTML on success, false on failure.
  64. */
  65. function wp_oembed_get( $url = '', $width = 0, $height = 0 ) {
  66. $embed = false;
  67. $res = array(
  68. 'width' => $width,
  69. 'height' => $height,
  70. );
  71. if ( function_exists( 'wp_oembed_get' ) ) {
  72. $embed = wp_oembed_get( $url, $res );
  73. }
  74. // try shortcode
  75. if ( ! $embed ) {
  76. global $wp_embed;
  77. $embed = $wp_embed->shortcode( $res, $url );
  78. }
  79. return $embed;
  80. }
  81. /*
  82. * ajax_query
  83. *
  84. * description
  85. *
  86. * @type function
  87. * @date 24/10/13
  88. * @since 5.0.0
  89. *
  90. * @param $post_id (int)
  91. * @return $post_id (int)
  92. */
  93. function ajax_query() {
  94. // validate
  95. if ( ! acf_verify_ajax() ) {
  96. die();
  97. }
  98. // get choices
  99. $response = $this->get_ajax_query( $_POST );
  100. // return
  101. wp_send_json( $response );
  102. }
  103. /*
  104. * get_ajax_query
  105. *
  106. * This function will return an array of data formatted for use in a select2 AJAX response
  107. *
  108. * @type function
  109. * @date 15/10/2014
  110. * @since 5.0.9
  111. *
  112. * @param $options (array)
  113. * @return (array)
  114. */
  115. function get_ajax_query( $args = array() ) {
  116. // defaults
  117. $args = acf_parse_args(
  118. $args,
  119. array(
  120. 's' => '',
  121. 'field_key' => '',
  122. )
  123. );
  124. // load field
  125. $field = acf_get_field( $args['field_key'] );
  126. if ( ! $field ) {
  127. return false;
  128. }
  129. // prepare field to correct width and height
  130. $field = $this->prepare_field( $field );
  131. // vars
  132. $response = array(
  133. 'url' => $args['s'],
  134. 'html' => $this->wp_oembed_get( $args['s'], $field['width'], $field['height'] ),
  135. );
  136. // return
  137. return $response;
  138. }
  139. /*
  140. * render_field()
  141. *
  142. * Create the HTML interface for your field
  143. *
  144. * @param $field - an array holding all the field's data
  145. *
  146. * @type action
  147. * @since 3.6
  148. * @date 23/01/13
  149. */
  150. function render_field( $field ) {
  151. // atts
  152. $atts = array(
  153. 'class' => 'acf-oembed',
  154. );
  155. // <strong><?php _e("Error.", 'acf'); </strong> _e("No embed found for the given URL.", 'acf');
  156. // value
  157. if ( $field['value'] ) {
  158. $atts['class'] .= ' has-value';
  159. }
  160. ?>
  161. <div <?php echo acf_esc_attrs( $atts ); ?>>
  162. <?php
  163. acf_hidden_input(
  164. array(
  165. 'class' => 'input-value',
  166. 'name' => $field['name'],
  167. 'value' => $field['value'],
  168. )
  169. );
  170. ?>
  171. <div class="title">
  172. <?php
  173. acf_text_input(
  174. array(
  175. 'class' => 'input-search',
  176. 'value' => $field['value'],
  177. 'placeholder' => __( 'Enter URL', 'acf' ),
  178. 'autocomplete' => 'off',
  179. )
  180. );
  181. ?>
  182. <div class="acf-actions -hover">
  183. <a data-name="clear-button" href="#" class="acf-icon -cancel grey"></a>
  184. </div>
  185. </div>
  186. <div class="canvas">
  187. <div class="canvas-media">
  188. <?php
  189. if ( $field['value'] ) {
  190. echo $this->wp_oembed_get( $field['value'], $field['width'], $field['height'] );
  191. }
  192. ?>
  193. </div>
  194. <i class="acf-icon -picture hide-if-value"></i>
  195. </div>
  196. </div>
  197. <?php
  198. }
  199. /*
  200. * render_field_settings()
  201. *
  202. * Create extra options for your field. This is rendered when editing a field.
  203. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  204. *
  205. * @param $field - an array holding all the field's data
  206. *
  207. * @type action
  208. * @since 3.6
  209. * @date 23/01/13
  210. */
  211. function render_field_settings( $field ) {
  212. acf_render_field_setting(
  213. $field,
  214. array(
  215. 'label' => __( 'Embed Size', 'acf' ),
  216. 'type' => 'text',
  217. 'name' => 'width',
  218. 'prepend' => __( 'Width', 'acf' ),
  219. 'append' => 'px',
  220. 'placeholder' => $this->width,
  221. )
  222. );
  223. acf_render_field_setting(
  224. $field,
  225. array(
  226. 'label' => __( 'Embed Size', 'acf' ),
  227. 'type' => 'text',
  228. 'name' => 'height',
  229. 'prepend' => __( 'Height', 'acf' ),
  230. 'append' => 'px',
  231. 'placeholder' => $this->height,
  232. '_append' => 'width',
  233. )
  234. );
  235. }
  236. /**
  237. * format_value()
  238. *
  239. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  240. *
  241. * @type filter
  242. * @since 3.6
  243. * @date 23/01/13
  244. *
  245. * @param $value (mixed) the value which was loaded from the database
  246. * @param $post_id (mixed) the $post_id from which the value was loaded
  247. * @param $field (array) the field array holding all the field options
  248. *
  249. * @return $value (mixed) the modified value
  250. */
  251. function format_value( $value, $post_id, $field ) {
  252. // bail early if no value
  253. if ( empty( $value ) ) {
  254. return $value;
  255. }
  256. // prepare field to correct width and height
  257. $field = $this->prepare_field( $field );
  258. // get oembed
  259. $value = $this->wp_oembed_get( $value, $field['width'], $field['height'] );
  260. // return
  261. return $value;
  262. }
  263. /**
  264. * Return the schema array for the REST API.
  265. *
  266. * @param array $field
  267. * @return array
  268. */
  269. public function get_rest_schema( array $field ) {
  270. $schema = parent::get_rest_schema( $field );
  271. $schema['format'] = 'uri';
  272. return $schema;
  273. }
  274. }
  275. // initialize
  276. acf_register_field_type( 'acf_field_oembed' );
  277. endif; // class_exists check
  278. ?>