Files
igny8/SYNC-FIX-IMPLEMENTATION.md
IGNY8 VPS (Salman) bcee76fab7 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.
2025-11-22 03:36:35 +00:00

8.9 KiB

WordPress Plugin → Backend Sync Fix - Complete Implementation

🎯 Problem Identified

The WordPress plugin was connecting successfully to the IGNY8 SaaS backend, but sync was not working because:

  1. Plugin authenticates via API key (fixed in previous commit)
  2. Connection test passes
  3. Missing: Plugin never pushed WordPress site structure (post types, taxonomies) to backend
  4. Result: Frontend Content Types tab shows empty data

The backend's content-types endpoint was trying to read from integration.config_json['content_types'], but this was never populated because the plugin didn't push it.


🔧 Solution Implemented

1. Backend: New update-structure Endpoint

File: backend/igny8_core/modules/integration/views.py

Added new action endpoint:

POST /api/v1/integration/integrations/{id}/update-structure/

Purpose: Accept WordPress site structure from plugin and store in integration config.

Request Body:

{
    "post_types": {
        "post": {"label": "Posts", "count": 123, "enabled": true, "fetch_limit": 100},
        "page": {"label": "Pages", "count": 12, "enabled": true, "fetch_limit": 100},
        "product": {"label": "Products", "count": 456, "enabled": false, "fetch_limit": 50}
    },
    "taxonomies": {
        "category": {"label": "Categories", "count": 25, "enabled": true, "fetch_limit": 100},
        "post_tag": {"label": "Tags", "count": 102, "enabled": true, "fetch_limit": 100},
        "product_cat": {"label": "Product Categories", "count": 15, "enabled": false, "fetch_limit": 50}
    },
    "plugin_connection_enabled": true,
    "two_way_sync_enabled": true,
    "timestamp": "2025-11-22T10:00:00Z"
}

Response:

{
    "success": true,
    "data": {
        "message": "Site structure updated successfully",
        "post_types_count": 3,
        "taxonomies_count": 3,
        "last_structure_fetch": "2025-11-22T10:00:00Z"
    }
}

2. WordPress Plugin: New Site Structure Functions

File: igny8-wp-plugin/includes/functions.php

igny8_get_site_structure()

Gathers WordPress site information:

  • Counts all public post types (posts, pages, products, etc.)
  • Counts all public taxonomies (categories, tags, product categories, etc.)
  • Returns structured data with enabled/disabled status

igny8_sync_site_structure_to_backend()

Pushes WordPress site structure to backend:

  1. Validates site ID exists
  2. Gets site integrations from backend
  3. Sends structure to update-structure endpoint
  4. Logs results and updates igny8_last_structure_sync timestamp

3. WordPress Plugin: Integration Connection Flow

File: igny8-wp-plugin/admin/class-admin.php

Modified handle_connection() method:

  • After successful login and API key setup
  • Calls igny8_sync_site_structure_to_backend() to immediately push structure
  • User sees success message and structure is synced

4. WordPress Plugin: Cron Job for Periodic Syncs

File: igny8-wp-plugin/includes/functions.php & sync/hooks.php

Added new daily cron job:

  • igny8_sync_site_structure - runs daily
  • Keeps post type counts and taxonomy data up-to-date
  • Enables the frontend to always show current structure

📋 Complete Sync Flow (Now Working)

┌─ WordPress Admin (Settings Page)
│
├─ User clicks "Connect to IGNY8"
│  ├─ Email + Password + API Key validated
│  ├─ Tokens/API key stored securely
│  ├─ Site ID retrieved and stored
│  └─ ✅ Connection successful
│
├─ [NEW] `handle_connection()` calls `igny8_sync_site_structure_to_backend()`
│  ├─ Gathers: post types, taxonomies, counts, enabled status
│  ├─ Prepares payload
│  └─ POST to `/api/v1/integration/integrations/{id}/update-structure/`
│
└─ Backend receives structure
   ├─ Updates `integration.config_json['content_types']`
   ├─ Stores `last_structure_fetch` timestamp
   ├─ Stores `plugin_connection_enabled` and `two_way_sync_enabled` flags
   └─ ✅ Structure stored
   
┌─ Frontend Content Types Tab
│  └─ GET `/api/v1/integration/integrations/{id}/content-types/`
│     ├─ Reads from `integration.config_json['content_types']`
│     ├─ Counts synced content from `Content` model
│     ├─ Returns: post types, taxonomies, sync counts
│     └─ ✅ Tab shows data (no longer empty!)

🔄 Sync Operations (Now Enabled)

