fields.class.php 11 KB

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