PluginProbe
WP Directory Kit / trunk
WP Directory Kit vtrunk
1.5.6 1.5.5 1.5.4 1.5.3 trunk 1.1.0 1.1.6 1.1.8 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.8 1.4.0 1.4.1 All 35 releases
wpdirectorykit / rest_api.php

rest_api.php in WP Directory Kit trunk, at rest_api.php

60 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly.
5 }
6
7 add_action('rest_api_init', function () {
8 register_rest_route('wdk/v1', '/listing/autosuggestion', [
9 'methods' => 'POST',
10 'callback' => 'wdk_listing_search_rest_callback',
11 'permission_callback' => function () {
12 return current_user_can('edit_posts');
13 },
14 ]);
15 });
16
17 function wdk_listing_search_rest_callback($request)
18 {
19 $params = $request->get_json_params();
20 $keyword = '';
21
22 if ($params && isset($params['keyword'])) {
23 $keyword = sanitize_text_field($params['keyword']);
24 }
25
26 if (strlen($keyword) < 2) {
27 return rest_ensure_response([
28 'success' => true,
29 'data' => []
30 ]);
31 }
32
33 $args = [
34 'post_type' => 'wdk-listing',
35 'posts_per_page' => 10,
36 's' => $keyword,
37 ];
38
39 // Optionally exclude the currently executed ID if provided
40 if (isset($params['executedId'])) {
41 $args['post__not_in'] = [intval($params['executedId'])];
42 }
43
44 $posts = get_posts($args);
45
46 $result = [];
47 foreach ($posts as $post) {
48 $result[] = [
49 'id' => $post->ID,
50 'title' => $post->post_title,
51 'edit' => urlencode(admin_url('admin.php?page=wdk_listing&id=' . $post->ID)),
52 ];
53 }
54
55 return rest_ensure_response([
56 'success' => true,
57 'data' => $result
58 ]);
59 }
60