fields.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Fields Class
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! class_exists( 'CSF_Fields' ) ) {
  11. abstract class CSF_Fields extends CSF_Abstract {
  12. public function __construct( $field = array(), $value = '', $unique = '', $where = '', $parent = '' ) {
  13. $this->field = $field;
  14. $this->value = $value;
  15. $this->unique = $unique;
  16. $this->where = $where;
  17. $this->parent = $parent;
  18. }
  19. public function field_name( $nested_name = '' ) {
  20. $field_id = ( ! empty( $this->field['id'] ) ) ? $this->field['id'] : '';
  21. $unique_id = ( ! empty( $this->unique ) ) ? $this->unique .'['. $field_id .']' : $field_id;
  22. $field_name = ( ! empty( $this->field['name'] ) ) ? $this->field['name'] : $unique_id;
  23. $tag_prefix = ( ! empty( $this->field['tag_prefix'] ) ) ? $this->field['tag_prefix'] : '';
  24. if ( ! empty( $tag_prefix ) ) {
  25. $nested_name = str_replace( '[', '['. $tag_prefix, $nested_name );
  26. }
  27. return $field_name . $nested_name;
  28. }
  29. public function field_attributes( $custom_atts = array() ) {
  30. $field_id = ( ! empty( $this->field['id'] ) ) ? $this->field['id'] : '';
  31. $attributes = ( ! empty( $this->field['attributes'] ) ) ? $this->field['attributes'] : array();
  32. if ( ! empty( $field_id ) && empty( $attributes['data-depend-id'] ) ) {
  33. $attributes['data-depend-id'] = $field_id;
  34. }
  35. if ( ! empty( $this->field['placeholder'] ) ) {
  36. $attributes['placeholder'] = $this->field['placeholder'];
  37. }
  38. $attributes = wp_parse_args( $attributes, $custom_atts );
  39. $atts = '';
  40. if ( ! empty( $attributes ) ) {
  41. foreach ( $attributes as $key => $value ) {
  42. if ( $value === 'only-key' ) {
  43. $atts .= ' '. esc_attr( $key );
  44. } else {
  45. $atts .= ' '. esc_attr( $key ) . '="'. esc_attr( $value ) .'"';
  46. }
  47. }
  48. }
  49. return $atts;
  50. }
  51. public function field_before() {
  52. return ( ! empty( $this->field['before'] ) ) ? '<div class="csf-before-text">'. $this->field['before'] .'</div>' : '';
  53. }
  54. public function field_after() {
  55. $output = ( ! empty( $this->field['after'] ) ) ? '<div class="csf-after-text">'. $this->field['after'] .'</div>' : '';
  56. $output .= ( ! empty( $this->field['desc'] ) ) ? '<div class="clear"></div><div class="csf-desc-text">'. $this->field['desc'] .'</div>' : '';
  57. $output .= ( ! empty( $this->field['help'] ) ) ? '<div class="csf-help"><span class="csf-help-text">'. $this->field['help'] .'</span><i class="fas fa-question-circle"></i></div>' : '';
  58. $output .= ( ! empty( $this->field['_error'] ) ) ? '<div class="csf-error-text">'. $this->field['_error'] .'</div>' : '';
  59. return $output;
  60. }
  61. public static function field_data( $type = '', $term = false, $query_args = array() ) {
  62. $options = array();
  63. $array_search = false;
  64. // sanitize type name
  65. if ( in_array( $type, array( 'page', 'pages' ) ) ) {
  66. $option = 'page';
  67. } else if ( in_array( $type, array( 'post', 'posts' ) ) ) {
  68. $option = 'post';
  69. } else if ( in_array( $type, array( 'category', 'categories' ) ) ) {
  70. $option = 'category';
  71. } else if ( in_array( $type, array( 'tag', 'tags' ) ) ) {
  72. $option = 'post_tag';
  73. } else if ( in_array( $type, array( 'menu', 'menus' ) ) ) {
  74. $option = 'nav_menu';
  75. } else {
  76. $option = '';
  77. }
  78. // switch type
  79. switch( $type ) {
  80. case 'page':
  81. case 'pages':
  82. case 'post':
  83. case 'posts':
  84. // term query required for ajax select
  85. if ( ! empty( $term ) ) {
  86. $query = new WP_Query( wp_parse_args( $query_args, array(
  87. 's' => $term,
  88. 'post_type' => $option,
  89. 'post_status' => 'publish',
  90. 'posts_per_page' => 25,
  91. ) ) );
  92. } else {
  93. $query = new WP_Query( wp_parse_args( $query_args, array(
  94. 'post_type' => $option,
  95. 'post_status' => 'publish',
  96. ) ) );
  97. }
  98. if ( ! is_wp_error( $query ) && ! empty( $query->posts ) ) {
  99. foreach ( $query->posts as $item ) {
  100. $options[$item->ID] = $item->post_title;
  101. }
  102. }
  103. break;
  104. case 'category':
  105. case 'categories':
  106. case 'tag':
  107. case 'tags':
  108. case 'menu':
  109. case 'menus':
  110. if ( ! empty( $term ) ) {
  111. $query = new WP_Term_Query( wp_parse_args( $query_args, array(
  112. 'search' => $term,
  113. 'taxonomy' => $option,
  114. 'hide_empty' => false,
  115. 'number' => 25,
  116. ) ) );
  117. } else {
  118. $query = new WP_Term_Query( wp_parse_args( $query_args, array(
  119. 'taxonomy' => $option,
  120. 'hide_empty' => false,
  121. ) ) );
  122. }
  123. if ( ! is_wp_error( $query ) && ! empty( $query->terms ) ) {
  124. foreach ( $query->terms as $item ) {
  125. $options[$item->term_id] = $item->name;
  126. }
  127. }
  128. break;
  129. case 'user':
  130. case 'users':
  131. if ( ! empty( $term ) ) {
  132. $query = new WP_User_Query( array(
  133. 'search' => '*'. $term .'*',
  134. 'number' => 25,
  135. 'orderby' => 'title',
  136. 'order' => 'ASC',
  137. 'fields' => array( 'display_name', 'ID' )
  138. ) );
  139. } else {
  140. $query = new WP_User_Query( array( 'fields' => array( 'display_name', 'ID' ) ) );
  141. }
  142. if ( ! is_wp_error( $query ) && ! empty( $query->get_results() ) ) {
  143. foreach ( $query->get_results() as $item ) {
  144. $options[$item->ID] = $item->display_name;
  145. }
  146. }
  147. break;
  148. case 'sidebar':
  149. case 'sidebars':
  150. global $wp_registered_sidebars;
  151. if ( ! empty( $wp_registered_sidebars ) ) {
  152. foreach ( $wp_registered_sidebars as $sidebar ) {
  153. $options[$sidebar['id']] = $sidebar['name'];
  154. }
  155. }
  156. $array_search = true;
  157. break;
  158. case 'role':
  159. case 'roles':
  160. global $wp_roles;
  161. if ( ! empty( $wp_roles ) ) {
  162. if ( ! empty( $wp_roles->roles ) ) {
  163. foreach ( $wp_roles->roles as $role_key => $role_value ) {
  164. $options[$role_key] = $role_value['name'];
  165. }
  166. }
  167. }
  168. $array_search = true;
  169. break;
  170. case 'post_type':
  171. case 'post_types':
  172. $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
  173. if ( ! is_wp_error( $post_types ) && ! empty( $post_types ) ) {
  174. foreach ( $post_types as $post_type ) {
  175. $options[$post_type->name] = $post_type->labels->name;
  176. }
  177. }
  178. $array_search = true;
  179. break;
  180. case 'location':
  181. case 'locations':
  182. $nav_menus = get_registered_nav_menus();
  183. if ( ! is_wp_error( $nav_menus ) && ! empty( $nav_menus ) ) {
  184. foreach ( $nav_menus as $nav_menu_key => $nav_menu_name ) {
  185. $options[$nav_menu_key] = $nav_menu_name;
  186. }
  187. }
  188. $array_search = true;
  189. break;
  190. default:
  191. if ( is_callable( $type ) ) {
  192. if ( ! empty( $term ) ) {
  193. $options = call_user_func( $type, $query_args );
  194. } else {
  195. $options = call_user_func( $type, $term, $query_args );
  196. }
  197. }
  198. break;
  199. }
  200. // Array search by "term"
  201. if ( ! empty( $term ) && ! empty( $options ) && ! empty( $array_search ) ) {
  202. $options = preg_grep( '/'. $term .'/i', $options );
  203. }
  204. // Make multidimensional array for ajax search
  205. if ( ! empty( $term ) && ! empty( $options ) ) {
  206. $arr = array();
  207. foreach ( $options as $option_key => $option_value ) {
  208. $arr[] = array( 'value' => $option_key, 'text' => $option_value );
  209. }
  210. $options = $arr;
  211. }
  212. return $options;
  213. }
  214. public function field_wp_query_data_title( $type, $values ) {
  215. $options = array();
  216. if ( ! empty( $values ) && is_array( $values ) ) {
  217. foreach ( $values as $value ) {
  218. $options[$value] = ucfirst( $value );
  219. switch( $type ) {
  220. case 'post':
  221. case 'posts':
  222. case 'page':
  223. case 'pages':
  224. $title = get_the_title( $value );
  225. if ( ! is_wp_error( $title ) && ! empty( $title ) ) {
  226. $options[$value] = $title;
  227. }
  228. break;
  229. case 'category':
  230. case 'categories':
  231. case 'tag':
  232. case 'tags':
  233. case 'menu':
  234. case 'menus':
  235. $term = get_term( $value );
  236. if ( ! is_wp_error( $term ) && ! empty( $term ) ) {
  237. $options[$value] = $term->name;
  238. }
  239. break;
  240. case 'user':
  241. case 'users':
  242. $user = get_user_by( 'id', $value );
  243. if ( ! is_wp_error( $user ) && ! empty( $user ) ) {
  244. $options[$value] = $user->display_name;
  245. }
  246. break;
  247. case 'sidebar':
  248. case 'sidebars':
  249. global $wp_registered_sidebars;
  250. if ( ! empty( $wp_registered_sidebars[$value] ) ) {
  251. $options[$value] = $wp_registered_sidebars[$value]['name'];
  252. }
  253. break;
  254. case 'role':
  255. case 'roles':
  256. global $wp_roles;
  257. if ( ! empty( $wp_roles ) && ! empty( $wp_roles->roles ) && ! empty( $wp_roles->roles[$value] ) ) {
  258. $options[$value] = $wp_roles->roles[$value]['name'];
  259. }
  260. break;
  261. case 'post_type':
  262. case 'post_types':
  263. $post_types = get_post_types( array( 'show_in_nav_menus' => true ) );
  264. if ( ! is_wp_error( $post_types ) && ! empty( $post_types ) && ! empty( $post_types[$value] ) ) {
  265. $options[$value] = ucfirst( $value );
  266. }
  267. break;
  268. case 'location':
  269. case 'locations':
  270. $nav_menus = get_registered_nav_menus();
  271. if ( ! is_wp_error( $nav_menus ) && ! empty( $nav_menus ) && ! empty( $nav_menus[$value] ) ) {
  272. $options[$value] = $nav_menus[$value];
  273. }
  274. break;
  275. default:
  276. if ( is_callable( $type .'_title' ) ) {
  277. $options[$value] = call_user_func( $type .'_title', $value );
  278. }
  279. break;
  280. }
  281. }
  282. }
  283. return $options;
  284. }
  285. }
  286. }