This commit is contained in:
alorig
2025-11-22 19:46:34 +05:00
parent cbb6198214
commit 8296685fbd
34 changed files with 12200 additions and 1 deletions

View File

@@ -0,0 +1,465 @@
<?php
/**
* IGNY8 API Client Class
*
* Handles all communication with IGNY8 API v1.0
* Follows WORDPRESS-PLUGIN-INTEGRATION.md guidelines
*
* @package Igny8Bridge
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
/**
* Igny8API Class
*/
class Igny8API {
/**
* API base URL
* Note: Base is /api, endpoints should include /v1/ prefix
*
* @var string
*/
private $base_url = 'https://api.igny8.com/api';
/**
* API key (used as access token)
*
* @var string|null
*/
private $access_token = null;
/**
* Whether authentication is via API key (always true now)
*
* @var bool
*/
private $api_key_auth = true;
/**
* Constructor
* Only uses API key for authentication
*/
public function __construct() {
if (function_exists('igny8_get_secure_option')) {
$api_key = igny8_get_secure_option('igny8_api_key');
} else {
$api_key = get_option('igny8_api_key');
}
// API key is the only authentication method
if (!empty($api_key)) {
$this->access_token = $api_key;
$this->api_key_auth = true;
}
}
/**
* Connect using API key
* Tests connection by calling /v1/integration/integrations/test-connection/ endpoint
*
* @param string $api_key API key from IGNY8 app
* @param int $site_id Site ID from IGNY8 app
* @return bool True on success, false on failure
*/
public function connect($api_key, $site_id = null) {
if (empty($api_key)) {
return false;
}
// Store API key
if (function_exists('igny8_store_secure_option')) {
igny8_store_secure_option('igny8_api_key', $api_key);
} else {
update_option('igny8_api_key', $api_key);
}
$this->access_token = $api_key;
$this->api_key_auth = true;
// If site_id provided, test connection to integration endpoint
if (!empty($site_id)) {
$test_response = $this->post('/v1/integration/integrations/test-connection/', array(
'site_id' => (int) $site_id,
'api_key' => $api_key,
'site_url' => get_site_url()
));
if ($test_response['success']) {
$timestamp = current_time('timestamp');
update_option('igny8_last_api_health_check', $timestamp);
return true;
}
return false;
}
// Fallback: if no site_id, just verify API key exists by making a simple call
// This tests that the API key is valid format at least
$timestamp = current_time('timestamp');
update_option('igny8_last_api_health_check', $timestamp);
return true;
}
/**
* Check if API is authenticated
*
* @return bool True if authenticated, false otherwise
*/
public function is_authenticated() {
return !empty($this->access_token);
}
/**
* Parse unified API response
*
* @param array|WP_Error $response HTTP response
* @return array Parsed response
*/
private function parse_response($response) {
if (is_wp_error($response)) {
return array(
'success' => false,
'error' => $response->get_error_message(),
'http_status' => 0
);
}
$status_code = wp_remote_retrieve_response_code($response);
$raw_body = wp_remote_retrieve_body($response);
$body = json_decode($raw_body, true);
// Handle non-JSON responses — allow empty arrays/objects but detect JSON decode errors
if (json_last_error() !== JSON_ERROR_NONE) {
return array(
'success' => false,
'error' => 'Invalid JSON response: ' . json_last_error_msg(),
'raw_body' => substr($raw_body, 0, 200),
'http_status' => $status_code
);
}
// Check if response follows unified format
if (isset($body['success'])) {
$body['http_status'] = $status_code;
// Handle throttling errors (429) - extract retry delay from error message
if ($status_code === 429 && isset($body['error'])) {
// Extract delay from error message like "Request was throttled. Expected available in 1 second."
if (preg_match('/Expected available in (\d+) second/i', $body['error'], $matches)) {
$body['retry_after'] = intval($matches[1]);
} elseif (preg_match('/(\d+) second/i', $body['error'], $matches)) {
$body['retry_after'] = intval($matches[1]);
} else {
// Default to 2 seconds if we can't parse it
$body['retry_after'] = 2;
}
}
return $body;
}
// Legacy format - wrap in unified format
if ($status_code >= 200 && $status_code < 300) {
return array(
'success' => true,
'data' => $body,
'http_status' => $status_code
);
} else {
$error_message = $body['detail'] ?? 'HTTP ' . $status_code . ' error';
// Handle throttling in legacy format
$retry_after = null;
if ($status_code === 429) {
if (preg_match('/Expected available in (\d+) second/i', $error_message, $matches)) {
$retry_after = intval($matches[1]);
} elseif (preg_match('/(\d+) second/i', $error_message, $matches)) {
$retry_after = intval($matches[1]);
} else {
$retry_after = 2;
}
}
$result = array(
'success' => false,
'error' => $error_message,
'http_status' => $status_code,
'raw_error' => $body
);
if ($retry_after !== null) {
$result['retry_after'] = $retry_after;
}
return $result;
}
}
/**
* Get headers with authentication
* Uses Bearer token format for API key authentication
*
* @return array Headers array
*/
private function get_headers() {
$headers = array(
'Content-Type' => 'application/json',
'Accept' => 'application/json'
);
if (!empty($this->access_token)) {
$headers['Authorization'] = 'Bearer ' . $this->access_token;
}
return $headers;
}
/**
* Make GET request with automatic retry on throttling
*
* @param string $endpoint API endpoint (e.g. /v1/auth/sites/ or /v1/integration/integrations/)
* @param int $max_retries Maximum number of retries for throttled requests (default: 3)
* @return array Response data
*/
public function get($endpoint, $max_retries = 3) {
if (!$this->is_authenticated()) {
return array('success' => false, 'error' => 'Not authenticated', 'http_status' => 401);
}
// Ensure endpoint starts with /v1
if (strpos($endpoint, '/v1/') === false) {
if (strpos($endpoint, '/') !== 0) {
$endpoint = '/' . $endpoint;
}
if (strpos($endpoint, '/v1') !== 0) {
$endpoint = '/v1' . $endpoint;
}
}
$url = $this->base_url . $endpoint;
$headers = $this->get_headers();
$retry_count = 0;
while ($retry_count <= $max_retries) {
// Debug logging (enable with WP_DEBUG or IGNY8_DEBUG constant)
$debug_enabled = (defined('WP_DEBUG') && WP_DEBUG) || (defined('IGNY8_DEBUG') && IGNY8_DEBUG);
if ($debug_enabled) {
error_log(sprintf(
'IGNY8 DEBUG GET: %s | Headers: %s',
$url,
json_encode(array_merge($headers, array('Authorization' => 'Bearer ***')))
));
}
$response = wp_remote_get($url, array(
'headers' => $headers,
'timeout' => 30
));
// Debug response
if ($debug_enabled) {
$status_code = wp_remote_retrieve_response_code($response);
$response_body = wp_remote_retrieve_body($response);
error_log(sprintf(
'IGNY8 DEBUG RESPONSE: Status=%s | Body=%s',
$status_code,
substr($response_body, 0, 500)
));
}
$body = $this->parse_response($response);
// If throttled (429), retry after the specified delay
if (isset($body['http_status']) && $body['http_status'] === 429 && $retry_count < $max_retries) {
$retry_after = isset($body['retry_after']) ? $body['retry_after'] : 2;
// Add a small buffer (0.5 seconds) to ensure we wait long enough
$wait_time = $retry_after + 0.5;
$wait_seconds = (int) ceil($wait_time); // Convert to integer, rounding up
// Log retry attempt
if ($debug_enabled) {
error_log(sprintf(
'IGNY8 DEBUG: Request throttled, retrying after %.1f seconds (attempt %d/%d)',
$wait_time,
$retry_count + 1,
$max_retries
));
}
// Wait before retrying
sleep($wait_seconds);
$retry_count++;
continue;
}
// Not throttled or max retries reached, return response
// API keys don't expire, so no refresh logic needed
// If 401, the API key is invalid or revoked
return $body;
}
// Should never reach here, but return last response if we do
return $body;
}
/**
* Make POST request with automatic retry on throttling
*
* @param string $endpoint API endpoint (e.g. /v1/integration/integrations/)
* @param array $data Request data
* @param int $max_retries Maximum number of retries for throttled requests (default: 3)
* @return array Response data
*/
public function post($endpoint, $data, $max_retries = 3) {
if (!$this->is_authenticated()) {
return array('success' => false, 'error' => 'Not authenticated', 'http_status' => 401);
}
// Ensure endpoint starts with /v1
if (strpos($endpoint, '/v1/') === false) {
if (strpos($endpoint, '/') !== 0) {
$endpoint = '/' . $endpoint;
}
if (strpos($endpoint, '/v1') !== 0) {
$endpoint = '/v1' . $endpoint;
}
}
$retry_count = 0;
while ($retry_count <= $max_retries) {
$response = wp_remote_post($this->base_url . $endpoint, array(
'headers' => $this->get_headers(),
'body' => json_encode($data),
'timeout' => 60
));
$body = $this->parse_response($response);
// If throttled (429), retry after the specified delay
if (isset($body['http_status']) && $body['http_status'] === 429 && $retry_count < $max_retries) {
$retry_after = isset($body['retry_after']) ? $body['retry_after'] : 2;
// Add a small buffer (0.5 seconds) to ensure we wait long enough
$wait_time = $retry_after + 0.5;
$wait_seconds = (int) ceil($wait_time); // Convert to integer, rounding up
// Log retry attempt
$debug_enabled = (defined('WP_DEBUG') && WP_DEBUG) || (defined('IGNY8_DEBUG') && IGNY8_DEBUG);
if ($debug_enabled) {
error_log(sprintf(
'IGNY8 DEBUG: Request throttled, retrying after %.1f seconds (attempt %d/%d)',
$wait_time,
$retry_count + 1,
$max_retries
));
}
// Wait before retrying
sleep($wait_seconds);
$retry_count++;
continue;
}
// Not throttled or max retries reached, return response
return $body;
}
// Should never reach here, but return last response if we do
return $body;
}
/**
* Make PUT request with automatic retry on throttling
*
* @param string $endpoint API endpoint (e.g. /v1/integration/integrations/1/update-structure/)
* @param array $data Request data
* @param int $max_retries Maximum number of retries for throttled requests (default: 3)
* @return array Response data
*/
public function put($endpoint, $data, $max_retries = 3) {
if (!$this->is_authenticated()) {
return array('success' => false, 'error' => 'Not authenticated', 'http_status' => 401);
}
// Ensure endpoint starts with /v1
if (strpos($endpoint, '/v1/') === false) {
if (strpos($endpoint, '/') !== 0) {
$endpoint = '/' . $endpoint;
}
if (strpos($endpoint, '/v1') !== 0) {
$endpoint = '/v1' . $endpoint;
}
}
$retry_count = 0;
while ($retry_count <= $max_retries) {
$response = wp_remote_request($this->base_url . $endpoint, array(
'method' => 'PUT',
'headers' => $this->get_headers(),
'body' => json_encode($data),
'timeout' => 60
));
$body = $this->parse_response($response);
// If throttled (429), retry after the specified delay
if (isset($body['http_status']) && $body['http_status'] === 429 && $retry_count < $max_retries) {
$retry_after = isset($body['retry_after']) ? $body['retry_after'] : 2;
$wait_time = $retry_after + 0.5;
$wait_seconds = (int) ceil($wait_time); // Convert to integer, rounding up
$debug_enabled = (defined('WP_DEBUG') && WP_DEBUG) || (defined('IGNY8_DEBUG') && IGNY8_DEBUG);
if ($debug_enabled) {
error_log(sprintf(
'IGNY8 DEBUG: Request throttled, retrying after %.1f seconds (attempt %d/%d)',
$wait_time,
$retry_count + 1,
$max_retries
));
}
sleep($wait_seconds);
$retry_count++;
continue;
}
return $body;
}
return $body;
}
/**
* Make DELETE request
*
* @param string $endpoint API endpoint
* @return array Response data
*/
public function delete($endpoint) {
if (!$this->is_authenticated()) {
return array('success' => false, 'error' => 'Not authenticated', 'http_status' => 401);
}
$response = wp_remote_request($this->base_url . $endpoint, array(
'method' => 'DELETE',
'headers' => $this->get_headers(),
'timeout' => 30
));
return $this->parse_response($response);
}
/**
* Get access token
*
* @return string|null Access token
*/
public function get_access_token() {
return $this->access_token;
}
}

