- Modernized authentication with the introduction of TenantProject

- Created a base mail template
- Adapt and improve document API to usage of catalogs and processors
- Adapt eveai_sync to new authentication mechanism and usage of catalogs and processors
This commit is contained in:
Josako
2024-11-21 17:24:33 +01:00
parent 4c009949b3
commit 7702a6dfcc
72 changed files with 2338 additions and 503 deletions

View File

@@ -6,6 +6,7 @@ class EveAI_API {
private $api_key;
private $access_token;
private $token_expiry;
private $catalog_id;
public function __construct() {
$this->api_url = get_option('eveai_api_url');
@@ -58,57 +59,70 @@ class EveAI_API {
}
private function make_request($method, $endpoint, $data = null) {
$this->ensure_valid_token();
try {
$this->ensure_valid_token();
error_log('EveAI API Request: ' . $method . ' ' . $this->api_url . $endpoint);
error_log('EveAI API Request: ' . $method . ' ' . $this->api_url . $endpoint);
$url = $this->api_url . $endpoint;
$url = $this->api_url . $endpoint;
$args = array(
'method' => $method,
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->access_token,
)
);
$args = array(
'method' => $method,
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->access_token,
)
);
if ($data !== null) {
$args['body'] = json_encode($data);
if ($data !== null) {
$args['body'] = json_encode($data);
}
$response = wp_remote_request($url, $args);
if (is_wp_error($response)) {
$error_message = $response->get_error_message();
error_log('EveAI API Error: ' . $error_message);
throw new Exception('API request failed: ' . $error_message);
}
$body = wp_remote_retrieve_body($response);
$status_code = wp_remote_retrieve_response_code($response);
error_log('EveAI API Response: ' . print_r($body, true));
error_log('EveAI API Status Code: ' . $status_code);
// Check if the body is already an array (decoded JSON)
if (!is_array($body)) {
$body = json_decode($body, true);
}
if ($status_code == 401) {
// Token might have expired, try to get a new one and retry the request
error_log('Token expired, trying to get a new one...');
$this->get_new_token();
return $this->make_request($method, $endpoint, $data);
}
if ($status_code >= 400) {
$error_type = isset($response_data['type']) ? $response_data['type'] : 'Unknown';
$error_message = isset($response_data['message']) ? $response_data['message'] : 'Unknown error';
$error_details = isset($response_data['debug']) ? json_encode($response_data['debug']) : '';
error_log("EveAI API Error ({$error_type}): {$error_message}");
if ($error_details) {
error_log("EveAI API Error Details: {$error_details}");
}
throw new Exception("API error ({$error_type}): {$error_message}");
}
return $response_data
// return $body;
} catch (Exception $e) {
error_log("EveAI API Exception: " . $e->getMessage());
throw $e;
}
$response = wp_remote_request($url, $args);
if (is_wp_error($response)) {
$error_message = $response->get_error_message();
error_log('EveAI API Error: ' . $error_message);
throw new Exception('API request failed: ' . $error_message);
}
$body = wp_remote_retrieve_body($response);
$status_code = wp_remote_retrieve_response_code($response);
error_log('EveAI API Response: ' . print_r($body, true));
error_log('EveAI API Status Code: ' . $status_code);
// Check if the body is already an array (decoded JSON)
if (!is_array($body)) {
$body = json_decode($body, true);
}
if ($status_code == 401) {
// Token might have expired, try to get a new one and retry the request
error_log('Token expired, trying to get a new one...');
$this->get_new_token();
return $this->make_request($method, $endpoint, $data);
}
if ($status_code >= 400) {
$error_message = isset($body['message']) ? $body['message'] : 'Unknown error';
error_log('EveAI API Error: ' . $error_message);
throw new Exception('API error: ' . $error_message);
}
return $body;
}
public function add_url($data) {
@@ -121,10 +135,19 @@ class EveAI_API {
}
public function invalidate_document($document_id) {
$data = array(
'valid_to' => gmdate('Y-m-d\TH:i:s\Z') // Current UTC time in ISO 8601 format
);
return $this->make_request('PUT', "/api/v1/documents/{$document_id}", $data);
error_log("EveAI API: Attempting to invalidate document {$document_id}");
try {
$result = $this->make_request('PUT', "/api/v1/documents/{$document_id}", [
'valid_to' => gmdate('Y-m-d\TH:i:s\Z') // Current UTC time in ISO 8601 format
]);
error_log("EveAI API: Successfully invalidated document {$document_id}");
return $result;
} catch (Exception $e) {
error_log("EveAI API: Error invalidating document {$document_id}: " . $e->getMessage());
throw $e;
}
}
public function refresh_document($document_id) {

View File

@@ -100,19 +100,20 @@ class EveAI_Post_Handler {
$post = get_post($post_id);
return array(
'name' => $post->post_title,
'system_metadata' => json_encode([
'user_metadata' => json_encode([
'post_id' => $post_id,
'type' => $post->post_type,
'author' => get_the_author_meta('display_name', $post->post_author),
'categories' => $post->post_type === 'post' ? wp_get_post_categories($post_id, array('fields' => 'names')) : [],
'tags' => $post->post_type === 'post' ? wp_get_post_tags($post_id, array('fields' => 'names')) : [],
]),
])
);
}
private function has_metadata_changed($old_data, $new_data) {
return $old_data['name'] !== $new_data['name'] ||
$old_data['user_metadata'] !== $new_data['user_metadata'];
(isset($old_data['user_metadata']) && isset($new_data['user_metadata']) &&
$old_data['user_metadata'] !== $new_data['user_metadata']);
}
private function refresh_document_with_info($evie_id, $data) {
@@ -139,17 +140,27 @@ class EveAI_Post_Handler {
}
public function handle_post_delete($post_id) {
// First check if we have an EveAI document ID for this post
$evie_id = get_post_meta($post_id, '_eveai_document_id', true);
if ($evie_id) {
try {
$this->api->invalidate_document($evie_id);
error_log("EveAI: Attempting to invalidate document {$evie_id} for post {$post_id}");
$result = $this->api->invalidate_document($evie_id);
error_log("EveAI: Successfully invalidated document {$evie_id}");
// Clean up post meta
delete_post_meta($post_id, '_eveai_document_id');
delete_post_meta($post_id, '_eveai_document_version_id');
return true;
} catch (Exception $e) {
error_log('EveAI invalidate error: ' . $e->getMessage());
add_action('admin_notices', function() use ($e) {
echo '<div class="notice notice-error is-dismissible">';
echo '<p>EveAI Sync Error: ' . esc_html($e->getMessage()) . '</p>';
echo '</div>';
});
error_log("EveAI: Error invalidating document {$evie_id}: " . $e->getMessage());
return false;
}
} else {
error_log("EveAI: No document ID found for post {$post_id}, skipping invalidation");
return false;
}
}
@@ -195,8 +206,10 @@ class EveAI_Post_Handler {
private function prepare_post_data($post_id) {
$post = get_post($post_id);
// Get the permalink but replace localhost with the FQDN, keeping the port
$url = get_permalink($post_id);
$data = array(
'url' => get_permalink($post_id),
'url' => $url,
'name' => $post->post_title,
'language' => get_option('eveai_default_language', 'en'),
'valid_from' => get_gmt_from_date($post->post_date, 'Y-m-d\TH:i:s\Z'),

View File

@@ -29,5 +29,19 @@ class EveAI_Sync {
add_action('add_meta_boxes', array($this->admin, 'add_sync_meta_box'));
add_action('eveai_sync_post', array($this->post_handler, 'sync_post'), 10, 2);
add_action('wp_ajax_eveai_bulk_sync', array($this->admin, 'handle_bulk_sync_ajax'));
// Additional delete hooks
add_action('trashed_post', array($this->post_handler, 'handle_post_delete'));
add_action('wp_trash_post', array($this->post_handler, 'handle_post_delete'));
// Add debug logging for delete actions
add_action('before_delete_post', function($post_id) {
error_log("EveAI Debug: before_delete_post triggered for post {$post_id}");
});
add_action('trashed_post', function($post_id) {
error_log("EveAI Debug: trashed_post triggered for post {$post_id}");
});
add_action('wp_trash_post', function($post_id) {
error_log("EveAI Debug: wp_trash_post triggered for post {$post_id}");
});
}
}