Implement site structure synchronization between WordPress and IGNY8 backend
- Added a new API endpoint in the `IntegrationViewSet` to update the WordPress site structure, including post types and taxonomies. - Implemented a function to retrieve the site structure and sync it to the IGNY8 backend after establishing a connection. - Scheduled a daily cron job to keep the site structure updated. - Enhanced the WordPress plugin to trigger synchronization upon successful API connection. - Updated relevant files to support the new synchronization feature, improving integration capabilities.
This commit is contained in:
@@ -288,6 +288,9 @@ class Igny8Admin {
|
||||
__('Successfully connected to IGNY8 API and stored API key.', 'igny8-bridge'),
|
||||
'updated'
|
||||
);
|
||||
|
||||
// Sync site structure to backend (post types, taxonomies, etc.)
|
||||
igny8_sync_site_structure_to_backend();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -487,6 +487,11 @@ function igny8_schedule_cron_jobs() {
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -522,5 +527,153 @@ function igny8_unschedule_cron_jobs() {
|
||||
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'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ function igny8_register_sync_hooks() {
|
||||
add_action('igny8_sync_taxonomies', 'igny8_cron_sync_taxonomies');
|
||||
add_action('igny8_sync_keywords', 'igny8_cron_sync_keywords');
|
||||
add_action('igny8_full_site_scan', 'igny8_cron_full_site_scan');
|
||||
add_action('igny8_sync_site_structure', 'igny8_sync_site_structure_to_backend');
|
||||
}
|
||||
|
||||
// Register hooks
|
||||
|
||||
Reference in New Issue
Block a user