View File

@@ -0,0 +1,202 @@
<?php
/**
* Link Insertion Queue
*
* Queues and processes link recommendations from IGNY8 Linker
*
* @package Igny8Bridge
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
/**
* Queue link insertion
*
* @param array $link_data Link data
* @return int|false Queue ID or false on failure
*/
function igny8_queue_link_insertion($link_data) {
if (!igny8_is_connection_enabled()) {
return false;
}
$queue = get_option('igny8_link_queue', array());
$queue_item = array(
'id' => uniqid('link_', true),
'post_id' => intval($link_data['post_id']),
'target_url' => esc_url_raw($link_data['target_url']),
'anchor' => sanitize_text_field($link_data['anchor']),
'source' => sanitize_text_field($link_data['source'] ?? 'igny8_linker'),
'priority' => sanitize_text_field($link_data['priority'] ?? 'normal'),
'status' => 'pending',
'created_at' => $link_data['created_at'] ?? current_time('mysql'),
'attempts' => 0
);
$queue[] = $queue_item;
// Limit queue size (keep last 1000 items)
if (count($queue) > 1000) {
$queue = array_slice($queue, -1000);
}
update_option('igny8_link_queue', $queue);
// Trigger processing if not already scheduled
if (!wp_next_scheduled('igny8_process_link_queue')) {
wp_schedule_single_event(time() + 60, 'igny8_process_link_queue');
}
return $queue_item['id'];
}
/**
* Process link insertion queue
*/
function igny8_process_link_queue() {
if (!igny8_is_connection_enabled()) {
return;
}
if (function_exists('igny8_is_module_enabled') && !igny8_is_module_enabled('linker')) {
return;
}
$queue = get_option('igny8_link_queue', array());
if (empty($queue)) {
return;
}
// Process up to 10 items per run
$processed = 0;
$max_per_run = 10;
foreach ($queue as $key => $item) {
if ($processed >= $max_per_run) {
break;
}
if ($item['status'] !== 'pending') {
continue;
}
$result = igny8_insert_link_into_post($item);
if ($result['success']) {
$queue[$key]['status'] = 'completed';
$queue[$key]['completed_at'] = current_time('mysql');
} else {
$queue[$key]['attempts']++;
if ($queue[$key]['attempts'] >= 3) {
$queue[$key]['status'] = 'failed';
$queue[$key]['error'] = $result['error'] ?? 'Unknown error';
}
}
$processed++;
}
update_option('igny8_link_queue', $queue);
// Schedule next run if there are pending items
$has_pending = false;
foreach ($queue as $item) {
if ($item['status'] === 'pending') {
$has_pending = true;
break;
}
}
if ($has_pending && !wp_next_scheduled('igny8_process_link_queue')) {
wp_schedule_single_event(time() + 60, 'igny8_process_link_queue');
}
}
/**
* Insert link into post content
*
* @param array $link_item Link queue item
* @return array Result
*/
function igny8_insert_link_into_post($link_item) {
$post_id = $link_item['post_id'];
$target_url = $link_item['target_url'];
$anchor = $link_item['anchor'];
$post = get_post($post_id);
if (!$post) {
return array('success' => false, 'error' => 'Post not found');
}
$content = $post->post_content;
// Check if link already exists
if (strpos($content, $target_url) !== false) {
return array('success' => true, 'message' => 'Link already exists');
}
// Find first occurrence of anchor text not already in a link
$anchor_escaped = preg_quote($anchor, '/');
// Pattern to find anchor text that's not inside an <a> tag
// This is a simplified approach - find anchor text and check if it's not in a link
$pattern = '/\b' . $anchor_escaped . '\b/i';
if (preg_match_all($pattern, $content, $matches, PREG_OFFSET_CAPTURE)) {
foreach ($matches[0] as $match) {
$position = $match[1];
$length = strlen($match[0]);
// Check if this position is inside an <a> tag
$before = substr($content, 0, $position);
$after = substr($content, $position + $length);
// Count unclosed <a> tags before this position
$open_tags = substr_count($before, '<a');
$close_tags = substr_count($before, '</a>');
// If not inside a link, replace it
if ($open_tags <= $close_tags) {
$link_html = '<a href="' . esc_url($target_url) . '">' . esc_html($anchor) . '</a>';
$new_content = substr_replace($content, $link_html, $position, $length);
$result = wp_update_post(array(
'ID' => $post_id,
'post_content' => $new_content
));
if ($result && !is_wp_error($result)) {
return array('success' => true, 'message' => 'Link inserted');
} else {
return array('success' => false, 'error' => 'Failed to update post');
}
}
}
}
// If anchor not found, append link at end of content
$link_html = "\n\n<p><a href=\"" . esc_url($target_url) . "\">" . esc_html($anchor) . "</a></p>";
$new_content = $content . $link_html;
$result = wp_update_post(array(
'ID' => $post_id,
'post_content' => $new_content
));
if ($result && !is_wp_error($result)) {
return array('success' => true, 'message' => 'Link appended');
} else {
return array('success' => false, 'error' => 'Failed to update post');
}
}
// Register cron hook
add_action('igny8_process_link_queue', 'igny8_process_link_queue');

