PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/hschoenburg/tlworks2
PHP | 880 lines | 221 code | 305 blank | 354 comment | 31 complexity | b705d67ba4cd450884fb0fe887dda6b7 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. // - delete cache was origional added to ensure changes to JSON / PHP would appear in WP when using memcache
  142. // - the downside is that wp_cache_delet is taxing on the system so has been commented out
  143. //wp_cache_delete( "get_field/key={$key}", 'acf' );
  144. //wp_cache_delete( "get_fields/parent={$parent}", 'acf' );
  145. }
  146. /*
  147. * remove_field
  148. *
  149. * This function will remove a $field to the local placeholder
  150. *
  151. * @type function
  152. * @date 10/03/2014
  153. * @since 5.0.0
  154. *
  155. * @param $key (string)
  156. * @return n/a
  157. */
  158. function remove_field( $key ) {
  159. // get field
  160. $field = acf_get_field( $key );
  161. // remove parent reference
  162. $this->remove_parent_reference( $field['parent'], $field['key'] );
  163. // remove field
  164. unset( $this->fields[ $key ] );
  165. // remove children
  166. if( acf_have_local_fields( $key) ) {
  167. acf_remove_local_fields( $key );
  168. }
  169. }
  170. function add_parent_reference( $parent_key, $field_key ) {
  171. // create array
  172. if( !isset($this->parents[ $parent_key ]) ) {
  173. $this->parents[ $parent_key ] = array();
  174. } elseif( in_array($field_key, $this->parents[ $parent_key ]) ) {
  175. // bail early if already in array
  176. return false;
  177. }
  178. // append
  179. $this->parents[ $parent_key ][] = $field_key;
  180. // return
  181. return true;
  182. }
  183. function remove_parent_reference( $parent_key, $field_key ) {
  184. // bail early if no parent
  185. if( !isset($this->parents[ $parent_key ]) ) {
  186. return false;
  187. }
  188. // remove
  189. $this->parents[ $parent_key ] = array_diff($this->parents[ $parent_key ], array($field_key));
  190. // return
  191. return true;
  192. }
  193. }
  194. /*
  195. * acf_local
  196. *
  197. * This function will return the one true acf_local
  198. *
  199. * @type function
  200. * @date 10/03/2014
  201. * @since 5.0.0
  202. *
  203. * @param n/a
  204. * @return acf_local (object)
  205. */
  206. function acf_local() {
  207. // globals
  208. global $acf_local;
  209. // instantiate
  210. if( !isset($acf_local) )
  211. {
  212. $acf_local = new acf_local();
  213. }
  214. // return
  215. return $acf_local;
  216. }
  217. /*
  218. * acf_disable_local
  219. *
  220. * This function will disable the local functionality for DB only interaction
  221. *
  222. * @type function
  223. * @date 11/06/2014
  224. * @since 5.0.0
  225. *
  226. * @param n/a
  227. * @return n/a
  228. */
  229. function acf_disable_local() {
  230. acf_local()->enabled = false;
  231. }
  232. /*
  233. * acf_enable_local
  234. *
  235. * This function will enable the local functionality
  236. *
  237. * @type function
  238. * @date 11/06/2014
  239. * @since 5.0.0
  240. *
  241. * @param n/a
  242. * @return n/a
  243. */
  244. function acf_enable_local() {
  245. acf_local()->enabled = true;
  246. }
  247. /*
  248. * acf_is_local_enabled
  249. *
  250. * This function will return true|false if the local functionality is enabled
  251. *
  252. * @type function
  253. * @date 11/06/2014
  254. * @since 5.0.0
  255. *
  256. * @param n/a
  257. * @return n/a
  258. */
  259. function acf_is_local_enabled() {
  260. // validate
  261. if( !acf_get_setting('local') ) {
  262. return false;
  263. }
  264. if( !acf_local()->enabled ) {
  265. return false;
  266. }
  267. // return
  268. return true;
  269. }
  270. /*
  271. * acf_count_local_field_groups
  272. *
  273. * This function will return the number of local field groups
  274. *
  275. * @type function
  276. * @date 3/12/2014
  277. * @since 5.1.5
  278. *
  279. * @param $type (string) specify the type. eg. 'json'
  280. * @return (int)
  281. */
  282. function acf_count_local_field_groups( $type = '' ) {
  283. // vars
  284. $count = 0;
  285. // check for groups
  286. if( !empty(acf_local()->groups) ) {
  287. // acf_local
  288. foreach( acf_local()->groups as $group ) {
  289. // ignore if not specific type
  290. if( $type && $group['local'] != $type ) {
  291. continue;
  292. }
  293. $count++;
  294. }
  295. }
  296. // return
  297. return $count;
  298. }
  299. /*
  300. * acf_have_local_field_groups
  301. *
  302. * This function will return true if fields exist for a given 'parent' key (field group key or field key)
  303. *
  304. * @type function
  305. * @date 10/03/2014
  306. * @since 5.0.0
  307. *
  308. * @param n/a
  309. * @return (bolean)
  310. */
  311. function acf_have_local_field_groups() {
  312. // validate
  313. if( !acf_is_local_enabled() ) {
  314. return false;
  315. }
  316. // check for groups
  317. if( !empty(acf_local()->groups) ) {
  318. return true;
  319. }
  320. // return
  321. return false;
  322. }
  323. /*
  324. * acf_get_local_field_groups
  325. *
  326. * This function will return an array of fields for a given 'parent' key (field group key or field key)
  327. *
  328. * @type function
  329. * @date 10/03/2014
  330. * @since 5.0.0
  331. *
  332. * @param $key (string)
  333. * @return (bolean)
  334. */
  335. function acf_get_local_field_groups() {
  336. // bail early if no groups
  337. if( !acf_have_local_field_groups() ) {
  338. return false;
  339. }
  340. // vars
  341. $groups = array();
  342. // acf_local
  343. foreach( acf_local()->groups as $group ) {
  344. $groups[] = $group;
  345. }
  346. // return
  347. return $groups;
  348. }
  349. /*
  350. * acf_add_local_field_group
  351. *
  352. * This function will add a $field group to the local placeholder
  353. *
  354. * @type function
  355. * @date 10/03/2014
  356. * @since 5.0.0
  357. *
  358. * @param $post_id (int)
  359. * @return $post_id (int)
  360. */
  361. function acf_add_local_field_group( $field_group ) {
  362. acf_local()->add_field_group( $field_group );
  363. }
  364. /*
  365. * acf_is_local_field_group
  366. *
  367. * This function will return true if the field group has been added as local
  368. *
  369. * @type function
  370. * @date 10/03/2014
  371. * @since 5.0.0
  372. *
  373. * @param $key (string)
  374. * @return (bolean)
  375. */
  376. function acf_is_local_field_group( $key ) {
  377. // validate
  378. if( !acf_is_local_enabled() ) {
  379. return false;
  380. }
  381. // check groups
  382. if( isset( acf_local()->groups[ $key ] ) ) {
  383. return true;
  384. }
  385. // return
  386. return false;
  387. }
  388. /*
  389. * acf_get_local_field_group
  390. *
  391. * This function will return a local field group for a given key
  392. *
  393. * @type function
  394. * @date 10/03/2014
  395. * @since 5.0.0
  396. *
  397. * @param $key (string)
  398. * @return (bolean)
  399. */
  400. function acf_get_local_field_group( $key ) {
  401. // bail early if no group
  402. if( !acf_is_local_field_group($key) ) {
  403. return false;
  404. }
  405. // return
  406. return acf_local()->groups[ $key ];
  407. }
  408. /*
  409. * acf_add_local_field
  410. *
  411. * This function will add a $field to the local placeholder
  412. *
  413. * @type function
  414. * @date 10/03/2014
  415. * @since 5.0.0
  416. *
  417. * @param $post_id (int)
  418. * @return $post_id (int)
  419. */
  420. function acf_add_local_field( $field ) {
  421. acf_local()->add_field( $field );
  422. }
  423. /*
  424. * acf_remove_local_field
  425. *
  426. * This function will remove a $field to the local placeholder
  427. *
  428. * @type function
  429. * @date 10/03/2014
  430. * @since 5.0.0
  431. *
  432. * @param $post_id (int)
  433. * @return $post_id (int)
  434. */
  435. function acf_remove_local_field( $key ) {
  436. acf_local()->remove_field( $key );
  437. }
  438. /*
  439. * acf_is_local_field
  440. *
  441. * This function will return true if the field has been added as local
  442. *
  443. * @type function
  444. * @date 10/03/2014
  445. * @since 5.0.0
  446. *
  447. * @param $key (string)
  448. * @return (bolean)
  449. */
  450. function acf_is_local_field( $key ) {
  451. // validate
  452. if( !acf_is_local_enabled() ) {
  453. return false;
  454. }
  455. // check fields
  456. if( isset( acf_local()->fields[ $key ] ) ) {
  457. return true;
  458. }
  459. // return
  460. return false;
  461. }
  462. /*
  463. * acf_get_local_field_group
  464. *
  465. * This function will return a local field for a given key
  466. *
  467. * @type function
  468. * @date 10/03/2014
  469. * @since 5.0.0
  470. *
  471. * @param $key (string)
  472. * @return (bolean)
  473. */
  474. function acf_get_local_field( $key ) {
  475. // bail early if no field
  476. if( !acf_is_local_field($key) ) {
  477. return false;
  478. }
  479. // return
  480. return acf_local()->fields[ $key ];
  481. }
  482. /*
  483. * acf_count_local_fields
  484. *
  485. * This function will return the number of local fields for a parent
  486. *
  487. * @type function
  488. * @date 3/12/2014
  489. * @since 5.1.5
  490. *
  491. * @param n/a
  492. * @return (int)
  493. */
  494. function acf_count_local_fields( $key ) {
  495. // check for fields
  496. if( !empty(acf_local()->parents[ $key ]) ) {
  497. return count( acf_local()->parents[ $key ] );
  498. }
  499. // return
  500. return 0;
  501. }
  502. /*
  503. * acf_have_local_fields
  504. *
  505. * This function will return true if fields exist for a given 'parent' key (field group key or field key)
  506. *
  507. * @type function
  508. * @date 10/03/2014
  509. * @since 5.0.0
  510. *
  511. * @param $key (string)
  512. * @return (bolean)
  513. */
  514. function acf_have_local_fields( $key ) {
  515. // validate
  516. if( !acf_is_local_enabled() ) {
  517. return false;
  518. }
  519. // check parents
  520. if( isset( acf_local()->parents[ $key ] ) ) {
  521. return true;
  522. }
  523. // return
  524. return false;
  525. }
  526. /*
  527. * acf_get_local_fields
  528. *
  529. * This function will return an array of fields for a given 'parent' key (field group key or field key)
  530. *
  531. * @type function
  532. * @date 10/03/2014
  533. * @since 5.0.0
  534. *
  535. * @param $key (string)
  536. * @return (bolean)
  537. */
  538. function acf_get_local_fields( $parent ) {
  539. // bail early if no parent
  540. if( !acf_have_local_fields($parent) ) {
  541. return false;
  542. }
  543. // vars
  544. $fields = array();
  545. // append
  546. foreach( acf_local()->parents[ $parent ] as $key ) {
  547. $fields[] = acf_get_field( $key );
  548. }
  549. // return
  550. return $fields;
  551. }
  552. /*
  553. * acf_remove_local_fields
  554. *
  555. * This function will remove the field reference for a field group
  556. *
  557. * @type function
  558. * @date 10/03/2014
  559. * @since 5.0.0
  560. *
  561. * @param $key (string)
  562. * @return (bolean)
  563. */
  564. function acf_remove_local_fields( $parent ) {
  565. // bail early if no reference
  566. if( empty( acf_local()->parents[ $parent ] ) ) {
  567. return false;
  568. }
  569. foreach( acf_local()->parents[ $parent ] as $key ) {
  570. acf_remove_local_field( $key );
  571. }
  572. // return
  573. return true;
  574. }
  575. ?>