/wp-content/plugins/advanced-custom-fields-pro/core/local.php

https://gitlab.com/surya.ayrus22/kcethsw1 · PHP · 878 lines · 223 code · 305 blank · 350 comment · 31 complexity · 688fa9a34d87d6cdcdd896e0cbb87ca3 MD5 · raw file

  1. <?php
  2. class acf_local {
  3. // vars
  4. var $enabled = true,
  5. $groups = array(),
  6. $fields = array(),
  7. $parents = array();
  8. function __construct() {
  9. add_filter('acf/get_field_groups', array($this, 'get_field_groups'), 10, 1);
  10. add_action('acf/delete_field', array($this, 'delete_field'), 10, 1);
  11. }
  12. /*
  13. * get_field_groups
  14. *
  15. * This function will override and add field groups to the `acf_get_field_groups()` results
  16. *
  17. * @type filter (acf/get_field_groups)
  18. * @date 5/12/2013
  19. * @since 5.0.0
  20. *
  21. * @param $field_groups (array)
  22. * @return $field_groups
  23. */
  24. function get_field_groups( $field_groups ) {
  25. // validate
  26. if( !acf_have_local_field_groups() ) {
  27. return $field_groups;
  28. }
  29. // vars
  30. $ignore = array();
  31. $added = false;
  32. // populate ignore list
  33. if( !empty($field_groups) ) {
  34. foreach( $field_groups as $k => $group ) {
  35. $ignore[] = $group['key'];
  36. }
  37. }
  38. // append field groups
  39. $groups = acf_get_local_field_groups();
  40. foreach( $groups as $group ) {
  41. // is ignore
  42. if( in_array($group['key'], $ignore) ) {
  43. continue;
  44. }
  45. // append
  46. $field_groups[] = $group;
  47. $added = true;
  48. }
  49. // order field groups based on menu_order, title
  50. if( $added ) {
  51. $menu_order = array();
  52. $title = array();
  53. foreach( $field_groups as $key => $row ) {
  54. $menu_order[ $key ] = $row['menu_order'];
  55. $title[ $key ] = $row['title'];
  56. }
  57. // sort the array with menu_order ascending
  58. array_multisort( $menu_order, SORT_ASC, $title, SORT_ASC, $field_groups );
  59. }
  60. // return
  61. return $field_groups;
  62. }
  63. /*
  64. * delete_field
  65. *
  66. * description
  67. *
  68. * @type function
  69. * @date 10/12/2014
  70. * @since 5.1.5
  71. *
  72. * @param $post_id (int)
  73. * @return $post_id (int)
  74. */
  75. function delete_field( $field ) {
  76. $this->remove_field( $field['key'] );
  77. }
  78. /*
  79. * add_field_group
  80. *
  81. * This function will add a $field group to the local placeholder
  82. *
  83. * @type function
  84. * @date 10/03/2014
  85. * @since 5.0.0
  86. *
  87. * @param $field_group (array)
  88. * @return n/a
  89. */
  90. function add_field_group( $field_group ) {
  91. // validate
  92. $field_group = acf_get_valid_field_group($field_group);
  93. // don't allow overrides
  94. if( acf_is_local_field_group($field_group['key']) ) {
  95. return;
  96. }
  97. // add local
  98. if( empty($field_group['local']) ) {
  99. $field_group['local'] = 'php';
  100. }
  101. // remove fields
  102. $fields = acf_extract_var($field_group, 'fields');
  103. // format fields
  104. $fields = acf_prepare_fields_for_import( $fields );
  105. // add field group
  106. $this->groups[ $field_group['key'] ] = $field_group;
  107. // add fields
  108. foreach( $fields as $field ) {
  109. // add parent
  110. if( empty($field['parent']) ) {
  111. $field['parent'] = $field_group['key'];
  112. }
  113. // add field
  114. $this->add_field( $field );
  115. }
  116. }
  117. /*
  118. * add_field
  119. *
  120. * This function will add a $field to the local placeholder
  121. *
  122. * @type function
  123. * @date 10/03/2014
  124. * @since 5.0.0
  125. *
  126. * @param $field (array)
  127. * @return n/a
  128. */
  129. function add_field( $field ) {
  130. // vars
  131. // - allow for the very unexpected case where no key or parent exist
  132. $key = acf_maybe_get($field, 'key', '');
  133. $parent = acf_maybe_get($field, 'parent', '');
  134. // add parent reference
  135. $this->add_parent_reference( $parent, $key );
  136. // add in menu order
  137. $field['menu_order'] = count( $this->parents[ $parent ] ) - 1;
  138. // add field
  139. $this->fields[ $key ] = $field;
  140. // clear cache
  141. wp_cache_delete( "get_field/key={$key}", 'acf' );
  142. wp_cache_delete( "get_fields/parent={$parent}", 'acf' );
  143. }
  144. /*
  145. * remove_field
  146. *
  147. * This function will remove a $field to the local placeholder
  148. *
  149. * @type function
  150. * @date 10/03/2014
  151. * @since 5.0.0
  152. *
  153. * @param $key (string)
  154. * @return n/a
  155. */
  156. function remove_field( $key ) {
  157. // get field
  158. $field = acf_get_field( $key );
  159. // remove parent reference
  160. $this->remove_parent_reference( $field['parent'], $field['key'] );
  161. // remove field
  162. unset( $this->fields[ $key ] );
  163. // remove children
  164. if( acf_have_local_fields( $key) ) {
  165. acf_remove_local_fields( $key );
  166. }
  167. }
  168. function add_parent_reference( $parent_key, $field_key ) {
  169. // create array
  170. if( !isset($this->parents[ $parent_key ]) ) {
  171. $this->parents[ $parent_key ] = array();
  172. } elseif( in_array($field_key, $this->parents[ $parent_key ]) ) {
  173. // bail early if already in array
  174. return false;
  175. }
  176. // append
  177. $this->parents[ $parent_key ][] = $field_key;
  178. // return
  179. return true;
  180. }
  181. function remove_parent_reference( $parent_key, $field_key ) {
  182. // bail early if no parent
  183. if( !isset($this->parents[ $parent_key ]) ) {
  184. return false;
  185. }
  186. // remove
  187. $this->parents[ $parent_key ] = array_diff($this->parents[ $parent_key ], array($field_key));
  188. // return
  189. return true;
  190. }
  191. }
  192. /*
  193. * acf_local
  194. *
  195. * This function will return the one true acf_local
  196. *
  197. * @type function
  198. * @date 10/03/2014
  199. * @since 5.0.0
  200. *
  201. * @param n/a
  202. * @return acf_local (object)
  203. */
  204. function acf_local() {
  205. // globals
  206. global $acf_local;
  207. // instantiate
  208. if( !isset($acf_local) )
  209. {
  210. $acf_local = new acf_local();
  211. }
  212. // return
  213. return $acf_local;
  214. }
  215. /*
  216. * acf_disable_local
  217. *
  218. * This function will disable the local functionality for DB only interaction
  219. *
  220. * @type function
  221. * @date 11/06/2014
  222. * @since 5.0.0
  223. *
  224. * @param n/a
  225. * @return n/a
  226. */
  227. function acf_disable_local() {
  228. acf_local()->enabled = false;
  229. }
  230. /*
  231. * acf_enable_local
  232. *
  233. * This function will enable the local functionality
  234. *
  235. * @type function
  236. * @date 11/06/2014
  237. * @since 5.0.0
  238. *
  239. * @param n/a
  240. * @return n/a
  241. */
  242. function acf_enable_local() {
  243. acf_local()->enabled = true;
  244. }
  245. /*
  246. * acf_is_local_enabled
  247. *
  248. * This function will return true|false if the local functionality is enabled
  249. *
  250. * @type function
  251. * @date 11/06/2014
  252. * @since 5.0.0
  253. *
  254. * @param n/a
  255. * @return n/a
  256. */
  257. function acf_is_local_enabled() {
  258. // validate
  259. if( !acf_get_setting('local') ) {
  260. return false;
  261. }
  262. if( !acf_local()->enabled ) {
  263. return false;
  264. }
  265. // return
  266. return true;
  267. }
  268. /*
  269. * acf_count_local_field_groups
  270. *
  271. * This function will return the number of local field groups
  272. *
  273. * @type function
  274. * @date 3/12/2014
  275. * @since 5.1.5
  276. *
  277. * @param $type (string) specify the type. eg. 'json'
  278. * @return (int)
  279. */
  280. function acf_count_local_field_groups( $type = '' ) {
  281. // vars
  282. $count = 0;
  283. // check for groups
  284. if( !empty(acf_local()->groups) ) {
  285. // acf_local
  286. foreach( acf_local()->groups as $group ) {
  287. // ignore if not specific type
  288. if( $type && $group['local'] != $type ) {
  289. continue;
  290. }
  291. $count++;
  292. }
  293. }
  294. // return
  295. return $count;
  296. }
  297. /*
  298. * acf_have_local_field_groups
  299. *
  300. * This function will return true if fields exist for a given 'parent' key (field group key or field key)
  301. *
  302. * @type function
  303. * @date 10/03/2014
  304. * @since 5.0.0
  305. *
  306. * @param n/a
  307. * @return (bolean)
  308. */
  309. function acf_have_local_field_groups() {
  310. // validate
  311. if( !acf_is_local_enabled() ) {
  312. return false;
  313. }
  314. // check for groups
  315. if( !empty(acf_local()->groups) ) {
  316. return true;
  317. }
  318. // return
  319. return false;
  320. }
  321. /*
  322. * acf_get_local_field_groups
  323. *
  324. * This function will return an array of fields for a given 'parent' key (field group key or field key)
  325. *
  326. * @type function
  327. * @date 10/03/2014
  328. * @since 5.0.0
  329. *
  330. * @param $key (string)
  331. * @return (bolean)
  332. */
  333. function acf_get_local_field_groups() {
  334. // bail early if no groups
  335. if( !acf_have_local_field_groups() ) {
  336. return false;
  337. }
  338. // vars
  339. $groups = array();
  340. // acf_local
  341. foreach( acf_local()->groups as $group ) {
  342. $groups[] = $group;
  343. }
  344. // return
  345. return $groups;
  346. }
  347. /*
  348. * acf_add_local_field_group
  349. *
  350. * This function will add a $field group to the local placeholder
  351. *
  352. * @type function
  353. * @date 10/03/2014
  354. * @since 5.0.0
  355. *
  356. * @param $post_id (int)
  357. * @return $post_id (int)
  358. */
  359. function acf_add_local_field_group( $field_group ) {
  360. acf_local()->add_field_group( $field_group );
  361. }
  362. /*
  363. * acf_is_local_field_group
  364. *
  365. * This function will return true if the field group has been added as local
  366. *
  367. * @type function
  368. * @date 10/03/2014
  369. * @since 5.0.0
  370. *
  371. * @param $key (string)
  372. * @return (bolean)
  373. */
  374. function acf_is_local_field_group( $key ) {
  375. // validate
  376. if( !acf_is_local_enabled() ) {
  377. return false;
  378. }
  379. // check groups
  380. if( isset( acf_local()->groups[ $key ] ) ) {
  381. return true;
  382. }
  383. // return
  384. return false;
  385. }
  386. /*
  387. * acf_get_local_field_group
  388. *
  389. * This function will return a local field group for a given key
  390. *
  391. * @type function
  392. * @date 10/03/2014
  393. * @since 5.0.0
  394. *
  395. * @param $key (string)
  396. * @return (bolean)
  397. */
  398. function acf_get_local_field_group( $key ) {
  399. // bail early if no group
  400. if( !acf_is_local_field_group($key) ) {
  401. return false;
  402. }
  403. // return
  404. return acf_local()->groups[ $key ];
  405. }
  406. /*
  407. * acf_add_local_field
  408. *
  409. * This function will add a $field to the local placeholder
  410. *
  411. * @type function
  412. * @date 10/03/2014
  413. * @since 5.0.0
  414. *
  415. * @param $post_id (int)
  416. * @return $post_id (int)
  417. */
  418. function acf_add_local_field( $field ) {
  419. acf_local()->add_field( $field );
  420. }
  421. /*
  422. * acf_remove_local_field
  423. *
  424. * This function will remove a $field to the local placeholder
  425. *
  426. * @type function
  427. * @date 10/03/2014
  428. * @since 5.0.0
  429. *
  430. * @param $post_id (int)
  431. * @return $post_id (int)
  432. */
  433. function acf_remove_local_field( $key ) {
  434. acf_local()->remove_field( $key );
  435. }
  436. /*
  437. * acf_is_local_field
  438. *
  439. * This function will return true if the field has been added as local
  440. *
  441. * @type function
  442. * @date 10/03/2014
  443. * @since 5.0.0
  444. *
  445. * @param $key (string)
  446. * @return (bolean)
  447. */
  448. function acf_is_local_field( $key ) {
  449. // validate
  450. if( !acf_is_local_enabled() ) {
  451. return false;
  452. }
  453. // check fields
  454. if( isset( acf_local()->fields[ $key ] ) ) {
  455. return true;
  456. }
  457. // return
  458. return false;
  459. }
  460. /*
  461. * acf_get_local_field_group
  462. *
  463. * This function will return a local field for a given key
  464. *
  465. * @type function
  466. * @date 10/03/2014
  467. * @since 5.0.0
  468. *
  469. * @param $key (string)
  470. * @return (bolean)
  471. */
  472. function acf_get_local_field( $key ) {
  473. // bail early if no field
  474. if( !acf_is_local_field($key) ) {
  475. return false;
  476. }
  477. // return
  478. return acf_local()->fields[ $key ];
  479. }
  480. /*
  481. * acf_count_local_fields
  482. *
  483. * This function will return the number of local fields for a parent
  484. *
  485. * @type function
  486. * @date 3/12/2014
  487. * @since 5.1.5
  488. *
  489. * @param n/a
  490. * @return (int)
  491. */
  492. function acf_count_local_fields( $key ) {
  493. // check for fields
  494. if( !empty(acf_local()->parents[ $key ]) ) {
  495. return count( acf_local()->parents[ $key ] );
  496. }
  497. // return
  498. return 0;
  499. }
  500. /*
  501. * acf_have_local_fields
  502. *
  503. * This function will return true if fields exist for a given 'parent' key (field group key or field key)
  504. *
  505. * @type function
  506. * @date 10/03/2014
  507. * @since 5.0.0
  508. *
  509. * @param $key (string)
  510. * @return (bolean)
  511. */
  512. function acf_have_local_fields( $key ) {
  513. // validate
  514. if( !acf_is_local_enabled() ) {
  515. return false;
  516. }
  517. // check parents
  518. if( isset( acf_local()->parents[ $key ] ) ) {
  519. return true;
  520. }
  521. // return
  522. return false;
  523. }
  524. /*
  525. * acf_get_local_fields
  526. *
  527. * This function will return an array of fields for a given 'parent' key (field group key or field key)
  528. *
  529. * @type function
  530. * @date 10/03/2014
  531. * @since 5.0.0
  532. *
  533. * @param $key (string)
  534. * @return (bolean)
  535. */
  536. function acf_get_local_fields( $parent ) {
  537. // bail early if no parent
  538. if( !acf_have_local_fields($parent) ) {
  539. return false;
  540. }
  541. // vars
  542. $fields = array();
  543. // append
  544. foreach( acf_local()->parents[ $parent ] as $key ) {
  545. $fields[] = acf_get_field( $key );
  546. }
  547. // return
  548. return $fields;
  549. }
  550. /*
  551. * acf_remove_local_fields
  552. *
  553. * This function will remove the field reference for a field group
  554. *
  555. * @type function
  556. * @date 10/03/2014
  557. * @since 5.0.0
  558. *
  559. * @param $key (string)
  560. * @return (bolean)
  561. */
  562. function acf_remove_local_fields( $parent ) {
  563. // bail early if no reference
  564. if( empty( acf_local()->parents[ $parent ] ) ) {
  565. return false;
  566. }
  567. foreach( acf_local()->parents[ $parent ] as $key ) {
  568. acf_remove_local_field( $key );
  569. }
  570. // return
  571. return true;
  572. }
  573. ?>