Нет описания

Fields.php 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Utils_Fields
  10. */
  11. class PUM_Utils_Fields {
  12. /**
  13. * @param $fields
  14. * @param $field_id
  15. *
  16. * @return bool|mixed
  17. */
  18. public static function get_field( $fields, $field_id ) {
  19. $fields = static::flatten_fields_array( $fields );
  20. return isset( $fields[ $field_id ] ) ? $fields[ $field_id ] : false;
  21. }
  22. /**
  23. * @param array $fields
  24. *
  25. * @return array
  26. */
  27. public static function get_form_default_values( $fields = array() ) {
  28. $fields = static::flatten_fields_array( $fields );
  29. return static::get_field_default_values( $fields );
  30. }
  31. /**
  32. * @param array $fields
  33. *
  34. * @return array
  35. */
  36. public static function get_field_default_values( $fields = array() ) {
  37. $defaults = array();
  38. foreach ( $fields as $field_id => $field ) {
  39. switch( $field['type'] ) {
  40. case 'checkbox':
  41. $defaults[ $field_id ] = ! empty( $field['std'] ) ? $field['std'] : false;
  42. break;
  43. default:
  44. $defaults[ $field_id ] = isset( $field['std'] ) ? $field['std'] : null;
  45. }
  46. }
  47. return $defaults;
  48. }
  49. /**
  50. * @param $tabs
  51. *
  52. * @return array
  53. */
  54. public static function flatten_fields_array( $tabs ) {
  55. $fields = array();
  56. foreach ( $tabs as $tab_id => $tab_sections ) {
  57. if ( self::is_field( $tab_sections ) ) {
  58. $fields[ $tab_id ] = $tab_sections;
  59. continue;
  60. } else {
  61. foreach ( $tab_sections as $section_id => $section_fields ) {
  62. if ( self::is_field( $tab_sections ) ) {
  63. $fields[ $section_id ] = $section_fields;
  64. continue;
  65. }
  66. foreach ( $section_fields as $field_id => $field ) {
  67. $fields[ $field_id ] = $field;
  68. continue;
  69. }
  70. }
  71. }
  72. }
  73. return $fields;
  74. }
  75. /**
  76. * @param $field
  77. *
  78. * @return array
  79. */
  80. public static function parse_field( $field ) {
  81. return wp_parse_args( $field, array(
  82. 'section' => 'main',
  83. 'type' => 'text',
  84. 'id' => null,
  85. 'label' => '',
  86. 'desc' => '',
  87. 'name' => null,
  88. 'templ_name' => null,
  89. 'size' => 'regular',
  90. 'options' => array(),
  91. 'std' => null,
  92. 'rows' => 5,
  93. 'cols' => 50,
  94. 'min' => 0,
  95. 'max' => 50,
  96. 'force_minmax' => false,
  97. 'step' => 1,
  98. 'select2' => null,
  99. 'object_type' => 'post_type',
  100. 'object_key' => 'post',
  101. 'post_type' => null,
  102. 'taxonomy' => null,
  103. 'multiple' => null,
  104. 'as_array' => false,
  105. 'placeholder' => null,
  106. 'checkbox_val' => 1,
  107. 'allow_blank' => true,
  108. 'readonly' => false,
  109. 'required' => false,
  110. 'disabled' => false,
  111. 'hook' => null,
  112. 'unit' => __( 'ms', 'popup-maker' ),
  113. 'desc_position' => 'bottom',
  114. 'units' => array(
  115. 'px' => 'px',
  116. '%' => '%',
  117. 'em' => 'em',
  118. 'rem' => 'rem',
  119. ),
  120. 'priority' => 10,
  121. 'doclink' => '',
  122. 'button_type' => 'submit',
  123. 'class' => '',
  124. 'messages' => array(),
  125. 'license_status' => '',
  126. 'value' => null,
  127. 'private' => false,
  128. ) );
  129. }
  130. /**
  131. * @param $fields
  132. * @param array $args
  133. *
  134. * @return mixed
  135. */
  136. public static function parse_tab_fields( $fields, $args = array() ) {
  137. $args = wp_parse_args( $args, array(
  138. 'has_sections' => false,
  139. 'name' => '%s',
  140. ) );
  141. if ( $args['has_sections'] ) {
  142. foreach ( $fields as $tab_id => $tab_sections ) {
  143. foreach ( $tab_sections as $section_id => $section_fields ) {
  144. if ( self::is_field( $section_fields ) ) {
  145. // Allow for flat tabs with no sections.
  146. $section_id = 'main';
  147. $section_fields = array(
  148. $section_id => $section_fields,
  149. );
  150. }
  151. $fields[ $tab_id ][ $section_id ] = self::parse_fields( $section_fields, $args['name'] );
  152. }
  153. }
  154. } else {
  155. foreach ( $fields as $tab_id => $tab_fields ) {
  156. $fields[ $tab_id ] = self::parse_fields( $tab_fields, $args['name'] );
  157. }
  158. }
  159. return $fields;
  160. }
  161. /**
  162. * @param array $fields
  163. * @param string $name
  164. *
  165. * @return mixed
  166. */
  167. public static function parse_fields( $fields, $name = '%' ) {
  168. if ( is_array( $fields ) && ! empty( $fields ) ) {
  169. foreach ( $fields as $field_id => $field ) {
  170. if ( ! is_array( $field ) || ! self::is_field( $field ) ) {
  171. continue;
  172. }
  173. // Remap old settings.
  174. if ( is_numeric( $field_id ) && ! empty( $field['id'] ) ) {
  175. try {
  176. $fields = PUM_Utils_Array::replace_key( $fields, $field_id, $field['id'] );
  177. } catch ( Exception $e ) {
  178. }
  179. $field_id = $field['id'];
  180. } elseif ( empty( $field['id'] ) && ! is_numeric( $field_id ) ) {
  181. $field['id'] = $field_id;
  182. }
  183. if ( empty( $field['name'] ) ) {
  184. $field['name'] = sprintf( $name, $field_id );
  185. }
  186. $fields[ $field_id ] = self::parse_field( $field );
  187. }
  188. }
  189. $fields = PUM_Utils_Array::sort( $fields, 'priority' );
  190. return $fields;
  191. }
  192. /**
  193. * Checks if an array is a field.
  194. *
  195. * @param array $array
  196. *
  197. * @return bool
  198. */
  199. public static function is_field( $array = array() ) {
  200. $field_tests = array(
  201. ! isset( $array['type'] ) && ( isset( $array['label'] ) || isset( $array['desc'] ) ),
  202. isset( $array['type'] ) && is_string( $array['type'] ),
  203. );
  204. return in_array( true, $field_tests );
  205. }
  206. /**
  207. * Checks if an array is a section.
  208. *
  209. * @param array $array
  210. *
  211. * @return bool
  212. */
  213. public static function is_section( $array = array() ) {
  214. return ! self::is_field( $array );
  215. }
  216. /**
  217. * @param array $args
  218. */
  219. public static function render_field( $args = array() ) {
  220. $args = static::parse_field( $args );
  221. // If no type default to text.
  222. $type = ! empty( $args['type'] ) ? $args['type'] : 'text';
  223. /**
  224. * Check if any actions hooked to this type of field and load run those.
  225. */
  226. if ( has_action( "pum_{$type}_field" ) ) {
  227. do_action( "pum_{$type}_field", $args );
  228. } else {
  229. if ( method_exists( 'PUM_Form_Fields', $type . '_callback' ) ) {
  230. /**
  231. * Check if renderer method exists and load that.
  232. */
  233. $function_name = array( 'PUM_Form_Fields', $type . '_callback' );
  234. } else if ( function_exists( "pum_{$type}_callback" ) ) {
  235. /**
  236. * Check if function exists and load that.
  237. */
  238. $function_name = "pum_{$type}_callback";
  239. } else {
  240. /**
  241. * No method exists, lets notify them the field type doesn't exist.
  242. */
  243. $function_name = array( 'PUM_Form_Fields', 'missing_callback' );
  244. }
  245. /**
  246. * Call the determined method, passing the field args & $value to the callback.
  247. */
  248. call_user_func_array( $function_name, array( $args ) );
  249. }
  250. }
  251. /**
  252. * @param PUM_Form $form
  253. */
  254. public static function render_form_fields( $form ) {
  255. $tabs = $form->get_tabs();
  256. $sections = $form->get_sections();
  257. $fields = $form->get_fields();
  258. if ( $form->has_tabs() ) {
  259. if ( $form->has_sections() ) {
  260. foreach ( $tabs as $tab_id => $tab_label ) {
  261. foreach ( $sections as $section_id => $section_label ) {
  262. foreach ( $fields[ $tab_id ][ $section_id ] as $field_id => $field_args ) {
  263. static::render_field( $field_args );
  264. }
  265. }
  266. }
  267. } else {
  268. foreach ( $tabs as $tab_id => $label ) {
  269. foreach ( $fields[ $tab_id ] as $field_id => $field_args ) {
  270. static::render_field( $field_args );
  271. }
  272. }
  273. }
  274. } else {
  275. foreach ( $fields as $field_id => $field_args ) {
  276. static::render_field( $field_args );
  277. }
  278. }
  279. }
  280. /**
  281. * Sanitizes an array of field values.
  282. *
  283. * @param $fields
  284. * @param $values
  285. *
  286. * @return mixed
  287. */
  288. public static function sanitize_fields( $values, $fields = array() ) {
  289. foreach ( $values as $key => $value ) {
  290. if ( is_string( $value ) ) {
  291. $values[ $key ] = sanitize_text_field( $value );
  292. }
  293. $field = self::get_field( $fields, $key );
  294. if ( $field ) {
  295. $values[ $key ] = self::sanitize_field( $field, $value );
  296. }
  297. }
  298. return $values;
  299. }
  300. /**
  301. * @param array $args
  302. * @param mixed $value
  303. * @param array $fields
  304. * @param array $values
  305. *
  306. * @return mixed|null
  307. */
  308. public static function sanitize_field( $args, $value = null, $fields = array(), $values = array() ) {
  309. // If no type default to text.
  310. $type = ! empty( $args['type'] ) ? $args['type'] : 'text';
  311. /**
  312. * Check if any actions hooked to this type of field and load run those.
  313. */
  314. if ( has_filter( "pum_{$type}_sanitize" ) ) {
  315. $value = apply_filters( "pum_{$type}_sanitize", $value, $args, $fields, $values );
  316. } else {
  317. /**
  318. * Check if override or custom function exists and load that.
  319. */
  320. if ( function_exists( "pum_{$type}_sanitize" ) ) {
  321. $function_name = "pum_{$type}_sanitize";
  322. } /**
  323. * Check if core method exists and load that.
  324. */ elseif ( method_exists( 'PUM_Utils_Sanitize', $type ) ) {
  325. $function_name = array( 'PUM_Utils_Sanitize', $type );
  326. } else {
  327. $function_name = null;
  328. }
  329. if ( $function_name ) {
  330. /**
  331. * Call the determined method, passing the field args & $value to the callback.
  332. */
  333. $value = call_user_func_array( $function_name, array( $value, $args, $fields, $values ) );
  334. }
  335. }
  336. $value = apply_filters( 'pum_settings_sanitize', $value, $args, $fields, $values );
  337. return $value;
  338. }
  339. }