class-acf-field-clone.php 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  1. <?php
  2. if ( ! class_exists( 'acf_field_clone' ) ) :
  3. class acf_field_clone 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 = 'clone';
  19. $this->label = _x( 'Clone', 'noun', 'acf' );
  20. $this->category = 'layout';
  21. $this->defaults = array(
  22. 'clone' => '',
  23. 'prefix_label' => 0,
  24. 'prefix_name' => 0,
  25. 'display' => 'seamless',
  26. 'layout' => 'block',
  27. );
  28. $this->cloning = array();
  29. $this->have_rows = 'single';
  30. // register filter
  31. acf_enable_filter( 'clone' );
  32. // ajax
  33. add_action( 'wp_ajax_acf/fields/clone/query', array( $this, 'ajax_query' ) );
  34. // filters
  35. add_filter( 'acf/get_fields', array( $this, 'acf_get_fields' ), 5, 2 );
  36. add_filter( 'acf/prepare_field', array( $this, 'acf_prepare_field' ), 10, 1 );
  37. add_filter( 'acf/clone_field', array( $this, 'acf_clone_field' ), 10, 2 );
  38. }
  39. /*
  40. * is_enabled
  41. *
  42. * This function will return true if acf_local functionality is enabled
  43. *
  44. * @type function
  45. * @date 14/07/2016
  46. * @since 5.4.0
  47. *
  48. * @param n/a
  49. * @return n/a
  50. */
  51. function is_enabled() {
  52. return acf_is_filter_enabled( 'clone' );
  53. }
  54. /*
  55. * load_field()
  56. *
  57. * This filter is appied to the $field after it is loaded from the database
  58. *
  59. * @type filter
  60. * @since 3.6
  61. * @date 23/01/13
  62. *
  63. * @param $field - the field array holding all the field options
  64. *
  65. * @return $field - the field array holding all the field options
  66. */
  67. function load_field( $field ) {
  68. // bail early if not enabled
  69. if ( ! $this->is_enabled() ) {
  70. return $field;
  71. }
  72. // load sub fields
  73. // - sub field name's will be modified to include prefix_name settings
  74. $field['sub_fields'] = $this->get_cloned_fields( $field );
  75. // return
  76. return $field;
  77. }
  78. /*
  79. * acf_get_fields
  80. *
  81. * This function will hook into the 'acf/get_fields' filter and inject/replace seamless clones fields
  82. *
  83. * @type function
  84. * @date 17/06/2016
  85. * @since 5.3.8
  86. *
  87. * @param $fields (array)
  88. * @param $parent (array)
  89. * @return $fields
  90. */
  91. function acf_get_fields( $fields, $parent ) {
  92. // bail early if empty
  93. if ( empty( $fields ) ) {
  94. return $fields;
  95. }
  96. // bail early if not enabled
  97. if ( ! $this->is_enabled() ) {
  98. return $fields;
  99. }
  100. // vars
  101. $i = 0;
  102. // loop
  103. while ( $i < count( $fields ) ) {
  104. // vars
  105. $field = $fields[ $i ];
  106. // $i
  107. $i++;
  108. // bail early if not a clone field
  109. if ( $field['type'] != 'clone' ) {
  110. continue;
  111. }
  112. // bail early if not seamless
  113. if ( $field['display'] != 'seamless' ) {
  114. continue;
  115. }
  116. // bail early if sub_fields isn't set or not an array
  117. if ( ! isset( $field['sub_fields'] ) || ! is_array( $field['sub_fields'] ) ) {
  118. continue;
  119. }
  120. // replace this clone field with sub fields
  121. $i--;
  122. array_splice( $fields, $i, 1, $field['sub_fields'] );
  123. }
  124. // return
  125. return $fields;
  126. }
  127. /*
  128. * get_cloned_fields
  129. *
  130. * This function will return an array of fields for a given clone field
  131. *
  132. * @type function
  133. * @date 28/06/2016
  134. * @since 5.3.8
  135. *
  136. * @param $field (array)
  137. * @param $parent (array)
  138. * @return (array)
  139. */
  140. function get_cloned_fields( $field ) {
  141. // vars
  142. $fields = array();
  143. // bail early if no clone setting
  144. if ( empty( $field['clone'] ) ) {
  145. return $fields;
  146. }
  147. // bail early if already cloning this field (avoid infinite looping)
  148. if ( isset( $this->cloning[ $field['key'] ] ) ) {
  149. return $fields;
  150. }
  151. // update local ref
  152. $this->cloning[ $field['key'] ] = 1;
  153. // Loop over selectors and load fields.
  154. foreach ( $field['clone'] as $selector ) {
  155. // Field Group selector.
  156. if ( acf_is_field_group_key( $selector ) ) {
  157. $field_group = acf_get_field_group( $selector );
  158. if ( ! $field_group ) {
  159. continue;
  160. }
  161. $field_group_fields = acf_get_fields( $field_group );
  162. if ( ! $field_group_fields ) {
  163. continue;
  164. }
  165. $fields = array_merge( $fields, $field_group_fields );
  166. // Field selector.
  167. } elseif ( acf_is_field_key( $selector ) ) {
  168. $fields[] = acf_get_field( $selector );
  169. }
  170. }
  171. // field has ve been loaded for this $parent, time to remove cloning ref
  172. unset( $this->cloning[ $field['key'] ] );
  173. // clear false values (fields that don't exist)
  174. $fields = array_filter( $fields );
  175. // bail early if no sub fields
  176. if ( empty( $fields ) ) {
  177. return array();
  178. }
  179. // loop
  180. // run acf_clone_field() on each cloned field to modify name, key, etc
  181. foreach ( array_keys( $fields ) as $i ) {
  182. $fields[ $i ] = acf_clone_field( $fields[ $i ], $field );
  183. }
  184. // return
  185. return $fields;
  186. }
  187. /*
  188. * acf_clone_field
  189. *
  190. * This function is run when cloning a clone field
  191. * Important to run the acf_clone_field function on sub fields to pass on settings such as 'parent_layout'
  192. *
  193. * @type function
  194. * @date 28/06/2016
  195. * @since 5.3.8
  196. *
  197. * @param $field (array)
  198. * @param $clone_field (array)
  199. * @return $field
  200. */
  201. function acf_clone_field( $field, $clone_field ) {
  202. // bail early if this field is being cloned by some other kind of field (future proof)
  203. if ( $clone_field['type'] != 'clone' ) {
  204. return $field;
  205. }
  206. // backup (used later)
  207. // - backup only once (cloned clone fields can cause issues)
  208. if ( ! isset( $field['__key'] ) ) {
  209. $field['__key'] = $field['key'];
  210. $field['__name'] = $field['_name'];
  211. $field['__label'] = $field['label'];
  212. }
  213. // seamless
  214. if ( $clone_field['display'] == 'seamless' ) {
  215. // modify key
  216. // - this will allow sub clone fields to correctly load values for the same cloned field
  217. // - the original key will later be restored by acf/prepare_field allowing conditional logic JS to work
  218. $field['key'] = $clone_field['key'] . '_' . $field['key'];
  219. // modify prefix allowing clone field to save sub fields
  220. // - only used for parent seamless fields. Block or sub field's prefix will be overriden which also works
  221. $field['prefix'] = $clone_field['prefix'] . '[' . $clone_field['key'] . ']';
  222. // modify parent
  223. $field['parent'] = $clone_field['parent'];
  224. // label_format
  225. if ( $clone_field['prefix_label'] ) {
  226. $field['label'] = $clone_field['label'] . ' ' . $field['label'];
  227. }
  228. }
  229. // prefix_name
  230. if ( $clone_field['prefix_name'] ) {
  231. // modify the field name
  232. // - this will allow field to load / save correctly
  233. $field['name'] = $clone_field['name'] . '_' . $field['_name'];
  234. // modify the field _name (orig name)
  235. // - this will allow fields to correctly understand the modified field
  236. if ( $clone_field['display'] == 'seamless' ) {
  237. $field['_name'] = $clone_field['_name'] . '_' . $field['_name'];
  238. }
  239. }
  240. // required
  241. if ( $clone_field['required'] ) {
  242. $field['required'] = 1;
  243. }
  244. // type specific
  245. // note: seamless clone fields will not be triggered
  246. if ( $field['type'] == 'clone' ) {
  247. $field = $this->acf_clone_clone_field( $field, $clone_field );
  248. }
  249. // return
  250. return $field;
  251. }
  252. /*
  253. * acf_clone_clone_field
  254. *
  255. * This function is run when cloning a clone field
  256. * Important to run the acf_clone_field function on sub fields to pass on settings such as 'parent_layout'
  257. * Do not delete! Removing this logic causes major issues with cloned clone fields within a flexible content layout.
  258. *
  259. * @type function
  260. * @date 28/06/2016
  261. * @since 5.3.8
  262. *
  263. * @param $field (array)
  264. * @param $clone_field (array)
  265. * @return $field
  266. */
  267. function acf_clone_clone_field( $field, $clone_field ) {
  268. // modify the $clone_field name
  269. // This seems odd, however, the $clone_field is later passed into the acf_clone_field() function
  270. // Do not delete!
  271. // when cloning a clone field, it is important to also change the _name too
  272. // this allows sub clone fields to appear correctly in get_row() row array
  273. if ( $field['prefix_name'] ) {
  274. $clone_field['name'] = $field['_name'];
  275. $clone_field['_name'] = $field['_name'];
  276. }
  277. // bail early if no sub fields
  278. if ( empty( $field['sub_fields'] ) ) {
  279. return $field;
  280. }
  281. // loop
  282. foreach ( $field['sub_fields'] as &$sub_field ) {
  283. // clone
  284. $sub_field = acf_clone_field( $sub_field, $clone_field );
  285. }
  286. // return
  287. return $field;
  288. }
  289. /*
  290. * prepare_field_for_db
  291. *
  292. * description
  293. *
  294. * @type function
  295. * @date 4/11/16
  296. * @since 5.5.0
  297. *
  298. * @param $post_id (int)
  299. * @return $post_id (int)
  300. */
  301. function prepare_field_for_db( $field ) {
  302. // bail early if no sub fields
  303. if ( empty( $field['sub_fields'] ) ) {
  304. return $field;
  305. }
  306. // bail early if name == _name
  307. // this is a parent clone field and does not require any modification to sub field names
  308. if ( $field['name'] == $field['_name'] ) {
  309. return $field;
  310. }
  311. // this is a sub field
  312. // _name = 'my_field'
  313. // name = 'rep_0_my_field'
  314. // modify all sub fields to add 'rep_0_' name prefix (prefix_name setting has already been applied)
  315. $length = strlen( $field['_name'] );
  316. $prefix = substr( $field['name'], 0, -$length );
  317. // bail early if _name is not found at the end of name (unknown potential error)
  318. if ( $prefix . $field['_name'] !== $field['name'] ) {
  319. return $field;
  320. }
  321. // acf_log('== prepare_field_for_db ==');
  322. // acf_log('- clone name:', $field['name']);
  323. // acf_log('- clone _name:', $field['_name']);
  324. // loop
  325. foreach ( $field['sub_fields'] as &$sub_field ) {
  326. $sub_field['name'] = $prefix . $sub_field['name'];
  327. }
  328. // return
  329. return $field;
  330. }
  331. /*
  332. * load_value()
  333. *
  334. * This filter is applied to the $value after it is loaded from the db
  335. *
  336. * @type filter
  337. * @since 3.6
  338. * @date 23/01/13
  339. *
  340. * @param $value (mixed) the value found in the database
  341. * @param $post_id (mixed) the $post_id from which the value was loaded
  342. * @param $field (array) the field array holding all the field options
  343. * @return $value
  344. */
  345. function load_value( $value, $post_id, $field ) {
  346. // bail early if no sub fields
  347. if ( empty( $field['sub_fields'] ) ) {
  348. return $value;
  349. }
  350. // modify names
  351. $field = $this->prepare_field_for_db( $field );
  352. // load sub fields
  353. $value = array();
  354. // loop
  355. foreach ( $field['sub_fields'] as $sub_field ) {
  356. // add value
  357. $value[ $sub_field['key'] ] = acf_get_value( $post_id, $sub_field );
  358. }
  359. // return
  360. return $value;
  361. }
  362. /*
  363. * format_value()
  364. *
  365. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  366. *
  367. * @type filter
  368. * @since 3.6
  369. * @date 23/01/13
  370. *
  371. * @param $value (mixed) the value which was loaded from the database
  372. * @param $post_id (mixed) the $post_id from which the value was loaded
  373. * @param $field (array) the field array holding all the field options
  374. *
  375. * @return $value (mixed) the modified value
  376. */
  377. function format_value( $value, $post_id, $field ) {
  378. // bail early if no value
  379. if ( empty( $value ) ) {
  380. return false;
  381. }
  382. // modify names
  383. $field = $this->prepare_field_for_db( $field );
  384. // loop
  385. foreach ( $field['sub_fields'] as $sub_field ) {
  386. // extract value
  387. $sub_value = acf_extract_var( $value, $sub_field['key'] );
  388. // format value
  389. $sub_value = acf_format_value( $sub_value, $post_id, $sub_field );
  390. // append to $row
  391. $value[ $sub_field['__name'] ] = $sub_value;
  392. }
  393. // return
  394. return $value;
  395. }
  396. /**
  397. * Apply basic formatting to prepare the value for default REST output.
  398. *
  399. * @param mixed $value
  400. * @param string|int $post_id
  401. * @param array $field
  402. * @return mixed
  403. */
  404. public function format_value_for_rest( $value, $post_id, array $field ) {
  405. if ( empty( $value ) || ! is_array( $value ) ) {
  406. return $value;
  407. }
  408. if ( ! is_array( $field ) || ! isset( $field['sub_fields'] ) || ! is_array( $field['sub_fields'] ) ) {
  409. return $value;
  410. }
  411. // Loop through each row and within that, each sub field to process sub fields individually.
  412. foreach ( $field['sub_fields'] as $sub_field ) {
  413. // Extract the sub field 'field_key'=>'value' pair from the $value and format it.
  414. $sub_value = acf_extract_var( $value, $sub_field['key'] );
  415. $sub_value = acf_format_value_for_rest( $sub_value, $post_id, $sub_field );
  416. // Add the sub field value back to the $value but mapped to the field name instead
  417. // of the key reference.
  418. $value[ $sub_field['name'] ] = $sub_value;
  419. }
  420. return $value;
  421. }
  422. /*
  423. * update_value()
  424. *
  425. * This filter is appied to the $value before it is updated in the db
  426. *
  427. * @type filter
  428. * @since 3.6
  429. * @date 23/01/13
  430. *
  431. * @param $value - the value which will be saved in the database
  432. * @param $field - the field array holding all the field options
  433. * @param $post_id - the $post_id of which the value will be saved
  434. *
  435. * @return $value - the modified value
  436. */
  437. function update_value( $value, $post_id, $field ) {
  438. // bail early if no value
  439. if ( ! acf_is_array( $value ) ) {
  440. return null;
  441. }
  442. // bail early if no sub fields
  443. if ( empty( $field['sub_fields'] ) ) {
  444. return null;
  445. }
  446. // modify names
  447. $field = $this->prepare_field_for_db( $field );
  448. // loop
  449. foreach ( $field['sub_fields'] as $sub_field ) {
  450. // vars
  451. $v = false;
  452. // key (backend)
  453. if ( isset( $value[ $sub_field['key'] ] ) ) {
  454. $v = $value[ $sub_field['key'] ];
  455. // name (frontend)
  456. } elseif ( isset( $value[ $sub_field['_name'] ] ) ) {
  457. $v = $value[ $sub_field['_name'] ];
  458. // empty
  459. } else {
  460. // input is not set (hidden by conditioanl logic)
  461. continue;
  462. }
  463. // restore original field key
  464. $sub_field = $this->acf_prepare_field( $sub_field );
  465. // update value
  466. acf_update_value( $v, $post_id, $sub_field );
  467. }
  468. // return
  469. return '';
  470. }
  471. /*
  472. * render_field()
  473. *
  474. * Create the HTML interface for your field
  475. *
  476. * @param $field - an array holding all the field's data
  477. *
  478. * @type action
  479. * @since 3.6
  480. * @date 23/01/13
  481. */
  482. function render_field( $field ) {
  483. // bail early if no sub fields
  484. if ( empty( $field['sub_fields'] ) ) {
  485. return;
  486. }
  487. // load values
  488. foreach ( $field['sub_fields'] as &$sub_field ) {
  489. // add value
  490. if ( isset( $field['value'][ $sub_field['key'] ] ) ) {
  491. // this is a normal value
  492. $sub_field['value'] = $field['value'][ $sub_field['key'] ];
  493. } elseif ( isset( $sub_field['default_value'] ) ) {
  494. // no value, but this sub field has a default value
  495. $sub_field['value'] = $sub_field['default_value'];
  496. }
  497. // update prefix to allow for nested values
  498. $sub_field['prefix'] = $field['name'];
  499. // restore label
  500. $sub_field['label'] = $sub_field['__label'];
  501. // restore required
  502. if ( $field['required'] ) {
  503. $sub_field['required'] = 0;
  504. }
  505. }
  506. // render
  507. if ( $field['layout'] == 'table' ) {
  508. $this->render_field_table( $field );
  509. } else {
  510. $this->render_field_block( $field );
  511. }
  512. }
  513. /*
  514. * render_field_block
  515. *
  516. * description
  517. *
  518. * @type function
  519. * @date 12/07/2016
  520. * @since 5.4.0
  521. *
  522. * @param $post_id (int)
  523. * @return $post_id (int)
  524. */
  525. function render_field_block( $field ) {
  526. // vars
  527. $label_placement = $field['layout'] == 'block' ? 'top' : 'left';
  528. // html
  529. echo '<div class="acf-clone-fields acf-fields -' . $label_placement . ' -border">';
  530. foreach ( $field['sub_fields'] as $sub_field ) {
  531. acf_render_field_wrap( $sub_field );
  532. }
  533. echo '</div>';
  534. }
  535. /*
  536. * render_field_table
  537. *
  538. * description
  539. *
  540. * @type function
  541. * @date 12/07/2016
  542. * @since 5.4.0
  543. *
  544. * @param $post_id (int)
  545. * @return $post_id (int)
  546. */
  547. function render_field_table( $field ) {
  548. ?>
  549. <table class="acf-table">
  550. <thead>
  551. <tr>
  552. <?php
  553. foreach ( $field['sub_fields'] as $sub_field ) :
  554. // Prepare field (allow sub fields to be removed).
  555. $sub_field = acf_prepare_field( $sub_field );
  556. if ( ! $sub_field ) {
  557. continue;
  558. }
  559. // Define attrs.
  560. $attrs = array();
  561. $attrs['class'] = 'acf-th';
  562. $attrs['data-name'] = $sub_field['_name'];
  563. $attrs['data-type'] = $sub_field['type'];
  564. $attrs['data-key'] = $sub_field['key'];
  565. if ( $sub_field['wrapper']['width'] ) {
  566. $attrs['data-width'] = $sub_field['wrapper']['width'];
  567. $attrs['style'] = 'width: ' . $sub_field['wrapper']['width'] . '%;';
  568. }
  569. ?>
  570. <th <?php echo acf_esc_attrs( $attrs ); ?>>
  571. <?php acf_render_field_label( $sub_field ); ?>
  572. <?php acf_render_field_instructions( $sub_field ); ?>
  573. </th>
  574. <?php endforeach; ?>
  575. </tr>
  576. </thead>
  577. <tbody>
  578. <tr class="acf-row">
  579. <?php
  580. foreach ( $field['sub_fields'] as $sub_field ) {
  581. acf_render_field_wrap( $sub_field, 'td' );
  582. }
  583. ?>
  584. </tr>
  585. </tbody>
  586. </table>
  587. <?php
  588. }
  589. /*
  590. * render_field_settings()
  591. *
  592. * Create extra options for your field. This is rendered when editing a field.
  593. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  594. *
  595. * @param $field - an array holding all the field's data
  596. *
  597. * @type action
  598. * @since 3.6
  599. * @date 23/01/13
  600. */
  601. function render_field_settings( $field ) {
  602. // temp enable 'local' to allow .json fields to be displayed
  603. acf_enable_filter( 'local' );
  604. // default_value
  605. acf_render_field_setting(
  606. $field,
  607. array(
  608. 'label' => __( 'Fields', 'acf' ),
  609. 'instructions' => __( 'Select one or more fields you wish to clone', 'acf' ),
  610. 'type' => 'select',
  611. 'name' => 'clone',
  612. 'multiple' => 1,
  613. 'allow_null' => 1,
  614. 'choices' => $this->get_clone_setting_choices( $field['clone'] ),
  615. 'ui' => 1,
  616. 'ajax' => 1,
  617. 'ajax_action' => 'acf/fields/clone/query',
  618. 'placeholder' => '',
  619. )
  620. );
  621. acf_disable_filter( 'local' );
  622. // display
  623. acf_render_field_setting(
  624. $field,
  625. array(
  626. 'label' => __( 'Display', 'acf' ),
  627. 'instructions' => __( 'Specify the style used to render the clone field', 'acf' ),
  628. 'type' => 'select',
  629. 'name' => 'display',
  630. 'class' => 'setting-display',
  631. 'choices' => array(
  632. 'group' => __( 'Group (displays selected fields in a group within this field)', 'acf' ),
  633. 'seamless' => __( 'Seamless (replaces this field with selected fields)', 'acf' ),
  634. ),
  635. )
  636. );
  637. // layout
  638. acf_render_field_setting(
  639. $field,
  640. array(
  641. 'label' => __( 'Layout', 'acf' ),
  642. 'instructions' => __( 'Specify the style used to render the selected fields', 'acf' ),
  643. 'type' => 'radio',
  644. 'name' => 'layout',
  645. 'layout' => 'horizontal',
  646. 'choices' => array(
  647. 'block' => __( 'Block', 'acf' ),
  648. 'table' => __( 'Table', 'acf' ),
  649. 'row' => __( 'Row', 'acf' ),
  650. ),
  651. )
  652. );
  653. // prefix_label
  654. $instructions = __( 'Labels will be displayed as %s', 'acf' );
  655. $instructions = sprintf( $instructions, '<code class="prefix-label-code-1"></code>' );
  656. acf_render_field_setting(
  657. $field,
  658. array(
  659. 'label' => __( 'Prefix Field Labels', 'acf' ),
  660. 'instructions' => $instructions,
  661. 'name' => 'prefix_label',
  662. 'class' => 'setting-prefix-label',
  663. 'type' => 'true_false',
  664. 'ui' => 1,
  665. )
  666. );
  667. // prefix_name
  668. $instructions = __( 'Values will be saved as %s', 'acf' );
  669. $instructions = sprintf( $instructions, '<code class="prefix-name-code-1"></code>' );
  670. acf_render_field_setting(
  671. $field,
  672. array(
  673. 'label' => __( 'Prefix Field Names', 'acf' ),
  674. 'instructions' => $instructions,
  675. 'name' => 'prefix_name',
  676. 'class' => 'setting-prefix-name',
  677. 'type' => 'true_false',
  678. 'ui' => 1,
  679. )
  680. );
  681. }
  682. /*
  683. * get_clone_setting_choices
  684. *
  685. * This function will return an array of choices data for Select2
  686. *
  687. * @type function
  688. * @date 17/06/2016
  689. * @since 5.3.8
  690. *
  691. * @param $value (mixed)
  692. * @return (array)
  693. */
  694. function get_clone_setting_choices( $value ) {
  695. // vars
  696. $choices = array();
  697. // bail early if no $value
  698. if ( empty( $value ) ) {
  699. return $choices;
  700. }
  701. // force value to array
  702. $value = acf_get_array( $value );
  703. // loop
  704. foreach ( $value as $v ) {
  705. $choices[ $v ] = $this->get_clone_setting_choice( $v );
  706. }
  707. // return
  708. return $choices;
  709. }
  710. /*
  711. * get_clone_setting_choice
  712. *
  713. * This function will return the label for a given clone choice
  714. *
  715. * @type function
  716. * @date 17/06/2016
  717. * @since 5.3.8
  718. *
  719. * @param $selector (mixed)
  720. * @return (string)
  721. */
  722. function get_clone_setting_choice( $selector = '' ) {
  723. // bail early no selector
  724. if ( ! $selector ) {
  725. return '';
  726. }
  727. // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified elsewhere.
  728. // ajax_fields
  729. if ( isset( $_POST['fields'][ $selector ] ) ) {
  730. return $this->get_clone_setting_field_choice( acf_sanitize_request_args( $_POST['fields'][ $selector ] ) );
  731. }
  732. // phpcs:enable WordPress.Security.NonceVerification.Missing
  733. // field
  734. if ( acf_is_field_key( $selector ) ) {
  735. return $this->get_clone_setting_field_choice( acf_get_field( $selector ) );
  736. }
  737. // group
  738. if ( acf_is_field_group_key( $selector ) ) {
  739. return $this->get_clone_setting_group_choice( acf_get_field_group( $selector ) );
  740. }
  741. // return
  742. return $selector;
  743. }
  744. /*
  745. * get_clone_setting_field_choice
  746. *
  747. * This function will return the text for a field choice
  748. *
  749. * @type function
  750. * @date 20/07/2016
  751. * @since 5.4.0
  752. *
  753. * @param $field (array)
  754. * @return (string)
  755. */
  756. function get_clone_setting_field_choice( $field ) {
  757. // bail early if no field
  758. if ( ! $field ) {
  759. return __( 'Unknown field', 'acf' );
  760. }
  761. // title
  762. $title = $field['label'] ? $field['label'] : __( '(no title)', 'acf' );
  763. // append type
  764. $title .= ' (' . $field['type'] . ')';
  765. // ancestors
  766. // - allow for AJAX to send through ancestors count
  767. $ancestors = isset( $field['ancestors'] ) ? $field['ancestors'] : count( acf_get_field_ancestors( $field ) );
  768. $title = str_repeat( '- ', $ancestors ) . $title;
  769. // return
  770. return $title;
  771. }
  772. /*
  773. * get_clone_setting_group_choice
  774. *
  775. * This function will return the text for a group choice
  776. *
  777. * @type function
  778. * @date 20/07/2016
  779. * @since 5.4.0
  780. *
  781. * @param $field_group (array)
  782. * @return (string)
  783. */
  784. function get_clone_setting_group_choice( $field_group ) {
  785. // bail early if no field group
  786. if ( ! $field_group ) {
  787. return __( 'Unknown field group', 'acf' );
  788. }
  789. // return
  790. return sprintf( __( 'All fields from %s field group', 'acf' ), $field_group['title'] );
  791. }
  792. /*
  793. * ajax_query
  794. *
  795. * description
  796. *
  797. * @type function
  798. * @date 17/06/2016
  799. * @since 5.3.8
  800. *
  801. * @param $post_id (int)
  802. * @return $post_id (int)
  803. */
  804. function ajax_query() {
  805. // validate
  806. if ( ! acf_verify_ajax() ) {
  807. die();
  808. }
  809. // disable field to allow clone fields to appear selectable
  810. acf_disable_filter( 'clone' );
  811. // options
  812. $options = acf_parse_args(
  813. $_POST,
  814. array(
  815. 'post_id' => 0,
  816. 'paged' => 0,
  817. 's' => '',
  818. 'title' => '',
  819. 'fields' => array(),
  820. )
  821. );
  822. // vars
  823. $results = array();
  824. $s = false;
  825. $i = -1;
  826. $limit = 20;
  827. $range_start = $limit * ( $options['paged'] - 1 ); // 0, 20, 40
  828. $range_end = $range_start + ( $limit - 1 ); // 19, 39, 59
  829. // search
  830. if ( $options['s'] !== '' ) {
  831. // strip slashes (search may be integer)
  832. $s = wp_unslash( strval( $options['s'] ) );
  833. }
  834. // load groups
  835. $field_groups = acf_get_field_groups();
  836. $field_group = false;
  837. // bail early if no field groups
  838. if ( empty( $field_groups ) ) {
  839. die();
  840. }
  841. // move current field group to start
  842. foreach ( array_keys( $field_groups ) as $j ) {
  843. // check ID
  844. if ( $field_groups[ $j ]['ID'] !== $options['post_id'] ) {
  845. continue;
  846. }
  847. // extract field group and move to start
  848. $field_group = acf_extract_var( $field_groups, $j );
  849. // field group found, stop looking
  850. break;
  851. }
  852. // if field group was not found, this is a new field group (not yet saved)
  853. if ( ! $field_group ) {
  854. $field_group = array(
  855. 'ID' => $options['post_id'],
  856. 'title' => $options['title'],
  857. 'key' => '',
  858. );
  859. }
  860. // move current field group to start of list
  861. array_unshift( $field_groups, $field_group );
  862. // loop
  863. foreach ( $field_groups as $field_group ) {
  864. // vars
  865. $fields = false;
  866. $ignore_s = false;
  867. $data = array(
  868. 'text' => $field_group['title'],
  869. 'children' => array(),
  870. );
  871. // get fields
  872. if ( $field_group['ID'] == $options['post_id'] ) {
  873. $fields = $options['fields'];
  874. } else {
  875. $fields = acf_get_fields( $field_group );
  876. $fields = acf_prepare_fields_for_import( $fields );
  877. }
  878. // bail early if no fields
  879. if ( ! $fields ) {
  880. continue;
  881. }
  882. // show all children for field group search match
  883. if ( $s !== false && stripos( $data['text'], $s ) !== false ) {
  884. $ignore_s = true;
  885. }
  886. // populate children
  887. $children = array();
  888. $children[] = $field_group['key'];
  889. foreach ( $fields as $field ) {
  890. $children[] = $field['key']; }
  891. // loop
  892. foreach ( $children as $child ) {
  893. // bail early if no key (fake field group or corrupt field)
  894. if ( ! $child ) {
  895. continue;
  896. }
  897. // vars
  898. $text = false;
  899. // bail early if is search, and $text does not contain $s
  900. if ( $s !== false && ! $ignore_s ) {
  901. // get early
  902. $text = $this->get_clone_setting_choice( $child );
  903. // search
  904. if ( stripos( $text, $s ) === false ) {
  905. continue;
  906. }
  907. }
  908. // $i
  909. $i++;
  910. // bail early if $i is out of bounds
  911. if ( $i < $range_start || $i > $range_end ) {
  912. continue;
  913. }
  914. // load text
  915. if ( $text === false ) {
  916. $text = $this->get_clone_setting_choice( $child );
  917. }
  918. // append
  919. $data['children'][] = array(
  920. 'id' => $child,
  921. 'text' => $text,
  922. );
  923. }
  924. // bail early if no children
  925. // - this group contained fields, but none shown on this page
  926. if ( empty( $data['children'] ) ) {
  927. continue;
  928. }
  929. // append
  930. $results[] = $data;
  931. // end loop if $i is out of bounds
  932. // - no need to look further
  933. if ( $i > $range_end ) {
  934. break;
  935. }
  936. }
  937. // return
  938. acf_send_ajax_results(
  939. array(
  940. 'results' => $results,
  941. 'limit' => $limit,
  942. )
  943. );
  944. }
  945. /*
  946. * acf_prepare_field
  947. *
  948. * This function will restore a field's key ready for input
  949. *
  950. * @type function
  951. * @date 6/09/2016
  952. * @since 5.4.0
  953. *
  954. * @param $field (array)
  955. * @return $field
  956. */
  957. function acf_prepare_field( $field ) {
  958. // bail early if not cloned
  959. if ( empty( $field['_clone'] ) ) {
  960. return $field;
  961. }
  962. // restore key
  963. if ( isset( $field['__key'] ) ) {
  964. $field['key'] = $field['__key'];
  965. }
  966. // return
  967. return $field;
  968. }
  969. /*
  970. * validate_value
  971. *
  972. * description
  973. *
  974. * @type function
  975. * @date 11/02/2014
  976. * @since 5.0.0
  977. *
  978. * @param $post_id (int)
  979. * @return $post_id (int)
  980. */
  981. function validate_value( $valid, $value, $field, $input ) {
  982. // bail early if no $value
  983. if ( empty( $value ) ) {
  984. return $valid;
  985. }
  986. // bail early if no sub fields
  987. if ( empty( $field['sub_fields'] ) ) {
  988. return $valid;
  989. }
  990. // loop
  991. foreach ( array_keys( $field['sub_fields'] ) as $i ) {
  992. // get sub field
  993. $sub_field = $field['sub_fields'][ $i ];
  994. $k = $sub_field['key'];
  995. // bail early if valu enot set (conditional logic?)
  996. if ( ! isset( $value[ $k ] ) ) {
  997. continue;
  998. }
  999. // validate
  1000. acf_validate_value( $value[ $k ], $sub_field, "{$input}[{$k}]" );
  1001. }
  1002. // return
  1003. return $valid;
  1004. }
  1005. /**
  1006. * Return the schema array for the REST API.
  1007. *
  1008. * @param array $field
  1009. * @return array
  1010. */
  1011. public function get_rest_schema( array $field ) {
  1012. $schema = array(
  1013. 'type' => array( 'object', 'null' ),
  1014. 'required' => ! empty( $field['required'] ) ? array() : false,
  1015. 'items' => array(
  1016. 'type' => 'object',
  1017. 'properties' => array(),
  1018. ),
  1019. );
  1020. foreach ( $field['sub_fields'] as $sub_field ) {
  1021. /** @var acf_field $type */
  1022. $type = acf_get_field_type( $sub_field['type'] );
  1023. if ( ! $type ) {
  1024. continue;
  1025. }
  1026. $sub_field_schema = $type->get_rest_schema( $sub_field );
  1027. // Passing null to nested fields has no effect. Remove this as a possible type to prevent
  1028. // confusion in the schema.
  1029. $null_type_index = array_search( 'null', $sub_field_schema['type'] );
  1030. if ( $null_type_index !== false ) {
  1031. unset( $sub_field_schema['type'][ $null_type_index ] );
  1032. }
  1033. $schema['items']['properties'][ $sub_field['name'] ] = $sub_field_schema;
  1034. /**
  1035. * If the clone field itself is marked as required, all subfields are required,
  1036. * regardless of the status of the original fields.
  1037. */
  1038. if ( is_array( $schema['required'] ) ) {
  1039. $schema['required'][] = $sub_field['name'];
  1040. }
  1041. }
  1042. return $schema;
  1043. }
  1044. }
  1045. // initialize
  1046. acf_register_field_type( 'acf_field_clone' );
  1047. endif; // class_exists check
  1048. ?>