PageRenderTime 26ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Svyrydov/test-project
PHP | 903 lines | 226 code | 310 blank | 367 comment | 31 complexity | de231ef30e3825bc50eaf2439f7b0328 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_reset_local
  249. *
  250. * This function will remove (reset) all field group and fields
  251. *
  252. * @type function
  253. * @date 2/06/2016
  254. * @since 5.3.8
  255. *
  256. * @param $post_id (int)
  257. * @return $post_id (int)
  258. */
  259. function acf_reset_local() {
  260. // vars
  261. acf_local()->groups = array();
  262. acf_local()->fields = array();
  263. acf_local()->parents = array();
  264. }
  265. /*
  266. * acf_is_local_enabled
  267. *
  268. * This function will return true|false if the local functionality is enabled
  269. *
  270. * @type function
  271. * @date 11/06/2014
  272. * @since 5.0.0
  273. *
  274. * @param n/a
  275. * @return n/a
  276. */
  277. function acf_is_local_enabled() {
  278. // validate
  279. if( !acf_get_setting('local') ) {
  280. return false;
  281. }
  282. if( !acf_local()->enabled ) {
  283. return false;
  284. }
  285. // return
  286. return true;
  287. }
  288. /*
  289. * acf_count_local_field_groups
  290. *
  291. * This function will return the number of local field groups
  292. *
  293. * @type function
  294. * @date 3/12/2014
  295. * @since 5.1.5
  296. *
  297. * @param $type (string) specify the type. eg. 'json'
  298. * @return (int)
  299. */
  300. function acf_count_local_field_groups( $type = '' ) {
  301. // vars
  302. $count = 0;
  303. // check for groups
  304. if( !empty(acf_local()->groups) ) {
  305. // acf_local
  306. foreach( acf_local()->groups as $group ) {
  307. // ignore if not specific type
  308. if( $type && $group['local'] != $type ) {
  309. continue;
  310. }
  311. $count++;
  312. }
  313. }
  314. // return
  315. return $count;
  316. }
  317. /*
  318. * acf_have_local_field_groups
  319. *
  320. * This function will return true if fields exist for a given 'parent' key (field group key or field key)
  321. *
  322. * @type function
  323. * @date 10/03/2014
  324. * @since 5.0.0
  325. *
  326. * @param n/a
  327. * @return (bolean)
  328. */
  329. function acf_have_local_field_groups() {
  330. // validate
  331. if( !acf_is_local_enabled() ) {
  332. return false;
  333. }
  334. // check for groups
  335. if( !empty(acf_local()->groups) ) {
  336. return true;
  337. }
  338. // return
  339. return false;
  340. }
  341. /*
  342. * acf_get_local_field_groups
  343. *
  344. * This function will return an array of fields for a given 'parent' key (field group key or field key)
  345. *
  346. * @type function
  347. * @date 10/03/2014
  348. * @since 5.0.0
  349. *
  350. * @param $key (string)
  351. * @return (bolean)
  352. */
  353. function acf_get_local_field_groups() {
  354. // bail early if no groups
  355. if( !acf_have_local_field_groups() ) {
  356. return false;
  357. }
  358. // vars
  359. $groups = array();
  360. // acf_local
  361. foreach( acf_local()->groups as $group ) {
  362. $groups[] = $group;
  363. }
  364. // return
  365. return $groups;
  366. }
  367. /*
  368. * acf_add_local_field_group
  369. *
  370. * This function will add a $field group to the local placeholder
  371. *
  372. * @type function
  373. * @date 10/03/2014
  374. * @since 5.0.0
  375. *
  376. * @param $post_id (int)
  377. * @return $post_id (int)
  378. */
  379. function acf_add_local_field_group( $field_group ) {
  380. acf_local()->add_field_group( $field_group );
  381. }
  382. /*
  383. * acf_is_local_field_group
  384. *
  385. * This function will return true if the field group has been added as local
  386. *
  387. * @type function
  388. * @date 10/03/2014
  389. * @since 5.0.0
  390. *
  391. * @param $key (string)
  392. * @return (bolean)
  393. */
  394. function acf_is_local_field_group( $key ) {
  395. // validate
  396. if( !acf_is_local_enabled() ) {
  397. return false;
  398. }
  399. // check groups
  400. if( isset( acf_local()->groups[ $key ] ) ) {
  401. return true;
  402. }
  403. // return
  404. return false;
  405. }
  406. /*
  407. * acf_get_local_field_group
  408. *
  409. * This function will return a local field group for a given key
  410. *
  411. * @type function
  412. * @date 10/03/2014
  413. * @since 5.0.0
  414. *
  415. * @param $key (string)
  416. * @return (bolean)
  417. */
  418. function acf_get_local_field_group( $key ) {
  419. // bail early if no group
  420. if( !acf_is_local_field_group($key) ) {
  421. return false;
  422. }
  423. // return
  424. return acf_local()->groups[ $key ];
  425. }
  426. /*
  427. * acf_add_local_field
  428. *
  429. * This function will add a $field to the local placeholder
  430. *
  431. * @type function
  432. * @date 10/03/2014
  433. * @since 5.0.0
  434. *
  435. * @param $post_id (int)
  436. * @return $post_id (int)
  437. */
  438. function acf_add_local_field( $field ) {
  439. acf_local()->add_field( $field );
  440. }
  441. /*
  442. * acf_remove_local_field
  443. *
  444. * This function will remove a $field to the local placeholder
  445. *
  446. * @type function
  447. * @date 10/03/2014
  448. * @since 5.0.0
  449. *
  450. * @param $post_id (int)
  451. * @return $post_id (int)
  452. */
  453. function acf_remove_local_field( $key ) {
  454. acf_local()->remove_field( $key );
  455. }
  456. /*
  457. * acf_is_local_field
  458. *
  459. * This function will return true if the field has been added as local
  460. *
  461. * @type function
  462. * @date 10/03/2014
  463. * @since 5.0.0
  464. *
  465. * @param $key (string)
  466. * @return (bolean)
  467. */
  468. function acf_is_local_field( $key ) {
  469. // validate
  470. if( !acf_is_local_enabled() ) {
  471. return false;
  472. }
  473. // check fields
  474. if( isset( acf_local()->fields[ $key ] ) ) {
  475. return true;
  476. }
  477. // return
  478. return false;
  479. }
  480. /*
  481. * acf_get_local_field_group
  482. *
  483. * This function will return a local field for a given key
  484. *
  485. * @type function
  486. * @date 10/03/2014
  487. * @since 5.0.0
  488. *
  489. * @param $key (string)
  490. * @return (bolean)
  491. */
  492. function acf_get_local_field( $key ) {
  493. // bail early if no field
  494. if( !acf_is_local_field($key) ) {
  495. return false;
  496. }
  497. // return
  498. return acf_local()->fields[ $key ];
  499. }
  500. /*
  501. * acf_count_local_fields
  502. *
  503. * This function will return the number of local fields for a parent
  504. *
  505. * @type function
  506. * @date 3/12/2014
  507. * @since 5.1.5
  508. *
  509. * @param n/a
  510. * @return (int)
  511. */
  512. function acf_count_local_fields( $key ) {
  513. // check for fields
  514. if( !empty(acf_local()->parents[ $key ]) ) {
  515. return count( acf_local()->parents[ $key ] );
  516. }
  517. // return
  518. return 0;
  519. }
  520. /*
  521. * acf_have_local_fields
  522. *
  523. * This function will return true if fields exist for a given 'parent' key (field group key or field key)
  524. *
  525. * @type function
  526. * @date 10/03/2014
  527. * @since 5.0.0
  528. *
  529. * @param $key (string)
  530. * @return (bolean)
  531. */
  532. function acf_have_local_fields( $key ) {
  533. // validate
  534. if( !acf_is_local_enabled() ) {
  535. return false;
  536. }
  537. // check parents
  538. if( isset( acf_local()->parents[ $key ] ) ) {
  539. return true;
  540. }
  541. // return
  542. return false;
  543. }
  544. /*
  545. * acf_get_local_fields
  546. *
  547. * This function will return an array of fields for a given 'parent' key (field group key or field key)
  548. *
  549. * @type function
  550. * @date 10/03/2014
  551. * @since 5.0.0
  552. *
  553. * @param $key (string)
  554. * @return (bolean)
  555. */
  556. function acf_get_local_fields( $parent ) {
  557. // bail early if no parent
  558. if( !acf_have_local_fields($parent) ) {
  559. return false;
  560. }
  561. // vars
  562. $fields = array();
  563. // append
  564. foreach( acf_local()->parents[ $parent ] as $key ) {
  565. $fields[] = acf_get_field( $key );
  566. }
  567. // return
  568. return $fields;
  569. }
  570. /*
  571. * acf_remove_local_fields
  572. *
  573. * This function will remove the field reference for a field group
  574. *
  575. * @type function
  576. * @date 10/03/2014
  577. * @since 5.0.0
  578. *
  579. * @param $key (string)
  580. * @return (bolean)
  581. */
  582. function acf_remove_local_fields( $parent ) {
  583. // bail early if no reference
  584. if( empty( acf_local()->parents[ $parent ] ) ) {
  585. return false;
  586. }
  587. foreach( acf_local()->parents[ $parent ] as $key ) {
  588. acf_remove_local_field( $key );
  589. }
  590. // return
  591. return true;
  592. }
  593. ?>