| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Controllers; |
| 6 |
|
| 7 |
use WP_REST_Request; |
| 8 |
use WP_REST_Response; |
| 9 |
use WP_Error; |
| 10 |
use Yatra\Core\Modules\ModuleManager; |
| 11 |
|
| 12 |
class ModuleController extends BaseController |
| 13 |
{ |
| 14 |
public function register_routes(): void |
| 15 |
{ |
| 16 |
$namespace = 'yatra/v1'; |
| 17 |
|
| 18 |
register_rest_route($namespace, '/modules', [ |
| 19 |
[ |
| 20 |
'methods' => \WP_REST_Server::READABLE, |
| 21 |
'callback' => [$this, 'get_modules'], |
| 22 |
'permission_callback' => [$this, 'check_permission'], |
| 23 |
], |
| 24 |
]); |
| 25 |
|
| 26 |
register_rest_route($namespace, '/modules/(?P<slug>[a-z0-9_\-]+)/toggle', [ |
| 27 |
[ |
| 28 |
'methods' => \WP_REST_Server::CREATABLE, |
| 29 |
'callback' => [$this, 'toggle_module'], |
| 30 |
'permission_callback' => [$this, 'check_permission'], |
| 31 |
'args' => [ |
| 32 |
'slug' => [ |
| 33 |
'required' => true, |
| 34 |
'sanitize_callback' => 'sanitize_key', |
| 35 |
], |
| 36 |
'enabled' => [ |
| 37 |
'required' => true, |
| 38 |
'validate_callback' => function ($value) { |
| 39 |
return is_bool($value) || $value === 'true' || $value === 'false' || $value === 1 || $value === 0; |
| 40 |
}, |
| 41 |
], |
| 42 |
], |
| 43 |
], |
| 44 |
]); |
| 45 |
|
| 46 |
register_rest_route($namespace, '/modules/bulk-toggle', [ |
| 47 |
[ |
| 48 |
'methods' => \WP_REST_Server::CREATABLE, |
| 49 |
'callback' => [$this, 'bulk_toggle'], |
| 50 |
'permission_callback' => [$this, 'check_permission'], |
| 51 |
], |
| 52 |
]); |
| 53 |
} |
| 54 |
|
| 55 |
public function check_permission(?WP_REST_Request $request = null): bool |
| 56 |
{ |
| 57 |
if (!is_user_logged_in()) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
if (current_user_can('manage_options')) { |
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
return current_user_can('yatra_edit_trips'); |
| 66 |
} |
| 67 |
|
| 68 |
public function get_modules(): WP_REST_Response |
| 69 |
{ |
| 70 |
return $this->success_response([ |
| 71 |
'data' => ModuleManager::getModules(), |
| 72 |
]); |
| 73 |
} |
| 74 |
|
| 75 |
public function toggle_module(WP_REST_Request $request) |
| 76 |
{ |
| 77 |
$slug = sanitize_key($request->get_param('slug')); |
| 78 |
$enabled = filter_var($request->get_param('enabled'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); |
| 79 |
|
| 80 |
if ($enabled === null) { |
| 81 |
return $this->error_response(__('Enabled flag is required.', 'yatra'), 400); |
| 82 |
} |
| 83 |
|
| 84 |
$modules = ModuleManager::getModules(); |
| 85 |
$module_exists = false; |
| 86 |
$target_module = null; |
| 87 |
|
| 88 |
foreach ($modules as $module) { |
| 89 |
if ($module['slug'] === $slug) { |
| 90 |
$module_exists = true; |
| 91 |
$target_module = $module; |
| 92 |
break; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if (!$module_exists || !$target_module) { |
| 97 |
return $this->error_response(__('Module not found.', 'yatra'), 404); |
| 98 |
} |
| 99 |
|
| 100 |
// Check if trying to enable a premium module without Pro |
| 101 |
if ($enabled && !empty($target_module['is_premium']) && !$target_module['is_available']) { |
| 102 |
return $this->error_response( |
| 103 |
sprintf( |
| 104 |
__('%s is a premium module. Yatra Pro is required to enable this module.', 'yatra'), |
| 105 |
$target_module['name'] |
| 106 |
), |
| 107 |
403 |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
// Check if trying to enable a module that requires Pro but Pro is not active |
| 112 |
if ($enabled && !empty($target_module['requires_pro'])) { |
| 113 |
$pro_active = apply_filters('yatra_is_pro_active', false); |
| 114 |
if (!$pro_active) { |
| 115 |
return $this->error_response( |
| 116 |
sprintf( |
| 117 |
__('%s requires Yatra Pro. Please install and activate Yatra Pro to enable this module.', 'yatra'), |
| 118 |
$target_module['name'] |
| 119 |
), |
| 120 |
403 |
| 121 |
); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
$updated = ModuleManager::setModuleStatus($slug, (bool) $enabled); |
| 126 |
|
| 127 |
return $this->success_response([ |
| 128 |
'data' => $updated, |
| 129 |
]); |
| 130 |
} |
| 131 |
|
| 132 |
public function bulk_toggle(WP_REST_Request $request) |
| 133 |
{ |
| 134 |
$items = $request->get_param('items'); |
| 135 |
if (!is_array($items) || empty($items)) { |
| 136 |
return $this->error_response(__('No module changes supplied.', 'yatra'), 400); |
| 137 |
} |
| 138 |
|
| 139 |
$modules = ModuleManager::getModules(); |
| 140 |
$module_map = []; |
| 141 |
foreach ($modules as $module) { |
| 142 |
$module_map[$module['slug']] = $module; |
| 143 |
} |
| 144 |
|
| 145 |
$sanitized = []; |
| 146 |
$blocked_modules = []; |
| 147 |
|
| 148 |
foreach ($items as $item) { |
| 149 |
if (empty($item['slug'])) { |
| 150 |
continue; |
| 151 |
} |
| 152 |
|
| 153 |
$slug = sanitize_key($item['slug']); |
| 154 |
$enabled = filter_var($item['enabled'] ?? false, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); |
| 155 |
if ($enabled === null) { |
| 156 |
continue; |
| 157 |
} |
| 158 |
|
| 159 |
// Check if module exists |
| 160 |
if (!isset($module_map[$slug])) { |
| 161 |
continue; |
| 162 |
} |
| 163 |
|
| 164 |
$target_module = $module_map[$slug]; |
| 165 |
|
| 166 |
// Check if trying to enable a premium module without Pro |
| 167 |
if ($enabled && !empty($target_module['is_premium']) && !$target_module['is_available']) { |
| 168 |
$blocked_modules[] = $target_module['name']; |
| 169 |
continue; |
| 170 |
} |
| 171 |
|
| 172 |
// Check if trying to enable a module that requires Pro but Pro is not active |
| 173 |
if ($enabled && !empty($target_module['requires_pro'])) { |
| 174 |
$pro_active = apply_filters('yatra_is_pro_active', false); |
| 175 |
if (!$pro_active) { |
| 176 |
$blocked_modules[] = $target_module['name']; |
| 177 |
continue; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
$sanitized[] = [ |
| 182 |
'slug' => $slug, |
| 183 |
'enabled' => (bool) $enabled, |
| 184 |
]; |
| 185 |
} |
| 186 |
|
| 187 |
if (empty($sanitized) && !empty($blocked_modules)) { |
| 188 |
return $this->error_response( |
| 189 |
sprintf( |
| 190 |
__('The following modules require Yatra Pro: %s', 'yatra'), |
| 191 |
implode(', ', $blocked_modules) |
| 192 |
), |
| 193 |
403 |
| 194 |
); |
| 195 |
} |
| 196 |
|
| 197 |
if (!empty($blocked_modules)) { |
| 198 |
// Partial success - some modules processed, some blocked |
| 199 |
$updated = ModuleManager::setMultipleStatuses($sanitized); |
| 200 |
|
| 201 |
return $this->success_response([ |
| 202 |
'data' => $updated, |
| 203 |
'message' => sprintf( |
| 204 |
__('Some modules were skipped because they require Yatra Pro: %s', 'yatra'), |
| 205 |
implode(', ', $blocked_modules) |
| 206 |
), |
| 207 |
]); |
| 208 |
} |
| 209 |
|
| 210 |
if (empty($sanitized)) { |
| 211 |
return $this->error_response(__('No valid module changes supplied.', 'yatra'), 400); |
| 212 |
} |
| 213 |
|
| 214 |
$updated = ModuleManager::setMultipleStatuses($sanitized); |
| 215 |
|
| 216 |
return $this->success_response([ |
| 217 |
'data' => $updated, |
| 218 |
]); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|