compatibility.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'ACF_Compatibility' ) ) :
  6. class ACF_Compatibility {
  7. /**
  8. * __construct
  9. *
  10. * Sets up the class functionality.
  11. *
  12. * @date 30/04/2014
  13. * @since 5.0.0
  14. *
  15. * @param void
  16. * @return void
  17. */
  18. function __construct() {
  19. // actions
  20. add_filter( 'acf/validate_field', array( $this, 'validate_field' ), 20, 1 );
  21. add_filter( 'acf/validate_field/type=textarea', array( $this, 'validate_textarea_field' ), 20, 1 );
  22. add_filter( 'acf/validate_field/type=relationship', array( $this, 'validate_relationship_field' ), 20, 1 );
  23. add_filter( 'acf/validate_field/type=post_object', array( $this, 'validate_relationship_field' ), 20, 1 );
  24. add_filter( 'acf/validate_field/type=page_link', array( $this, 'validate_relationship_field' ), 20, 1 );
  25. add_filter( 'acf/validate_field/type=image', array( $this, 'validate_image_field' ), 20, 1 );
  26. add_filter( 'acf/validate_field/type=file', array( $this, 'validate_image_field' ), 20, 1 );
  27. add_filter( 'acf/validate_field/type=wysiwyg', array( $this, 'validate_wysiwyg_field' ), 20, 1 );
  28. add_filter( 'acf/validate_field/type=date_picker', array( $this, 'validate_date_picker_field' ), 20, 1 );
  29. add_filter( 'acf/validate_field/type=taxonomy', array( $this, 'validate_taxonomy_field' ), 20, 1 );
  30. add_filter( 'acf/validate_field/type=date_time_picker', array( $this, 'validate_date_time_picker_field' ), 20, 1 );
  31. add_filter( 'acf/validate_field/type=user', array( $this, 'validate_user_field' ), 20, 1 );
  32. add_filter( 'acf/validate_field_group', array( $this, 'validate_field_group' ), 20, 1 );
  33. // Modify field wrapper attributes
  34. add_filter( 'acf/field_wrapper_attributes', array( $this, 'field_wrapper_attributes' ), 20, 2 );
  35. // location
  36. add_filter( 'acf/location/validate_rule/type=post_taxonomy', array( $this, 'validate_post_taxonomy_location_rule' ), 20, 1 );
  37. add_filter( 'acf/location/validate_rule/type=post_category', array( $this, 'validate_post_taxonomy_location_rule' ), 20, 1 );
  38. // Update settings
  39. add_action( 'acf/init', array( $this, 'init' ) );
  40. }
  41. /**
  42. * init
  43. *
  44. * Adds compatibility for deprecated settings.
  45. *
  46. * @date 10/6/19
  47. * @since 5.8.1
  48. *
  49. * @param void
  50. * @return void
  51. */
  52. function init() {
  53. // Update "show_admin" setting based on defined constant.
  54. if ( defined( 'ACF_LITE' ) && ACF_LITE ) {
  55. acf_update_setting( 'show_admin', false );
  56. }
  57. }
  58. /**
  59. * field_wrapper_attributes
  60. *
  61. * Adds compatibility with deprecated field wrap attributes.
  62. *
  63. * @date 21/1/19
  64. * @since 5.7.10
  65. *
  66. * @param array $wrapper The wrapper attributes array.
  67. * @param array $field The field array.
  68. */
  69. function field_wrapper_attributes( $wrapper, $field ) {
  70. // Check compatibility setting.
  71. if ( acf_get_compatibility( 'field_wrapper_class' ) ) {
  72. $wrapper['class'] .= " field_type-{$field['type']}";
  73. if ( $field['key'] ) {
  74. $wrapper['class'] .= " field_key-{$field['key']}";
  75. }
  76. }
  77. // Return wrapper.
  78. return $wrapper;
  79. }
  80. /**
  81. * validate_field
  82. *
  83. * Adds compatibility with deprecated settings
  84. *
  85. * @date 23/04/2014
  86. * @since 5.0.0
  87. *
  88. * @param array $field The field array.
  89. * @return array $field
  90. */
  91. function validate_field( $field ) {
  92. // conditional logic data structure changed to groups in version 5.0.0
  93. // convert previous data (status, rules, allorany) into groups
  94. if ( isset( $field['conditional_logic']['status'] ) ) {
  95. // check status
  96. if ( $field['conditional_logic']['status'] ) {
  97. $field['conditional_logic'] = acf_convert_rules_to_groups( $field['conditional_logic']['rules'], $field['conditional_logic']['allorany'] );
  98. } else {
  99. $field['conditional_logic'] = 0;
  100. }
  101. }
  102. // return
  103. return $field;
  104. }
  105. /**
  106. * validate_textarea_field
  107. *
  108. * Adds compatibility with deprecated settings
  109. *
  110. * @date 23/04/2014
  111. * @since 5.0.0
  112. *
  113. * @param array $field The field array.
  114. * @return array $field
  115. */
  116. function validate_textarea_field( $field ) {
  117. // formatting has been removed
  118. $formatting = acf_extract_var( $field, 'formatting' );
  119. if ( $formatting === 'br' ) {
  120. $field['new_lines'] = 'br';
  121. }
  122. // return
  123. return $field;
  124. }
  125. /**
  126. * validate_relationship_field
  127. *
  128. * Adds compatibility with deprecated settings
  129. *
  130. * @date 23/04/2014
  131. * @since 5.0.0
  132. *
  133. * @param array $field The field array.
  134. * @return array $field
  135. */
  136. function validate_relationship_field( $field ) {
  137. // remove 'all' from post_type
  138. if ( acf_in_array( 'all', $field['post_type'] ) ) {
  139. $field['post_type'] = array();
  140. }
  141. // remove 'all' from taxonomy
  142. if ( acf_in_array( 'all', $field['taxonomy'] ) ) {
  143. $field['taxonomy'] = array();
  144. }
  145. // result_elements is now elements
  146. if ( isset( $field['result_elements'] ) ) {
  147. $field['elements'] = acf_extract_var( $field, 'result_elements' );
  148. }
  149. // return
  150. return $field;
  151. }
  152. /**
  153. * validate_image_field
  154. *
  155. * Adds compatibility with deprecated settings
  156. *
  157. * @date 23/04/2014
  158. * @since 5.0.0
  159. *
  160. * @param array $field The field array.
  161. * @return array $field
  162. */
  163. function validate_image_field( $field ) {
  164. // save_format is now return_format
  165. if ( isset( $field['save_format'] ) ) {
  166. $field['return_format'] = acf_extract_var( $field, 'save_format' );
  167. }
  168. // object is now array
  169. if ( $field['return_format'] == 'object' ) {
  170. $field['return_format'] = 'array';
  171. }
  172. // return
  173. return $field;
  174. }
  175. /**
  176. * validate_wysiwyg_field
  177. *
  178. * Adds compatibility with deprecated settings
  179. *
  180. * @date 23/04/2014
  181. * @since 5.0.0
  182. *
  183. * @param array $field The field array.
  184. * @return array $field
  185. */
  186. function validate_wysiwyg_field( $field ) {
  187. // media_upload is now numeric
  188. if ( $field['media_upload'] === 'yes' ) {
  189. $field['media_upload'] = 1;
  190. } elseif ( $field['media_upload'] === 'no' ) {
  191. $field['media_upload'] = 0;
  192. }
  193. // return
  194. return $field;
  195. }
  196. /**
  197. * validate_date_picker_field
  198. *
  199. * Adds compatibility with deprecated settings
  200. *
  201. * @date 23/04/2014
  202. * @since 5.0.0
  203. *
  204. * @param array $field The field array.
  205. * @return array $field
  206. */
  207. function validate_date_picker_field( $field ) {
  208. // date_format has changed to display_format
  209. if ( isset( $field['date_format'] ) ) {
  210. // extract vars
  211. $date_format = $field['date_format'];
  212. $display_format = $field['display_format'];
  213. // convert from js to php
  214. $display_format = acf_convert_date_to_php( $display_format );
  215. // append settings
  216. $field['display_format'] = $display_format;
  217. $field['save_format'] = $date_format;
  218. // clean up
  219. unset( $field['date_format'] );
  220. }
  221. // return
  222. return $field;
  223. }
  224. /**
  225. * validate_taxonomy_field
  226. *
  227. * Adds compatibility with deprecated settings
  228. *
  229. * @date 23/04/2014
  230. * @since 5.2.7
  231. *
  232. * @param array $field The field array.
  233. * @return array $field
  234. */
  235. function validate_taxonomy_field( $field ) {
  236. // load_save_terms deprecated in favour of separate save_terms
  237. if ( isset( $field['load_save_terms'] ) ) {
  238. $field['save_terms'] = acf_extract_var( $field, 'load_save_terms' );
  239. }
  240. // return
  241. return $field;
  242. }
  243. /**
  244. * validate_date_time_picker_field
  245. *
  246. * Adds compatibility with deprecated settings
  247. *
  248. * @date 23/04/2014
  249. * @since 5.2.7
  250. *
  251. * @param array $field The field array.
  252. * @return array $field
  253. */
  254. function validate_date_time_picker_field( $field ) {
  255. // 3rd party date time picker
  256. // https://github.com/soderlind/acf-field-date-time-picker
  257. if ( ! empty( $field['time_format'] ) ) {
  258. // extract vars
  259. $time_format = acf_extract_var( $field, 'time_format' );
  260. $date_format = acf_extract_var( $field, 'date_format' );
  261. $get_as_timestamp = acf_extract_var( $field, 'get_as_timestamp' );
  262. // convert from js to php
  263. $time_format = acf_convert_time_to_php( $time_format );
  264. $date_format = acf_convert_date_to_php( $date_format );
  265. // append settings
  266. $field['return_format'] = $date_format . ' ' . $time_format;
  267. $field['display_format'] = $date_format . ' ' . $time_format;
  268. // timestamp
  269. if ( $get_as_timestamp === 'true' ) {
  270. $field['return_format'] = 'U';
  271. }
  272. }
  273. // return
  274. return $field;
  275. }
  276. /**
  277. * validate_user_field
  278. *
  279. * Adds compatibility with deprecated settings
  280. *
  281. * @date 23/04/2014
  282. * @since 5.2.7
  283. *
  284. * @param array $field The field array.
  285. * @return array $field
  286. */
  287. function validate_user_field( $field ) {
  288. // remove 'all' from roles
  289. if ( acf_in_array( 'all', $field['role'] ) ) {
  290. $field['role'] = '';
  291. }
  292. // field_type removed in favour of multiple
  293. if ( isset( $field['field_type'] ) ) {
  294. // extract vars
  295. $field_type = acf_extract_var( $field, 'field_type' );
  296. // multiple
  297. if ( $field_type === 'multi_select' ) {
  298. $field['multiple'] = true;
  299. }
  300. }
  301. // return
  302. return $field;
  303. }
  304. /*
  305. * validate_field_group
  306. *
  307. * This function will provide compatibility with ACF4 field groups
  308. *
  309. * @type function
  310. * @date 23/04/2014
  311. * @since 5.0.0
  312. *
  313. * @param $field_group (array)
  314. * @return $field_group
  315. */
  316. function validate_field_group( $field_group ) {
  317. // vars
  318. $version = 5;
  319. // field group key was added in version 5.0.0
  320. // detect ACF4 data and generate key
  321. if ( ! $field_group['key'] ) {
  322. $version = 4;
  323. $field_group['key'] = isset( $field_group['id'] ) ? "group_{$field_group['id']}" : uniqid( 'group_' );
  324. }
  325. // prior to version 5.0.0, settings were saved in an 'options' array
  326. // extract and merge options into the field group
  327. if ( isset( $field_group['options'] ) ) {
  328. $options = acf_extract_var( $field_group, 'options' );
  329. $field_group = array_merge( $field_group, $options );
  330. }
  331. // location data structure changed to groups in version 4.1.0
  332. // convert previous data (rules, allorany) into groups
  333. if ( isset( $field_group['location']['rules'] ) ) {
  334. $field_group['location'] = acf_convert_rules_to_groups( $field_group['location']['rules'], $field_group['location']['allorany'] );
  335. }
  336. // some location rule names have changed in version 5.0.0
  337. // loop over location data and modify rules
  338. $replace = array(
  339. 'taxonomy' => 'post_taxonomy',
  340. 'ef_media' => 'attachment',
  341. 'ef_taxonomy' => 'taxonomy',
  342. 'ef_user' => 'user_role',
  343. 'user_type' => 'current_user_role', // 5.2.0
  344. );
  345. // only replace 'taxonomy' rule if is an ACF4 field group
  346. if ( $version > 4 ) {
  347. unset( $replace['taxonomy'] );
  348. }
  349. // loop over location groups
  350. if ( $field_group['location'] ) {
  351. foreach ( $field_group['location'] as $i => $group ) {
  352. // loop over group rules
  353. if ( $group ) {
  354. foreach ( $group as $j => $rule ) {
  355. // migrate param
  356. if ( isset( $replace[ $rule['param'] ] ) ) {
  357. $field_group['location'][ $i ][ $j ]['param'] = $replace[ $rule['param'] ];
  358. }
  359. }
  360. }
  361. }
  362. }
  363. // change layout to style (v5.0.0)
  364. if ( isset( $field_group['layout'] ) ) {
  365. $field_group['style'] = acf_extract_var( $field_group, 'layout' );
  366. }
  367. // change no_box to seamless (v5.0.0)
  368. if ( $field_group['style'] === 'no_box' ) {
  369. $field_group['style'] = 'seamless';
  370. }
  371. // return
  372. return $field_group;
  373. }
  374. /**
  375. * validate_post_taxonomy_location_rule
  376. *
  377. * description
  378. *
  379. * @date 27/8/18
  380. * @since 5.7.4
  381. *
  382. * @param type $var Description. Default.
  383. * @return type Description.
  384. */
  385. function validate_post_taxonomy_location_rule( $rule ) {
  386. // previous versions of ACF (v4.4.12) saved value as term_id
  387. // convert term_id into "taxonomy:slug" string
  388. if ( is_numeric( $rule['value'] ) ) {
  389. $term = acf_get_term( $rule['value'] );
  390. if ( $term ) {
  391. $rule['value'] = acf_encode_term( $term );
  392. }
  393. }
  394. // return
  395. return $rule;
  396. }
  397. }
  398. acf_new_instance( 'ACF_Compatibility' );
  399. endif; // class_exists check
  400. /*
  401. * acf_get_compatibility
  402. *
  403. * Returns true if compatibility is enabled for the given component.
  404. *
  405. * @date 20/1/15
  406. * @since 5.1.5
  407. *
  408. * @param string $name The name of the component to check.
  409. * @return bool
  410. */
  411. function acf_get_compatibility( $name ) {
  412. return apply_filters( "acf/compatibility/{$name}", false );
  413. }