get_json_params(); if (!is_array($params)) { return new WP_REST_Response(array('message' => 'A JSON request body is required'), 400); } $operation_id = $params['operation_id'] ?? ''; if (!is_string($operation_id) || !preg_match('/^[a-f0-9]{64}$/Di', $operation_id)) { return new WP_REST_Response(array('message' => 'A signed operation_id is required'), 401); } $post_id = $params['post_id'] ?? 0; if (!is_int($post_id) && (!is_string($post_id) || !preg_match('/^[0-9]+$/D', $post_id))) { return new WP_REST_Response(array('message' => 'Invalid post ID'), 422); } $mode = $params['mode'] ?? ''; if (!is_string($mode)) { return new WP_REST_Response(array('message' => 'Invalid repair mode'), 422); } $approved_before = $params['before_hash'] ?? ''; $approved_after = $params['after_hash'] ?? ''; if ($mode === 'apply' && (!is_string($approved_before) || !is_string($approved_after))) { return new WP_REST_Response(array('message' => 'Content changed after preview; prepare another proposal'), 409); } $id = absint($post_id); $post = get_post($id); if (!$post || $post->post_status !== 'publish' || !in_array($post->post_type, array('page', 'post', 'product'), true)) { return new WP_REST_Response(array('message' => 'Published resource unavailable'), 409); } if (!class_exists('WP_HTML_Tag_Processor') || strlen($post->post_content) > 500000) { return new WP_REST_Response(array('message' => 'Update WordPress or use manual link repair for this resource'), 409); } // Builder-managed bodies have another source of truth; editing their cache is unsafe. if (get_post_meta($id, '_elementor_edit_mode', true) || strpos($post->post_content, '[vc_') !== false) { return new WP_REST_Response(array('message' => 'Repair builder-managed links in the page builder'), 409); } $before = $post->post_content; $before_hash = hash('sha256', $before); if ($mode === 'read') { return new WP_REST_Response(array('link_repair_version' => 1, 'post_id' => $id, 'url' => get_permalink($id), 'body' => $before, 'body_hash' => $before_hash), 200); } $href = $params['href'] ?? null; $replacement = $params['replacement'] ?? null; if (!is_string($href) || $href === '' || strlen($href) > 2048 || !is_string($replacement) || ($replacement !== '' && (!preg_match('#^https?://#i', $replacement) || esc_url_raw($replacement) !== $replacement))) { return new WP_REST_Response(array('message' => 'Invalid link change'), 422); } if ($href === $replacement) return new WP_REST_Response(array('message' => 'Link is unchanged'), 422); $processor = new WP_HTML_Tag_Processor($before); $count = 0; while ($processor->next_tag('A')) { if ($processor->get_attribute('href') !== $href) continue; if ($replacement === '') $processor->remove_attribute('href'); else $processor->set_attribute('href', $replacement); $count++; } if (!$count) return new WP_REST_Response(array('message' => 'Link no longer exists in the stored body'), 409); $after = $processor->get_updated_html(); $after_hash = hash('sha256', $after); $result = array('post_id' => $id, 'before_hash' => $before_hash, 'after_hash' => $after_hash, 'before' => $before, 'after' => $after, 'occurrences' => $count, 'url' => get_permalink($id)); if ($mode === 'preview') return new WP_REST_Response($result, 200); if ($mode !== 'apply' || !hash_equals($before_hash, $approved_before) || !hash_equals($after_hash, $approved_after)) { return new WP_REST_Response(array('message' => 'Content changed after preview; prepare another proposal'), 409); } // Preserve a native revision as well as the platform's durable before/after record. wp_save_post_revision($id); global $wpdb; // Atomic content comparison protects against a concurrent editor save. Do not // use a read followed by an unconditional wp_update_post overwrite. $changed = $wpdb->query($wpdb->prepare( "UPDATE {$wpdb->posts} SET post_content = %s, post_modified = %s, post_modified_gmt = %s WHERE ID = %d AND BINARY post_content = BINARY %s AND post_status = 'publish'", $after, current_time('mysql'), current_time('mysql', true), $id, $before )); if ($changed !== 1) return new WP_REST_Response(array('message' => 'Concurrent content change; nothing overwritten'), 409); clean_post_cache($id); $updated = get_post($id); wp_save_post_revision($id); do_action('post_updated', $id, $updated, $post); do_action('save_post_' . $updated->post_type, $id, $updated, true); do_action('save_post', $id, $updated, true); do_action('wp_after_insert_post', $id, $updated, true, $post); return new WP_REST_Response(array('post_id' => $id, 'body_hash' => hash('sha256', get_post($id)->post_content), 'occurrences' => $count), 200); }