PluginProbe
Ultimate Store Kit – Addon For WooCommerce, EDD and Elementor / trunk
Ultimate Store Kit – Addon For WooCommerce, EDD and Elementor vtrunk
3.0.7 3.0.5 3.0.4 3.0.3 3.0.2 trunk 1.5.0 1.5.1 1.5.2 1.6.1 1.6.2 1.6.3 1.6.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.2 2.1.3 All 87 releases
ultimate-store-kit / includes / API / Base.php

Base.php in Ultimate Store Kit – Addon For WooCommerce, EDD and Elementor trunk, at includes/API/Base.php

91 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Abstract REST API Base Class
5 *
6 * Shared by both core and pro plugins to avoid redundant REST API code.
7 */
8
9 namespace UltimateStoreKit\API;
10
11 if (!defined('ABSPATH')) {
12 exit;
13 }
14
15 abstract class Base {
16
17 public function __construct() {
18 add_action('rest_api_init', [$this, 'register_routes']);
19 }
20
21 /**
22 * REST namespace, e.g. 'ultimate-store-kit/v1'.
23 */
24 abstract protected function get_namespace();
25
26 /**
27 * Return an array of route definitions. Each element:
28 * [
29 * 'path' => '/settings',
30 * 'methods' => 'POST',
31 * 'callback' => [$this, 'method_name'],
32 * 'args' => [], // optional
33 * 'permission_callback' => callable, // optional, defaults to check_admin_permission
34 * ]
35 */
36 abstract protected function get_routes();
37
38 /**
39 * Register all routes returned by get_routes().
40 */
41 public function register_routes() {
42 foreach ($this->get_routes() as $route) {
43 $config = [
44 'methods' => $route['methods'] ?? 'POST',
45 'callback' => $route['callback'],
46 'permission_callback' => $route['permission_callback'] ?? [$this, 'check_admin_permission'],
47 ];
48
49 if (!empty($route['args'])) {
50 $config['args'] = $route['args'];
51 }
52
53 register_rest_route($this->get_namespace(), $route['path'], $config);
54 }
55 }
56
57 /**
58 * Default permission callback — only administrators.
59 */
60 public function check_admin_permission() {
61 return current_user_can('manage_options');
62 }
63
64 /**
65 * Return a success REST response.
66 */
67 protected function success($data) {
68 return rest_ensure_response($data);
69 }
70
71 /**
72 * Return a WP_Error REST response.
73 */
74 protected function error($code, $message, $status = 400) {
75 return new \WP_Error($code, $message, ['status' => $status]);
76 }
77
78 /**
79 * Sanitize a flat key-value settings array.
80 */
81 protected function sanitize_settings($settings) {
82 $sanitized = [];
83 if (is_array($settings)) {
84 foreach ($settings as $key => $value) {
85 $sanitized[sanitize_text_field($key)] = sanitize_text_field($value);
86 }
87 }
88 return $sanitized;
89 }
90 }
91