View File

@@ -0,0 +1,444 @@
<?php
/**
* REST API Endpoints for IGNY8
*
* Provides endpoints for IGNY8 to query WordPress posts by content_id
*
* @package Igny8Bridge
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
/**
* Igny8RestAPI Class
*/
class Igny8RestAPI {
/**
* Constructor
*/
public function __construct() {
add_action('rest_api_init', array($this, 'register_routes'));
}
/**
* Register REST API routes
*/
public function register_routes() {
// Get post by IGNY8 content_id
register_rest_route('igny8/v1', '/post-by-content-id/(?P<content_id>\d+)', array(
'methods' => 'GET',
'callback' => array($this, 'get_post_by_content_id'),
'permission_callback' => array($this, 'check_permission'),
'args' => array(
'content_id' => array(
'required' => true,
'type' => 'integer',
'description' => 'IGNY8 content ID'
)
)
));
// Get post by IGNY8 task_id
register_rest_route('igny8/v1', '/post-by-task-id/(?P<task_id>\d+)', array(
'methods' => 'GET',
'callback' => array($this, 'get_post_by_task_id'),
'permission_callback' => array($this, 'check_permission'),
'args' => array(
'task_id' => array(
'required' => true,
'type' => 'integer',
'description' => 'IGNY8 task ID'
)
)
));
// Get post status by content_id
register_rest_route('igny8/v1', '/post-status/(?P<content_id>\d+)', array(
'methods' => 'GET',
'callback' => array($this, 'get_post_status_by_content_id'),
'permission_callback' => array($this, 'check_permission'),
'args' => array(
'content_id' => array(
'required' => true,
'type' => 'integer',
'description' => 'IGNY8 content ID'
)
)
));
// Site metadata - post types, taxonomies and counts (unified response format)
register_rest_route('igny8/v1', '/site-metadata/', array(
'methods' => 'GET',
// We perform permission checks inside callback to ensure unified response format
'callback' => array($this, 'get_site_metadata'),
'permission_callback' => '__return_true',
));
// Plugin status endpoint - returns connection status and API key info
register_rest_route('igny8/v1', '/status', array(
'methods' => 'GET',
'callback' => array($this, 'get_status'),
'permission_callback' => '__return_true', // Public endpoint for health checks
));
}
/**
* Check API permission - uses API key only
*
* @param WP_REST_Request $request Request object
* @return bool|WP_Error
*/
public function check_permission($request) {
// Check if authenticated with IGNY8 via API key
$api = new Igny8API();
// Accept explicit X-IGNY8-API-KEY header for incoming requests
$header_api_key = $request->get_header('x-igny8-api-key');
if ($header_api_key) {
$stored_api_key = function_exists('igny8_get_secure_option') ? igny8_get_secure_option('igny8_api_key') : get_option('igny8_api_key');
if ($stored_api_key && hash_equals($stored_api_key, $header_api_key)) {
return true;
}
}
// Check Authorization Bearer header
$auth_header = $request->get_header('Authorization');
if ($auth_header) {
$stored_api_key = function_exists('igny8_get_secure_option') ? igny8_get_secure_option('igny8_api_key') : get_option('igny8_api_key');
if ($stored_api_key && strpos($auth_header, 'Bearer ' . $stored_api_key) !== false) {
return true;
}
}
// Allow if API key is configured (for internal use)
if ($api->is_authenticated()) {
return true;
}
return new WP_Error(
'rest_forbidden',
__('IGNY8 API key not authenticated', 'igny8-bridge'),
array('status' => 401)
);
}
/**
* Get post by content_id
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response|WP_Error
*/
public function get_post_by_content_id($request) {
// Double-check connection is enabled
if (!igny8_is_connection_enabled()) {
return new WP_Error(
'rest_forbidden',
__('IGNY8 connection is disabled', 'igny8-bridge'),
array('status' => 403)
);
}
$content_id = intval($request['content_id']);
// Find post by content_id meta
$posts = get_posts(array(
'meta_key' => '_igny8_content_id',
'meta_value' => $content_id,
'post_type' => 'any',
'posts_per_page' => 1,
'post_status' => 'any'
));
if (empty($posts)) {
return new WP_Error(
'rest_not_found',
__('Post not found for this content ID', 'igny8-bridge'),
array('status' => 404)
);
}
$post = $posts[0];
return rest_ensure_response(array(
'success' => true,
'data' => array(
'post_id' => $post->ID,
'title' => $post->post_title,
'status' => $post->post_status,
'wordpress_status' => $post->post_status,
'igny8_status' => igny8_map_wp_status_to_igny8($post->post_status),
'url' => get_permalink($post->ID),
'post_type' => $post->post_type,
'content_id' => $content_id,
'task_id' => get_post_meta($post->ID, '_igny8_task_id', true),
'last_synced' => get_post_meta($post->ID, '_igny8_last_synced', true)
)
));
}
/**
* Get post by task_id
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response|WP_Error
*/
public function get_post_by_task_id($request) {
// Double-check connection is enabled
if (!igny8_is_connection_enabled()) {
return new WP_Error(
'rest_forbidden',
__('IGNY8 connection is disabled', 'igny8-bridge'),
array('status' => 403)
);
}
$task_id = intval($request['task_id']);
// Find post by task_id meta
$posts = get_posts(array(
'meta_key' => '_igny8_task_id',
'meta_value' => $task_id,
'post_type' => 'any',
'posts_per_page' => 1,
'post_status' => 'any'
));
if (empty($posts)) {
return new WP_Error(
'rest_not_found',
__('Post not found for this task ID', 'igny8-bridge'),
array('status' => 404)
);
}
$post = $posts[0];
return rest_ensure_response(array(
'success' => true,
'data' => array(
'post_id' => $post->ID,
'title' => $post->post_title,
'status' => $post->post_status,
'wordpress_status' => $post->post_status,
'igny8_status' => igny8_map_wp_status_to_igny8($post->post_status),
'url' => get_permalink($post->ID),
'post_type' => $post->post_type,
'task_id' => $task_id,
'content_id' => get_post_meta($post->ID, '_igny8_content_id', true),
'last_synced' => get_post_meta($post->ID, '_igny8_last_synced', true)
)
));
}
/**
* Get post status by content_id
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response|WP_Error
*/
public function get_post_status_by_content_id($request) {
// Double-check connection is enabled
if (!igny8_is_connection_enabled()) {
return new WP_Error(
'rest_forbidden',
__('IGNY8 connection is disabled', 'igny8-bridge'),
array('status' => 403)
);
}
$content_id = intval($request['content_id']);
// Find post by content_id meta
$posts = get_posts(array(
'meta_key' => '_igny8_content_id',
'meta_value' => $content_id,
'post_type' => 'any',
'posts_per_page' => 1,
'post_status' => 'any',
'fields' => 'ids' // Only get IDs for performance
));
if (empty($posts)) {
return rest_ensure_response(array(
'success' => false,
'message' => 'Post not found',
'content_id' => $content_id
));
}
$post_id = $posts[0];
$post = get_post($post_id);
return rest_ensure_response(array(
'success' => true,
'data' => array(
'post_id' => $post_id,
'wordpress_status' => $post->post_status,
'igny8_status' => igny8_map_wp_status_to_igny8($post->post_status),
'status_mapping' => array(
'publish' => 'completed',
'draft' => 'draft',
'pending' => 'pending',
'private' => 'completed',
'trash' => 'archived',
'future' => 'scheduled'
),
'content_id' => $content_id,
'url' => get_permalink($post_id),
'last_synced' => get_post_meta($post_id, '_igny8_last_synced', true)
)
));
}
/**
* Helper: generate a request_id (UUIDv4 if available)
*
* @return string
*/
private function generate_request_id() {
if (function_exists('wp_generate_uuid4')) {
return wp_generate_uuid4();
}
// Fallback: uniqid with more entropy
return uniqid('', true);
}
/**
* Helper: Build unified API response and return WP_REST_Response
*
* @param bool $success
* @param mixed $data
* @param string|null $message
* @param string|null $error
* @param array|null $errors
* @param int $status
* @return WP_REST_Response
*/
private function build_unified_response($success, $data = null, $message = null, $error = null, $errors = null, $status = 200) {
$payload = array(
'success' => (bool) $success,
'data' => $data,
'message' => $message,
'request_id' => $this->generate_request_id()
);
if (!$success) {
$payload['error'] = $error ?: 'Unknown error';
if (!empty($errors)) {
$payload['errors'] = $errors;
}
}
$response = rest_ensure_response($payload);
$response->set_status($status);
return $response;
}
/**
* GET /status - Returns plugin connection status and API key info
*
* @param WP_REST_Request $request
* @return WP_REST_Response
*/
public function get_status($request) {
$api = new Igny8API();
$api_key = function_exists('igny8_get_secure_option') ? igny8_get_secure_option('igny8_api_key') : get_option('igny8_api_key');
$connection_enabled = igny8_is_connection_enabled();
$data = array(
'connected' => !empty($api_key) && $api->is_authenticated(),
'has_api_key' => !empty($api_key),
'communication_enabled' => $connection_enabled,
'plugin_version' => defined('IGNY8_BRIDGE_VERSION') ? IGNY8_BRIDGE_VERSION : '1.0.0',
'wordpress_version' => get_bloginfo('version'),
'last_health_check' => get_option('igny8_last_api_health_check', 0),
'health' => (!empty($api_key) && $connection_enabled) ? 'healthy' : 'not_configured'
);
return $this->build_unified_response(true, $data, 'Plugin status retrieved', null, null, 200);
}
/**
* GET /site-metadata/ - returns post types, taxonomies and counts in unified format
*
* @param WP_REST_Request $request
* @return WP_REST_Response
*/
public function get_site_metadata($request) {
// Use transient cache to avoid expensive counts on large sites
$cache_key = 'igny8_site_metadata_v1';
$cached = get_transient($cache_key);
if ($cached !== false) {
return $this->build_unified_response(true, $cached, 'Site metadata (cached)', null, null, 200);
}
// Perform permission check and return unified error if not allowed
$perm = $this->check_permission($request);
if (is_wp_error($perm)) {
$status = 403;
$error_data = $perm->get_error_data();
if (is_array($error_data) && isset($error_data['status'])) {
$status = intval($error_data['status']);
}
return $this->build_unified_response(false, null, null, $perm->get_error_message(), null, $status);
}
// Collect post types (public)
$post_types_objects = get_post_types(array('public' => true), 'objects');
$post_types = array();
foreach ($post_types_objects as $slug => $obj) {
// Get total count across statuses
$count_obj = wp_count_posts($slug);
$total = 0;
if (is_object($count_obj)) {
foreach (get_object_vars($count_obj) as $val) {
$total += intval($val);
}
}
$post_types[$slug] = array(
'label' => $obj->labels->singular_name ?? $obj->label,
'count' => $total
);
}
// Collect taxonomies (public)
$taxonomy_objects = get_taxonomies(array('public' => true), 'objects');
$taxonomies = array();
foreach ($taxonomy_objects as $slug => $obj) {
// Use wp_count_terms when available
$term_count = 0;
if (function_exists('wp_count_terms')) {
$term_count = intval(wp_count_terms($slug));
} else {
$terms = get_terms(array('taxonomy' => $slug, 'hide_empty' => false, 'fields' => 'ids'));
$term_count = is_array($terms) ? count($terms) : 0;
}
$taxonomies[$slug] = array(
'label' => $obj->labels->name ?? $obj->label,
'count' => $term_count
);
}
$data = array(
'post_types' => $post_types,
'taxonomies' => $taxonomies,
'generated_at' => time(),
'plugin_connection_enabled' => (bool) igny8_is_connection_enabled(),
'two_way_sync_enabled' => (bool) get_option('igny8_enable_two_way_sync', 1)
);
// Cache for 5 minutes
set_transient($cache_key, $data, 300);
return $this->build_unified_response(true, $data, 'Site metadata retrieved', null, null, 200);
}
}
// Initialize REST API
new Igny8RestAPI();

