PluginProbe
Download Manager Addons for Elementor / trunk
Download Manager Addons for Elementor vtrunk
trunk 1.0.5 1.2.3 1.2.5 1.3.0
wpdm-elementor / src / api / API.php

API.php in Download Manager Addons for Elementor trunk, at src/api/API.php

193 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST API endpoints for WPDM Elementor integration.
4 *
5 * @package WPDM\Elementor\API
6 * @since 1.0.0
7 */
8
9 namespace WPDM\Elementor\API;
10
11 use WP_REST_Request;
12 use WP_REST_Response;
13
14 /**
15 * Class API
16 *
17 * Handles REST API endpoints for the WPDM Elementor integration.
18 * Provides package search functionality for SELECT2 dropdowns.
19 *
20 * @since 1.0.0
21 */
22 class API
23 {
24 /**
25 * REST API namespace.
26 *
27 * @var string
28 */
29 const API_NAMESPACE = 'wpdm-elementor/v1';
30
31 /**
32 * Get the singleton instance.
33 *
34 * @since 1.0.0
35 * @return self The singleton instance.
36 */
37 public static function getInstance(): self
38 {
39 static $instance;
40 if (is_null($instance)) {
41 $instance = new self();
42 }
43 return $instance;
44 }
45
46 /**
47 * Private constructor.
48 *
49 * Registers the REST API initialization hook.
50 *
51 * @since 1.0.0
52 */
53 private function __construct()
54 {
55 // Check if rest_api_init already fired
56 if (did_action('rest_api_init')) {
57 $this->registerAPIEndpoints();
58 } else {
59 add_action('rest_api_init', [$this, 'registerAPIEndpoints']);
60 }
61 }
62
63 /**
64 * Register REST API endpoints.
65 *
66 * @since 1.0.0
67 * @return void
68 */
69 public function registerAPIEndpoints(): void
70 {
71 register_rest_route(
72 self::API_NAMESPACE,
73 '/search-packages',
74 [
75 'methods' => 'GET',
76 'callback' => [$this, 'searchPackages'],
77 'permission_callback' => '__return_true',
78 ]
79 );
80
81 register_rest_route(
82 self::API_NAMESPACE,
83 '/search-categories',
84 [
85 'methods' => 'GET',
86 'callback' => [$this, 'searchCategories'],
87 'permission_callback' => '__return_true',
88 ]
89 );
90 }
91
92 /**
93 * Check if the current user has permission to access the endpoint.
94 *
95 * Returns true as this is a read-only search endpoint
96 * used in Elementor editor context.
97 *
98 * @since 1.0.0
99 * @return bool Always returns true.
100 */
101 public function checkPermission(): bool
102 {
103 return true;
104 }
105
106 /**
107 * Search packages by title.
108 *
109 * Returns packages matching the search term for SELECT2 dropdown.
110 *
111 * @since 1.0.0
112 * @param WP_REST_Request $request REST request object.
113 * @return WP_REST_Response JSON response with package results.
114 */
115 public function searchPackages(WP_REST_Request $request): WP_REST_Response
116 {
117 global $wpdb;
118
119 $term = $request->get_param('term');
120 $packages = [];
121
122 if (!empty($term)) {
123 // Use prepared statement for security
124 $like_term = '%' . $wpdb->esc_like($term) . '%';
125
126 $results = $wpdb->get_results(
127 $wpdb->prepare(
128 "SELECT ID, post_title
129 FROM {$wpdb->posts}
130 WHERE post_type = 'wpdmpro'
131 AND post_status = 'publish'
132 AND post_title LIKE %s
133 ORDER BY post_title ASC
134 LIMIT 20",
135 $like_term
136 )
137 );
138
139 if ($results) {
140 foreach ($results as $row) {
141 $packages[] = [
142 'id' => (int) $row->ID,
143 'text' => esc_html($row->post_title),
144 ];
145 }
146 }
147 }
148
149 // Results key is required for jQuery SELECT2
150 return new WP_REST_Response(['results' => $packages], 200);
151 }
152
153 /**
154 * Search categories by name.
155 *
156 * Returns categories matching the search term for SELECT2 dropdown.
157 *
158 * @since 1.3.0
159 * @param WP_REST_Request $request REST request object.
160 * @return WP_REST_Response JSON response with category results.
161 */
162 public function searchCategories(WP_REST_Request $request): WP_REST_Response
163 {
164 $term = $request->get_param('term');
165 $categories = [];
166
167 $args = [
168 'taxonomy' => 'wpdmcategory',
169 'hide_empty' => false,
170 'number' => 20,
171 'orderby' => 'name',
172 'order' => 'ASC',
173 ];
174
175 if (!empty($term)) {
176 $args['search'] = $term;
177 }
178
179 $terms = get_terms($args);
180
181 if (!is_wp_error($terms) && !empty($terms)) {
182 foreach ($terms as $category) {
183 $categories[] = [
184 'id' => $category->slug,
185 'text' => esc_html($category->name),
186 ];
187 }
188 }
189
190 return new WP_REST_Response(['results' => $categories], 200);
191 }
192 }
193