api = new Aiflab_Chatweb_API();
// Set temp directory
$upload_dir = wp_upload_dir();
$this->temp_dir = $upload_dir['basedir'] . '/chatweb-posts/';
// Create directory if not exists
if (!file_exists($this->temp_dir)) {
wp_mkdir_p($this->temp_dir);
// Add .htaccess to protect files
$htaccess = $this->temp_dir . '.htaccess';
if (!file_exists($htaccess)) {
file_put_contents($htaccess, "Order deny,allow\nDeny from all");
}
// Add index.php
$index = $this->temp_dir . 'index.php';
if (!file_exists($index)) {
file_put_contents($index, 'post_status !== 'publish') {
return false;
}
// Get content with filters applied
$content = apply_filters('the_content', $post->post_content);
// Get featured image
$featured_image = get_the_post_thumbnail_url($post_id, 'full');
// Get excerpt
$excerpt = $post->post_excerpt;
if (empty($excerpt)) {
$excerpt = wp_trim_words($post->post_content, 55, '...');
}
// Build post data
$post_data = array(
'source' => 'wordpress',
'source_product_id' => (string) $post_id,
'title' => $post->post_title,
'slug' => $post->post_name,
'link' => get_permalink($post_id),
'description' => $content,
'short_description' => $excerpt,
'status' => $post->post_status,
'image_url' => $featured_image ? $featured_image : '',
'additional_images' => $this->extract_images_from_content($content),
'crawled_at' => current_time('Y-m-d H:i:s')
);
return $post_data;
}
/**
* Extract all image URLs from content (only main src, not srcset)
*
* @param string $content HTML content
* @return array Array of image URLs
*/
private function extract_images_from_content($content) {
$images = array();
// Match all img tags and extract src attribute
preg_match_all('/
]+src=["\']([^"\']+)["\'][^>]*>/i', $content, $matches);
if (!empty($matches[1])) {
// Remove duplicates and reindex
$images = array_values(array_unique($matches[1]));
}
return $images;
}
/**
* Create JSON file for a single post
*
* @param int $post_id Post ID
* @return string|false File path or false on failure
*/
public function create_post_json_file($post_id) {
$post_data = $this->prepare_post_data($post_id);
if (!$post_data) {
return false;
}
// Create filename: post_.json
$filename = 'post_' . $post_id . '.json';
$file_path = $this->temp_dir . $filename;
// Write JSON file
$json_content = json_encode($post_data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
$result = file_put_contents($file_path, $json_content);
if ($result === false) {
return false;
}
return $file_path;
}
/**
* Sync a single post (create file + upload)
*
* @param int $post_id Post ID
* @return array Result with success status
*/
public function sync_single_post($post_id) {
// Create JSON file
$file_path = $this->create_post_json_file($post_id);
if (!$file_path) {
return array(
'success' => false,
'error' => 'Failed to create JSON file for post ' . $post_id,
'post_id' => $post_id
);
}
// Upload file to API
$result = $this->api->upload_file('extension/posts/sync', $file_path);
// Delete file immediately after API call (success or failure)
if (file_exists($file_path)) {
@unlink($file_path);
}
// Add post_id to result
$result['post_id'] = $post_id;
return $result;
}
/**
* Sync multiple posts in a batch
*
* @param array $post_ids Array of post IDs
* @return array Results for each post
*/
public function sync_posts_batch($post_ids) {
$results = array(
'synced' => 0,
'failed' => 0,
'failed_posts' => array()
);
foreach ($post_ids as $post_id) {
$result = $this->sync_single_post($post_id);
if ($result['success']) {
$results['synced']++;
} else {
$results['failed']++;
$post = get_post($post_id);
$results['failed_posts'][] = array(
'id' => $post_id,
'name' => $post ? $post->post_title : 'Unknown',
'error' => $result['error'] ?? 'Unknown error'
);
}
}
return $results;
}
/**
* Cleanup all temporary JSON files
*
* @return int Number of files deleted
*/
public function cleanup_temp_files() {
$files = glob($this->temp_dir . 'post_*.json');
$count = 0;
if (!empty($files)) {
foreach ($files as $file) {
if (is_file($file) && unlink($file)) {
$count++;
}
}
}
return $count;
}
/**
* Get temp directory path
*
* @return string Temp directory path
*/
public function get_temp_dir() {
return $this->temp_dir;
}
}