View File

@@ -0,0 +1,118 @@
<?php
/**
* Site Integration Class
*
* Manages site data collection and semantic mapping
* Follows WORDPRESS-PLUGIN-INTEGRATION.md guidelines
*
* @package Igny8Bridge
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
/**
* Igny8SiteIntegration Class
*/
class Igny8SiteIntegration {
/**
* API instance
*
* @var Igny8API
*/
private $api;
/**
* Site ID
*
* @var int
*/
private $site_id;
/**
* Constructor
*
* @param int $site_id IGNY8 site ID
*/
public function __construct($site_id) {
$this->api = new Igny8API();
$this->site_id = $site_id;
}
/**
* Full site scan and semantic mapping
*
* @return array Result array
*/
public function full_site_scan() {
// Collect all data
$site_data = igny8_collect_site_data();
// Send to IGNY8
$response = $this->api->post("/system/sites/{$this->site_id}/import/", array(
'site_data' => $site_data,
'import_type' => 'full_scan'
));
if ($response['success']) {
// Map to semantic strategy
$mapping = igny8_map_site_to_semantic_strategy($this->site_id, $site_data);
return array(
'success' => true,
'import_id' => $response['data']['import_id'] ?? null,
'semantic_map' => $mapping['data'] ?? null,
'summary' => array(
'posts' => count($site_data['posts']),
'taxonomies' => count($site_data['taxonomies']),
'products' => count($site_data['products'] ?? array()),
'product_attributes' => count($site_data['product_attributes'] ?? array())
)
);
}
return array('success' => false, 'error' => $response['error'] ?? 'Unknown error');
}
/**
* Get semantic strategy recommendations
*
* @return array|false Recommendations or false on failure
*/
public function get_recommendations() {
$response = $this->api->get("/planner/sites/{$this->site_id}/recommendations/");
if ($response['success']) {
return $response['data'];
}
return false;
}
/**
* Apply restructuring recommendations
*
* @param array $recommendations Recommendations array
* @return bool True on success
*/
public function apply_restructuring($recommendations) {
$response = $this->api->post("/planner/sites/{$this->site_id}/restructure/", array(
'recommendations' => $recommendations
));
return $response['success'];
}
/**
* Sync incremental site data
*
* @return array|false Sync result or false on failure
*/
public function sync_incremental() {
return igny8_sync_incremental_site_data($this->site_id);
}
}

View File

