wp plugin loaded
This commit is contained in:
294
igny8-wp-integration-plugin/includes/class-igny8-rest-api.php
Normal file
294
igny8-wp-integration-plugin/includes/class-igny8-rest-api.php
Normal file
@@ -0,0 +1,294 @@
|
||||
<?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'
|
||||
)
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check API permission
|
||||
*
|
||||
* @param WP_REST_Request $request Request object
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function check_permission($request) {
|
||||
// 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)
|
||||
);
|
||||
}
|
||||
|
||||
// Check if authenticated with IGNY8
|
||||
$api = new Igny8API();
|
||||
|
||||
if (!$api->is_authenticated()) {
|
||||
return new WP_Error(
|
||||
'rest_forbidden',
|
||||
__('IGNY8 API not authenticated', 'igny8-bridge'),
|
||||
array('status' => 401)
|
||||
);
|
||||
}
|
||||
|
||||
// Verify API token from request header
|
||||
$auth_header = $request->get_header('Authorization');
|
||||
|
||||
if ($auth_header) {
|
||||
$token = get_option('igny8_access_token');
|
||||
if ($token && strpos($auth_header, 'Bearer ' . $token) !== false) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Allow if IGNY8 is connected (for internal use)
|
||||
if ($api->is_authenticated()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return new WP_Error(
|
||||
'rest_forbidden',
|
||||
__('Invalid authentication', 'igny8-bridge'),
|
||||
array('status' => 403)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize REST API
|
||||
new Igny8RestAPI();
|
||||
|
||||
Reference in New Issue
Block a user