| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yatra\Controllers; |
| 4 |
|
| 5 |
use Yatra\Services\SampleDataService; |
| 6 |
use Yatra\WordPress\API; |
| 7 |
|
| 8 |
/** |
| 9 |
* Sample Data Controller |
| 10 |
* |
| 11 |
* Handles importing sample data for Yatra plugin |
| 12 |
*/ |
| 13 |
class SampleDataController |
| 14 |
{ |
| 15 |
/** |
| 16 |
* Constructor |
| 17 |
*/ |
| 18 |
public function __construct() |
| 19 |
{ |
| 20 |
add_action('admin_init', [$this, 'handle_import_actions']); |
| 21 |
add_action('admin_notices', [$this, 'show_admin_notices']); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Register REST API routes |
| 26 |
*/ |
| 27 |
public function register_routes() |
| 28 |
{ |
| 29 |
register_rest_route('yatra/v1', '/sample-data/import', [ |
| 30 |
[ |
| 31 |
'methods' => 'POST', |
| 32 |
'callback' => [$this, 'import_sample_data'], |
| 33 |
'permission_callback' => [$this, 'check_permissions'], |
| 34 |
'args' => [ |
| 35 |
'data_types' => [ |
| 36 |
'type' => 'array', |
| 37 |
'items' => [ |
| 38 |
'type' => 'string', |
| 39 |
'enum' => ['trips', 'categories', 'destinations', 'bookings', 'pages'] |
| 40 |
] |
| 41 |
], |
| 42 |
'overwrite' => [ |
| 43 |
'type' => 'boolean', |
| 44 |
'default' => false |
| 45 |
] |
| 46 |
] |
| 47 |
] |
| 48 |
]); |
| 49 |
|
| 50 |
register_rest_route('yatra/v1', '/sample-data/status', [ |
| 51 |
[ |
| 52 |
'methods' => 'GET', |
| 53 |
'callback' => [$this, 'get_import_status'], |
| 54 |
'permission_callback' => [$this, 'check_permissions'] |
| 55 |
] |
| 56 |
]); |
| 57 |
|
| 58 |
register_rest_route('yatra/v1', '/sample-data/cleanup', [ |
| 59 |
[ |
| 60 |
'methods' => 'DELETE', |
| 61 |
'callback' => [$this, 'cleanup_sample_data'], |
| 62 |
'permission_callback' => [$this, 'check_permissions'] |
| 63 |
] |
| 64 |
]); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Sample-data import / cleanup — gated on the settings cap. |
| 69 |
* Installing or removing sample content is a one-time setup |
| 70 |
* task with the same risk profile as other settings changes. |
| 71 |
* WP admins pass via the Team module's admin-fallback filter. |
| 72 |
*/ |
| 73 |
public function check_permissions() |
| 74 |
{ |
| 75 |
return current_user_can('yatra_manage_settings'); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Handle import actions from admin forms |
| 80 |
*/ |
| 81 |
public function handle_import_actions() |
| 82 |
{ |
| 83 |
if (isset($_POST['yatra_import_sample_data']) && |
| 84 |
isset($_POST['_wpnonce']) && |
| 85 |
wp_verify_nonce($_POST['_wpnonce'], 'yatra_import_sample_data')) { |
| 86 |
|
| 87 |
$data_types = isset($_POST['data_types']) ? (array) $_POST['data_types'] : []; |
| 88 |
$overwrite = isset($_POST['overwrite']) && $_POST['overwrite'] === '1'; |
| 89 |
|
| 90 |
$result = $this->perform_import($data_types, $overwrite); |
| 91 |
|
| 92 |
if ($result['success']) { |
| 93 |
wp_redirect(add_query_arg(['yatra_notice' => 'import_success'], wp_get_referer())); |
| 94 |
exit; |
| 95 |
} else { |
| 96 |
wp_redirect(add_query_arg(['yatra_notice' => 'import_error', 'message' => urlencode($result['message'])], wp_get_referer())); |
| 97 |
exit; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
if (isset($_POST['yatra_cleanup_sample_data']) && |
| 102 |
isset($_POST['_wpnonce']) && |
| 103 |
wp_verify_nonce($_POST['_wpnonce'], 'yatra_cleanup_sample_data')) { |
| 104 |
|
| 105 |
$result = $this->perform_cleanup(); |
| 106 |
|
| 107 |
if ($result['success']) { |
| 108 |
wp_redirect(add_query_arg(['yatra_notice' => 'cleanup_success'], wp_get_referer())); |
| 109 |
exit; |
| 110 |
} else { |
| 111 |
wp_redirect(add_query_arg(['yatra_notice' => 'cleanup_error', 'message' => urlencode($result['message'])], wp_get_referer())); |
| 112 |
exit; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Import sample data via REST API |
| 119 |
*/ |
| 120 |
public function import_sample_data($request) |
| 121 |
{ |
| 122 |
$data_types = $request->get_param('data_types'); |
| 123 |
$overwrite = $request->get_param('overwrite'); |
| 124 |
|
| 125 |
$result = $this->perform_import($data_types, $overwrite); |
| 126 |
|
| 127 |
if ($result['success']) { |
| 128 |
return new \WP_REST_Response([ |
| 129 |
'success' => true, |
| 130 |
'message' => $result['message'], |
| 131 |
'data' => $result['data'] |
| 132 |
], 200); |
| 133 |
} else { |
| 134 |
return new \WP_REST_Response([ |
| 135 |
'success' => false, |
| 136 |
'message' => $result['message'] |
| 137 |
], 400); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get import status |
| 143 |
*/ |
| 144 |
public function get_import_status() |
| 145 |
{ |
| 146 |
$service = new SampleDataService(); |
| 147 |
$status = $service->get_import_status(); |
| 148 |
|
| 149 |
return new \WP_REST_Response($status, 200); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Cleanup sample data via REST API |
| 154 |
*/ |
| 155 |
public function cleanup_sample_data($request) |
| 156 |
{ |
| 157 |
$result = $this->perform_cleanup(); |
| 158 |
|
| 159 |
if ($result['success']) { |
| 160 |
return new \WP_REST_Response([ |
| 161 |
'success' => true, |
| 162 |
'message' => $result['message'] |
| 163 |
], 200); |
| 164 |
} else { |
| 165 |
return new \WP_REST_Response([ |
| 166 |
'success' => false, |
| 167 |
'message' => $result['message'] |
| 168 |
], 400); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Perform the actual import |
| 174 |
*/ |
| 175 |
private function perform_import($data_types = [], $overwrite = false) |
| 176 |
{ |
| 177 |
$service = new SampleDataService(); |
| 178 |
|
| 179 |
try { |
| 180 |
// Import all sample data from JSON files |
| 181 |
$result = $service->import_sample_data(); |
| 182 |
|
| 183 |
if ($result['success']) { |
| 184 |
// Store import status |
| 185 |
update_option('yatra_sample_data_imported', true); |
| 186 |
update_option('yatra_sample_data_import_date', current_time('mysql')); |
| 187 |
|
| 188 |
return [ |
| 189 |
'success' => true, |
| 190 |
'message' => $result['message'], |
| 191 |
'data' => $result['data'] |
| 192 |
]; |
| 193 |
} else { |
| 194 |
return [ |
| 195 |
'success' => false, |
| 196 |
'message' => $result['message'] ?? __('Import failed.', 'yatra') |
| 197 |
]; |
| 198 |
} |
| 199 |
} catch (\Exception $e) { |
| 200 |
return [ |
| 201 |
'success' => false, |
| 202 |
'message' => __('An error occurred during import. Please check the error logs.', 'yatra') |
| 203 |
]; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Perform cleanup of sample data |
| 209 |
*/ |
| 210 |
private function perform_cleanup() |
| 211 |
{ |
| 212 |
$service = new SampleDataService(); |
| 213 |
|
| 214 |
try { |
| 215 |
$result = $service->cleanup_sample_data(); |
| 216 |
|
| 217 |
if ($result['success']) { |
| 218 |
// Clear import status |
| 219 |
delete_option('yatra_sample_data_imported'); |
| 220 |
delete_option('yatra_sample_data_import_date'); |
| 221 |
delete_option('yatra_sample_data_types'); |
| 222 |
|
| 223 |
return [ |
| 224 |
'success' => true, |
| 225 |
'message' => __('Sample data cleaned up successfully!', 'yatra') |
| 226 |
]; |
| 227 |
} else { |
| 228 |
return [ |
| 229 |
'success' => false, |
| 230 |
'message' => $result['message'] ?? __('Cleanup failed.', 'yatra') |
| 231 |
]; |
| 232 |
} |
| 233 |
} catch (\Exception $e) { |
| 234 |
return [ |
| 235 |
'success' => false, |
| 236 |
'message' => __('An error occurred during cleanup. Please check the error logs.', 'yatra') |
| 237 |
]; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Show admin notices |
| 243 |
*/ |
| 244 |
public function show_admin_notices() |
| 245 |
{ |
| 246 |
if (isset($_GET['yatra_notice'])) { |
| 247 |
$notice = sanitize_key($_GET['yatra_notice']); |
| 248 |
$message = ''; |
| 249 |
|
| 250 |
switch ($notice) { |
| 251 |
case 'import_success': |
| 252 |
$message = __('Sample data imported successfully!', 'yatra'); |
| 253 |
$class = 'notice-success'; |
| 254 |
break; |
| 255 |
|
| 256 |
case 'import_error': |
| 257 |
$message = isset($_GET['message']) ? |
| 258 |
urldecode(sanitize_text_field($_GET['message'])) : |
| 259 |
__('Import failed. Please try again.', 'yatra'); |
| 260 |
$class = 'notice-error'; |
| 261 |
break; |
| 262 |
|
| 263 |
case 'cleanup_success': |
| 264 |
$message = __('Sample data cleaned up successfully!', 'yatra'); |
| 265 |
$class = 'notice-success'; |
| 266 |
break; |
| 267 |
|
| 268 |
case 'cleanup_error': |
| 269 |
$message = isset($_GET['message']) ? |
| 270 |
urldecode(sanitize_text_field($_GET['message'])) : |
| 271 |
__('Cleanup failed. Please try again.', 'yatra'); |
| 272 |
$class = 'notice-error'; |
| 273 |
break; |
| 274 |
|
| 275 |
default: |
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
printf( |
| 280 |
'<div class="notice %s is-dismissible"><p>%s</p></div>', |
| 281 |
esc_attr($class), |
| 282 |
esc_html($message) |
| 283 |
); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Check if sample data is imported |
| 289 |
*/ |
| 290 |
public static function is_sample_data_imported() |
| 291 |
{ |
| 292 |
return get_option('yatra_sample_data_imported', false); |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Get imported data types |
| 297 |
*/ |
| 298 |
public static function get_imported_data_types() |
| 299 |
{ |
| 300 |
return get_option('yatra_sample_data_types', []); |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Get import date |
| 305 |
*/ |
| 306 |
public static function get_import_date() |
| 307 |
{ |
| 308 |
return get_option('yatra_sample_data_import_date', ''); |
| 309 |
} |
| 310 |
} |
| 311 |
|