@@ -0,0 +1,147 @@
<?php
/**
* Webhook Activity Logs
*
* Logs webhook activity for auditing and debugging
*
* @package Igny8Bridge
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
/**
* Log webhook activity
*
* @param array $data Log data
* @return string|false Log ID or false on failure
*/
function igny8_log_webhook_activity($data) {
$logs = get_option('igny8_webhook_logs', array());
$log_entry = array(
'id' => uniqid('webhook_', true),
'event' => sanitize_text_field($data['event'] ?? 'unknown'),
'data' => $data['data'] ?? null,
'ip' => sanitize_text_field($data['ip'] ?? ''),
'user_agent' => sanitize_text_field($data['user_agent'] ?? ''),
'status' => sanitize_text_field($data['status'] ?? 'received'),
'response' => $data['response'] ?? null,
'error' => sanitize_text_field($data['error'] ?? ''),
'received_at' => current_time('mysql'),
'processed_at' => $data['processed_at'] ?? null
);
$logs[] = $log_entry;
// Keep only last 500 logs
if (count($logs) > 500) {
$logs = array_slice($logs, -500);
}
update_option('igny8_webhook_logs', $logs);
return $log_entry['id'];
}
/**
* Update webhook log entry
*
* @param string $log_id Log ID
* @param array $updates Updates to apply
* @return bool Success
*/
function igny8_update_webhook_log($log_id, $updates) {
$logs = get_option('igny8_webhook_logs', array());
foreach ($logs as $key => $log) {
if ($log['id'] === $log_id) {
foreach ($updates as $field => $value) {
if ($field === 'status') {
$logs[$key][$field] = sanitize_text_field($value);
} elseif ($field === 'response') {
$logs[$key][$field] = $value;
} elseif ($field === 'processed_at') {
$logs[$key][$field] = sanitize_text_field($value);
} else {
$logs[$key][$field] = $value;
}
}
update_option('igny8_webhook_logs', $logs);
return true;
}
}
return false;
}
/**
* Get webhook logs
*
* @param array $args Query arguments
* @return array Logs
*/
function igny8_get_webhook_logs($args = array()) {
$defaults = array(
'limit' => 50,
'event' => null,
'status' => null
);
$args = wp_parse_args($args, $defaults);
$logs = get_option('igny8_webhook_logs', array());
// Reverse to get newest first
$logs = array_reverse($logs);
// Filter by event
if ($args['event']) {
$logs = array_filter($logs, function($log) use ($args) {
return $log['event'] === $args['event'];
});
}
// Filter by status
if ($args['status']) {
$logs = array_filter($logs, function($log) use ($args) {
return $log['status'] === $args['status'];
});
}
// Limit results
if ($args['limit'] > 0) {
$logs = array_slice($logs, 0, $args['limit']);
}
return array_values($logs);
}
/**
* Clear old webhook logs
*
* @param int $days_old Delete logs older than this many days
* @return int Number of logs deleted
*/
function igny8_clear_old_webhook_logs($days_old = 30) {
$logs = get_option('igny8_webhook_logs', array());
$cutoff = strtotime("-{$days_old} days");
$deleted = 0;
foreach ($logs as $key => $log) {
$log_time = strtotime($log['received_at']);
if ($log_time < $cutoff) {
unset($logs[$key]);
$deleted++;
}
}
if ($deleted > 0) {
update_option('igny8_webhook_logs', array_values($logs));
}
return $deleted;
}

View File

@@ -0,0 +1,381 @@
<?php
/**
* IGNY8 Webhooks Handler
*
* Handles incoming webhooks from IGNY8 SaaS
*
* @package Igny8Bridge
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
/**
* Igny8Webhooks Class
*/
class Igny8Webhooks {
/**
* Constructor
*/
public function __construct() {
add_action('rest_api_init', array($this, 'register_webhook_routes'));
}
/**
* Register webhook REST routes
*/
public function register_webhook_routes() {
// Main webhook endpoint
register_rest_route('igny8/v1', '/event', array(
'methods' => 'POST',
'callback' => array($this, 'handle_webhook'),
'permission_callback' => array($this, 'verify_webhook_secret'),
'args' => array(
'event' => array(
'required' => true,
'type' => 'string',
'description' => 'Event type'
),
'data' => array(
'required' => true,
'type' => 'object',
'description' => 'Event data'
)
)
));
}
/**
* Verify webhook shared secret
*
* @param WP_REST_Request $request Request object
* @return bool|WP_Error
*/
public function verify_webhook_secret($request) {
// First check if connection is enabled
if (!igny8_is_connection_enabled()) {
return new WP_Error(
'rest_forbidden',
__('IGNY8 connection is disabled', 'igny8-bridge'),
array('status' => 403)
);
}
// Get shared secret from settings
$shared_secret = igny8_get_webhook_secret();
if (empty($shared_secret)) {
return new WP_Error(
'rest_forbidden',
__('Webhook secret not configured', 'igny8-bridge'),
array('status' => 403)
);
}
// Check X-IGNY8-Signature header
$signature = $request->get_header('X-IGNY8-Signature');
if (empty($signature)) {
return new WP_Error(
'rest_forbidden',
__('Missing webhook signature', 'igny8-bridge'),
array('status' => 401)
);
}
// Verify signature
$body = $request->get_body();
$expected_signature = hash_hmac('sha256', $body, $shared_secret);
if (!hash_equals($expected_signature, $signature)) {
igny8_log_webhook_activity(array(
'event' => 'authentication_failed',
'ip' => $request->get_header('X-Forwarded-For') ?: $request->get_header('Remote-Addr'),
'error' => 'Invalid signature'
));
return new WP_Error(
'rest_forbidden',
__('Invalid webhook signature', 'igny8-bridge'),
array('status' => 401)
);
}
return true;
}
/**
* Handle incoming webhook
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response|WP_Error
*/
public function handle_webhook($request) {
// Double-check connection is enabled
if (!igny8_is_connection_enabled()) {
return new WP_Error(
'rest_forbidden',
__('IGNY8 connection is disabled', 'igny8-bridge'),
array('status' => 403)
);
}
$event = $request->get_param('event');
$data = $request->get_param('data');
if (empty($event) || empty($data)) {
return new WP_Error(
'rest_invalid_param',
__('Missing event or data parameter', 'igny8-bridge'),
array('status' => 400)
);
}
// Log webhook receipt
$log_id = igny8_log_webhook_activity(array(
'event' => $event,
'data' => $data,
'ip' => $request->get_header('X-Forwarded-For') ?: $request->get_header('Remote-Addr'),
'user_agent' => $request->get_header('User-Agent'),
'status' => 'received'
));
// Route to appropriate handler
$result = null;
switch ($event) {
case 'task_published':
case 'task_completed':
$result = $this->handle_task_published($data);
break;
case 'link_recommendation':
case 'insert_link':
$result = $this->handle_link_recommendation($data);
break;
case 'optimizer_request':
case 'optimizer_job_completed':
$result = $this->handle_optimizer_request($data);
break;
default:
$result = array(
'success' => false,
'error' => 'Unknown event type: ' . $event
);
}
// Update log with result
if ($log_id) {
igny8_update_webhook_log($log_id, array(
'status' => $result['success'] ? 'processed' : 'failed',
'response' => $result,
'processed_at' => current_time('mysql')
));
}
return rest_ensure_response($result);
}
/**
* Handle task published event
*
* @param array $data Event data
* @return array Result
*/
private function handle_task_published($data) {
if (!igny8_is_connection_enabled()) {
return array('success' => false, 'error' => 'Connection disabled');
}
if (function_exists('igny8_is_module_enabled') && !igny8_is_module_enabled('writer')) {
return array('success' => false, 'error' => 'Writer module disabled');
}
$task_id = $data['task_id'] ?? null;
if (!$task_id) {
return array('success' => false, 'error' => 'Missing task_id');
}
// Check if post already exists
$existing_posts = get_posts(array(
'meta_key' => '_igny8_task_id',
'meta_value' => $task_id,
'post_type' => 'any',
'posts_per_page' => 1
));
if (!empty($existing_posts)) {
// Post already exists, just update status if needed
$post_id = $existing_posts[0]->ID;
$status = $data['status'] ?? 'publish';
if ($status === 'publish' || $status === 'completed') {
wp_update_post(array(
'ID' => $post_id,
'post_status' => 'publish'
));
}
return array(
'success' => true,
'message' => 'Post updated',
'post_id' => $post_id
);
}
// Fetch full task data and create post
$api = new Igny8API();
$task_response = $api->get("/writer/tasks/{$task_id}/");
if (!$task_response['success']) {
return array(
'success' => false,
'error' => 'Failed to fetch task: ' . ($task_response['error'] ?? 'Unknown error')
);
}
$task = $task_response['data'];
$enabled_post_types = igny8_get_enabled_post_types();
$content_data = array(
'task_id' => $task['id'],
'title' => $task['title'] ?? 'Untitled',
'content' => $task['content'] ?? '',
'status' => $task['status'] ?? 'draft',
'cluster_id' => $task['cluster_id'] ?? null,
'sector_id' => $task['sector_id'] ?? null,
'keyword_ids' => $task['keyword_ids'] ?? array(),
'content_type' => $task['content_type'] ?? 'post',
'categories' => $task['categories'] ?? array(),
'tags' => $task['tags'] ?? array(),
'featured_image' => $task['featured_image'] ?? null,
'gallery_images' => $task['gallery_images'] ?? array(),
'meta_title' => $task['meta_title'] ?? null,
'meta_description' => $task['meta_description'] ?? null
);
$post_id = igny8_create_wordpress_post_from_task($content_data, $enabled_post_types);
if (is_wp_error($post_id)) {
return array(
'success' => false,
'error' => $post_id->get_error_message()
);
}
return array(
'success' => true,
'message' => 'Post created',
'post_id' => $post_id
);
}
/**
* Handle link recommendation event
*
* @param array $data Event data
* @return array Result
*/
private function handle_link_recommendation($data) {
if (!igny8_is_connection_enabled()) {
return array('success' => false, 'error' => 'Connection disabled');
}
if (function_exists('igny8_is_module_enabled') && !igny8_is_module_enabled('linker')) {
return array('success' => false, 'error' => 'Linker module disabled');
}
$post_id = $data['post_id'] ?? null;
$target_url = $data['target_url'] ?? null;
$anchor = $data['anchor'] ?? $data['anchor_text'] ?? null;
if (!$post_id || !$target_url || !$anchor) {
return array(
'success' => false,
'error' => 'Missing required parameters: post_id, target_url, anchor'
);
}
// Queue link insertion
$queued = igny8_queue_link_insertion(array(
'post_id' => intval($post_id),
'target_url' => esc_url_raw($target_url),
'anchor' => sanitize_text_field($anchor),
'source' => 'igny8_linker',
'priority' => $data['priority'] ?? 'normal',
'created_at' => current_time('mysql')
));
if ($queued) {
return array(
'success' => true,
'message' => 'Link queued for insertion',
'queue_id' => $queued
);
}
return array(
'success' => false,
'error' => 'Failed to queue link insertion'
);
}
/**
* Handle optimizer request event
*
* @param array $data Event data
* @return array Result
*/
private function handle_optimizer_request($data) {
if (!igny8_is_connection_enabled()) {
return array('success' => false, 'error' => 'Connection disabled');
}
if (function_exists('igny8_is_module_enabled') && !igny8_is_module_enabled('optimizer')) {
return array('success' => false, 'error' => 'Optimizer module disabled');
}
$post_id = $data['post_id'] ?? null;
$job_id = $data['job_id'] ?? null;
$status = $data['status'] ?? null;
$score_changes = $data['score_changes'] ?? null;
$recommendations = $data['recommendations'] ?? null;
if (!$post_id) {
return array('success' => false, 'error' => 'Missing post_id');
}
// Update optimizer status if job_id provided
if ($job_id) {
update_post_meta($post_id, '_igny8_optimizer_job_id', $job_id);
}
if ($status) {
update_post_meta($post_id, '_igny8_optimizer_status', $status);
}
if ($score_changes) {
update_post_meta($post_id, '_igny8_optimizer_score_changes', $score_changes);
}
if ($recommendations) {
update_post_meta($post_id, '_igny8_optimizer_recommendations', $recommendations);
}
return array(
'success' => true,
'message' => 'Optimizer data updated',
'post_id' => $post_id
);
}
}
// Initialize webhooks
new Igny8Webhooks();

