Sin descripción

class-acf-field-oembed.php 6.5KB

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