PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

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