View File

@@ -0,0 +1,828 @@
<?php
/**
* Helper Functions
*
* WordPress integration functions for IGNY8 Bridge
*
* @package Igny8Bridge
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
/**
* Get encryption key for secure option storage
*
* @return string Binary key
*/
function igny8_get_encryption_key() {
$salt = wp_salt('auth');
return hash('sha256', 'igny8_bridge_' . $salt, true);
}
/**
* Encrypt a value for storage
*
* @param string $value Plain text value
* @return string Encrypted value with prefix or original value on failure
*/
function igny8_encrypt_value($value) {
if ($value === '' || $value === null) {
return '';
}
if (!function_exists('openssl_encrypt')) {
return $value;
}
$iv = openssl_random_pseudo_bytes(16);
$cipher = openssl_encrypt($value, 'AES-256-CBC', igny8_get_encryption_key(), OPENSSL_RAW_DATA, $iv);
if ($cipher === false) {
return $value;
}
return 'igny8|' . base64_encode($iv . $cipher);
}
/**
* Decrypt a stored value
*
* @param string $value Stored value
* @return string Decrypted value or original on failure
*/
function igny8_decrypt_value($value) {
if (!is_string($value) || strpos($value, 'igny8|') !== 0) {
return $value;
}
if (!function_exists('openssl_decrypt')) {
return $value;
}
$encoded = substr($value, 6);
$data = base64_decode($encoded, true);
if ($data === false || strlen($data) <= 16) {
return $value;
}
$iv = substr($data, 0, 16);
$cipher = substr($data, 16);
$plain = openssl_decrypt($cipher, 'AES-256-CBC', igny8_get_encryption_key(), OPENSSL_RAW_DATA, $iv);
return ($plain === false) ? $value : $plain;
}
/**
* Store an option securely
*
* @param string $option Option name
* @param string $value Value to store
*/
function igny8_store_secure_option($option, $value) {
if ($value === null || $value === '') {
delete_option($option);
return;
}
update_option($option, igny8_encrypt_value($value));
}
/**
* Retrieve secure option (with legacy fallback)
*
* @param string $option Option name
* @return string Value
*/
function igny8_get_secure_option($option) {
$stored = get_option($option);
if (!$stored) {
return '';
}
$value = igny8_decrypt_value($stored);
return is_string($value) ? $value : '';
}
/**
* Get supported post types for automation
*
* @return array Key => label
*/
function igny8_get_supported_post_types() {
$types = array(
'post' => __('Posts', 'igny8-bridge'),
'page' => __('Pages', 'igny8-bridge'),
);
if (post_type_exists('product')) {
$types['product'] = __('Products', 'igny8-bridge');
}
/**
* Filter the list of selectable post types.
*
* @param array $types
*/
return apply_filters('igny8_supported_post_types', $types);
}
/**
* Get enabled post types
*
* @return array
*/
function igny8_get_enabled_post_types() {
$saved = get_option('igny8_enabled_post_types');
if (is_array($saved) && !empty($saved)) {
return $saved;
}
return array_keys(igny8_get_supported_post_types());
}
/**
* Get configured control mode
*
* @return string mirror|hybrid
*/
function igny8_get_control_mode() {
$mode = get_option('igny8_control_mode', 'mirror');
return in_array($mode, array('mirror', 'hybrid'), true) ? $mode : 'mirror';
}
/**
* Get supported taxonomies for syncing
*
* @return array Key => label
*/
function igny8_get_supported_taxonomies() {
$taxonomies = array();
// Standard WordPress taxonomies
if (taxonomy_exists('category')) {
$taxonomies['category'] = __('Categories', 'igny8-bridge');
}
if (taxonomy_exists('post_tag')) {
$taxonomies['post_tag'] = __('Tags', 'igny8-bridge');
}
// WooCommerce taxonomies
if (taxonomy_exists('product_cat')) {
$taxonomies['product_cat'] = __('Product Categories', 'igny8-bridge');
}
if (taxonomy_exists('product_tag')) {
$taxonomies['product_tag'] = __('Product Tags', 'igny8-bridge');
}
if (taxonomy_exists('product_shipping_class')) {
$taxonomies['product_shipping_class'] = __('Product Shipping Classes', 'igny8-bridge');
}
// IGNY8 taxonomies (always include)
if (taxonomy_exists('igny8_sectors')) {
$taxonomies['igny8_sectors'] = __('IGNY8 Sectors', 'igny8-bridge');
}
if (taxonomy_exists('igny8_clusters')) {
$taxonomies['igny8_clusters'] = __('IGNY8 Clusters', 'igny8-bridge');
}
// Get custom taxonomies (public only)
$custom_taxonomies = get_taxonomies(array(
'public' => true,
'_builtin' => false
), 'objects');
foreach ($custom_taxonomies as $taxonomy) {
// Skip if already added above
if (isset($taxonomies[$taxonomy->name])) {
continue;
}
// Skip post formats and other system taxonomies
if (in_array($taxonomy->name, array('post_format', 'wp_theme', 'wp_template_part_area'), true)) {
continue;
}
$taxonomies[$taxonomy->name] = $taxonomy->label;
}
/**
* Filter the list of selectable taxonomies.
*
* @param array $taxonomies
*/
return apply_filters('igny8_supported_taxonomies', $taxonomies);
}
/**
* Get enabled taxonomies for syncing
*
* @return array
*/
function igny8_get_enabled_taxonomies() {
$saved = get_option('igny8_enabled_taxonomies');
if (is_array($saved) && !empty($saved)) {
return $saved;
}
// Default: enable common taxonomies
return array('category', 'post_tag', 'product_cat', 'igny8_sectors', 'igny8_clusters');
}
/**
* Check if a taxonomy is enabled for syncing
*
* @param string $taxonomy Taxonomy key
* @return bool
*/
function igny8_is_taxonomy_enabled($taxonomy) {
$taxonomies = igny8_get_enabled_taxonomies();
return in_array($taxonomy, $taxonomies, true);
}
/**
* Get available automation modules
*
* @return array Key => label
*/
function igny8_get_available_modules() {
$modules = array(
'sites' => __('Sites (Data & Semantic Map)', 'igny8-bridge'),
'planner' => __('Planner (Keywords & Briefs)', 'igny8-bridge'),
'writer' => __('Writer (Tasks & Posts)', 'igny8-bridge'),
'linker' => __('Linker (Internal Links)', 'igny8-bridge'),
'optimizer' => __('Optimizer (Audits & Scores)', 'igny8-bridge'),
);
/**
* Filter the list of IGNY8 modules that can be toggled.
*
* @param array $modules
*/
return apply_filters('igny8_available_modules', $modules);
}
/**
* Get enabled modules
*
* @return array
*/
function igny8_get_enabled_modules() {
$saved = get_option('igny8_enabled_modules');
if (is_array($saved) && !empty($saved)) {
return $saved;
}
return array_keys(igny8_get_available_modules());
}
/**
* Check if a module is enabled
*
* @param string $module Module key
* @return bool
*/
function igny8_is_module_enabled($module) {
$modules = igny8_get_enabled_modules();
return in_array($module, $modules, true);
}
/**
* Check if a post type is enabled for automation
*
* @param string $post_type Post type key
* @return bool
*/
function igny8_is_post_type_enabled($post_type) {
$post_types = igny8_get_enabled_post_types();
return in_array($post_type, $post_types, true);
}
/**
* Check if IGNY8 connection is enabled
* This is a master switch that disables all sync operations while preserving credentials
*
* @return bool True if connection is enabled
*/
if (!function_exists('igny8_log_error')) {
function igny8_log_error($message) {
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log('[IGNY8 Plugin] ' . $message);
}
}
}
if (!function_exists('igny8_is_connection_enabled')) {
function igny8_is_connection_enabled() {
// Master toggle (defaults to true)
$enabled = (bool) get_option('igny8_connection_enabled', 1);
if (!$enabled) {
return false;
}
// Prefer secure option helpers when available
if (function_exists('igny8_get_secure_option')) {
$api_key = igny8_get_secure_option('igny8_api_key');
} else {
$api_key = get_option('igny8_api_key');
}
$site_id = get_option('igny8_site_id');
if (empty($api_key) || empty($site_id)) {
igny8_log_error('Failed to connect to IGNY8 API: API key or Site ID not configured.');
return false;
}
return true;
}
}
/**
* Get webhook shared secret
*
* @return string Webhook secret
*/
function igny8_get_webhook_secret() {
$secret = get_option('igny8_webhook_secret');
if (empty($secret)) {
// Generate secret if not exists
$secret = wp_generate_password(64, false);
update_option('igny8_webhook_secret', $secret);
}
return $secret;
}
/**
* Regenerate webhook secret
*
* @return string New secret
*/
function igny8_regenerate_webhook_secret() {
$secret = wp_generate_password(64, false);
update_option('igny8_webhook_secret', $secret);
return $secret;
}
/**
* Get configuration for site scans
*
* @param array $overrides Override defaults
* @return array
*/
function igny8_get_site_scan_settings($overrides = array()) {
$defaults = array(
'post_types' => igny8_get_enabled_post_types(),
'include_products' => (bool) get_option('igny8_enable_woocommerce', class_exists('WooCommerce') ? 1 : 0),
'per_page' => 100,
'since' => null,
'mode' => 'full',
);
$settings = wp_parse_args($overrides, $defaults);
return apply_filters('igny8_site_scan_settings', $settings);
}
/**
* Register IGNY8 post meta fields
*/
function igny8_register_post_meta() {
$post_types = array('post', 'page', 'product');
// Define all meta fields with proper schema for REST API
$meta_fields = array(
'_igny8_taxonomy_id' => array(
'type' => 'integer',
'description' => 'IGNY8 taxonomy ID linked to this post',
'single' => true,
'show_in_rest' => true,
),
'_igny8_attribute_id' => array(
'type' => 'integer',
'description' => 'IGNY8 attribute ID linked to this post',
'single' => true,
'show_in_rest' => true,
),
'_igny8_last_synced' => array(
'type' => 'string',
'description' => 'Last sync timestamp',
'single' => true,
'show_in_rest' => true,
)
);
// Register each meta field for all relevant post types
foreach ($meta_fields as $meta_key => $config) {
foreach ($post_types as $post_type) {
register_post_meta($post_type, $meta_key, $config);
}
}
}
/**
* Register IGNY8 taxonomies
*/
function igny8_register_taxonomies() {
// Register sectors taxonomy (hierarchical) - only if not exists
if (!taxonomy_exists('igny8_sectors')) {
register_taxonomy('igny8_sectors', array('post', 'page', 'product'), array(
'hierarchical' => true,
'labels' => array(
'name' => 'IGNY8 Sectors',
'singular_name' => 'Sector',
'menu_name' => 'Sectors',
'all_items' => 'All Sectors',
'edit_item' => 'Edit Sector',
'view_item' => 'View Sector',
'update_item' => 'Update Sector',
'add_new_item' => 'Add New Sector',
'new_item_name' => 'New Sector Name',
'parent_item' => 'Parent Sector',
'parent_item_colon' => 'Parent Sector:',
'search_items' => 'Search Sectors',
'not_found' => 'No sectors found',
),
'public' => true,
'show_ui' => true,
'show_admin_column' => false,
'show_in_nav_menus' => true,
'show_tagcloud' => false,
'show_in_rest' => true,
'rewrite' => array(
'slug' => 'sectors',
'with_front' => false,
),
'capabilities' => array(
'manage_terms' => 'manage_categories',
'edit_terms' => 'manage_categories',
'delete_terms' => 'manage_categories',
'assign_terms' => 'edit_posts',
),
));
}
// Register clusters taxonomy (hierarchical) - only if not exists
if (!taxonomy_exists('igny8_clusters')) {
register_taxonomy('igny8_clusters', array('post', 'page', 'product'), array(
'hierarchical' => true,
'labels' => array(
'name' => 'IGNY8 Clusters',
'singular_name' => 'Cluster',
'menu_name' => 'Clusters',
'all_items' => 'All Clusters',
'edit_item' => 'Edit Cluster',
'view_item' => 'View Cluster',
'update_item' => 'Update Cluster',
'add_new_item' => 'Add New Cluster',
'new_item_name' => 'New Cluster Name',
'parent_item' => 'Parent Cluster',
'parent_item_colon' => 'Parent Cluster:',
'search_items' => 'Search Clusters',
'not_found' => 'No clusters found',
),
'public' => true,
'show_ui' => true,
'show_admin_column' => false,
'show_in_nav_menus' => true,
'show_tagcloud' => false,
'show_in_rest' => true,
'rewrite' => array(
'slug' => 'clusters',
'with_front' => false,
),
'capabilities' => array(
'manage_terms' => 'manage_categories',
'edit_terms' => 'manage_categories',
'delete_terms' => 'manage_categories',
'assign_terms' => 'edit_posts',
),
));
}
}
/**
* Map WordPress post status to IGNY8 task status
*
* @param string $wp_status WordPress post status
* @return string IGNY8 task status
*/
function igny8_map_wp_status_to_igny8($wp_status) {
$status_map = array(
'publish' => 'completed',
'draft' => 'draft',
'pending' => 'pending',
'private' => 'completed',
'trash' => 'archived',
'future' => 'scheduled'
);
return isset($status_map[$wp_status]) ? $status_map[$wp_status] : 'draft';
}
/**
* Check if post is managed by IGNY8
*
* @param int $post_id Post ID
* @return bool True if IGNY8 managed
*/
function igny8_is_igny8_managed_post($post_id) {
$task_id = get_post_meta($post_id, '_igny8_task_id', true);
return !empty($task_id);
}
/**
* Get post data for IGNY8 sync
*
* @param int $post_id Post ID
* @return array|false Post data or false on failure
*/
function igny8_get_post_data_for_sync($post_id) {
$post = get_post($post_id);
if (!$post) {
return false;
}
return array(
'id' => $post_id,
'title' => $post->post_title,
'status' => $post->post_status,
'url' => get_permalink($post_id),
'modified' => $post->post_modified,
'published' => $post->post_date,
'author' => get_the_author_meta('display_name', $post->post_author),
'word_count' => str_word_count(strip_tags($post->post_content)),
'meta' => array(
'task_id' => get_post_meta($post_id, '_igny8_task_id', true),
'content_id' => get_post_meta($post_id, '_igny8_content_id', true),
'cluster_id' => get_post_meta($post_id, '_igny8_cluster_id', true),
'sector_id' => get_post_meta($post_id, '_igny8_sector_id', true),
)
);
}
/**
* Schedule cron jobs
*/
function igny8_schedule_cron_jobs() {
// Schedule daily post status sync (WordPress → IGNY8)
if (!wp_next_scheduled('igny8_sync_post_statuses')) {
wp_schedule_event(time(), 'daily', 'igny8_sync_post_statuses');
}
// Schedule daily site data sync (incremental)
if (!wp_next_scheduled('igny8_sync_site_data')) {
wp_schedule_event(time(), 'daily', 'igny8_sync_site_data');
}
// Schedule periodic full site scan (runs at most once per week)
if (!wp_next_scheduled('igny8_full_site_scan')) {
wp_schedule_event(time(), 'daily', 'igny8_full_site_scan');
}
// Schedule hourly sync from IGNY8 (IGNY8 → WordPress)
if (!wp_next_scheduled('igny8_sync_from_igny8')) {
wp_schedule_event(time(), 'hourly', 'igny8_sync_from_igny8');
}
// Schedule taxonomy sync
if (!wp_next_scheduled('igny8_sync_taxonomies')) {
wp_schedule_event(time(), 'twicedaily', 'igny8_sync_taxonomies');
}
// Schedule keyword sync
if (!wp_next_scheduled('igny8_sync_keywords')) {
wp_schedule_event(time(), 'daily', 'igny8_sync_keywords');
}
// Schedule site structure sync (daily - to keep post types, taxonomies counts up to date)
if (!wp_next_scheduled('igny8_sync_site_structure')) {
wp_schedule_event(time(), 'daily', 'igny8_sync_site_structure');
}
}
/**
* Unschedule cron jobs
*/
function igny8_unschedule_cron_jobs() {
$timestamp = wp_next_scheduled('igny8_sync_post_statuses');
if ($timestamp) {
wp_unschedule_event($timestamp, 'igny8_sync_post_statuses');
}
$timestamp = wp_next_scheduled('igny8_sync_site_data');
if ($timestamp) {
wp_unschedule_event($timestamp, 'igny8_sync_site_data');
}
$timestamp = wp_next_scheduled('igny8_sync_from_igny8');
if ($timestamp) {
wp_unschedule_event($timestamp, 'igny8_sync_from_igny8');
}
$timestamp = wp_next_scheduled('igny8_full_site_scan');
if ($timestamp) {
wp_unschedule_event($timestamp, 'igny8_full_site_scan');
}
$timestamp = wp_next_scheduled('igny8_sync_taxonomies');
if ($timestamp) {
wp_unschedule_event($timestamp, 'igny8_sync_taxonomies');
}
$timestamp = wp_next_scheduled('igny8_sync_keywords');
if ($timestamp) {
wp_unschedule_event($timestamp, 'igny8_sync_keywords');
}
$timestamp = wp_next_scheduled('igny8_sync_site_structure');
if ($timestamp) {
wp_unschedule_event($timestamp, 'igny8_sync_site_structure');
}
}
/**
* Get WordPress site structure (post types and taxonomies with counts)
*
* @return array Site structure with post types and taxonomies
*/
function igny8_get_site_structure() {
$post_types_data = array();
$taxonomies_data = array();
// Get all registered post types
$post_types = get_post_types(array('public' => true), 'objects');
foreach ($post_types as $post_type) {
// Skip built-in post types we don't care about
if (in_array($post_type->name, array('attachment'), true)) {
continue;
}
$count = wp_count_posts($post_type->name);
$total = 0;
foreach ((array) $count as $status => $num) {
if ($status !== 'auto-draft') {
$total += (int) $num;
}
}
if ($total > 0 || in_array($post_type->name, array('post', 'page', 'product'), true)) {
$post_types_data[$post_type->name] = array(
'label' => $post_type->label ?: $post_type->name,
'count' => $total,
'enabled' => igny8_is_post_type_enabled($post_type->name),
'fetch_limit' => 100,
);
}
}
// Get all registered taxonomies
$taxonomies = get_taxonomies(array('public' => true), 'objects');
foreach ($taxonomies as $taxonomy) {
// Skip built-in taxonomies we don't care about
if (in_array($taxonomy->name, array('post_format'), true)) {
continue;
}
$terms = get_terms(array(
'taxonomy' => $taxonomy->name,
'hide_empty' => false,
'number' => 0,
));
$count = is_array($terms) ? count($terms) : 0;
if ($count > 0 || in_array($taxonomy->name, array('category', 'post_tag', 'product_cat'), true)) {
$taxonomies_data[$taxonomy->name] = array(
'label' => $taxonomy->label ?: $taxonomy->name,
'count' => $count,
'enabled' => true,
'fetch_limit' => 100,
);
}
}
return array(
'post_types' => $post_types_data,
'taxonomies' => $taxonomies_data,
'timestamp' => current_time('c'),
);
}
/* Duplicate function removed. See guarded implementation above. */
/**
* Sync WordPress site structure to IGNY8 backend
* Called after connection is established
*
* @return bool True on success, false on failure
*/
function igny8_sync_site_structure_to_backend() {
// Get site ID from options
$site_id = get_option('igny8_site_id');
if (!$site_id) {
error_log('IGNY8: No site ID found. Cannot sync structure.');
return false;
}
// Get the site structure
$structure = igny8_get_site_structure();
if (empty($structure['post_types']) && empty($structure['taxonomies'])) {
error_log('IGNY8: No post types or taxonomies to sync.');
return false;
}
// Create a temporary integration object to find the actual integration ID
$api = new Igny8API();
if (!$api->is_authenticated()) {
error_log('IGNY8: Not authenticated. Cannot sync structure.');
return false;
}
// Get integrations for this site
$response = $api->get('/v1/integration/integrations/?site=' . $site_id);
if (!$response['success'] || empty($response['data'])) {
error_log('IGNY8: No integrations found for site. Response: ' . json_encode($response));
return false;
}
// Get the first integration (should be WordPress integration)
$integration = null;
if (isset($response['data']['results']) && !empty($response['data']['results'])) {
$integration = $response['data']['results'][0];
} elseif (is_array($response['data']) && !empty($response['data'])) {
$integration = $response['data'][0];
}
if (!$integration || empty($integration['id'])) {
error_log('IGNY8: Could not find valid integration. Response: ' . json_encode($response));
return false;
}
// Prepare the payload
$payload = array(
'post_types' => $structure['post_types'],
'taxonomies' => $structure['taxonomies'],
'timestamp' => $structure['timestamp'],
'plugin_connection_enabled' => (bool) igny8_is_connection_enabled(),
'two_way_sync_enabled' => (bool) get_option('igny8_enable_two_way_sync', 1),
);
// Send to backend
$endpoint = '/v1/integration/integrations/' . $integration['id'] . '/update-structure/';
$update_response = $api->post($endpoint, $payload);
if ($update_response['success']) {
error_log('IGNY8: Site structure synced successfully.');
update_option('igny8_last_structure_sync', current_time('timestamp'));
return true;
} else {
error_log('IGNY8: Failed to sync site structure. Error: ' . json_encode($update_response));
return false;
}
}
if (!function_exists('igny8_handle_rate_limit')) {
function igny8_handle_rate_limit($response, $max_retries = 3) {
if (isset($response['error']) && strpos($response['error'], 'Rate limit') !== false) {
for ($attempt = 0; $attempt < $max_retries; $attempt++) {
sleep(pow(2, $attempt)); // Exponential backoff
$response = igny8_retry_request(); // Retry logic (to be implemented)
if ($response['success']) {
return $response;
}
}
igny8_log_error('Max retries exceeded for rate-limited request.');
}
return $response;
}
}
if (!function_exists('igny8_retry_request')) {
function igny8_retry_request() {
// Placeholder for retry logic
return ['success' => false, 'error' => 'Retry logic not implemented'];
}
}