class-fl-builder-auto-suggest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /**
  3. * A class for working with auto suggest AJAX requests.
  4. *
  5. * @since 1.2.3
  6. */
  7. final class FLBuilderAutoSuggest {
  8. /**
  9. * Checks for an auto suggest request. If one is found
  10. * the data will be echoed as a JSON response.
  11. *
  12. * @since 1.2.3
  13. * @return array
  14. */
  15. static public function init() {
  16. if ( isset( $_REQUEST['fl_as_action'] ) && isset( $_REQUEST['fl_as_query'] ) ) {
  17. switch ( $_REQUEST['fl_as_action'] ) {
  18. case 'fl_as_posts':
  19. $data = self::posts();
  20. break;
  21. case 'fl_as_terms':
  22. $data = self::terms();
  23. break;
  24. case 'fl_as_users':
  25. $data = self::users();
  26. break;
  27. case 'fl_as_links':
  28. $data = self::links();
  29. break;
  30. }
  31. if ( isset( $data ) ) {
  32. return $data;
  33. }
  34. }
  35. }
  36. /**
  37. * Returns a JSON encoded value for a suggest field.
  38. *
  39. * @since 1.2.3
  40. * @param string $action The type of auto suggest action.
  41. * @param string $value The current value.
  42. * @param string $data Additional auto suggest data.
  43. * @return string The JSON encoded value.
  44. */
  45. static public function get_value( $action = '', $value = '', $data = '' ) {
  46. switch ( $action ) {
  47. case 'fl_as_posts':
  48. $data = self::posts_value( $value );
  49. break;
  50. case 'fl_as_terms':
  51. $data = self::terms_value( $value, $data );
  52. break;
  53. case 'fl_as_users':
  54. $data = self::users_value( $value );
  55. break;
  56. default :
  57. if ( function_exists( $action . '_value' ) ) {
  58. $data = call_user_func_array( $action . '_value', array( $value, $data ) );
  59. }
  60. break;
  61. }
  62. return isset( $data ) ? str_replace( "'", '&#39;', json_encode( $data ) ) : '';
  63. }
  64. /**
  65. * Returns the values for all suggest fields in a settings form.
  66. *
  67. * @since 2.0
  68. * @param array $fields
  69. * @return array
  70. */
  71. static public function get_values( $fields ) {
  72. $values = array();
  73. foreach ( $fields as $field ) {
  74. $values[ $field['name'] ] = self::get_value( $field['action'], $field['value'], $field['data'] );
  75. }
  76. return $values;
  77. }
  78. /**
  79. * Returns the SQL escaped like value for auto suggest queries.
  80. *
  81. * @since 1.2.3
  82. * @return string
  83. */
  84. static public function get_like() {
  85. global $wpdb;
  86. $like = stripslashes( urldecode( $_REQUEST['fl_as_query'] ) );
  87. if ( method_exists( $wpdb, 'esc_like' ) ) {
  88. $like = esc_sql( $wpdb->esc_like( $like ) );
  89. } else {
  90. $like = like_escape( esc_sql( $like ) );
  91. }
  92. return $like;
  93. }
  94. /**
  95. * Returns data for post auto suggest queries.
  96. *
  97. * @since 1.2.3
  98. * @return array
  99. */
  100. static public function posts() {
  101. global $wpdb;
  102. $data = array();
  103. $like = self::get_like();
  104. $types = explode( ',', esc_sql( $_REQUEST['fl_as_action_data'] ) );
  105. $types_in = join( "', '", array_map( 'esc_sql', $types ) );
  106. // @codingStandardsIgnoreStart
  107. $posts = $wpdb->get_results( $wpdb->prepare( "
  108. SELECT ID, post_title FROM {$wpdb->posts}
  109. WHERE post_title LIKE %s
  110. AND post_type IN ('{$types_in}')
  111. AND post_status = 'publish'
  112. ", '%' . $like . '%' ) );
  113. // @codingStandardsIgnoreEnd
  114. foreach ( $posts as $post ) {
  115. $data[] = array(
  116. 'name' => $post->post_title,
  117. 'value' => $post->ID,
  118. );
  119. }
  120. return $data;
  121. }
  122. /**
  123. * Returns data for selected posts.
  124. *
  125. * @since 1.2.3
  126. * @param string $ids The selected post ids.
  127. * @return array An array of post data.
  128. */
  129. static public function posts_value( $ids ) {
  130. global $wpdb;
  131. $data = array();
  132. if ( ! empty( $ids ) ) {
  133. $order = implode( ',', array_filter( explode( ',', $ids ), 'intval' ) );
  134. $list = explode( ',', $ids );
  135. $how_many = count( $list );
  136. $placeholders = array_fill( 0, $how_many, '%d' );
  137. $format = implode( ', ', $placeholders );
  138. $query = "SELECT ID, post_title FROM {$wpdb->posts} WHERE ID IN ($format) ORDER BY FIELD(ID, $order)";
  139. // @codingStandardsIgnoreStart
  140. $posts = $wpdb->get_results( $wpdb->prepare( $query, $list ) );
  141. // @codingStandardsIgnoreEnd
  142. foreach ( $posts as $post ) {
  143. $data[] = array(
  144. 'name' => $post->post_title,
  145. 'value' => $post->ID,
  146. );
  147. }
  148. }
  149. return $data;
  150. }
  151. /**
  152. * Returns data for term auto suggest queries.
  153. *
  154. * @since 1.2.3
  155. * @return array
  156. */
  157. static public function terms() {
  158. $data = array();
  159. $cats = get_categories(array(
  160. 'hide_empty' => 0,
  161. 'taxonomy' => $_REQUEST['fl_as_action_data'],
  162. ));
  163. foreach ( $cats as $cat ) {
  164. $data[] = array(
  165. 'name' => $cat->name,
  166. 'value' => $cat->term_id,
  167. );
  168. }
  169. return $data;
  170. }
  171. /**
  172. * Returns data for selected terms.
  173. *
  174. * @since 1.2.3
  175. * @param string $ids The selected term ids.
  176. * @param string $taxonomy The taxonomy to look in.
  177. * @return array An array of term data.
  178. */
  179. static public function terms_value( $ids, $taxonomy ) {
  180. $data = array();
  181. if ( ! empty( $ids ) ) {
  182. $cats = get_categories(array(
  183. 'hide_empty' => 0,
  184. 'taxonomy' => $taxonomy,
  185. 'include' => $ids,
  186. ));
  187. foreach ( $cats as $cat ) {
  188. $data[] = array(
  189. 'name' => $cat->name,
  190. 'value' => $cat->term_id,
  191. );
  192. }
  193. }
  194. return $data;
  195. }
  196. /**
  197. * Returns data for user auto suggest queries.
  198. *
  199. * @since 1.2.3
  200. * @return array
  201. */
  202. static public function users() {
  203. global $wpdb;
  204. $data = array();
  205. $like = self::get_like();
  206. $users = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->users} WHERE user_login LIKE %s", '%' . $like . '%' ) );
  207. foreach ( $users as $user ) {
  208. $data[] = array(
  209. 'name' => $user->user_login,
  210. 'value' => $user->ID,
  211. );
  212. }
  213. return $data;
  214. }
  215. /**
  216. * Returns data for selected users.
  217. *
  218. * @since 1.2.3
  219. * @param string $ids The selected user ids.
  220. * @return array An array of user data.
  221. */
  222. static public function users_value( $ids ) {
  223. global $wpdb;
  224. $data = array();
  225. if ( ! empty( $ids ) ) {
  226. $list = explode( ',', $ids );
  227. $how_many = count( $list );
  228. $placeholders = array_fill( 0, $how_many, '%d' );
  229. $format = implode( ', ', $placeholders );
  230. $query = "SELECT * FROM {$wpdb->users} WHERE ID IN ($format)";
  231. // @codingStandardsIgnoreStart
  232. $users = $wpdb->get_results( $wpdb->prepare( $query, $list ) );
  233. // @codingStandardsIgnoreEnd
  234. foreach ( $users as $user ) {
  235. $data[] = array(
  236. 'name' => $user->user_login,
  237. 'value' => $user->ID,
  238. );
  239. }
  240. }
  241. return $data;
  242. }
  243. /**
  244. * Returns data for link auto suggest queries.
  245. *
  246. * @since 1.3.9
  247. * @return array
  248. */
  249. static public function links() {
  250. global $wpdb;
  251. $data = array();
  252. $like = self::get_like();
  253. $types = FLBuilderLoop::post_types();
  254. $slugs = array();
  255. foreach ( $types as $slug => $type ) {
  256. $slugs[] = esc_sql( $slug );
  257. }
  258. // we cant use an array of arrays for prepare() so use sprintf 1st.
  259. $query = sprintf( "SELECT ID, post_title FROM {$wpdb->posts}
  260. WHERE post_title LIKE %%s
  261. AND post_type IN ('%s')
  262. AND post_status = 'publish'",
  263. implode( "', '", $slugs )
  264. );
  265. // @codingStandardsIgnoreStart
  266. $posts = $wpdb->get_results( $wpdb->prepare( $query, '%' . esc_sql( $like ) . '%' ) );
  267. // @codingStandardsIgnoreEnd
  268. foreach ( $posts as $post ) {
  269. $data[] = array(
  270. 'name' => $post->post_title,
  271. 'value' => get_permalink( $post->ID ),
  272. );
  273. }
  274. return $data;
  275. }
  276. }