/wp-content/plugins/advanced-custom-fields-pro/pro/acf-pro.php

https://gitlab.com/Svyrydov/test-project · PHP · 407 lines · 110 code · 141 blank · 156 comment · 15 complexity · 810de81ae0bc77df03d5d1988d48cec3 MD5 · raw file

  1. <?php
  2. if( !class_exists('acf_pro') ):
  3. class acf_pro {
  4. /*
  5. * __construct
  6. *
  7. *
  8. *
  9. * @type function
  10. * @date 23/06/12
  11. * @since 5.0.0
  12. *
  13. * @param N/A
  14. * @return N/A
  15. */
  16. function __construct() {
  17. // update setting
  18. acf_update_setting( 'pro', true );
  19. acf_update_setting( 'name', __('Advanced Custom Fields PRO', 'acf') );
  20. // api
  21. acf_include('pro/api/api-pro.php');
  22. acf_include('pro/api/api-options-page.php');
  23. // updates
  24. acf_include('pro/core/updates.php');
  25. // admin
  26. if( is_admin() ) {
  27. // options page
  28. acf_include('pro/admin/options-page.php');
  29. // settings
  30. acf_include('pro/admin/settings-updates.php');
  31. }
  32. // actions
  33. add_action('init', array($this, 'register_assets'));
  34. add_action('acf/include_field_types', array($this, 'include_field_types'), 5);
  35. add_action('acf/input/admin_enqueue_scripts', array($this, 'input_admin_enqueue_scripts'));
  36. add_action('acf/field_group/admin_enqueue_scripts', array($this, 'field_group_admin_enqueue_scripts'));
  37. add_action('acf/field_group/admin_l10n', array($this, 'field_group_admin_l10n'));
  38. // filters
  39. add_filter('acf/get_valid_field', array($this, 'get_valid_field'), 11, 1);
  40. add_filter('acf/prepare_field_for_export', array($this, 'prepare_field_for_export'));
  41. add_filter('acf/prepare_field_for_import', array($this, 'prepare_field_for_import'));
  42. }
  43. /*
  44. * include_field_types
  45. *
  46. * description
  47. *
  48. * @type function
  49. * @date 21/10/2015
  50. * @since 5.2.3
  51. *
  52. * @param $post_id (int)
  53. * @return $post_id (int)
  54. */
  55. function include_field_types() {
  56. acf_include('pro/fields/repeater.php');
  57. acf_include('pro/fields/flexible-content.php');
  58. acf_include('pro/fields/gallery.php');
  59. }
  60. /*
  61. * get_valid_field
  62. *
  63. * This function will provide compatibility with ACF4 fields
  64. *
  65. * @type function
  66. * @date 23/04/2014
  67. * @since 5.0.0
  68. *
  69. * @param $field (array)
  70. * @return $field
  71. */
  72. function get_valid_field( $field ) {
  73. // extract old width
  74. $width = acf_extract_var( $field, 'column_width' );
  75. // if old width, update the new width
  76. if( $width ) {
  77. $field['wrapper']['width'] = $width;
  78. }
  79. // return
  80. return $field;
  81. }
  82. /*
  83. * register_assets
  84. *
  85. * description
  86. *
  87. * @type function
  88. * @date 4/11/2013
  89. * @since 5.0.0
  90. *
  91. * @param $post_id (int)
  92. * @return $post_id (int)
  93. */
  94. function register_assets() {
  95. // min
  96. $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
  97. // register scripts
  98. wp_register_script( 'acf-pro-input', acf_get_dir( "pro/assets/js/acf-pro-input{$min}.js" ), false, acf_get_setting('version') );
  99. wp_register_script( 'acf-pro-field-group', acf_get_dir( "pro/assets/js/acf-pro-field-group{$min}.js" ), false, acf_get_setting('version') );
  100. // register styles
  101. wp_register_style( 'acf-pro-input', acf_get_dir( 'pro/assets/css/acf-pro-input.css' ), false, acf_get_setting('version') );
  102. wp_register_style( 'acf-pro-field-group', acf_get_dir( 'pro/assets/css/acf-pro-field-group.css' ), false, acf_get_setting('version') );
  103. }
  104. /*
  105. * input_admin_enqueue_scripts
  106. *
  107. * description
  108. *
  109. * @type function
  110. * @date 4/11/2013
  111. * @since 5.0.0
  112. *
  113. * @param $post_id (int)
  114. * @return $post_id (int)
  115. */
  116. function input_admin_enqueue_scripts() {
  117. // scripts
  118. wp_enqueue_script('acf-pro-input');
  119. // styles
  120. wp_enqueue_style('acf-pro-input');
  121. }
  122. /*
  123. * field_group_admin_l10n
  124. *
  125. * description
  126. *
  127. * @type function
  128. * @date 1/05/2014
  129. * @since 5.0.0
  130. *
  131. * @param $post_id (int)
  132. * @return $post_id (int)
  133. */
  134. function field_group_admin_l10n( $l10n ) {
  135. // append
  136. $l10n['flexible_content'] = array(
  137. 'layout_warning' => __('Flexible Content requires at least 1 layout','acf')
  138. );
  139. // return
  140. return $l10n;
  141. }
  142. /*
  143. * field_group_admin_enqueue_scripts
  144. *
  145. * description
  146. *
  147. * @type function
  148. * @date 4/11/2013
  149. * @since 5.0.0
  150. *
  151. * @param $post_id (int)
  152. * @return $post_id (int)
  153. */
  154. function field_group_admin_enqueue_scripts() {
  155. // scripts
  156. wp_enqueue_script('acf-pro-field-group');
  157. // styles
  158. wp_enqueue_style('acf-pro-field-group');
  159. }
  160. /*
  161. * prepare_field_for_export
  162. *
  163. * description
  164. *
  165. * @type function
  166. * @date 11/03/2014
  167. * @since 5.0.0
  168. *
  169. * @param $post_id (int)
  170. * @return $post_id (int)
  171. */
  172. function prepare_field_for_export( $field ) {
  173. // sub field (parent_layout)
  174. acf_extract_var( $field, 'parent_layout');
  175. // repeater
  176. if( $field['type'] == 'repeater' ) {
  177. $field['sub_fields'] = acf_prepare_fields_for_export( $field['sub_fields'] );
  178. // flexible content
  179. } elseif( $field['type'] == 'flexible_content' ) {
  180. foreach( $field['layouts'] as $l => $layout ) {
  181. $field['layouts'][ $l ]['sub_fields'] = acf_prepare_fields_for_export( $layout['sub_fields'] );
  182. }
  183. }
  184. // return
  185. return $field;
  186. }
  187. /*
  188. * prepare_field_for_import
  189. *
  190. * description
  191. *
  192. * @type function
  193. * @date 11/03/2014
  194. * @since 5.0.0
  195. *
  196. * @param $post_id (int)
  197. * @return $post_id (int)
  198. */
  199. function prepare_field_for_import( $field ) {
  200. // var
  201. $extra = array();
  202. // sub fields
  203. if( $field['type'] == 'repeater' ) {
  204. // extract sub fields
  205. $sub_fields = acf_extract_var( $field, 'sub_fields');
  206. // reset field setting
  207. $field['sub_fields'] = array();
  208. if( !empty($sub_fields) ) {
  209. foreach( array_keys($sub_fields) as $i ) {
  210. // extract sub field
  211. $sub_field = acf_extract_var( $sub_fields, $i );
  212. // attributes
  213. $sub_field['parent'] = $field['key'];
  214. // append to extra
  215. $extra[] = $sub_field;
  216. }
  217. }
  218. } elseif( $field['type'] == 'flexible_content' ) {
  219. // extract layouts
  220. $layouts = acf_extract_var( $field, 'layouts');
  221. // reset field setting
  222. $field['layouts'] = array();
  223. // validate layouts
  224. if( !empty($layouts) ) {
  225. // loop over layouts
  226. foreach( array_keys($layouts) as $i ) {
  227. // extract layout
  228. $layout = acf_extract_var( $layouts, $i );
  229. // get valid layout (fixes ACF4 export code bug undefined index 'key')
  230. if( empty($layout['key']) ) {
  231. $layout['key'] = uniqid();
  232. }
  233. // extract sub fields
  234. $sub_fields = acf_extract_var( $layout, 'sub_fields');
  235. // validate sub fields
  236. if( !empty($sub_fields) ) {
  237. // loop over sub fields
  238. foreach( array_keys($sub_fields) as $j ) {
  239. // extract sub field
  240. $sub_field = acf_extract_var( $sub_fields, $j );
  241. // attributes
  242. $sub_field['parent'] = $field['key'];
  243. $sub_field['parent_layout'] = $layout['key'];
  244. // append to extra
  245. $extra[] = $sub_field;
  246. }
  247. }
  248. // append to layout
  249. $field['layouts'][] = $layout;
  250. }
  251. }
  252. }
  253. // extra
  254. if( !empty($extra) ) {
  255. array_unshift($extra, $field);
  256. return $extra;
  257. }
  258. // return
  259. return $field;
  260. }
  261. }
  262. // instantiate
  263. new acf_pro();
  264. // end class
  265. endif;
  266. ?>