vc-grids-functions.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. /**
  6. * @param $term
  7. *
  8. * @return array|bool
  9. * @since 4.5.2
  10. *
  11. */
  12. function vc_autocomplete_taxonomies_field_render( $term ) {
  13. $vc_taxonomies_types = vc_taxonomies_types();
  14. $terms = get_terms( array_keys( $vc_taxonomies_types ), array(
  15. 'include' => array( $term['value'] ),
  16. 'hide_empty' => false,
  17. ) );
  18. $data = false;
  19. if ( is_array( $terms ) && 1 === count( $terms ) ) {
  20. $term = $terms[0];
  21. $data = vc_get_term_object( $term );
  22. }
  23. return $data;
  24. }
  25. /**
  26. * @param $search_string
  27. *
  28. * @return array|bool
  29. * @since 4.5.2
  30. *
  31. */
  32. function vc_autocomplete_taxonomies_field_search( $search_string ) {
  33. $data = array();
  34. $vc_filter_by = vc_post_param( 'vc_filter_by', '' );
  35. $vc_filter_by_post_type = vc_post_param( 'vc_filter_post_type', '' );
  36. $vc_taxonomies_types = strlen( $vc_filter_by ) > 0 ? array( $vc_filter_by ) : array_keys( vc_taxonomies_types( $vc_filter_by_post_type ) );
  37. if ( empty( $vc_taxonomies_types ) ) {
  38. return array();
  39. }
  40. $vc_taxonomies = get_terms( $vc_taxonomies_types, array(
  41. 'hide_empty' => false,
  42. 'search' => $search_string,
  43. ) );
  44. if ( is_array( $vc_taxonomies ) && ! empty( $vc_taxonomies ) ) {
  45. foreach ( $vc_taxonomies as $t ) {
  46. if ( is_object( $t ) ) {
  47. $data[] = vc_get_term_object( $t );
  48. }
  49. }
  50. }
  51. return $data;
  52. }
  53. /**
  54. * @param $search
  55. * @param $wp_query
  56. *
  57. * @return string
  58. */
  59. function vc_search_by_title_only( $search, &$wp_query ) {
  60. global $wpdb;
  61. if ( empty( $search ) ) {
  62. return $search;
  63. }
  64. // skip processing - no search term in query
  65. $q = $wp_query->query_vars;
  66. if ( isset( $q['vc_search_by_title_only'] ) && $q['vc_search_by_title_only'] ) {
  67. $n = ! empty( $q['exact'] ) ? '' : '%';
  68. $search = '';
  69. $searchand = '';
  70. foreach ( (array) $q['search_terms'] as $term ) {
  71. $term = $wpdb->esc_like( $term );
  72. $like = $n . $term . $n;
  73. $search .= $wpdb->prepare( "%s ($wpdb->posts.post_title LIKE %s)", $searchand, $like );
  74. $searchand = ' AND ';
  75. }
  76. if ( ! empty( $search ) ) {
  77. $search = " AND ({$search}) ";
  78. if ( ! is_user_logged_in() ) {
  79. $search .= " AND ($wpdb->posts.post_password = '') ";
  80. }
  81. }
  82. }
  83. return $search;
  84. }
  85. /**
  86. * @param $search_string
  87. *
  88. * @return array
  89. */
  90. function vc_include_field_search( $search_string ) {
  91. $query = $search_string;
  92. $data = array();
  93. $args = array(
  94. 's' => $query,
  95. 'post_type' => 'any',
  96. );
  97. $args['vc_search_by_title_only'] = true;
  98. $args['numberposts'] = - 1;
  99. if ( 0 === strlen( $args['s'] ) ) {
  100. unset( $args['s'] );
  101. }
  102. add_filter( 'posts_search', 'vc_search_by_title_only', 500, 2 );
  103. $posts = get_posts( $args );
  104. if ( is_array( $posts ) && ! empty( $posts ) ) {
  105. foreach ( $posts as $post ) {
  106. $data[] = array(
  107. 'value' => $post->ID,
  108. 'label' => $post->post_title,
  109. 'group' => $post->post_type,
  110. );
  111. }
  112. }
  113. return $data;
  114. }
  115. /**
  116. * @param $value
  117. *
  118. * @return array|bool
  119. */
  120. function vc_include_field_render( $value ) {
  121. $post = get_post( $value['value'] );
  122. return is_null( $post ) ? false : array(
  123. 'label' => $post->post_title,
  124. 'value' => $post->ID,
  125. 'group' => $post->post_type,
  126. );
  127. }
  128. /**
  129. * @param $data_arr
  130. *
  131. * @return array
  132. */
  133. function vc_exclude_field_search( $data_arr ) {
  134. $query = isset( $data_arr['query'] ) ? $data_arr['query'] : null;
  135. $term = isset( $data_arr['term'] ) ? $data_arr['term'] : '';
  136. $data = array();
  137. $args = ! empty( $query ) ? array(
  138. 's' => $term,
  139. 'post_type' => $query,
  140. ) : array(
  141. 's' => $term,
  142. 'post_type' => 'any',
  143. );
  144. $args['vc_search_by_title_only'] = true;
  145. $args['numberposts'] = - 1;
  146. if ( 0 === strlen( $args['s'] ) ) {
  147. unset( $args['s'] );
  148. }
  149. add_filter( 'posts_search', 'vc_search_by_title_only', 500, 2 );
  150. $posts = get_posts( $args );
  151. if ( is_array( $posts ) && ! empty( $posts ) ) {
  152. foreach ( $posts as $post ) {
  153. $data[] = array(
  154. 'value' => $post->ID,
  155. 'label' => $post->post_title,
  156. 'group' => $post->post_type,
  157. );
  158. }
  159. }
  160. return $data;
  161. }
  162. /**
  163. * @param $value
  164. *
  165. * @return array|bool
  166. */
  167. function vc_exclude_field_render( $value ) {
  168. $post = get_post( $value['value'] );
  169. return is_null( $post ) ? false : array(
  170. 'label' => $post->post_title,
  171. 'value' => $post->ID,
  172. 'group' => $post->post_type,
  173. );
  174. }