| 1 |
<?php |
| 2 |
// If this file is called directly, abort. |
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
|
| 8 |
/** |
| 9 |
* Custom HTML Pages REST API Handler |
| 10 |
* |
| 11 |
* Provides REST API endpoints for creating and managing custom HTML pages |
| 12 |
* |
| 13 |
* @package Metasync |
| 14 |
* @subpackage Metasync/custom-pages |
| 15 |
* @since 2.5.10 |
| 16 |
*/ |
| 17 |
|
| 18 |
class Metasync_Custom_Pages_API |
| 19 |
{ |
| 20 |
/** |
| 21 |
* REST API namespace |
| 22 |
*/ |
| 23 |
private const NAMESPACE = 'metasync/v1'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Initialize the API handler |
| 27 |
*/ |
| 28 |
public function __construct() |
| 29 |
{ |
| 30 |
add_action('rest_api_init', array($this, 'register_routes')); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Register REST API routes |
| 35 |
*/ |
| 36 |
public function register_routes() |
| 37 |
{ |
| 38 |
// Create custom HTML page |
| 39 |
register_rest_route( |
| 40 |
self::NAMESPACE, |
| 41 |
'/custom-pages/create', |
| 42 |
array( |
| 43 |
'methods' => 'POST', |
| 44 |
'callback' => array($this, 'create_custom_html_page'), |
| 45 |
'permission_callback' => array($this, 'validate_api_key'), |
| 46 |
'args' => $this->get_create_page_args() |
| 47 |
) |
| 48 |
); |
| 49 |
|
| 50 |
// Update custom HTML page |
| 51 |
register_rest_route( |
| 52 |
self::NAMESPACE, |
| 53 |
'/custom-pages/update', |
| 54 |
array( |
| 55 |
'methods' => 'POST', |
| 56 |
'callback' => array($this, 'update_custom_html_page'), |
| 57 |
'permission_callback' => array($this, 'validate_api_key'), |
| 58 |
'args' => $this->get_update_page_args() |
| 59 |
) |
| 60 |
); |
| 61 |
|
| 62 |
// Get custom HTML page |
| 63 |
register_rest_route( |
| 64 |
self::NAMESPACE, |
| 65 |
'/custom-pages/get', |
| 66 |
array( |
| 67 |
'methods' => 'GET', |
| 68 |
'callback' => array($this, 'get_custom_html_page'), |
| 69 |
'permission_callback' => array($this, 'validate_api_key'), |
| 70 |
'args' => array( |
| 71 |
'page_id' => array( |
| 72 |
'required' => false, |
| 73 |
'type' => 'integer', |
| 74 |
'description' => 'Page ID' |
| 75 |
), |
| 76 |
'slug' => array( |
| 77 |
'required' => false, |
| 78 |
'type' => 'string', |
| 79 |
'description' => 'Page slug' |
| 80 |
) |
| 81 |
) |
| 82 |
) |
| 83 |
); |
| 84 |
|
| 85 |
// List all custom HTML pages |
| 86 |
register_rest_route( |
| 87 |
self::NAMESPACE, |
| 88 |
'/custom-pages/list', |
| 89 |
array( |
| 90 |
'methods' => 'GET', |
| 91 |
'callback' => array($this, 'list_custom_html_pages'), |
| 92 |
'permission_callback' => array($this, 'validate_api_key') |
| 93 |
) |
| 94 |
); |
| 95 |
|
| 96 |
// Delete custom HTML page |
| 97 |
register_rest_route( |
| 98 |
self::NAMESPACE, |
| 99 |
'/custom-pages/delete', |
| 100 |
array( |
| 101 |
'methods' => 'DELETE', |
| 102 |
'callback' => array($this, 'delete_custom_html_page'), |
| 103 |
'permission_callback' => array($this, 'validate_api_key'), |
| 104 |
'args' => array( |
| 105 |
'page_id' => array( |
| 106 |
'required' => true, |
| 107 |
'type' => 'integer', |
| 108 |
'description' => 'Page ID to delete' |
| 109 |
) |
| 110 |
) |
| 111 |
) |
| 112 |
); |
| 113 |
|
| 114 |
// Import LPS ZIP as custom HTML page |
| 115 |
register_rest_route( |
| 116 |
self::NAMESPACE, |
| 117 |
'/custom-pages/import-zip', |
| 118 |
array( |
| 119 |
'methods' => 'POST', |
| 120 |
'callback' => array($this, 'import_lps_page'), |
| 121 |
'permission_callback' => array($this, 'validate_api_key'), |
| 122 |
'args' => $this->get_import_zip_args() |
| 123 |
) |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Validate API key from request |
| 129 |
* Uses the same authentication as otto_crawl_notify endpoint |
| 130 |
*/ |
| 131 |
public function validate_api_key($request) |
| 132 |
{ |
| 133 |
// Get API key from Authorization: Bearer header (preferred), x-api-key header, or apikey query parameter |
| 134 |
$api_key = ''; |
| 135 |
|
| 136 |
$auth_header = $request->get_header('authorization'); |
| 137 |
if (!empty($auth_header) && preg_match('/^Bearer\s+(.+)$/i', $auth_header, $matches)) { |
| 138 |
$api_key = sanitize_text_field($matches[1]); |
| 139 |
} |
| 140 |
|
| 141 |
if (empty($api_key)) { |
| 142 |
$header_key = $request->get_header('x-api-key'); |
| 143 |
if (!empty($header_key)) { |
| 144 |
$api_key = sanitize_text_field($header_key); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
if (empty($api_key)) { |
| 149 |
$param_key = $request->get_param('apikey'); |
| 150 |
if (!empty($param_key)) { |
| 151 |
$api_key = sanitize_text_field($param_key); |
| 152 |
// Deprecated: ?apikey= query-param auth — switch callers to Authorization: Bearer |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
if (empty($api_key)) { |
| 157 |
return new WP_Error( |
| 158 |
'missing_api_key', |
| 159 |
'API key is required. Provide it via Authorization: Bearer header, x-api-key header, or apikey query parameter.', |
| 160 |
array('status' => 401) |
| 161 |
); |
| 162 |
} |
| 163 |
|
| 164 |
// Get stored API key from settings |
| 165 |
$settings = Metasync::get_option('general'); |
| 166 |
$stored_api_key = $settings['apikey'] ?? null; |
| 167 |
|
| 168 |
if (empty($stored_api_key)) { |
| 169 |
return new WP_Error( |
| 170 |
'api_key_not_configured', |
| 171 |
'API key is not configured in MetaSync settings.', |
| 172 |
array('status' => 500) |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
// Validate API key |
| 177 |
if (!hash_equals($stored_api_key, $api_key)) { |
| 178 |
return new WP_Error( |
| 179 |
'invalid_api_key', |
| 180 |
'Invalid API key provided.', |
| 181 |
array('status' => 403) |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Get arguments for create page endpoint |
| 190 |
*/ |
| 191 |
private function get_create_page_args() |
| 192 |
{ |
| 193 |
return array( |
| 194 |
'title' => array( |
| 195 |
'required' => true, |
| 196 |
'type' => 'string', |
| 197 |
'description' => 'Page title', |
| 198 |
'sanitize_callback' => 'sanitize_text_field' |
| 199 |
), |
| 200 |
'slug' => array( |
| 201 |
'required' => false, |
| 202 |
'type' => 'string', |
| 203 |
'description' => 'Page slug (URL-friendly name)', |
| 204 |
'sanitize_callback' => 'sanitize_title' |
| 205 |
), |
| 206 |
'html_content' => array( |
| 207 |
'required' => true, |
| 208 |
'type' => 'string', |
| 209 |
'description' => 'Raw HTML content' |
| 210 |
), |
| 211 |
'status' => array( |
| 212 |
'required' => false, |
| 213 |
'type' => 'string', |
| 214 |
'default' => 'publish', |
| 215 |
'enum' => array('publish', 'draft', 'pending'), |
| 216 |
'description' => 'Page status' |
| 217 |
), |
| 218 |
'enable_raw_html' => array( |
| 219 |
'required' => false, |
| 220 |
'type' => 'boolean', |
| 221 |
'default' => true, |
| 222 |
'description' => 'Enable raw HTML mode (bypass theme)' |
| 223 |
), |
| 224 |
'filename' => array( |
| 225 |
'required' => false, |
| 226 |
'type' => 'string', |
| 227 |
'description' => 'Original HTML filename (for reference)', |
| 228 |
'sanitize_callback' => 'sanitize_file_name' |
| 229 |
) |
| 230 |
); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Get arguments for update page endpoint |
| 235 |
*/ |
| 236 |
private function get_update_page_args() |
| 237 |
{ |
| 238 |
$args = $this->get_create_page_args(); |
| 239 |
|
| 240 |
// Add page_id as required for updates |
| 241 |
$args['page_id'] = array( |
| 242 |
'required' => true, |
| 243 |
'type' => 'integer', |
| 244 |
'description' => 'Page ID to update' |
| 245 |
); |
| 246 |
|
| 247 |
// Make other fields optional for updates |
| 248 |
$args['title']['required'] = false; |
| 249 |
$args['html_content']['required'] = false; |
| 250 |
|
| 251 |
return $args; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Create a new custom HTML page |
| 256 |
*/ |
| 257 |
public function create_custom_html_page($request) |
| 258 |
{ |
| 259 |
$params = $request->get_params(); |
| 260 |
|
| 261 |
// Prepare page data |
| 262 |
$page_data = array( |
| 263 |
'post_title' => sanitize_text_field($params['title']), |
| 264 |
'post_type' => 'page', |
| 265 |
'post_status' => sanitize_text_field($params['status'] ?? 'publish'), |
| 266 |
'post_content' => '', // We'll store HTML in meta |
| 267 |
); |
| 268 |
|
| 269 |
// Set slug if provided |
| 270 |
if (!empty($params['slug'])) { |
| 271 |
$page_data['post_name'] = sanitize_title($params['slug']); |
| 272 |
} |
| 273 |
|
| 274 |
// Check if page with same slug already exists |
| 275 |
if (!empty($page_data['post_name'])) { |
| 276 |
$existing_page = get_page_by_path($page_data['post_name'], OBJECT, 'page'); |
| 277 |
if ($existing_page) { |
| 278 |
return new WP_Error( |
| 279 |
'page_exists', |
| 280 |
'A page with this slug already exists. Use the update endpoint instead.', |
| 281 |
array('status' => 409) |
| 282 |
); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
// Insert the page |
| 287 |
$page_id = wp_insert_post($page_data, true); |
| 288 |
|
| 289 |
if (is_wp_error($page_id)) { |
| 290 |
return new WP_Error( |
| 291 |
'page_creation_failed', |
| 292 |
'Failed to create page: ' . $page_id->get_error_message(), |
| 293 |
array('status' => 500) |
| 294 |
); |
| 295 |
} |
| 296 |
|
| 297 |
// Mark as custom HTML page |
| 298 |
update_post_meta($page_id, Metasync_Custom_Pages::META_IS_CUSTOM_HTML_PAGE, '1'); |
| 299 |
|
| 300 |
// Mark as created via API |
| 301 |
update_post_meta($page_id, Metasync_Custom_Pages::META_CREATED_VIA_API, '1'); |
| 302 |
|
| 303 |
// Enable raw HTML mode |
| 304 |
$enable_raw_html = isset($params['enable_raw_html']) ? (bool) $params['enable_raw_html'] : true; |
| 305 |
update_post_meta($page_id, Metasync_Custom_Pages::META_HTML_ENABLED, $enable_raw_html ? '1' : '0'); |
| 306 |
|
| 307 |
// Store HTML content |
| 308 |
if (!empty($params['html_content'])) { |
| 309 |
// No sanitization for HTML content - admin users are trusted |
| 310 |
update_post_meta($page_id, Metasync_Custom_Pages::META_HTML_CONTENT, wp_unslash($params['html_content'])); |
| 311 |
} |
| 312 |
|
| 313 |
// Store filename if provided |
| 314 |
if (!empty($params['filename'])) { |
| 315 |
update_post_meta($page_id, Metasync_Custom_Pages::META_HTML_FILENAME, sanitize_file_name($params['filename'])); |
| 316 |
} |
| 317 |
|
| 318 |
// Clear cache |
| 319 |
wp_cache_delete($page_id, 'posts'); |
| 320 |
wp_cache_delete($page_id, 'post_meta'); |
| 321 |
|
| 322 |
// Return success response |
| 323 |
return rest_ensure_response(array( |
| 324 |
'success' => true, |
| 325 |
'message' => 'Custom HTML page created successfully', |
| 326 |
'data' => array( |
| 327 |
'page_id' => $page_id, |
| 328 |
'title' => get_the_title($page_id), |
| 329 |
'slug' => get_post_field('post_name', $page_id), |
| 330 |
'url' => get_permalink($page_id), |
| 331 |
'edit_url' => get_edit_post_link($page_id, 'raw'), |
| 332 |
'status' => get_post_status($page_id), |
| 333 |
'raw_html_enabled' => $enable_raw_html, |
| 334 |
'html_length' => strlen($params['html_content'] ?? ''), |
| 335 |
'created_at' => get_post_field('post_date', $page_id) |
| 336 |
) |
| 337 |
)); |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Update an existing custom HTML page |
| 342 |
*/ |
| 343 |
public function update_custom_html_page($request) |
| 344 |
{ |
| 345 |
$params = $request->get_params(); |
| 346 |
$page_id = intval($params['page_id']); |
| 347 |
|
| 348 |
// Verify page exists |
| 349 |
$page = get_post($page_id); |
| 350 |
if (!$page || $page->post_type !== 'page') { |
| 351 |
return new WP_Error( |
| 352 |
'page_not_found', |
| 353 |
'Page not found with ID: ' . $page_id, |
| 354 |
array('status' => 404) |
| 355 |
); |
| 356 |
} |
| 357 |
|
| 358 |
// Verify it's a custom HTML page |
| 359 |
$is_custom_html_page = get_post_meta($page_id, Metasync_Custom_Pages::META_IS_CUSTOM_HTML_PAGE, true); |
| 360 |
if ($is_custom_html_page !== '1') { |
| 361 |
return new WP_Error( |
| 362 |
'not_custom_html_page', |
| 363 |
'This page is not a custom HTML page.', |
| 364 |
array('status' => 400) |
| 365 |
); |
| 366 |
} |
| 367 |
|
| 368 |
// Update page data if provided |
| 369 |
$update_data = array('ID' => $page_id); |
| 370 |
|
| 371 |
if (isset($params['title'])) { |
| 372 |
$update_data['post_title'] = sanitize_text_field($params['title']); |
| 373 |
} |
| 374 |
|
| 375 |
if (isset($params['slug'])) { |
| 376 |
$update_data['post_name'] = sanitize_title($params['slug']); |
| 377 |
} |
| 378 |
|
| 379 |
if (isset($params['status'])) { |
| 380 |
$update_data['post_status'] = sanitize_text_field($params['status']); |
| 381 |
} |
| 382 |
|
| 383 |
// Update page if there are changes |
| 384 |
if (count($update_data) > 1) { |
| 385 |
$result = wp_update_post($update_data, true); |
| 386 |
if (is_wp_error($result)) { |
| 387 |
return new WP_Error( |
| 388 |
'page_update_failed', |
| 389 |
'Failed to update page: ' . $result->get_error_message(), |
| 390 |
array('status' => 500) |
| 391 |
); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
// Update HTML content if provided |
| 396 |
if (isset($params['html_content'])) { |
| 397 |
update_post_meta($page_id, Metasync_Custom_Pages::META_HTML_CONTENT, wp_unslash($params['html_content'])); |
| 398 |
} |
| 399 |
|
| 400 |
// Update raw HTML mode if provided |
| 401 |
if (isset($params['enable_raw_html'])) { |
| 402 |
$enable_raw_html = (bool) $params['enable_raw_html']; |
| 403 |
update_post_meta($page_id, Metasync_Custom_Pages::META_HTML_ENABLED, $enable_raw_html ? '1' : '0'); |
| 404 |
} |
| 405 |
|
| 406 |
// Update filename if provided |
| 407 |
if (isset($params['filename'])) { |
| 408 |
update_post_meta($page_id, Metasync_Custom_Pages::META_HTML_FILENAME, sanitize_file_name($params['filename'])); |
| 409 |
} |
| 410 |
|
| 411 |
// Clear cache |
| 412 |
wp_cache_delete($page_id, 'posts'); |
| 413 |
wp_cache_delete($page_id, 'post_meta'); |
| 414 |
|
| 415 |
// Return success response |
| 416 |
return rest_ensure_response(array( |
| 417 |
'success' => true, |
| 418 |
'message' => 'Custom HTML page updated successfully', |
| 419 |
'data' => array( |
| 420 |
'page_id' => $page_id, |
| 421 |
'title' => get_the_title($page_id), |
| 422 |
'slug' => get_post_field('post_name', $page_id), |
| 423 |
'url' => get_permalink($page_id), |
| 424 |
'edit_url' => get_edit_post_link($page_id, 'raw'), |
| 425 |
'status' => get_post_status($page_id), |
| 426 |
'raw_html_enabled' => get_post_meta($page_id, Metasync_Custom_Pages::META_HTML_ENABLED, true) === '1', |
| 427 |
'html_length' => strlen(get_post_meta($page_id, Metasync_Custom_Pages::META_HTML_CONTENT, true)), |
| 428 |
'updated_at' => get_post_field('post_modified', $page_id) |
| 429 |
) |
| 430 |
)); |
| 431 |
} |
| 432 |
|
| 433 |
/** |
| 434 |
* Get a custom HTML page |
| 435 |
*/ |
| 436 |
public function get_custom_html_page($request) |
| 437 |
{ |
| 438 |
$page_id = $request->get_param('page_id'); |
| 439 |
$slug = $request->get_param('slug'); |
| 440 |
|
| 441 |
if (empty($page_id) && empty($slug)) { |
| 442 |
return new WP_Error( |
| 443 |
'missing_identifier', |
| 444 |
'Either page_id or slug must be provided.', |
| 445 |
array('status' => 400) |
| 446 |
); |
| 447 |
} |
| 448 |
|
| 449 |
// Get page by ID or slug |
| 450 |
if (!empty($page_id)) { |
| 451 |
$page = get_post(intval($page_id)); |
| 452 |
} else { |
| 453 |
$page = get_page_by_path($slug, OBJECT, 'page'); |
| 454 |
} |
| 455 |
|
| 456 |
if (!$page || $page->post_type !== 'page') { |
| 457 |
return new WP_Error( |
| 458 |
'page_not_found', |
| 459 |
'Page not found.', |
| 460 |
array('status' => 404) |
| 461 |
); |
| 462 |
} |
| 463 |
|
| 464 |
// Verify it's a custom HTML page |
| 465 |
$is_custom_html_page = get_post_meta($page->ID, Metasync_Custom_Pages::META_IS_CUSTOM_HTML_PAGE, true); |
| 466 |
if ($is_custom_html_page !== '1') { |
| 467 |
return new WP_Error( |
| 468 |
'not_custom_html_page', |
| 469 |
'This page is not a custom HTML page.', |
| 470 |
array('status' => 400) |
| 471 |
); |
| 472 |
} |
| 473 |
|
| 474 |
// Get HTML content |
| 475 |
$html_content = get_post_meta($page->ID, Metasync_Custom_Pages::META_HTML_CONTENT, true); |
| 476 |
$html_enabled = get_post_meta($page->ID, Metasync_Custom_Pages::META_HTML_ENABLED, true); |
| 477 |
$html_filename = get_post_meta($page->ID, Metasync_Custom_Pages::META_HTML_FILENAME, true); |
| 478 |
|
| 479 |
return rest_ensure_response(array( |
| 480 |
'success' => true, |
| 481 |
'data' => array( |
| 482 |
'page_id' => $page->ID, |
| 483 |
'title' => $page->post_title, |
| 484 |
'slug' => $page->post_name, |
| 485 |
'url' => get_permalink($page->ID), |
| 486 |
'edit_url' => get_edit_post_link($page->ID, 'raw'), |
| 487 |
'status' => $page->post_status, |
| 488 |
'raw_html_enabled' => $html_enabled === '1', |
| 489 |
'html_content' => $html_content, |
| 490 |
'html_filename' => $html_filename, |
| 491 |
'html_length' => strlen($html_content), |
| 492 |
'created_at' => $page->post_date, |
| 493 |
'updated_at' => $page->post_modified |
| 494 |
) |
| 495 |
)); |
| 496 |
} |
| 497 |
|
| 498 |
/** |
| 499 |
* List all custom HTML pages |
| 500 |
*/ |
| 501 |
public function list_custom_html_pages($request) |
| 502 |
{ |
| 503 |
$pages = Metasync_Custom_Pages::get_custom_pages(); |
| 504 |
|
| 505 |
$pages_data = array(); |
| 506 |
foreach ($pages as $page) { |
| 507 |
$html_enabled = get_post_meta($page->ID, Metasync_Custom_Pages::META_HTML_ENABLED, true); |
| 508 |
$html_content = get_post_meta($page->ID, Metasync_Custom_Pages::META_HTML_CONTENT, true); |
| 509 |
$html_filename = get_post_meta($page->ID, Metasync_Custom_Pages::META_HTML_FILENAME, true); |
| 510 |
|
| 511 |
$pages_data[] = array( |
| 512 |
'page_id' => $page->ID, |
| 513 |
'title' => $page->post_title, |
| 514 |
'slug' => $page->post_name, |
| 515 |
'url' => get_permalink($page->ID), |
| 516 |
'edit_url' => get_edit_post_link($page->ID, 'raw'), |
| 517 |
'status' => $page->post_status, |
| 518 |
'raw_html_enabled' => $html_enabled === '1', |
| 519 |
'html_filename' => $html_filename, |
| 520 |
'html_length' => strlen($html_content), |
| 521 |
'created_at' => $page->post_date, |
| 522 |
'updated_at' => $page->post_modified |
| 523 |
); |
| 524 |
} |
| 525 |
|
| 526 |
return rest_ensure_response(array( |
| 527 |
'success' => true, |
| 528 |
'count' => count($pages_data), |
| 529 |
'data' => $pages_data |
| 530 |
)); |
| 531 |
} |
| 532 |
|
| 533 |
/** |
| 534 |
* Delete a custom HTML page and clean up its on-disk assets. |
| 535 |
* |
| 536 |
* Shared implementation behind both the REST endpoint and the MCP delete tool |
| 537 |
* so the two paths cannot drift. Performs: page validation, front-page reset |
| 538 |
* (when the page is the static front page), permanent deletion, and |
| 539 |
* reference-counted removal of the LPS asset folder. |
| 540 |
* |
| 541 |
* @param int $page_id |
| 542 |
* @return array|WP_Error Result payload on success, WP_Error (with status) on failure. |
| 543 |
*/ |
| 544 |
public function delete_custom_page_with_cleanup($page_id) |
| 545 |
{ |
| 546 |
$page_id = intval($page_id); |
| 547 |
|
| 548 |
// Verify page exists |
| 549 |
$page = get_post($page_id); |
| 550 |
if (!$page || $page->post_type !== 'page') { |
| 551 |
return new WP_Error( |
| 552 |
'page_not_found', |
| 553 |
'Page not found with ID: ' . $page_id, |
| 554 |
array('status' => 404) |
| 555 |
); |
| 556 |
} |
| 557 |
|
| 558 |
// Verify it's a custom HTML page |
| 559 |
$is_custom_html_page = get_post_meta($page_id, Metasync_Custom_Pages::META_IS_CUSTOM_HTML_PAGE, true); |
| 560 |
if ($is_custom_html_page !== '1') { |
| 561 |
return new WP_Error( |
| 562 |
'not_custom_html_page', |
| 563 |
'This page is not a custom HTML page.', |
| 564 |
array('status' => 400) |
| 565 |
); |
| 566 |
} |
| 567 |
|
| 568 |
// Capture the on-disk assets folder BEFORE deleting the post — its meta is |
| 569 |
// gone afterwards. Prefer the recorded folder (it can differ from the slug); |
| 570 |
// fall back to the slug for pages imported before this was tracked. |
| 571 |
$assets_folder = get_post_meta($page_id, Metasync_Custom_Pages::META_ASSETS_FOLDER, true); |
| 572 |
if (empty($assets_folder)) { |
| 573 |
$assets_folder = $page->post_name; |
| 574 |
} |
| 575 |
|
| 576 |
// Capture whether this page is the static front page BEFORE deleting it - |
| 577 |
// WordPress/plugins may clear page_on_front during wp_delete_post(), so a |
| 578 |
// post-delete read could miss it. |
| 579 |
$was_front_page = ((int) get_option('page_on_front') === $page_id); |
| 580 |
|
| 581 |
// Delete the page permanently |
| 582 |
$result = wp_delete_post($page_id, true); |
| 583 |
|
| 584 |
if (!$result) { |
| 585 |
return new WP_Error( |
| 586 |
'delete_failed', |
| 587 |
'Failed to delete page.', |
| 588 |
array('status' => 500) |
| 589 |
); |
| 590 |
} |
| 591 |
|
| 592 |
// Reset the WordPress static front page when this page WAS it (captured |
| 593 |
// before the delete; some setups clear page_on_front during wp_delete_post(), |
| 594 |
// so reading it afterwards would miss the match). Idempotent + only on success. |
| 595 |
$front_page_reset = false; |
| 596 |
if ($was_front_page) { |
| 597 |
update_option('show_on_front', 'posts'); |
| 598 |
delete_option('page_on_front'); |
| 599 |
$front_page_reset = true; |
| 600 |
} |
| 601 |
|
| 602 |
// Remove the LPS asset folder associated with this page — but only if no |
| 603 |
// other page still references it. Multi-page imports share one folder across |
| 604 |
// many pages, so deleting one page must not wipe assets the remaining pages |
| 605 |
// still need (reference counting). |
| 606 |
$assets_removed = false; |
| 607 |
$assets_still_referenced = false; |
| 608 |
if (!empty($assets_folder) && strpos($assets_folder, '..') === false && strpos($assets_folder, '/') === false && strpos($assets_folder, '\\') === false) { |
| 609 |
$others = get_posts(array( |
| 610 |
'post_type' => 'page', |
| 611 |
// 'any' + 'trash': bare 'any' excludes trashed posts, so a trashed |
| 612 |
// sibling sharing this folder would be missed and its assets wiped |
| 613 |
// (breaking it on restore). Listing 'trash' alongside 'any' removes it |
| 614 |
// from the exclusion set while still matching every other status — |
| 615 |
// including custom plugin statuses, which a hardcoded list would drop. |
| 616 |
'post_status' => array('any', 'trash'), |
| 617 |
'numberposts' => 1, |
| 618 |
'fields' => 'ids', |
| 619 |
'no_found_rows' => true, |
| 620 |
'suppress_filters' => true, |
| 621 |
'exclude' => array($page_id), |
| 622 |
'meta_key' => Metasync_Custom_Pages::META_ASSETS_FOLDER, |
| 623 |
'meta_value' => $assets_folder, |
| 624 |
)); |
| 625 |
if (!empty($others)) { |
| 626 |
// Other pages still use this folder — keep it. |
| 627 |
$assets_still_referenced = true; |
| 628 |
} else { |
| 629 |
$upload_dir = wp_upload_dir(); |
| 630 |
if (empty($upload_dir['error'])) { |
| 631 |
$base_dir = trailingslashit($upload_dir['basedir']) . 'metasync-pages'; |
| 632 |
$target_dir = trailingslashit($base_dir) . $assets_folder; |
| 633 |
$base_real = realpath($base_dir); |
| 634 |
$target_real = realpath($target_dir); |
| 635 |
if ($base_real !== false && $target_real !== false && strpos($target_real, $base_real) === 0) { |
| 636 |
$this->recursive_rmdir($target_real); |
| 637 |
$assets_removed = !file_exists($target_real); |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
return array( |
| 644 |
'page_id' => $page_id, |
| 645 |
'title' => $page->post_title, |
| 646 |
'assets_removed' => $assets_removed, |
| 647 |
'assets_folder_still_referenced' => $assets_still_referenced, |
| 648 |
'front_page_reset' => $front_page_reset, |
| 649 |
); |
| 650 |
} |
| 651 |
|
| 652 |
/** |
| 653 |
* Delete a custom HTML page (REST endpoint). |
| 654 |
* |
| 655 |
* Delegates to the shared delete_custom_page_with_cleanup() implementation. |
| 656 |
*/ |
| 657 |
public function delete_custom_html_page($request) |
| 658 |
{ |
| 659 |
$page_id = intval($request->get_param('page_id')); |
| 660 |
|
| 661 |
$result = $this->delete_custom_page_with_cleanup($page_id); |
| 662 |
if (is_wp_error($result)) { |
| 663 |
// Return the WP_Error directly so the REST framework maps its embedded |
| 664 |
// status (404/400/500) to the HTTP status code. |
| 665 |
return $result; |
| 666 |
} |
| 667 |
|
| 668 |
return rest_ensure_response(array( |
| 669 |
'success' => true, |
| 670 |
'message' => 'Custom HTML page deleted successfully', |
| 671 |
'data' => $result |
| 672 |
)); |
| 673 |
} |
| 674 |
|
| 675 |
/** |
| 676 |
* Get arguments for the import-zip endpoint |
| 677 |
*/ |
| 678 |
private function get_import_zip_args() |
| 679 |
{ |
| 680 |
return array( |
| 681 |
'slug' => array( |
| 682 |
'required' => false, |
| 683 |
'type' => 'string', |
| 684 |
'description' => 'Page slug (single-page imports only). Required when the ZIP has no pages.manifest.json; ignored for multi-page bundles, which take slugs from the manifest.' |
| 685 |
), |
| 686 |
'title' => array( |
| 687 |
'required' => false, |
| 688 |
'type' => 'string', |
| 689 |
'description' => 'Page title (single-page imports only). Ignored for multi-page bundles, which take titles from the manifest.', |
| 690 |
'sanitize_callback' => 'sanitize_text_field' |
| 691 |
), |
| 692 |
'status' => array( |
| 693 |
'required' => false, |
| 694 |
'type' => 'string', |
| 695 |
'default' => 'publish', |
| 696 |
'enum' => array('publish', 'draft', 'pending'), |
| 697 |
'description' => 'Page status' |
| 698 |
), |
| 699 |
'overwrite' => array( |
| 700 |
'required' => false, |
| 701 |
'type' => 'boolean', |
| 702 |
'default' => true, |
| 703 |
'description' => 'When true, an existing page with the same slug is overwritten.' |
| 704 |
), |
| 705 |
'download_url' => array( |
| 706 |
'required' => false, |
| 707 |
'type' => 'string', |
| 708 |
'description' => 'URL to download the LPS ZIP from. If provided, the ZIP is fetched server-side instead of requiring a multipart upload.', |
| 709 |
'sanitize_callback' => 'esc_url_raw' |
| 710 |
), |
| 711 |
'assets_folder' => array( |
| 712 |
'required' => false, |
| 713 |
'type' => 'string', |
| 714 |
'description' => 'On-disk folder name the bundle is extracted to (under uploads/metasync-pages/). This is the path LPS bakes into the asset URLs at build time and is intentionally separate from the page slug. Defaults to the slug when omitted.' |
| 715 |
), |
| 716 |
'external_ref' => array( |
| 717 |
'required' => false, |
| 718 |
'type' => 'string', |
| 719 |
'description' => 'LPS project UUID used as the stable per-project home dedup key.', |
| 720 |
'sanitize_callback' => 'sanitize_text_field' |
| 721 |
) |
| 722 |
); |
| 723 |
} |
| 724 |
|
| 725 |
/** |
| 726 |
* REST callback: import LPS ZIP as a custom HTML page. |
| 727 |
* |
| 728 |
* Accepts either a JSON body with download_url (preferred) or a multipart zip_file upload. |
| 729 |
*/ |
| 730 |
public function import_lps_page($request) |
| 731 |
{ |
| 732 |
// --- Audit tracking (one persistent record is written on every exit path) --- |
| 733 |
$_lps_start_ms = (int) round(microtime(true) * 1000); |
| 734 |
$_lps_result = null; // WP_Error|array — set before each return |
| 735 |
$_lps_http_st = 500; // updated before each return |
| 736 |
$_lps_input = array( |
| 737 |
'source_type' => 'unknown', |
| 738 |
'zip_size_bytes' => 0, |
| 739 |
'assets_folder' => '', |
| 740 |
'pages_in_manifest' => null, |
| 741 |
); |
| 742 |
|
| 743 |
// Re-derive the auth method + masked key prefix using the same precedence as |
| 744 |
// validate_api_key() (Bearer header, then x-api-key header, then apikey param). |
| 745 |
$_lps_auth_method = ''; |
| 746 |
$_lps_api_key_prefix = ''; |
| 747 |
$_lps_matched_key = ''; |
| 748 |
$_lps_auth_header = $request->get_header('authorization'); |
| 749 |
if (!empty($_lps_auth_header) && preg_match('/^Bearer\s+(.+)$/i', $_lps_auth_header, $_lps_m)) { |
| 750 |
$_lps_auth_method = 'bearer'; |
| 751 |
$_lps_matched_key = sanitize_text_field($_lps_m[1]); |
| 752 |
} |
| 753 |
if ($_lps_matched_key === '') { |
| 754 |
$_lps_hk = $request->get_header('x-api-key'); |
| 755 |
if (!empty($_lps_hk)) { |
| 756 |
$_lps_auth_method = 'x-api-key'; |
| 757 |
$_lps_matched_key = sanitize_text_field($_lps_hk); |
| 758 |
} |
| 759 |
} |
| 760 |
if ($_lps_matched_key === '') { |
| 761 |
$_lps_pk = $request->get_param('apikey'); |
| 762 |
if (!empty($_lps_pk)) { |
| 763 |
$_lps_auth_method = 'apikey-query'; |
| 764 |
$_lps_matched_key = sanitize_text_field($_lps_pk); |
| 765 |
} |
| 766 |
} |
| 767 |
if (strlen($_lps_matched_key) >= 8) { |
| 768 |
$_lps_api_key_prefix = substr($_lps_matched_key, 0, 8) . '...'; |
| 769 |
} |
| 770 |
|
| 771 |
try { |
| 772 |
$raw_slug = $request->get_param('slug'); |
| 773 |
if (is_string($raw_slug) && (strpos($raw_slug, '..') !== false || strpos($raw_slug, '/') !== false || strpos($raw_slug, '\\') !== false)) { |
| 774 |
$_lps_http_st = 400; |
| 775 |
$_lps_result = new WP_Error( |
| 776 |
'invalid_slug_path', |
| 777 |
'Slug must not contain path separators or parent references.', |
| 778 |
array('status' => 400) |
| 779 |
); |
| 780 |
return $_lps_result; |
| 781 |
} |
| 782 |
|
| 783 |
$params = $request->get_params(); |
| 784 |
$slug = isset($params['slug']) ? sanitize_title($params['slug']) : ''; |
| 785 |
$title = isset($params['title']) ? sanitize_text_field($params['title']) : ''; |
| 786 |
$status = isset($params['status']) ? sanitize_text_field($params['status']) : 'publish'; |
| 787 |
$overwrite = isset($params['overwrite']) ? (bool) $params['overwrite'] : true; |
| 788 |
$download_url = isset($params['download_url']) ? $params['download_url'] : ''; |
| 789 |
$assets_folder = isset($params['assets_folder']) ? $params['assets_folder'] : ''; |
| 790 |
$_lps_input['assets_folder'] = $assets_folder; |
| 791 |
$external_ref = isset($params['external_ref']) ? sanitize_text_field($params['external_ref']) : ''; |
| 792 |
$_lps_input['external_ref'] = $external_ref; |
| 793 |
|
| 794 |
$zip_path = ''; |
| 795 |
$tmp_file = null; |
| 796 |
|
| 797 |
$max_zip_bytes = 50 * 1024 * 1024; |
| 798 |
|
| 799 |
try { |
| 800 |
if (!empty($download_url)) { |
| 801 |
$_lps_input['source_type'] = 'download_url'; |
| 802 |
// wp_tempnam() lives in wp-admin/includes/file.php, which is NOT loaded |
| 803 |
// during a normal REST request — load it so this works in any context. |
| 804 |
if (!function_exists('wp_tempnam')) { |
| 805 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 806 |
} |
| 807 |
$tmp_file = wp_tempnam('lps_import_'); |
| 808 |
if (!$tmp_file) { |
| 809 |
$_lps_http_st = 500; |
| 810 |
$_lps_result = new WP_Error('tmp_file_failed', 'Failed to create temporary file.', array('status' => 500)); |
| 811 |
return $_lps_result; |
| 812 |
} |
| 813 |
|
| 814 |
$response = wp_safe_remote_get($download_url, array( |
| 815 |
'timeout' => 60, |
| 816 |
'stream' => true, |
| 817 |
'filename' => $tmp_file, |
| 818 |
)); |
| 819 |
|
| 820 |
if (is_wp_error($response)) { |
| 821 |
$_lps_http_st = 400; |
| 822 |
$_lps_result = new WP_Error('zip_download_failed', 'Failed to download ZIP: ' . $response->get_error_message(), array('status' => 400)); |
| 823 |
return $_lps_result; |
| 824 |
} |
| 825 |
|
| 826 |
$code = wp_remote_retrieve_response_code($response); |
| 827 |
if ((int) $code !== 200) { |
| 828 |
$_lps_http_st = 400; |
| 829 |
$_lps_result = new WP_Error('zip_download_http_error', 'ZIP download returned HTTP ' . intval($code), array('status' => 400)); |
| 830 |
return $_lps_result; |
| 831 |
} |
| 832 |
|
| 833 |
$content_type = strtolower((string) wp_remote_retrieve_header($response, 'content-type')); |
| 834 |
if ($content_type !== '' && strpos($content_type, 'zip') === false && strpos($content_type, 'octet-stream') === false) { |
| 835 |
$_lps_http_st = 400; |
| 836 |
$_lps_result = new WP_Error('invalid_zip_content_type', 'Downloaded file is not a ZIP (Content-Type: ' . sanitize_text_field($content_type) . ').', array('status' => 400)); |
| 837 |
return $_lps_result; |
| 838 |
} |
| 839 |
|
| 840 |
$downloaded_size = file_exists($tmp_file) ? filesize($tmp_file) : 0; |
| 841 |
$_lps_input['zip_size_bytes'] = $downloaded_size; |
| 842 |
if ($downloaded_size === 0) { |
| 843 |
$_lps_http_st = 400; |
| 844 |
$_lps_result = new WP_Error('zip_download_empty', 'Downloaded ZIP file is empty.', array('status' => 400)); |
| 845 |
return $_lps_result; |
| 846 |
} |
| 847 |
if ($downloaded_size > $max_zip_bytes) { |
| 848 |
$_lps_http_st = 413; |
| 849 |
$_lps_result = new WP_Error('zip_too_large', 'Downloaded ZIP exceeds the maximum allowed size (' . intval($max_zip_bytes / 1024 / 1024) . ' MB).', array('status' => 413)); |
| 850 |
return $_lps_result; |
| 851 |
} |
| 852 |
|
| 853 |
$zip_path = $tmp_file; |
| 854 |
|
| 855 |
} elseif (!empty($_FILES['zip_file']) && is_array($_FILES['zip_file'])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- REST route authenticated by API key, not cookies |
| 856 |
$file = $_FILES['zip_file']; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- REST route authenticated by API key, not cookies |
| 857 |
$_lps_input['source_type'] = 'upload'; |
| 858 |
|
| 859 |
if (!isset($file['error']) || $file['error'] !== UPLOAD_ERR_OK) { |
| 860 |
$_lps_http_st = 400; |
| 861 |
$_lps_result = new WP_Error('zip_upload_error', 'Uploaded ZIP file has an error code: ' . (isset($file['error']) ? intval($file['error']) : 'unknown'), array('status' => 400)); |
| 862 |
return $_lps_result; |
| 863 |
} |
| 864 |
|
| 865 |
if (empty($file['tmp_name']) || !is_uploaded_file($file['tmp_name'])) { |
| 866 |
$_lps_http_st = 400; |
| 867 |
$_lps_result = new WP_Error('invalid_zip_upload', 'Invalid ZIP upload.', array('status' => 400)); |
| 868 |
return $_lps_result; |
| 869 |
} |
| 870 |
|
| 871 |
$allowed_mime_types = array('application/zip', 'application/octet-stream', 'application/x-zip-compressed', 'multipart/x-zip'); |
| 872 |
if (!empty($file['type']) && !in_array($file['type'], $allowed_mime_types, true)) { |
| 873 |
$_lps_http_st = 400; |
| 874 |
$_lps_result = new WP_Error('invalid_zip_mime', 'Uploaded file is not a ZIP archive (type: ' . sanitize_text_field($file['type']) . ').', array('status' => 400)); |
| 875 |
return $_lps_result; |
| 876 |
} |
| 877 |
|
| 878 |
$uploaded_size = isset($file['size']) ? (int) $file['size'] : 0; |
| 879 |
$_lps_input['zip_size_bytes'] = $uploaded_size; |
| 880 |
if ($uploaded_size > $max_zip_bytes) { |
| 881 |
$_lps_http_st = 413; |
| 882 |
$_lps_result = new WP_Error('zip_too_large', 'Uploaded ZIP exceeds the maximum allowed size (' . intval($max_zip_bytes / 1024 / 1024) . ' MB).', array('status' => 413)); |
| 883 |
return $_lps_result; |
| 884 |
} |
| 885 |
|
| 886 |
$zip_path = $file['tmp_name']; |
| 887 |
|
| 888 |
} else { |
| 889 |
$_lps_http_st = 400; |
| 890 |
$_lps_result = new WP_Error('missing_zip_source', 'Provide either a download_url or a multipart zip_file upload.', array('status' => 400)); |
| 891 |
return $_lps_result; |
| 892 |
} |
| 893 |
|
| 894 |
$result = $this->extract_and_create_lps_page($zip_path, $slug, $title, $status, $overwrite, $assets_folder, $external_ref); |
| 895 |
$_lps_result = $result; |
| 896 |
|
| 897 |
if (is_wp_error($result)) { |
| 898 |
$_err_data = $result->get_error_data(); |
| 899 |
$_lps_http_st = (int) ((is_array($_err_data) && isset($_err_data['status'])) ? $_err_data['status'] : 500); |
| 900 |
return $result; |
| 901 |
} |
| 902 |
|
| 903 |
// Map the import outcome to an HTTP status: |
| 904 |
// 200 — all pages created/updated |
| 905 |
// 207 — partial success (some pages failed) |
| 906 |
// 422 — every page failed |
| 907 |
// (Single-page slug conflicts already return 409 as a WP_Error above.) |
| 908 |
$status_code = 200; |
| 909 |
$data = (isset($result['data']) && is_array($result['data'])) ? $result['data'] : array(); |
| 910 |
if (isset($data['mode']) && $data['mode'] === 'multi') { |
| 911 |
$_lps_input['pages_in_manifest'] = isset($data['pages_total']) ? (int) $data['pages_total'] : null; |
| 912 |
$succeeded = count(isset($data['created']) ? $data['created'] : array()) |
| 913 |
+ count(isset($data['updated']) ? $data['updated'] : array()); |
| 914 |
$failed = count(isset($data['failed']) ? $data['failed'] : array()); |
| 915 |
if ($failed > 0 && $succeeded > 0) { |
| 916 |
$status_code = 207; |
| 917 |
} elseif ($failed > 0 && $succeeded === 0) { |
| 918 |
$status_code = 422; |
| 919 |
$result['success'] = false; |
| 920 |
$_lps_result = $result; |
| 921 |
} |
| 922 |
} elseif (isset($data['mode']) && $data['mode'] === 'single') { |
| 923 |
$_lps_input['pages_in_manifest'] = 1; |
| 924 |
} |
| 925 |
|
| 926 |
$_lps_http_st = $status_code; |
| 927 |
$response = rest_ensure_response($result); |
| 928 |
$response->set_status($status_code); |
| 929 |
return $response; |
| 930 |
|
| 931 |
} finally { |
| 932 |
if (!empty($tmp_file) && file_exists($tmp_file)) { |
| 933 |
@wp_delete_file($tmp_file); |
| 934 |
} |
| 935 |
} |
| 936 |
} finally { |
| 937 |
self::write_lps_import_audit(array( |
| 938 |
'result' => $_lps_result, |
| 939 |
'http_status' => $_lps_http_st, |
| 940 |
'input' => $_lps_input, |
| 941 |
'start_ms' => $_lps_start_ms, |
| 942 |
'auth_method' => $_lps_auth_method, |
| 943 |
'api_key_prefix' => $_lps_api_key_prefix, |
| 944 |
)); |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
/** |
| 949 |
* Shared helper: extract a ZIP archive and create or update a custom HTML page. |
| 950 |
* |
| 951 |
* Made public so the MCP tool (in a different class) can invoke it directly. |
| 952 |
* |
| 953 |
* @param string $zip_path Filesystem path to the ZIP file. |
| 954 |
* @param string $slug Page slug (will be re-sanitized). |
| 955 |
* @param string $title Page title. |
| 956 |
* @param string $status Page status (publish|draft|pending). |
| 957 |
* @param bool $overwrite If true, existing page+assets with same slug are overwritten. |
| 958 |
* @param string $assets_folder On-disk folder name to extract into (under metasync-pages/). |
| 959 |
* Defaults to the slug when empty. Must match the path LPS baked |
| 960 |
* into the bundle's asset URLs at build time. |
| 961 |
* @param string $external_ref Stable per-project LPS UUID. When non-empty it is stored as |
| 962 |
* post meta and used as the primary home-page dedup key. |
| 963 |
* @return array|WP_Error Response array on success, WP_Error on failure. |
| 964 |
*/ |
| 965 |
public function extract_and_create_lps_page($zip_path, $slug, $title, $status = 'publish', $overwrite = true, $assets_folder = '', $external_ref = '') |
| 966 |
{ |
| 967 |
if (!class_exists('ZipArchive')) { |
| 968 |
return new WP_Error( |
| 969 |
'zip_unavailable', |
| 970 |
'ZipArchive PHP extension is not available on this server.', |
| 971 |
array('status' => 500) |
| 972 |
); |
| 973 |
} |
| 974 |
|
| 975 |
// Basic slug sanity (single-page slugs never contain path separators). Empty |
| 976 |
// is allowed here because multi-page imports take their slugs from the manifest. |
| 977 |
if (is_string($slug) && $slug !== '' && (strpos($slug, '..') !== false || strpos($slug, '/') !== false || strpos($slug, '\\') !== false)) { |
| 978 |
return new WP_Error( |
| 979 |
'invalid_slug_path', |
| 980 |
'Slug must not contain path separators or parent references.', |
| 981 |
array('status' => 400) |
| 982 |
); |
| 983 |
} |
| 984 |
$slug = is_string($slug) ? sanitize_title($slug) : ''; |
| 985 |
|
| 986 |
$upload_dir = wp_upload_dir(); |
| 987 |
if (!empty($upload_dir['error'])) { |
| 988 |
return new WP_Error( |
| 989 |
'upload_dir_error', |
| 990 |
'WordPress uploads directory error: ' . $upload_dir['error'], |
| 991 |
array('status' => 500) |
| 992 |
); |
| 993 |
} |
| 994 |
|
| 995 |
$base_dir = trailingslashit($upload_dir['basedir']) . 'metasync-pages'; |
| 996 |
|
| 997 |
// Resolve the on-disk assets folder. LPS bakes this path into the bundle's |
| 998 |
// asset URLs (Vite `base`) at build time, so it is intentionally separate |
| 999 |
// from the page slug. Fall back to the slug for single-page imports that do |
| 1000 |
// not send one. Do NOT transform the value (no sanitize_title) — it must |
| 1001 |
// match exactly what LPS baked in — only reject anything unsafe. |
| 1002 |
$assets_folder = is_string($assets_folder) ? trim($assets_folder) : ''; |
| 1003 |
if ($assets_folder === '') { |
| 1004 |
$assets_folder = $slug; |
| 1005 |
} |
| 1006 |
if ($assets_folder === '' || !preg_match('#^[A-Za-z0-9._-]+$#', $assets_folder) || strpos($assets_folder, '..') !== false || $assets_folder[0] === '.') { |
| 1007 |
return new WP_Error( |
| 1008 |
'invalid_assets_folder', |
| 1009 |
'A valid assets_folder (or a slug to derive it from) is required; it may contain only letters, numbers, dot, underscore and hyphen, must not start with a dot, and must not contain "..".', |
| 1010 |
array('status' => 400) |
| 1011 |
); |
| 1012 |
} |
| 1013 |
|
| 1014 |
$target_dir = trailingslashit($base_dir) . $assets_folder; |
| 1015 |
|
| 1016 |
// Ensure the parent metasync-pages directory exists, then resolve it for |
| 1017 |
// containment checks against the staging and target directories. |
| 1018 |
if (!file_exists($base_dir)) { |
| 1019 |
wp_mkdir_p($base_dir); |
| 1020 |
} |
| 1021 |
$base_real = realpath($base_dir); |
| 1022 |
if ($base_real === false) { |
| 1023 |
return new WP_Error( |
| 1024 |
'uploads_dir_unwritable', |
| 1025 |
'Could not create or resolve the metasync-pages uploads directory.', |
| 1026 |
array('status' => 500) |
| 1027 |
); |
| 1028 |
} |
| 1029 |
|
| 1030 |
// Concurrency guard: serialize imports that target the same assets_folder so |
| 1031 |
// two simultaneous requests cannot race on the same extract/swap. The lock is |
| 1032 |
// an flock() on a per-folder lock file, explicitly released in the finally |
| 1033 |
// block below; the OS also drops it if the request dies, so a crash can never |
| 1034 |
// leave a stale lock behind. |
| 1035 |
$lps_import_lock = $this->acquire_import_lock($assets_folder, $base_dir); |
| 1036 |
if (is_wp_error($lps_import_lock)) { |
| 1037 |
return $lps_import_lock; |
| 1038 |
} |
| 1039 |
|
| 1040 |
// Everything below runs under the import lock; the finally block guarantees |
| 1041 |
// the lock is explicitly released no matter which return path is taken. |
| 1042 |
try { |
| 1043 |
|
| 1044 |
// Extract into a staging directory first. The live assets are only removed |
| 1045 |
// and replaced once the new bundle is fully extracted AND validated, so a |
| 1046 |
// failed or partial extraction can never destroy the currently-served page. |
| 1047 |
$staging_dir = trailingslashit($base_dir) . '.import-tmp-' . $assets_folder . '-' . uniqid(); |
| 1048 |
if (!wp_mkdir_p($staging_dir)) { |
| 1049 |
return new WP_Error( |
| 1050 |
'staging_mkdir_failed', |
| 1051 |
'Failed to create a staging directory for extraction.', |
| 1052 |
array('status' => 500) |
| 1053 |
); |
| 1054 |
} |
| 1055 |
$staging_real = realpath($staging_dir); |
| 1056 |
if ($staging_real === false || strpos($staging_real, $base_real) !== 0) { |
| 1057 |
$this->recursive_rmdir($staging_dir); |
| 1058 |
return new WP_Error( |
| 1059 |
'path_traversal_detected', |
| 1060 |
'Resolved staging directory escapes the metasync-pages base.', |
| 1061 |
array('status' => 400) |
| 1062 |
); |
| 1063 |
} |
| 1064 |
|
| 1065 |
$zip = new ZipArchive(); |
| 1066 |
$open_result = $zip->open($zip_path); |
| 1067 |
if ($open_result !== true) { |
| 1068 |
$this->recursive_rmdir($staging_dir); |
| 1069 |
return new WP_Error( |
| 1070 |
'zip_open_failed', |
| 1071 |
'Failed to open ZIP archive (code: ' . intval($open_result) . ').', |
| 1072 |
array('status' => 400) |
| 1073 |
); |
| 1074 |
} |
| 1075 |
|
| 1076 |
// ZIP Slip guard: validate every entry name before extraction. |
| 1077 |
// Reject absolute paths, parent traversal, and disallow .php files entirely. |
| 1078 |
for ($i = 0; $i < $zip->numFiles; $i++) { |
| 1079 |
$entry = $zip->getNameIndex($i); |
| 1080 |
if ($entry === false || $entry === '') { |
| 1081 |
$zip->close(); |
| 1082 |
$this->recursive_rmdir($staging_dir); |
| 1083 |
return new WP_Error('zip_invalid_entry', 'ZIP contains an invalid entry name.', array('status' => 400)); |
| 1084 |
} |
| 1085 |
$normalized = str_replace('\\', '/', $entry); |
| 1086 |
if ($normalized[0] === '/' || preg_match('#(^|/)\.\.(/|$)#', $normalized)) { |
| 1087 |
$zip->close(); |
| 1088 |
$this->recursive_rmdir($staging_dir); |
| 1089 |
return new WP_Error('zip_slip_detected', 'ZIP contains an entry with an unsafe path: ' . sanitize_text_field($entry), array('status' => 400)); |
| 1090 |
} |
| 1091 |
$ext = strtolower(pathinfo($normalized, PATHINFO_EXTENSION)); |
| 1092 |
$blocked_exts = array('php', 'phtml', 'php3', 'php4', 'php5', 'php7', 'phar', 'pl', 'py', 'jsp', 'asp', 'aspx', 'cgi', 'sh', 'htaccess'); |
| 1093 |
if (in_array($ext, $blocked_exts, true) || basename($normalized) === '.htaccess') { |
| 1094 |
$zip->close(); |
| 1095 |
$this->recursive_rmdir($staging_dir); |
| 1096 |
return new WP_Error('zip_unsafe_entry', 'ZIP contains a disallowed file type: ' . sanitize_text_field($entry), array('status' => 400)); |
| 1097 |
} |
| 1098 |
} |
| 1099 |
|
| 1100 |
$extracted = $zip->extractTo($staging_dir); |
| 1101 |
$zip->close(); |
| 1102 |
|
| 1103 |
if (!$extracted) { |
| 1104 |
$this->recursive_rmdir($staging_dir); |
| 1105 |
return new WP_Error( |
| 1106 |
'zip_extract_failed', |
| 1107 |
'Failed to extract ZIP archive contents.', |
| 1108 |
array('status' => 500) |
| 1109 |
); |
| 1110 |
} |
| 1111 |
|
| 1112 |
// Drop a hardening .htaccess to block any script execution from the directory. |
| 1113 |
// (Apache-only; on Nginx the pre-extraction extension blocklist above is the |
| 1114 |
// real guard.) Written into staging so it travels with the swap. |
| 1115 |
$htaccess_content = "# Auto-generated by MetaSync LPS import — do not edit.\n" |
| 1116 |
. "<FilesMatch \"\\.(php|phtml|php3|php4|php5|php7|phar|pl|py|jsp|asp|aspx|cgi|sh)$\">\n" |
| 1117 |
. " Require all denied\n" |
| 1118 |
. " Deny from all\n" |
| 1119 |
. "</FilesMatch>\n" |
| 1120 |
. "Options -ExecCGI\n" |
| 1121 |
. "RemoveHandler .php .phtml .php3 .php4 .php5 .php7 .phar\n" |
| 1122 |
. "RemoveType .php .phtml .php3 .php4 .php5 .php7 .phar\n"; |
| 1123 |
@file_put_contents(trailingslashit($staging_dir) . '.htaccess', $htaccess_content); |
| 1124 |
|
| 1125 |
$assets_dir_url = trailingslashit($upload_dir['baseurl']) . 'metasync-pages/' . $assets_folder; |
| 1126 |
|
| 1127 |
// Decide mode: a pages.manifest.json at the bundle root means multi-page. |
| 1128 |
$has_manifest = file_exists(trailingslashit($staging_dir) . 'pages.manifest.json'); |
| 1129 |
|
| 1130 |
// ---- Multi-page import (manifest-driven) -------------------------------- |
| 1131 |
if ($has_manifest) { |
| 1132 |
// Parse and validate the manifest while it is still only in staging, so a |
| 1133 |
// broken manifest can neither leave an orphan folder nor destroy a prior |
| 1134 |
// import — the live assets stay untouched until we know the bundle is good. |
| 1135 |
$manifest_raw = file_get_contents(trailingslashit($staging_dir) . 'pages.manifest.json'); |
| 1136 |
if ($manifest_raw === false) { |
| 1137 |
$this->recursive_rmdir($staging_dir); |
| 1138 |
return new WP_Error('manifest_unreadable', 'Could not read pages.manifest.json.', array('status' => 500)); |
| 1139 |
} |
| 1140 |
$manifest = json_decode($manifest_raw, true); |
| 1141 |
if (!is_array($manifest) || empty($manifest['pages']) || !is_array($manifest['pages'])) { |
| 1142 |
$this->recursive_rmdir($staging_dir); |
| 1143 |
return new WP_Error('invalid_manifest', 'pages.manifest.json is missing a non-empty "pages" array.', array('status' => 422)); |
| 1144 |
} |
| 1145 |
|
| 1146 |
// Verify every page's pre-rendered HTML is present in staging BEFORE the |
| 1147 |
// swap. A structurally incomplete bundle must be rejected here — otherwise |
| 1148 |
// the swap would wipe a prior import's live assets and only then discover |
| 1149 |
// the missing files, leaving the existing pages broken. (Slug conflicts are |
| 1150 |
// different: they are reported per-page; a missing HTML file is a bad bundle.) |
| 1151 |
foreach ($manifest['pages'] as $manifest_page) { |
| 1152 |
if (!is_array($manifest_page)) { |
| 1153 |
continue; |
| 1154 |
} |
| 1155 |
$mp_slug = isset($manifest_page['slug']) ? trim((string) $manifest_page['slug'], '/') : ''; |
| 1156 |
$mp_rel = ($mp_slug === '') ? 'index.html' : $mp_slug . '/index.html'; |
| 1157 |
if (!file_exists(trailingslashit($staging_dir) . $mp_rel)) { |
| 1158 |
$this->recursive_rmdir($staging_dir); |
| 1159 |
return new WP_Error( |
| 1160 |
'incomplete_bundle', |
| 1161 |
'Bundle is missing index.html for page "' . sanitize_text_field($mp_slug === '' ? '(home)' : $mp_slug) . '"; aborting before touching live assets.', |
| 1162 |
array('status' => 422) |
| 1163 |
); |
| 1164 |
} |
| 1165 |
} |
| 1166 |
|
| 1167 |
$swap = $this->swap_staging_into_place($staging_dir, $target_dir, $base_real); |
| 1168 |
if (is_wp_error($swap)) { |
| 1169 |
return $swap; |
| 1170 |
} |
| 1171 |
return $this->create_pages_from_manifest($manifest['pages'], $target_dir, $assets_folder, $assets_dir_url, $status, $overwrite, $external_ref); |
| 1172 |
} |
| 1173 |
|
| 1174 |
// ---- Single-page import ------------------------------------------------- |
| 1175 |
if (empty($slug)) { |
| 1176 |
$this->recursive_rmdir($staging_dir); |
| 1177 |
return new WP_Error('invalid_slug', 'A non-empty slug is required for a single-page import.', array('status' => 400)); |
| 1178 |
} |
| 1179 |
if (empty($title)) { |
| 1180 |
$this->recursive_rmdir($staging_dir); |
| 1181 |
return new WP_Error('missing_title', 'A non-empty title is required for a single-page import.', array('status' => 400)); |
| 1182 |
} |
| 1183 |
|
| 1184 |
// Validate the bundle in staging BEFORE touching the live directory. |
| 1185 |
$index_path = trailingslashit($staging_dir) . 'index.html'; |
| 1186 |
if (!file_exists($index_path)) { |
| 1187 |
$this->recursive_rmdir($staging_dir); |
| 1188 |
return new WP_Error('missing_index_html', 'index.html was not found at the root of the extracted ZIP.', array('status' => 422)); |
| 1189 |
} |
| 1190 |
$html = file_get_contents($index_path); |
| 1191 |
if ($html === false) { |
| 1192 |
$this->recursive_rmdir($staging_dir); |
| 1193 |
return new WP_Error('index_html_unreadable', 'Could not read index.html from the extracted ZIP.', array('status' => 500)); |
| 1194 |
} |
| 1195 |
|
| 1196 |
// Ownership gate — checked while the new bundle is still only in staging, so |
| 1197 |
// rejecting here leaves the live page and its assets completely untouched. |
| 1198 |
// Only pages we previously created via LPS import may be overwritten. |
| 1199 |
$existing_page = get_page_by_path($slug, OBJECT, 'page'); |
| 1200 |
if ($existing_page) { |
| 1201 |
$is_lps_page = get_post_meta($existing_page->ID, Metasync_Custom_Pages::META_LPS_IMPORT, true) === '1'; |
| 1202 |
if (!$is_lps_page) { |
| 1203 |
$this->recursive_rmdir($staging_dir); |
| 1204 |
return new WP_Error( |
| 1205 |
'slug_taken_by_existing_content', |
| 1206 |
'A page already exists at this slug and was not created by LPS, so it will not be overwritten.', |
| 1207 |
array( |
| 1208 |
'status' => 409, |
| 1209 |
'conflict_with' => array( |
| 1210 |
'post_id' => (int) $existing_page->ID, |
| 1211 |
'post_type' => $existing_page->post_type, |
| 1212 |
'title' => $existing_page->post_title, |
| 1213 |
), |
| 1214 |
'suggested_slug' => $this->suggest_available_slug($slug), |
| 1215 |
) |
| 1216 |
); |
| 1217 |
} |
| 1218 |
if (!$overwrite) { |
| 1219 |
$this->recursive_rmdir($staging_dir); |
| 1220 |
return new WP_Error( |
| 1221 |
'page_slug_exists', |
| 1222 |
'An LPS page already exists at this slug. Pass overwrite=true to replace it.', |
| 1223 |
array('status' => 409, 'suggested_slug' => $this->suggest_available_slug($slug)) |
| 1224 |
); |
| 1225 |
} |
| 1226 |
} |
| 1227 |
|
| 1228 |
// Bundle validated and ownership confirmed — swap into place. |
| 1229 |
$swap = $this->swap_staging_into_place($staging_dir, $target_dir, $base_real); |
| 1230 |
if (is_wp_error($swap)) { |
| 1231 |
return $swap; |
| 1232 |
} |
| 1233 |
|
| 1234 |
if ($existing_page) { |
| 1235 |
$update_result = wp_update_post(array( |
| 1236 |
'ID' => $existing_page->ID, |
| 1237 |
'post_title' => $title, |
| 1238 |
'post_status' => $status |
| 1239 |
), true); |
| 1240 |
if (is_wp_error($update_result)) { |
| 1241 |
return new WP_Error('page_update_failed', 'Failed to update existing page: ' . $update_result->get_error_message(), array('status' => 500)); |
| 1242 |
} |
| 1243 |
$page_id = $existing_page->ID; |
| 1244 |
} else { |
| 1245 |
$page_id = wp_insert_post(array( |
| 1246 |
'post_title' => $title, |
| 1247 |
'post_name' => $slug, |
| 1248 |
'post_type' => 'page', |
| 1249 |
'post_status' => $status, |
| 1250 |
'post_content' => '' |
| 1251 |
), true); |
| 1252 |
if (is_wp_error($page_id)) { |
| 1253 |
return new WP_Error('page_insert_failed', 'Failed to create page: ' . $page_id->get_error_message(), array('status' => 500)); |
| 1254 |
} |
| 1255 |
} |
| 1256 |
|
| 1257 |
update_post_meta($page_id, Metasync_Custom_Pages::META_IS_CUSTOM_HTML_PAGE, '1'); |
| 1258 |
update_post_meta($page_id, Metasync_Custom_Pages::META_CREATED_VIA_API, '1'); |
| 1259 |
update_post_meta($page_id, Metasync_Custom_Pages::META_LPS_IMPORT, '1'); |
| 1260 |
update_post_meta($page_id, Metasync_Custom_Pages::META_ASSETS_FOLDER, $assets_folder); |
| 1261 |
if ($external_ref !== '') { |
| 1262 |
update_post_meta($page_id, Metasync_Custom_Pages::META_LPS_PROJECT_REF, $external_ref); |
| 1263 |
} |
| 1264 |
update_post_meta($page_id, Metasync_Custom_Pages::META_HTML_ENABLED, '1'); |
| 1265 |
update_post_meta($page_id, Metasync_Custom_Pages::META_HTML_CONTENT, wp_unslash($html)); |
| 1266 |
update_post_meta($page_id, Metasync_Custom_Pages::META_HTML_FILENAME, 'index.html'); |
| 1267 |
|
| 1268 |
wp_cache_delete($page_id, 'posts'); |
| 1269 |
wp_cache_delete($page_id, 'post_meta'); |
| 1270 |
|
| 1271 |
// Purge page + edge caches so the re-published HTML references the new asset hashes. |
| 1272 |
$this->purge_lps_page_caches(array(array('page_id' => $page_id, 'url' => get_permalink($page_id)))); |
| 1273 |
|
| 1274 |
return array( |
| 1275 |
'success' => true, |
| 1276 |
'message' => 'LPS ZIP imported successfully', |
| 1277 |
'data' => array( |
| 1278 |
'mode' => 'single', |
| 1279 |
'was_update' => (bool) $existing_page, |
| 1280 |
'page_id' => $page_id, |
| 1281 |
'slug' => get_post_field('post_name', $page_id), |
| 1282 |
'url' => get_permalink($page_id), |
| 1283 |
'edit_url' => get_edit_post_link($page_id, ''), |
| 1284 |
'status' => get_post_status($page_id), |
| 1285 |
'html_length' => strlen($html), |
| 1286 |
'assets_folder' => $assets_folder, |
| 1287 |
'assets_dir_url' => $assets_dir_url |
| 1288 |
) |
| 1289 |
); |
| 1290 |
|
| 1291 |
} finally { |
| 1292 |
if (is_resource($lps_import_lock)) { |
| 1293 |
@flock($lps_import_lock, LOCK_UN); |
| 1294 |
@fclose($lps_import_lock); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- operates on a native stream handle |
| 1295 |
} |
| 1296 |
} |
| 1297 |
} |
| 1298 |
|
| 1299 |
/** |
| 1300 |
* Acquire an exclusive, non-blocking lock for an assets_folder import. |
| 1301 |
* |
| 1302 |
* Uses flock() on a per-folder lock file under metasync-pages/.locks/. The |
| 1303 |
* returned handle must stay referenced for the duration of the import; the lock |
| 1304 |
* releases when the handle closes (on return / request end), so a crash cannot |
| 1305 |
* leave a stale lock. Returns a 423 WP_Error if another import for the same |
| 1306 |
* folder is already running. |
| 1307 |
* |
| 1308 |
* @param string $assets_folder Folder key to lock on. |
| 1309 |
* @param string $base_dir The metasync-pages base directory. |
| 1310 |
* @return resource|WP_Error Open file handle holding the lock, or WP_Error. |
| 1311 |
*/ |
| 1312 |
private function acquire_import_lock($assets_folder, $base_dir) |
| 1313 |
{ |
| 1314 |
$lock_dir = trailingslashit($base_dir) . '.locks'; |
| 1315 |
if (!file_exists($lock_dir)) { |
| 1316 |
wp_mkdir_p($lock_dir); |
| 1317 |
} |
| 1318 |
$lock_path = trailingslashit($lock_dir) . md5($assets_folder) . '.lock'; |
| 1319 |
|
| 1320 |
$handle = @fopen($lock_path, 'c'); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- streaming file I/O; WP_Filesystem cannot return a raw stream handle |
| 1321 |
if ($handle === false) { |
| 1322 |
return new WP_Error('lock_unavailable', 'Could not open the import lock file.', array('status' => 500)); |
| 1323 |
} |
| 1324 |
if (!flock($handle, LOCK_EX | LOCK_NB)) { |
| 1325 |
fclose($handle); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- operates on a native stream handle |
| 1326 |
return new WP_Error( |
| 1327 |
'import_in_progress', |
| 1328 |
'Another import for this assets_folder is already in progress. Retry shortly.', |
| 1329 |
array('status' => 423) |
| 1330 |
); |
| 1331 |
} |
| 1332 |
return $handle; |
| 1333 |
} |
| 1334 |
|
| 1335 |
/** |
| 1336 |
* Move a validated staging directory into its final place atomically. |
| 1337 |
* |
| 1338 |
* Removes any existing target dir, renames staging over it, then re-checks |
| 1339 |
* containment under the metasync-pages base. |
| 1340 |
* |
| 1341 |
* @return true|WP_Error |
| 1342 |
*/ |
| 1343 |
private function swap_staging_into_place($staging_dir, $target_dir, $base_real) |
| 1344 |
{ |
| 1345 |
if (file_exists($target_dir)) { |
| 1346 |
$this->recursive_rmdir($target_dir); |
| 1347 |
} |
| 1348 |
if (!@rename($staging_dir, $target_dir)) { // phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename -- atomic same-filesystem replace; WP_Filesystem::move() needs credential bootstrapping on non-direct hosts |
| 1349 |
$this->recursive_rmdir($staging_dir); |
| 1350 |
return new WP_Error( |
| 1351 |
'assets_swap_failed', |
| 1352 |
'Failed to move the extracted assets into place.', |
| 1353 |
array('status' => 500) |
| 1354 |
); |
| 1355 |
} |
| 1356 |
$target_real = realpath($target_dir); |
| 1357 |
if ($target_real === false || strpos($target_real, $base_real) !== 0) { |
| 1358 |
return new WP_Error( |
| 1359 |
'path_traversal_detected', |
| 1360 |
'Resolved target directory escapes the metasync-pages base.', |
| 1361 |
array('status' => 400) |
| 1362 |
); |
| 1363 |
} |
| 1364 |
return true; |
| 1365 |
} |
| 1366 |
|
| 1367 |
/** |
| 1368 |
* Purge full-page and edge/CDN caches for imported LPS pages. |
| 1369 |
* |
| 1370 |
* LPS bundles use content-hashed asset filenames. A re-publish writes new |
| 1371 |
* hashes and deletes the old bundle, but a page-cache plugin or CDN may keep |
| 1372 |
* serving the OLD HTML — which references the now-deleted assets (404 → |
| 1373 |
* unstyled / white screen). This clears those layers for every created/updated |
| 1374 |
* page (and the front page when one was set/updated) so the served HTML always |
| 1375 |
* references the current asset hashes. Reuses the plugin's existing |
| 1376 |
* Metasync_Cache_Purge (page-cache plugins) and Metasync_Edge_Cache_Purge (CDN) |
| 1377 |
* subsystems, with the edge purge batched into a single call. |
| 1378 |
* |
| 1379 |
* Wrapped in a \Throwable guard so a purge failure can never abort or error the |
| 1380 |
* import (same pattern as the audit logger). |
| 1381 |
* |
| 1382 |
* @param array $page_records Records carrying 'page_id', 'url', and optional 'is_front_page'. |
| 1383 |
*/ |
| 1384 |
private function purge_lps_page_caches(array $page_records) |
| 1385 |
{ |
| 1386 |
try { |
| 1387 |
$urls = array(); |
| 1388 |
$front_page_seen = false; |
| 1389 |
|
| 1390 |
foreach ($page_records as $rec) { |
| 1391 |
if (!empty($rec['is_front_page'])) { |
| 1392 |
$front_page_seen = true; |
| 1393 |
} |
| 1394 |
|
| 1395 |
$url = !empty($rec['url']) ? $rec['url'] : ''; |
| 1396 |
if (empty($url) && !empty($rec['page_id'])) { |
| 1397 |
$url = get_permalink((int) $rec['page_id']); |
| 1398 |
} |
| 1399 |
|
| 1400 |
if (!empty($url) && !in_array($url, $urls, true)) { |
| 1401 |
$urls[] = $url; |
| 1402 |
} |
| 1403 |
} |
| 1404 |
|
| 1405 |
// A front page is served at "/" — purge that URL too. |
| 1406 |
if ($front_page_seen) { |
| 1407 |
$home = home_url('/'); |
| 1408 |
if (!in_array($home, $urls, true)) { |
| 1409 |
$urls[] = $home; |
| 1410 |
} |
| 1411 |
} |
| 1412 |
|
| 1413 |
if (empty($urls)) { |
| 1414 |
return; |
| 1415 |
} |
| 1416 |
|
| 1417 |
// Page-cache plugins: purge each URL individually. |
| 1418 |
if (class_exists('Metasync_Cache_Purge')) { |
| 1419 |
foreach ($urls as $url) { |
| 1420 |
Metasync_Cache_Purge::purge_single_url($url); |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
// Edge/CDN caches: one batched call for all URLs (avoids N API calls). |
| 1425 |
if (class_exists('Metasync_Edge_Cache_Purge')) { |
| 1426 |
Metasync_Edge_Cache_Purge::purge($urls); |
| 1427 |
} |
| 1428 |
} catch (\Throwable $e) { |
| 1429 |
error_log('MetaSync: LPS import cache purge failed - ' . $e->getMessage()); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- genuine failure path, bounded, no secrets |
| 1430 |
} |
| 1431 |
} |
| 1432 |
|
| 1433 |
/** |
| 1434 |
* Find this project's existing LPS home page by a stable per-project marker, |
| 1435 |
* independent of its slug, so a re-publish updates the same home instead of |
| 1436 |
* creating a duplicate. |
| 1437 |
* |
| 1438 |
* When $external_ref (the LPS project UUID) is supplied it is the primary key: |
| 1439 |
* matched on META_LPS_PROJECT_REF, robust to assets_folder/slug changes between |
| 1440 |
* publishes. When absent (i.e. before CA forwards external_ref) it falls back to |
| 1441 |
* the legacy META_LPS_HOME == assets_folder lookup — no regression. |
| 1442 |
* |
| 1443 |
* @param string $assets_folder Legacy per-project key (fallback). |
| 1444 |
* @param string $external_ref LPS project UUID (preferred key when present). |
| 1445 |
* @return int Page ID, or 0 if none. |
| 1446 |
*/ |
| 1447 |
private function find_lps_home_page($assets_folder, $external_ref = '') |
| 1448 |
{ |
| 1449 |
if ($external_ref !== '') { |
| 1450 |
// Match the project's HOME specifically: the page carrying this project's |
| 1451 |
// UUID (written on every page) AND flagged as a home (META_LPS_HOME is |
| 1452 |
// written only on the home). Without the home clause this matched any page. |
| 1453 |
$ref_ids = get_posts(array( |
| 1454 |
'post_type' => 'page', |
| 1455 |
'post_status' => array('publish', 'future', 'draft', 'pending', 'private'), |
| 1456 |
'numberposts' => 1, |
| 1457 |
'fields' => 'ids', |
| 1458 |
'no_found_rows' => true, |
| 1459 |
'suppress_filters' => true, |
| 1460 |
'meta_query' => array( |
| 1461 |
'relation' => 'AND', |
| 1462 |
array('key' => Metasync_Custom_Pages::META_LPS_PROJECT_REF, 'value' => $external_ref), |
| 1463 |
array('key' => Metasync_Custom_Pages::META_LPS_HOME, 'compare' => 'EXISTS'), |
| 1464 |
), |
| 1465 |
)); |
| 1466 |
if (!empty($ref_ids)) { |
| 1467 |
return (int) $ref_ids[0]; |
| 1468 |
} |
| 1469 |
} |
| 1470 |
|
| 1471 |
$ids = get_posts(array( |
| 1472 |
'post_type' => 'page', |
| 1473 |
'post_status' => array('publish', 'future', 'draft', 'pending', 'private'), |
| 1474 |
'numberposts' => 1, |
| 1475 |
'fields' => 'ids', |
| 1476 |
'no_found_rows' => true, |
| 1477 |
'suppress_filters' => true, |
| 1478 |
'meta_key' => Metasync_Custom_Pages::META_LPS_HOME, |
| 1479 |
'meta_value' => $assets_folder, |
| 1480 |
)); |
| 1481 |
return !empty($ids) ? (int) $ids[0] : 0; |
| 1482 |
} |
| 1483 |
|
| 1484 |
/** |
| 1485 |
* Create or update one WordPress page per entry in pages.manifest.json. |
| 1486 |
* |
| 1487 |
* All pages share the single $assets_folder. Slugs are created shallowest-first |
| 1488 |
* so parent pages exist before their children (e.g. "departments" before |
| 1489 |
* "departments/cardiology"); missing ancestors are auto-created as LPS-owned |
| 1490 |
* placeholder pages. Per-page conflicts with non-LPS content are skipped and |
| 1491 |
* reported with a suggested_slug rather than overwriting the user's content. |
| 1492 |
* |
| 1493 |
* @param array $pages Validated manifest "pages" array (parsed by the caller |
| 1494 |
* before the staging→live swap). |
| 1495 |
* @return array Response with created/updated/failed arrays. |
| 1496 |
*/ |
| 1497 |
private function create_pages_from_manifest($pages, $target_dir, $assets_folder, $assets_dir_url, $status, $overwrite, $external_ref = '') |
| 1498 |
{ |
| 1499 |
// Shallowest slugs first so parents are created before their children. |
| 1500 |
usort($pages, function ($a, $b) { |
| 1501 |
$da = isset($a['slug']) ? substr_count(trim((string) $a['slug'], '/'), '/') : 0; |
| 1502 |
$db = isset($b['slug']) ? substr_count(trim((string) $b['slug'], '/'), '/') : 0; |
| 1503 |
if ($da === $db) { |
| 1504 |
return 0; |
| 1505 |
} |
| 1506 |
return ($da < $db) ? -1 : 1; |
| 1507 |
}); |
| 1508 |
|
| 1509 |
$created = array(); |
| 1510 |
$updated = array(); |
| 1511 |
$failed = array(); |
| 1512 |
$slug_to_id = array(); // full slug path => page ID, for parent resolution |
| 1513 |
|
| 1514 |
// Resolve the bundle root once before the loop so we can verify every |
| 1515 |
// per-page HTML path stays inside it. The string blocklist above rejects |
| 1516 |
// literal "." and backslashes, but cannot catch symlinks in the bundle or |
| 1517 |
// encoding tricks. realpath() gives the canonical on-disk path, making |
| 1518 |
// containment provable regardless of how the path was constructed. |
| 1519 |
$target_real = realpath($target_dir); |
| 1520 |
if ($target_real === false) { |
| 1521 |
// $target_dir must exist at this point (the swap already ran); if |
| 1522 |
// realpath fails anyway, fail safe and bail out entirely. |
| 1523 |
return array( |
| 1524 |
'success' => false, |
| 1525 |
'message' => 'Could not resolve the bundle directory path.', |
| 1526 |
'data' => array('mode' => 'multi', 'assets_folder' => $assets_folder, 'assets_dir_url' => $assets_dir_url, 'pages_total' => count($pages), 'created' => array(), 'updated' => array(), 'failed' => array()), |
| 1527 |
); |
| 1528 |
} |
| 1529 |
// Ensure the prefix check below cannot match a sibling directory that |
| 1530 |
// starts with the same characters as $target_real (e.g. /foo/bar vs |
| 1531 |
// /foo/bar-extra). Adding DIRECTORY_SEPARATOR guarantees we match a real |
| 1532 |
// path boundary. |
| 1533 |
$target_real_prefix = rtrim($target_real, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
| 1534 |
|
| 1535 |
foreach ($pages as $entry) { |
| 1536 |
if (!is_array($entry)) { |
| 1537 |
continue; |
| 1538 |
} |
| 1539 |
|
| 1540 |
$is_home = !empty($entry['isHome']); |
| 1541 |
$raw_slug = isset($entry['slug']) ? trim((string) $entry['slug'], '/') : ''; |
| 1542 |
|
| 1543 |
// Reject path-traversal slugs before $raw_slug is used to build the |
| 1544 |
// on-disk HTML path below. A crafted manifest slug (e.g. |
| 1545 |
// "././././some-dir") could otherwise escape $target_dir and have |
| 1546 |
// file_get_contents() read an index.html from outside the bundle. The |
| 1547 |
// raw value is checked as-is, before any normalization. |
| 1548 |
if (strpos($raw_slug, '..') !== false || strpos($raw_slug, '\\') !== false) { |
| 1549 |
$failed[] = array( |
| 1550 |
'slug' => $raw_slug, |
| 1551 |
'code' => 'invalid_slug_path', |
| 1552 |
'message' => 'Slug contains unsafe path characters.', |
| 1553 |
); |
| 1554 |
continue; |
| 1555 |
} |
| 1556 |
|
| 1557 |
// Home page (isHome / empty slug) is handled specially below: it is |
| 1558 |
// created and set as the WordPress static front page so it serves at "/". |
| 1559 |
$effective_slug = ($is_home && $raw_slug === '') ? 'home' : $raw_slug; |
| 1560 |
|
| 1561 |
// Normalize each segment exactly as WordPress will persist it, so the |
| 1562 |
// get_page_by_path() lookups and stored records match what wp_insert_post |
| 1563 |
// creates (a raw mixed-case slug would desync the object cache). The raw |
| 1564 |
// slug is still used for the on-disk HTML path, which mirrors the bundle. |
| 1565 |
$effective_slug = implode('/', array_map('sanitize_title', explode('/', $effective_slug))); |
| 1566 |
|
| 1567 |
if ($effective_slug === '') { |
| 1568 |
$failed[] = array('slug' => '(home)', 'code' => 'invalid_slug', 'message' => 'Page has an empty slug.'); |
| 1569 |
continue; |
| 1570 |
} |
| 1571 |
|
| 1572 |
// Per-page title from the manifest (name, then seo.title). |
| 1573 |
$seo = (isset($entry['seo']) && is_array($entry['seo'])) ? $entry['seo'] : array(); |
| 1574 |
$title = ''; |
| 1575 |
if (!empty($entry['name'])) { |
| 1576 |
$title = sanitize_text_field($entry['name']); |
| 1577 |
} elseif (!empty($seo['title'])) { |
| 1578 |
$title = sanitize_text_field($seo['title']); |
| 1579 |
} |
| 1580 |
if ($title === '') { |
| 1581 |
$title = $effective_slug; |
| 1582 |
} |
| 1583 |
|
| 1584 |
// Locate this page's pre-rendered HTML inside the bundle. |
| 1585 |
$rel = ($raw_slug === '') ? 'index.html' : $raw_slug . '/index.html'; |
| 1586 |
$html_file = trailingslashit($target_dir) . $rel; |
| 1587 |
if (!file_exists($html_file)) { |
| 1588 |
$failed[] = array('slug' => $effective_slug, 'code' => 'missing_html', 'message' => 'No index.html for this page in the bundle.'); |
| 1589 |
continue; |
| 1590 |
} |
| 1591 |
// Containment check: verify the resolved, canonical path stays under |
| 1592 |
// $target_dir. The string blocklist above catches literal "." and |
| 1593 |
// backslashes, but realpath() is the authoritative guard — it resolves |
| 1594 |
// any symlinks in the bundle and proves the final on-disk read is |
| 1595 |
// inside the bundle directory regardless of how the path was built. |
| 1596 |
// (Same pattern used at lines 1055-1056 for staging and 1353-1354 for |
| 1597 |
// the post-swap target.) |
| 1598 |
$html_real = realpath($html_file); |
| 1599 |
if ($html_real === false || strpos($html_real, $target_real_prefix) !== 0) { |
| 1600 |
$failed[] = array( |
| 1601 |
'slug' => $effective_slug, |
| 1602 |
'code' => 'invalid_slug_path', |
| 1603 |
'message' => 'Resolved HTML path escapes the bundle directory.', |
| 1604 |
); |
| 1605 |
continue; |
| 1606 |
} |
| 1607 |
$html = file_get_contents($html_file); |
| 1608 |
if ($html === false) { |
| 1609 |
$failed[] = array('slug' => $effective_slug, 'code' => 'html_unreadable', 'message' => 'Could not read this page\'s index.html.'); |
| 1610 |
continue; |
| 1611 |
} |
| 1612 |
|
| 1613 |
// --- Home page: create it and set it as the WordPress static front page |
| 1614 |
// so it serves at "/" (the LPS app's router expects home at the root). |
| 1615 |
// The page is identified per-project via the META_LPS_HOME marker (value = |
| 1616 |
// assets_folder), so re-publishing the SAME project updates its own home |
| 1617 |
// instead of creating duplicates — even if its slug was renamed. A DIFFERENT |
| 1618 |
// project gets its own new home and takes over the front page; the previous |
| 1619 |
// project's home is left in place (non-destructive). Home conflicts are NOT |
| 1620 |
// reported to LPS — a taken 'home' slug just falls back to 'home-2'. |
| 1621 |
// Home dedup keys on external_ref (the LPS project UUID) when present, |
| 1622 |
// falling back to assets_folder for pre-external_ref imports. |
| 1623 |
if ($is_home) { |
| 1624 |
$home_id = $this->find_lps_home_page($assets_folder, $external_ref); |
| 1625 |
$home_existing = $home_id > 0; |
| 1626 |
if ($home_existing) { |
| 1627 |
$res = wp_update_post(array('ID' => $home_id, 'post_title' => $title, 'post_status' => 'publish'), true); |
| 1628 |
if (is_wp_error($res)) { |
| 1629 |
$failed[] = array('slug' => 'home', 'code' => 'home_update_failed', 'message' => $res->get_error_message()); |
| 1630 |
continue; |
| 1631 |
} |
| 1632 |
} else { |
| 1633 |
// 'home' if free; otherwise next available (home-2, ...). Front-page |
| 1634 |
// makes the slug invisible, so any free slug is fine. |
| 1635 |
$home_slug = get_page_by_path('home', OBJECT, 'page') ? $this->suggest_available_slug('home') : 'home'; |
| 1636 |
$home_id = wp_insert_post(array( |
| 1637 |
'post_title' => $title, |
| 1638 |
'post_name' => $home_slug, |
| 1639 |
'post_type' => 'page', |
| 1640 |
'post_status' => 'publish', |
| 1641 |
'post_content' => '' |
| 1642 |
), true); |
| 1643 |
if (is_wp_error($home_id)) { |
| 1644 |
$failed[] = array('slug' => 'home', 'code' => 'home_insert_failed', 'message' => $home_id->get_error_message()); |
| 1645 |
continue; |
| 1646 |
} |
| 1647 |
} |
| 1648 |
|
| 1649 |
update_post_meta($home_id, Metasync_Custom_Pages::META_IS_CUSTOM_HTML_PAGE, '1'); |
| 1650 |
update_post_meta($home_id, Metasync_Custom_Pages::META_CREATED_VIA_API, '1'); |
| 1651 |
update_post_meta($home_id, Metasync_Custom_Pages::META_LPS_IMPORT, '1'); |
| 1652 |
update_post_meta($home_id, Metasync_Custom_Pages::META_ASSETS_FOLDER, $assets_folder); |
| 1653 |
update_post_meta($home_id, Metasync_Custom_Pages::META_LPS_HOME, $assets_folder); |
| 1654 |
if ($external_ref !== '') { |
| 1655 |
update_post_meta($home_id, Metasync_Custom_Pages::META_LPS_PROJECT_REF, $external_ref); |
| 1656 |
} |
| 1657 |
update_post_meta($home_id, Metasync_Custom_Pages::META_HTML_ENABLED, '1'); |
| 1658 |
update_post_meta($home_id, Metasync_Custom_Pages::META_HTML_CONTENT, wp_unslash($html)); |
| 1659 |
update_post_meta($home_id, Metasync_Custom_Pages::META_HTML_FILENAME, $rel); |
| 1660 |
|
| 1661 |
// Make it the site's static front page (serves at "/"). |
| 1662 |
update_option('show_on_front', 'page'); |
| 1663 |
update_option('page_on_front', $home_id); |
| 1664 |
|
| 1665 |
wp_cache_delete($home_id, 'posts'); |
| 1666 |
wp_cache_delete($home_id, 'post_meta'); |
| 1667 |
|
| 1668 |
$rec = array('slug' => get_post_field('post_name', $home_id), 'page_id' => $home_id, 'url' => get_permalink($home_id), 'is_front_page' => true); |
| 1669 |
if ($home_existing) { |
| 1670 |
$updated[] = $rec; |
| 1671 |
} else { |
| 1672 |
$created[] = $rec; |
| 1673 |
} |
| 1674 |
continue; |
| 1675 |
} |
| 1676 |
|
| 1677 |
// Split slug into ancestors + leaf; resolve/create the parent chain. |
| 1678 |
$segments = explode('/', $effective_slug); |
| 1679 |
$leaf = sanitize_title(array_pop($segments)); |
| 1680 |
if ($leaf === '') { |
| 1681 |
$failed[] = array('slug' => $effective_slug, 'code' => 'invalid_slug', 'message' => 'Slug sanitized to empty.'); |
| 1682 |
continue; |
| 1683 |
} |
| 1684 |
|
| 1685 |
$parent_id = 0; |
| 1686 |
$parent_ok = true; |
| 1687 |
$parent_fail_code = 'parent_failed'; |
| 1688 |
$parent_fail_extra = array(); |
| 1689 |
$ancestor_path = ''; |
| 1690 |
foreach ($segments as $seg) { |
| 1691 |
$seg = sanitize_title($seg); |
| 1692 |
if ($seg === '') { |
| 1693 |
$parent_ok = false; |
| 1694 |
break; |
| 1695 |
} |
| 1696 |
$ancestor_path = ($ancestor_path === '') ? $seg : $ancestor_path . '/' . $seg; |
| 1697 |
if (isset($slug_to_id[$ancestor_path])) { |
| 1698 |
$parent_id = $slug_to_id[$ancestor_path]; |
| 1699 |
continue; |
| 1700 |
} |
| 1701 |
// find our previously-created ancestor by stable tag (project + |
| 1702 |
// path) first, so a re-import reuses it even if WordPress renamed its slug |
| 1703 |
// on a collision — instead of auto-creating another placeholder each time. |
| 1704 |
$tagged_ancestor = $this->find_lps_page_by_path($assets_folder, $external_ref, $ancestor_path); |
| 1705 |
if ($tagged_ancestor > 0) { |
| 1706 |
$parent_id = $tagged_ancestor; |
| 1707 |
$slug_to_id[$ancestor_path] = $parent_id; |
| 1708 |
continue; |
| 1709 |
} |
| 1710 |
$ancestor_page = get_page_by_path($ancestor_path, OBJECT, 'page'); |
| 1711 |
if ($ancestor_page) { |
| 1712 |
// only nest under ancestors we own. If an existing page on this |
| 1713 |
// path was NOT created by LPS, refuse to graft LPS content under the |
| 1714 |
// user's content — consistent with the leaf ownership gate below. |
| 1715 |
if (get_post_meta($ancestor_page->ID, Metasync_Custom_Pages::META_LPS_IMPORT, true) !== '1') { |
| 1716 |
$parent_ok = false; |
| 1717 |
$parent_fail_code = 'parent_not_owned'; |
| 1718 |
$parent_fail_extra = array( |
| 1719 |
'conflict_with' => array( |
| 1720 |
'post_id' => (int) $ancestor_page->ID, |
| 1721 |
'post_type' => $ancestor_page->post_type, |
| 1722 |
'title' => $ancestor_page->post_title, |
| 1723 |
), |
| 1724 |
// Suggest an alternate for the CONFLICTING ANCESTOR (the actual blocker), |
| 1725 |
// not the leaf — renaming the leaf wouldn't resolve an owned-ancestor clash. |
| 1726 |
'suggested_slug' => $this->suggest_available_slug($ancestor_path), |
| 1727 |
); |
| 1728 |
break; |
| 1729 |
} |
| 1730 |
$parent_id = $ancestor_page->ID; |
| 1731 |
} else { |
| 1732 |
// Auto-create a minimal LPS-owned placeholder so the URL nests. |
| 1733 |
$new_parent = wp_insert_post(array( |
| 1734 |
'post_title' => $seg, |
| 1735 |
'post_name' => $seg, |
| 1736 |
'post_type' => 'page', |
| 1737 |
'post_status' => $status, |
| 1738 |
'post_parent' => $parent_id, |
| 1739 |
'post_content' => '' |
| 1740 |
), true); |
| 1741 |
if (is_wp_error($new_parent)) { |
| 1742 |
$parent_ok = false; |
| 1743 |
break; |
| 1744 |
} |
| 1745 |
update_post_meta($new_parent, Metasync_Custom_Pages::META_LPS_IMPORT, '1'); |
| 1746 |
update_post_meta($new_parent, Metasync_Custom_Pages::META_ASSETS_FOLDER, $assets_folder); |
| 1747 |
if ($external_ref !== '') { |
| 1748 |
update_post_meta($new_parent, Metasync_Custom_Pages::META_LPS_PROJECT_REF, $external_ref); |
| 1749 |
} |
| 1750 |
// tag the placeholder with its manifest path so re-imports find it. |
| 1751 |
update_post_meta($new_parent, Metasync_Custom_Pages::META_LPS_PAGE_PATH, $ancestor_path); |
| 1752 |
$parent_id = $new_parent; |
| 1753 |
} |
| 1754 |
$slug_to_id[$ancestor_path] = $parent_id; |
| 1755 |
} |
| 1756 |
if (!$parent_ok) { |
| 1757 |
$failed[] = array_merge( |
| 1758 |
array( |
| 1759 |
'slug' => $effective_slug, |
| 1760 |
'code' => $parent_fail_code, |
| 1761 |
'message' => ($parent_fail_code === 'parent_not_owned') |
| 1762 |
? 'A parent page on this path is owned by existing non-LPS content; LPS will not nest under it.' |
| 1763 |
: 'Could not resolve the parent page hierarchy.', |
| 1764 |
), |
| 1765 |
$parent_fail_extra |
| 1766 |
); |
| 1767 |
continue; |
| 1768 |
} |
| 1769 |
|
| 1770 |
// find our previously-imported page by its stable tag (project + |
| 1771 |
// manifest path) first, so a re-import updates the SAME page even if WordPress |
| 1772 |
// renamed its slug on a collision — instead of creating a duplicate. Only when |
| 1773 |
// no tagged page exists do we fall back to the intended slug path, which is |
| 1774 |
// also where a non-LPS ownership conflict is detected (a user page sitting on |
| 1775 |
// the slug we want). |
| 1776 |
$existing = null; |
| 1777 |
$tagged_id = $this->find_lps_page_by_path($assets_folder, $external_ref, $effective_slug); |
| 1778 |
if ($tagged_id > 0) { |
| 1779 |
$existing = get_post($tagged_id); |
| 1780 |
} |
| 1781 |
if (!$existing) { |
| 1782 |
$existing = get_page_by_path($effective_slug, OBJECT, 'page'); |
| 1783 |
} |
| 1784 |
if ($existing) { |
| 1785 |
$is_lps = get_post_meta($existing->ID, Metasync_Custom_Pages::META_LPS_IMPORT, true) === '1'; |
| 1786 |
if (!$is_lps) { |
| 1787 |
$failed[] = array( |
| 1788 |
'slug' => $effective_slug, |
| 1789 |
'code' => 'slug_taken_by_existing_content', |
| 1790 |
'conflict_with' => array('post_id' => (int) $existing->ID, 'post_type' => $existing->post_type, 'title' => $existing->post_title), |
| 1791 |
'suggested_slug' => $this->suggest_available_slug($effective_slug), |
| 1792 |
); |
| 1793 |
continue; |
| 1794 |
} |
| 1795 |
if (!$overwrite) { |
| 1796 |
$failed[] = array('slug' => $effective_slug, 'code' => 'page_slug_exists', 'suggested_slug' => $this->suggest_available_slug($effective_slug)); |
| 1797 |
continue; |
| 1798 |
} |
| 1799 |
} |
| 1800 |
|
| 1801 |
// Create or update. |
| 1802 |
if ($existing) { |
| 1803 |
$res = wp_update_post(array( |
| 1804 |
'ID' => $existing->ID, |
| 1805 |
'post_title' => $title, |
| 1806 |
'post_status' => $status, |
| 1807 |
'post_parent' => $parent_id |
| 1808 |
), true); |
| 1809 |
if (is_wp_error($res)) { |
| 1810 |
$failed[] = array('slug' => $effective_slug, 'code' => 'page_update_failed', 'message' => $res->get_error_message()); |
| 1811 |
continue; |
| 1812 |
} |
| 1813 |
$page_id = $existing->ID; |
| 1814 |
$was_update = true; |
| 1815 |
} else { |
| 1816 |
$page_id = wp_insert_post(array( |
| 1817 |
'post_title' => $title, |
| 1818 |
'post_name' => $leaf, |
| 1819 |
'post_type' => 'page', |
| 1820 |
'post_status' => $status, |
| 1821 |
'post_parent' => $parent_id, |
| 1822 |
'post_content' => '' |
| 1823 |
), true); |
| 1824 |
if (is_wp_error($page_id)) { |
| 1825 |
$failed[] = array('slug' => $effective_slug, 'code' => 'page_insert_failed', 'message' => $page_id->get_error_message()); |
| 1826 |
continue; |
| 1827 |
} |
| 1828 |
$was_update = false; |
| 1829 |
} |
| 1830 |
|
| 1831 |
update_post_meta($page_id, Metasync_Custom_Pages::META_IS_CUSTOM_HTML_PAGE, '1'); |
| 1832 |
update_post_meta($page_id, Metasync_Custom_Pages::META_CREATED_VIA_API, '1'); |
| 1833 |
update_post_meta($page_id, Metasync_Custom_Pages::META_LPS_IMPORT, '1'); |
| 1834 |
update_post_meta($page_id, Metasync_Custom_Pages::META_ASSETS_FOLDER, $assets_folder); |
| 1835 |
if ($external_ref !== '') { |
| 1836 |
update_post_meta($page_id, Metasync_Custom_Pages::META_LPS_PROJECT_REF, $external_ref); |
| 1837 |
} |
| 1838 |
// tag the page with its manifest path so re-imports find it by identity, not slug. |
| 1839 |
update_post_meta($page_id, Metasync_Custom_Pages::META_LPS_PAGE_PATH, $effective_slug); |
| 1840 |
update_post_meta($page_id, Metasync_Custom_Pages::META_HTML_ENABLED, '1'); |
| 1841 |
update_post_meta($page_id, Metasync_Custom_Pages::META_HTML_CONTENT, wp_unslash($html)); |
| 1842 |
update_post_meta($page_id, Metasync_Custom_Pages::META_HTML_FILENAME, $rel); |
| 1843 |
wp_cache_delete($page_id, 'posts'); |
| 1844 |
wp_cache_delete($page_id, 'post_meta'); |
| 1845 |
|
| 1846 |
$slug_to_id[$effective_slug] = $page_id; |
| 1847 |
$record = array('slug' => $effective_slug, 'page_id' => $page_id, 'url' => get_permalink($page_id)); |
| 1848 |
if ($was_update) { |
| 1849 |
$updated[] = $record; |
| 1850 |
} else { |
| 1851 |
$created[] = $record; |
| 1852 |
} |
| 1853 |
} |
| 1854 |
|
| 1855 |
// Purge page + edge caches so the re-published HTML references the new asset hashes. |
| 1856 |
$this->purge_lps_page_caches(array_merge($created, $updated)); |
| 1857 |
|
| 1858 |
return array( |
| 1859 |
'success' => true, |
| 1860 |
'message' => sprintf('LPS multi-page import complete: %d created, %d updated, %d failed.', count($created), count($updated), count($failed)), |
| 1861 |
'data' => array( |
| 1862 |
'mode' => 'multi', |
| 1863 |
'assets_folder' => $assets_folder, |
| 1864 |
'assets_dir_url' => $assets_dir_url, |
| 1865 |
'pages_total' => count($pages), |
| 1866 |
'created' => $created, |
| 1867 |
'updated' => $updated, |
| 1868 |
'failed' => $failed, |
| 1869 |
) |
| 1870 |
); |
| 1871 |
} |
| 1872 |
|
| 1873 |
/** |
| 1874 |
* Find a previously-imported LPS page by its stable per-page identity |
| 1875 |
* (project key + manifest path), independent of the slug WordPress actually |
| 1876 |
* stored. This makes re-imports idempotent even when a slug was renamed on a |
| 1877 |
* collision (e.g. "2026" -> "2026-2"): we match on META_LPS_PAGE_PATH within |
| 1878 |
* the same project, so the existing page is updated instead of duplicated. |
| 1879 |
* |
| 1880 |
* The project is keyed on external_ref (the LPS project UUID) when present, |
| 1881 |
* falling back to assets_folder for pre-external_ref imports — mirroring |
| 1882 |
* find_lps_home_page(). |
| 1883 |
* |
| 1884 |
* @param string $assets_folder Per-project key (fallback). |
| 1885 |
* @param string $external_ref LPS project UUID (preferred key when present). |
| 1886 |
* @param string $page_path Manifest path of the page (e.g. "blog/2026/launch"). |
| 1887 |
* @return int Post ID of the matching LPS page, or 0 when none exists. |
| 1888 |
*/ |
| 1889 |
private function find_lps_page_by_path($assets_folder, $external_ref, $page_path) |
| 1890 |
{ |
| 1891 |
$page_path = (string) $page_path; |
| 1892 |
if ($page_path === '') { |
| 1893 |
return 0; |
| 1894 |
} |
| 1895 |
|
| 1896 |
// Prefer the stable project UUID; fall back to assets_folder. |
| 1897 |
if ($external_ref !== '') { |
| 1898 |
$project_clause = array('key' => Metasync_Custom_Pages::META_LPS_PROJECT_REF, 'value' => $external_ref); |
| 1899 |
} else { |
| 1900 |
$project_clause = array('key' => Metasync_Custom_Pages::META_ASSETS_FOLDER, 'value' => $assets_folder); |
| 1901 |
} |
| 1902 |
|
| 1903 |
// suppress_filters + explicit statuses mirror find_lps_home_page() so query |
| 1904 |
// hooks (e.g. WPML/Polylang on pre_get_posts) cannot silently alter this |
| 1905 |
// identity lookup and cause a missed match -> duplicate. The META_LPS_IMPORT |
| 1906 |
// clause enforces the "tagged == LPS-owned" invariant rather than assuming it. |
| 1907 |
$ids = get_posts(array( |
| 1908 |
'post_type' => 'page', |
| 1909 |
'post_status' => array('publish', 'future', 'draft', 'pending', 'private'), |
| 1910 |
'posts_per_page' => 1, |
| 1911 |
'fields' => 'ids', |
| 1912 |
'suppress_filters' => true, |
| 1913 |
'no_found_rows' => true, |
| 1914 |
'meta_query' => array( |
| 1915 |
'relation' => 'AND', |
| 1916 |
$project_clause, |
| 1917 |
array('key' => Metasync_Custom_Pages::META_LPS_PAGE_PATH, 'value' => $page_path), |
| 1918 |
array('key' => Metasync_Custom_Pages::META_LPS_IMPORT, 'value' => '1'), |
| 1919 |
), |
| 1920 |
)); |
| 1921 |
|
| 1922 |
return !empty($ids) ? (int) $ids[0] : 0; |
| 1923 |
} |
| 1924 |
|
| 1925 |
/** |
| 1926 |
* Find the next available page path by appending -2, -3, ... to the LEAF |
| 1927 |
* segment, when the requested slug is taken by content we may not overwrite. |
| 1928 |
* |
| 1929 |
* Accepts a full slug path. For a nested slug the suffix is applied to the |
| 1930 |
* last segment and the candidate is checked at its real position under the |
| 1931 |
* parent (e.g. "departments/cardiology" -> "departments/cardiology-2"), not |
| 1932 |
* at the site root. |
| 1933 |
* |
| 1934 |
* @param string $slug The desired (already sanitized) slug, possibly nested. |
| 1935 |
* @return string A full slug path that no existing page currently uses. |
| 1936 |
*/ |
| 1937 |
private function suggest_available_slug($slug) |
| 1938 |
{ |
| 1939 |
$slug = trim((string) $slug, '/'); |
| 1940 |
$pos = strrpos($slug, '/'); |
| 1941 |
$parent = ($pos === false) ? '' : substr($slug, 0, $pos + 1); // keeps trailing slash |
| 1942 |
$leaf = ($pos === false) ? $slug : substr($slug, $pos + 1); |
| 1943 |
|
| 1944 |
for ($i = 2; $i <= 100; $i++) { |
| 1945 |
$candidate = $parent . $leaf . '-' . $i; |
| 1946 |
if (!get_page_by_path($candidate, OBJECT, 'page')) { |
| 1947 |
return $candidate; |
| 1948 |
} |
| 1949 |
} |
| 1950 |
|
| 1951 |
// Extremely unlikely fallback: append a random suffix. |
| 1952 |
return $parent . $leaf . '-' . wp_rand(1000, 9999); |
| 1953 |
} |
| 1954 |
|
| 1955 |
/** |
| 1956 |
* Recursively delete a directory and its contents. |
| 1957 |
* |
| 1958 |
* Safe to call only on paths under wp-content/uploads/metasync-pages/ — |
| 1959 |
* callers are responsible for that containment check. |
| 1960 |
*/ |
| 1961 |
private function recursive_rmdir($dir) |
| 1962 |
{ |
| 1963 |
if (!file_exists($dir)) { |
| 1964 |
return true; |
| 1965 |
} |
| 1966 |
|
| 1967 |
if (!is_dir($dir)) { |
| 1968 |
return @unlink($dir); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink -- boolean return value is consumed |
| 1969 |
} |
| 1970 |
|
| 1971 |
$iterator = new RecursiveIteratorIterator( |
| 1972 |
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), |
| 1973 |
RecursiveIteratorIterator::CHILD_FIRST |
| 1974 |
); |
| 1975 |
|
| 1976 |
foreach ($iterator as $item) { |
| 1977 |
if ($item->isDir()) { |
| 1978 |
@rmdir($item->getPathname()); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- WP_Filesystem would prompt for FTP credentials on non-direct hosts |
| 1979 |
} else { |
| 1980 |
@wp_delete_file($item->getPathname()); |
| 1981 |
} |
| 1982 |
} |
| 1983 |
|
| 1984 |
return @rmdir($dir); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- WP_Filesystem would prompt for FTP credentials on non-direct hosts |
| 1985 |
} |
| 1986 |
|
| 1987 |
/** |
| 1988 |
* Write exactly one persistent audit record for an LPS ZIP import. |
| 1989 |
* |
| 1990 |
* Called from both the REST handler (import_lps_page) and the MCP tool on |
| 1991 |
* every exit path — success or failure. The record always lands in the |
| 1992 |
* existing metasync_sync_history table (covered by its bounded cleanup), |
| 1993 |
* independent of WP_DEBUG. Neither the ZIP bytes nor the page HTML are |
| 1994 |
* stored: only sizes, counts, slugs and short reasons. |
| 1995 |
* |
| 1996 |
* @param array $ctx { |
| 1997 |
* @type WP_Error|array|null $result Helper result (or WP_Error on failure). |
| 1998 |
* @type int $http_status Mapped HTTP status (200/207/422/409/423/...). |
| 1999 |
* @type array $input source_type, zip_size_bytes, assets_folder, pages_in_manifest. |
| 2000 |
* @type int $start_ms Start time in ms for duration (0 to skip). |
| 2001 |
* @type string $auth_method bearer|x-api-key|apikey-query|mcp-capability. |
| 2002 |
* @type string $api_key_prefix Masked key prefix (NEVER the full key). |
| 2003 |
* @type string $error_message Optional externally-supplied error message. |
| 2004 |
* } |
| 2005 |
* @return void |
| 2006 |
*/ |
| 2007 |
public static function write_lps_import_audit(array $ctx) |
| 2008 |
{ |
| 2009 |
try { |
| 2010 |
require_once dirname(__FILE__, 2) . '/sync-history/class-metasync-sync-history-database.php'; |
| 2011 |
|
| 2012 |
$result = isset($ctx['result']) ? $ctx['result'] : null; |
| 2013 |
$http_status = isset($ctx['http_status']) ? (int) $ctx['http_status'] : 500; |
| 2014 |
$input = (isset($ctx['input']) && is_array($ctx['input'])) ? $ctx['input'] : array(); |
| 2015 |
$start_ms = isset($ctx['start_ms']) ? (int) $ctx['start_ms'] : 0; |
| 2016 |
$auth_method = isset($ctx['auth_method']) ? (string) $ctx['auth_method'] : ''; |
| 2017 |
$api_key_prefix = isset($ctx['api_key_prefix']) ? (string) $ctx['api_key_prefix'] : ''; |
| 2018 |
$ext_error = isset($ctx['error_message']) ? (string) $ctx['error_message'] : ''; |
| 2019 |
$duration_ms = $start_ms > 0 ? max(0, (int) round(microtime(true) * 1000) - $start_ms) : null; |
| 2020 |
|
| 2021 |
$per_page = array(); |
| 2022 |
$counts = array('created' => 0, 'updated' => 0, 'failed' => 0, 'skipped' => 0); |
| 2023 |
$front_page = array('set' => false, 'page_id' => null); |
| 2024 |
$error_msg = $ext_error; |
| 2025 |
|
| 2026 |
if ($result instanceof WP_Error) { |
| 2027 |
$error_msg = $result->get_error_message(); |
| 2028 |
$counts['failed'] = 1; |
| 2029 |
} elseif (is_array($result) && isset($result['data']) && is_array($result['data'])) { |
| 2030 |
$data = $result['data']; |
| 2031 |
$mode = isset($data['mode']) ? $data['mode'] : ''; |
| 2032 |
|
| 2033 |
if ($mode === 'multi') { |
| 2034 |
foreach ((isset($data['created']) && is_array($data['created'])) ? $data['created'] : array() as $p) { |
| 2035 |
$per_page[] = array( |
| 2036 |
'slug' => isset($p['slug']) ? $p['slug'] : '', |
| 2037 |
'action' => 'created', |
| 2038 |
'page_id' => isset($p['page_id']) ? $p['page_id'] : null, |
| 2039 |
'reason' => null, |
| 2040 |
); |
| 2041 |
$counts['created']++; |
| 2042 |
if (!empty($p['is_front_page'])) { |
| 2043 |
$front_page = array('set' => true, 'page_id' => isset($p['page_id']) ? $p['page_id'] : null); |
| 2044 |
} |
| 2045 |
} |
| 2046 |
foreach ((isset($data['updated']) && is_array($data['updated'])) ? $data['updated'] : array() as $p) { |
| 2047 |
$per_page[] = array( |
| 2048 |
'slug' => isset($p['slug']) ? $p['slug'] : '', |
| 2049 |
'action' => 'updated', |
| 2050 |
'page_id' => isset($p['page_id']) ? $p['page_id'] : null, |
| 2051 |
'reason' => null, |
| 2052 |
); |
| 2053 |
$counts['updated']++; |
| 2054 |
if (!empty($p['is_front_page'])) { |
| 2055 |
$front_page = array('set' => true, 'page_id' => isset($p['page_id']) ? $p['page_id'] : null); |
| 2056 |
} |
| 2057 |
} |
| 2058 |
foreach ((isset($data['failed']) && is_array($data['failed'])) ? $data['failed'] : array() as $p) { |
| 2059 |
$reason = isset($p['code']) ? $p['code'] : ''; |
| 2060 |
if (isset($p['message'])) { |
| 2061 |
$reason .= ($reason !== '' ? ': ' : '') . $p['message']; |
| 2062 |
} |
| 2063 |
$per_page[] = array( |
| 2064 |
'slug' => isset($p['slug']) ? $p['slug'] : '', |
| 2065 |
'action' => 'failed', |
| 2066 |
'page_id' => null, |
| 2067 |
'reason' => $reason !== '' ? $reason : 'failed', |
| 2068 |
); |
| 2069 |
$counts['failed']++; |
| 2070 |
} |
| 2071 |
} elseif ($mode === 'single') { |
| 2072 |
$single_action = !empty($data['was_update']) ? 'updated' : 'created'; |
| 2073 |
$counts[$single_action] = 1; |
| 2074 |
$per_page[] = array( |
| 2075 |
'slug' => isset($data['slug']) ? $data['slug'] : '', |
| 2076 |
'action' => $single_action, |
| 2077 |
'page_id' => isset($data['page_id']) ? $data['page_id'] : null, |
| 2078 |
'reason' => null, |
| 2079 |
); |
| 2080 |
} |
| 2081 |
} elseif ($result === null) { |
| 2082 |
// Never reached the extraction helper (early validation failure). |
| 2083 |
$counts['failed'] = 1; |
| 2084 |
} |
| 2085 |
|
| 2086 |
if ($http_status === 200) { |
| 2087 |
$overall_status = 'success'; |
| 2088 |
} elseif ($http_status === 207) { |
| 2089 |
$overall_status = 'partial'; |
| 2090 |
} elseif ($http_status === 409) { |
| 2091 |
$overall_status = 'conflict'; |
| 2092 |
} elseif ($http_status === 423) { |
| 2093 |
$overall_status = 'locked'; |
| 2094 |
} else { |
| 2095 |
$overall_status = 'failed'; |
| 2096 |
} |
| 2097 |
|
| 2098 |
$assets_folder = isset($input['assets_folder']) ? $input['assets_folder'] : ''; |
| 2099 |
$title_slug = $assets_folder !== '' ? $assets_folder : (isset($per_page[0]['slug']) && $per_page[0]['slug'] !== '' ? $per_page[0]['slug'] : 'unknown'); |
| 2100 |
|
| 2101 |
$site_url = function_exists('get_site_url') ? get_site_url() : ''; |
| 2102 |
|
| 2103 |
$meta_payload = array( |
| 2104 |
'timestamp' => function_exists('current_time') ? current_time('mysql') : gmdate('Y-m-d H:i:s'), |
| 2105 |
'site_url' => $site_url, |
| 2106 |
'api_key_prefix' => $api_key_prefix, |
| 2107 |
'plugin_version' => defined('METASYNC_VERSION') ? METASYNC_VERSION : '', |
| 2108 |
'auth_method' => $auth_method, |
| 2109 |
'source_type' => isset($input['source_type']) ? $input['source_type'] : 'unknown', |
| 2110 |
'assets_folder' => $assets_folder, |
| 2111 |
'external_ref' => isset($input['external_ref']) ? (string) $input['external_ref'] : '', |
| 2112 |
'zip_size_bytes' => isset($input['zip_size_bytes']) ? (int) $input['zip_size_bytes'] : 0, |
| 2113 |
'pages_in_manifest' => isset($input['pages_in_manifest']) ? $input['pages_in_manifest'] : null, |
| 2114 |
'counts' => $counts, |
| 2115 |
'front_page' => $front_page, |
| 2116 |
'per_page' => $per_page, |
| 2117 |
'http_status' => $http_status, |
| 2118 |
'overall_status' => $overall_status, |
| 2119 |
'error_message' => $error_msg !== '' ? substr($error_msg, 0, 500) : '', |
| 2120 |
'duration_ms' => $duration_ms, |
| 2121 |
); |
| 2122 |
|
| 2123 |
// User-facing badge status: first import -> Published, re-sync -> Updated, |
| 2124 |
// nothing imported -> failed. Per-page conflicts are intentionally NOT |
| 2125 |
// surfaced here (Website Studio notifies the customer to republish). |
| 2126 |
if ($counts['created'] > 0) { |
| 2127 |
$db_status = 'published'; |
| 2128 |
} elseif ($counts['updated'] > 0) { |
| 2129 |
$db_status = 'updated'; |
| 2130 |
} else { |
| 2131 |
$db_status = 'failed'; |
| 2132 |
} |
| 2133 |
$db = new Metasync_Sync_History_Database(); |
| 2134 |
$db->add(array( |
| 2135 |
'title' => 'Website Studio (LPS) import: ' . $title_slug, |
| 2136 |
'source' => 'Website Studio (LPS)', |
| 2137 |
'status' => $db_status, |
| 2138 |
'content_type' => 'lps_import', |
| 2139 |
'url' => $site_url, |
| 2140 |
'meta_data' => json_encode($meta_payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), |
| 2141 |
)); |
| 2142 |
|
| 2143 |
// Optional deep-debug breadcrumb — the DB write above always happens |
| 2144 |
// regardless of WP_DEBUG; this line is purely supplementary. |
| 2145 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 2146 |
error_log(sprintf( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- debug-gated, no secrets |
| 2147 |
'Metasync LPS import audit: status=%s http=%d created=%d updated=%d failed=%d', |
| 2148 |
$overall_status, |
| 2149 |
$http_status, |
| 2150 |
$counts['created'], |
| 2151 |
$counts['updated'], |
| 2152 |
$counts['failed'] |
| 2153 |
)); |
| 2154 |
} |
| 2155 |
} catch (\Throwable $e) { |
| 2156 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 2157 |
error_log('Metasync LPS audit log failed: ' . $e->getMessage()); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- debug-gated, no secrets |
| 2158 |
} |
| 2159 |
} |
| 2160 |
} |
| 2161 |
} |
| 2162 |
|