Nessuna descrizione

Helpers.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Admin_Helpers
  10. */
  11. class PUM_Admin_Helpers {
  12. /**
  13. * @param array $args
  14. *
  15. * @return array
  16. */
  17. public static function post_type_dropdown_options( $args = array(), $compare = 'and' ) {
  18. $args = wp_parse_args( $args, array(
  19. 'public' => null,
  20. 'publicly_queryable' => null,
  21. 'exclude_from_search' => null,
  22. 'show_ui' => null,
  23. 'capability_type' => null,
  24. 'hierarchical' => null,
  25. 'menu_position' => null,
  26. 'menu_icon' => null,
  27. 'permalink_epmask' => null,
  28. 'rewrite' => null,
  29. 'query_var' => null,
  30. '_builtin' => null,
  31. ) );
  32. foreach( $args as $key => $value ) {
  33. if ( $value === null ) {
  34. unset( $args[ $key ] );
  35. }
  36. }
  37. $options = array();
  38. foreach ( get_post_types( $args, 'objects', $compare ) as $post_type ) {
  39. if ( in_array( $post_type->name, array( 'revision', 'nav_menu_item', 'custom_css', 'customize_changeset', 'oembed_cache', 'popup_theme', 'nf_sub' ) ) ) {
  40. // continue;
  41. }
  42. $labels = get_post_type_labels( $post_type );
  43. $options[ esc_attr( $post_type->name ) ] = esc_html( $labels->name );
  44. }
  45. return $options;
  46. }
  47. /**
  48. * @deprecated 1.7.20
  49. * @see PUM_Helper_Array::move_item
  50. *
  51. * @param array $ref_arr
  52. * @param string $key1
  53. * @param int|string $move
  54. * @param string|null $key2
  55. *
  56. * @return bool
  57. */
  58. public static function move_item( &$ref_arr, $key1, $move, $key2 = null ) {
  59. return PUM_Utils_Array::move_item( $ref_arr, $key1, $move, $key2 );
  60. }
  61. /**
  62. * @deprecated 1.7.20
  63. * @see PUM_Helper_Array::remove_keys_starting_with
  64. *
  65. * @param array $array
  66. * @param bool $string
  67. *
  68. * @return array
  69. */
  70. public static function remove_keys_starting_with( $array, $string = false ) {
  71. return PUM_Utils_Array::remove_keys_starting_with( $array, $string );
  72. }
  73. /**
  74. * @deprecated 1.7.20
  75. * @see PUM_Helper_Array::sort_by_sort
  76. *
  77. * @param array $a
  78. * @param array $b
  79. *
  80. * @return array
  81. */
  82. public static function sort_by_sort( $a, $b ) {
  83. return PUM_Utils_Array::sort_by_sort( $a, $b );
  84. }
  85. /**
  86. * @param array $fields
  87. *
  88. * @return array
  89. */
  90. public static function get_field_defaults( $fields = array() ) {
  91. $defaults = array();
  92. foreach ( $fields as $field_id => $field ) {
  93. if ( isset( $field['std'] ) ) {
  94. $defaults[ $field_id ] = $field['std'];
  95. } else {
  96. $defaults[ $field_id ] = 'checkbox' === $field['type'] ? null : false;
  97. }
  98. }
  99. return $defaults;
  100. }
  101. /**
  102. * @deprecated 1.7.20
  103. * @see PUM_Utils_Array::from_object instead.
  104. *
  105. * @param $array
  106. * @param $old_key
  107. * @param $new_key
  108. *
  109. * @return array
  110. * @throws Exception
  111. */
  112. public static function replace_key( $array, $old_key, $new_key ) {
  113. return PUM_Utils_Array::replace_key( $array, $old_key, $new_key );
  114. }
  115. /**
  116. * @param $tabs
  117. *
  118. * @return array
  119. */
  120. public static function flatten_fields_array( $tabs ) {
  121. $fields = array();
  122. foreach ( $tabs as $tab_id => $tab_sections ) {
  123. if ( self::is_field( $tab_sections ) ) {
  124. $fields[ $tab_id ] = $tab_sections;
  125. continue;
  126. } else {
  127. foreach ( $tab_sections as $section_id => $section_fields ) {
  128. if ( self::is_field( $tab_sections ) ) {
  129. $fields[ $section_id ] = $section_fields;
  130. continue;
  131. }
  132. foreach ( $section_fields as $field_id => $field ) {
  133. $fields[ $field_id ] = $field;
  134. continue;
  135. }
  136. }
  137. }
  138. }
  139. return $fields;
  140. }
  141. /**
  142. * @param $field
  143. *
  144. * @return array
  145. */
  146. public static function parse_field( $field ) {
  147. return wp_parse_args( $field, array(
  148. 'section' => 'main',
  149. 'type' => 'text',
  150. 'id' => null,
  151. 'label' => '',
  152. 'desc' => '',
  153. 'name' => null,
  154. 'templ_name' => null,
  155. 'size' => 'regular',
  156. 'options' => array(),
  157. 'std' => null,
  158. 'rows' => 5,
  159. 'cols' => 50,
  160. 'min' => 0,
  161. 'max' => 50,
  162. 'force_minmax' => false,
  163. 'step' => 1,
  164. 'select2' => null,
  165. 'object_type' => 'post_type',
  166. 'object_key' => 'post',
  167. 'post_type' => null,
  168. 'taxonomy' => null,
  169. 'multiple' => null,
  170. 'as_array' => false,
  171. 'placeholder' => null,
  172. 'checkbox_val' => 1,
  173. 'allow_blank' => true,
  174. 'readonly' => false,
  175. 'required' => false,
  176. 'disabled' => false,
  177. 'hook' => null,
  178. 'unit' => __( 'ms', 'popup-maker' ),
  179. 'desc_position' => 'bottom',
  180. 'units' => array(
  181. 'px' => 'px',
  182. '%' => '%',
  183. 'em' => 'em',
  184. 'rem' => 'rem',
  185. ),
  186. 'priority' => 10,
  187. 'doclink' => '',
  188. 'button_type' => 'submit',
  189. 'class' => '',
  190. 'messages' => array(),
  191. 'license_status' => '',
  192. 'private' => false,
  193. ) );
  194. }
  195. /**
  196. * @param $fields
  197. * @param array $args
  198. *
  199. * @return mixed
  200. */
  201. public static function parse_tab_fields( $fields, $args = array() ) {
  202. $args = wp_parse_args( $args, array(
  203. 'has_subtabs' => false,
  204. 'name' => '%s',
  205. ) );
  206. if ( $args['has_subtabs'] ) {
  207. foreach ( $fields as $tab_id => $tab_sections ) {
  208. foreach ( $tab_sections as $section_id => $section_fields ) {
  209. if ( self::is_field( $section_fields ) ) {
  210. // Allow for flat tabs with no sections.
  211. $section_id = 'main';
  212. $section_fields = array(
  213. $section_id => $section_fields,
  214. );
  215. }
  216. $fields[ $tab_id ][ $section_id ] = self::parse_fields( $section_fields, $args['name'] );
  217. }
  218. }
  219. } else {
  220. foreach ( $fields as $tab_id => $tab_fields ) {
  221. $fields[ $tab_id ] = self::parse_fields( $tab_fields, $args['name'] );
  222. }
  223. }
  224. return $fields;
  225. }
  226. /**
  227. * @param array $fields
  228. * @param string $name
  229. *
  230. * @return mixed
  231. */
  232. public static function parse_fields( $fields, $name = '%' ) {
  233. if ( is_array( $fields ) && ! empty( $fields ) ) {
  234. foreach ( $fields as $field_id => $field ) {
  235. if ( ! is_array( $field ) || ! self::is_field( $field ) ) {
  236. continue;
  237. }
  238. // Remap old settings.
  239. if ( is_numeric( $field_id ) && ! empty( $field['id'] ) ) {
  240. try {
  241. $fields = PUM_Utils_Array::replace_key( $fields, $field_id, $field['id'] );
  242. } catch ( Exception $e ) {
  243. }
  244. $field_id = $field['id'];
  245. } elseif ( empty( $field['id'] ) && ! is_numeric( $field_id ) ) {
  246. $field['id'] = $field_id;
  247. }
  248. if ( ! empty( $field['name'] ) && empty( $field['label'] ) ) {
  249. $field['label'] = $field['name'];
  250. unset( $field['name'] );
  251. }
  252. if ( empty( $field['name'] ) ) {
  253. $field['name'] = sprintf( $name, $field_id );
  254. }
  255. $fields[ $field_id ] = self::parse_field( $field );
  256. }
  257. }
  258. $fields = PUM_Utils_Array::sort( $fields, 'priority' );
  259. return $fields;
  260. }
  261. /**
  262. * Sort array by priority value
  263. *
  264. * @deprecated 1.7.20
  265. * @see PUM_Utils_Array::sort_by_priority instead.
  266. *
  267. * @param $a
  268. * @param $b
  269. *
  270. * @return int
  271. */
  272. public static function sort_by_priority( $a, $b ) {
  273. return PUM_Utils_Array::sort_by_priority( $a, $b );
  274. }
  275. /**
  276. * Checks if an array is a field.
  277. *
  278. * @param array $array
  279. *
  280. * @return bool
  281. */
  282. public static function is_field( $array = array() ) {
  283. $field_tests = array(
  284. ! isset( $array['type'] ) && ( isset( $array['label'] ) || isset( $array['desc'] ) ),
  285. isset( $array['type'] ) && is_string( $array['type'] ),
  286. );
  287. return in_array( true, $field_tests );
  288. }
  289. /**
  290. * Checks if an array is a section.
  291. *
  292. * @param array $array
  293. *
  294. * @return bool
  295. */
  296. public static function is_section( $array = array() ) {
  297. return ! self::is_field( $array );
  298. }
  299. /**
  300. * @deprecated 1.7.0
  301. *
  302. * @param array $args
  303. */
  304. public static function modal( $args = array() ) {
  305. $args = wp_parse_args( $args, array(
  306. 'id' => 'default',
  307. 'title' => '',
  308. 'description' => '',
  309. 'class' => '',
  310. 'cancel_button' => true,
  311. 'cancel_button_text' => __( 'Cancel', 'popup-maker' ),
  312. 'save_button' => true,
  313. 'save_button_text' => __( 'Add', 'popup-maker' ),
  314. ) );
  315. ?>
  316. <div id="<?php echo $args['id']; ?>" class="pum-modal-background <?php echo esc_attr( $args['class'] ); ?>" role="dialog" aria-hidden="true" aria-labelledby="<?php echo $args['id']; ?>-title"
  317. <?php if ( '' != $args['description'] ) { ?>aria-describedby="<?php echo $args['id']; ?>-description"<?php } ?>>
  318. <div class="pum-modal-wrap">
  319. <form class="pum-form">
  320. <div class="pum-modal-header">
  321. <?php if ( '' != $args['title'] ) { ?>
  322. <span id="<?php echo $args['id']; ?>-title" class="pum-modal-title"><?php echo $args['title']; ?></span>
  323. <?php } ?>
  324. <button type="button" class="pum-modal-close" aria-label="<?php _e( 'Close', 'popup-maker' ); ?>"></button>
  325. </div>
  326. <?php if ( '' != $args['description'] ) { ?>
  327. <span id="<?php echo $args['id']; ?>-description" class="screen-reader-text"><?php echo $args['description']; ?></span>
  328. <?php } ?>
  329. <div class="pum-modal-content">
  330. <?php echo $args['content']; ?>
  331. </div>
  332. <?php if ( $args['save_button'] || $args['cancel_button'] ) { ?>
  333. <div class="pum-modal-footer submitbox">
  334. <?php if ( $args['cancel_button'] ) { ?>
  335. <div class="cancel">
  336. <button type="button" class="submitdelete no-button" href="#"><?php echo $args['cancel_button_text']; ?></button>
  337. </div>
  338. <?php } ?>
  339. <?php if ( $args['save_button'] ) { ?>
  340. <div class="pum-submit">
  341. <span class="spinner"></span>
  342. <button class="button button-primary"><?php echo $args['save_button_text']; ?></button>
  343. </div>
  344. <?php } ?>
  345. </div>
  346. <?php } ?>
  347. </form>
  348. </div>
  349. </div>
  350. <?php
  351. }
  352. /**
  353. * @deprecated 1.7.20
  354. * @see PUM_Utils_Array::from_object instead.
  355. *
  356. * @param $obj
  357. *
  358. * @return array
  359. */
  360. public static function object_to_array( $obj ) {
  361. return PUM_Utils_Array::from_object( $obj );
  362. }
  363. }