This commit is contained in:
alorig
2025-11-22 12:38:12 +05:00
parent 3d3ac0647e
commit 3fb86eacf1
3 changed files with 371 additions and 5 deletions

View File

@@ -0,0 +1,318 @@
# Site ID and Sync Settings Fix
## Issues Addressed
### 1. ❌ "Site ID not set" Error in WordPress Plugin
**Problem:** Plugin showed "X Site ID not set" error preventing sync operations
**Root Cause:**
- Plugin was taking the first site from `/system/sites/` API response
- User has multiple sites (Home & Garden, Salman Sadiq, etc.)
- Wrong site was being selected
**Solution:**
- Match site by domain name instead of taking first result
- Added fallback to first site if no domain match
- Added manual Site ID input field if auto-detection fails
### 2. ✅ Taxonomy Selection Impact Verified
**Question:** Does the "Taxonomies to Sync" checkbox selection have actual impact?
**Answer:** **YES!** The taxonomy selection is fully functional:
**Code Reference: `data/site-collection.php` (lines 318-325)**
```php
// Get enabled taxonomies from settings
if (function_exists('igny8_get_enabled_taxonomies')) {
$enabled_taxonomies = igny8_get_enabled_taxonomies();
if (!empty($enabled_taxonomies)) {
$tracked_taxonomies = $enabled_taxonomies;
}
}
foreach ($tracked_taxonomies as $taxonomy) {
if (!taxonomy_exists($taxonomy)) {
continue;
}
// Only syncs enabled taxonomies
}
```
**Only checked taxonomies will be collected and synced**
**When Selection Matters:**
- ✅ "Collect & Send Site Data" button - respects selections
- ✅ Cron jobs (automatic syncs) - respects selections
- ✅ Manual syncs from WP Admin - respects selections
### 3. ✅ Post Type Selection Impact Verified
**Question:** Does the "Post Types to Sync" checkbox selection have actual impact?
**Answer:** **YES!** The post type selection is fully functional:
**Code Reference: `data/site-collection.php` (lines 302-315)**
```php
foreach ((array) $settings['post_types'] as $post_type) {
if (!post_type_exists($post_type) || !igny8_is_post_type_enabled($post_type)) {
continue; // Skips disabled post types
}
$posts = igny8_fetch_wordpress_posts($post_type, ...);
// Only fetches enabled post types
}
```
**Only checked post types will be collected and synced**
**When Selection Matters:**
- ✅ "Collect & Send Site Data" button - respects selections
- ✅ Cron jobs (automatic syncs) - respects selections
- ✅ Manual syncs from WP Admin - respects selections
### 4. ✅ App "Sync Now" Button Impact Verified
**Question:** Does the "Sync Now" button on Content Types page respect plugin settings?
**Answer:** **PARTIALLY** - It respects integration config, but uses backend-stored settings:
**How It Works:**
1. Frontend calls: `/v1/integration/integrations/{id}/sync/`
2. Backend reads integration's `config_json.content_types`
3. Syncs based on what's **enabled in the integration record**
**Important Notes:**
- ✅ The "Sync Now" button syncs FROM WordPress TO IGNY8
- ✅ It uses the settings stored in the integration config (backend database)
- ⚠️ If you change plugin settings, you need to send site structure first:
- Click "Collect & Send Site Data" in plugin
- This updates the integration config in the backend
- Then "Sync Now" will use the new settings
**Workflow:**
```
Plugin Settings Changed
Click "Collect & Send Site Data" (updates backend config)
Click "Sync Now" in App (uses updated config)
Syncs with new settings
```
---
## Changes Made
### File 1: `admin/class-admin.php`
**Change 1: Site ID Detection by Domain**
```php
// Before (line 278-283)
$site_response = $api->get('/system/sites/');
if ($site_response['success'] && !empty($site_response['results'])) {
$site = $site_response['results'][0]; // Takes first
update_option('igny8_site_id', $site['id']);
}
// After (line 278-299)
$site_response = $api->get('/system/sites/');
if ($site_response['success'] && !empty($site_response['results'])) {
$current_site_url = get_site_url();
$current_domain = parse_url($current_site_url, PHP_URL_HOST);
// Try to find matching site by domain
$matched_site = null;
foreach ($site_response['results'] as $site) {
if (!empty($site['domain'])) {
$site_domain = parse_url($site['domain'], PHP_URL_HOST);
if ($site_domain === $current_domain) {
$matched_site = $site;
break;
}
}
}
// Use matched site or fallback to first
if ($matched_site) {
update_option('igny8_site_id', $matched_site['id']);
error_log('IGNY8: Matched site by domain: ' . $matched_site['name']);
} else {
$site = $site_response['results'][0];
update_option('igny8_site_id', $site['id']);
error_log('IGNY8: No domain match, using first site');
}
}
```
**Change 2: Allow Manual Site ID (line 63-68)**
```php
// Added sanitization for manual site ID entry
register_setting('igny8_settings', 'igny8_site_id', array(
'type' => 'integer',
'sanitize_callback' => 'absint'
));
```
### File 2: `admin/settings.php`
**Change: Manual Site ID Input Field (line 210-232)**
```php
<?php if ($site_id) : ?>
<tr>
<th scope="row"><?php _e('Site ID', 'igny8-bridge'); ?></th>
<td>
<?php echo esc_html($site_id); ?>
<p class="description">
<?php _e('Auto-detected from your IGNY8 account.', 'igny8-bridge'); ?>
</p>
</td>
</tr>
<?php else : ?>
<tr>
<th scope="row">
<label for="igny8_site_id_manual"><?php _e('Site ID (Manual)', 'igny8-bridge'); ?></label>
</th>
<td>
<input
type="number"
id="igny8_site_id_manual"
name="igny8_site_id"
value=""
class="regular-text"
/>
<p class="description">
<?php _e('If auto-detection failed, manually enter your IGNY8 Site ID here.', 'igny8-bridge'); ?>
</p>
</td>
</tr>
<?php endif; ?>
```
---
## How to Deploy
### Step 1: Upload Updated Files to homeg8.com
Upload these 2 files via FTP/SFTP to `wp-content/plugins/igny8-wp-plugin/`:
1.`admin/class-admin.php` (Site ID domain matching)
2.`admin/settings.php` (Manual Site ID field)
### Step 2: Reconnect the Plugin
**Option A: Automatic (Recommended)**
1. Go to **WordPress Admin → Settings → IGNY8 Bridge**
2. Click "Revoke API Key" button
3. Re-enter your email, password, and API key
4. Click "Connect to IGNY8"
5. Plugin should now detect Site ID correctly by matching "homeg8.com" domain
**Option B: Manual (If Auto-detection Still Fails)**
1. Go to IGNY8 app and find your Site ID:
- Navigate to Sites → Home & Garden Site
- Check the URL: `https://app.igny8.com/sites/5/...`
- Site ID is `5`
2. Go to WordPress Admin → Settings → IGNY8 Bridge
3. Scroll to "Connection Status" section
4. If "Site ID not set", you'll see a manual input field
5. Enter `5` (or your site ID)
6. Click "Save Connection Settings"
### Step 3: Verify Fix
1. **Check Site ID:**
- Settings → IGNY8 Bridge
- "Connection Status" section should show "Site ID: 5"
2. **Test Sync Operations:**
- Should no longer show "X Site ID not set" error
- Click "Collect & Send Site Data" - should work
- Click "Sync Posts to IGNY8" - should work
3. **Check App Frontend:**
- Go to IGNY8 App → Sites → Home & Garden → Settings → Content Types
- Click "Sync Now"
- Should see content being synced
---
## Testing Checklist
### Plugin Side (WordPress Admin)
- [ ] Site ID is correctly detected and displayed
- [ ] "Collect & Send Site Data" button works (no Site ID error)
- [ ] "Sync Posts to IGNY8" button works
- [ ] "Sync Taxonomies" button works
- [ ] Taxonomy selections are saved correctly
- [ ] Post type selections are saved correctly
- [ ] Only selected taxonomies are synced
- [ ] Only selected post types are synced
### App Side (IGNY8 Frontend)
- [ ] Site shows "Connected" status
- [ ] Content Types page shows correct counts
- [ ] "Sync Now" button triggers sync
- [ ] Synced content appears with correct counts
- [ ] Integration test connection succeeds
---
## FAQ
### Q: What if Site ID is still not detected?
**A:** Use the manual Site ID input:
1. Find your Site ID in the app URL
2. Enter it manually in the plugin settings
3. Save settings
### Q: Do I need to re-save taxonomy/post type selections?
**A:** No, existing selections are preserved. Only Site ID is affected.
### Q: Will this affect existing synced content?
**A:** No, existing content is safe. This only fixes the Site ID detection.
### Q: How do I find my Site ID?
**A:** Check the IGNY8 app URL when viewing your site:
- URL format: `https://app.igny8.com/sites/{SITE_ID}/...`
- Example: `https://app.igny8.com/sites/5/dashboard` → Site ID is `5`
### Q: Does the "Sync Now" button in the app use my plugin settings immediately?
**A:** Not immediately. You need to:
1. Change settings in plugin
2. Click "Collect & Send Site Data" to update backend
3. Then "Sync Now" will use new settings
---
## Summary
### ✅ **What's Fixed:**
1. Site ID now correctly detected by domain matching
2. Manual Site ID input available as fallback
3. Better logging for debugging
### ✅ **What's Confirmed Working:**
1. Taxonomy selection affects sync (only checked taxonomies sync)
2. Post type selection affects sync (only checked post types sync)
3. "Collect & Send Site Data" respects selections
4. "Sync Now" in app uses integration config
### ⚠️ **Important Workflow:**
```
Change Plugin Settings
Click "Collect & Send Site Data" (updates backend)
Click "Sync Now" in App (uses updated settings)
```
---
## Support
If issues persist:
1. Check WordPress debug log: `wp-content/debug.log`
2. Check for error: "IGNY8: Matched site by domain: {site_name}"
3. If no match found: "IGNY8: No domain match, using first site"
4. Use manual Site ID input if auto-detection fails