On Connection:

  1. Structure pushed to backend (initial)
  2. Frontend can display Content Types

Periodic (Daily):

  1. Cron job re-syncs structure
  2. Keeps counts up-to-date
  3. Reflects new post types/taxonomies
  4. Updates enabled/disabled status

On Sync:

  1. Backend receives updated structure
  2. Frontend reflects changes instantly
  3. Sync counts populate as content flows in

🚀 Testing the Fix

Step 1: Backend Readiness

cd /data/app/igny8
docker-compose restart backend  # Apply new endpoint

Step 2: WordPress Plugin

  1. Go to WordPress Admin → Settings → IGNY8 API
  2. Fill in credentials:
    • Email: Your IGNY8 account email
    • API Key: From Django admin (Sites → Generate WordPress API Keys)
    • Password: Your IGNY8 password
  3. Click "Connect to IGNY8"
    • Should show: "Successfully connected..."
    • Backend: Site structure pushed and stored

Step 3: Frontend Content Types Tab

  1. Go to Site Settings → Content Types tab
  2. Should now show:
    • Post Types (Posts, Pages, Products)
    • Taxonomies (Categories, Tags, Product Categories)
    • Counts for each
    • Sync status

📊 Data Flow Verification

Check Backend (Django Admin)

# SSH into backend container
docker exec -it igny8_backend bash

# Enter Django shell
python manage.py shell

# Check integration config
from igny8_core.business.integration.models import SiteIntegration
integration = SiteIntegration.objects.filter(platform='wordpress').first()
print(integration.config_json)
# Should show: {'content_types': {'post_types': {...}, 'taxonomies': {...}}, 'last_structure_fetch': '...'}

Check WordPress Logs

# WordPress debug log
tail -f /path/to/wordpress/wp-content/debug.log

# Should show:
# IGNY8: Site structure synced successfully.
# IGNY8 DEBUG GET: /v1/integration/integrations?site=5 (retrieval of integration ID)
# IGNY8 DEBUG POST: /v1/integration/integrations/123/update-structure/ (push structure)
# IGNY8 DEBUG RESPONSE: Status=200 (success)

🔐 Security Considerations

  • Endpoint requires authentication (API key or JWT)
  • Site-level access control maintained
  • Only active sites can push structure
  • Plugin uses secure option storage for API keys
  • No sensitive data in structure payload

📝 Files Modified

Backend:

  • backend/igny8_core/modules/integration/views.py
    • Added update_site_structure() action endpoint
    • Added timezone import

WordPress Plugin:

  • igny8-wp-plugin/admin/class-admin.php

    • Call igny8_sync_site_structure_to_backend() after connection
  • igny8-wp-plugin/includes/functions.php

    • Added igny8_get_site_structure()
    • Added igny8_sync_site_structure_to_backend()
    • Added daily cron job for periodic sync
  • igny8-wp-plugin/sync/hooks.php

    • Registered igny8_sync_site_structure cron hook

What's Now Working

  1. Plugin connects and authenticates (API key-based)
  2. Plugin pushes WordPress site structure to backend
  3. Backend stores structure in integration config
  4. Frontend Content Types tab displays post types and taxonomies
  5. Frontend shows sync counts (as content syncs in)
  6. Periodic cron job keeps structure updated
  7. Two-way sync enabled/disabled status reflected
  8. Full round-trip data flow working

🎯 Next Steps

Phase 1: Content Sync (Already Implemented)

  • Structure metadata pushed
  • Actual post/taxonomy sync using this structure
  • Update task content-types tab to use new data

Phase 2: Bidirectional Sync

  • IGNY8 → WordPress content pushback
  • Webhook handling for real-time updates
  • Conflict resolution logic

Phase 3: Advanced Features

  • Selective sync (enable/disable post types)
  • Custom fetch limits per type
  • Taxonomy hierarchy preservation
  • SEO metadata sync

📞 Support

If sync is still not working after this fix:

  1. Check backend logs: docker logs igny8_backend | grep update-structure
  2. Check plugin logs: wp-content/debug.log (if WP_DEBUG enabled)
  3. Verify integration exists: Django admin → Integration → SiteIntegration
  4. Test endpoint directly:
    curl -H "Authorization: Bearer {API_KEY}" \
      -X POST https://api.igny8.com/api/v1/integration/integrations/123/update-structure/ \
      -H "Content-Type: application/json" \
      -d '{"post_types": {...}, "taxonomies": {...}}'
    

Status: 🟢 FULLY IMPLEMENTED AND TESTED

Last Updated: November 22, 2025