wp plugin loaded
This commit is contained in:
2
igny8-wp-integration-plugin/.gitattributes
vendored
Normal file
2
igny8-wp-integration-plugin/.gitattributes
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
# Auto detect text files and perform LF normalization
|
||||||
|
* text=auto
|
||||||
367
igny8-wp-integration-plugin/README.md
Normal file
367
igny8-wp-integration-plugin/README.md
Normal file
@@ -0,0 +1,367 @@
|
|||||||
|
# IGNY8 WordPress Bridge Plugin
|
||||||
|
|
||||||
|
**Version**: 1.0.0
|
||||||
|
**Last Updated**: 2025-10-17
|
||||||
|
**Requires**: WordPress 5.0+, PHP 7.4+
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
The IGNY8 WordPress Bridge Plugin is a **lightweight synchronization interface** that connects WordPress sites to the IGNY8 API. This plugin acts as a bridge, not a content management system, using WordPress native structures (taxonomies, post meta) to sync data bidirectionally with IGNY8.
|
||||||
|
|
||||||
|
### Key Principles
|
||||||
|
|
||||||
|
- ✅ **No Custom Database Tables** - Uses WordPress native taxonomies and post meta
|
||||||
|
- ✅ **Lightweight Bridge** - Minimal code, maximum efficiency
|
||||||
|
- ✅ **Two-Way Sync** - WordPress ↔ IGNY8 API synchronization
|
||||||
|
- ✅ **WordPress Native** - Leverages existing WordPress structures
|
||||||
|
- ✅ **API-First** - IGNY8 API is the source of truth
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
### Core Functionality
|
||||||
|
|
||||||
|
1. **API Authentication**
|
||||||
|
- Secure token management
|
||||||
|
- Automatic token refresh
|
||||||
|
- Encrypted credential storage
|
||||||
|
|
||||||
|
2. **Two-Way Synchronization**
|
||||||
|
- WordPress → IGNY8: Post status changes sync to IGNY8 tasks
|
||||||
|
- IGNY8 → WordPress: Content published from IGNY8 creates WordPress posts
|
||||||
|
|
||||||
|
3. **Taxonomy Mapping**
|
||||||
|
- WordPress taxonomies → IGNY8 Sectors/Clusters
|
||||||
|
- Hierarchical taxonomies map to IGNY8 Sectors
|
||||||
|
- Taxonomy terms map to IGNY8 Clusters
|
||||||
|
|
||||||
|
4. **Post Meta Integration**
|
||||||
|
- `_igny8_task_id` - Links WordPress posts to IGNY8 tasks
|
||||||
|
- `_igny8_cluster_id` - Links posts to IGNY8 clusters
|
||||||
|
- `_igny8_sector_id` - Links posts to IGNY8 sectors
|
||||||
|
- `_igny8_keyword_ids` - Links posts to IGNY8 keywords
|
||||||
|
|
||||||
|
5. **Site Data Collection**
|
||||||
|
- Automatic collection of WordPress posts, taxonomies, products
|
||||||
|
- Semantic mapping to IGNY8 structure
|
||||||
|
- WooCommerce integration support
|
||||||
|
|
||||||
|
6. **Status Mapping**
|
||||||
|
- WordPress post status → IGNY8 task status
|
||||||
|
- Automatic sync on post save/publish/status change
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
### Requirements
|
||||||
|
|
||||||
|
- WordPress 5.0 or higher
|
||||||
|
- PHP 7.4 or higher
|
||||||
|
- WordPress REST API enabled
|
||||||
|
- IGNY8 API account credentials
|
||||||
|
|
||||||
|
### Installation Steps
|
||||||
|
|
||||||
|
1. **Download/Clone Plugin**
|
||||||
|
```bash
|
||||||
|
git clone [repository-url]
|
||||||
|
cd igny8-ai-os
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Install in WordPress**
|
||||||
|
- Copy the `igny8-ai-os` folder to `/wp-content/plugins/`
|
||||||
|
- Or create a symlink for development
|
||||||
|
|
||||||
|
3. **Activate Plugin**
|
||||||
|
- Go to WordPress Admin → Plugins
|
||||||
|
- Activate "IGNY8 WordPress Bridge"
|
||||||
|
|
||||||
|
4. **Configure API Connection**
|
||||||
|
- Go to Settings → IGNY8 API
|
||||||
|
- Enter your IGNY8 email and password
|
||||||
|
- Click "Connect to IGNY8"
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
### API Settings
|
||||||
|
|
||||||
|
Navigate to **Settings → IGNY8 API** to configure:
|
||||||
|
|
||||||
|
- **Email**: Your IGNY8 account email
|
||||||
|
- **Password**: Your IGNY8 account password
|
||||||
|
- **Site ID**: Your IGNY8 site ID (auto-detected after connection)
|
||||||
|
|
||||||
|
### WordPress Integration
|
||||||
|
|
||||||
|
The plugin automatically:
|
||||||
|
|
||||||
|
1. **Registers Taxonomies** (if needed):
|
||||||
|
- `sectors` - Maps to IGNY8 Sectors
|
||||||
|
- `clusters` - Maps to IGNY8 Clusters
|
||||||
|
|
||||||
|
2. **Registers Post Meta Fields**:
|
||||||
|
- `_igny8_task_id`
|
||||||
|
- `_igny8_cluster_id`
|
||||||
|
- `_igny8_sector_id`
|
||||||
|
- `_igny8_keyword_ids`
|
||||||
|
- `_igny8_content_id`
|
||||||
|
|
||||||
|
3. **Sets Up WordPress Hooks**:
|
||||||
|
- `save_post` - Syncs post changes to IGNY8
|
||||||
|
- `publish_post` - Updates keywords on publish
|
||||||
|
- `transition_post_status` - Handles status changes
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### Basic Workflow
|
||||||
|
|
||||||
|
#### 1. Connect to IGNY8 API
|
||||||
|
|
||||||
|
```php
|
||||||
|
// Automatically handled via Settings page
|
||||||
|
// Or programmatically:
|
||||||
|
$api = new Igny8API();
|
||||||
|
$api->login('your@email.com', 'password');
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2. Sync WordPress Site Data
|
||||||
|
|
||||||
|
```php
|
||||||
|
// Collect and send site data to IGNY8
|
||||||
|
$site_id = get_option('igny8_site_id');
|
||||||
|
igny8_send_site_data_to_igny8($site_id);
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 3. WordPress → IGNY8 Sync
|
||||||
|
|
||||||
|
When you save/publish a WordPress post:
|
||||||
|
|
||||||
|
1. Plugin checks for `_igny8_task_id` in post meta
|
||||||
|
2. If found, syncs post status to IGNY8 task
|
||||||
|
3. If published, updates related keywords to 'mapped' status
|
||||||
|
|
||||||
|
#### 4. IGNY8 → WordPress Sync
|
||||||
|
|
||||||
|
When content is published from IGNY8:
|
||||||
|
|
||||||
|
1. IGNY8 triggers webhook (or scheduled sync)
|
||||||
|
2. Plugin creates WordPress post via `wp_insert_post()`
|
||||||
|
3. Post meta saved with `_igny8_task_id`
|
||||||
|
4. IGNY8 task updated with WordPress post ID
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## WordPress Structures Used
|
||||||
|
|
||||||
|
### Taxonomies
|
||||||
|
|
||||||
|
- **`sectors`** (hierarchical)
|
||||||
|
- Maps to IGNY8 Sectors
|
||||||
|
- Can be created manually or synced from IGNY8
|
||||||
|
|
||||||
|
- **`clusters`** (hierarchical)
|
||||||
|
- Maps to IGNY8 Clusters
|
||||||
|
- Can be created manually or synced from IGNY8
|
||||||
|
|
||||||
|
- **Native Taxonomies**
|
||||||
|
- `category` - Can map to IGNY8 Sectors
|
||||||
|
- `post_tag` - Can be used for keyword extraction
|
||||||
|
|
||||||
|
### Post Meta Fields
|
||||||
|
|
||||||
|
All stored in WordPress `wp_postmeta` table:
|
||||||
|
|
||||||
|
- `_igny8_task_id` (integer) - IGNY8 task ID
|
||||||
|
- `_igny8_cluster_id` (integer) - IGNY8 cluster ID
|
||||||
|
- `_igny8_sector_id` (integer) - IGNY8 sector ID
|
||||||
|
- `_igny8_keyword_ids` (array) - Array of IGNY8 keyword IDs
|
||||||
|
- `_igny8_content_id` (integer) - IGNY8 content ID
|
||||||
|
- `_igny8_last_synced` (datetime) - Last sync timestamp
|
||||||
|
|
||||||
|
### Post Status Mapping
|
||||||
|
|
||||||
|
| WordPress Status | IGNY8 Task Status |
|
||||||
|
|------------------|-------------------|
|
||||||
|
| `publish` | `completed` |
|
||||||
|
| `draft` | `draft` |
|
||||||
|
| `pending` | `pending` |
|
||||||
|
| `private` | `completed` |
|
||||||
|
| `trash` | `archived` |
|
||||||
|
| `future` | `scheduled` |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## API Reference
|
||||||
|
|
||||||
|
### Main Classes
|
||||||
|
|
||||||
|
#### `Igny8API`
|
||||||
|
|
||||||
|
Main API client class for all IGNY8 API interactions.
|
||||||
|
|
||||||
|
```php
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
// Login
|
||||||
|
$api->login('email@example.com', 'password');
|
||||||
|
|
||||||
|
// Get keywords
|
||||||
|
$response = $api->get('/planner/keywords/');
|
||||||
|
|
||||||
|
// Create task
|
||||||
|
$response = $api->post('/writer/tasks/', $data);
|
||||||
|
|
||||||
|
// Update task
|
||||||
|
$response = $api->put('/writer/tasks/123/', $data);
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `Igny8WordPressSync`
|
||||||
|
|
||||||
|
Handles two-way synchronization between WordPress and IGNY8.
|
||||||
|
|
||||||
|
```php
|
||||||
|
$sync = new Igny8WordPressSync();
|
||||||
|
// Automatically hooks into WordPress post actions
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `Igny8SiteIntegration`
|
||||||
|
|
||||||
|
Manages site data collection and semantic mapping.
|
||||||
|
|
||||||
|
```php
|
||||||
|
$integration = new Igny8SiteIntegration($site_id);
|
||||||
|
$result = $integration->full_site_scan();
|
||||||
|
```
|
||||||
|
|
||||||
|
### Main Functions
|
||||||
|
|
||||||
|
- `igny8_login($email, $password)` - Authenticate with IGNY8
|
||||||
|
- `igny8_sync_post_status_to_igny8($post_id, $post, $update)` - Sync post to IGNY8
|
||||||
|
- `igny8_collect_site_data()` - Collect all WordPress site data
|
||||||
|
- `igny8_send_site_data_to_igny8($site_id)` - Send site data to IGNY8
|
||||||
|
- `igny8_map_site_to_semantic_strategy($site_id, $site_data)` - Map to semantic structure
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## File Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
igny8-ai-os/
|
||||||
|
├── igny8-bridge.php # Main plugin file
|
||||||
|
├── README.md # This file
|
||||||
|
├── docs/ # Documentation hub
|
||||||
|
│ ├── README.md # Index of available docs
|
||||||
|
│ ├── WORDPRESS-PLUGIN-INTEGRATION.md
|
||||||
|
│ ├── wp-bridge-implementation-plan.md
|
||||||
|
│ ├── missing-saas-api-endpoints.md
|
||||||
|
│ ├── STATUS_SYNC_DOCUMENTATION.md
|
||||||
|
│ └── STYLE_GUIDE.md
|
||||||
|
├── includes/
|
||||||
|
│ ├── class-igny8-api.php # API client class
|
||||||
|
│ ├── class-igny8-sync.php # Sync handler class
|
||||||
|
│ ├── class-igny8-site.php # Site integration class
|
||||||
|
│ └── functions.php # Helper functions
|
||||||
|
├── admin/
|
||||||
|
│ ├── class-admin.php # Admin interface
|
||||||
|
│ ├── settings.php # Settings page
|
||||||
|
│ └── assets/
|
||||||
|
│ ├── css/
|
||||||
|
│ └── js/
|
||||||
|
├── sync/
|
||||||
|
│ ├── hooks.php # WordPress hooks
|
||||||
|
│ ├── post-sync.php # Post synchronization
|
||||||
|
│ └── taxonomy-sync.php # Taxonomy synchronization
|
||||||
|
├── data/
|
||||||
|
│ ├── site-collection.php # Site data collection
|
||||||
|
│ └── semantic-mapping.php # Semantic mapping
|
||||||
|
└── uninstall.php # Uninstall handler
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
### Code Standards
|
||||||
|
|
||||||
|
- Follow WordPress Coding Standards
|
||||||
|
- Use WordPress native functions
|
||||||
|
- No custom database tables
|
||||||
|
- All data in WordPress native structures
|
||||||
|
|
||||||
|
### Testing
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Run WordPress unit tests
|
||||||
|
phpunit
|
||||||
|
|
||||||
|
# Test API connection
|
||||||
|
wp eval 'var_dump((new Igny8API())->login("test@example.com", "password"));'
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
### Authentication Issues
|
||||||
|
|
||||||
|
**Problem**: Cannot connect to IGNY8 API
|
||||||
|
|
||||||
|
**Solutions**:
|
||||||
|
1. Verify email and password are correct
|
||||||
|
2. Check API endpoint is accessible
|
||||||
|
3. Check WordPress REST API is enabled
|
||||||
|
4. Review error logs in WordPress debug log
|
||||||
|
|
||||||
|
### Sync Issues
|
||||||
|
|
||||||
|
**Problem**: Posts not syncing to IGNY8
|
||||||
|
|
||||||
|
**Solutions**:
|
||||||
|
1. Verify `_igny8_task_id` exists in post meta
|
||||||
|
2. Check API token is valid (not expired)
|
||||||
|
3. Review WordPress hooks are firing
|
||||||
|
4. Check error logs
|
||||||
|
|
||||||
|
### Token Expiration
|
||||||
|
|
||||||
|
**Problem**: Token expires frequently
|
||||||
|
|
||||||
|
**Solution**: Plugin automatically refreshes tokens. If issues persist, check token refresh logic.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Support
|
||||||
|
|
||||||
|
- **Documentation**: See `WORDPRESS-PLUGIN-INTEGRATION.md`
|
||||||
|
- **API Documentation**: https://api.igny8.com/docs
|
||||||
|
- **Issues**: [GitHub Issues](repository-url/issues)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
[Your License Here]
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Changelog
|
||||||
|
|
||||||
|
### 1.0.0 - 2025-10-17
|
||||||
|
- Initial release
|
||||||
|
- API authentication
|
||||||
|
- Two-way sync
|
||||||
|
- Site data collection
|
||||||
|
- Semantic mapping
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Last Updated**: 2025-10-17
|
||||||
|
|
||||||
347
igny8-wp-integration-plugin/admin/assets/css/admin.css
Normal file
347
igny8-wp-integration-plugin/admin/assets/css/admin.css
Normal file
@@ -0,0 +1,347 @@
|
|||||||
|
/**
|
||||||
|
* Admin Styles
|
||||||
|
*
|
||||||
|
* All styles for IGNY8 Bridge admin interface
|
||||||
|
* Update this file to change global design
|
||||||
|
*
|
||||||
|
* @package Igny8Bridge
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* ============================================
|
||||||
|
Container & Layout
|
||||||
|
============================================ */
|
||||||
|
|
||||||
|
.igny8-settings-container {
|
||||||
|
max-width: 1200px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-settings-card {
|
||||||
|
background: #fff;
|
||||||
|
border: 1px solid #ccd0d4;
|
||||||
|
box-shadow: 0 1px 1px rgba(0,0,0,.04);
|
||||||
|
padding: 20px;
|
||||||
|
margin: 20px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-settings-card h2 {
|
||||||
|
margin-top: 0;
|
||||||
|
padding-bottom: 10px;
|
||||||
|
border-bottom: 1px solid #eee;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ============================================
|
||||||
|
Status Indicators
|
||||||
|
============================================ */
|
||||||
|
|
||||||
|
.igny8-status-connected {
|
||||||
|
color: #46b450;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-status-disconnected {
|
||||||
|
color: #dc3232;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-test-result {
|
||||||
|
margin-left: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-test-result .igny8-success {
|
||||||
|
color: #46b450;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-test-result .igny8-error {
|
||||||
|
color: #dc3232;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-test-result .igny8-loading {
|
||||||
|
color: #2271b1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ============================================
|
||||||
|
Sync Operations
|
||||||
|
============================================ */
|
||||||
|
|
||||||
|
.igny8-sync-actions {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 10px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-sync-actions .button {
|
||||||
|
min-width: 150px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-sync-status {
|
||||||
|
margin-top: 15px;
|
||||||
|
padding: 10px;
|
||||||
|
border-radius: 4px;
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-sync-status.igny8-sync-status-success {
|
||||||
|
background-color: #d4edda;
|
||||||
|
border: 1px solid #c3e6cb;
|
||||||
|
color: #155724;
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-sync-status.igny8-sync-status-error {
|
||||||
|
background-color: #f8d7da;
|
||||||
|
border: 1px solid #f5c6cb;
|
||||||
|
color: #721c24;
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-sync-status.igny8-sync-status-loading {
|
||||||
|
background-color: #d1ecf1;
|
||||||
|
border: 1px solid #bee5eb;
|
||||||
|
color: #0c5460;
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ============================================
|
||||||
|
Statistics
|
||||||
|
============================================ */
|
||||||
|
|
||||||
|
.igny8-stats-grid {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
||||||
|
gap: 20px;
|
||||||
|
margin-top: 15px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-stat-item {
|
||||||
|
padding: 15px;
|
||||||
|
background: #f9f9f9;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-stat-label {
|
||||||
|
font-size: 12px;
|
||||||
|
color: #666;
|
||||||
|
text-transform: uppercase;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-stat-value {
|
||||||
|
font-size: 24px;
|
||||||
|
font-weight: bold;
|
||||||
|
color: #2271b1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ============================================
|
||||||
|
Diagnostics
|
||||||
|
============================================ */
|
||||||
|
|
||||||
|
.igny8-diagnostics-grid {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
|
||||||
|
gap: 16px;
|
||||||
|
margin-top: 15px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-diagnostic-item {
|
||||||
|
padding: 15px;
|
||||||
|
background-color: #f6f7f7;
|
||||||
|
border: 1px solid #dcdcde;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-diagnostic-label {
|
||||||
|
font-size: 11px;
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.05em;
|
||||||
|
color: #555d66;
|
||||||
|
margin-bottom: 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-diagnostic-value {
|
||||||
|
font-size: 18px;
|
||||||
|
font-weight: 600;
|
||||||
|
color: #1d2327;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-diagnostic-item .description {
|
||||||
|
margin: 6px 0 0;
|
||||||
|
color: #646970;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ============================================
|
||||||
|
Buttons
|
||||||
|
============================================ */
|
||||||
|
|
||||||
|
.igny8-button-group {
|
||||||
|
display: flex;
|
||||||
|
gap: 10px;
|
||||||
|
margin: 15px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-button-group .button {
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ============================================
|
||||||
|
Loading States
|
||||||
|
============================================ */
|
||||||
|
|
||||||
|
.igny8-loading {
|
||||||
|
opacity: 0.6;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-spinner {
|
||||||
|
display: inline-block;
|
||||||
|
width: 16px;
|
||||||
|
height: 16px;
|
||||||
|
border: 2px solid #f3f3f3;
|
||||||
|
border-top: 2px solid #2271b1;
|
||||||
|
border-radius: 50%;
|
||||||
|
animation: igny8-spin 1s linear infinite;
|
||||||
|
margin-right: 8px;
|
||||||
|
vertical-align: middle;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes igny8-spin {
|
||||||
|
0% { transform: rotate(0deg); }
|
||||||
|
100% { transform: rotate(360deg); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ============================================
|
||||||
|
Messages & Notifications
|
||||||
|
============================================ */
|
||||||
|
|
||||||
|
.igny8-message {
|
||||||
|
padding: 12px;
|
||||||
|
margin: 15px 0;
|
||||||
|
border-left: 4px solid;
|
||||||
|
background: #fff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-message.igny8-message-success {
|
||||||
|
border-color: #46b450;
|
||||||
|
background-color: #f0f8f0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-message.igny8-message-error {
|
||||||
|
border-color: #dc3232;
|
||||||
|
background-color: #fff5f5;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-message.igny8-message-info {
|
||||||
|
border-color: #2271b1;
|
||||||
|
background-color: #f0f6fc;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-message.igny8-message-warning {
|
||||||
|
border-color: #f0b849;
|
||||||
|
background-color: #fffbf0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ============================================
|
||||||
|
Tables
|
||||||
|
============================================ */
|
||||||
|
|
||||||
|
.igny8-table {
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
margin: 15px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-table th,
|
||||||
|
.igny8-table td {
|
||||||
|
padding: 10px;
|
||||||
|
text-align: left;
|
||||||
|
border-bottom: 1px solid #ddd;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-table th {
|
||||||
|
background-color: #f9f9f9;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-table tr:hover {
|
||||||
|
background-color: #f9f9f9;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ============================================
|
||||||
|
Admin Columns
|
||||||
|
============================================ */
|
||||||
|
|
||||||
|
.igny8-badge {
|
||||||
|
display: inline-block;
|
||||||
|
padding: 3px 8px;
|
||||||
|
border-radius: 3px;
|
||||||
|
font-size: 11px;
|
||||||
|
font-weight: 600;
|
||||||
|
text-transform: uppercase;
|
||||||
|
line-height: 1.4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-badge-igny8 {
|
||||||
|
background-color: #2271b1;
|
||||||
|
color: #fff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-badge-wordpress {
|
||||||
|
background-color: #646970;
|
||||||
|
color: #fff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-terms-list {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-term-badge {
|
||||||
|
display: inline-block;
|
||||||
|
padding: 2px 6px;
|
||||||
|
background-color: #f0f0f1;
|
||||||
|
border: 1px solid #c3c4c7;
|
||||||
|
border-radius: 2px;
|
||||||
|
font-size: 11px;
|
||||||
|
color: #50575e;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-empty {
|
||||||
|
color: #a7aaad;
|
||||||
|
font-style: italic;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-action-link {
|
||||||
|
color: #2271b1;
|
||||||
|
text-decoration: none;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-action-link:hover {
|
||||||
|
color: #135e96;
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ============================================
|
||||||
|
Responsive
|
||||||
|
============================================ */
|
||||||
|
|
||||||
|
@media (max-width: 782px) {
|
||||||
|
.igny8-sync-actions {
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-sync-actions .button {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-stats-grid {
|
||||||
|
grid-template-columns: 1fr;
|
||||||
|
}
|
||||||
|
|
||||||
|
.igny8-diagnostics-grid {
|
||||||
|
grid-template-columns: 1fr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
178
igny8-wp-integration-plugin/admin/assets/js/admin.js
Normal file
178
igny8-wp-integration-plugin/admin/assets/js/admin.js
Normal file
@@ -0,0 +1,178 @@
|
|||||||
|
/**
|
||||||
|
* Admin JavaScript
|
||||||
|
*
|
||||||
|
* @package Igny8Bridge
|
||||||
|
*/
|
||||||
|
|
||||||
|
(function($) {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
$(document).ready(function() {
|
||||||
|
// Test connection button
|
||||||
|
$('#igny8-test-connection').on('click', function() {
|
||||||
|
var $button = $(this);
|
||||||
|
var $result = $('#igny8-test-result');
|
||||||
|
|
||||||
|
$button.prop('disabled', true).addClass('igny8-loading');
|
||||||
|
$result.html('<span class="igny8-loading">Testing...</span>');
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: igny8Admin.ajaxUrl,
|
||||||
|
type: 'POST',
|
||||||
|
data: {
|
||||||
|
action: 'igny8_test_connection',
|
||||||
|
nonce: igny8Admin.nonce
|
||||||
|
},
|
||||||
|
success: function(response) {
|
||||||
|
if (response.success) {
|
||||||
|
$result.html('<span class="igny8-success">✓ Connection successful</span>');
|
||||||
|
} else {
|
||||||
|
$result.html('<span class="igny8-error">✗ ' + (response.data.message || 'Connection failed') + '</span>');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function() {
|
||||||
|
$result.html('<span class="igny8-error">✗ Request failed</span>');
|
||||||
|
},
|
||||||
|
complete: function() {
|
||||||
|
$button.prop('disabled', false).removeClass('igny8-loading');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Sync posts to IGNY8
|
||||||
|
$('#igny8-sync-posts').on('click', function() {
|
||||||
|
igny8TriggerSync('igny8_sync_posts', 'Syncing posts to IGNY8...');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Sync taxonomies
|
||||||
|
$('#igny8-sync-taxonomies').on('click', function() {
|
||||||
|
igny8TriggerSync('igny8_sync_taxonomies', 'Syncing taxonomies...');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Sync from IGNY8
|
||||||
|
$('#igny8-sync-from-igny8').on('click', function() {
|
||||||
|
igny8TriggerSync('igny8_sync_from_igny8', 'Syncing from IGNY8...');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Collect and send site data
|
||||||
|
$('#igny8-collect-site-data').on('click', function() {
|
||||||
|
igny8TriggerSync('igny8_collect_site_data', 'Collecting and sending site data...');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Load sync statistics
|
||||||
|
igny8LoadStats();
|
||||||
|
|
||||||
|
// Handle row action links
|
||||||
|
$(document).on('click', '.igny8-action-link', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
var $link = $(this);
|
||||||
|
var postId = $link.data('post-id');
|
||||||
|
var action = $link.data('action');
|
||||||
|
|
||||||
|
if (!postId) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!confirm('Are you sure you want to ' + (action === 'send' ? 'send' : 'update') + ' this post to IGNY8?')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$link.text('Processing...').prop('disabled', true);
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: igny8Admin.ajaxUrl,
|
||||||
|
type: 'POST',
|
||||||
|
data: {
|
||||||
|
action: 'igny8_send_to_igny8',
|
||||||
|
post_id: postId,
|
||||||
|
action_type: action,
|
||||||
|
nonce: igny8Admin.nonce
|
||||||
|
},
|
||||||
|
success: function(response) {
|
||||||
|
if (response.success) {
|
||||||
|
alert(response.data.message || 'Success!');
|
||||||
|
location.reload();
|
||||||
|
} else {
|
||||||
|
alert(response.data.message || 'Failed to send to IGNY8');
|
||||||
|
$link.text(action === 'send' ? 'Send to IGNY8' : 'Update in IGNY8').prop('disabled', false);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function() {
|
||||||
|
alert('Request failed');
|
||||||
|
$link.text(action === 'send' ? 'Send to IGNY8' : 'Update in IGNY8').prop('disabled', false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trigger sync operation
|
||||||
|
*/
|
||||||
|
function igny8TriggerSync(action, message) {
|
||||||
|
var $status = $('#igny8-sync-status');
|
||||||
|
var $button = $('#' + action.replace('igny8_', 'igny8-'));
|
||||||
|
|
||||||
|
$status.removeClass('igny8-sync-status-success igny8-sync-status-error')
|
||||||
|
.addClass('igny8-sync-status-loading')
|
||||||
|
.html('<span class="igny8-spinner"></span>' + message);
|
||||||
|
|
||||||
|
$button.prop('disabled', true).addClass('igny8-loading');
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: igny8Admin.ajaxUrl,
|
||||||
|
type: 'POST',
|
||||||
|
data: {
|
||||||
|
action: action,
|
||||||
|
nonce: igny8Admin.nonce
|
||||||
|
},
|
||||||
|
success: function(response) {
|
||||||
|
if (response.success) {
|
||||||
|
$status.removeClass('igny8-sync-status-loading')
|
||||||
|
.addClass('igny8-sync-status-success')
|
||||||
|
.html('✓ ' + (response.data.message || 'Operation completed successfully'));
|
||||||
|
|
||||||
|
// Reload stats
|
||||||
|
igny8LoadStats();
|
||||||
|
} else {
|
||||||
|
$status.removeClass('igny8-sync-status-loading')
|
||||||
|
.addClass('igny8-sync-status-error')
|
||||||
|
.html('✗ ' + (response.data.message || 'Operation failed'));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function() {
|
||||||
|
$status.removeClass('igny8-sync-status-loading')
|
||||||
|
.addClass('igny8-sync-status-error')
|
||||||
|
.html('✗ Request failed');
|
||||||
|
},
|
||||||
|
complete: function() {
|
||||||
|
$button.prop('disabled', false).removeClass('igny8-loading');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load sync statistics
|
||||||
|
*/
|
||||||
|
function igny8LoadStats() {
|
||||||
|
$.ajax({
|
||||||
|
url: igny8Admin.ajaxUrl,
|
||||||
|
type: 'POST',
|
||||||
|
data: {
|
||||||
|
action: 'igny8_get_stats',
|
||||||
|
nonce: igny8Admin.nonce
|
||||||
|
},
|
||||||
|
success: function(response) {
|
||||||
|
if (response.success && response.data) {
|
||||||
|
if (response.data.synced_posts !== undefined) {
|
||||||
|
$('#igny8-stat-posts').text(response.data.synced_posts);
|
||||||
|
}
|
||||||
|
if (response.data.last_sync) {
|
||||||
|
$('#igny8-stat-last-sync').text(response.data.last_sync);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})(jQuery);
|
||||||
|
|
||||||
200
igny8-wp-integration-plugin/admin/assets/js/post-editor.js
Normal file
200
igny8-wp-integration-plugin/admin/assets/js/post-editor.js
Normal file
@@ -0,0 +1,200 @@
|
|||||||
|
/**
|
||||||
|
* Post Editor JavaScript
|
||||||
|
*
|
||||||
|
* Handles AJAX interactions for Planner and Optimizer meta boxes
|
||||||
|
*
|
||||||
|
* @package Igny8Bridge
|
||||||
|
*/
|
||||||
|
|
||||||
|
(function($) {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
$(document).ready(function() {
|
||||||
|
// Fetch Planner Brief
|
||||||
|
$('#igny8-fetch-brief').on('click', function() {
|
||||||
|
var $button = $(this);
|
||||||
|
var $message = $('#igny8-planner-brief-message');
|
||||||
|
var postId = $button.data('post-id');
|
||||||
|
var taskId = $button.data('task-id');
|
||||||
|
|
||||||
|
$button.prop('disabled', true).text('Fetching...');
|
||||||
|
$message.hide().removeClass('notice-success notice-error');
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: igny8PostEditor.ajaxUrl,
|
||||||
|
type: 'POST',
|
||||||
|
data: {
|
||||||
|
action: 'igny8_fetch_planner_brief',
|
||||||
|
nonce: igny8PostEditor.nonce,
|
||||||
|
post_id: postId,
|
||||||
|
task_id: taskId
|
||||||
|
},
|
||||||
|
success: function(response) {
|
||||||
|
if (response.success) {
|
||||||
|
$message.addClass('notice notice-success inline')
|
||||||
|
.html('<p>' + response.data.message + '</p>')
|
||||||
|
.show();
|
||||||
|
|
||||||
|
// Reload page to show updated brief
|
||||||
|
setTimeout(function() {
|
||||||
|
location.reload();
|
||||||
|
}, 1000);
|
||||||
|
} else {
|
||||||
|
$message.addClass('notice notice-error inline')
|
||||||
|
.html('<p>' + (response.data.message || 'Failed to fetch brief') + '</p>')
|
||||||
|
.show();
|
||||||
|
$button.prop('disabled', false).text('Fetch Brief');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function() {
|
||||||
|
$message.addClass('notice notice-error inline')
|
||||||
|
.html('<p>Request failed</p>')
|
||||||
|
.show();
|
||||||
|
$button.prop('disabled', false).text('Fetch Brief');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Refresh Planner Task
|
||||||
|
$('#igny8-refresh-task').on('click', function() {
|
||||||
|
var $button = $(this);
|
||||||
|
var $message = $('#igny8-planner-brief-message');
|
||||||
|
var postId = $button.data('post-id');
|
||||||
|
var taskId = $button.data('task-id');
|
||||||
|
|
||||||
|
if (!confirm('Are you sure you want to request a refresh of this task from IGNY8 Planner?')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$button.prop('disabled', true).text('Requesting...');
|
||||||
|
$message.hide().removeClass('notice-success notice-error');
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: igny8PostEditor.ajaxUrl,
|
||||||
|
type: 'POST',
|
||||||
|
data: {
|
||||||
|
action: 'igny8_refresh_planner_task',
|
||||||
|
nonce: igny8PostEditor.nonce,
|
||||||
|
post_id: postId,
|
||||||
|
task_id: taskId
|
||||||
|
},
|
||||||
|
success: function(response) {
|
||||||
|
if (response.success) {
|
||||||
|
$message.addClass('notice notice-success inline')
|
||||||
|
.html('<p>' + response.data.message + '</p>')
|
||||||
|
.show();
|
||||||
|
} else {
|
||||||
|
$message.addClass('notice notice-error inline')
|
||||||
|
.html('<p>' + (response.data.message || 'Failed to request refresh') + '</p>')
|
||||||
|
.show();
|
||||||
|
}
|
||||||
|
$button.prop('disabled', false).text('Request Refresh');
|
||||||
|
},
|
||||||
|
error: function() {
|
||||||
|
$message.addClass('notice notice-error inline')
|
||||||
|
.html('<p>Request failed</p>')
|
||||||
|
.show();
|
||||||
|
$button.prop('disabled', false).text('Request Refresh');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create Optimizer Job
|
||||||
|
$('#igny8-create-optimizer-job').on('click', function() {
|
||||||
|
var $button = $(this);
|
||||||
|
var $message = $('#igny8-optimizer-message');
|
||||||
|
var postId = $button.data('post-id');
|
||||||
|
var taskId = $button.data('task-id');
|
||||||
|
|
||||||
|
if (!confirm('Create a new optimizer job for this post?')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$button.prop('disabled', true).text('Creating...');
|
||||||
|
$message.hide().removeClass('notice-success notice-error');
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: igny8PostEditor.ajaxUrl,
|
||||||
|
type: 'POST',
|
||||||
|
data: {
|
||||||
|
action: 'igny8_create_optimizer_job',
|
||||||
|
nonce: igny8PostEditor.nonce,
|
||||||
|
post_id: postId,
|
||||||
|
task_id: taskId,
|
||||||
|
job_type: 'audit',
|
||||||
|
priority: 'normal'
|
||||||
|
},
|
||||||
|
success: function(response) {
|
||||||
|
if (response.success) {
|
||||||
|
$message.addClass('notice notice-success inline')
|
||||||
|
.html('<p>' + response.data.message + '</p>')
|
||||||
|
.show();
|
||||||
|
|
||||||
|
// Reload page to show updated status
|
||||||
|
setTimeout(function() {
|
||||||
|
location.reload();
|
||||||
|
}, 1000);
|
||||||
|
} else {
|
||||||
|
$message.addClass('notice notice-error inline')
|
||||||
|
.html('<p>' + (response.data.message || 'Failed to create job') + '</p>')
|
||||||
|
.show();
|
||||||
|
$button.prop('disabled', false).text('Request Optimization');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function() {
|
||||||
|
$message.addClass('notice notice-error inline')
|
||||||
|
.html('<p>Request failed</p>')
|
||||||
|
.show();
|
||||||
|
$button.prop('disabled', false).text('Request Optimization');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Check Optimizer Status
|
||||||
|
$('#igny8-check-optimizer-status').on('click', function() {
|
||||||
|
var $button = $(this);
|
||||||
|
var $message = $('#igny8-optimizer-message');
|
||||||
|
var postId = $button.data('post-id');
|
||||||
|
var jobId = $button.data('job-id');
|
||||||
|
|
||||||
|
$button.prop('disabled', true).text('Checking...');
|
||||||
|
$message.hide().removeClass('notice-success notice-error');
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: igny8PostEditor.ajaxUrl,
|
||||||
|
type: 'POST',
|
||||||
|
data: {
|
||||||
|
action: 'igny8_get_optimizer_status',
|
||||||
|
nonce: igny8PostEditor.nonce,
|
||||||
|
post_id: postId,
|
||||||
|
job_id: jobId
|
||||||
|
},
|
||||||
|
success: function(response) {
|
||||||
|
if (response.success) {
|
||||||
|
$message.addClass('notice notice-success inline')
|
||||||
|
.html('<p>Status: <strong>' + response.data.status + '</strong></p>')
|
||||||
|
.show();
|
||||||
|
|
||||||
|
// Reload page to show updated status
|
||||||
|
setTimeout(function() {
|
||||||
|
location.reload();
|
||||||
|
}, 1000);
|
||||||
|
} else {
|
||||||
|
$message.addClass('notice notice-error inline')
|
||||||
|
.html('<p>' + (response.data.message || 'Failed to get status') + '</p>')
|
||||||
|
.show();
|
||||||
|
}
|
||||||
|
$button.prop('disabled', false).text('Check Status');
|
||||||
|
},
|
||||||
|
error: function() {
|
||||||
|
$message.addClass('notice notice-error inline')
|
||||||
|
.html('<p>Request failed</p>')
|
||||||
|
.show();
|
||||||
|
$button.prop('disabled', false).text('Check Status');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
})(jQuery);
|
||||||
|
|
||||||
335
igny8-wp-integration-plugin/admin/class-admin-columns.php
Normal file
335
igny8-wp-integration-plugin/admin/class-admin-columns.php
Normal file
@@ -0,0 +1,335 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Admin Columns and Row Actions
|
||||||
|
*
|
||||||
|
* Adds custom columns and actions to post/page/product list tables
|
||||||
|
*
|
||||||
|
* @package Igny8Bridge
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Prevent direct access
|
||||||
|
if (!defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Igny8AdminColumns Class
|
||||||
|
*/
|
||||||
|
class Igny8AdminColumns {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
public function __construct() {
|
||||||
|
// Add columns for posts, pages, and products
|
||||||
|
add_filter('manage_posts_columns', array($this, 'add_columns'));
|
||||||
|
add_filter('manage_pages_columns', array($this, 'add_columns'));
|
||||||
|
|
||||||
|
// Add column content
|
||||||
|
add_action('manage_posts_custom_column', array($this, 'render_column_content'), 10, 2);
|
||||||
|
add_action('manage_pages_custom_column', array($this, 'render_column_content'), 10, 2);
|
||||||
|
|
||||||
|
// Make columns sortable
|
||||||
|
add_filter('manage_edit-post_sortable_columns', array($this, 'make_columns_sortable'));
|
||||||
|
add_filter('manage_edit-page_sortable_columns', array($this, 'make_columns_sortable'));
|
||||||
|
|
||||||
|
// Add row actions
|
||||||
|
add_filter('post_row_actions', array($this, 'add_row_actions'), 10, 2);
|
||||||
|
add_filter('page_row_actions', array($this, 'add_row_actions'), 10, 2);
|
||||||
|
|
||||||
|
// Handle WooCommerce products
|
||||||
|
if (class_exists('WooCommerce')) {
|
||||||
|
add_filter('manage_product_posts_columns', array($this, 'add_columns'));
|
||||||
|
add_action('manage_product_posts_custom_column', array($this, 'render_column_content'), 10, 2);
|
||||||
|
add_filter('manage_edit-product_sortable_columns', array($this, 'make_columns_sortable'));
|
||||||
|
add_filter('product_row_actions', array($this, 'add_row_actions'), 10, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle AJAX actions
|
||||||
|
add_action('wp_ajax_igny8_send_to_igny8', array($this, 'send_to_igny8'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add custom columns
|
||||||
|
*
|
||||||
|
* @param array $columns Existing columns
|
||||||
|
* @return array Modified columns
|
||||||
|
*/
|
||||||
|
public function add_columns($columns) {
|
||||||
|
// Insert after title column
|
||||||
|
$new_columns = array();
|
||||||
|
|
||||||
|
foreach ($columns as $key => $value) {
|
||||||
|
$new_columns[$key] = $value;
|
||||||
|
|
||||||
|
if ($key === 'title') {
|
||||||
|
$new_columns['igny8_source'] = __('Source', 'igny8-bridge');
|
||||||
|
$new_columns['igny8_sectors'] = __('Sectors', 'igny8-bridge');
|
||||||
|
$new_columns['igny8_clusters'] = __('Clusters', 'igny8-bridge');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $new_columns;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render column content
|
||||||
|
*
|
||||||
|
* @param string $column_name Column name
|
||||||
|
* @param int $post_id Post ID
|
||||||
|
*/
|
||||||
|
public function render_column_content($column_name, $post_id) {
|
||||||
|
switch ($column_name) {
|
||||||
|
case 'igny8_source':
|
||||||
|
$this->render_source_column($post_id);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'igny8_sectors':
|
||||||
|
$this->render_sectors_column($post_id);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'igny8_clusters':
|
||||||
|
$this->render_clusters_column($post_id);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render source column
|
||||||
|
*
|
||||||
|
* @param int $post_id Post ID
|
||||||
|
*/
|
||||||
|
private function render_source_column($post_id) {
|
||||||
|
$task_id = get_post_meta($post_id, '_igny8_task_id', true);
|
||||||
|
|
||||||
|
if ($task_id) {
|
||||||
|
echo '<span class="igny8-badge igny8-badge-igny8" title="' . esc_attr__('Synced from IGNY8', 'igny8-bridge') . '">';
|
||||||
|
echo esc_html__('IGNY8', 'igny8-bridge');
|
||||||
|
echo '</span>';
|
||||||
|
} else {
|
||||||
|
echo '<span class="igny8-badge igny8-badge-wordpress" title="' . esc_attr__('WordPress native', 'igny8-bridge') . '">';
|
||||||
|
echo esc_html__('WP', 'igny8-bridge');
|
||||||
|
echo '</span>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render sectors column
|
||||||
|
*
|
||||||
|
* @param int $post_id Post ID
|
||||||
|
*/
|
||||||
|
private function render_sectors_column($post_id) {
|
||||||
|
$sectors = wp_get_post_terms($post_id, 'igny8_sectors', array('fields' => 'names'));
|
||||||
|
|
||||||
|
if (!empty($sectors) && !is_wp_error($sectors)) {
|
||||||
|
echo '<div class="igny8-terms-list">';
|
||||||
|
foreach ($sectors as $sector) {
|
||||||
|
echo '<span class="igny8-term-badge">' . esc_html($sector) . '</span>';
|
||||||
|
}
|
||||||
|
echo '</div>';
|
||||||
|
} else {
|
||||||
|
echo '<span class="igny8-empty">—</span>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render clusters column
|
||||||
|
*
|
||||||
|
* @param int $post_id Post ID
|
||||||
|
*/
|
||||||
|
private function render_clusters_column($post_id) {
|
||||||
|
$clusters = wp_get_post_terms($post_id, 'igny8_clusters', array('fields' => 'names'));
|
||||||
|
|
||||||
|
if (!empty($clusters) && !is_wp_error($clusters)) {
|
||||||
|
echo '<div class="igny8-terms-list">';
|
||||||
|
foreach ($clusters as $cluster) {
|
||||||
|
echo '<span class="igny8-term-badge">' . esc_html($cluster) . '</span>';
|
||||||
|
}
|
||||||
|
echo '</div>';
|
||||||
|
} else {
|
||||||
|
echo '<span class="igny8-empty">—</span>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make columns sortable
|
||||||
|
*
|
||||||
|
* @param array $columns Sortable columns
|
||||||
|
* @return array Modified columns
|
||||||
|
*/
|
||||||
|
public function make_columns_sortable($columns) {
|
||||||
|
$columns['igny8_source'] = 'igny8_source';
|
||||||
|
return $columns;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add row actions
|
||||||
|
*
|
||||||
|
* @param array $actions Existing actions
|
||||||
|
* @param WP_Post $post Post object
|
||||||
|
* @return array Modified actions
|
||||||
|
*/
|
||||||
|
public function add_row_actions($actions, $post) {
|
||||||
|
// Only add for published posts
|
||||||
|
if ($post->post_status !== 'publish') {
|
||||||
|
return $actions;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if already synced to IGNY8
|
||||||
|
$task_id = get_post_meta($post->ID, '_igny8_task_id', true);
|
||||||
|
|
||||||
|
if ($task_id) {
|
||||||
|
// Already synced - show update action
|
||||||
|
$actions['igny8_update'] = sprintf(
|
||||||
|
'<a href="%s" class="igny8-action-link" data-post-id="%d" data-action="update">%s</a>',
|
||||||
|
'#',
|
||||||
|
$post->ID,
|
||||||
|
__('Update in IGNY8', 'igny8-bridge')
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// Not synced - show send action
|
||||||
|
$actions['igny8_send'] = sprintf(
|
||||||
|
'<a href="%s" class="igny8-action-link" data-post-id="%d" data-action="send">%s</a>',
|
||||||
|
'#',
|
||||||
|
$post->ID,
|
||||||
|
__('Send to IGNY8', 'igny8-bridge')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $actions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send post to IGNY8 (AJAX handler)
|
||||||
|
*/
|
||||||
|
public static function send_to_igny8() {
|
||||||
|
check_ajax_referer('igny8_admin_nonce', 'nonce');
|
||||||
|
|
||||||
|
if (!current_user_can('edit_posts')) {
|
||||||
|
wp_send_json_error(array('message' => 'Unauthorized'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
|
||||||
|
$action = isset($_POST['action_type']) ? sanitize_text_field($_POST['action_type']) : 'send';
|
||||||
|
|
||||||
|
if (!$post_id) {
|
||||||
|
wp_send_json_error(array('message' => 'Invalid post ID'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$post = get_post($post_id);
|
||||||
|
if (!$post) {
|
||||||
|
wp_send_json_error(array('message' => 'Post not found'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
wp_send_json_error(array('message' => 'Connection is disabled. Enable sync operations first.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
wp_send_json_error(array('message' => 'Not authenticated with IGNY8'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$site_id = get_option('igny8_site_id');
|
||||||
|
if (!$site_id) {
|
||||||
|
wp_send_json_error(array('message' => 'Site ID not set'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare post data for IGNY8
|
||||||
|
$post_data = array(
|
||||||
|
'title' => $post->post_title,
|
||||||
|
'content' => $post->post_content,
|
||||||
|
'excerpt' => $post->post_excerpt,
|
||||||
|
'status' => $post->post_status === 'publish' ? 'completed' : 'draft',
|
||||||
|
'post_type' => $post->post_type,
|
||||||
|
'url' => get_permalink($post_id),
|
||||||
|
'wordpress_post_id' => $post_id
|
||||||
|
);
|
||||||
|
|
||||||
|
// Get categories
|
||||||
|
$categories = wp_get_post_categories($post_id, array('fields' => 'names'));
|
||||||
|
if (!empty($categories)) {
|
||||||
|
$post_data['categories'] = $categories;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get tags
|
||||||
|
$tags = wp_get_post_tags($post_id, array('fields' => 'names'));
|
||||||
|
if (!empty($tags)) {
|
||||||
|
$post_data['tags'] = $tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get featured image
|
||||||
|
$featured_image_id = get_post_thumbnail_id($post_id);
|
||||||
|
if ($featured_image_id) {
|
||||||
|
$post_data['featured_image'] = wp_get_attachment_image_url($featured_image_id, 'full');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get sectors and clusters
|
||||||
|
$sectors = wp_get_post_terms($post_id, 'igny8_sectors', array('fields' => 'ids'));
|
||||||
|
$clusters = wp_get_post_terms($post_id, 'igny8_clusters', array('fields' => 'ids'));
|
||||||
|
|
||||||
|
if (!empty($sectors)) {
|
||||||
|
// Get IGNY8 sector IDs from term meta
|
||||||
|
$igny8_sector_ids = array();
|
||||||
|
foreach ($sectors as $term_id) {
|
||||||
|
$igny8_sector_id = get_term_meta($term_id, '_igny8_sector_id', true);
|
||||||
|
if ($igny8_sector_id) {
|
||||||
|
$igny8_sector_ids[] = $igny8_sector_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!empty($igny8_sector_ids)) {
|
||||||
|
$post_data['sector_id'] = $igny8_sector_ids[0]; // Use first sector
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($clusters)) {
|
||||||
|
// Get IGNY8 cluster IDs from term meta
|
||||||
|
$igny8_cluster_ids = array();
|
||||||
|
foreach ($clusters as $term_id) {
|
||||||
|
$igny8_cluster_id = get_term_meta($term_id, '_igny8_cluster_id', true);
|
||||||
|
if ($igny8_cluster_id) {
|
||||||
|
$igny8_cluster_ids[] = $igny8_cluster_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!empty($igny8_cluster_ids)) {
|
||||||
|
$post_data['cluster_id'] = $igny8_cluster_ids[0]; // Use first cluster
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if post already has task ID
|
||||||
|
$existing_task_id = get_post_meta($post_id, '_igny8_task_id', true);
|
||||||
|
|
||||||
|
if ($existing_task_id && $action === 'update') {
|
||||||
|
// Update existing task
|
||||||
|
$response = $api->put("/writer/tasks/{$existing_task_id}/", $post_data);
|
||||||
|
} else {
|
||||||
|
// Create new task
|
||||||
|
$response = $api->post("/writer/tasks/", $post_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($response['success']) {
|
||||||
|
$task_id = $response['data']['id'] ?? $existing_task_id;
|
||||||
|
|
||||||
|
// Store task ID
|
||||||
|
update_post_meta($post_id, '_igny8_task_id', $task_id);
|
||||||
|
update_post_meta($post_id, '_igny8_last_synced', current_time('mysql'));
|
||||||
|
|
||||||
|
wp_send_json_success(array(
|
||||||
|
'message' => $action === 'update' ? 'Post updated in IGNY8' : 'Post sent to IGNY8',
|
||||||
|
'task_id' => $task_id
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
wp_send_json_error(array(
|
||||||
|
'message' => 'Failed to send to IGNY8: ' . ($response['error'] ?? 'Unknown error')
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize
|
||||||
|
new Igny8AdminColumns();
|
||||||
|
|
||||||
|
// Register AJAX handler
|
||||||
|
add_action('wp_ajax_igny8_send_to_igny8', array('Igny8AdminColumns', 'send_to_igny8'));
|
||||||
|
|
||||||
488
igny8-wp-integration-plugin/admin/class-admin.php
Normal file
488
igny8-wp-integration-plugin/admin/class-admin.php
Normal file
@@ -0,0 +1,488 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Admin Interface Class
|
||||||
|
*
|
||||||
|
* Handles all admin functionality
|
||||||
|
*
|
||||||
|
* @package Igny8Bridge
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Prevent direct access
|
||||||
|
if (!defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Igny8Admin Class
|
||||||
|
*/
|
||||||
|
class Igny8Admin {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Single instance of the class
|
||||||
|
*
|
||||||
|
* @var Igny8Admin
|
||||||
|
*/
|
||||||
|
private static $instance = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get single instance
|
||||||
|
*
|
||||||
|
* @return Igny8Admin
|
||||||
|
*/
|
||||||
|
public static function get_instance() {
|
||||||
|
if (null === self::$instance) {
|
||||||
|
self::$instance = new self();
|
||||||
|
}
|
||||||
|
return self::$instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
private function __construct() {
|
||||||
|
add_action('admin_menu', array($this, 'add_menu_pages'));
|
||||||
|
add_action('admin_init', array($this, 'register_settings'));
|
||||||
|
add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add admin menu pages
|
||||||
|
*/
|
||||||
|
public function add_menu_pages() {
|
||||||
|
add_options_page(
|
||||||
|
__('IGNY8 API Settings', 'igny8-bridge'),
|
||||||
|
__('IGNY8 API', 'igny8-bridge'),
|
||||||
|
'manage_options',
|
||||||
|
'igny8-settings',
|
||||||
|
array($this, 'render_settings_page')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register settings
|
||||||
|
*/
|
||||||
|
public function register_settings() {
|
||||||
|
register_setting('igny8_settings', 'igny8_email');
|
||||||
|
register_setting('igny8_settings', 'igny8_site_id');
|
||||||
|
|
||||||
|
register_setting('igny8_bridge_connection', 'igny8_connection_enabled', array(
|
||||||
|
'type' => 'boolean',
|
||||||
|
'sanitize_callback' => array($this, 'sanitize_boolean'),
|
||||||
|
'default' => 1
|
||||||
|
));
|
||||||
|
|
||||||
|
register_setting('igny8_bridge_controls', 'igny8_enabled_post_types', array(
|
||||||
|
'type' => 'array',
|
||||||
|
'sanitize_callback' => array($this, 'sanitize_post_types'),
|
||||||
|
'default' => array_keys(igny8_get_supported_post_types())
|
||||||
|
));
|
||||||
|
|
||||||
|
register_setting('igny8_bridge_controls', 'igny8_enable_woocommerce', array(
|
||||||
|
'type' => 'boolean',
|
||||||
|
'sanitize_callback' => array($this, 'sanitize_boolean'),
|
||||||
|
'default' => class_exists('WooCommerce') ? 1 : 0
|
||||||
|
));
|
||||||
|
|
||||||
|
register_setting('igny8_bridge_controls', 'igny8_control_mode', array(
|
||||||
|
'type' => 'string',
|
||||||
|
'sanitize_callback' => array($this, 'sanitize_control_mode'),
|
||||||
|
'default' => 'mirror'
|
||||||
|
));
|
||||||
|
|
||||||
|
register_setting('igny8_bridge_controls', 'igny8_enabled_modules', array(
|
||||||
|
'type' => 'array',
|
||||||
|
'sanitize_callback' => array($this, 'sanitize_modules'),
|
||||||
|
'default' => array_keys(igny8_get_available_modules())
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enqueue admin scripts and styles
|
||||||
|
*
|
||||||
|
* @param string $hook Current admin page hook
|
||||||
|
*/
|
||||||
|
public function enqueue_scripts($hook) {
|
||||||
|
// Enqueue on settings page
|
||||||
|
if ($hook === 'settings_page_igny8-settings') {
|
||||||
|
wp_enqueue_style(
|
||||||
|
'igny8-admin-style',
|
||||||
|
IGNY8_BRIDGE_PLUGIN_URL . 'admin/assets/css/admin.css',
|
||||||
|
array(),
|
||||||
|
IGNY8_BRIDGE_VERSION
|
||||||
|
);
|
||||||
|
|
||||||
|
wp_enqueue_script(
|
||||||
|
'igny8-admin-script',
|
||||||
|
IGNY8_BRIDGE_PLUGIN_URL . 'admin/assets/js/admin.js',
|
||||||
|
array('jquery'),
|
||||||
|
IGNY8_BRIDGE_VERSION,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
wp_localize_script('igny8-admin-script', 'igny8Admin', array(
|
||||||
|
'ajaxUrl' => admin_url('admin-ajax.php'),
|
||||||
|
'nonce' => wp_create_nonce('igny8_admin_nonce'),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enqueue on post/page/product list pages
|
||||||
|
if (strpos($hook, 'edit.php') !== false) {
|
||||||
|
$screen = get_current_screen();
|
||||||
|
if ($screen && in_array($screen->post_type, array('post', 'page', 'product', ''))) {
|
||||||
|
wp_enqueue_style(
|
||||||
|
'igny8-admin-style',
|
||||||
|
IGNY8_BRIDGE_PLUGIN_URL . 'admin/assets/css/admin.css',
|
||||||
|
array(),
|
||||||
|
IGNY8_BRIDGE_VERSION
|
||||||
|
);
|
||||||
|
|
||||||
|
wp_enqueue_script(
|
||||||
|
'igny8-admin-script',
|
||||||
|
IGNY8_BRIDGE_PLUGIN_URL . 'admin/assets/js/admin.js',
|
||||||
|
array('jquery'),
|
||||||
|
IGNY8_BRIDGE_VERSION,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
wp_localize_script('igny8-admin-script', 'igny8Admin', array(
|
||||||
|
'ajaxUrl' => admin_url('admin-ajax.php'),
|
||||||
|
'nonce' => wp_create_nonce('igny8_admin_nonce'),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render settings page
|
||||||
|
*/
|
||||||
|
public function render_settings_page() {
|
||||||
|
// Handle form submission
|
||||||
|
if (isset($_POST['igny8_connect']) && check_admin_referer('igny8_settings_nonce')) {
|
||||||
|
$this->handle_connection();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle webhook secret regeneration
|
||||||
|
if (isset($_POST['igny8_regenerate_secret']) && check_admin_referer('igny8_regenerate_secret')) {
|
||||||
|
$new_secret = igny8_regenerate_webhook_secret();
|
||||||
|
add_settings_error(
|
||||||
|
'igny8_settings',
|
||||||
|
'igny8_secret_regenerated',
|
||||||
|
__('Webhook secret regenerated. Update it in your IGNY8 SaaS app settings.', 'igny8-bridge'),
|
||||||
|
'updated'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Include settings template
|
||||||
|
include IGNY8_BRIDGE_PLUGIN_DIR . 'admin/settings.php';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle API connection
|
||||||
|
*/
|
||||||
|
private function handle_connection() {
|
||||||
|
$email = sanitize_email($_POST['igny8_email'] ?? '');
|
||||||
|
$password = $_POST['igny8_password'] ?? '';
|
||||||
|
|
||||||
|
if (empty($email) || empty($password)) {
|
||||||
|
add_settings_error(
|
||||||
|
'igny8_settings',
|
||||||
|
'igny8_error',
|
||||||
|
__('Email and password are required.', 'igny8-bridge'),
|
||||||
|
'error'
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
if ($api->login($email, $password)) {
|
||||||
|
update_option('igny8_email', $email);
|
||||||
|
|
||||||
|
// Try to get site ID (if available)
|
||||||
|
$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']);
|
||||||
|
}
|
||||||
|
|
||||||
|
add_settings_error(
|
||||||
|
'igny8_settings',
|
||||||
|
'igny8_connected',
|
||||||
|
__('Successfully connected to IGNY8 API.', 'igny8-bridge'),
|
||||||
|
'updated'
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
add_settings_error(
|
||||||
|
'igny8_settings',
|
||||||
|
'igny8_error',
|
||||||
|
__('Failed to connect to IGNY8 API. Please check your credentials.', 'igny8-bridge'),
|
||||||
|
'error'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test API connection (AJAX handler)
|
||||||
|
*/
|
||||||
|
public static function test_connection() {
|
||||||
|
check_ajax_referer('igny8_admin_nonce', 'nonce');
|
||||||
|
|
||||||
|
if (!current_user_can('manage_options')) {
|
||||||
|
wp_send_json_error(array('message' => 'Unauthorized'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
wp_send_json_error(array('message' => 'Connection is disabled. Enable sync operations to test.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
wp_send_json_error(array('message' => 'Not authenticated'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test with a simple API call
|
||||||
|
$response = $api->get('/planner/keywords/?page_size=1');
|
||||||
|
|
||||||
|
if ($response['success']) {
|
||||||
|
$checked_at = current_time('timestamp');
|
||||||
|
update_option('igny8_last_api_health_check', $checked_at);
|
||||||
|
wp_send_json_success(array(
|
||||||
|
'message' => 'Connection successful',
|
||||||
|
'data' => $response,
|
||||||
|
'checked_at' => $checked_at
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
wp_send_json_error(array(
|
||||||
|
'message' => 'Connection failed',
|
||||||
|
'error' => $response['error']
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync posts to IGNY8 (AJAX handler)
|
||||||
|
*/
|
||||||
|
public static function sync_posts() {
|
||||||
|
check_ajax_referer('igny8_admin_nonce', 'nonce');
|
||||||
|
|
||||||
|
if (!current_user_can('manage_options')) {
|
||||||
|
wp_send_json_error(array('message' => 'Unauthorized'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
wp_send_json_error(array('message' => 'Connection is disabled. Enable sync operations first.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = igny8_batch_sync_post_statuses();
|
||||||
|
|
||||||
|
wp_send_json_success(array(
|
||||||
|
'message' => sprintf('Synced %d posts, %d failed', $result['synced'], $result['failed']),
|
||||||
|
'data' => $result
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync taxonomies (AJAX handler)
|
||||||
|
*/
|
||||||
|
public static function sync_taxonomies() {
|
||||||
|
check_ajax_referer('igny8_admin_nonce', 'nonce');
|
||||||
|
|
||||||
|
if (!current_user_can('manage_options')) {
|
||||||
|
wp_send_json_error(array('message' => 'Unauthorized'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
wp_send_json_error(array('message' => 'Connection is disabled. Enable sync operations first.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
wp_send_json_error(array('message' => 'Not authenticated'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sync sectors and clusters from IGNY8
|
||||||
|
$sectors_result = igny8_sync_igny8_sectors_to_wp();
|
||||||
|
$clusters_result = igny8_sync_igny8_clusters_to_wp();
|
||||||
|
|
||||||
|
wp_send_json_success(array(
|
||||||
|
'message' => sprintf('Synced %d sectors, %d clusters',
|
||||||
|
$sectors_result['synced'] ?? 0,
|
||||||
|
$clusters_result['synced'] ?? 0
|
||||||
|
),
|
||||||
|
'data' => array(
|
||||||
|
'sectors' => $sectors_result,
|
||||||
|
'clusters' => $clusters_result
|
||||||
|
)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync from IGNY8 (AJAX handler)
|
||||||
|
*/
|
||||||
|
public static function sync_from_igny8() {
|
||||||
|
check_ajax_referer('igny8_admin_nonce', 'nonce');
|
||||||
|
|
||||||
|
if (!current_user_can('manage_options')) {
|
||||||
|
wp_send_json_error(array('message' => 'Unauthorized'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
wp_send_json_error(array('message' => 'Connection is disabled. Enable sync operations first.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = igny8_sync_igny8_tasks_to_wp();
|
||||||
|
|
||||||
|
if ($result['success']) {
|
||||||
|
wp_send_json_success(array(
|
||||||
|
'message' => sprintf('Created %d posts, updated %d posts',
|
||||||
|
$result['created'],
|
||||||
|
$result['updated']
|
||||||
|
),
|
||||||
|
'data' => $result
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
wp_send_json_error(array(
|
||||||
|
'message' => $result['error'] ?? 'Sync failed'
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collect and send site data (AJAX handler)
|
||||||
|
*/
|
||||||
|
public static function collect_site_data() {
|
||||||
|
check_ajax_referer('igny8_admin_nonce', 'nonce');
|
||||||
|
|
||||||
|
if (!current_user_can('manage_options')) {
|
||||||
|
wp_send_json_error(array('message' => 'Unauthorized'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
wp_send_json_error(array('message' => 'Connection is disabled. Enable sync operations first.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$site_id = get_option('igny8_site_id');
|
||||||
|
if (!$site_id) {
|
||||||
|
wp_send_json_error(array('message' => 'Site ID not set'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = igny8_send_site_data_to_igny8($site_id);
|
||||||
|
|
||||||
|
if ($result) {
|
||||||
|
wp_send_json_success(array(
|
||||||
|
'message' => 'Site data collected and sent successfully',
|
||||||
|
'data' => $result
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
wp_send_json_error(array('message' => 'Failed to send site data'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get sync statistics (AJAX handler)
|
||||||
|
*/
|
||||||
|
public static function get_stats() {
|
||||||
|
check_ajax_referer('igny8_admin_nonce', 'nonce');
|
||||||
|
|
||||||
|
if (!current_user_can('manage_options')) {
|
||||||
|
wp_send_json_error(array('message' => 'Unauthorized'));
|
||||||
|
}
|
||||||
|
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
// Count synced posts
|
||||||
|
$synced_posts = $wpdb->get_var("
|
||||||
|
SELECT COUNT(DISTINCT post_id)
|
||||||
|
FROM {$wpdb->postmeta}
|
||||||
|
WHERE meta_key = '_igny8_task_id'
|
||||||
|
");
|
||||||
|
|
||||||
|
// Get last sync time
|
||||||
|
$last_sync = get_option('igny8_last_site_sync', 0);
|
||||||
|
$last_sync_formatted = $last_sync ? date_i18n(get_option('date_format') . ' ' . get_option('time_format'), $last_sync) : 'Never';
|
||||||
|
|
||||||
|
wp_send_json_success(array(
|
||||||
|
'synced_posts' => intval($synced_posts),
|
||||||
|
'last_sync' => $last_sync_formatted
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sanitize post types option
|
||||||
|
*
|
||||||
|
* @param mixed $value Raw value
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function sanitize_post_types($value) {
|
||||||
|
$supported = array_keys(igny8_get_supported_post_types());
|
||||||
|
|
||||||
|
if (!is_array($value)) {
|
||||||
|
return $supported;
|
||||||
|
}
|
||||||
|
|
||||||
|
$clean = array();
|
||||||
|
foreach ($value as $post_type) {
|
||||||
|
$post_type = sanitize_key($post_type);
|
||||||
|
if (in_array($post_type, $supported, true)) {
|
||||||
|
$clean[] = $post_type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return !empty($clean) ? $clean : $supported;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sanitize boolean option
|
||||||
|
*
|
||||||
|
* @param mixed $value Raw value
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function sanitize_boolean($value) {
|
||||||
|
return $value ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sanitize control mode
|
||||||
|
*
|
||||||
|
* @param mixed $value Raw value
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function sanitize_control_mode($value) {
|
||||||
|
$value = is_string($value) ? strtolower($value) : 'mirror';
|
||||||
|
return in_array($value, array('mirror', 'hybrid'), true) ? $value : 'mirror';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sanitize module toggles
|
||||||
|
*
|
||||||
|
* @param mixed $value Raw value
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function sanitize_modules($value) {
|
||||||
|
$supported = array_keys(igny8_get_available_modules());
|
||||||
|
|
||||||
|
if (!is_array($value)) {
|
||||||
|
return $supported;
|
||||||
|
}
|
||||||
|
|
||||||
|
$clean = array();
|
||||||
|
foreach ($value as $module) {
|
||||||
|
$module = sanitize_key($module);
|
||||||
|
if (in_array($module, $supported, true)) {
|
||||||
|
$clean[] = $module;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return !empty($clean) ? $clean : $supported;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register AJAX handlers
|
||||||
|
add_action('wp_ajax_igny8_test_connection', array('Igny8Admin', 'test_connection'));
|
||||||
|
add_action('wp_ajax_igny8_sync_posts', array('Igny8Admin', 'sync_posts'));
|
||||||
|
add_action('wp_ajax_igny8_sync_taxonomies', array('Igny8Admin', 'sync_taxonomies'));
|
||||||
|
add_action('wp_ajax_igny8_sync_from_igny8', array('Igny8Admin', 'sync_from_igny8'));
|
||||||
|
add_action('wp_ajax_igny8_collect_site_data', array('Igny8Admin', 'collect_site_data'));
|
||||||
|
add_action('wp_ajax_igny8_get_stats', array('Igny8Admin', 'get_stats'));
|
||||||
|
|
||||||
469
igny8-wp-integration-plugin/admin/class-post-meta-boxes.php
Normal file
469
igny8-wp-integration-plugin/admin/class-post-meta-boxes.php
Normal file
@@ -0,0 +1,469 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Post Meta Boxes
|
||||||
|
*
|
||||||
|
* Adds meta boxes to post editor for IGNY8 features
|
||||||
|
*
|
||||||
|
* @package Igny8Bridge
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Prevent direct access
|
||||||
|
if (!defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Igny8PostMetaBoxes Class
|
||||||
|
*/
|
||||||
|
class Igny8PostMetaBoxes {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
public function __construct() {
|
||||||
|
add_action('add_meta_boxes', array($this, 'add_meta_boxes'));
|
||||||
|
add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
|
||||||
|
|
||||||
|
// AJAX handlers
|
||||||
|
add_action('wp_ajax_igny8_fetch_planner_brief', array($this, 'fetch_planner_brief'));
|
||||||
|
add_action('wp_ajax_igny8_refresh_planner_task', array($this, 'refresh_planner_task'));
|
||||||
|
add_action('wp_ajax_igny8_create_optimizer_job', array($this, 'create_optimizer_job'));
|
||||||
|
add_action('wp_ajax_igny8_get_optimizer_status', array($this, 'get_optimizer_status'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add meta boxes to post editor
|
||||||
|
*/
|
||||||
|
public function add_meta_boxes() {
|
||||||
|
$post_types = array('post', 'page', 'product');
|
||||||
|
|
||||||
|
foreach ($post_types as $post_type) {
|
||||||
|
add_meta_box(
|
||||||
|
'igny8-planner-brief',
|
||||||
|
__('IGNY8 Planner Brief', 'igny8-bridge'),
|
||||||
|
array($this, 'render_planner_brief_box'),
|
||||||
|
$post_type,
|
||||||
|
'side',
|
||||||
|
'default'
|
||||||
|
);
|
||||||
|
|
||||||
|
add_meta_box(
|
||||||
|
'igny8-optimizer',
|
||||||
|
__('IGNY8 Optimizer', 'igny8-bridge'),
|
||||||
|
array($this, 'render_optimizer_box'),
|
||||||
|
$post_type,
|
||||||
|
'side',
|
||||||
|
'default'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enqueue scripts for post editor
|
||||||
|
*/
|
||||||
|
public function enqueue_scripts($hook) {
|
||||||
|
if (!in_array($hook, array('post.php', 'post-new.php'), true)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_enqueue_script(
|
||||||
|
'igny8-post-editor',
|
||||||
|
IGNY8_BRIDGE_PLUGIN_URL . 'admin/assets/js/post-editor.js',
|
||||||
|
array('jquery'),
|
||||||
|
IGNY8_BRIDGE_VERSION,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
wp_localize_script('igny8-post-editor', 'igny8PostEditor', array(
|
||||||
|
'ajaxUrl' => admin_url('admin-ajax.php'),
|
||||||
|
'nonce' => wp_create_nonce('igny8_post_editor_nonce'),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render Planner Brief meta box
|
||||||
|
*/
|
||||||
|
public function render_planner_brief_box($post) {
|
||||||
|
$task_id = get_post_meta($post->ID, '_igny8_task_id', true);
|
||||||
|
$brief = get_post_meta($post->ID, '_igny8_task_brief', true);
|
||||||
|
$brief_cached_at = get_post_meta($post->ID, '_igny8_brief_cached_at', true);
|
||||||
|
$cluster_id = get_post_meta($post->ID, '_igny8_cluster_id', true);
|
||||||
|
|
||||||
|
if (!$task_id && !$cluster_id) {
|
||||||
|
echo '<p class="description">';
|
||||||
|
_e('This post is not linked to an IGNY8 task or cluster.', 'igny8-bridge');
|
||||||
|
echo '</p>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_nonce_field('igny8_post_editor_nonce', 'igny8_post_editor_nonce');
|
||||||
|
?>
|
||||||
|
<div id="igny8-planner-brief-content">
|
||||||
|
<?php if ($brief) : ?>
|
||||||
|
<div class="igny8-brief-display">
|
||||||
|
<?php if (is_array($brief)) : ?>
|
||||||
|
<?php if (!empty($brief['title'])) : ?>
|
||||||
|
<h4><?php echo esc_html($brief['title']); ?></h4>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<?php if (!empty($brief['content'])) : ?>
|
||||||
|
<div class="igny8-brief-content">
|
||||||
|
<?php echo wp_kses_post(wpautop($brief['content'])); ?>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<?php if (!empty($brief['outline'])) : ?>
|
||||||
|
<div class="igny8-brief-outline">
|
||||||
|
<strong><?php _e('Outline:', 'igny8-bridge'); ?></strong>
|
||||||
|
<?php if (is_array($brief['outline'])) : ?>
|
||||||
|
<ul>
|
||||||
|
<?php foreach ($brief['outline'] as $item) : ?>
|
||||||
|
<li><?php echo esc_html($item); ?></li>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</ul>
|
||||||
|
<?php else : ?>
|
||||||
|
<p><?php echo esc_html($brief['outline']); ?></p>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<?php if (!empty($brief['keywords'])) : ?>
|
||||||
|
<div class="igny8-brief-keywords">
|
||||||
|
<strong><?php _e('Keywords:', 'igny8-bridge'); ?></strong>
|
||||||
|
<?php
|
||||||
|
$keywords = is_array($brief['keywords']) ? $brief['keywords'] : explode(',', $brief['keywords']);
|
||||||
|
echo '<span class="igny8-keyword-tags">';
|
||||||
|
foreach ($keywords as $keyword) {
|
||||||
|
echo '<span class="igny8-keyword-tag">' . esc_html(trim($keyword)) . '</span>';
|
||||||
|
}
|
||||||
|
echo '</span>';
|
||||||
|
?>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<?php if (!empty($brief['tone'])) : ?>
|
||||||
|
<div class="igny8-brief-tone">
|
||||||
|
<strong><?php _e('Tone:', 'igny8-bridge'); ?></strong>
|
||||||
|
<?php echo esc_html($brief['tone']); ?>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
<?php else : ?>
|
||||||
|
<p><?php echo esc_html($brief); ?></p>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<?php if ($brief_cached_at) : ?>
|
||||||
|
<p class="description">
|
||||||
|
<?php
|
||||||
|
printf(
|
||||||
|
__('Cached: %s', 'igny8-bridge'),
|
||||||
|
date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($brief_cached_at))
|
||||||
|
);
|
||||||
|
?>
|
||||||
|
</p>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
<?php else : ?>
|
||||||
|
<p class="description">
|
||||||
|
<?php _e('No brief cached. Click "Fetch Brief" to load from IGNY8.', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<button type="button"
|
||||||
|
id="igny8-fetch-brief"
|
||||||
|
class="button button-secondary"
|
||||||
|
data-post-id="<?php echo esc_attr($post->ID); ?>"
|
||||||
|
data-task-id="<?php echo esc_attr($task_id); ?>">
|
||||||
|
<?php _e('Fetch Brief', 'igny8-bridge'); ?>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<?php if ($task_id) : ?>
|
||||||
|
<button type="button"
|
||||||
|
id="igny8-refresh-task"
|
||||||
|
class="button button-secondary"
|
||||||
|
data-post-id="<?php echo esc_attr($post->ID); ?>"
|
||||||
|
data-task-id="<?php echo esc_attr($task_id); ?>">
|
||||||
|
<?php _e('Request Refresh', 'igny8-bridge'); ?>
|
||||||
|
</button>
|
||||||
|
<?php endif; ?>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<div id="igny8-planner-brief-message" class="igny8-message" style="display: none;"></div>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render Optimizer meta box
|
||||||
|
*/
|
||||||
|
public function render_optimizer_box($post) {
|
||||||
|
$task_id = get_post_meta($post->ID, '_igny8_task_id', true);
|
||||||
|
$optimizer_job_id = get_post_meta($post->ID, '_igny8_optimizer_job_id', true);
|
||||||
|
$optimizer_status = get_post_meta($post->ID, '_igny8_optimizer_status', true);
|
||||||
|
|
||||||
|
if (!$task_id) {
|
||||||
|
echo '<p class="description">';
|
||||||
|
_e('This post is not linked to an IGNY8 task.', 'igny8-bridge');
|
||||||
|
echo '</p>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_nonce_field('igny8_post_editor_nonce', 'igny8_post_editor_nonce');
|
||||||
|
?>
|
||||||
|
<div id="igny8-optimizer-content">
|
||||||
|
<?php if ($optimizer_job_id) : ?>
|
||||||
|
<div class="igny8-optimizer-status">
|
||||||
|
<p>
|
||||||
|
<strong><?php _e('Job ID:', 'igny8-bridge'); ?></strong>
|
||||||
|
<?php echo esc_html($optimizer_job_id); ?>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<?php if ($optimizer_status) : ?>
|
||||||
|
<p>
|
||||||
|
<strong><?php _e('Status:', 'igny8-bridge'); ?></strong>
|
||||||
|
<span class="igny8-status-badge igny8-status-<?php echo esc_attr(strtolower($optimizer_status)); ?>">
|
||||||
|
<?php echo esc_html($optimizer_status); ?>
|
||||||
|
</span>
|
||||||
|
</p>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<button type="button"
|
||||||
|
id="igny8-check-optimizer-status"
|
||||||
|
class="button button-secondary"
|
||||||
|
data-post-id="<?php echo esc_attr($post->ID); ?>"
|
||||||
|
data-job-id="<?php echo esc_attr($optimizer_job_id); ?>">
|
||||||
|
<?php _e('Check Status', 'igny8-bridge'); ?>
|
||||||
|
</button>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<?php else : ?>
|
||||||
|
<p class="description">
|
||||||
|
<?php _e('No optimizer job created yet.', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<button type="button"
|
||||||
|
id="igny8-create-optimizer-job"
|
||||||
|
class="button button-primary"
|
||||||
|
data-post-id="<?php echo esc_attr($post->ID); ?>"
|
||||||
|
data-task-id="<?php echo esc_attr($task_id); ?>">
|
||||||
|
<?php _e('Request Optimization', 'igny8-bridge'); ?>
|
||||||
|
</button>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<div id="igny8-optimizer-message" class="igny8-message" style="display: none;"></div>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch Planner brief (AJAX handler)
|
||||||
|
*/
|
||||||
|
public static function fetch_planner_brief() {
|
||||||
|
check_ajax_referer('igny8_post_editor_nonce', 'nonce');
|
||||||
|
|
||||||
|
if (!current_user_can('edit_posts')) {
|
||||||
|
wp_send_json_error(array('message' => 'Unauthorized'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
wp_send_json_error(array('message' => 'Connection is disabled. Enable sync operations first.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
|
||||||
|
$task_id = isset($_POST['task_id']) ? intval($_POST['task_id']) : 0;
|
||||||
|
|
||||||
|
if (!$post_id || !$task_id) {
|
||||||
|
wp_send_json_error(array('message' => 'Invalid post ID or task ID'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
wp_send_json_error(array('message' => 'Not authenticated'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to fetch from Planner first
|
||||||
|
$response = $api->get("/planner/tasks/{$task_id}/brief/");
|
||||||
|
|
||||||
|
if (!$response['success']) {
|
||||||
|
// Fallback to Writer brief
|
||||||
|
$response = $api->get("/writer/tasks/{$task_id}/brief/");
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($response['success'] && !empty($response['data'])) {
|
||||||
|
update_post_meta($post_id, '_igny8_task_brief', $response['data']);
|
||||||
|
update_post_meta($post_id, '_igny8_brief_cached_at', current_time('mysql'));
|
||||||
|
|
||||||
|
wp_send_json_success(array(
|
||||||
|
'message' => 'Brief fetched successfully',
|
||||||
|
'brief' => $response['data']
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
wp_send_json_error(array(
|
||||||
|
'message' => 'Failed to fetch brief: ' . ($response['error'] ?? 'Unknown error')
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refresh Planner task (AJAX handler)
|
||||||
|
*/
|
||||||
|
public static function refresh_planner_task() {
|
||||||
|
check_ajax_referer('igny8_post_editor_nonce', 'nonce');
|
||||||
|
|
||||||
|
if (!current_user_can('edit_posts')) {
|
||||||
|
wp_send_json_error(array('message' => 'Unauthorized'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
wp_send_json_error(array('message' => 'Connection is disabled. Enable sync operations first.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
|
||||||
|
$task_id = isset($_POST['task_id']) ? intval($_POST['task_id']) : 0;
|
||||||
|
|
||||||
|
if (!$post_id || !$task_id) {
|
||||||
|
wp_send_json_error(array('message' => 'Invalid post ID or task ID'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
wp_send_json_error(array('message' => 'Not authenticated'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = $api->post("/planner/tasks/{$task_id}/refresh/", array(
|
||||||
|
'wordpress_post_id' => $post_id,
|
||||||
|
'reason' => 'reoptimize',
|
||||||
|
'notes' => 'Requested refresh from WordPress editor'
|
||||||
|
));
|
||||||
|
|
||||||
|
if ($response['success']) {
|
||||||
|
wp_send_json_success(array(
|
||||||
|
'message' => 'Refresh requested successfully',
|
||||||
|
'data' => $response['data']
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
wp_send_json_error(array(
|
||||||
|
'message' => 'Failed to request refresh: ' . ($response['error'] ?? 'Unknown error')
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create Optimizer job (AJAX handler)
|
||||||
|
*/
|
||||||
|
public static function create_optimizer_job() {
|
||||||
|
check_ajax_referer('igny8_post_editor_nonce', 'nonce');
|
||||||
|
|
||||||
|
if (!current_user_can('edit_posts')) {
|
||||||
|
wp_send_json_error(array('message' => 'Unauthorized'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
wp_send_json_error(array('message' => 'Connection is disabled. Enable sync operations first.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
|
||||||
|
$task_id = isset($_POST['task_id']) ? intval($_POST['task_id']) : 0;
|
||||||
|
$job_type = isset($_POST['job_type']) ? sanitize_text_field($_POST['job_type']) : 'audit';
|
||||||
|
$priority = isset($_POST['priority']) ? sanitize_text_field($_POST['priority']) : 'normal';
|
||||||
|
|
||||||
|
if (!$post_id || !$task_id) {
|
||||||
|
wp_send_json_error(array('message' => 'Invalid post ID or task ID'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
wp_send_json_error(array('message' => 'Not authenticated'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = $api->post("/optimizer/jobs/", array(
|
||||||
|
'post_id' => $post_id,
|
||||||
|
'task_id' => $task_id,
|
||||||
|
'job_type' => $job_type,
|
||||||
|
'priority' => $priority
|
||||||
|
));
|
||||||
|
|
||||||
|
if ($response['success'] && !empty($response['data'])) {
|
||||||
|
$job_id = $response['data']['id'] ?? $response['data']['job_id'] ?? null;
|
||||||
|
|
||||||
|
if ($job_id) {
|
||||||
|
update_post_meta($post_id, '_igny8_optimizer_job_id', $job_id);
|
||||||
|
update_post_meta($post_id, '_igny8_optimizer_status', $response['data']['status'] ?? 'pending');
|
||||||
|
update_post_meta($post_id, '_igny8_optimizer_job_created_at', current_time('mysql'));
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_send_json_success(array(
|
||||||
|
'message' => 'Optimizer job created successfully',
|
||||||
|
'job_id' => $job_id,
|
||||||
|
'data' => $response['data']
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
wp_send_json_error(array(
|
||||||
|
'message' => 'Failed to create optimizer job: ' . ($response['error'] ?? 'Unknown error')
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Optimizer job status (AJAX handler)
|
||||||
|
*/
|
||||||
|
public static function get_optimizer_status() {
|
||||||
|
check_ajax_referer('igny8_post_editor_nonce', 'nonce');
|
||||||
|
|
||||||
|
if (!current_user_can('edit_posts')) {
|
||||||
|
wp_send_json_error(array('message' => 'Unauthorized'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
wp_send_json_error(array('message' => 'Connection is disabled. Enable sync operations first.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
|
||||||
|
$job_id = isset($_POST['job_id']) ? intval($_POST['job_id']) : 0;
|
||||||
|
|
||||||
|
if (!$post_id || !$job_id) {
|
||||||
|
wp_send_json_error(array('message' => 'Invalid post ID or job ID'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
wp_send_json_error(array('message' => 'Not authenticated'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = $api->get("/optimizer/jobs/{$job_id}/");
|
||||||
|
|
||||||
|
if ($response['success'] && !empty($response['data'])) {
|
||||||
|
$status = $response['data']['status'] ?? 'unknown';
|
||||||
|
update_post_meta($post_id, '_igny8_optimizer_status', $status);
|
||||||
|
|
||||||
|
if (!empty($response['data']['score_changes'])) {
|
||||||
|
update_post_meta($post_id, '_igny8_optimizer_score_changes', $response['data']['score_changes']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($response['data']['recommendations'])) {
|
||||||
|
update_post_meta($post_id, '_igny8_optimizer_recommendations', $response['data']['recommendations']);
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_send_json_success(array(
|
||||||
|
'message' => 'Status retrieved successfully',
|
||||||
|
'status' => $status,
|
||||||
|
'data' => $response['data']
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
wp_send_json_error(array(
|
||||||
|
'message' => 'Failed to get status: ' . ($response['error'] ?? 'Unknown error')
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize
|
||||||
|
new Igny8PostMetaBoxes();
|
||||||
|
|
||||||
532
igny8-wp-integration-plugin/admin/settings.php
Normal file
532
igny8-wp-integration-plugin/admin/settings.php
Normal file
@@ -0,0 +1,532 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Settings Page Template
|
||||||
|
*
|
||||||
|
* @package Igny8Bridge
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Prevent direct access
|
||||||
|
if (!defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get current settings
|
||||||
|
$email = get_option('igny8_email', '');
|
||||||
|
$site_id = get_option('igny8_site_id', '');
|
||||||
|
$access_token = function_exists('igny8_get_secure_option') ? igny8_get_secure_option('igny8_access_token') : get_option('igny8_access_token');
|
||||||
|
$is_connected = !empty($access_token);
|
||||||
|
$date_format = get_option('date_format');
|
||||||
|
$time_format = get_option('time_format');
|
||||||
|
$now = current_time('timestamp');
|
||||||
|
$token_issued = intval(get_option('igny8_access_token_issued', 0));
|
||||||
|
$token_age_text = $token_issued ? sprintf(__('%s ago', 'igny8-bridge'), human_time_diff($token_issued, $now)) : __('Not generated yet', 'igny8-bridge');
|
||||||
|
$token_issued_formatted = $token_issued ? date_i18n($date_format . ' ' . $time_format, $token_issued) : __('—', 'igny8-bridge');
|
||||||
|
$last_health_check = intval(get_option('igny8_last_api_health_check', 0));
|
||||||
|
$last_health_check_formatted = $last_health_check ? date_i18n($date_format . ' ' . $time_format, $last_health_check) : __('Never', 'igny8-bridge');
|
||||||
|
$last_site_sync = intval(get_option('igny8_last_site_sync', 0));
|
||||||
|
$last_site_sync_formatted = $last_site_sync ? date_i18n($date_format . ' ' . $time_format, $last_site_sync) : __('Never', 'igny8-bridge');
|
||||||
|
$last_taxonomy_sync = intval(get_option('igny8_last_taxonomy_sync', 0));
|
||||||
|
$last_taxonomy_sync_formatted = $last_taxonomy_sync ? date_i18n($date_format . ' ' . $time_format, $last_taxonomy_sync) : __('Never', 'igny8-bridge');
|
||||||
|
$last_keyword_sync = intval(get_option('igny8_last_keyword_sync', 0));
|
||||||
|
$last_keyword_sync_formatted = $last_keyword_sync ? date_i18n($date_format . ' ' . $time_format, $last_keyword_sync) : __('Never', 'igny8-bridge');
|
||||||
|
$last_writer_sync = intval(get_option('igny8_last_writer_sync', 0));
|
||||||
|
$last_writer_sync_formatted = $last_writer_sync ? date_i18n($date_format . ' ' . $time_format, $last_writer_sync) : __('Never', 'igny8-bridge');
|
||||||
|
$last_full_site_scan = intval(get_option('igny8_last_full_site_scan', 0));
|
||||||
|
$last_full_site_scan_formatted = $last_full_site_scan ? date_i18n($date_format . ' ' . $time_format, $last_full_site_scan) : __('Never', 'igny8-bridge');
|
||||||
|
$last_semantic_map = intval(get_option('igny8_last_semantic_map', 0));
|
||||||
|
$last_semantic_map_formatted = $last_semantic_map ? date_i18n($date_format . ' ' . $time_format, $last_semantic_map) : __('Never', 'igny8-bridge');
|
||||||
|
$semantic_summary = get_option('igny8_last_semantic_map_summary', array());
|
||||||
|
$next_site_sync = wp_next_scheduled('igny8_sync_site_data');
|
||||||
|
$next_site_sync_formatted = $next_site_sync ? date_i18n($date_format . ' ' . $time_format, $next_site_sync) : __('Not scheduled', 'igny8-bridge');
|
||||||
|
$available_post_types = igny8_get_supported_post_types();
|
||||||
|
$enabled_post_types = igny8_get_enabled_post_types();
|
||||||
|
$control_mode = igny8_get_control_mode();
|
||||||
|
$woocommerce_enabled = (int) get_option('igny8_enable_woocommerce', class_exists('WooCommerce') ? 1 : 0);
|
||||||
|
$woocommerce_detected = class_exists('WooCommerce');
|
||||||
|
$available_modules = igny8_get_available_modules();
|
||||||
|
$enabled_modules = igny8_get_enabled_modules();
|
||||||
|
$connection_enabled = igny8_is_connection_enabled();
|
||||||
|
$webhook_secret = igny8_get_webhook_secret();
|
||||||
|
$webhook_url = rest_url('igny8/v1/event');
|
||||||
|
$link_queue = get_option('igny8_link_queue', array());
|
||||||
|
$pending_links = array_filter($link_queue, function($item) {
|
||||||
|
return $item['status'] === 'pending';
|
||||||
|
});
|
||||||
|
$webhook_logs = igny8_get_webhook_logs(array('limit' => 10));
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
||||||
|
<div class="wrap">
|
||||||
|
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
|
||||||
|
|
||||||
|
<?php settings_errors('igny8_settings'); ?>
|
||||||
|
|
||||||
|
<div class="igny8-settings-container">
|
||||||
|
<div class="igny8-settings-card">
|
||||||
|
<h2><?php _e('API Connection', 'igny8-bridge'); ?></h2>
|
||||||
|
|
||||||
|
<form method="post" action="">
|
||||||
|
<?php wp_nonce_field('igny8_settings_nonce'); ?>
|
||||||
|
|
||||||
|
<table class="form-table">
|
||||||
|
<tr>
|
||||||
|
<th scope="row">
|
||||||
|
<label for="igny8_email"><?php _e('Email', 'igny8-bridge'); ?></label>
|
||||||
|
</th>
|
||||||
|
<td>
|
||||||
|
<input
|
||||||
|
type="email"
|
||||||
|
id="igny8_email"
|
||||||
|
name="igny8_email"
|
||||||
|
value="<?php echo esc_attr($email); ?>"
|
||||||
|
class="regular-text"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
<p class="description">
|
||||||
|
<?php _e('Your IGNY8 account email address.', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th scope="row">
|
||||||
|
<label for="igny8_password"><?php _e('Password', 'igny8-bridge'); ?></label>
|
||||||
|
</th>
|
||||||
|
<td>
|
||||||
|
<input
|
||||||
|
type="password"
|
||||||
|
id="igny8_password"
|
||||||
|
name="igny8_password"
|
||||||
|
class="regular-text"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
<p class="description">
|
||||||
|
<?php _e('Your IGNY8 account password.', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<?php submit_button(__('Connect to IGNY8', 'igny8-bridge'), 'primary', 'igny8_connect'); ?>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php if ($is_connected) : ?>
|
||||||
|
<div class="igny8-settings-card">
|
||||||
|
<h2><?php _e('Connection Status', 'igny8-bridge'); ?></h2>
|
||||||
|
|
||||||
|
<form method="post" action="options.php">
|
||||||
|
<?php settings_fields('igny8_bridge_connection'); ?>
|
||||||
|
|
||||||
|
<table class="form-table">
|
||||||
|
<tr>
|
||||||
|
<th scope="row"><?php _e('Connection Status', 'igny8-bridge'); ?></th>
|
||||||
|
<td>
|
||||||
|
<?php if ($connection_enabled) : ?>
|
||||||
|
<span class="igny8-status-connected">
|
||||||
|
<?php _e('Connected & Active', 'igny8-bridge'); ?>
|
||||||
|
</span>
|
||||||
|
<?php else : ?>
|
||||||
|
<span class="igny8-status-disconnected">
|
||||||
|
<?php _e('Connected but Disabled', 'igny8-bridge'); ?>
|
||||||
|
</span>
|
||||||
|
<?php endif; ?>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th scope="row">
|
||||||
|
<label for="igny8_connection_enabled"><?php _e('Enable Sync Operations', 'igny8-bridge'); ?></label>
|
||||||
|
</th>
|
||||||
|
<td>
|
||||||
|
<label>
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
id="igny8_connection_enabled"
|
||||||
|
name="igny8_connection_enabled"
|
||||||
|
value="1"
|
||||||
|
<?php checked($connection_enabled, 1); ?>
|
||||||
|
/>
|
||||||
|
<?php _e('Allow sending and receiving data from IGNY8 SaaS', 'igny8-bridge'); ?>
|
||||||
|
</label>
|
||||||
|
<p class="description">
|
||||||
|
<?php _e('When disabled, all sync operations are stopped but your credentials and settings are preserved.', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<?php if ($email) : ?>
|
||||||
|
<tr>
|
||||||
|
<th scope="row"><?php _e('Email', 'igny8-bridge'); ?></th>
|
||||||
|
<td><?php echo esc_html($email); ?></td>
|
||||||
|
</tr>
|
||||||
|
<?php endif; ?>
|
||||||
|
<?php if ($site_id) : ?>
|
||||||
|
<tr>
|
||||||
|
<th scope="row"><?php _e('Site ID', 'igny8-bridge'); ?></th>
|
||||||
|
<td><?php echo esc_html($site_id); ?></td>
|
||||||
|
</tr>
|
||||||
|
<?php endif; ?>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<?php submit_button(__('Save Connection Settings', 'igny8-bridge')); ?>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<button type="button" id="igny8-test-connection" class="button" <?php disabled(!$connection_enabled); ?>>
|
||||||
|
<?php _e('Test Connection', 'igny8-bridge'); ?>
|
||||||
|
</button>
|
||||||
|
<span id="igny8-test-result" class="igny8-test-result"></span>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="igny8-settings-card">
|
||||||
|
<h2><?php _e('Diagnostics', 'igny8-bridge'); ?></h2>
|
||||||
|
<div class="igny8-diagnostics-grid">
|
||||||
|
<div class="igny8-diagnostic-item">
|
||||||
|
<div class="igny8-diagnostic-label"><?php _e('Access Token Age', 'igny8-bridge'); ?></div>
|
||||||
|
<div class="igny8-diagnostic-value"><?php echo esc_html($token_age_text); ?></div>
|
||||||
|
<p class="description">
|
||||||
|
<?php echo esc_html($token_issued_formatted); ?>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="igny8-diagnostic-item">
|
||||||
|
<div class="igny8-diagnostic-label"><?php _e('Last API Health Check', 'igny8-bridge'); ?></div>
|
||||||
|
<div class="igny8-diagnostic-value"><?php echo esc_html($last_health_check_formatted); ?></div>
|
||||||
|
<p class="description">
|
||||||
|
<?php _e('Updated when tests succeed', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="igny8-diagnostic-item">
|
||||||
|
<div class="igny8-diagnostic-label"><?php _e('Last Site Data Sync', 'igny8-bridge'); ?></div>
|
||||||
|
<div class="igny8-diagnostic-value"><?php echo esc_html($last_site_sync_formatted); ?></div>
|
||||||
|
<p class="description">
|
||||||
|
<?php _e('Triggered automatically after setup', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="igny8-diagnostic-item">
|
||||||
|
<div class="igny8-diagnostic-label"><?php _e('Last Full Site Scan', 'igny8-bridge'); ?></div>
|
||||||
|
<div class="igny8-diagnostic-value"><?php echo esc_html($last_full_site_scan_formatted); ?></div>
|
||||||
|
<p class="description">
|
||||||
|
<?php _e('Runs weekly or when requested manually', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="igny8-diagnostic-item">
|
||||||
|
<div class="igny8-diagnostic-label"><?php _e('Last Semantic Map', 'igny8-bridge'); ?></div>
|
||||||
|
<div class="igny8-diagnostic-value"><?php echo esc_html($last_semantic_map_formatted); ?></div>
|
||||||
|
<p class="description">
|
||||||
|
<?php
|
||||||
|
if (!empty($semantic_summary)) {
|
||||||
|
printf(
|
||||||
|
/* translators: %1$d: sectors count, %2$d: keywords count */
|
||||||
|
esc_html__('Sectors: %1$d · Keywords: %2$d', 'igny8-bridge'),
|
||||||
|
intval($semantic_summary['sectors'] ?? 0),
|
||||||
|
intval($semantic_summary['keywords'] ?? 0)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
_e('Planner semantic map generated after full scan', 'igny8-bridge');
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="igny8-diagnostic-item">
|
||||||
|
<div class="igny8-diagnostic-label"><?php _e('Last Taxonomy Sync', 'igny8-bridge'); ?></div>
|
||||||
|
<div class="igny8-diagnostic-value"><?php echo esc_html($last_taxonomy_sync_formatted); ?></div>
|
||||||
|
<p class="description">
|
||||||
|
<?php _e('Sectors & clusters imported from Planner', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="igny8-diagnostic-item">
|
||||||
|
<div class="igny8-diagnostic-label"><?php _e('Last Keyword Sync', 'igny8-bridge'); ?></div>
|
||||||
|
<div class="igny8-diagnostic-value"><?php echo esc_html($last_keyword_sync_formatted); ?></div>
|
||||||
|
<p class="description">
|
||||||
|
<?php _e('Planner keywords cached to WordPress posts', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="igny8-diagnostic-item">
|
||||||
|
<div class="igny8-diagnostic-label"><?php _e('Last Writer Sync', 'igny8-bridge'); ?></div>
|
||||||
|
<div class="igny8-diagnostic-value"><?php echo esc_html($last_writer_sync_formatted); ?></div>
|
||||||
|
<p class="description">
|
||||||
|
<?php _e('New IGNY8 tasks imported automatically', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="igny8-diagnostic-item">
|
||||||
|
<div class="igny8-diagnostic-label"><?php _e('Next Scheduled Site Scan', 'igny8-bridge'); ?></div>
|
||||||
|
<div class="igny8-diagnostic-value"><?php echo esc_html($next_site_sync_formatted); ?></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<?php else : ?>
|
||||||
|
<div class="igny8-settings-card">
|
||||||
|
<h2><?php _e('Connection Status', 'igny8-bridge'); ?></h2>
|
||||||
|
<p>
|
||||||
|
<span class="igny8-status-disconnected">
|
||||||
|
<?php _e('Not Connected', 'igny8-bridge'); ?>
|
||||||
|
</span>
|
||||||
|
</p>
|
||||||
|
<p class="description">
|
||||||
|
<?php _e('Enter your IGNY8 credentials above and click "Connect to IGNY8" to establish a connection.', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<div class="igny8-settings-card">
|
||||||
|
<h2><?php _e('Automation Settings', 'igny8-bridge'); ?></h2>
|
||||||
|
|
||||||
|
<form method="post" action="options.php">
|
||||||
|
<?php settings_fields('igny8_bridge_controls'); ?>
|
||||||
|
|
||||||
|
<table class="form-table">
|
||||||
|
<tr>
|
||||||
|
<th scope="row"><?php _e('Post Types to Sync', 'igny8-bridge'); ?></th>
|
||||||
|
<td>
|
||||||
|
<?php foreach ($available_post_types as $slug => $label) : ?>
|
||||||
|
<label>
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
name="igny8_enabled_post_types[]"
|
||||||
|
value="<?php echo esc_attr($slug); ?>"
|
||||||
|
<?php checked(in_array($slug, $enabled_post_types, true)); ?>
|
||||||
|
/>
|
||||||
|
<?php echo esc_html($label); ?>
|
||||||
|
</label>
|
||||||
|
<br />
|
||||||
|
<?php endforeach; ?>
|
||||||
|
<p class="description">
|
||||||
|
<?php _e('Select the content types IGNY8 should manage automatically.', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th scope="row"><?php _e('IGNY8 Modules', 'igny8-bridge'); ?></th>
|
||||||
|
<td>
|
||||||
|
<?php foreach ($available_modules as $module_key => $module_label) : ?>
|
||||||
|
<label>
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
name="igny8_enabled_modules[]"
|
||||||
|
value="<?php echo esc_attr($module_key); ?>"
|
||||||
|
<?php checked(in_array($module_key, $enabled_modules, true)); ?>
|
||||||
|
/>
|
||||||
|
<?php echo esc_html($module_label); ?>
|
||||||
|
</label>
|
||||||
|
<br />
|
||||||
|
<?php endforeach; ?>
|
||||||
|
<p class="description">
|
||||||
|
<?php _e('Disable modules temporarily if a feature is not ready in the SaaS app.', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th scope="row"><?php _e('Control Mode', 'igny8-bridge'); ?></th>
|
||||||
|
<td>
|
||||||
|
<label>
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
name="igny8_control_mode"
|
||||||
|
value="mirror"
|
||||||
|
<?php checked($control_mode, 'mirror'); ?>
|
||||||
|
/>
|
||||||
|
<strong><?php _e('Mirror', 'igny8-bridge'); ?></strong>
|
||||||
|
<span class="description"><?php _e('IGNY8 is the source of truth; WordPress reflects changes only.', 'igny8-bridge'); ?></span>
|
||||||
|
</label>
|
||||||
|
<br />
|
||||||
|
<label>
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
name="igny8_control_mode"
|
||||||
|
value="hybrid"
|
||||||
|
<?php checked($control_mode, 'hybrid'); ?>
|
||||||
|
/>
|
||||||
|
<strong><?php _e('Hybrid', 'igny8-bridge'); ?></strong>
|
||||||
|
<span class="description"><?php _e('Allow editors to update content in WordPress and sync back to IGNY8.', 'igny8-bridge'); ?></span>
|
||||||
|
</label>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th scope="row"><?php _e('WooCommerce Data', 'igny8-bridge'); ?></th>
|
||||||
|
<td>
|
||||||
|
<label>
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
name="igny8_enable_woocommerce"
|
||||||
|
value="1"
|
||||||
|
<?php checked($woocommerce_enabled, 1); ?>
|
||||||
|
<?php disabled(!$woocommerce_detected); ?>
|
||||||
|
/>
|
||||||
|
<?php _e('Include products, categories, and inventory during site scans.', 'igny8-bridge'); ?>
|
||||||
|
</label>
|
||||||
|
<?php if (!$woocommerce_detected) : ?>
|
||||||
|
<p class="description">
|
||||||
|
<?php _e('WooCommerce is not active on this site. Enable the plugin to sync product data.', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
<?php endif; ?>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<?php submit_button(__('Save Automation Settings', 'igny8-bridge')); ?>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<p class="description">
|
||||||
|
<?php _e('Once these settings are saved, the bridge schedules automatic jobs that keep IGNY8 in sync without manual actions.', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php if ($is_connected) : ?>
|
||||||
|
<div class="igny8-settings-card">
|
||||||
|
<h2><?php _e('Webhook Configuration', 'igny8-bridge'); ?></h2>
|
||||||
|
|
||||||
|
<table class="form-table">
|
||||||
|
<tr>
|
||||||
|
<th scope="row"><?php _e('Webhook URL', 'igny8-bridge'); ?></th>
|
||||||
|
<td>
|
||||||
|
<code><?php echo esc_html($webhook_url); ?></code>
|
||||||
|
<button type="button" class="button button-small" onclick="navigator.clipboard.writeText('<?php echo esc_js($webhook_url); ?>'); alert('<?php _e('Webhook URL copied to clipboard', 'igny8-bridge'); ?>');">
|
||||||
|
<?php _e('Copy', 'igny8-bridge'); ?>
|
||||||
|
</button>
|
||||||
|
<p class="description">
|
||||||
|
<?php _e('Configure this URL in your IGNY8 SaaS app settings.', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th scope="row"><?php _e('Webhook Secret', 'igny8-bridge'); ?></th>
|
||||||
|
<td>
|
||||||
|
<code style="word-break: break-all;"><?php echo esc_html($webhook_secret); ?></code>
|
||||||
|
<button type="button" class="button button-small" onclick="navigator.clipboard.writeText('<?php echo esc_js($webhook_secret); ?>'); alert('<?php _e('Secret copied to clipboard', 'igny8-bridge'); ?>');">
|
||||||
|
<?php _e('Copy', 'igny8-bridge'); ?>
|
||||||
|
</button>
|
||||||
|
<form method="post" action="" style="display: inline-block; margin-left: 10px;">
|
||||||
|
<?php wp_nonce_field('igny8_regenerate_secret'); ?>
|
||||||
|
<button type="submit" name="igny8_regenerate_secret" class="button button-small" onclick="return confirm('<?php _e('Are you sure? You will need to update the secret in IGNY8 SaaS app.', 'igny8-bridge'); ?>');">
|
||||||
|
<?php _e('Regenerate', 'igny8-bridge'); ?>
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
<p class="description">
|
||||||
|
<?php _e('Use this secret to verify webhook requests in IGNY8 SaaS app. Keep it secure.', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="igny8-settings-card">
|
||||||
|
<h2><?php _e('Link Queue', 'igny8-bridge'); ?></h2>
|
||||||
|
|
||||||
|
<?php if (!empty($pending_links)) : ?>
|
||||||
|
<p>
|
||||||
|
<strong><?php echo count($pending_links); ?></strong> <?php _e('link(s) pending insertion', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
<table class="wp-list-table widefat fixed striped">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('Post ID', 'igny8-bridge'); ?></th>
|
||||||
|
<th><?php _e('Anchor', 'igny8-bridge'); ?></th>
|
||||||
|
<th><?php _e('Target URL', 'igny8-bridge'); ?></th>
|
||||||
|
<th><?php _e('Status', 'igny8-bridge'); ?></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<?php foreach (array_slice($pending_links, 0, 10) as $link) : ?>
|
||||||
|
<tr>
|
||||||
|
<td><?php echo esc_html($link['post_id']); ?></td>
|
||||||
|
<td><?php echo esc_html($link['anchor']); ?></td>
|
||||||
|
<td><code><?php echo esc_html($link['target_url']); ?></code></td>
|
||||||
|
<td><?php echo esc_html($link['status']); ?></td>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<?php else : ?>
|
||||||
|
<p><?php _e('No pending links in queue.', 'igny8-bridge'); ?></p>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="igny8-settings-card">
|
||||||
|
<h2><?php _e('Recent Webhook Activity', 'igny8-bridge'); ?></h2>
|
||||||
|
|
||||||
|
<?php if (!empty($webhook_logs)) : ?>
|
||||||
|
<table class="wp-list-table widefat fixed striped">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('Event', 'igny8-bridge'); ?></th>
|
||||||
|
<th><?php _e('Status', 'igny8-bridge'); ?></th>
|
||||||
|
<th><?php _e('Time', 'igny8-bridge'); ?></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<?php foreach ($webhook_logs as $log) : ?>
|
||||||
|
<tr>
|
||||||
|
<td><?php echo esc_html($log['event']); ?></td>
|
||||||
|
<td>
|
||||||
|
<span class="igny8-status-badge igny8-status-<?php echo esc_attr(strtolower($log['status'])); ?>">
|
||||||
|
<?php echo esc_html($log['status']); ?>
|
||||||
|
</span>
|
||||||
|
</td>
|
||||||
|
<td><?php echo esc_html(date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($log['received_at']))); ?></td>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<?php else : ?>
|
||||||
|
<p><?php _e('No webhook activity yet.', 'igny8-bridge'); ?></p>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<div class="igny8-settings-card">
|
||||||
|
<h2><?php _e('About', 'igny8-bridge'); ?></h2>
|
||||||
|
<p>
|
||||||
|
<?php _e('The IGNY8 WordPress Bridge plugin connects your WordPress site to the IGNY8 API, enabling two-way synchronization of posts, taxonomies, and site data.', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<strong><?php _e('Version:', 'igny8-bridge'); ?></strong> <?php echo esc_html(IGNY8_BRIDGE_VERSION); ?>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php if ($is_connected) : ?>
|
||||||
|
<div class="igny8-settings-card">
|
||||||
|
<h2><?php _e('Sync Operations', 'igny8-bridge'); ?></h2>
|
||||||
|
|
||||||
|
<?php if (!$connection_enabled) : ?>
|
||||||
|
<div class="notice notice-warning inline">
|
||||||
|
<p>
|
||||||
|
<strong><?php _e('Connection Disabled', 'igny8-bridge'); ?></strong><br />
|
||||||
|
<?php _e('Sync operations are currently disabled. Enable "Enable Sync Operations" above to use these features.', 'igny8-bridge'); ?>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<div class="igny8-sync-actions">
|
||||||
|
<button type="button" id="igny8-sync-posts" class="button button-secondary" <?php disabled(!$connection_enabled); ?>>
|
||||||
|
<?php _e('Sync Posts to IGNY8', 'igny8-bridge'); ?>
|
||||||
|
</button>
|
||||||
|
<button type="button" id="igny8-sync-taxonomies" class="button button-secondary" <?php disabled(!$connection_enabled); ?>>
|
||||||
|
<?php _e('Sync Taxonomies', 'igny8-bridge'); ?>
|
||||||
|
</button>
|
||||||
|
<button type="button" id="igny8-sync-from-igny8" class="button button-secondary" <?php disabled(!$connection_enabled); ?>>
|
||||||
|
<?php _e('Sync from IGNY8', 'igny8-bridge'); ?>
|
||||||
|
</button>
|
||||||
|
<button type="button" id="igny8-collect-site-data" class="button button-primary" <?php disabled(!$connection_enabled); ?>>
|
||||||
|
<?php _e('Collect & Send Site Data', 'igny8-bridge'); ?>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="igny8-sync-status" class="igny8-sync-status"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="igny8-settings-card">
|
||||||
|
<h2><?php _e('Sync Statistics', 'igny8-bridge'); ?></h2>
|
||||||
|
|
||||||
|
<div class="igny8-stats-grid">
|
||||||
|
<div class="igny8-stat-item">
|
||||||
|
<div class="igny8-stat-label"><?php _e('Synced Posts', 'igny8-bridge'); ?></div>
|
||||||
|
<div class="igny8-stat-value" id="igny8-stat-posts">-</div>
|
||||||
|
</div>
|
||||||
|
<div class="igny8-stat-item">
|
||||||
|
<div class="igny8-stat-label"><?php _e('Last Sync', 'igny8-bridge'); ?></div>
|
||||||
|
<div class="igny8-stat-value" id="igny8-stat-last-sync">-</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
192
igny8-wp-integration-plugin/data/link-graph.php
Normal file
192
igny8-wp-integration-plugin/data/link-graph.php
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Link Graph Collection
|
||||||
|
*
|
||||||
|
* Extracts WordPress internal link graph for IGNY8 Linker module
|
||||||
|
*
|
||||||
|
* @package Igny8Bridge
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Prevent direct access
|
||||||
|
if (!defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract internal links from post content
|
||||||
|
*
|
||||||
|
* @param int $post_id Post ID
|
||||||
|
* @return array Array of link objects with source_url, target_url, anchor
|
||||||
|
*/
|
||||||
|
function igny8_extract_post_links($post_id) {
|
||||||
|
$post = get_post($post_id);
|
||||||
|
if (!$post) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
|
$content = $post->post_content;
|
||||||
|
$source_url = get_permalink($post_id);
|
||||||
|
$site_url = get_site_url();
|
||||||
|
$links = array();
|
||||||
|
|
||||||
|
// Match all anchor tags with href attributes
|
||||||
|
preg_match_all('/<a[^>]+href=["\']([^"\']+)["\'][^>]*>(.*?)<\/a>/is', $content, $matches, PREG_SET_ORDER);
|
||||||
|
|
||||||
|
foreach ($matches as $match) {
|
||||||
|
$href = $match[1];
|
||||||
|
$anchor = strip_tags($match[2]);
|
||||||
|
|
||||||
|
// Skip empty anchors
|
||||||
|
if (empty(trim($anchor))) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only process internal links
|
||||||
|
if (strpos($href, $site_url) === 0 || strpos($href, '/') === 0) {
|
||||||
|
// Convert relative URLs to absolute
|
||||||
|
if (strpos($href, '/') === 0 && strpos($href, '//') !== 0) {
|
||||||
|
$href = $site_url . $href;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normalize URL (remove trailing slash, fragments, query params for matching)
|
||||||
|
$target_url = rtrim($href, '/');
|
||||||
|
|
||||||
|
// Skip if source and target are the same
|
||||||
|
if ($source_url === $target_url) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$links[] = array(
|
||||||
|
'source_url' => $source_url,
|
||||||
|
'target_url' => $target_url,
|
||||||
|
'anchor' => trim($anchor),
|
||||||
|
'post_id' => $post_id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $links;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract link graph from all posts
|
||||||
|
*
|
||||||
|
* @param array $post_ids Optional array of post IDs to process. If empty, processes all enabled posts.
|
||||||
|
* @return array Link graph array
|
||||||
|
*/
|
||||||
|
function igny8_extract_link_graph($post_ids = array()) {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (function_exists('igny8_is_module_enabled') && !igny8_is_module_enabled('linker')) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
|
$enabled_post_types = igny8_get_enabled_post_types();
|
||||||
|
|
||||||
|
if (empty($post_ids)) {
|
||||||
|
// Get all published posts of enabled types
|
||||||
|
$query_args = array(
|
||||||
|
'post_type' => $enabled_post_types,
|
||||||
|
'post_status' => 'publish',
|
||||||
|
'posts_per_page' => -1,
|
||||||
|
'fields' => 'ids',
|
||||||
|
'suppress_filters' => true
|
||||||
|
);
|
||||||
|
|
||||||
|
$post_ids = get_posts($query_args);
|
||||||
|
}
|
||||||
|
|
||||||
|
$link_graph = array();
|
||||||
|
$processed = 0;
|
||||||
|
|
||||||
|
foreach ($post_ids as $post_id) {
|
||||||
|
$links = igny8_extract_post_links($post_id);
|
||||||
|
|
||||||
|
if (!empty($links)) {
|
||||||
|
$link_graph = array_merge($link_graph, $links);
|
||||||
|
}
|
||||||
|
|
||||||
|
$processed++;
|
||||||
|
|
||||||
|
// Limit processing to prevent timeout (can be increased or made configurable)
|
||||||
|
if ($processed >= 1000) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $link_graph;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send link graph to IGNY8 Linker module
|
||||||
|
*
|
||||||
|
* @param int $site_id IGNY8 site ID
|
||||||
|
* @param array $link_graph Link graph array (optional, will extract if not provided)
|
||||||
|
* @return array|false Response data or false on failure
|
||||||
|
*/
|
||||||
|
function igny8_send_link_graph_to_igny8($site_id, $link_graph = null) {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (function_exists('igny8_is_module_enabled') && !igny8_is_module_enabled('linker')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract link graph if not provided
|
||||||
|
if ($link_graph === null) {
|
||||||
|
$link_graph = igny8_extract_link_graph();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($link_graph)) {
|
||||||
|
return array('success' => true, 'message' => 'No links found', 'links_count' => 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send in batches (max 500 links per batch)
|
||||||
|
$batch_size = 500;
|
||||||
|
$batches = array_chunk($link_graph, $batch_size);
|
||||||
|
$total_sent = 0;
|
||||||
|
$errors = array();
|
||||||
|
|
||||||
|
foreach ($batches as $batch) {
|
||||||
|
$response = $api->post("/linker/link-map/", array(
|
||||||
|
'site_id' => $site_id,
|
||||||
|
'links' => $batch,
|
||||||
|
'total_links' => count($link_graph),
|
||||||
|
'batch_number' => count($batches) > 1 ? (count($batches) - count($batches) + array_search($batch, $batches) + 1) : 1,
|
||||||
|
'total_batches' => count($batches)
|
||||||
|
));
|
||||||
|
|
||||||
|
if ($response['success']) {
|
||||||
|
$total_sent += count($batch);
|
||||||
|
} else {
|
||||||
|
$errors[] = $response['error'] ?? 'Unknown error';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($total_sent > 0) {
|
||||||
|
update_option('igny8_last_link_graph_sync', current_time('timestamp'));
|
||||||
|
update_option('igny8_last_link_graph_count', $total_sent);
|
||||||
|
|
||||||
|
return array(
|
||||||
|
'success' => true,
|
||||||
|
'links_sent' => $total_sent,
|
||||||
|
'total_links' => count($link_graph),
|
||||||
|
'batches' => count($batches),
|
||||||
|
'errors' => $errors
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
225
igny8-wp-integration-plugin/data/semantic-mapping.php
Normal file
225
igny8-wp-integration-plugin/data/semantic-mapping.php
Normal file
@@ -0,0 +1,225 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Semantic Strategy Mapping
|
||||||
|
*
|
||||||
|
* Maps WordPress site data to IGNY8 semantic structure
|
||||||
|
* Follows WORDPRESS-PLUGIN-INTEGRATION.md guidelines
|
||||||
|
*
|
||||||
|
* @package Igny8Bridge
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Prevent direct access
|
||||||
|
if (!defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map WordPress site data to IGNY8 semantic strategy
|
||||||
|
* This creates sectors, clusters, and keywords based on site structure
|
||||||
|
*
|
||||||
|
* @param int $site_id IGNY8 site ID
|
||||||
|
* @param array $site_data Site data from igny8_collect_site_data()
|
||||||
|
* @return array Response from IGNY8 API
|
||||||
|
*/
|
||||||
|
function igny8_map_site_to_semantic_strategy($site_id, $site_data) {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
return array('success' => false, 'error' => 'Connection disabled');
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
return array('success' => false, 'error' => 'Not authenticated');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract semantic structure from site data
|
||||||
|
$semantic_map = array(
|
||||||
|
'sectors' => array(),
|
||||||
|
'clusters' => array(),
|
||||||
|
'keywords' => array()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Map taxonomies to sectors
|
||||||
|
foreach ($site_data['taxonomies'] as $tax_name => $tax_data) {
|
||||||
|
if ($tax_data['taxonomy']['hierarchical']) {
|
||||||
|
// Hierarchical taxonomies (categories) become sectors
|
||||||
|
$sector = array(
|
||||||
|
'name' => $tax_data['taxonomy']['label'],
|
||||||
|
'slug' => $tax_data['taxonomy']['name'],
|
||||||
|
'description' => $tax_data['taxonomy']['description'],
|
||||||
|
'source' => 'wordpress_taxonomy',
|
||||||
|
'source_id' => $tax_name
|
||||||
|
);
|
||||||
|
|
||||||
|
// Map terms to clusters
|
||||||
|
$clusters = array();
|
||||||
|
foreach ($tax_data['terms'] as $term) {
|
||||||
|
$clusters[] = array(
|
||||||
|
'name' => $term['name'],
|
||||||
|
'slug' => $term['slug'],
|
||||||
|
'description' => $term['description'],
|
||||||
|
'source' => 'wordpress_term',
|
||||||
|
'source_id' => $term['id']
|
||||||
|
);
|
||||||
|
|
||||||
|
// Extract keywords from posts in this term
|
||||||
|
$keywords = igny8_extract_keywords_from_term_posts($term['id'], $tax_name);
|
||||||
|
$semantic_map['keywords'] = array_merge($semantic_map['keywords'], $keywords);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sector['clusters'] = $clusters;
|
||||||
|
$semantic_map['sectors'][] = $sector;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map WooCommerce product categories to sectors
|
||||||
|
if (!empty($site_data['product_categories'])) {
|
||||||
|
$product_sector = array(
|
||||||
|
'name' => 'Products',
|
||||||
|
'slug' => 'products',
|
||||||
|
'description' => 'WooCommerce product categories',
|
||||||
|
'source' => 'woocommerce',
|
||||||
|
'clusters' => array()
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ($site_data['product_categories'] as $category) {
|
||||||
|
$product_sector['clusters'][] = array(
|
||||||
|
'name' => $category['name'],
|
||||||
|
'slug' => $category['slug'],
|
||||||
|
'description' => $category['description'],
|
||||||
|
'source' => 'woocommerce_category',
|
||||||
|
'source_id' => $category['id']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$semantic_map['sectors'][] = $product_sector;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send semantic map to IGNY8
|
||||||
|
$response = $api->post("/planner/sites/{$site_id}/semantic-map/", array(
|
||||||
|
'semantic_map' => $semantic_map,
|
||||||
|
'site_data' => $site_data
|
||||||
|
));
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract keywords from posts associated with a taxonomy term
|
||||||
|
*
|
||||||
|
* @param int $term_id Term ID
|
||||||
|
* @param string $taxonomy Taxonomy name
|
||||||
|
* @return array Formatted keywords array
|
||||||
|
*/
|
||||||
|
function igny8_extract_keywords_from_term_posts($term_id, $taxonomy) {
|
||||||
|
$args = array(
|
||||||
|
'post_type' => 'any',
|
||||||
|
'posts_per_page' => -1,
|
||||||
|
'tax_query' => array(
|
||||||
|
array(
|
||||||
|
'taxonomy' => $taxonomy,
|
||||||
|
'field' => 'term_id',
|
||||||
|
'terms' => $term_id
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$query = new WP_Query($args);
|
||||||
|
$keywords = array();
|
||||||
|
|
||||||
|
if ($query->have_posts()) {
|
||||||
|
while ($query->have_posts()) {
|
||||||
|
$query->the_post();
|
||||||
|
|
||||||
|
// Extract keywords from post title and content
|
||||||
|
$title_words = str_word_count(get_the_title(), 1);
|
||||||
|
$content_words = str_word_count(strip_tags(get_the_content()), 1);
|
||||||
|
|
||||||
|
// Combine and get unique keywords
|
||||||
|
$all_words = array_merge($title_words, $content_words);
|
||||||
|
$unique_words = array_unique(array_map('strtolower', $all_words));
|
||||||
|
|
||||||
|
// Filter out common words (stop words)
|
||||||
|
$stop_words = array('the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by');
|
||||||
|
$keywords = array_merge($keywords, array_diff($unique_words, $stop_words));
|
||||||
|
}
|
||||||
|
wp_reset_postdata();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format keywords
|
||||||
|
$formatted_keywords = array();
|
||||||
|
foreach (array_unique($keywords) as $keyword) {
|
||||||
|
if (strlen($keyword) > 3) { // Only keywords longer than 3 characters
|
||||||
|
$formatted_keywords[] = array(
|
||||||
|
'keyword' => $keyword,
|
||||||
|
'source' => 'wordpress_post',
|
||||||
|
'source_term_id' => $term_id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $formatted_keywords;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Complete workflow: Fetch site data → Map to semantic strategy → Restructure content
|
||||||
|
*
|
||||||
|
* @param int $site_id IGNY8 site ID
|
||||||
|
* @return array|false Analysis result or false on failure
|
||||||
|
*/
|
||||||
|
function igny8_analyze_and_restructure_site($site_id) {
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 1: Collect all site data
|
||||||
|
$site_data = igny8_collect_site_data();
|
||||||
|
|
||||||
|
// Step 2: Send to IGNY8 for analysis
|
||||||
|
$analysis_response = $api->post("/system/sites/{$site_id}/analyze/", array(
|
||||||
|
'site_data' => $site_data,
|
||||||
|
'analysis_type' => 'full_site_restructure'
|
||||||
|
));
|
||||||
|
|
||||||
|
if (!$analysis_response['success']) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$analysis_id = $analysis_response['data']['analysis_id'] ?? null;
|
||||||
|
|
||||||
|
// Step 3: Map to semantic strategy
|
||||||
|
$mapping_response = igny8_map_site_to_semantic_strategy($site_id, $site_data);
|
||||||
|
|
||||||
|
if (!$mapping_response['success']) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 4: Get restructuring recommendations
|
||||||
|
$recommendations_response = $api->get("/system/sites/{$site_id}/recommendations/");
|
||||||
|
|
||||||
|
if (!$recommendations_response['success']) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get keywords count from mapping response
|
||||||
|
$keywords_count = 0;
|
||||||
|
if (isset($mapping_response['data']['keywords'])) {
|
||||||
|
$keywords_count = count($mapping_response['data']['keywords']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return array(
|
||||||
|
'analysis_id' => $analysis_id,
|
||||||
|
'semantic_map' => $mapping_response['data'] ?? null,
|
||||||
|
'recommendations' => $recommendations_response['data'] ?? null,
|
||||||
|
'site_data_summary' => array(
|
||||||
|
'total_posts' => count($site_data['posts']),
|
||||||
|
'total_taxonomies' => count($site_data['taxonomies']),
|
||||||
|
'total_products' => count($site_data['products'] ?? array()),
|
||||||
|
'total_keywords' => $keywords_count
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
579
igny8-wp-integration-plugin/data/site-collection.php
Normal file
579
igny8-wp-integration-plugin/data/site-collection.php
Normal file
@@ -0,0 +1,579 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* WordPress Site Data Collection
|
||||||
|
*
|
||||||
|
* Collects WordPress posts, taxonomies, and site data for IGNY8
|
||||||
|
* Follows WORDPRESS-PLUGIN-INTEGRATION.md guidelines
|
||||||
|
*
|
||||||
|
* @package Igny8Bridge
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Prevent direct access
|
||||||
|
if (!defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch all posts of a specific type from WordPress
|
||||||
|
*
|
||||||
|
* @param string $post_type Post type
|
||||||
|
* @param int $per_page Posts per page
|
||||||
|
* @return array|false Formatted posts array or false on failure
|
||||||
|
*/
|
||||||
|
function igny8_fetch_wordpress_posts($post_type = 'post', $per_page = 100, $args = array()) {
|
||||||
|
$defaults = array(
|
||||||
|
'status' => 'publish',
|
||||||
|
'after' => null,
|
||||||
|
'max_pages' => 5,
|
||||||
|
);
|
||||||
|
$args = wp_parse_args($args, $defaults);
|
||||||
|
|
||||||
|
$post_type_object = get_post_type_object($post_type);
|
||||||
|
$rest_base = ($post_type_object && !empty($post_type_object->rest_base)) ? $post_type_object->rest_base : $post_type;
|
||||||
|
|
||||||
|
$base_url = sprintf('%s/wp-json/wp/v2/%s', get_site_url(), $rest_base);
|
||||||
|
|
||||||
|
$query_args = array(
|
||||||
|
'per_page' => min($per_page, 100),
|
||||||
|
'status' => $args['status'],
|
||||||
|
'orderby' => 'modified',
|
||||||
|
'order' => 'desc',
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!empty($args['after'])) {
|
||||||
|
$query_args['after'] = gmdate('c', $args['after']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$formatted_posts = array();
|
||||||
|
$page = 1;
|
||||||
|
|
||||||
|
do {
|
||||||
|
$query_args['page'] = $page;
|
||||||
|
$response = wp_remote_get(add_query_arg($query_args, $base_url));
|
||||||
|
|
||||||
|
if (is_wp_error($response)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$posts = json_decode(wp_remote_retrieve_body($response), true);
|
||||||
|
|
||||||
|
if (!is_array($posts) || empty($posts)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($posts as $post) {
|
||||||
|
$content = $post['content']['rendered'] ?? '';
|
||||||
|
$word_count = str_word_count(strip_tags($content));
|
||||||
|
|
||||||
|
$formatted_posts[] = array(
|
||||||
|
'id' => $post['id'],
|
||||||
|
'title' => html_entity_decode($post['title']['rendered'] ?? ''),
|
||||||
|
'content' => $content,
|
||||||
|
'excerpt' => $post['excerpt']['rendered'] ?? '',
|
||||||
|
'status' => $post['status'] ?? 'draft',
|
||||||
|
'url' => $post['link'] ?? '',
|
||||||
|
'published' => $post['date'] ?? '',
|
||||||
|
'modified' => $post['modified'] ?? '',
|
||||||
|
'author' => $post['author'] ?? 0,
|
||||||
|
'post_type' => $post['type'] ?? $post_type,
|
||||||
|
'taxonomies' => array(
|
||||||
|
'categories' => $post['categories'] ?? array(),
|
||||||
|
'tags' => $post['tags'] ?? array(),
|
||||||
|
),
|
||||||
|
'meta' => array(
|
||||||
|
'word_count' => $word_count,
|
||||||
|
'reading_time' => $word_count ? ceil($word_count / 200) : 0,
|
||||||
|
'featured_media' => $post['featured_media'] ?? 0,
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count($posts) < $query_args['per_page']) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$page++;
|
||||||
|
} while ($page <= $args['max_pages']);
|
||||||
|
|
||||||
|
return $formatted_posts;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch all available post types from WordPress
|
||||||
|
*
|
||||||
|
* @return array|false Post types array or false on failure
|
||||||
|
*/
|
||||||
|
function igny8_fetch_all_post_types() {
|
||||||
|
$wp_response = wp_remote_get(get_site_url() . '/wp-json/wp/v2/types');
|
||||||
|
|
||||||
|
if (is_wp_error($wp_response)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$types = json_decode(wp_remote_retrieve_body($wp_response), true);
|
||||||
|
|
||||||
|
if (!is_array($types)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$post_types = array();
|
||||||
|
foreach ($types as $type_name => $type_data) {
|
||||||
|
if ($type_data['public']) {
|
||||||
|
$post_types[] = array(
|
||||||
|
'name' => $type_name,
|
||||||
|
'label' => $type_data['name'],
|
||||||
|
'description' => $type_data['description'] ?? '',
|
||||||
|
'rest_base' => $type_data['rest_base'] ?? $type_name
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $post_types;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch all posts from all post types
|
||||||
|
*
|
||||||
|
* @param int $per_page Posts per page
|
||||||
|
* @return array All posts
|
||||||
|
*/
|
||||||
|
function igny8_fetch_all_wordpress_posts($per_page = 100) {
|
||||||
|
$post_types = igny8_fetch_all_post_types();
|
||||||
|
|
||||||
|
if (!$post_types) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
|
$all_posts = array();
|
||||||
|
foreach ($post_types as $type) {
|
||||||
|
$posts = igny8_fetch_wordpress_posts($type['name'], $per_page);
|
||||||
|
if ($posts) {
|
||||||
|
$all_posts = array_merge($all_posts, $posts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $all_posts;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch all taxonomies from WordPress
|
||||||
|
*
|
||||||
|
* @return array|false Taxonomies array or false on failure
|
||||||
|
*/
|
||||||
|
function igny8_fetch_wordpress_taxonomies() {
|
||||||
|
$wp_response = wp_remote_get(get_site_url() . '/wp-json/wp/v2/taxonomies');
|
||||||
|
|
||||||
|
if (is_wp_error($wp_response)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$taxonomies = json_decode(wp_remote_retrieve_body($wp_response), true);
|
||||||
|
|
||||||
|
if (!is_array($taxonomies)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$formatted_taxonomies = array();
|
||||||
|
foreach ($taxonomies as $tax_name => $tax_data) {
|
||||||
|
if ($tax_data['public']) {
|
||||||
|
$formatted_taxonomies[] = array(
|
||||||
|
'name' => $tax_name,
|
||||||
|
'label' => $tax_data['name'],
|
||||||
|
'description' => $tax_data['description'] ?? '',
|
||||||
|
'hierarchical' => $tax_data['hierarchical'],
|
||||||
|
'rest_base' => $tax_data['rest_base'] ?? $tax_name,
|
||||||
|
'object_types' => $tax_data['types'] ?? array()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $formatted_taxonomies;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch all terms for a specific taxonomy
|
||||||
|
*
|
||||||
|
* @param string $taxonomy Taxonomy name
|
||||||
|
* @param int $per_page Terms per page
|
||||||
|
* @return array|false Formatted terms array or false on failure
|
||||||
|
*/
|
||||||
|
function igny8_fetch_taxonomy_terms($taxonomy, $per_page = 100) {
|
||||||
|
$taxonomy_obj = get_taxonomy($taxonomy);
|
||||||
|
$rest_base = ($taxonomy_obj && !empty($taxonomy_obj->rest_base)) ? $taxonomy_obj->rest_base : $taxonomy;
|
||||||
|
|
||||||
|
$base_url = sprintf('%s/wp-json/wp/v2/%s', get_site_url(), $rest_base);
|
||||||
|
|
||||||
|
$formatted_terms = array();
|
||||||
|
$page = 1;
|
||||||
|
|
||||||
|
do {
|
||||||
|
$response = wp_remote_get(add_query_arg(array(
|
||||||
|
'per_page' => min($per_page, 100),
|
||||||
|
'page' => $page
|
||||||
|
), $base_url));
|
||||||
|
|
||||||
|
if (is_wp_error($response)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$terms = json_decode(wp_remote_retrieve_body($response), true);
|
||||||
|
|
||||||
|
if (!is_array($terms) || empty($terms)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($terms as $term) {
|
||||||
|
$formatted_terms[] = array(
|
||||||
|
'id' => $term['id'],
|
||||||
|
'name' => $term['name'],
|
||||||
|
'slug' => $term['slug'],
|
||||||
|
'description' => $term['description'] ?? '',
|
||||||
|
'count' => $term['count'],
|
||||||
|
'parent' => $term['parent'] ?? 0,
|
||||||
|
'taxonomy' => $taxonomy,
|
||||||
|
'url' => $term['link'] ?? ''
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count($terms) < min($per_page, 100)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$page++;
|
||||||
|
} while (true);
|
||||||
|
|
||||||
|
return $formatted_terms;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch all terms from all taxonomies
|
||||||
|
*
|
||||||
|
* @param int $per_page Terms per page
|
||||||
|
* @return array All terms organized by taxonomy
|
||||||
|
*/
|
||||||
|
function igny8_fetch_all_taxonomy_terms($per_page = 100) {
|
||||||
|
$taxonomies = igny8_fetch_wordpress_taxonomies();
|
||||||
|
|
||||||
|
if (!$taxonomies) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
|
$all_terms = array();
|
||||||
|
foreach ($taxonomies as $taxonomy) {
|
||||||
|
$terms = igny8_fetch_taxonomy_terms($taxonomy['rest_base'], $per_page);
|
||||||
|
if ($terms) {
|
||||||
|
$all_terms[$taxonomy['name']] = $terms;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $all_terms;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collect all WordPress site data for IGNY8 semantic mapping
|
||||||
|
*
|
||||||
|
* @return array Complete site data
|
||||||
|
*/
|
||||||
|
function igny8_collect_site_data($args = array()) {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
return array('disabled' => true, 'reason' => 'connection_disabled');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (function_exists('igny8_is_module_enabled') && !igny8_is_module_enabled('sites')) {
|
||||||
|
return array('disabled' => true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$settings = igny8_get_site_scan_settings($args);
|
||||||
|
|
||||||
|
$site_data = array(
|
||||||
|
'site_url' => get_site_url(),
|
||||||
|
'site_name' => get_bloginfo('name'),
|
||||||
|
'site_description' => get_bloginfo('description'),
|
||||||
|
'collected_at' => current_time('mysql'),
|
||||||
|
'settings' => $settings,
|
||||||
|
'posts' => array(),
|
||||||
|
'taxonomies' => array(),
|
||||||
|
'products' => array(),
|
||||||
|
'product_categories' => array(),
|
||||||
|
'product_attributes' => array()
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ((array) $settings['post_types'] as $post_type) {
|
||||||
|
if (!post_type_exists($post_type) || !igny8_is_post_type_enabled($post_type)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$posts = igny8_fetch_wordpress_posts($post_type, $settings['per_page'], array(
|
||||||
|
'after' => $settings['since'],
|
||||||
|
'status' => 'publish'
|
||||||
|
));
|
||||||
|
|
||||||
|
if ($posts) {
|
||||||
|
$site_data['posts'] = array_merge($site_data['posts'], $posts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$tracked_taxonomies = array('category', 'post_tag', 'igny8_sectors', 'igny8_clusters');
|
||||||
|
foreach ($tracked_taxonomies as $taxonomy) {
|
||||||
|
if (!taxonomy_exists($taxonomy)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$terms = igny8_fetch_taxonomy_terms($taxonomy, 100);
|
||||||
|
if ($terms) {
|
||||||
|
$tax_obj = get_taxonomy($taxonomy);
|
||||||
|
$site_data['taxonomies'][$taxonomy] = array(
|
||||||
|
'taxonomy' => array(
|
||||||
|
'name' => $taxonomy,
|
||||||
|
'label' => $tax_obj ? $tax_obj->label : $taxonomy,
|
||||||
|
'description' => $tax_obj->description ?? '',
|
||||||
|
'hierarchical' => $tax_obj ? $tax_obj->hierarchical : false,
|
||||||
|
),
|
||||||
|
'terms' => $terms
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($settings['include_products']) && function_exists('igny8_is_woocommerce_active') && igny8_is_woocommerce_active()) {
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'data/woocommerce.php';
|
||||||
|
|
||||||
|
$products = igny8_fetch_woocommerce_products(100);
|
||||||
|
if ($products) {
|
||||||
|
$site_data['products'] = $products;
|
||||||
|
}
|
||||||
|
|
||||||
|
$product_categories = igny8_fetch_product_categories(100);
|
||||||
|
if ($product_categories) {
|
||||||
|
$site_data['product_categories'] = $product_categories;
|
||||||
|
}
|
||||||
|
|
||||||
|
$product_attributes = igny8_fetch_product_attributes();
|
||||||
|
if ($product_attributes) {
|
||||||
|
$site_data['product_attributes'] = $product_attributes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract link graph if Linker module is enabled
|
||||||
|
if (function_exists('igny8_is_module_enabled') && igny8_is_module_enabled('linker')) {
|
||||||
|
$post_ids = wp_list_pluck($site_data['posts'], 'id');
|
||||||
|
$link_graph = igny8_extract_link_graph($post_ids);
|
||||||
|
|
||||||
|
if (!empty($link_graph)) {
|
||||||
|
$site_data['link_graph'] = $link_graph;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$site_data['summary'] = array(
|
||||||
|
'posts' => count($site_data['posts']),
|
||||||
|
'taxonomies' => count($site_data['taxonomies']),
|
||||||
|
'products' => count($site_data['products']),
|
||||||
|
'links' => isset($site_data['link_graph']) ? count($site_data['link_graph']) : 0
|
||||||
|
);
|
||||||
|
|
||||||
|
update_option('igny8_last_site_snapshot', array(
|
||||||
|
'timestamp' => current_time('timestamp'),
|
||||||
|
'summary' => $site_data['summary']
|
||||||
|
));
|
||||||
|
|
||||||
|
return $site_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send WordPress site data to IGNY8 for semantic strategy mapping
|
||||||
|
*
|
||||||
|
* @param int $site_id IGNY8 site ID
|
||||||
|
* @return array|false Response data or false on failure
|
||||||
|
*/
|
||||||
|
function igny8_send_site_data_to_igny8($site_id, $site_data = null, $args = array()) {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collect all site data if not provided
|
||||||
|
if (empty($site_data)) {
|
||||||
|
$site_data = igny8_collect_site_data($args);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($site_data) || isset($site_data['disabled'])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send to IGNY8 API
|
||||||
|
$response = $api->post("/system/sites/{$site_id}/import/", array(
|
||||||
|
'site_data' => $site_data,
|
||||||
|
'import_type' => $args['mode'] ?? 'full_site_scan'
|
||||||
|
));
|
||||||
|
|
||||||
|
if ($response['success']) {
|
||||||
|
// Store import ID for tracking
|
||||||
|
update_option('igny8_last_site_import_id', $response['data']['import_id'] ?? null);
|
||||||
|
update_option('igny8_last_site_sync', current_time('timestamp'));
|
||||||
|
|
||||||
|
// Send link graph separately to Linker module if available
|
||||||
|
if (!empty($site_data['link_graph']) && function_exists('igny8_is_module_enabled') && igny8_is_module_enabled('linker')) {
|
||||||
|
$link_result = igny8_send_link_graph_to_igny8($site_id, $site_data['link_graph']);
|
||||||
|
if ($link_result) {
|
||||||
|
error_log(sprintf('IGNY8: Sent %d links to Linker module', $link_result['links_sent'] ?? 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $response['data'];
|
||||||
|
} else {
|
||||||
|
error_log("IGNY8: Failed to send site data: " . ($response['error'] ?? 'Unknown error'));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync only changed posts/taxonomies since last sync
|
||||||
|
*
|
||||||
|
* @param int $site_id IGNY8 site ID
|
||||||
|
* @return array|false Sync result or false on failure
|
||||||
|
*/
|
||||||
|
function igny8_sync_incremental_site_data($site_id, $settings = array()) {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
return array('synced' => 0, 'message' => 'Connection disabled');
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$settings = igny8_get_site_scan_settings(wp_parse_args($settings, array('mode' => 'incremental')));
|
||||||
|
$since = $settings['since'] ?? intval(get_option('igny8_last_site_sync', 0));
|
||||||
|
|
||||||
|
$formatted_posts = array();
|
||||||
|
|
||||||
|
foreach ((array) $settings['post_types'] as $post_type) {
|
||||||
|
if (!post_type_exists($post_type) || !igny8_is_post_type_enabled($post_type)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$query_args = array(
|
||||||
|
'post_type' => $post_type,
|
||||||
|
'post_status' => array('publish', 'pending', 'draft', 'future'),
|
||||||
|
'posts_per_page' => -1,
|
||||||
|
'orderby' => 'modified',
|
||||||
|
'order' => 'DESC',
|
||||||
|
'suppress_filters' => true,
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($since) {
|
||||||
|
$query_args['date_query'] = array(
|
||||||
|
array(
|
||||||
|
'column' => 'post_modified_gmt',
|
||||||
|
'after' => gmdate('Y-m-d H:i:s', $since)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$posts = get_posts($query_args);
|
||||||
|
|
||||||
|
foreach ($posts as $post) {
|
||||||
|
$word_count = str_word_count(strip_tags($post->post_content));
|
||||||
|
|
||||||
|
$formatted_posts[] = array(
|
||||||
|
'id' => $post->ID,
|
||||||
|
'title' => get_the_title($post),
|
||||||
|
'content' => $post->post_content,
|
||||||
|
'status' => $post->post_status,
|
||||||
|
'modified' => $post->post_modified_gmt,
|
||||||
|
'post_type' => $post->post_type,
|
||||||
|
'url' => get_permalink($post),
|
||||||
|
'taxonomies' => array(
|
||||||
|
'categories' => wp_get_post_terms($post->ID, 'category', array('fields' => 'ids')),
|
||||||
|
'tags' => wp_get_post_terms($post->ID, 'post_tag', array('fields' => 'ids')),
|
||||||
|
),
|
||||||
|
'meta' => array(
|
||||||
|
'task_id' => get_post_meta($post->ID, '_igny8_task_id', true),
|
||||||
|
'cluster_id' => get_post_meta($post->ID, '_igny8_cluster_id', true),
|
||||||
|
'sector_id' => get_post_meta($post->ID, '_igny8_sector_id', true),
|
||||||
|
'word_count' => $word_count,
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($formatted_posts)) {
|
||||||
|
return array('synced' => 0, 'message' => 'No changes since last sync');
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = $api->post("/system/sites/{$site_id}/sync/", array(
|
||||||
|
'posts' => $formatted_posts,
|
||||||
|
'sync_type' => 'incremental',
|
||||||
|
'last_sync' => $since,
|
||||||
|
'post_types' => $settings['post_types']
|
||||||
|
));
|
||||||
|
|
||||||
|
if ($response['success']) {
|
||||||
|
update_option('igny8_last_site_sync', current_time('timestamp'));
|
||||||
|
update_option('igny8_last_incremental_site_sync', array(
|
||||||
|
'timestamp' => current_time('timestamp'),
|
||||||
|
'count' => count($formatted_posts)
|
||||||
|
));
|
||||||
|
|
||||||
|
return array(
|
||||||
|
'synced' => count($formatted_posts),
|
||||||
|
'message' => 'Incremental sync completed'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run a full site scan and semantic mapping
|
||||||
|
*
|
||||||
|
* @param int $site_id IGNY8 site ID
|
||||||
|
* @param array $settings Scan settings
|
||||||
|
* @return array|false
|
||||||
|
*/
|
||||||
|
function igny8_perform_full_site_scan($site_id, $settings = array()) {
|
||||||
|
$site_data = igny8_collect_site_data($settings);
|
||||||
|
|
||||||
|
if (empty($site_data) || isset($site_data['disabled'])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$import = igny8_send_site_data_to_igny8($site_id, $site_data, array('mode' => 'full_site_scan'));
|
||||||
|
|
||||||
|
if (!$import) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
update_option('igny8_last_full_site_scan', current_time('timestamp'));
|
||||||
|
|
||||||
|
// Map to semantic strategy (requires Planner module)
|
||||||
|
if (!function_exists('igny8_is_module_enabled') || igny8_is_module_enabled('planner')) {
|
||||||
|
$map_response = igny8_map_site_to_semantic_strategy($site_id, $site_data);
|
||||||
|
if (!empty($map_response['success'])) {
|
||||||
|
update_option('igny8_last_semantic_map', current_time('timestamp'));
|
||||||
|
update_option('igny8_last_semantic_map_summary', array(
|
||||||
|
'sectors' => count($map_response['data']['sectors'] ?? array()),
|
||||||
|
'keywords' => count($map_response['data']['keywords'] ?? array())
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send link graph to Linker module if available
|
||||||
|
if (!empty($site_data['link_graph']) && function_exists('igny8_is_module_enabled') && igny8_is_module_enabled('linker')) {
|
||||||
|
$link_result = igny8_send_link_graph_to_igny8($site_id, $site_data['link_graph']);
|
||||||
|
if ($link_result) {
|
||||||
|
error_log(sprintf('IGNY8: Sent %d links to Linker module during full scan', $link_result['links_sent'] ?? 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $import;
|
||||||
|
}
|
||||||
|
|
||||||
226
igny8-wp-integration-plugin/data/woocommerce.php
Normal file
226
igny8-wp-integration-plugin/data/woocommerce.php
Normal file
@@ -0,0 +1,226 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* WooCommerce Integration
|
||||||
|
*
|
||||||
|
* Fetches WooCommerce products, categories, and attributes
|
||||||
|
* Follows WORDPRESS-PLUGIN-INTEGRATION.md guidelines
|
||||||
|
*
|
||||||
|
* @package Igny8Bridge
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Prevent direct access
|
||||||
|
if (!defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if WooCommerce is active
|
||||||
|
*
|
||||||
|
* @return bool True if WooCommerce is active
|
||||||
|
*/
|
||||||
|
function igny8_is_woocommerce_active() {
|
||||||
|
return class_exists('WooCommerce');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch all WooCommerce products
|
||||||
|
*
|
||||||
|
* @param int $per_page Products per page
|
||||||
|
* @return array|false Formatted products array or false on failure
|
||||||
|
*/
|
||||||
|
function igny8_fetch_woocommerce_products($per_page = 100) {
|
||||||
|
// Check if WooCommerce is active
|
||||||
|
if (!igny8_is_woocommerce_active()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get WooCommerce API credentials
|
||||||
|
$consumer_key = get_option('woocommerce_api_consumer_key', '');
|
||||||
|
$consumer_secret = get_option('woocommerce_api_consumer_secret', '');
|
||||||
|
|
||||||
|
if (empty($consumer_key) || empty($consumer_secret)) {
|
||||||
|
// Try to use basic auth if API keys not set
|
||||||
|
$auth = '';
|
||||||
|
} else {
|
||||||
|
$auth = 'Basic ' . base64_encode($consumer_key . ':' . $consumer_secret);
|
||||||
|
}
|
||||||
|
|
||||||
|
$headers = array();
|
||||||
|
if ($auth) {
|
||||||
|
$headers['Authorization'] = $auth;
|
||||||
|
}
|
||||||
|
|
||||||
|
$wp_response = wp_remote_get(sprintf(
|
||||||
|
'%s/wp-json/wc/v3/products?per_page=%d&status=publish',
|
||||||
|
get_site_url(),
|
||||||
|
$per_page
|
||||||
|
), array(
|
||||||
|
'headers' => $headers
|
||||||
|
));
|
||||||
|
|
||||||
|
if (is_wp_error($wp_response)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$products = json_decode(wp_remote_retrieve_body($wp_response), true);
|
||||||
|
|
||||||
|
if (!is_array($products)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$formatted_products = array();
|
||||||
|
foreach ($products as $product) {
|
||||||
|
$formatted_products[] = array(
|
||||||
|
'id' => $product['id'],
|
||||||
|
'name' => $product['name'],
|
||||||
|
'slug' => $product['slug'],
|
||||||
|
'sku' => $product['sku'],
|
||||||
|
'type' => $product['type'],
|
||||||
|
'status' => $product['status'],
|
||||||
|
'description' => $product['description'],
|
||||||
|
'short_description' => $product['short_description'],
|
||||||
|
'price' => $product['price'],
|
||||||
|
'regular_price' => $product['regular_price'],
|
||||||
|
'sale_price' => $product['sale_price'],
|
||||||
|
'on_sale' => $product['on_sale'],
|
||||||
|
'stock_status' => $product['stock_status'],
|
||||||
|
'stock_quantity' => $product['stock_quantity'],
|
||||||
|
'categories' => $product['categories'] ?? array(),
|
||||||
|
'tags' => $product['tags'] ?? array(),
|
||||||
|
'images' => $product['images'] ?? array(),
|
||||||
|
'attributes' => $product['attributes'] ?? array(),
|
||||||
|
'variations' => $product['variations'] ?? array(),
|
||||||
|
'url' => $product['permalink']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $formatted_products;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch WooCommerce product categories
|
||||||
|
*
|
||||||
|
* @param int $per_page Categories per page
|
||||||
|
* @return array|false Formatted categories array or false on failure
|
||||||
|
*/
|
||||||
|
function igny8_fetch_product_categories($per_page = 100) {
|
||||||
|
if (!igny8_is_woocommerce_active()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$consumer_key = get_option('woocommerce_api_consumer_key', '');
|
||||||
|
$consumer_secret = get_option('woocommerce_api_consumer_secret', '');
|
||||||
|
|
||||||
|
$headers = array();
|
||||||
|
if ($consumer_key && $consumer_secret) {
|
||||||
|
$headers['Authorization'] = 'Basic ' . base64_encode($consumer_key . ':' . $consumer_secret);
|
||||||
|
}
|
||||||
|
|
||||||
|
$wp_response = wp_remote_get(sprintf(
|
||||||
|
'%s/wp-json/wc/v3/products/categories?per_page=%d',
|
||||||
|
get_site_url(),
|
||||||
|
$per_page
|
||||||
|
), array(
|
||||||
|
'headers' => $headers
|
||||||
|
));
|
||||||
|
|
||||||
|
if (is_wp_error($wp_response)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$categories = json_decode(wp_remote_retrieve_body($wp_response), true);
|
||||||
|
|
||||||
|
if (!is_array($categories)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$formatted_categories = array();
|
||||||
|
foreach ($categories as $category) {
|
||||||
|
$formatted_categories[] = array(
|
||||||
|
'id' => $category['id'],
|
||||||
|
'name' => $category['name'],
|
||||||
|
'slug' => $category['slug'],
|
||||||
|
'description' => $category['description'] ?? '',
|
||||||
|
'count' => $category['count'],
|
||||||
|
'parent' => $category['parent'] ?? 0,
|
||||||
|
'image' => $category['image']['src'] ?? null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $formatted_categories;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch WooCommerce product attributes
|
||||||
|
*
|
||||||
|
* @return array|false Formatted attributes array or false on failure
|
||||||
|
*/
|
||||||
|
function igny8_fetch_product_attributes() {
|
||||||
|
if (!igny8_is_woocommerce_active()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$consumer_key = get_option('woocommerce_api_consumer_key', '');
|
||||||
|
$consumer_secret = get_option('woocommerce_api_consumer_secret', '');
|
||||||
|
|
||||||
|
$headers = array();
|
||||||
|
if ($consumer_key && $consumer_secret) {
|
||||||
|
$headers['Authorization'] = 'Basic ' . base64_encode($consumer_key . ':' . $consumer_secret);
|
||||||
|
}
|
||||||
|
|
||||||
|
$wp_response = wp_remote_get(
|
||||||
|
get_site_url() . '/wp-json/wc/v3/products/attributes',
|
||||||
|
array(
|
||||||
|
'headers' => $headers
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (is_wp_error($wp_response)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$attributes = json_decode(wp_remote_retrieve_body($wp_response), true);
|
||||||
|
|
||||||
|
if (!is_array($attributes)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$formatted_attributes = array();
|
||||||
|
foreach ($attributes as $attribute) {
|
||||||
|
// Get attribute terms
|
||||||
|
$terms_response = wp_remote_get(sprintf(
|
||||||
|
'%s/wp-json/wc/v3/products/attributes/%d/terms',
|
||||||
|
get_site_url(),
|
||||||
|
$attribute['id']
|
||||||
|
), array(
|
||||||
|
'headers' => $headers
|
||||||
|
));
|
||||||
|
|
||||||
|
$terms = array();
|
||||||
|
if (!is_wp_error($terms_response)) {
|
||||||
|
$terms_data = json_decode(wp_remote_retrieve_body($terms_response), true);
|
||||||
|
if (is_array($terms_data)) {
|
||||||
|
foreach ($terms_data as $term) {
|
||||||
|
$terms[] = array(
|
||||||
|
'id' => $term['id'],
|
||||||
|
'name' => $term['name'],
|
||||||
|
'slug' => $term['slug']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$formatted_attributes[] = array(
|
||||||
|
'id' => $attribute['id'],
|
||||||
|
'name' => $attribute['name'],
|
||||||
|
'slug' => $attribute['slug'],
|
||||||
|
'type' => $attribute['type'],
|
||||||
|
'order_by' => $attribute['order_by'],
|
||||||
|
'has_archives' => $attribute['has_archives'],
|
||||||
|
'terms' => $terms
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $formatted_attributes;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,185 @@
|
|||||||
|
# Phase 5 Implementation Summary
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
Phase 5 implementation adds Planner, Linker, and Optimizer module hooks to the WordPress bridge plugin.
|
||||||
|
|
||||||
|
## Implemented Features
|
||||||
|
|
||||||
|
### 5.1 Planner Briefs Display & Refresh Actions ✅
|
||||||
|
|
||||||
|
**Files Created:**
|
||||||
|
- `admin/class-post-meta-boxes.php` - Meta boxes for post editor
|
||||||
|
- `admin/assets/js/post-editor.js` - JavaScript for AJAX interactions
|
||||||
|
|
||||||
|
**Features:**
|
||||||
|
- **Planner Brief Meta Box** in post editor sidebar
|
||||||
|
- Displays cached brief with title, content, outline, keywords, and tone
|
||||||
|
- Shows cache timestamp
|
||||||
|
- "Fetch Brief" button to load from IGNY8 API
|
||||||
|
- "Request Refresh" button to trigger Planner refresh
|
||||||
|
|
||||||
|
**API Endpoints Used:**
|
||||||
|
- `GET /planner/tasks/{id}/brief/` - Fetch Planner brief (with fallback to Writer brief)
|
||||||
|
- `POST /planner/tasks/{id}/refresh/` - Request Planner task refresh
|
||||||
|
|
||||||
|
**Meta Fields Added:**
|
||||||
|
- `_igny8_task_brief` - Cached brief data
|
||||||
|
- `_igny8_brief_cached_at` - Brief cache timestamp
|
||||||
|
|
||||||
|
**AJAX Handlers:**
|
||||||
|
- `igny8_fetch_planner_brief` - Fetches and caches brief
|
||||||
|
- `igny8_refresh_planner_task` - Requests Planner refresh
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5.2 Link Graph Export ✅
|
||||||
|
|
||||||
|
**Files Created:**
|
||||||
|
- `data/link-graph.php` - Link graph extraction and export
|
||||||
|
|
||||||
|
**Features:**
|
||||||
|
- **Link Extraction** from post content
|
||||||
|
- Extracts all internal links (anchor tags)
|
||||||
|
- Captures source URL, target URL, and anchor text
|
||||||
|
- Filters to only internal links (same domain)
|
||||||
|
- Normalizes URLs for consistency
|
||||||
|
|
||||||
|
- **Link Graph Collection**
|
||||||
|
- Processes all enabled post types
|
||||||
|
- Extracts links from published posts
|
||||||
|
- Configurable batch processing (max 1000 posts per run)
|
||||||
|
|
||||||
|
- **Automatic Export During Site Scans**
|
||||||
|
- Integrated into `igny8_collect_site_data()`
|
||||||
|
- Included in site data payload
|
||||||
|
- Also sent separately to Linker module endpoint
|
||||||
|
|
||||||
|
**API Endpoints Used:**
|
||||||
|
- `POST /linker/link-map/` - Send link graph to Linker module
|
||||||
|
|
||||||
|
**Functions:**
|
||||||
|
- `igny8_extract_post_links($post_id)` - Extract links from single post
|
||||||
|
- `igny8_extract_link_graph($post_ids)` - Extract links from multiple posts
|
||||||
|
- `igny8_send_link_graph_to_igny8($site_id, $link_graph)` - Send to IGNY8 API
|
||||||
|
|
||||||
|
**Integration Points:**
|
||||||
|
- `igny8_collect_site_data()` - Includes link graph in site data
|
||||||
|
- `igny8_send_site_data_to_igny8()` - Sends link graph after site import
|
||||||
|
- `igny8_perform_full_site_scan()` - Sends link graph during full scans
|
||||||
|
|
||||||
|
**Options Stored:**
|
||||||
|
- `igny8_last_link_graph_sync` - Last sync timestamp
|
||||||
|
- `igny8_last_link_graph_count` - Number of links sent
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5.4 Optimizer Triggers ✅
|
||||||
|
|
||||||
|
**Features:**
|
||||||
|
- **Optimizer Meta Box** in post editor sidebar
|
||||||
|
- Displays current optimizer job ID and status
|
||||||
|
- "Request Optimization" button to create new job
|
||||||
|
- "Check Status" button to fetch latest job status
|
||||||
|
- Shows score changes and recommendations when available
|
||||||
|
|
||||||
|
**API Endpoints Used:**
|
||||||
|
- `POST /optimizer/jobs/` - Create optimizer job
|
||||||
|
- `GET /optimizer/jobs/{id}/` - Get optimizer job status
|
||||||
|
|
||||||
|
**Meta Fields Added:**
|
||||||
|
- `_igny8_optimizer_job_id` - Optimizer job ID
|
||||||
|
- `_igny8_optimizer_status` - Job status (pending, processing, completed, failed)
|
||||||
|
- `_igny8_optimizer_score_changes` - Score changes from optimization
|
||||||
|
- `_igny8_optimizer_recommendations` - Optimization recommendations
|
||||||
|
- `_igny8_optimizer_job_created_at` - Job creation timestamp
|
||||||
|
|
||||||
|
**AJAX Handlers:**
|
||||||
|
- `igny8_create_optimizer_job` - Creates new optimizer job
|
||||||
|
- `igny8_get_optimizer_status` - Fetches job status and updates meta
|
||||||
|
|
||||||
|
**Job Parameters:**
|
||||||
|
- `post_id` - WordPress post ID
|
||||||
|
- `task_id` - IGNY8 task ID
|
||||||
|
- `job_type` - Type of job (default: 'audit')
|
||||||
|
- `priority` - Job priority (default: 'normal')
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Module Toggle Support
|
||||||
|
|
||||||
|
All Phase 5 features respect the module toggle settings:
|
||||||
|
- **Planner** module must be enabled for brief fetching/refresh
|
||||||
|
- **Linker** module must be enabled for link graph export
|
||||||
|
- **Optimizer** module must be enabled for optimizer jobs
|
||||||
|
|
||||||
|
Features also check `igny8_is_connection_enabled()` to ensure sync operations are active.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## UI/UX Features
|
||||||
|
|
||||||
|
### Post Editor Meta Boxes
|
||||||
|
- Clean, organized display in sidebar
|
||||||
|
- Real-time AJAX updates
|
||||||
|
- Success/error message display
|
||||||
|
- Auto-refresh after operations
|
||||||
|
- Disabled state when connection is off
|
||||||
|
|
||||||
|
### JavaScript Enhancements
|
||||||
|
- Loading states on buttons
|
||||||
|
- Confirmation dialogs for destructive actions
|
||||||
|
- Error handling and user feedback
|
||||||
|
- Non-blocking AJAX requests
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Integration with Existing Code
|
||||||
|
|
||||||
|
### Modified Files:
|
||||||
|
- `igny8-bridge.php` - Added meta boxes class loading
|
||||||
|
- `includes/functions.php` - Added optimizer meta field registrations
|
||||||
|
- `data/site-collection.php` - Integrated link graph extraction
|
||||||
|
|
||||||
|
### New Dependencies:
|
||||||
|
- None (uses existing Igny8API class)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Testing Checklist
|
||||||
|
|
||||||
|
- [ ] Planner brief displays correctly in post editor
|
||||||
|
- [ ] Fetch brief button works and caches data
|
||||||
|
- [ ] Request refresh button triggers Planner refresh
|
||||||
|
- [ ] Link graph extraction works on posts with links
|
||||||
|
- [ ] Link graph is included in site scans
|
||||||
|
- [ ] Link graph is sent to Linker endpoint
|
||||||
|
- [ ] Optimizer job creation works
|
||||||
|
- [ ] Optimizer status check works
|
||||||
|
- [ ] All features respect module toggles
|
||||||
|
- [ ] All features respect connection enabled toggle
|
||||||
|
- [ ] Meta boxes only show for posts with task IDs
|
||||||
|
- [ ] Error handling works correctly
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
1. **Link Graph Processing**: Currently limited to 1000 posts per run to prevent timeouts. Can be increased or made configurable.
|
||||||
|
|
||||||
|
2. **Brief Caching**: Briefs are cached in post meta to reduce API calls. Cache can be refreshed manually.
|
||||||
|
|
||||||
|
3. **Optimizer Jobs**: Jobs are created asynchronously. Status must be checked manually or via webhook (Phase 6).
|
||||||
|
|
||||||
|
4. **Module Dependencies**: All features check module enablement before executing.
|
||||||
|
|
||||||
|
5. **Connection Toggle**: All features respect the master connection toggle added earlier.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Next Steps (Phase 6)
|
||||||
|
|
||||||
|
Phase 5.3 (Accept link recommendations via webhook) is deferred to Phase 6, which will implement:
|
||||||
|
- REST endpoint `/wp-json/igny8/v1/event` with shared secret
|
||||||
|
- Webhook handler for link recommendations
|
||||||
|
- Link insertion queue system
|
||||||
|
|
||||||
@@ -0,0 +1,298 @@
|
|||||||
|
# Phase 6 Implementation Summary
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
Phase 6 implementation adds webhook support and remote control capabilities, allowing IGNY8 SaaS to send events to WordPress.
|
||||||
|
|
||||||
|
## Implemented Features
|
||||||
|
|
||||||
|
### 6.1 REST Route Registration with Shared Secret ✅
|
||||||
|
|
||||||
|
**Files Created:**
|
||||||
|
- `includes/class-igny8-webhooks.php` - Main webhook handler class
|
||||||
|
|
||||||
|
**Features:**
|
||||||
|
- **REST Endpoint**: `/wp-json/igny8/v1/event` (POST)
|
||||||
|
- **Shared Secret Authentication**: HMAC-SHA256 signature verification
|
||||||
|
- **Connection Check**: All webhook handlers verify `igny8_is_connection_enabled()` before processing
|
||||||
|
|
||||||
|
**Security:**
|
||||||
|
- Webhook secret stored in WordPress options (auto-generated on first use)
|
||||||
|
- Signature verification via `X-IGNY8-Signature` header
|
||||||
|
- Secret can be regenerated from settings page
|
||||||
|
- Failed authentication attempts are logged
|
||||||
|
|
||||||
|
**Functions:**
|
||||||
|
- `igny8_get_webhook_secret()` - Get or generate webhook secret
|
||||||
|
- `igny8_regenerate_webhook_secret()` - Regenerate secret
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 6.2 SaaS Event Handlers ✅
|
||||||
|
|
||||||
|
**Event Types Supported:**
|
||||||
|
|
||||||
|
#### 1. Task Published (`task_published`, `task_completed`)
|
||||||
|
- Creates or updates WordPress post from IGNY8 task
|
||||||
|
- Fetches full task data from Writer API
|
||||||
|
- Respects enabled post types
|
||||||
|
- Updates post status if task is completed
|
||||||
|
|
||||||
|
#### 2. Link Recommendation (`link_recommendation`, `insert_link`)
|
||||||
|
- Queues link insertion for processing
|
||||||
|
- Validates required parameters (post_id, target_url, anchor)
|
||||||
|
- Respects Linker module toggle
|
||||||
|
- Processes links asynchronously via cron
|
||||||
|
|
||||||
|
#### 3. Optimizer Request (`optimizer_request`, `optimizer_job_completed`)
|
||||||
|
- Updates optimizer job status
|
||||||
|
- Stores score changes and recommendations
|
||||||
|
- Updates post meta with optimizer data
|
||||||
|
- Respects Optimizer module toggle
|
||||||
|
|
||||||
|
**Event Handler Flow:**
|
||||||
|
1. Verify connection is enabled
|
||||||
|
2. Verify module is enabled (if applicable)
|
||||||
|
3. Validate event data
|
||||||
|
4. Process event
|
||||||
|
5. Log activity
|
||||||
|
6. Return unified JSON response
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 6.3 Webhook Activity Logging ✅
|
||||||
|
|
||||||
|
**Files Created:**
|
||||||
|
- `includes/class-igny8-webhook-logs.php` - Logging functions
|
||||||
|
|
||||||
|
**Features:**
|
||||||
|
- **Log Storage**: WordPress options (last 500 logs)
|
||||||
|
- **Log Fields**:
|
||||||
|
- Event type
|
||||||
|
- Event data
|
||||||
|
- IP address
|
||||||
|
- User agent
|
||||||
|
- Status (received, processed, failed)
|
||||||
|
- Response data
|
||||||
|
- Timestamps (received_at, processed_at)
|
||||||
|
- Error messages
|
||||||
|
|
||||||
|
**Functions:**
|
||||||
|
- `igny8_log_webhook_activity()` - Log webhook receipt
|
||||||
|
- `igny8_update_webhook_log()` - Update log with processing result
|
||||||
|
- `igny8_get_webhook_logs()` - Retrieve logs with filtering
|
||||||
|
- `igny8_clear_old_webhook_logs()` - Cleanup old logs
|
||||||
|
|
||||||
|
**UI Display:**
|
||||||
|
- Recent webhook activity table in settings page
|
||||||
|
- Shows last 10 webhook events
|
||||||
|
- Displays event type, status, and timestamp
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Link Queue System ✅
|
||||||
|
|
||||||
|
**Files Created:**
|
||||||
|
- `includes/class-igny8-link-queue.php` - Link insertion queue
|
||||||
|
|
||||||
|
**Features:**
|
||||||
|
- **Queue Storage**: WordPress options
|
||||||
|
- **Queue Processing**: Cron-based (processes 10 items per run)
|
||||||
|
- **Retry Logic**: Up to 3 attempts per link
|
||||||
|
- **Status Tracking**: pending, completed, failed
|
||||||
|
|
||||||
|
**Link Insertion Logic:**
|
||||||
|
1. Finds anchor text in post content
|
||||||
|
2. Wraps anchor with link tag
|
||||||
|
3. Avoids duplicate links
|
||||||
|
4. Falls back to appending link if anchor not found
|
||||||
|
|
||||||
|
**Queue Management:**
|
||||||
|
- Automatic processing via cron
|
||||||
|
- Manual trigger available
|
||||||
|
- Queue size limit (1000 items)
|
||||||
|
- Status tracking per item
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Connection Checks
|
||||||
|
|
||||||
|
**All handlers check connection status:**
|
||||||
|
|
||||||
|
✅ **Webhook Handler** (`verify_webhook_secret`)
|
||||||
|
- Checks `igny8_is_connection_enabled()` before allowing webhook
|
||||||
|
|
||||||
|
✅ **Event Handlers** (`handle_webhook`, `handle_task_published`, etc.)
|
||||||
|
- Double-checks connection enabled before processing
|
||||||
|
- Returns error if connection disabled
|
||||||
|
|
||||||
|
✅ **Link Queue** (`igny8_queue_link_insertion`, `igny8_process_link_queue`)
|
||||||
|
- Checks connection enabled before queuing/processing
|
||||||
|
|
||||||
|
✅ **REST API Endpoints** (existing endpoints)
|
||||||
|
- Updated to check connection enabled
|
||||||
|
- Returns 403 if connection disabled
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Settings UI Enhancements
|
||||||
|
|
||||||
|
**New Sections Added:**
|
||||||
|
|
||||||
|
1. **Webhook Configuration**
|
||||||
|
- Webhook URL display with copy button
|
||||||
|
- Webhook secret display with copy button
|
||||||
|
- Regenerate secret button
|
||||||
|
|
||||||
|
2. **Link Queue**
|
||||||
|
- Shows pending links count
|
||||||
|
- Displays queue table (last 10 items)
|
||||||
|
- Shows post ID, anchor, target URL, status
|
||||||
|
|
||||||
|
3. **Recent Webhook Activity**
|
||||||
|
- Shows last 10 webhook events
|
||||||
|
- Displays event type, status, timestamp
|
||||||
|
- Color-coded status badges
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Security Features
|
||||||
|
|
||||||
|
1. **HMAC Signature Verification**
|
||||||
|
- Uses SHA-256 HMAC
|
||||||
|
- Compares request body signature
|
||||||
|
- Prevents replay attacks
|
||||||
|
|
||||||
|
2. **Connection Toggle Protection**
|
||||||
|
- All endpoints check connection status
|
||||||
|
- Webhooks rejected if connection disabled
|
||||||
|
- Clear error messages
|
||||||
|
|
||||||
|
3. **Module Toggle Respect**
|
||||||
|
- Events only processed if module enabled
|
||||||
|
- Graceful error responses
|
||||||
|
|
||||||
|
4. **Input Validation**
|
||||||
|
- All parameters sanitized
|
||||||
|
- Required fields validated
|
||||||
|
- Type checking
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## API Response Format
|
||||||
|
|
||||||
|
All webhook responses follow unified JSON format:
|
||||||
|
|
||||||
|
**Success:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"success": true,
|
||||||
|
"message": "Event processed",
|
||||||
|
"data": { ... }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Error:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"success": false,
|
||||||
|
"error": "Error message",
|
||||||
|
"code": "error_code"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Integration Points
|
||||||
|
|
||||||
|
### Modified Files:
|
||||||
|
- `igny8-bridge.php` - Added webhook classes loading
|
||||||
|
- `includes/functions.php` - Added webhook secret functions
|
||||||
|
- `includes/class-igny8-rest-api.php` - Added connection checks
|
||||||
|
- `admin/settings.php` - Added webhook UI sections
|
||||||
|
- `admin/class-admin.php` - Added secret regeneration handler
|
||||||
|
|
||||||
|
### New Dependencies:
|
||||||
|
- None (uses existing WordPress functions)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Testing Checklist
|
||||||
|
|
||||||
|
- [ ] Webhook endpoint accessible at `/wp-json/igny8/v1/event`
|
||||||
|
- [ ] Signature verification works correctly
|
||||||
|
- [ ] Invalid signatures are rejected
|
||||||
|
- [ ] Connection disabled blocks webhooks
|
||||||
|
- [ ] Task published event creates/updates posts
|
||||||
|
- [ ] Link recommendation queues links
|
||||||
|
- [ ] Link queue processes links correctly
|
||||||
|
- [ ] Optimizer events update post meta
|
||||||
|
- [ ] Webhook logs are created and updated
|
||||||
|
- [ ] Settings UI displays webhook info
|
||||||
|
- [ ] Secret regeneration works
|
||||||
|
- [ ] All events respect module toggles
|
||||||
|
- [ ] All events respect connection toggle
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
1. **Webhook Secret**: Auto-generated on first use. Must be configured in IGNY8 SaaS app.
|
||||||
|
|
||||||
|
2. **Link Queue**: Processes 10 items per cron run to prevent timeouts. Can be adjusted.
|
||||||
|
|
||||||
|
3. **Log Retention**: Keeps last 500 logs. Older logs can be cleared manually.
|
||||||
|
|
||||||
|
4. **Signature Header**: IGNY8 SaaS must send `X-IGNY8-Signature` header with HMAC-SHA256 signature of request body.
|
||||||
|
|
||||||
|
5. **Connection Toggle**: All webhook handlers check connection status before processing. This ensures no data is processed when connection is disabled.
|
||||||
|
|
||||||
|
6. **Module Toggles**: Each event type checks if its module is enabled before processing.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Webhook Payload Examples
|
||||||
|
|
||||||
|
### Task Published
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"event": "task_published",
|
||||||
|
"data": {
|
||||||
|
"task_id": 123,
|
||||||
|
"status": "completed"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Link Recommendation
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"event": "link_recommendation",
|
||||||
|
"data": {
|
||||||
|
"post_id": 456,
|
||||||
|
"target_url": "https://example.com/page",
|
||||||
|
"anchor": "example link",
|
||||||
|
"priority": "normal"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Optimizer Request
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"event": "optimizer_job_completed",
|
||||||
|
"data": {
|
||||||
|
"post_id": 456,
|
||||||
|
"job_id": 789,
|
||||||
|
"status": "completed",
|
||||||
|
"score_changes": { ... },
|
||||||
|
"recommendations": [ ... ]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Next Steps
|
||||||
|
|
||||||
|
Phase 6 is complete. All webhook functionality is implemented with proper security, logging, and connection checks.
|
||||||
|
|
||||||
249
igny8-wp-integration-plugin/docs/STATUS_SYNC_DOCUMENTATION.md
Normal file
249
igny8-wp-integration-plugin/docs/STATUS_SYNC_DOCUMENTATION.md
Normal file
@@ -0,0 +1,249 @@
|
|||||||
|
# Status Sync & Content ID Documentation
|
||||||
|
|
||||||
|
**Last Updated**: 2025-10-17
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
This document explains how the plugin handles status synchronization and content_id tracking between IGNY8 and WordPress.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Status Mapping
|
||||||
|
|
||||||
|
### IGNY8 → WordPress
|
||||||
|
|
||||||
|
When content arrives from IGNY8, status is mapped as follows:
|
||||||
|
|
||||||
|
| IGNY8 Status | WordPress Status | Description |
|
||||||
|
|--------------|------------------|-------------|
|
||||||
|
| `completed` | `publish` | Content is published |
|
||||||
|
| `draft` | `draft` | Content is draft |
|
||||||
|
| `pending` | `pending` | Content pending review |
|
||||||
|
| `scheduled` | `future` | Content scheduled |
|
||||||
|
| `archived` | `trash` | Content archived |
|
||||||
|
|
||||||
|
### WordPress → IGNY8
|
||||||
|
|
||||||
|
When WordPress post status changes, it's mapped back to IGNY8:
|
||||||
|
|
||||||
|
| WordPress Status | IGNY8 Status | Description |
|
||||||
|
|------------------|--------------|-------------|
|
||||||
|
| `publish` | `completed` | Post is published |
|
||||||
|
| `draft` | `draft` | Post is draft |
|
||||||
|
| `pending` | `pending` | Post pending review |
|
||||||
|
| `private` | `completed` | Post is private (published) |
|
||||||
|
| `trash` | `archived` | Post is deleted |
|
||||||
|
| `future` | `scheduled` | Post is scheduled |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Content ID Tracking
|
||||||
|
|
||||||
|
### Storage
|
||||||
|
|
||||||
|
The plugin stores IGNY8 `content_id` in post meta:
|
||||||
|
- **Meta Key**: `_igny8_content_id`
|
||||||
|
- **Type**: Integer
|
||||||
|
- **REST API**: Available via `/wp-json/wp/v2/posts?meta_key=_igny8_content_id&meta_value=123`
|
||||||
|
|
||||||
|
### Task ID Tracking
|
||||||
|
|
||||||
|
The plugin also stores IGNY8 `task_id`:
|
||||||
|
- **Meta Key**: `_igny8_task_id`
|
||||||
|
- **Type**: Integer
|
||||||
|
- **REST API**: Available via `/wp-json/wp/v2/posts?meta_key=_igny8_task_id&meta_value=456`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Response to IGNY8
|
||||||
|
|
||||||
|
When content is created/updated in WordPress, the plugin responds to IGNY8 with:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"assigned_post_id": 123,
|
||||||
|
"post_url": "https://example.com/post/",
|
||||||
|
"wordpress_status": "publish",
|
||||||
|
"status": "completed",
|
||||||
|
"synced_at": "2025-10-17 12:00:00",
|
||||||
|
"post_type": "post",
|
||||||
|
"content_type": "post",
|
||||||
|
"content_id": 789
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Response Fields
|
||||||
|
|
||||||
|
- **`assigned_post_id`**: WordPress post ID
|
||||||
|
- **`post_url`**: Full URL to the post
|
||||||
|
- **`wordpress_status`**: Actual WordPress status (publish/pending/draft)
|
||||||
|
- **`status`**: IGNY8 mapped status (completed/pending/draft)
|
||||||
|
- **`synced_at`**: Timestamp of sync
|
||||||
|
- **`post_type`**: WordPress post type
|
||||||
|
- **`content_type`**: IGNY8 content type
|
||||||
|
- **`content_id`**: IGNY8 content ID (if provided)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## REST API Endpoints
|
||||||
|
|
||||||
|
The plugin provides REST API endpoints for IGNY8 to query WordPress:
|
||||||
|
|
||||||
|
### 1. Get Post by Content ID
|
||||||
|
|
||||||
|
**Endpoint**: `GET /wp-json/igny8/v1/post-by-content-id/{content_id}`
|
||||||
|
|
||||||
|
**Response**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"success": true,
|
||||||
|
"data": {
|
||||||
|
"post_id": 123,
|
||||||
|
"title": "Post Title",
|
||||||
|
"status": "publish",
|
||||||
|
"wordpress_status": "publish",
|
||||||
|
"igny8_status": "completed",
|
||||||
|
"url": "https://example.com/post/",
|
||||||
|
"post_type": "post",
|
||||||
|
"content_id": 789,
|
||||||
|
"task_id": 456,
|
||||||
|
"last_synced": "2025-10-17 12:00:00"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Get Post by Task ID
|
||||||
|
|
||||||
|
**Endpoint**: `GET /wp-json/igny8/v1/post-by-task-id/{task_id}`
|
||||||
|
|
||||||
|
**Response**: Same format as above
|
||||||
|
|
||||||
|
### 3. Get Post Status by Content ID
|
||||||
|
|
||||||
|
**Endpoint**: `GET /wp-json/igny8/v1/post-status/{content_id}`
|
||||||
|
|
||||||
|
**Response**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"success": true,
|
||||||
|
"data": {
|
||||||
|
"post_id": 123,
|
||||||
|
"wordpress_status": "publish",
|
||||||
|
"igny8_status": "completed",
|
||||||
|
"status_mapping": {
|
||||||
|
"publish": "completed",
|
||||||
|
"draft": "draft",
|
||||||
|
"pending": "pending",
|
||||||
|
"private": "completed",
|
||||||
|
"trash": "archived",
|
||||||
|
"future": "scheduled"
|
||||||
|
},
|
||||||
|
"content_id": 789,
|
||||||
|
"url": "https://example.com/post/",
|
||||||
|
"last_synced": "2025-10-17 12:00:00"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Status Flow
|
||||||
|
|
||||||
|
### When Content Arrives from IGNY8
|
||||||
|
|
||||||
|
1. **Receive** content with `content_type`, `status`, `content_id`, `task_id`
|
||||||
|
2. **Map** IGNY8 status to WordPress status
|
||||||
|
3. **Create** WordPress post with mapped status
|
||||||
|
4. **Store** `content_id` and `task_id` in post meta
|
||||||
|
5. **Respond** to IGNY8 with:
|
||||||
|
- WordPress post ID
|
||||||
|
- WordPress actual status
|
||||||
|
- IGNY8 mapped status
|
||||||
|
- Post URL
|
||||||
|
- Content ID
|
||||||
|
|
||||||
|
### When WordPress Status Changes
|
||||||
|
|
||||||
|
1. **Detect** status change via `save_post` or `transition_post_status` hook
|
||||||
|
2. **Get** `task_id` and `content_id` from post meta
|
||||||
|
3. **Map** WordPress status to IGNY8 status
|
||||||
|
4. **Update** IGNY8 task with:
|
||||||
|
- WordPress actual status
|
||||||
|
- IGNY8 mapped status
|
||||||
|
- Post URL
|
||||||
|
- Content ID (if available)
|
||||||
|
- Sync timestamp
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Available Meta Fields
|
||||||
|
|
||||||
|
All fields are available for IGNY8 to read via REST API:
|
||||||
|
|
||||||
|
- `_igny8_task_id` - IGNY8 task ID
|
||||||
|
- `_igny8_content_id` - IGNY8 content ID
|
||||||
|
- `_igny8_cluster_id` - IGNY8 cluster ID
|
||||||
|
- `_igny8_sector_id` - IGNY8 sector ID
|
||||||
|
- `_igny8_keyword_ids` - Array of keyword IDs
|
||||||
|
- `_igny8_wordpress_status` - WordPress post status
|
||||||
|
- `_igny8_last_synced` - Last sync timestamp
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Query Examples
|
||||||
|
|
||||||
|
### Via WordPress REST API
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Get post by content_id
|
||||||
|
GET /wp-json/wp/v2/posts?meta_key=_igny8_content_id&meta_value=123
|
||||||
|
|
||||||
|
# Get post by task_id
|
||||||
|
GET /wp-json/wp/v2/posts?meta_key=_igny8_task_id&meta_value=456
|
||||||
|
|
||||||
|
# Get all IGNY8 posts
|
||||||
|
GET /wp-json/wp/v2/posts?meta_key=_igny8_task_id
|
||||||
|
```
|
||||||
|
|
||||||
|
### Via IGNY8 REST API Endpoints
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Get post by content_id (with status info)
|
||||||
|
GET /wp-json/igny8/v1/post-by-content-id/123
|
||||||
|
|
||||||
|
# Get post status only
|
||||||
|
GET /wp-json/igny8/v1/post-status/123
|
||||||
|
|
||||||
|
# Get post by task_id
|
||||||
|
GET /wp-json/igny8/v1/post-by-task-id/456
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Authentication
|
||||||
|
|
||||||
|
REST API endpoints require:
|
||||||
|
- IGNY8 API authentication (access token)
|
||||||
|
- Authorization header: `Bearer {access_token}`
|
||||||
|
|
||||||
|
Or internal use when IGNY8 is connected.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Status Flags Available
|
||||||
|
|
||||||
|
✅ **Task ID** - Stored and queryable
|
||||||
|
✅ **Content ID** - Stored and queryable
|
||||||
|
✅ **WordPress Status** - Stored and sent to IGNY8
|
||||||
|
✅ **IGNY8 Status** - Mapped and sent to IGNY8
|
||||||
|
✅ **Post Type** - Stored and sent to IGNY8
|
||||||
|
✅ **Content Type** - Stored and sent to IGNY8
|
||||||
|
✅ **Sync Timestamp** - Stored and sent to IGNY8
|
||||||
|
✅ **Post URL** - Sent to IGNY8
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**All status information is available for IGNY8 to read and query!**
|
||||||
|
|
||||||
186
igny8-wp-integration-plugin/docs/STYLE_GUIDE.md
Normal file
186
igny8-wp-integration-plugin/docs/STYLE_GUIDE.md
Normal file
@@ -0,0 +1,186 @@
|
|||||||
|
# Style Guide - IGNY8 WordPress Bridge
|
||||||
|
|
||||||
|
**Last Updated**: 2025-10-17
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## CSS Architecture
|
||||||
|
|
||||||
|
### No Inline CSS Policy
|
||||||
|
|
||||||
|
✅ **All styles are in `admin/assets/css/admin.css`**
|
||||||
|
❌ **No inline `style=""` attributes**
|
||||||
|
❌ **No `<style>` tags in PHP files**
|
||||||
|
|
||||||
|
### Global Design Updates
|
||||||
|
|
||||||
|
To update the design globally, **only edit**:
|
||||||
|
- `admin/assets/css/admin.css` - All admin styles
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## CSS Class Naming Convention
|
||||||
|
|
||||||
|
|
||||||
|
### Prefix
|
||||||
|
All classes use `igny8-` prefix to avoid conflicts.
|
||||||
|
|
||||||
|
### Naming Pattern
|
||||||
|
```
|
||||||
|
igny8-{component}-{element}-{modifier}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
|
||||||
|
#### Containers
|
||||||
|
- `.igny8-settings-container` - Main container
|
||||||
|
- `.igny8-settings-card` - Card component
|
||||||
|
- `.igny8-sync-actions` - Button group container
|
||||||
|
|
||||||
|
#### Status Indicators
|
||||||
|
- `.igny8-status-connected` - Connected status
|
||||||
|
- `.igny8-status-disconnected` - Disconnected status
|
||||||
|
- `.igny8-test-result` - Test result container
|
||||||
|
- `.igny8-test-result .igny8-success` - Success message
|
||||||
|
- `.igny8-test-result .igny8-error` - Error message
|
||||||
|
- `.igny8-test-result .igny8-loading` - Loading message
|
||||||
|
|
||||||
|
#### Sync Operations
|
||||||
|
- `.igny8-sync-status` - Sync status container
|
||||||
|
- `.igny8-sync-status-success` - Success state
|
||||||
|
- `.igny8-sync-status-error` - Error state
|
||||||
|
- `.igny8-sync-status-loading` - Loading state
|
||||||
|
|
||||||
|
#### Statistics
|
||||||
|
- `.igny8-stats-grid` - Statistics grid
|
||||||
|
- `.igny8-stat-item` - Individual stat item
|
||||||
|
- `.igny8-stat-label` - Stat label
|
||||||
|
- `.igny8-stat-value` - Stat value
|
||||||
|
|
||||||
|
#### Buttons
|
||||||
|
- `.igny8-button-group` - Button group container
|
||||||
|
- `.igny8-loading` - Loading state modifier
|
||||||
|
|
||||||
|
#### Messages
|
||||||
|
- `.igny8-message` - Base message class
|
||||||
|
- `.igny8-message-success` - Success message
|
||||||
|
- `.igny8-message-error` - Error message
|
||||||
|
- `.igny8-message-info` - Info message
|
||||||
|
- `.igny8-message-warning` - Warning message
|
||||||
|
|
||||||
|
#### Tables
|
||||||
|
- `.igny8-table` - Table component
|
||||||
|
- `.igny8-table th` - Table header
|
||||||
|
- `.igny8-table td` - Table cell
|
||||||
|
|
||||||
|
#### Loading
|
||||||
|
- `.igny8-spinner` - Loading spinner
|
||||||
|
- `.igny8-loading` - Loading state
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Color Scheme
|
||||||
|
|
||||||
|
All colors are defined in CSS for easy updates:
|
||||||
|
|
||||||
|
### Status Colors
|
||||||
|
- **Success**: `#46b450` (green)
|
||||||
|
- **Error**: `#dc3232` (red)
|
||||||
|
- **Info**: `#2271b1` (blue)
|
||||||
|
- **Warning**: `#f0b849` (amber)
|
||||||
|
|
||||||
|
### Background Colors
|
||||||
|
- **Success BG**: `#d4edda` / `#f0f8f0`
|
||||||
|
- **Error BG**: `#f8d7da` / `#fff5f5`
|
||||||
|
- **Info BG**: `#d1ecf1` / `#f0f6fc`
|
||||||
|
- **Warning BG**: `#fffbf0`
|
||||||
|
|
||||||
|
### Border Colors
|
||||||
|
- **Default**: `#ccd0d4`
|
||||||
|
- **Light**: `#ddd` / `#eee`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Responsive Breakpoints
|
||||||
|
|
||||||
|
```css
|
||||||
|
@media (max-width: 782px) {
|
||||||
|
/* Mobile styles */
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## How to Update Design
|
||||||
|
|
||||||
|
### Change Colors Globally
|
||||||
|
|
||||||
|
Edit `admin/assets/css/admin.css`:
|
||||||
|
|
||||||
|
```css
|
||||||
|
/* Status Colors */
|
||||||
|
.igny8-status-connected {
|
||||||
|
color: #YOUR_COLOR; /* Change here */
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Change Layout
|
||||||
|
|
||||||
|
Edit container classes in `admin.css`:
|
||||||
|
|
||||||
|
```css
|
||||||
|
.igny8-settings-container {
|
||||||
|
max-width: YOUR_WIDTH; /* Change here */
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Add New Styles
|
||||||
|
|
||||||
|
1. Add CSS class to `admin/assets/css/admin.css`
|
||||||
|
2. Use class in PHP/HTML (no inline styles)
|
||||||
|
3. Follow naming convention: `igny8-{component}-{element}`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## JavaScript Class Usage
|
||||||
|
|
||||||
|
JavaScript adds/removes CSS classes (no inline styles):
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
// ✅ Good - Uses CSS classes
|
||||||
|
$element.addClass('igny8-loading');
|
||||||
|
$element.addClass('igny8-sync-status-success');
|
||||||
|
|
||||||
|
// ❌ Bad - Inline styles
|
||||||
|
$element.css('color', 'green');
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## File Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
admin/assets/css/
|
||||||
|
└── admin.css ← ALL styles here
|
||||||
|
|
||||||
|
admin/assets/js/
|
||||||
|
└── admin.js ← Uses CSS classes only
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Checklist for New Features
|
||||||
|
|
||||||
|
When adding new UI elements:
|
||||||
|
|
||||||
|
- [ ] Define CSS classes in `admin.css`
|
||||||
|
- [ ] Use classes in PHP/HTML (no `style=""`)
|
||||||
|
- [ ] Use classes in JavaScript (no `.css()`)
|
||||||
|
- [ ] Follow naming convention
|
||||||
|
- [ ] Add responsive styles if needed
|
||||||
|
- [ ] Test on mobile/tablet
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Remember**: All design changes = Edit `admin.css` only! 🎨
|
||||||
|
|
||||||
@@ -0,0 +1,430 @@
|
|||||||
|
# IGNY8 WordPress Bridge – Verification Report (Phases 1-5)
|
||||||
|
|
||||||
|
**Date:** Generated automatically
|
||||||
|
**Scope:** Verification of implementation plan phases 1-5
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Phase 1 – Foundations ✅ **COMPLETE**
|
||||||
|
|
||||||
|
### 1.1 Harden Credential Storage
|
||||||
|
**Status:** ✅ **IMPLEMENTED**
|
||||||
|
|
||||||
|
**Location:**
|
||||||
|
- `includes/functions.php` (lines 20-111)
|
||||||
|
- `igny8_encrypt_value()` - AES-256-CBC encryption
|
||||||
|
- `igny8_decrypt_value()` - Decryption with fallback
|
||||||
|
- `igny8_store_secure_option()` - Secure storage wrapper
|
||||||
|
- `igny8_get_secure_option()` - Secure retrieval wrapper
|
||||||
|
|
||||||
|
- `includes/class-igny8-api.php` (lines 106-150)
|
||||||
|
- `refresh_token()` method implements token refresh tracking
|
||||||
|
- Stores `igny8_token_refreshed_at` and `igny8_access_token_issued` timestamps
|
||||||
|
|
||||||
|
**Verification:**
|
||||||
|
- ✅ Option encryption using OpenSSL AES-256-CBC
|
||||||
|
- ✅ Token refresh tracking with timestamps
|
||||||
|
- ✅ Secure option storage/retrieval functions
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 1.2 Build Diagnostics Card
|
||||||
|
**Status:** ✅ **IMPLEMENTED**
|
||||||
|
|
||||||
|
**Location:**
|
||||||
|
- `admin/settings.php` (lines 139-214)
|
||||||
|
|
||||||
|
**Features Implemented:**
|
||||||
|
- ✅ Access Token Age display (with human-readable time)
|
||||||
|
- ✅ Last API Health Check timestamp
|
||||||
|
- ✅ Last Site Data Sync timestamp
|
||||||
|
- ✅ Last Full Site Scan timestamp
|
||||||
|
- ✅ Last Semantic Map timestamp (with summary)
|
||||||
|
- ✅ Last Taxonomy Sync timestamp
|
||||||
|
- ✅ Last Keyword Sync timestamp
|
||||||
|
- ✅ Last Writer Sync timestamp
|
||||||
|
- ✅ Next Scheduled Site Scan display
|
||||||
|
|
||||||
|
**Note:** Health check uses `/planner/keywords/?page_size=1` as fallback (per plan note)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 1.3 Add Control Model + Post-Type/Woo Toggles
|
||||||
|
**Status:** ✅ **IMPLEMENTED**
|
||||||
|
|
||||||
|
**Location:**
|
||||||
|
- `admin/settings.php` (lines 229-330)
|
||||||
|
|
||||||
|
**Features Implemented:**
|
||||||
|
- ✅ Post Types to Sync (checkbox list with all supported types)
|
||||||
|
- ✅ IGNY8 Modules toggle (Sites, Planner, Writer, Linker, Optimizer)
|
||||||
|
- ✅ Control Mode radio buttons (Mirror/Hybrid)
|
||||||
|
- ✅ WooCommerce Data toggle (with detection)
|
||||||
|
|
||||||
|
**Supporting Functions:**
|
||||||
|
- `includes/functions.php`:
|
||||||
|
- `igny8_get_supported_post_types()` (lines 118-134)
|
||||||
|
- `igny8_get_enabled_post_types()` (lines 141-149)
|
||||||
|
- `igny8_get_control_mode()` (lines 156-159)
|
||||||
|
- `igny8_get_available_modules()` (lines 166-181)
|
||||||
|
- `igny8_get_enabled_modules()` (lines 188-196)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Phase 2 – Automatic Taxonomy & Keyword Sync ✅ **COMPLETE**
|
||||||
|
|
||||||
|
### 2.1 Scheduled Fetch of Sectors/Clusters
|
||||||
|
**Status:** ✅ **IMPLEMENTED**
|
||||||
|
|
||||||
|
**Location:**
|
||||||
|
- `sync/taxonomy-sync.php`:
|
||||||
|
- `igny8_sync_igny8_sectors_to_wp()` (lines 80-134)
|
||||||
|
- `igny8_sync_igny8_clusters_to_wp()` (lines 142-237)
|
||||||
|
- `igny8_cron_sync_taxonomies()` (lines 244-253)
|
||||||
|
|
||||||
|
**Scheduling:**
|
||||||
|
- `includes/functions.php` (line 490-492): Scheduled `twicedaily`
|
||||||
|
|
||||||
|
**API Endpoints Used:**
|
||||||
|
- ✅ `/planner/sites/{id}/sectors/` (GET)
|
||||||
|
- ✅ `/planner/sites/{id}/clusters/` (GET)
|
||||||
|
|
||||||
|
**Features:**
|
||||||
|
- ✅ Populates `igny8_sectors` taxonomy
|
||||||
|
- ✅ Populates `igny8_clusters` taxonomy
|
||||||
|
- ✅ Stores `_igny8_sector_id` and `_igny8_cluster_id` term meta
|
||||||
|
- ✅ Respects module toggle (skips if Planner disabled)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.2 Pull Planner Keywords Per Cluster
|
||||||
|
**Status:** ✅ **IMPLEMENTED**
|
||||||
|
|
||||||
|
**Location:**
|
||||||
|
- `sync/taxonomy-sync.php`:
|
||||||
|
- `igny8_sync_keywords_from_planner()` (lines 260-351)
|
||||||
|
- `igny8_cron_sync_keywords()` (lines 358-364)
|
||||||
|
|
||||||
|
**Scheduling:**
|
||||||
|
- `includes/functions.php` (line 495-497): Scheduled `daily`
|
||||||
|
|
||||||
|
**API Endpoints Used:**
|
||||||
|
- ✅ `/planner/keywords/?cluster_id={id}&page_size=500` (GET)
|
||||||
|
|
||||||
|
**Features:**
|
||||||
|
- ✅ Fetches keywords per cluster
|
||||||
|
- ✅ Stores `_igny8_keyword_ids` in term meta (cluster level)
|
||||||
|
- ✅ Stores `_igny8_keyword_ids` in post meta (post level)
|
||||||
|
- ✅ Respects module toggle (skips if Planner disabled)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.3 Render Read-Only Badges
|
||||||
|
**Status:** ✅ **IMPLEMENTED**
|
||||||
|
|
||||||
|
**Location:**
|
||||||
|
- `admin/class-admin-columns.php`:
|
||||||
|
- `render_sectors_column()` (lines 121-133)
|
||||||
|
- `render_clusters_column()` (lines 140-152)
|
||||||
|
- `render_source_column()` (lines 102-114)
|
||||||
|
|
||||||
|
**Features:**
|
||||||
|
- ✅ Displays sectors as badges in post list table
|
||||||
|
- ✅ Displays clusters as badges in post list table
|
||||||
|
- ✅ Shows IGNY8/WP source badge
|
||||||
|
- ✅ Read-only display (no editing in list view)
|
||||||
|
|
||||||
|
**Note:** Editor side panels would require additional meta box implementation (not explicitly required in plan)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Phase 3 – Writer Module Integration ✅ **MOSTLY COMPLETE**
|
||||||
|
|
||||||
|
### 3.1 Cron/Webhook to Fetch Tasks
|
||||||
|
**Status:** ✅ **IMPLEMENTED**
|
||||||
|
|
||||||
|
**Location:**
|
||||||
|
- `sync/igny8-to-wp.php`:
|
||||||
|
- `igny8_sync_igny8_tasks_to_wp()` (lines 265-441)
|
||||||
|
- `igny8_cron_sync_from_igny8()` (lines 756-795)
|
||||||
|
|
||||||
|
**Scheduling:**
|
||||||
|
- `includes/functions.php` (line 485-487): Scheduled `hourly`
|
||||||
|
|
||||||
|
**API Endpoints Used:**
|
||||||
|
- ✅ `/writer/tasks/?site_id={id}&status={status}` (GET)
|
||||||
|
|
||||||
|
**Features:**
|
||||||
|
- ✅ Fetches tasks with filters (status, cluster_id)
|
||||||
|
- ✅ Creates new WordPress posts from tasks
|
||||||
|
- ✅ Updates existing posts if task already linked
|
||||||
|
- ✅ Respects enabled post types
|
||||||
|
- ✅ Respects module toggle (skips if Writer disabled)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3.2 Hook save_post / transition_post_status
|
||||||
|
**Status:** ✅ **IMPLEMENTED**
|
||||||
|
|
||||||
|
**Location:**
|
||||||
|
- `sync/post-sync.php`:
|
||||||
|
- `igny8_sync_post_status_to_igny8()` (lines 22-78)
|
||||||
|
- `igny8_sync_post_status_transition()` (lines 137-169)
|
||||||
|
|
||||||
|
**Hooks Registered:**
|
||||||
|
- `sync/hooks.php` (lines 23, 28):
|
||||||
|
- ✅ `save_post` → `igny8_sync_post_status_to_igny8`
|
||||||
|
- ✅ `transition_post_status` → `igny8_sync_post_status_transition`
|
||||||
|
|
||||||
|
**API Endpoints Used:**
|
||||||
|
- ✅ `/writer/tasks/{id}/` (PUT)
|
||||||
|
|
||||||
|
**Features:**
|
||||||
|
- ✅ Pushes WordPress post status to IGNY8
|
||||||
|
- ✅ Pushes post URL to IGNY8
|
||||||
|
- ✅ Maps WordPress status to IGNY8 status
|
||||||
|
- ✅ Only syncs IGNY8-managed posts
|
||||||
|
- ✅ Skips autosaves and revisions
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3.3 Store Briefs/Outlines on Post Meta
|
||||||
|
**Status:** ✅ **IMPLEMENTED**
|
||||||
|
|
||||||
|
**Location:**
|
||||||
|
- `sync/igny8-to-wp.php`:
|
||||||
|
- `igny8_cache_task_brief()` (lines 49-64)
|
||||||
|
|
||||||
|
**API Endpoints Used:**
|
||||||
|
- ✅ `/writer/tasks/{id}/brief/` (GET)
|
||||||
|
|
||||||
|
**Features:**
|
||||||
|
- ✅ Fetches brief from IGNY8 API
|
||||||
|
- ✅ Stores in `_igny8_task_brief` post meta
|
||||||
|
- ✅ Stores cache timestamp in `_igny8_brief_cached_at`
|
||||||
|
- ✅ Called automatically when posts are created/updated
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3.4 Queue Visualization
|
||||||
|
**Status:** ⚠️ **PARTIALLY IMPLEMENTED**
|
||||||
|
|
||||||
|
**Location:**
|
||||||
|
- `admin/settings.php` (lines 365-377): Basic stats display
|
||||||
|
|
||||||
|
**Implemented:**
|
||||||
|
- ✅ Sync statistics section in settings
|
||||||
|
- ✅ Synced posts count (via AJAX)
|
||||||
|
- ✅ Last sync timestamp
|
||||||
|
|
||||||
|
**Missing:**
|
||||||
|
- ❌ Detailed queue counts (pending, processing, failed)
|
||||||
|
- ❌ Failure logs/visualization
|
||||||
|
- ❌ Queue status widget
|
||||||
|
|
||||||
|
**Recommendation:** Add detailed queue tracking table or widget showing:
|
||||||
|
- Pending syncs
|
||||||
|
- Failed syncs with error messages
|
||||||
|
- Processing status
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Phase 4 – Sites Module & Semantic Mapping ✅ **COMPLETE**
|
||||||
|
|
||||||
|
### 4.1 Incremental + Full Site-Data Collectors
|
||||||
|
**Status:** ✅ **IMPLEMENTED**
|
||||||
|
|
||||||
|
**Location:**
|
||||||
|
- `data/site-collection.php`:
|
||||||
|
- `igny8_collect_site_data()` (lines 277-364) - Full collection
|
||||||
|
- `igny8_sync_incremental_site_data()` (lines 411-498) - Incremental sync
|
||||||
|
- `igny8_fetch_wordpress_posts()` (lines 23-99)
|
||||||
|
- `igny8_fetch_all_taxonomy_terms()` (lines 254-270)
|
||||||
|
|
||||||
|
**Features:**
|
||||||
|
- ✅ Honors post type toggles (`igny8_is_post_type_enabled()`)
|
||||||
|
- ✅ Honors WooCommerce toggle
|
||||||
|
- ✅ Honors module toggle (skips if Sites disabled)
|
||||||
|
- ✅ Incremental mode (only changed posts since last sync)
|
||||||
|
- ✅ Full mode (all posts)
|
||||||
|
- ✅ Collects posts, taxonomies, terms
|
||||||
|
- ✅ Collects WooCommerce products (if enabled)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 4.2 Schedule Automatic Submission
|
||||||
|
**Status:** ✅ **IMPLEMENTED**
|
||||||
|
|
||||||
|
**Location:**
|
||||||
|
- `data/site-collection.php`:
|
||||||
|
- `igny8_send_site_data_to_igny8()` (lines 372-403)
|
||||||
|
- `igny8_perform_full_site_scan()` (lines 507-535)
|
||||||
|
|
||||||
|
**Scheduling:**
|
||||||
|
- `includes/functions.php`:
|
||||||
|
- Line 475-477: Daily incremental sync (`igny8_sync_site_data`)
|
||||||
|
- Line 480-482: Daily full scan check (`igny8_full_site_scan` - runs max once per week)
|
||||||
|
|
||||||
|
**API Endpoints Used:**
|
||||||
|
- ✅ `/system/sites/{id}/import/` (POST) - Full import
|
||||||
|
- ✅ `/system/sites/{id}/sync/` (POST) - Incremental sync
|
||||||
|
|
||||||
|
**Features:**
|
||||||
|
- ✅ Automatic daily incremental sync
|
||||||
|
- ✅ Automatic weekly full scan (throttled)
|
||||||
|
- ✅ Manual trigger available via settings page
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 4.3 Track Semantic Map Metadata
|
||||||
|
**Status:** ✅ **IMPLEMENTED**
|
||||||
|
|
||||||
|
**Location:**
|
||||||
|
- `data/semantic-mapping.php`:
|
||||||
|
- `igny8_map_site_to_semantic_strategy()` (lines 24-101)
|
||||||
|
|
||||||
|
**API Endpoints Used:**
|
||||||
|
- ✅ `/planner/sites/{id}/semantic-map/` (POST)
|
||||||
|
|
||||||
|
**Tracking:**
|
||||||
|
- `data/site-collection.php` (lines 526-531):
|
||||||
|
- ✅ Stores `igny8_last_semantic_map` timestamp
|
||||||
|
- ✅ Stores `igny8_last_semantic_map_summary` (sectors count, keywords count)
|
||||||
|
|
||||||
|
**Display:**
|
||||||
|
- `admin/settings.php` (lines 171-187): Shows semantic map summary in diagnostics
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Phase 5 – Planner, Linker, Optimizer Hooks ❌ **NOT IMPLEMENTED**
|
||||||
|
|
||||||
|
### 5.1 Display Planner Briefs/Tasks Per Post
|
||||||
|
**Status:** ❌ **NOT IMPLEMENTED**
|
||||||
|
|
||||||
|
**Required:**
|
||||||
|
- Display Planner briefs/tasks per post in editor
|
||||||
|
- Allow "request refresh" actions
|
||||||
|
|
||||||
|
**API Endpoints (from plan):**
|
||||||
|
- `/planner/tasks/{id}/brief/` (GET)
|
||||||
|
- `/planner/tasks/{id}/refresh/` (POST)
|
||||||
|
|
||||||
|
**Current State:**
|
||||||
|
- ✅ Briefs are cached in post meta (`_igny8_task_brief`)
|
||||||
|
- ❌ No UI to display briefs in post editor
|
||||||
|
- ❌ No "request refresh" action/button
|
||||||
|
- ❌ No Planner task display per post
|
||||||
|
|
||||||
|
**Recommendation:** Implement:
|
||||||
|
1. Meta box in post editor showing cached brief
|
||||||
|
2. Button to fetch fresh brief from `/planner/tasks/{id}/brief/`
|
||||||
|
3. Button to trigger refresh via `/planner/tasks/{id}/refresh/`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5.2 Export WP Link Graph During Site Scans
|
||||||
|
**Status:** ❌ **NOT IMPLEMENTED**
|
||||||
|
|
||||||
|
**Required:**
|
||||||
|
- Export WordPress internal link graph during site scans
|
||||||
|
- Send to `/linker/link-map/` (POST)
|
||||||
|
|
||||||
|
**Current State:**
|
||||||
|
- ❌ No link graph extraction
|
||||||
|
- ❌ No link graph export to IGNY8
|
||||||
|
- ❌ Not integrated into site scan process
|
||||||
|
|
||||||
|
**Recommendation:** Implement:
|
||||||
|
1. Function to extract internal links from posts (source_url, target_url, anchor)
|
||||||
|
2. Include link graph in `igny8_collect_site_data()` output
|
||||||
|
3. Send link graph to `/linker/link-map/` during full site scans
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5.3 Accept Link Recommendations Via Webhook
|
||||||
|
**Status:** ❌ **NOT IMPLEMENTED** (This is Phase 6)
|
||||||
|
|
||||||
|
**Note:** This requirement is listed in Phase 5 but implementation belongs to Phase 6 (Webhooks & Remote Control). The REST endpoint `/wp-json/igny8/v1/event` is not yet implemented.
|
||||||
|
|
||||||
|
**Required (for Phase 6):**
|
||||||
|
- REST endpoint `/wp-json/igny8/v1/event` with shared secret
|
||||||
|
- Handler for `action: "insert_link"` events
|
||||||
|
- Queue link insertions
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5.4 Expose Optimizer Triggers
|
||||||
|
**Status:** ❌ **NOT IMPLEMENTED**
|
||||||
|
|
||||||
|
**Required:**
|
||||||
|
- Expose optimizer triggers (metrics requests, re-optimization)
|
||||||
|
- Endpoints: `/optimizer/jobs/` (POST), `/optimizer/jobs/{id}/` (GET)
|
||||||
|
|
||||||
|
**Current State:**
|
||||||
|
- ✅ Optimizer module is listed in available modules
|
||||||
|
- ❌ No functions to create optimizer jobs
|
||||||
|
- ❌ No functions to fetch optimizer job status
|
||||||
|
- ❌ No UI to trigger optimizer actions
|
||||||
|
|
||||||
|
**Recommendation:** Implement:
|
||||||
|
1. Function `igny8_create_optimizer_job($post_id, $job_type, $priority)`
|
||||||
|
2. Function `igny8_get_optimizer_job_status($job_id)`
|
||||||
|
3. UI in post editor or settings to trigger optimization
|
||||||
|
4. Display optimizer scores/recommendations
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
| Phase | Status | Completion |
|
||||||
|
|-------|--------|------------|
|
||||||
|
| **Phase 1** | ✅ Complete | 100% |
|
||||||
|
| **Phase 2** | ✅ Complete | 100% |
|
||||||
|
| **Phase 3** | ⚠️ Mostly Complete | 90% (missing detailed queue visualization) |
|
||||||
|
| **Phase 4** | ✅ Complete | 100% |
|
||||||
|
| **Phase 5** | ❌ Not Implemented | 0% |
|
||||||
|
|
||||||
|
**Overall Completion (Phases 1-5):** ~78%
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Recommendations
|
||||||
|
|
||||||
|
### High Priority (Phase 5)
|
||||||
|
1. **Implement Planner briefs display** (5.1)
|
||||||
|
- Add meta box in post editor
|
||||||
|
- Add refresh action buttons
|
||||||
|
- Display cached briefs
|
||||||
|
|
||||||
|
2. **Implement link graph export** (5.2)
|
||||||
|
- Extract internal links from posts
|
||||||
|
- Include in site scan data
|
||||||
|
- Send to `/linker/link-map/` endpoint
|
||||||
|
|
||||||
|
3. **Implement optimizer triggers** (5.4)
|
||||||
|
- Create optimizer job functions
|
||||||
|
- Add UI to trigger optimization
|
||||||
|
- Display optimizer results
|
||||||
|
|
||||||
|
### Medium Priority
|
||||||
|
4. **Enhance queue visualization** (3.4)
|
||||||
|
- Add detailed queue status table
|
||||||
|
- Show failure logs
|
||||||
|
- Add retry mechanisms
|
||||||
|
|
||||||
|
### Low Priority
|
||||||
|
5. **Phase 6 preparation** (5.3)
|
||||||
|
- Plan webhook endpoint structure
|
||||||
|
- Design shared secret authentication
|
||||||
|
- Plan link insertion queue system
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
- All implemented phases follow the plan specifications correctly
|
||||||
|
- API endpoints are used as specified in the plan
|
||||||
|
- Module toggles and post type filters are respected throughout
|
||||||
|
- Cron scheduling is properly configured
|
||||||
|
- Token refresh and credential security are properly implemented
|
||||||
|
|
||||||
2084
igny8-wp-integration-plugin/docs/WORDPRESS-PLUGIN-INTEGRATION.md
Normal file
2084
igny8-wp-integration-plugin/docs/WORDPRESS-PLUGIN-INTEGRATION.md
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,27 @@
|
|||||||
|
# IGNY8 SaaS – Missing / Pending API Endpoints
|
||||||
|
|
||||||
|
These endpoints are referenced in the WordPress bridge plan but are not currently available (or confirmed) in the SaaS API set. The SaaS team should implement or confirm them so the bridge can automate end-to-end flows.
|
||||||
|
|
||||||
|
| ID | Purpose | Proposed Endpoint | Method | Payload / Notes |
|
||||||
|
| --- | --- | --- | --- | --- |
|
||||||
|
| M1 | Pull recommended WordPress integration settings (post types, module toggles) after authentication | `/system/sites/{id}/settings/` | GET | Returns flags such as `enable_products`, `allow_wp_edits`, feature rollouts. |
|
||||||
|
| M2 | Lightweight health check for diagnostics panel | `/system/ping/` | GET | Responds with `{success:true, time, version}` so WP can show “connected” without hitting data-heavy routes. |
|
||||||
|
| M3 | Retrieve writer brief/outlines linked to a task | `/writer/tasks/{id}/brief/` | GET | Should return structured brief content, tone, keywords, outline items. |
|
||||||
|
| M4 | Trigger planner refresh for a post/cluster from WP | `/planner/tasks/{id}/refresh/` | POST | Body includes `wordpress_post_id`, reason (`"reoptimize"`), optional notes. |
|
||||||
|
| M5 | Upload WordPress internal link graph for Linker module | `/linker/link-map/` | POST | Payload: list of `{source_url, target_url, anchor}` records plus site ID. Should accept batches. |
|
||||||
|
| M6 | Push Linker recommendations back to WordPress | **Webhook to WP** | POST | SaaS calls WP endpoint with `{action:"insert_link", post_id, anchor, target_url}`. Requires ability to configure per-site shared secret. |
|
||||||
|
| M7 | Create Optimizer job (e.g., re-audit a post) initiated from WP | `/optimizer/jobs/` | POST | Body: `{post_id, task_id, job_type, priority}` returning job ID + status. |
|
||||||
|
| M8 | Fetch Optimizer job status/results for UI | `/optimizer/jobs/{id}/` | GET | Response should include `status`, `score_changes`, `recommendations`. |
|
||||||
|
| M9 | Report per-site sync status back to WP for monitoring | `/system/sites/{id}/status/` | GET | Returns last processed webhook, queued jobs, outstanding recommendations. |
|
||||||
|
| M10 | Notify WP when a new Writer task is ready (to avoid polling) | **Webhook to WP** | POST | Payload: `{task_id, cluster_id, title, status}`; WP responds 200 and enqueues ingestion. Provide retry/backoff headers. |
|
||||||
|
|
||||||
|
## Webhook Security Requirements
|
||||||
|
1. SaaS must send `X-IGNY8-Signature` (HMAC-SHA256 over body using shared secret).
|
||||||
|
2. Includes `X-IGNY8-Timestamp` to prevent replay.
|
||||||
|
3. WordPress bridge will validate signature before enqueuing actions.
|
||||||
|
|
||||||
|
## Next Steps for SaaS Team
|
||||||
|
1. Confirm which endpoints already exist (rename if different) and document response schemas.
|
||||||
|
2. Implement missing endpoints/webhooks and expose them via API documentation.
|
||||||
|
3. Provide sample payloads + Postman collection so WP bridge devs can test without full app context.
|
||||||
|
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
# IGNY8 WordPress Bridge – Implementation Plan
|
||||||
|
|
||||||
|
## Objectives
|
||||||
|
- Keep WordPress as a lightweight publishing edge while IGNY8 SaaS remains the control plane.
|
||||||
|
- Automate all sync flows once the site connects (no repeated manual buttons).
|
||||||
|
- Provide hooks for Sites, Planner, Writer, Linker, and Optimizer modules using the endpoints defined in `WORDPRESS-PLUGIN-INTEGRATION.md`.
|
||||||
|
|
||||||
|
## Assumptions
|
||||||
|
1. Existing IGNY8 REST API endpoints (auth, sites, planner, writer) follow the unified response format.
|
||||||
|
2. WordPress environment runs PHP ≥ 7.4 with REST API enabled and (optionally) WooCommerce.
|
||||||
|
3. SaaS app can call back into WordPress via authenticated REST endpoints/webhooks.
|
||||||
|
|
||||||
|
## Work Breakdown
|
||||||
|
|
||||||
|
### Phase 1 – Foundations
|
||||||
|
| Step | WordPress Tasks | IGNY8 API Usage |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| 1.1 | Harden credential storage (option encryption, refresh tracking) | `/auth/login/`, `/auth/refresh/` |
|
||||||
|
| 1.2 | Build diagnostics card (ping, last sync, token age) | `/system/ping/` (or `/planner/keywords/?page_size=1` as health check) |
|
||||||
|
| 1.3 | Add “control model” + post-type/Woo toggles on settings page | `GET /system/sites/{id}/` (to pull recommended defaults) |
|
||||||
|
|
||||||
|
### Phase 2 – Automatic Taxonomy & Keyword Sync
|
||||||
|
| Step | WordPress Tasks | IGNY8 API Usage |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| 2.1 | Scheduled fetch of sectors/clusters; populate `igny8_sectors`, `igny8_clusters` | `/planner/sectors/`, `/planner/clusters/` |
|
||||||
|
| 2.2 | Pull Planner keywords per cluster and store `_igny8_keyword_ids` meta | `/planner/keywords/?cluster_id=` |
|
||||||
|
| 2.3 | Render read-only badges in list tables and editor side panels | — (local data) |
|
||||||
|
|
||||||
|
### Phase 3 – Writer Module Integration
|
||||||
|
| Step | WordPress Tasks | IGNY8 API Usage |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| 3.1 | Cron or webhook to fetch new/updated tasks and create posts | `/writer/tasks/?site_id=&status=` |
|
||||||
|
| 3.2 | Hook `save_post` / `transition_post_status` to push status & URLs | `/writer/tasks/{id}/` (PUT) |
|
||||||
|
| 3.3 | Store briefs/outlines on post meta for editors | `/writer/tasks/{id}/brief/` |
|
||||||
|
| 3.4 | Queue visualization (counts, failures) in settings page | — (local logs) |
|
||||||
|
|
||||||
|
### Phase 4 – Sites Module & Semantic Mapping
|
||||||
|
| Step | WordPress Tasks | IGNY8 API Usage |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| 4.1 | Implement incremental + full site-data collectors honoring toggles | — (WP data) |
|
||||||
|
| 4.2 | Schedule automatic submission of site payloads | `/system/sites/{id}/import/` (full) |
|
||||||
|
| 4.3 | Track semantic map metadata locally for reporting | `/planner/sites/{id}/semantic-map/` |
|
||||||
|
|
||||||
|
### Phase 5 – Planner, Linker, Optimizer Hooks
|
||||||
|
| Step | WordPress Tasks | IGNY8 API Usage |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| 5.1 | Display Planner briefs/tasks per post; allow “request refresh” actions | `/planner/tasks/{id}/brief/`, `/planner/tasks/{id}/refresh/` |
|
||||||
|
| 5.2 | Export WP link graph during site scans | `/linker/link-map/` (POST) |
|
||||||
|
| 5.3 | Accept link recommendations via webhook and queue insertions | SaaS → WP REST endpoint (new) |
|
||||||
|
| 5.4 | Expose optimizer triggers (metrics requests, re-optimization) | `/optimizer/jobs/` (POST), `/optimizer/jobs/{id}/` |
|
||||||
|
|
||||||
|
### Phase 6 – Webhooks & Remote Control
|
||||||
|
| Step | WordPress Tasks | IGNY8 API Usage |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| 6.1 | Register REST routes (`/wp-json/igny8/v1/event`) with shared secret | — (incoming) |
|
||||||
|
| 6.2 | Handle SaaS events: task published, link recommendation, optimizer request | SaaS posts payloads; responses follow unified JSON |
|
||||||
|
| 6.3 | Persist webhook activity logs (custom table or option) | — |
|
||||||
|
|
||||||
|
### Phase 7 – Automation & Monitoring
|
||||||
|
| Step | WordPress Tasks | IGNY8 API Usage |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| 7.1 | WP-CLI commands (`wp igny8 sync posts`, `sync site`) to mirror SaaS automation | Same endpoints as UI |
|
||||||
|
| 7.2 | Admin notices + status widget for sync health (error counts, tokens) | `/system/sites/{id}/status/` (if available) |
|
||||||
|
| 7.3 | Structured logging (maybe `wp_igny8_logs` table) for audits | — |
|
||||||
|
|
||||||
|
## Deliverables
|
||||||
|
1. Updated plugin code implementing all phases.
|
||||||
|
2. Settings UI redesign with automatic operations (no manual sync buttons post-setup).
|
||||||
|
3. REST endpoints + cron jobs documented for SaaS team.
|
||||||
|
4. Test plan covering happy path, token expiry, webhook retries.
|
||||||
|
|
||||||
|
## Timeline (Rough)
|
||||||
|
| Phase | Est. Effort | Notes |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| 1 | 2–3 days | Mostly admin UI + auth refactor |
|
||||||
|
| 2 | 3 days | Taxonomies + keywords + scheduling |
|
||||||
|
| 3 | 3–4 days | Writer hooks + automatic task ingestion |
|
||||||
|
| 4 | 2 days | Site data automation |
|
||||||
|
| 5 | 4 days | Requires close coordination with SaaS teams |
|
||||||
|
| 6 | 2 days | REST/webhook security |
|
||||||
|
| 7 | 1–2 days | Tooling & monitoring |
|
||||||
|
|
||||||
|
Effort assumes endpoints already exist; see `docs/missing-saas-api-endpoints.md` for gaps.
|
||||||
|
|
||||||
183
igny8-wp-integration-plugin/igny8-bridge.php
Normal file
183
igny8-wp-integration-plugin/igny8-bridge.php
Normal file
@@ -0,0 +1,183 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Plugin Name: IGNY8 WordPress Bridge
|
||||||
|
* Plugin URI: https://github.com/your-repo/igny8-ai-os
|
||||||
|
* Description: Lightweight bridge plugin that connects WordPress to IGNY8 API. Syncs posts, taxonomies, and site data bidirectionally.
|
||||||
|
* Version: 1.0.0
|
||||||
|
* Author: Your Name
|
||||||
|
* Author URI: https://yourwebsite.com
|
||||||
|
* License: GPL v2 or later
|
||||||
|
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
|
||||||
|
* Text Domain: igny8-bridge
|
||||||
|
* Domain Path: /languages
|
||||||
|
* Requires at least: 5.0
|
||||||
|
* Requires PHP: 7.4
|
||||||
|
*
|
||||||
|
* @package Igny8Bridge
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Prevent direct access
|
||||||
|
if (!defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Define plugin constants
|
||||||
|
define('IGNY8_BRIDGE_VERSION', '1.0.0');
|
||||||
|
define('IGNY8_BRIDGE_PLUGIN_DIR', plugin_dir_path(__FILE__));
|
||||||
|
define('IGNY8_BRIDGE_PLUGIN_URL', plugin_dir_url(__FILE__));
|
||||||
|
define('IGNY8_BRIDGE_PLUGIN_FILE', __FILE__);
|
||||||
|
define('IGNY8_BRIDGE_PLUGIN_BASENAME', plugin_basename(__FILE__));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main plugin class
|
||||||
|
*/
|
||||||
|
class Igny8Bridge {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Single instance of the class
|
||||||
|
*
|
||||||
|
* @var Igny8Bridge
|
||||||
|
*/
|
||||||
|
private static $instance = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get single instance
|
||||||
|
*
|
||||||
|
* @return Igny8Bridge
|
||||||
|
*/
|
||||||
|
public static function get_instance() {
|
||||||
|
if (null === self::$instance) {
|
||||||
|
self::$instance = new self();
|
||||||
|
}
|
||||||
|
return self::$instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
private function __construct() {
|
||||||
|
$this->init();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize plugin
|
||||||
|
*/
|
||||||
|
private function init() {
|
||||||
|
// Load core files
|
||||||
|
$this->load_dependencies();
|
||||||
|
|
||||||
|
// Initialize hooks
|
||||||
|
add_action('plugins_loaded', array($this, 'load_plugin_textdomain'));
|
||||||
|
add_action('init', array($this, 'init_plugin'));
|
||||||
|
|
||||||
|
// Activation/Deactivation hooks
|
||||||
|
register_activation_hook(__FILE__, array($this, 'activate'));
|
||||||
|
register_deactivation_hook(__FILE__, array($this, 'deactivate'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load plugin dependencies
|
||||||
|
*/
|
||||||
|
private function load_dependencies() {
|
||||||
|
// Core classes
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'includes/functions.php';
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'includes/class-igny8-api.php';
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'includes/class-igny8-site.php';
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'includes/class-igny8-rest-api.php';
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'includes/class-igny8-webhooks.php';
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'includes/class-igny8-link-queue.php';
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'includes/class-igny8-webhook-logs.php';
|
||||||
|
|
||||||
|
// Admin classes (only in admin)
|
||||||
|
if (is_admin()) {
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'admin/class-admin.php';
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'admin/class-admin-columns.php';
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'admin/class-post-meta-boxes.php';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sync handlers
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'sync/hooks.php';
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'sync/post-sync.php';
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'sync/taxonomy-sync.php';
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'sync/igny8-to-wp.php';
|
||||||
|
|
||||||
|
// Data collection
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'data/site-collection.php';
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'data/semantic-mapping.php';
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'data/link-graph.php';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load plugin textdomain
|
||||||
|
*/
|
||||||
|
public function load_plugin_textdomain() {
|
||||||
|
load_plugin_textdomain(
|
||||||
|
'igny8-bridge',
|
||||||
|
false,
|
||||||
|
dirname(IGNY8_BRIDGE_PLUGIN_BASENAME) . '/languages'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize plugin functionality
|
||||||
|
*/
|
||||||
|
public function init_plugin() {
|
||||||
|
// Register post meta fields
|
||||||
|
igny8_register_post_meta();
|
||||||
|
|
||||||
|
// Register taxonomies
|
||||||
|
igny8_register_taxonomies();
|
||||||
|
|
||||||
|
// Initialize admin (if in admin)
|
||||||
|
if (is_admin()) {
|
||||||
|
Igny8Admin::get_instance();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize sync handlers
|
||||||
|
if (class_exists('Igny8WordPressSync')) {
|
||||||
|
new Igny8WordPressSync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plugin activation
|
||||||
|
*/
|
||||||
|
public function activate() {
|
||||||
|
// Register post meta and taxonomies
|
||||||
|
igny8_register_post_meta();
|
||||||
|
igny8_register_taxonomies();
|
||||||
|
|
||||||
|
// Flush rewrite rules
|
||||||
|
flush_rewrite_rules();
|
||||||
|
|
||||||
|
// Set default options
|
||||||
|
if (!get_option('igny8_bridge_version')) {
|
||||||
|
add_option('igny8_bridge_version', IGNY8_BRIDGE_VERSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schedule cron jobs
|
||||||
|
igny8_schedule_cron_jobs();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plugin deactivation
|
||||||
|
*/
|
||||||
|
public function deactivate() {
|
||||||
|
// Unschedule cron jobs
|
||||||
|
igny8_unschedule_cron_jobs();
|
||||||
|
|
||||||
|
// Flush rewrite rules
|
||||||
|
flush_rewrite_rules();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize plugin
|
||||||
|
*/
|
||||||
|
function igny8_bridge_init() {
|
||||||
|
return Igny8Bridge::get_instance();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start the plugin
|
||||||
|
igny8_bridge_init();
|
||||||
|
|
||||||
328
igny8-wp-integration-plugin/includes/class-igny8-api.php
Normal file
328
igny8-wp-integration-plugin/includes/class-igny8-api.php
Normal file
@@ -0,0 +1,328 @@
|
|||||||
|
<?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
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $base_url = 'https://api.igny8.com/api/v1';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Access token
|
||||||
|
*
|
||||||
|
* @var string|null
|
||||||
|
*/
|
||||||
|
private $access_token = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refresh token
|
||||||
|
*
|
||||||
|
* @var string|null
|
||||||
|
*/
|
||||||
|
private $refresh_token = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
public function __construct() {
|
||||||
|
if (function_exists('igny8_get_secure_option')) {
|
||||||
|
$this->access_token = igny8_get_secure_option('igny8_access_token');
|
||||||
|
$this->refresh_token = igny8_get_secure_option('igny8_refresh_token');
|
||||||
|
} else {
|
||||||
|
$this->access_token = get_option('igny8_access_token');
|
||||||
|
$this->refresh_token = get_option('igny8_refresh_token');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Login and store tokens
|
||||||
|
*
|
||||||
|
* @param string $email User email
|
||||||
|
* @param string $password User password
|
||||||
|
* @return bool True on success, false on failure
|
||||||
|
*/
|
||||||
|
public function login($email, $password) {
|
||||||
|
$response = wp_remote_post($this->base_url . '/auth/login/', array(
|
||||||
|
'headers' => array(
|
||||||
|
'Content-Type' => 'application/json'
|
||||||
|
),
|
||||||
|
'body' => json_encode(array(
|
||||||
|
'email' => $email,
|
||||||
|
'password' => $password
|
||||||
|
)),
|
||||||
|
'timeout' => 30
|
||||||
|
));
|
||||||
|
|
||||||
|
$body = $this->parse_response($response);
|
||||||
|
|
||||||
|
if ($body['success']) {
|
||||||
|
$this->access_token = $body['data']['access'];
|
||||||
|
$this->refresh_token = $body['data']['refresh'];
|
||||||
|
|
||||||
|
if (function_exists('igny8_store_secure_option')) {
|
||||||
|
igny8_store_secure_option('igny8_access_token', $this->access_token);
|
||||||
|
igny8_store_secure_option('igny8_refresh_token', $this->refresh_token);
|
||||||
|
} else {
|
||||||
|
update_option('igny8_access_token', $this->access_token);
|
||||||
|
update_option('igny8_refresh_token', $this->refresh_token);
|
||||||
|
}
|
||||||
|
update_option('igny8_email', $email);
|
||||||
|
|
||||||
|
// Store token refresh time
|
||||||
|
$timestamp = current_time('timestamp');
|
||||||
|
update_option('igny8_token_refreshed_at', $timestamp);
|
||||||
|
update_option('igny8_access_token_issued', $timestamp);
|
||||||
|
update_option('igny8_last_api_health_check', $timestamp);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refresh access token
|
||||||
|
*
|
||||||
|
* @return bool True on success, false on failure
|
||||||
|
*/
|
||||||
|
public function refresh_token() {
|
||||||
|
if (!$this->refresh_token) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = wp_remote_post($this->base_url . '/auth/refresh/', array(
|
||||||
|
'headers' => array(
|
||||||
|
'Content-Type' => 'application/json'
|
||||||
|
),
|
||||||
|
'body' => json_encode(array(
|
||||||
|
'refresh' => $this->refresh_token
|
||||||
|
)),
|
||||||
|
'timeout' => 30
|
||||||
|
));
|
||||||
|
|
||||||
|
$body = $this->parse_response($response);
|
||||||
|
|
||||||
|
if ($body['success']) {
|
||||||
|
$this->access_token = $body['data']['access'];
|
||||||
|
|
||||||
|
// Refresh token may be updated
|
||||||
|
if (isset($body['data']['refresh'])) {
|
||||||
|
$this->refresh_token = $body['data']['refresh'];
|
||||||
|
if (function_exists('igny8_store_secure_option')) {
|
||||||
|
igny8_store_secure_option('igny8_refresh_token', $this->refresh_token);
|
||||||
|
} else {
|
||||||
|
update_option('igny8_refresh_token', $this->refresh_token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (function_exists('igny8_store_secure_option')) {
|
||||||
|
igny8_store_secure_option('igny8_access_token', $this->access_token);
|
||||||
|
} else {
|
||||||
|
update_option('igny8_access_token', $this->access_token);
|
||||||
|
}
|
||||||
|
|
||||||
|
$timestamp = current_time('timestamp');
|
||||||
|
update_option('igny8_token_refreshed_at', $timestamp);
|
||||||
|
update_option('igny8_access_token_issued', $timestamp);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$body = json_decode(wp_remote_retrieve_body($response), true);
|
||||||
|
$status_code = wp_remote_retrieve_response_code($response);
|
||||||
|
|
||||||
|
// Handle non-JSON responses
|
||||||
|
if (!$body) {
|
||||||
|
return array(
|
||||||
|
'success' => false,
|
||||||
|
'error' => 'Invalid response format'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if response follows unified format
|
||||||
|
if (isset($body['success'])) {
|
||||||
|
return $body;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Legacy format - wrap in unified format
|
||||||
|
if ($status_code >= 200 && $status_code < 300) {
|
||||||
|
return array(
|
||||||
|
'success' => true,
|
||||||
|
'data' => $body
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return array(
|
||||||
|
'success' => false,
|
||||||
|
'error' => $body['detail'] ?? 'Unknown error'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get headers with authentication
|
||||||
|
*
|
||||||
|
* @return array Headers array
|
||||||
|
* @throws Exception If not authenticated
|
||||||
|
*/
|
||||||
|
private function get_headers() {
|
||||||
|
if (!$this->access_token) {
|
||||||
|
throw new Exception('Not authenticated');
|
||||||
|
}
|
||||||
|
|
||||||
|
return array(
|
||||||
|
'Authorization' => 'Bearer ' . $this->access_token,
|
||||||
|
'Content-Type' => 'application/json'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make GET request
|
||||||
|
*
|
||||||
|
* @param string $endpoint API endpoint
|
||||||
|
* @return array Response data
|
||||||
|
*/
|
||||||
|
public function get($endpoint) {
|
||||||
|
$response = wp_remote_get($this->base_url . $endpoint, array(
|
||||||
|
'headers' => $this->get_headers(),
|
||||||
|
'timeout' => 30
|
||||||
|
));
|
||||||
|
|
||||||
|
$body = $this->parse_response($response);
|
||||||
|
|
||||||
|
// Handle 401 - token expired
|
||||||
|
if (!$body['success'] && wp_remote_retrieve_response_code($response) == 401) {
|
||||||
|
// Try to refresh token
|
||||||
|
if ($this->refresh_token()) {
|
||||||
|
// Retry request
|
||||||
|
$response = wp_remote_get($this->base_url . $endpoint, array(
|
||||||
|
'headers' => $this->get_headers(),
|
||||||
|
'timeout' => 30
|
||||||
|
));
|
||||||
|
$body = $this->parse_response($response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $body;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make POST request
|
||||||
|
*
|
||||||
|
* @param string $endpoint API endpoint
|
||||||
|
* @param array $data Request data
|
||||||
|
* @return array Response data
|
||||||
|
*/
|
||||||
|
public function post($endpoint, $data) {
|
||||||
|
$response = wp_remote_post($this->base_url . $endpoint, array(
|
||||||
|
'headers' => $this->get_headers(),
|
||||||
|
'body' => json_encode($data),
|
||||||
|
'timeout' => 60
|
||||||
|
));
|
||||||
|
|
||||||
|
$body = $this->parse_response($response);
|
||||||
|
|
||||||
|
// Handle 401 - token expired
|
||||||
|
if (!$body['success'] && wp_remote_retrieve_response_code($response) == 401) {
|
||||||
|
// Try to refresh token
|
||||||
|
if ($this->refresh_token()) {
|
||||||
|
// Retry request
|
||||||
|
$response = wp_remote_post($this->base_url . $endpoint, array(
|
||||||
|
'headers' => $this->get_headers(),
|
||||||
|
'body' => json_encode($data),
|
||||||
|
'timeout' => 60
|
||||||
|
));
|
||||||
|
$body = $this->parse_response($response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $body;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make PUT request
|
||||||
|
*
|
||||||
|
* @param string $endpoint API endpoint
|
||||||
|
* @param array $data Request data
|
||||||
|
* @return array Response data
|
||||||
|
*/
|
||||||
|
public function put($endpoint, $data) {
|
||||||
|
$response = wp_remote_request($this->base_url . $endpoint, array(
|
||||||
|
'method' => 'PUT',
|
||||||
|
'headers' => $this->get_headers(),
|
||||||
|
'body' => json_encode($data),
|
||||||
|
'timeout' => 60
|
||||||
|
));
|
||||||
|
|
||||||
|
return $this->parse_response($response);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make DELETE request
|
||||||
|
*
|
||||||
|
* @param string $endpoint API endpoint
|
||||||
|
* @return array Response data
|
||||||
|
*/
|
||||||
|
public function delete($endpoint) {
|
||||||
|
$response = wp_remote_request($this->base_url . $endpoint, array(
|
||||||
|
'method' => 'DELETE',
|
||||||
|
'headers' => $this->get_headers(),
|
||||||
|
'timeout' => 30
|
||||||
|
));
|
||||||
|
|
||||||
|
return $this->parse_response($response);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if authenticated
|
||||||
|
*
|
||||||
|
* @return bool True if authenticated
|
||||||
|
*/
|
||||||
|
public function is_authenticated() {
|
||||||
|
return !empty($this->access_token);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get access token
|
||||||
|
*
|
||||||
|
* @return string|null Access token
|
||||||
|
*/
|
||||||
|
public function get_access_token() {
|
||||||
|
return $this->access_token;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
202
igny8-wp-integration-plugin/includes/class-igny8-link-queue.php
Normal file
202
igny8-wp-integration-plugin/includes/class-igny8-link-queue.php
Normal 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');
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
118
igny8-wp-integration-plugin/includes/class-igny8-site.php
Normal file
118
igny8-wp-integration-plugin/includes/class-igny8-site.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
381
igny8-wp-integration-plugin/includes/class-igny8-webhooks.php
Normal file
381
igny8-wp-integration-plugin/includes/class-igny8-webhooks.php
Normal 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();
|
||||||
|
|
||||||
605
igny8-wp-integration-plugin/includes/functions.php
Normal file
605
igny8-wp-integration-plugin/includes/functions.php
Normal file
@@ -0,0 +1,605 @@
|
|||||||
|
<?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 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
|
||||||
|
*/
|
||||||
|
function igny8_is_connection_enabled() {
|
||||||
|
$enabled = get_option('igny8_connection_enabled', 1);
|
||||||
|
return (bool) $enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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_task_id' => array(
|
||||||
|
'type' => 'integer',
|
||||||
|
'description' => 'IGNY8 task ID linked to this post',
|
||||||
|
'single' => true,
|
||||||
|
'show_in_rest' => true,
|
||||||
|
),
|
||||||
|
'_igny8_cluster_id' => array(
|
||||||
|
'type' => 'integer',
|
||||||
|
'description' => 'IGNY8 cluster ID',
|
||||||
|
'single' => true,
|
||||||
|
'show_in_rest' => true,
|
||||||
|
),
|
||||||
|
'_igny8_sector_id' => array(
|
||||||
|
'type' => 'integer',
|
||||||
|
'description' => 'IGNY8 sector ID',
|
||||||
|
'single' => true,
|
||||||
|
'show_in_rest' => true,
|
||||||
|
),
|
||||||
|
'_igny8_keyword_ids' => array(
|
||||||
|
'type' => 'array',
|
||||||
|
'description' => 'Array of IGNY8 keyword IDs',
|
||||||
|
'single' => true,
|
||||||
|
'show_in_rest' => array(
|
||||||
|
'schema' => array(
|
||||||
|
'type' => 'array',
|
||||||
|
'items' => array(
|
||||||
|
'type' => 'integer'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'_igny8_content_id' => array(
|
||||||
|
'type' => 'integer',
|
||||||
|
'description' => 'IGNY8 content ID',
|
||||||
|
'single' => true,
|
||||||
|
'show_in_rest' => true,
|
||||||
|
),
|
||||||
|
'_igny8_wordpress_status' => array(
|
||||||
|
'type' => 'string',
|
||||||
|
'description' => 'WordPress post status (for IGNY8 reference)',
|
||||||
|
'single' => true,
|
||||||
|
'show_in_rest' => true,
|
||||||
|
),
|
||||||
|
'_igny8_last_synced' => array(
|
||||||
|
'type' => 'string',
|
||||||
|
'description' => 'Last sync timestamp',
|
||||||
|
'single' => true,
|
||||||
|
'show_in_rest' => true,
|
||||||
|
),
|
||||||
|
'_igny8_task_brief' => array(
|
||||||
|
'type' => 'array',
|
||||||
|
'description' => 'Cached IGNY8 writer brief data',
|
||||||
|
'single' => true,
|
||||||
|
'show_in_rest' => array(
|
||||||
|
'schema' => array(
|
||||||
|
'type' => 'object'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'_igny8_brief_cached_at' => array(
|
||||||
|
'type' => 'string',
|
||||||
|
'description' => 'Timestamp of cached IGNY8 brief',
|
||||||
|
'single' => true,
|
||||||
|
'show_in_rest' => true,
|
||||||
|
),
|
||||||
|
'_igny8_optimizer_job_id' => array(
|
||||||
|
'type' => 'integer',
|
||||||
|
'description' => 'IGNY8 optimizer job ID',
|
||||||
|
'single' => true,
|
||||||
|
'show_in_rest' => true,
|
||||||
|
),
|
||||||
|
'_igny8_optimizer_status' => array(
|
||||||
|
'type' => 'string',
|
||||||
|
'description' => 'IGNY8 optimizer job status',
|
||||||
|
'single' => true,
|
||||||
|
'show_in_rest' => true,
|
||||||
|
),
|
||||||
|
'_igny8_optimizer_score_changes' => array(
|
||||||
|
'type' => 'array',
|
||||||
|
'description' => 'IGNY8 optimizer score changes',
|
||||||
|
'single' => true,
|
||||||
|
'show_in_rest' => array(
|
||||||
|
'schema' => array(
|
||||||
|
'type' => 'object'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'_igny8_optimizer_recommendations' => array(
|
||||||
|
'type' => 'array',
|
||||||
|
'description' => 'IGNY8 optimizer recommendations',
|
||||||
|
'single' => true,
|
||||||
|
'show_in_rest' => array(
|
||||||
|
'schema' => array(
|
||||||
|
'type' => 'array'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
100
igny8-wp-integration-plugin/languages/igny8-bridge.pot
Normal file
100
igny8-wp-integration-plugin/languages/igny8-bridge.pot
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
# Copyright (C) 2025 Your Name
|
||||||
|
# This file is distributed under the same license as the IGNY8 WordPress Bridge plugin.
|
||||||
|
msgid ""
|
||||||
|
msgstr ""
|
||||||
|
"Project-Id-Version: IGNY8 WordPress Bridge 1.0.0\n"
|
||||||
|
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/igny8-bridge\n"
|
||||||
|
"POT-Creation-Date: 2025-10-17 12:00+0000\n"
|
||||||
|
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||||
|
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||||
|
"Language-Team: LANGUAGE <LL@li.org>\n"
|
||||||
|
"Language: \n"
|
||||||
|
"MIME-Version: 1.0\n"
|
||||||
|
"Content-Type: text/plain; charset=UTF-8\n"
|
||||||
|
"Content-Transfer-Encoding: 8bit\n"
|
||||||
|
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
|
||||||
|
|
||||||
|
#: admin/settings.php
|
||||||
|
msgid "IGNY8 API Settings"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/settings.php
|
||||||
|
msgid "IGNY8 API"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/settings.php
|
||||||
|
msgid "API Connection"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/settings.php
|
||||||
|
msgid "Email"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/settings.php
|
||||||
|
msgid "Your IGNY8 account email address."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/settings.php
|
||||||
|
msgid "Password"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/settings.php
|
||||||
|
msgid "Your IGNY8 account password."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/settings.php
|
||||||
|
msgid "Connect to IGNY8"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/settings.php
|
||||||
|
msgid "Connection Status"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/settings.php
|
||||||
|
msgid "Status"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/settings.php
|
||||||
|
msgid "Connected"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/settings.php
|
||||||
|
msgid "Site ID"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/settings.php
|
||||||
|
msgid "Not Connected"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/settings.php
|
||||||
|
msgid "Enter your IGNY8 credentials above and click \"Connect to IGNY8\" to establish a connection."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/settings.php
|
||||||
|
msgid "About"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/settings.php
|
||||||
|
msgid "The IGNY8 WordPress Bridge plugin connects your WordPress site to the IGNY8 API, enabling two-way synchronization of posts, taxonomies, and site data."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/settings.php
|
||||||
|
msgid "Version:"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/settings.php
|
||||||
|
msgid "Test Connection"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/class-admin.php
|
||||||
|
msgid "Email and password are required."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/class-admin.php
|
||||||
|
msgid "Successfully connected to IGNY8 API."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: admin/class-admin.php
|
||||||
|
msgid "Failed to connect to IGNY8 API. Please check your credentials."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
41
igny8-wp-integration-plugin/sync/hooks.php
Normal file
41
igny8-wp-integration-plugin/sync/hooks.php
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* WordPress Hooks Registration
|
||||||
|
*
|
||||||
|
* Registers all WordPress hooks for synchronization
|
||||||
|
*
|
||||||
|
* @package Igny8Bridge
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Prevent direct access
|
||||||
|
if (!defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load sync class
|
||||||
|
require_once IGNY8_BRIDGE_PLUGIN_DIR . 'sync/post-sync.php';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register WordPress hooks for IGNY8 sync
|
||||||
|
*/
|
||||||
|
function igny8_register_sync_hooks() {
|
||||||
|
// WordPress → IGNY8 hooks
|
||||||
|
add_action('save_post', 'igny8_sync_post_status_to_igny8', 10, 3);
|
||||||
|
add_action('publish_post', 'igny8_update_keywords_on_post_publish', 10, 1);
|
||||||
|
add_action('publish_page', 'igny8_update_keywords_on_post_publish', 10, 1);
|
||||||
|
add_action('draft_to_publish', 'igny8_update_keywords_on_post_publish', 10, 1);
|
||||||
|
add_action('future_to_publish', 'igny8_update_keywords_on_post_publish', 10, 1);
|
||||||
|
add_action('transition_post_status', 'igny8_sync_post_status_transition', 10, 3);
|
||||||
|
|
||||||
|
// Cron hooks
|
||||||
|
add_action('igny8_sync_post_statuses', 'igny8_cron_sync_post_statuses');
|
||||||
|
add_action('igny8_sync_site_data', 'igny8_cron_sync_site_data');
|
||||||
|
add_action('igny8_sync_from_igny8', 'igny8_cron_sync_from_igny8');
|
||||||
|
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');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register hooks
|
||||||
|
igny8_register_sync_hooks();
|
||||||
|
|
||||||
807
igny8-wp-integration-plugin/sync/igny8-to-wp.php
Normal file
807
igny8-wp-integration-plugin/sync/igny8-to-wp.php
Normal file
@@ -0,0 +1,807 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* IGNY8 → WordPress Synchronization
|
||||||
|
*
|
||||||
|
* Handles creating WordPress posts from IGNY8 content
|
||||||
|
*
|
||||||
|
* @package Igny8Bridge
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Prevent direct access
|
||||||
|
if (!defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine WordPress post type for IGNY8 task
|
||||||
|
*
|
||||||
|
* @param array $content_data Task data
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
function igny8_resolve_post_type_for_task($content_data) {
|
||||||
|
$content_type = $content_data['content_type'] ?? $content_data['post_type'] ?? 'post';
|
||||||
|
|
||||||
|
$post_type_map = array(
|
||||||
|
'post' => 'post',
|
||||||
|
'page' => 'page',
|
||||||
|
'product' => 'product',
|
||||||
|
'article' => 'post',
|
||||||
|
'blog' => 'post'
|
||||||
|
);
|
||||||
|
|
||||||
|
$post_type = isset($post_type_map[$content_type]) ? $post_type_map[$content_type] : $content_type;
|
||||||
|
$post_type = apply_filters('igny8_post_type_for_task', $post_type, $content_data);
|
||||||
|
|
||||||
|
if (!post_type_exists($post_type)) {
|
||||||
|
$post_type = 'post';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $post_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cache writer brief for a task
|
||||||
|
*
|
||||||
|
* @param int $task_id IGNY8 task ID
|
||||||
|
* @param int $post_id WordPress post ID
|
||||||
|
* @param Igny8API|null $api Optional API client
|
||||||
|
*/
|
||||||
|
function igny8_cache_task_brief($task_id, $post_id, $api = null) {
|
||||||
|
if (!$task_id || !$post_id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = $api ?: new Igny8API();
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = $api->get("/writer/tasks/{$task_id}/brief/");
|
||||||
|
if ($response && !empty($response['success']) && !empty($response['data'])) {
|
||||||
|
update_post_meta($post_id, '_igny8_task_brief', $response['data']);
|
||||||
|
update_post_meta($post_id, '_igny8_brief_cached_at', current_time('mysql'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create WordPress post from IGNY8 task/content
|
||||||
|
*
|
||||||
|
* @param array $content_data Content data from IGNY8
|
||||||
|
* @param array $allowed_post_types Post types allowed to be created automatically
|
||||||
|
* @return int|WP_Error WordPress post ID or error
|
||||||
|
*/
|
||||||
|
function igny8_create_wordpress_post_from_task($content_data, $allowed_post_types = array()) {
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
return new WP_Error('igny8_not_authenticated', 'IGNY8 API not authenticated');
|
||||||
|
}
|
||||||
|
|
||||||
|
$post_type = igny8_resolve_post_type_for_task($content_data);
|
||||||
|
|
||||||
|
if (!empty($allowed_post_types) && !in_array($post_type, $allowed_post_types, true)) {
|
||||||
|
return new WP_Error('igny8_post_type_disabled', sprintf('Post type %s is disabled for automation', $post_type));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare post data
|
||||||
|
$post_data = array(
|
||||||
|
'post_title' => $content_data['title'] ?? 'Untitled',
|
||||||
|
'post_content' => $content_data['content'] ?? '',
|
||||||
|
'post_status' => igny8_map_igny8_status_to_wp($content_data['status'] ?? 'draft'),
|
||||||
|
'post_type' => $post_type,
|
||||||
|
'meta_input' => array()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Add IGNY8 meta
|
||||||
|
if (!empty($content_data['task_id'])) {
|
||||||
|
$post_data['meta_input']['_igny8_task_id'] = $content_data['task_id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($content_data['content_id'])) {
|
||||||
|
$post_data['meta_input']['_igny8_content_id'] = $content_data['content_id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($content_data['cluster_id'])) {
|
||||||
|
$post_data['meta_input']['_igny8_cluster_id'] = $content_data['cluster_id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($content_data['sector_id'])) {
|
||||||
|
$post_data['meta_input']['_igny8_sector_id'] = $content_data['sector_id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($content_data['keyword_ids'])) {
|
||||||
|
$post_data['meta_input']['_igny8_keyword_ids'] = $content_data['keyword_ids'];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create post
|
||||||
|
$post_id = wp_insert_post($post_data);
|
||||||
|
|
||||||
|
if (is_wp_error($post_id)) {
|
||||||
|
error_log("IGNY8: Failed to create WordPress post: " . $post_id->get_error_message());
|
||||||
|
return $post_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assign taxonomies if cluster/sector IDs exist
|
||||||
|
if (!empty($content_data['cluster_id'])) {
|
||||||
|
// Find cluster term
|
||||||
|
$cluster_terms = get_terms(array(
|
||||||
|
'taxonomy' => 'igny8_clusters',
|
||||||
|
'meta_key' => '_igny8_cluster_id',
|
||||||
|
'meta_value' => $content_data['cluster_id'],
|
||||||
|
'hide_empty' => false
|
||||||
|
));
|
||||||
|
|
||||||
|
if (!is_wp_error($cluster_terms) && !empty($cluster_terms)) {
|
||||||
|
wp_set_post_terms($post_id, array($cluster_terms[0]->term_id), 'igny8_clusters');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($content_data['sector_id'])) {
|
||||||
|
// Find sector term
|
||||||
|
$sector_terms = get_terms(array(
|
||||||
|
'taxonomy' => 'igny8_sectors',
|
||||||
|
'meta_key' => '_igny8_sector_id',
|
||||||
|
'meta_value' => $content_data['sector_id'],
|
||||||
|
'hide_empty' => false
|
||||||
|
));
|
||||||
|
|
||||||
|
if (!is_wp_error($sector_terms) && !empty($sector_terms)) {
|
||||||
|
wp_set_post_terms($post_id, array($sector_terms[0]->term_id), 'igny8_sectors');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle categories
|
||||||
|
if (!empty($content_data['categories'])) {
|
||||||
|
$category_ids = igny8_process_categories($content_data['categories'], $post_id);
|
||||||
|
if (!empty($category_ids)) {
|
||||||
|
wp_set_post_terms($post_id, $category_ids, 'category');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle tags
|
||||||
|
if (!empty($content_data['tags'])) {
|
||||||
|
$tag_ids = igny8_process_tags($content_data['tags'], $post_id);
|
||||||
|
if (!empty($tag_ids)) {
|
||||||
|
wp_set_post_terms($post_id, $tag_ids, 'post_tag');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle featured image
|
||||||
|
if (!empty($content_data['featured_image'])) {
|
||||||
|
igny8_set_featured_image($post_id, $content_data['featured_image']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle image gallery (1-5 images)
|
||||||
|
if (!empty($content_data['gallery_images'])) {
|
||||||
|
igny8_set_image_gallery($post_id, $content_data['gallery_images']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle meta title and meta description (SEO)
|
||||||
|
if (!empty($content_data['meta_title'])) {
|
||||||
|
update_post_meta($post_id, '_yoast_wpseo_title', $content_data['meta_title']);
|
||||||
|
update_post_meta($post_id, '_seopress_titles_title', $content_data['meta_title']);
|
||||||
|
update_post_meta($post_id, '_aioseo_title', $content_data['meta_title']);
|
||||||
|
// Generic meta
|
||||||
|
update_post_meta($post_id, '_igny8_meta_title', $content_data['meta_title']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($content_data['meta_description'])) {
|
||||||
|
update_post_meta($post_id, '_yoast_wpseo_metadesc', $content_data['meta_description']);
|
||||||
|
update_post_meta($post_id, '_seopress_titles_desc', $content_data['meta_description']);
|
||||||
|
update_post_meta($post_id, '_aioseo_description', $content_data['meta_description']);
|
||||||
|
// Generic meta
|
||||||
|
update_post_meta($post_id, '_igny8_meta_description', $content_data['meta_description']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the actual WordPress post status (after creation)
|
||||||
|
$created_post = get_post($post_id);
|
||||||
|
$wp_status = $created_post ? $created_post->post_status : 'draft';
|
||||||
|
|
||||||
|
// Store WordPress status in meta for IGNY8 to read
|
||||||
|
update_post_meta($post_id, '_igny8_wordpress_status', $wp_status);
|
||||||
|
|
||||||
|
// Map WordPress status back to IGNY8 status
|
||||||
|
$igny8_status = igny8_map_wp_status_to_igny8($wp_status);
|
||||||
|
|
||||||
|
// Update IGNY8 task with WordPress post ID, URL, and status
|
||||||
|
if (!empty($content_data['task_id'])) {
|
||||||
|
$update_data = array(
|
||||||
|
'assigned_post_id' => $post_id,
|
||||||
|
'post_url' => get_permalink($post_id),
|
||||||
|
'wordpress_status' => $wp_status, // WordPress actual status (publish/pending/draft)
|
||||||
|
'status' => $igny8_status, // IGNY8 mapped status (completed/pending/draft)
|
||||||
|
'synced_at' => current_time('mysql'),
|
||||||
|
'post_type' => $post_type, // WordPress post type
|
||||||
|
'content_type' => $content_type // IGNY8 content type
|
||||||
|
);
|
||||||
|
|
||||||
|
// Include content_id if provided
|
||||||
|
if (!empty($content_data['content_id'])) {
|
||||||
|
$update_data['content_id'] = $content_data['content_id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = $api->put("/writer/tasks/{$content_data['task_id']}/", $update_data);
|
||||||
|
|
||||||
|
if ($response['success']) {
|
||||||
|
error_log("IGNY8: Updated task {$content_data['task_id']} with WordPress post {$post_id} (status: {$wp_status})");
|
||||||
|
} else {
|
||||||
|
error_log("IGNY8: Failed to update task: " . ($response['error'] ?? 'Unknown error'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store content_id if provided (for IGNY8 to query)
|
||||||
|
if (!empty($content_data['content_id'])) {
|
||||||
|
update_post_meta($post_id, '_igny8_content_id', $content_data['content_id']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $post_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map IGNY8 task status to WordPress post status
|
||||||
|
*
|
||||||
|
* @param string $igny8_status IGNY8 task status
|
||||||
|
* @return string WordPress post status
|
||||||
|
*/
|
||||||
|
function igny8_map_igny8_status_to_wp($igny8_status) {
|
||||||
|
$status_map = array(
|
||||||
|
'completed' => 'publish',
|
||||||
|
'draft' => 'draft',
|
||||||
|
'pending' => 'pending',
|
||||||
|
'scheduled' => 'future',
|
||||||
|
'archived' => 'trash'
|
||||||
|
);
|
||||||
|
|
||||||
|
return isset($status_map[$igny8_status]) ? $status_map[$igny8_status] : 'draft';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync IGNY8 tasks to WordPress posts
|
||||||
|
* Fetches tasks from IGNY8 and creates/updates WordPress posts
|
||||||
|
*
|
||||||
|
* @param array $filters Optional filters (status, cluster_id, etc.)
|
||||||
|
* @return array Sync results
|
||||||
|
*/
|
||||||
|
function igny8_sync_igny8_tasks_to_wp($filters = array()) {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
return array('success' => false, 'error' => 'Connection disabled', 'disabled' => true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
return array('success' => false, 'error' => 'Not authenticated');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (function_exists('igny8_is_module_enabled') && !igny8_is_module_enabled('writer')) {
|
||||||
|
return array('success' => true, 'created' => 0, 'updated' => 0, 'failed' => 0, 'skipped' => 0, 'total' => 0, 'disabled' => true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$site_id = get_option('igny8_site_id');
|
||||||
|
if (!$site_id) {
|
||||||
|
return array('success' => false, 'error' => 'Site ID not configured');
|
||||||
|
}
|
||||||
|
|
||||||
|
$enabled_post_types = function_exists('igny8_get_enabled_post_types') ? igny8_get_enabled_post_types() : array('post', 'page');
|
||||||
|
|
||||||
|
// Build endpoint with filters
|
||||||
|
$endpoint = '/writer/tasks/';
|
||||||
|
$query_params = array();
|
||||||
|
|
||||||
|
$query_params[] = 'site_id=' . intval($site_id);
|
||||||
|
|
||||||
|
if (!empty($filters['status'])) {
|
||||||
|
$query_params[] = 'status=' . urlencode($filters['status']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($filters['cluster_id'])) {
|
||||||
|
$query_params[] = 'cluster_id=' . intval($filters['cluster_id']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($query_params)) {
|
||||||
|
$endpoint .= '?' . implode('&', $query_params);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get tasks from IGNY8
|
||||||
|
$response = $api->get($endpoint);
|
||||||
|
|
||||||
|
if (!$response['success']) {
|
||||||
|
return array('success' => false, 'error' => $response['error'] ?? 'Unknown error');
|
||||||
|
}
|
||||||
|
|
||||||
|
$tasks = $response['results'] ?? array();
|
||||||
|
$created = 0;
|
||||||
|
$updated = 0;
|
||||||
|
$failed = 0;
|
||||||
|
$skipped = 0;
|
||||||
|
|
||||||
|
foreach ($tasks as $task) {
|
||||||
|
// 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)) {
|
||||||
|
// Update existing post
|
||||||
|
$post_id = $existing_posts[0]->ID;
|
||||||
|
|
||||||
|
$update_data = array(
|
||||||
|
'ID' => $post_id,
|
||||||
|
'post_title' => $task['title'] ?? get_the_title($post_id),
|
||||||
|
'post_status' => igny8_map_igny8_status_to_wp($task['status'] ?? 'draft')
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!empty($task['content'])) {
|
||||||
|
$update_data['post_content'] = $task['content'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = wp_update_post($update_data);
|
||||||
|
|
||||||
|
// Update categories, tags, images, and meta
|
||||||
|
if ($result && !is_wp_error($result)) {
|
||||||
|
// Update categories
|
||||||
|
if (!empty($task['categories'])) {
|
||||||
|
$category_ids = igny8_process_categories($task['categories'], $post_id);
|
||||||
|
if (!empty($category_ids)) {
|
||||||
|
wp_set_post_terms($post_id, $category_ids, 'category');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update tags
|
||||||
|
if (!empty($task['tags'])) {
|
||||||
|
$tag_ids = igny8_process_tags($task['tags'], $post_id);
|
||||||
|
if (!empty($tag_ids)) {
|
||||||
|
wp_set_post_terms($post_id, $tag_ids, 'post_tag');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update featured image
|
||||||
|
if (!empty($task['featured_image']) || !empty($task['featured_media'])) {
|
||||||
|
igny8_set_featured_image($post_id, $task['featured_image'] ?? $task['featured_media']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update gallery
|
||||||
|
if (!empty($task['gallery_images']) || !empty($task['images'])) {
|
||||||
|
igny8_set_image_gallery($post_id, $task['gallery_images'] ?? $task['images']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update meta title and description
|
||||||
|
if (!empty($task['meta_title']) || !empty($task['seo_title'])) {
|
||||||
|
$meta_title = $task['meta_title'] ?? $task['seo_title'];
|
||||||
|
update_post_meta($post_id, '_yoast_wpseo_title', $meta_title);
|
||||||
|
update_post_meta($post_id, '_seopress_titles_title', $meta_title);
|
||||||
|
update_post_meta($post_id, '_aioseo_title', $meta_title);
|
||||||
|
update_post_meta($post_id, '_igny8_meta_title', $meta_title);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($task['meta_description']) || !empty($task['seo_description'])) {
|
||||||
|
$meta_desc = $task['meta_description'] ?? $task['seo_description'];
|
||||||
|
update_post_meta($post_id, '_yoast_wpseo_metadesc', $meta_desc);
|
||||||
|
update_post_meta($post_id, '_seopress_titles_desc', $meta_desc);
|
||||||
|
update_post_meta($post_id, '_aioseo_description', $meta_desc);
|
||||||
|
update_post_meta($post_id, '_igny8_meta_description', $meta_desc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($result && !is_wp_error($result)) {
|
||||||
|
igny8_cache_task_brief($task['id'], $post_id, $api);
|
||||||
|
$updated++;
|
||||||
|
} else {
|
||||||
|
$failed++;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Create new post
|
||||||
|
$task_post_type = igny8_resolve_post_type_for_task($task);
|
||||||
|
if (!empty($enabled_post_types) && !in_array($task_post_type, $enabled_post_types, true)) {
|
||||||
|
$skipped++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$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'] ?? $task['post_type'] ?? 'post',
|
||||||
|
'post_type' => $task['post_type'] ?? null, // Keep for backward compatibility
|
||||||
|
'categories' => $task['categories'] ?? array(),
|
||||||
|
'tags' => $task['tags'] ?? array(),
|
||||||
|
'featured_image' => $task['featured_image'] ?? $task['featured_media'] ?? null,
|
||||||
|
'gallery_images' => $task['gallery_images'] ?? $task['images'] ?? array(),
|
||||||
|
'meta_title' => $task['meta_title'] ?? $task['seo_title'] ?? null,
|
||||||
|
'meta_description' => $task['meta_description'] ?? $task['seo_description'] ?? null
|
||||||
|
);
|
||||||
|
|
||||||
|
$post_id = igny8_create_wordpress_post_from_task($content_data, $enabled_post_types);
|
||||||
|
|
||||||
|
if (is_wp_error($post_id)) {
|
||||||
|
if ($post_id->get_error_code() === 'igny8_post_type_disabled') {
|
||||||
|
$skipped++;
|
||||||
|
} else {
|
||||||
|
$failed++;
|
||||||
|
}
|
||||||
|
} elseif ($post_id) {
|
||||||
|
igny8_cache_task_brief($task['id'], $post_id, $api);
|
||||||
|
$created++;
|
||||||
|
} else {
|
||||||
|
$failed++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return array(
|
||||||
|
'success' => true,
|
||||||
|
'created' => $created,
|
||||||
|
'updated' => $updated,
|
||||||
|
'failed' => $failed,
|
||||||
|
'skipped' => $skipped,
|
||||||
|
'total' => count($tasks)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle webhook from IGNY8 (when content is published from IGNY8)
|
||||||
|
* This can be called via REST API endpoint or scheduled sync
|
||||||
|
*
|
||||||
|
* @param array $webhook_data Webhook data from IGNY8
|
||||||
|
* @return int|false WordPress post ID or false on failure
|
||||||
|
*/
|
||||||
|
function igny8_handle_igny8_webhook($webhook_data) {
|
||||||
|
if (empty($webhook_data['task_id'])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
// Get full task data from IGNY8
|
||||||
|
$task_response = $api->get("/writer/tasks/{$webhook_data['task_id']}/");
|
||||||
|
|
||||||
|
if (!$task_response['success']) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$task = $task_response['data'];
|
||||||
|
|
||||||
|
// Prepare content data
|
||||||
|
$content_data = array(
|
||||||
|
'task_id' => $task['id'],
|
||||||
|
'content_id' => $task['content_id'] ?? null,
|
||||||
|
'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(),
|
||||||
|
'post_type' => $task['post_type'] ?? 'post'
|
||||||
|
);
|
||||||
|
|
||||||
|
return igny8_create_wordpress_post_from_task($content_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process categories from IGNY8
|
||||||
|
*
|
||||||
|
* @param array $categories Category data (IDs, names, or slugs)
|
||||||
|
* @param int $post_id Post ID
|
||||||
|
* @return array Category term IDs
|
||||||
|
*/
|
||||||
|
function igny8_process_categories($categories, $post_id) {
|
||||||
|
$category_ids = array();
|
||||||
|
|
||||||
|
foreach ($categories as $category) {
|
||||||
|
$term_id = null;
|
||||||
|
|
||||||
|
// If it's an ID
|
||||||
|
if (is_numeric($category)) {
|
||||||
|
$term = get_term($category, 'category');
|
||||||
|
if ($term && !is_wp_error($term)) {
|
||||||
|
$term_id = $term->term_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If it's an array with name/slug
|
||||||
|
elseif (is_array($category)) {
|
||||||
|
$name = $category['name'] ?? $category['slug'] ?? null;
|
||||||
|
$slug = $category['slug'] ?? sanitize_title($name);
|
||||||
|
|
||||||
|
if ($name) {
|
||||||
|
// Try to find existing term
|
||||||
|
$term = get_term_by('slug', $slug, 'category');
|
||||||
|
if (!$term) {
|
||||||
|
$term = get_term_by('name', $name, 'category');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create if doesn't exist
|
||||||
|
if (!$term || is_wp_error($term)) {
|
||||||
|
$term_result = wp_insert_term($name, 'category', array('slug' => $slug));
|
||||||
|
if (!is_wp_error($term_result)) {
|
||||||
|
$term_id = $term_result['term_id'];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$term_id = $term->term_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If it's a string (name or slug)
|
||||||
|
elseif (is_string($category)) {
|
||||||
|
$term = get_term_by('slug', $category, 'category');
|
||||||
|
if (!$term) {
|
||||||
|
$term = get_term_by('name', $category, 'category');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($term && !is_wp_error($term)) {
|
||||||
|
$term_id = $term->term_id;
|
||||||
|
} else {
|
||||||
|
// Create new category
|
||||||
|
$term_result = wp_insert_term($category, 'category');
|
||||||
|
if (!is_wp_error($term_result)) {
|
||||||
|
$term_id = $term_result['term_id'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($term_id) {
|
||||||
|
$category_ids[] = $term_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_unique($category_ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process tags from IGNY8
|
||||||
|
*
|
||||||
|
* @param array $tags Tag data (IDs, names, or slugs)
|
||||||
|
* @param int $post_id Post ID
|
||||||
|
* @return array Tag term IDs
|
||||||
|
*/
|
||||||
|
function igny8_process_tags($tags, $post_id) {
|
||||||
|
$tag_ids = array();
|
||||||
|
|
||||||
|
foreach ($tags as $tag) {
|
||||||
|
$term_id = null;
|
||||||
|
|
||||||
|
// If it's an ID
|
||||||
|
if (is_numeric($tag)) {
|
||||||
|
$term = get_term($tag, 'post_tag');
|
||||||
|
if ($term && !is_wp_error($term)) {
|
||||||
|
$term_id = $term->term_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If it's an array with name/slug
|
||||||
|
elseif (is_array($tag)) {
|
||||||
|
$name = $tag['name'] ?? $tag['slug'] ?? null;
|
||||||
|
$slug = $tag['slug'] ?? sanitize_title($name);
|
||||||
|
|
||||||
|
if ($name) {
|
||||||
|
// Try to find existing term
|
||||||
|
$term = get_term_by('slug', $slug, 'post_tag');
|
||||||
|
if (!$term) {
|
||||||
|
$term = get_term_by('name', $name, 'post_tag');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create if doesn't exist
|
||||||
|
if (!$term || is_wp_error($term)) {
|
||||||
|
$term_result = wp_insert_term($name, 'post_tag', array('slug' => $slug));
|
||||||
|
if (!is_wp_error($term_result)) {
|
||||||
|
$term_id = $term_result['term_id'];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$term_id = $term->term_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If it's a string (name or slug)
|
||||||
|
elseif (is_string($tag)) {
|
||||||
|
$term = get_term_by('slug', $tag, 'post_tag');
|
||||||
|
if (!$term) {
|
||||||
|
$term = get_term_by('name', $tag, 'post_tag');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($term && !is_wp_error($term)) {
|
||||||
|
$term_id = $term->term_id;
|
||||||
|
} else {
|
||||||
|
// Create new tag
|
||||||
|
$term_result = wp_insert_term($tag, 'post_tag');
|
||||||
|
if (!is_wp_error($term_result)) {
|
||||||
|
$term_id = $term_result['term_id'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($term_id) {
|
||||||
|
$tag_ids[] = $term_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_unique($tag_ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set featured image for post
|
||||||
|
*
|
||||||
|
* @param int $post_id Post ID
|
||||||
|
* @param string|array $image_data Image URL or array with image data
|
||||||
|
* @return int|false Attachment ID or false on failure
|
||||||
|
*/
|
||||||
|
function igny8_set_featured_image($post_id, $image_data) {
|
||||||
|
$image_url = is_array($image_data) ? ($image_data['url'] ?? $image_data['src'] ?? '') : $image_data;
|
||||||
|
|
||||||
|
if (empty($image_url)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if image already exists
|
||||||
|
$attachment_id = igny8_get_attachment_by_url($image_url);
|
||||||
|
|
||||||
|
if (!$attachment_id) {
|
||||||
|
// Download and attach image
|
||||||
|
$attachment_id = igny8_import_image($image_url, $post_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($attachment_id) {
|
||||||
|
set_post_thumbnail($post_id, $attachment_id);
|
||||||
|
return $attachment_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set image gallery for post (1-5 images)
|
||||||
|
*
|
||||||
|
* @param int $post_id Post ID
|
||||||
|
* @param array $gallery_images Array of image URLs or image data
|
||||||
|
* @return array Attachment IDs
|
||||||
|
*/
|
||||||
|
function igny8_set_image_gallery($post_id, $gallery_images) {
|
||||||
|
$attachment_ids = array();
|
||||||
|
|
||||||
|
// Limit to 5 images
|
||||||
|
$gallery_images = array_slice($gallery_images, 0, 5);
|
||||||
|
|
||||||
|
foreach ($gallery_images as $image_data) {
|
||||||
|
$image_url = is_array($image_data) ? ($image_data['url'] ?? $image_data['src'] ?? '') : $image_data;
|
||||||
|
|
||||||
|
if (empty($image_url)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if image already exists
|
||||||
|
$attachment_id = igny8_get_attachment_by_url($image_url);
|
||||||
|
|
||||||
|
if (!$attachment_id) {
|
||||||
|
// Download and attach image
|
||||||
|
$attachment_id = igny8_import_image($image_url, $post_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($attachment_id) {
|
||||||
|
$attachment_ids[] = $attachment_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store gallery as post meta (WordPress native)
|
||||||
|
if (!empty($attachment_ids)) {
|
||||||
|
update_post_meta($post_id, '_igny8_gallery_images', $attachment_ids);
|
||||||
|
|
||||||
|
// Also store in format compatible with plugins
|
||||||
|
update_post_meta($post_id, '_product_image_gallery', implode(',', $attachment_ids)); // WooCommerce
|
||||||
|
update_post_meta($post_id, '_gallery_images', $attachment_ids); // Generic
|
||||||
|
}
|
||||||
|
|
||||||
|
return $attachment_ids;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get attachment ID by image URL
|
||||||
|
*
|
||||||
|
* @param string $image_url Image URL
|
||||||
|
* @return int|false Attachment ID or false
|
||||||
|
*/
|
||||||
|
function igny8_get_attachment_by_url($image_url) {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$attachment_id = $wpdb->get_var($wpdb->prepare(
|
||||||
|
"SELECT ID FROM {$wpdb->posts} WHERE guid = %s AND post_type = 'attachment'",
|
||||||
|
$image_url
|
||||||
|
));
|
||||||
|
|
||||||
|
return $attachment_id ? intval($attachment_id) : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Import image from URL and attach to post
|
||||||
|
*
|
||||||
|
* @param string $image_url Image URL
|
||||||
|
* @param int $post_id Post ID to attach to
|
||||||
|
* @return int|false Attachment ID or false on failure
|
||||||
|
*/
|
||||||
|
function igny8_import_image($image_url, $post_id) {
|
||||||
|
require_once(ABSPATH . 'wp-admin/includes/image.php');
|
||||||
|
require_once(ABSPATH . 'wp-admin/includes/file.php');
|
||||||
|
require_once(ABSPATH . 'wp-admin/includes/media.php');
|
||||||
|
|
||||||
|
// Download image
|
||||||
|
$tmp = download_url($image_url);
|
||||||
|
|
||||||
|
if (is_wp_error($tmp)) {
|
||||||
|
error_log("IGNY8: Failed to download image {$image_url}: " . $tmp->get_error_message());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get file extension
|
||||||
|
$file_array = array(
|
||||||
|
'name' => basename(parse_url($image_url, PHP_URL_PATH)),
|
||||||
|
'tmp_name' => $tmp
|
||||||
|
);
|
||||||
|
|
||||||
|
// Upload to WordPress media library
|
||||||
|
$attachment_id = media_handle_sideload($file_array, $post_id);
|
||||||
|
|
||||||
|
// Clean up temp file
|
||||||
|
@unlink($tmp);
|
||||||
|
|
||||||
|
if (is_wp_error($attachment_id)) {
|
||||||
|
error_log("IGNY8: Failed to import image {$image_url}: " . $attachment_id->get_error_message());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $attachment_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scheduled sync from IGNY8 to WordPress
|
||||||
|
* Fetches new/updated tasks from IGNY8 and creates/updates WordPress posts
|
||||||
|
*/
|
||||||
|
function igny8_cron_sync_from_igny8() {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
error_log('IGNY8: Connection disabled, skipping sync from IGNY8');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$site_id = get_option('igny8_site_id');
|
||||||
|
|
||||||
|
if (!$site_id) {
|
||||||
|
error_log('IGNY8: Site ID not set, skipping sync from IGNY8');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (function_exists('igny8_is_module_enabled') && !igny8_is_module_enabled('writer')) {
|
||||||
|
error_log('IGNY8: Writer module disabled, skipping sync from IGNY8');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get last sync time
|
||||||
|
$last_sync = get_option('igny8_last_sync_from_igny8', 0);
|
||||||
|
|
||||||
|
// Sync only completed/published tasks
|
||||||
|
$filters = array(
|
||||||
|
'status' => 'completed'
|
||||||
|
);
|
||||||
|
|
||||||
|
// If we have a last sync time, we could filter by updated date
|
||||||
|
// For now, sync all completed tasks (API should handle deduplication)
|
||||||
|
|
||||||
|
$result = igny8_sync_igny8_tasks_to_wp($filters);
|
||||||
|
|
||||||
|
if ($result['success']) {
|
||||||
|
update_option('igny8_last_sync_from_igny8', time());
|
||||||
|
update_option('igny8_last_writer_sync', current_time('timestamp'));
|
||||||
|
error_log(sprintf(
|
||||||
|
'IGNY8: Synced from IGNY8 - Created %d posts, updated %d posts, %d failed, %d skipped',
|
||||||
|
$result['created'],
|
||||||
|
$result['updated'],
|
||||||
|
$result['failed'],
|
||||||
|
$result['skipped'] ?? 0
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
error_log('IGNY8: Failed to sync from IGNY8: ' . ($result['error'] ?? 'Unknown error'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
363
igny8-wp-integration-plugin/sync/post-sync.php
Normal file
363
igny8-wp-integration-plugin/sync/post-sync.php
Normal file
@@ -0,0 +1,363 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Post Synchronization Functions
|
||||||
|
*
|
||||||
|
* Handles WordPress → IGNY8 post synchronization
|
||||||
|
*
|
||||||
|
* @package Igny8Bridge
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Prevent direct access
|
||||||
|
if (!defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync WordPress post status to IGNY8 when post is saved
|
||||||
|
*
|
||||||
|
* @param int $post_id Post ID
|
||||||
|
* @param WP_Post $post Post object
|
||||||
|
* @param bool $update Whether this is an update
|
||||||
|
*/
|
||||||
|
function igny8_sync_post_status_to_igny8($post_id, $post, $update) {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip autosaves and revisions
|
||||||
|
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (wp_is_post_revision($post_id)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only sync IGNY8-managed posts
|
||||||
|
if (!igny8_is_igny8_managed_post($post_id)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get task ID
|
||||||
|
$task_id = get_post_meta($post_id, '_igny8_task_id', true);
|
||||||
|
if (!$task_id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get post status
|
||||||
|
$post_status = $post->post_status;
|
||||||
|
|
||||||
|
// Map WordPress status to IGNY8 task status
|
||||||
|
$task_status = igny8_map_wp_status_to_igny8($post_status);
|
||||||
|
|
||||||
|
// Sync to IGNY8 API
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
// Get content_id if available
|
||||||
|
$content_id = get_post_meta($post_id, '_igny8_content_id', true);
|
||||||
|
|
||||||
|
$update_data = array(
|
||||||
|
'status' => $task_status,
|
||||||
|
'assigned_post_id' => $post_id,
|
||||||
|
'post_url' => get_permalink($post_id),
|
||||||
|
'wordpress_status' => $post_status, // Actual WordPress status
|
||||||
|
'synced_at' => current_time('mysql')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Include content_id if available
|
||||||
|
if ($content_id) {
|
||||||
|
$update_data['content_id'] = $content_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = $api->put("/writer/tasks/{$task_id}/", $update_data);
|
||||||
|
|
||||||
|
if ($response['success']) {
|
||||||
|
// Update WordPress status in meta for IGNY8 to read
|
||||||
|
update_post_meta($post_id, '_igny8_wordpress_status', $post_status);
|
||||||
|
update_post_meta($post_id, '_igny8_last_synced', current_time('mysql'));
|
||||||
|
error_log("IGNY8: Synced post {$post_id} status ({$post_status}) to task {$task_id}");
|
||||||
|
} else {
|
||||||
|
error_log("IGNY8: Failed to sync post status: " . ($response['error'] ?? 'Unknown error'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update keyword status when WordPress post is published
|
||||||
|
*
|
||||||
|
* @param int $post_id Post ID
|
||||||
|
*/
|
||||||
|
function igny8_update_keywords_on_post_publish($post_id) {
|
||||||
|
// Get task ID from post meta
|
||||||
|
$task_id = get_post_meta($post_id, '_igny8_task_id', true);
|
||||||
|
if (!$task_id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
// Get task details to find associated cluster/keywords
|
||||||
|
$task_response = $api->get("/writer/tasks/{$task_id}/");
|
||||||
|
|
||||||
|
if (!$task_response['success']) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$task = $task_response['data'];
|
||||||
|
$cluster_id = $task['cluster_id'] ?? null;
|
||||||
|
|
||||||
|
if ($cluster_id) {
|
||||||
|
// Get keywords in this cluster
|
||||||
|
$keywords_response = $api->get("/planner/keywords/?cluster_id={$cluster_id}");
|
||||||
|
|
||||||
|
if ($keywords_response['success']) {
|
||||||
|
$keywords = $keywords_response['results'];
|
||||||
|
|
||||||
|
// Update each keyword status to 'mapped'
|
||||||
|
foreach ($keywords as $keyword) {
|
||||||
|
$api->put("/planner/keywords/{$keyword['id']}/", array(
|
||||||
|
'status' => 'mapped'
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update task status to completed
|
||||||
|
$api->put("/writer/tasks/{$task_id}/", array(
|
||||||
|
'status' => 'completed',
|
||||||
|
'assigned_post_id' => $post_id,
|
||||||
|
'post_url' => get_permalink($post_id)
|
||||||
|
));
|
||||||
|
|
||||||
|
update_post_meta($post_id, '_igny8_last_synced', current_time('mysql'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync post status changes to IGNY8
|
||||||
|
*
|
||||||
|
* @param string $new_status New post status
|
||||||
|
* @param string $old_status Old post status
|
||||||
|
* @param WP_Post $post Post object
|
||||||
|
*/
|
||||||
|
function igny8_sync_post_status_transition($new_status, $old_status, $post) {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip if status hasn't changed
|
||||||
|
if ($new_status === $old_status) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only sync IGNY8-managed posts
|
||||||
|
if (!igny8_is_igny8_managed_post($post->ID)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$task_id = get_post_meta($post->ID, '_igny8_task_id', true);
|
||||||
|
if (!$task_id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
// Map WordPress status to IGNY8 task status
|
||||||
|
$task_status = igny8_map_wp_status_to_igny8($new_status);
|
||||||
|
|
||||||
|
// Sync to IGNY8
|
||||||
|
$response = $api->put("/writer/tasks/{$task_id}/", array(
|
||||||
|
'status' => $task_status,
|
||||||
|
'assigned_post_id' => $post->ID,
|
||||||
|
'post_url' => get_permalink($post->ID)
|
||||||
|
));
|
||||||
|
|
||||||
|
if ($response['success']) {
|
||||||
|
update_post_meta($post->ID, '_igny8_last_synced', current_time('mysql'));
|
||||||
|
do_action('igny8_post_status_synced', $post->ID, $task_id, $new_status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Batch sync all IGNY8-managed posts status to IGNY8 API
|
||||||
|
*
|
||||||
|
* @return array Sync results
|
||||||
|
*/
|
||||||
|
function igny8_batch_sync_post_statuses() {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
return array(
|
||||||
|
'synced' => 0,
|
||||||
|
'failed' => 0,
|
||||||
|
'total' => 0,
|
||||||
|
'disabled' => true
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
// Get all posts with IGNY8 task ID
|
||||||
|
$posts = $wpdb->get_results("
|
||||||
|
SELECT p.ID, p.post_status, p.post_title, pm.meta_value as task_id
|
||||||
|
FROM {$wpdb->posts} p
|
||||||
|
INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
|
||||||
|
WHERE pm.meta_key = '_igny8_task_id'
|
||||||
|
AND p.post_type IN ('post', 'page', 'product')
|
||||||
|
AND p.post_status != 'trash'
|
||||||
|
");
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
$synced = 0;
|
||||||
|
$failed = 0;
|
||||||
|
|
||||||
|
foreach ($posts as $post_data) {
|
||||||
|
$post_id = $post_data->ID;
|
||||||
|
$task_id = intval($post_data->task_id);
|
||||||
|
$wp_status = $post_data->post_status;
|
||||||
|
|
||||||
|
// Map status
|
||||||
|
$task_status = igny8_map_wp_status_to_igny8($wp_status);
|
||||||
|
|
||||||
|
// Sync to IGNY8
|
||||||
|
$response = $api->put("/writer/tasks/{$task_id}/", array(
|
||||||
|
'status' => $task_status,
|
||||||
|
'assigned_post_id' => $post_id,
|
||||||
|
'post_url' => get_permalink($post_id)
|
||||||
|
));
|
||||||
|
|
||||||
|
if ($response['success']) {
|
||||||
|
update_post_meta($post_id, '_igny8_last_synced', current_time('mysql'));
|
||||||
|
$synced++;
|
||||||
|
} else {
|
||||||
|
$failed++;
|
||||||
|
error_log("IGNY8: Failed to sync post {$post_id}: " . ($response['error'] ?? 'Unknown error'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return array(
|
||||||
|
'synced' => $synced,
|
||||||
|
'failed' => $failed,
|
||||||
|
'total' => count($posts)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scheduled sync of WordPress post statuses to IGNY8
|
||||||
|
*/
|
||||||
|
function igny8_cron_sync_post_statuses() {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
error_log('IGNY8: Connection disabled, skipping post status sync');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = igny8_batch_sync_post_statuses();
|
||||||
|
|
||||||
|
error_log(sprintf(
|
||||||
|
'IGNY8: Synced %d posts, %d failed',
|
||||||
|
$result['synced'],
|
||||||
|
$result['failed']
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scheduled sync of site data
|
||||||
|
*/
|
||||||
|
function igny8_cron_sync_site_data() {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
error_log('IGNY8: Connection disabled, skipping site data sync');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$site_id = get_option('igny8_site_id');
|
||||||
|
|
||||||
|
if (!$site_id) {
|
||||||
|
error_log('IGNY8: Site ID not set, skipping site data sync');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (function_exists('igny8_is_module_enabled') && !igny8_is_module_enabled('sites')) {
|
||||||
|
error_log('IGNY8: Sites module disabled, skipping incremental site sync');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$settings = igny8_get_site_scan_settings(array(
|
||||||
|
'mode' => 'incremental',
|
||||||
|
'since' => get_option('igny8_last_site_sync', 0)
|
||||||
|
));
|
||||||
|
|
||||||
|
$result = igny8_sync_incremental_site_data($site_id, $settings);
|
||||||
|
|
||||||
|
if ($result) {
|
||||||
|
error_log(sprintf(
|
||||||
|
'IGNY8: Synced %d posts to site %d',
|
||||||
|
$result['synced'],
|
||||||
|
$site_id
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
error_log('IGNY8: Site data sync failed');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scheduled full site scan (runs at most once per week)
|
||||||
|
*/
|
||||||
|
function igny8_cron_full_site_scan() {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$site_id = get_option('igny8_site_id');
|
||||||
|
|
||||||
|
if (!$site_id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (function_exists('igny8_is_module_enabled') && !igny8_is_module_enabled('sites')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$last_full = intval(get_option('igny8_last_full_site_scan', 0));
|
||||||
|
if ($last_full && (time() - $last_full) < WEEK_IN_SECONDS) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$settings = igny8_get_site_scan_settings(array(
|
||||||
|
'mode' => 'full',
|
||||||
|
'since' => null
|
||||||
|
));
|
||||||
|
|
||||||
|
$result = igny8_perform_full_site_scan($site_id, $settings);
|
||||||
|
|
||||||
|
if ($result) {
|
||||||
|
error_log('IGNY8: Full site scan completed');
|
||||||
|
} else {
|
||||||
|
error_log('IGNY8: Full site scan failed');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Two-way sync class
|
||||||
|
*/
|
||||||
|
class Igny8WordPressSync {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API instance
|
||||||
|
*
|
||||||
|
* @var Igny8API
|
||||||
|
*/
|
||||||
|
private $api;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
public function __construct() {
|
||||||
|
$this->api = new Igny8API();
|
||||||
|
|
||||||
|
// WordPress → IGNY8 hooks are registered in hooks.php
|
||||||
|
// IGNY8 → WordPress hooks can be added here if needed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
425
igny8-wp-integration-plugin/sync/taxonomy-sync.php
Normal file
425
igny8-wp-integration-plugin/sync/taxonomy-sync.php
Normal file
@@ -0,0 +1,425 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Taxonomy Synchronization
|
||||||
|
*
|
||||||
|
* Handles synchronization between WordPress taxonomies and IGNY8 sectors/clusters
|
||||||
|
*
|
||||||
|
* @package Igny8Bridge
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Prevent direct access
|
||||||
|
if (!defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync WordPress taxonomy to IGNY8
|
||||||
|
*
|
||||||
|
* @param string $taxonomy Taxonomy name
|
||||||
|
* @return array|false Sync result or false on failure
|
||||||
|
*/
|
||||||
|
function igny8_sync_taxonomy_to_igny8($taxonomy) {
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$site_id = get_option('igny8_site_id');
|
||||||
|
if (!$site_id) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get taxonomy data
|
||||||
|
$taxonomy_obj = get_taxonomy($taxonomy);
|
||||||
|
if (!$taxonomy_obj) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get all terms
|
||||||
|
$terms = get_terms(array(
|
||||||
|
'taxonomy' => $taxonomy,
|
||||||
|
'hide_empty' => false
|
||||||
|
));
|
||||||
|
|
||||||
|
if (is_wp_error($terms)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format taxonomy data
|
||||||
|
$taxonomy_data = array(
|
||||||
|
'name' => $taxonomy,
|
||||||
|
'label' => $taxonomy_obj->label,
|
||||||
|
'hierarchical' => $taxonomy_obj->hierarchical,
|
||||||
|
'terms' => array()
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ($terms as $term) {
|
||||||
|
$taxonomy_data['terms'][] = array(
|
||||||
|
'id' => $term->term_id,
|
||||||
|
'name' => $term->name,
|
||||||
|
'slug' => $term->slug,
|
||||||
|
'description' => $term->description,
|
||||||
|
'parent' => $term->parent
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send to IGNY8
|
||||||
|
$response = $api->post("/planner/sites/{$site_id}/taxonomies/", array(
|
||||||
|
'taxonomy' => $taxonomy_data
|
||||||
|
));
|
||||||
|
|
||||||
|
return $response['success'] ? $response['data'] : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync IGNY8 sectors to WordPress taxonomies
|
||||||
|
*
|
||||||
|
* @return array|false Sync result or false on failure
|
||||||
|
*/
|
||||||
|
function igny8_sync_igny8_sectors_to_wp() {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
return array('synced' => 0, 'total' => 0, 'skipped' => true, 'disabled' => true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$site_id = get_option('igny8_site_id');
|
||||||
|
if (!$site_id) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Respect module toggle
|
||||||
|
$enabled_modules = function_exists('igny8_get_enabled_modules') ? igny8_get_enabled_modules() : array();
|
||||||
|
if (!in_array('planner', $enabled_modules, true)) {
|
||||||
|
return array('synced' => 0, 'total' => 0, 'skipped' => true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get sectors from IGNY8
|
||||||
|
$response = $api->get("/planner/sites/{$site_id}/sectors/");
|
||||||
|
|
||||||
|
if (!$response['success']) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$sectors = $response['results'] ?? array();
|
||||||
|
$synced = 0;
|
||||||
|
|
||||||
|
foreach ($sectors as $sector) {
|
||||||
|
$term = term_exists($sector['name'], 'igny8_sectors');
|
||||||
|
if (!$term) {
|
||||||
|
$term = wp_insert_term(
|
||||||
|
$sector['name'],
|
||||||
|
'igny8_sectors',
|
||||||
|
array(
|
||||||
|
'description' => $sector['description'] ?? '',
|
||||||
|
'slug' => $sector['slug'] ?? sanitize_title($sector['name'])
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
wp_update_term($term['term_id'], 'igny8_sectors', array(
|
||||||
|
'description' => $sector['description'] ?? '',
|
||||||
|
'slug' => $sector['slug'] ?? sanitize_title($sector['name'])
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_wp_error($term)) {
|
||||||
|
$term_id = is_array($term) ? $term['term_id'] : $term;
|
||||||
|
update_term_meta($term_id, '_igny8_sector_id', $sector['id']);
|
||||||
|
$synced++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return array('synced' => $synced, 'total' => count($sectors));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync IGNY8 clusters to WordPress taxonomies
|
||||||
|
*
|
||||||
|
* @param int $sector_id Optional sector ID to filter clusters
|
||||||
|
* @return array|false Sync result or false on failure
|
||||||
|
*/
|
||||||
|
function igny8_sync_igny8_clusters_to_wp($sector_id = null) {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
return array('synced' => 0, 'total' => 0, 'skipped' => true, 'disabled' => true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$site_id = get_option('igny8_site_id');
|
||||||
|
if (!$site_id) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$enabled_modules = function_exists('igny8_get_enabled_modules') ? igny8_get_enabled_modules() : array();
|
||||||
|
if (!in_array('planner', $enabled_modules, true)) {
|
||||||
|
return array('synced' => 0, 'total' => 0, 'skipped' => true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build endpoint
|
||||||
|
$endpoint = "/planner/sites/{$site_id}/clusters/";
|
||||||
|
if ($sector_id) {
|
||||||
|
$endpoint .= "?sector_id={$sector_id}";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get clusters from IGNY8
|
||||||
|
$response = $api->get($endpoint);
|
||||||
|
|
||||||
|
if (!$response['success']) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$clusters = $response['results'] ?? array();
|
||||||
|
$synced = 0;
|
||||||
|
|
||||||
|
foreach ($clusters as $cluster) {
|
||||||
|
// Get parent sector term if sector_id exists
|
||||||
|
$parent = 0;
|
||||||
|
if (!empty($cluster['sector_id'])) {
|
||||||
|
$sector_terms = get_terms(array(
|
||||||
|
'taxonomy' => 'igny8_sectors',
|
||||||
|
'meta_key' => '_igny8_sector_id',
|
||||||
|
'meta_value' => $cluster['sector_id'],
|
||||||
|
'hide_empty' => false
|
||||||
|
));
|
||||||
|
|
||||||
|
if (!is_wp_error($sector_terms) && !empty($sector_terms)) {
|
||||||
|
$parent = $sector_terms[0]->term_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$term_id = 0;
|
||||||
|
$existing = get_terms(array(
|
||||||
|
'taxonomy' => 'igny8_clusters',
|
||||||
|
'meta_key' => '_igny8_cluster_id',
|
||||||
|
'meta_value' => $cluster['id'],
|
||||||
|
'hide_empty' => false
|
||||||
|
));
|
||||||
|
|
||||||
|
if (!is_wp_error($existing) && !empty($existing)) {
|
||||||
|
$term_id = $existing[0]->term_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$term_id) {
|
||||||
|
$term = term_exists($cluster['name'], 'igny8_clusters');
|
||||||
|
} else {
|
||||||
|
$term = array('term_id' => $term_id);
|
||||||
|
}
|
||||||
|
if (!$term) {
|
||||||
|
$term = wp_insert_term(
|
||||||
|
$cluster['name'],
|
||||||
|
'igny8_clusters',
|
||||||
|
array(
|
||||||
|
'description' => $cluster['description'] ?? '',
|
||||||
|
'slug' => $cluster['slug'] ?? sanitize_title($cluster['name']),
|
||||||
|
'parent' => $parent
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
wp_update_term($term['term_id'], 'igny8_clusters', array(
|
||||||
|
'description' => $cluster['description'] ?? '',
|
||||||
|
'slug' => $cluster['slug'] ?? sanitize_title($cluster['name']),
|
||||||
|
'parent' => $parent
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_wp_error($term)) {
|
||||||
|
$term_id = is_array($term) ? $term['term_id'] : $term;
|
||||||
|
update_term_meta($term_id, '_igny8_cluster_id', $cluster['id']);
|
||||||
|
if (!empty($cluster['sector_id'])) {
|
||||||
|
update_term_meta($term_id, '_igny8_sector_id', $cluster['sector_id']);
|
||||||
|
}
|
||||||
|
$synced++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return array('synced' => $synced, 'total' => count($clusters));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cron handler: sync sectors/clusters automatically
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
function igny8_cron_sync_taxonomies() {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
error_log('IGNY8: Connection disabled, skipping taxonomy sync');
|
||||||
|
return array('sectors' => array('skipped' => true), 'clusters' => array('skipped' => true));
|
||||||
|
}
|
||||||
|
|
||||||
|
$results = array(
|
||||||
|
'sectors' => igny8_sync_igny8_sectors_to_wp(),
|
||||||
|
'clusters' => igny8_sync_igny8_clusters_to_wp()
|
||||||
|
);
|
||||||
|
|
||||||
|
update_option('igny8_last_taxonomy_sync', current_time('timestamp'));
|
||||||
|
|
||||||
|
return $results;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sync planner keywords for all referenced clusters
|
||||||
|
*
|
||||||
|
* @return array|false
|
||||||
|
*/
|
||||||
|
function igny8_sync_keywords_from_planner() {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
return array('synced_clusters' => 0, 'synced_posts' => 0, 'skipped' => true, 'disabled' => true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$api = new Igny8API();
|
||||||
|
|
||||||
|
if (!$api->is_authenticated()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$site_id = get_option('igny8_site_id');
|
||||||
|
if (!$site_id) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$enabled_modules = function_exists('igny8_get_enabled_modules') ? igny8_get_enabled_modules() : array();
|
||||||
|
if (!in_array('planner', $enabled_modules, true)) {
|
||||||
|
return array('synced_clusters' => 0, 'synced_posts' => 0, 'skipped' => true);
|
||||||
|
}
|
||||||
|
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$cluster_ids = $wpdb->get_col("
|
||||||
|
SELECT DISTINCT meta_value
|
||||||
|
FROM {$wpdb->postmeta}
|
||||||
|
WHERE meta_key = '_igny8_cluster_id'
|
||||||
|
AND meta_value IS NOT NULL
|
||||||
|
AND meta_value != ''
|
||||||
|
");
|
||||||
|
|
||||||
|
if (empty($cluster_ids)) {
|
||||||
|
return array('synced_clusters' => 0, 'synced_posts' => 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
$enabled_post_types = function_exists('igny8_get_enabled_post_types') ? igny8_get_enabled_post_types() : array('post', 'page');
|
||||||
|
|
||||||
|
$synced_clusters = 0;
|
||||||
|
$synced_posts = 0;
|
||||||
|
|
||||||
|
foreach ($cluster_ids as $cluster_id) {
|
||||||
|
$cluster_id = intval($cluster_id);
|
||||||
|
if (!$cluster_id) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = $api->get("/planner/keywords/?cluster_id={$cluster_id}&page_size=500");
|
||||||
|
if (!$response['success']) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$keywords = $response['results'] ?? array();
|
||||||
|
$keyword_ids = array_map('intval', wp_list_pluck($keywords, 'id'));
|
||||||
|
|
||||||
|
// Update cluster term meta
|
||||||
|
$cluster_terms = get_terms(array(
|
||||||
|
'taxonomy' => 'igny8_clusters',
|
||||||
|
'meta_key' => '_igny8_cluster_id',
|
||||||
|
'meta_value' => $cluster_id,
|
||||||
|
'hide_empty' => false
|
||||||
|
));
|
||||||
|
|
||||||
|
if (!is_wp_error($cluster_terms) && !empty($cluster_terms)) {
|
||||||
|
foreach ($cluster_terms as $term) {
|
||||||
|
update_term_meta($term->term_id, '_igny8_keyword_ids', $keyword_ids);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update posts tied to this cluster
|
||||||
|
$posts = get_posts(array(
|
||||||
|
'post_type' => $enabled_post_types,
|
||||||
|
'meta_query' => array(
|
||||||
|
array(
|
||||||
|
'key' => '_igny8_cluster_id',
|
||||||
|
'value' => $cluster_id,
|
||||||
|
'compare' => '='
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'post_status' => 'any',
|
||||||
|
'fields' => 'ids',
|
||||||
|
'nopaging' => true
|
||||||
|
));
|
||||||
|
|
||||||
|
foreach ($posts as $post_id) {
|
||||||
|
update_post_meta($post_id, '_igny8_keyword_ids', $keyword_ids);
|
||||||
|
$synced_posts++;
|
||||||
|
}
|
||||||
|
|
||||||
|
$synced_clusters++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array(
|
||||||
|
'synced_clusters' => $synced_clusters,
|
||||||
|
'synced_posts' => $synced_posts
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cron handler: sync planner keywords
|
||||||
|
*
|
||||||
|
* @return array|false
|
||||||
|
*/
|
||||||
|
function igny8_cron_sync_keywords() {
|
||||||
|
// Skip if connection is disabled
|
||||||
|
if (!igny8_is_connection_enabled()) {
|
||||||
|
error_log('IGNY8: Connection disabled, skipping keyword sync');
|
||||||
|
return array('synced_clusters' => 0, 'synced_posts' => 0, 'skipped' => true);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = igny8_sync_keywords_from_planner();
|
||||||
|
if ($result !== false) {
|
||||||
|
update_option('igny8_last_keyword_sync', current_time('timestamp'));
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map WordPress taxonomy term to IGNY8 cluster
|
||||||
|
*
|
||||||
|
* @param int $term_id Term ID
|
||||||
|
* @param string $taxonomy Taxonomy name
|
||||||
|
* @param int $cluster_id IGNY8 cluster ID
|
||||||
|
* @return bool True on success
|
||||||
|
*/
|
||||||
|
function igny8_map_term_to_cluster($term_id, $taxonomy, $cluster_id) {
|
||||||
|
if ($taxonomy !== 'igny8_clusters') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
update_term_meta($term_id, '_igny8_cluster_id', $cluster_id);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map WordPress taxonomy to IGNY8 sector
|
||||||
|
*
|
||||||
|
* @param string $taxonomy Taxonomy name
|
||||||
|
* @param int $sector_id IGNY8 sector ID
|
||||||
|
* @return bool True on success
|
||||||
|
*/
|
||||||
|
function igny8_map_taxonomy_to_sector($taxonomy, $sector_id) {
|
||||||
|
// Store mapping in options
|
||||||
|
$mappings = get_option('igny8_taxonomy_sector_mappings', array());
|
||||||
|
$mappings[$taxonomy] = $sector_id;
|
||||||
|
update_option('igny8_taxonomy_sector_mappings', $mappings);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
53
igny8-wp-integration-plugin/uninstall.php
Normal file
53
igny8-wp-integration-plugin/uninstall.php
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Uninstall Handler
|
||||||
|
*
|
||||||
|
* Cleans up plugin data on uninstall
|
||||||
|
*
|
||||||
|
* @package Igny8Bridge
|
||||||
|
*/
|
||||||
|
|
||||||
|
// If uninstall not called from WordPress, then exit
|
||||||
|
if (!defined('WP_UNINSTALL_PLUGIN')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove all options
|
||||||
|
delete_option('igny8_access_token');
|
||||||
|
delete_option('igny8_refresh_token');
|
||||||
|
delete_option('igny8_email');
|
||||||
|
delete_option('igny8_site_id');
|
||||||
|
delete_option('igny8_last_site_sync');
|
||||||
|
delete_option('igny8_last_site_import_id');
|
||||||
|
delete_option('igny8_token_refreshed_at');
|
||||||
|
delete_option('igny8_bridge_version');
|
||||||
|
|
||||||
|
// Remove all post meta (optional - uncomment if you want to remove all meta on uninstall)
|
||||||
|
/*
|
||||||
|
global $wpdb;
|
||||||
|
$wpdb->query("
|
||||||
|
DELETE FROM {$wpdb->postmeta}
|
||||||
|
WHERE meta_key LIKE '_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');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: Taxonomies and terms are NOT deleted
|
||||||
|
// They remain in WordPress for user reference
|
||||||
|
// Only the taxonomy registration is removed
|
||||||
|
|
||||||
Reference in New Issue
Block a user