View File

@@ -63,7 +63,10 @@ class Igny8Admin {
*/
public function register_settings() {
register_setting('igny8_settings', 'igny8_email');
register_setting('igny8_settings', 'igny8_site_id');
register_setting('igny8_settings', 'igny8_site_id', array(
'type' => 'integer',
'sanitize_callback' => 'absint'
));
register_setting('igny8_settings', 'igny8_enable_two_way_sync', array(
'type' => 'boolean',
'sanitize_callback' => array($this, 'sanitize_boolean'),
@@ -275,11 +278,33 @@ class Igny8Admin {
}
}
// Try to get site ID (if available) using the authenticated client
// Try to get site ID by matching current site URL
$site_response = $api->get('/system/sites/');
if ($site_response['success'] && !empty($site_response['results'])) {
$site = $site_response['results'][0];
update_option('igny8_site_id', $site['id']);
$current_site_url = get_site_url();
$current_domain = parse_url($current_site_url, PHP_URL_HOST);
// Try to find matching site by domain
$matched_site = null;
foreach ($site_response['results'] as $site) {
if (!empty($site['domain'])) {
$site_domain = parse_url($site['domain'], PHP_URL_HOST);
if ($site_domain === $current_domain) {
$matched_site = $site;
break;
}
}
}
// Fallback to first site if no match found
if ($matched_site) {
update_option('igny8_site_id', $matched_site['id']);
error_log('IGNY8: Matched site by domain: ' . $matched_site['name'] . ' (ID: ' . $matched_site['id'] . ')');
} else {
$site = $site_response['results'][0];
update_option('igny8_site_id', $site['id']);
error_log('IGNY8: No domain match, using first site: ' . $site['name'] . ' (ID: ' . $site['id'] . ')');
}
}
add_settings_error(

View File

@@ -214,7 +214,30 @@ $webhook_logs = igny8_get_webhook_logs(array('limit' => 10));
<?php if ($site_id) : ?>
<tr>
<th scope="row"><?php _e('Site ID', 'igny8-bridge'); ?></th>
<td><?php echo esc_html($site_id); ?></td>
<td>
<?php echo esc_html($site_id); ?>
<p class="description">
<?php _e('Auto-detected from your IGNY8 account. If incorrect, reconnect or manually enter below.', 'igny8-bridge'); ?>
</p>
</td>
</tr>
<?php else : ?>
<tr>
<th scope="row">
<label for="igny8_site_id_manual"><?php _e('Site ID (Manual)', 'igny8-bridge'); ?></label>
</th>
<td>
<input
type="number"
id="igny8_site_id_manual"
name="igny8_site_id"
value=""
class="regular-text"
/>
<p class="description">
<?php _e('If auto-detection failed, manually enter your IGNY8 Site ID here. You can find it in the IGNY8 app URL: /sites/{site_id}/...', 'igny8-bridge'); ?>
</p>
</td>
</tr>
<?php endif; ?>
</table>