/* Plugin Name: Image Processor & Optimizer Description: Downloads external images, sets featured image, centers images, optimizes them in background. Version: 1.2 Author: rasanashr */ if (!defined('ABSPATH')) exit; add_action('save_post', 'process_external_images', 10, 3); function process_external_images($post_id, $post, $update) { if (wp_is_post_revision($post_id) || wp_is_post_autosave($post_id)) return; if ($post->post_type != 'post') return; if (!in_array($post->post_status, ['publish', 'future'])) return; $content = $post->post_content; $dom = new DOMDocument(); libxml_use_internal_errors(true); $dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8')); libxml_clear_errors(); $xpath = new DOMXPath($dom); $images = $xpath->query('//img'); if (!$images || $images->length === 0) return; require_once ABSPATH . 'wp-admin/includes/image.php'; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; $featured_set = false; $updated = false; foreach ($images as $img) { $src = $img->getAttribute('data-src') ?: $img->getAttribute('src'); if (strpos($src, home_url()) === 0) continue; $parsed_url = strtok($src, '?#'); $path = parse_url($parsed_url, PHP_URL_PATH); $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION)); if (!in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp'])) continue; $tmp = download_url($parsed_url); if (is_wp_error($tmp)) continue; $file_array = [ 'name' => basename($path), 'tmp_name' => $tmp, ]; $overrides = ['test_form' => false]; $results = wp_handle_sideload($file_array, $overrides); if (!empty($results['error'])) { @unlink($file_array['tmp_name']); continue; } $file_path = $results['file']; $attachment = [ 'post_mime_type' => $results['type'], 'post_title' => sanitize_file_name(basename($file_path)), 'post_content' => '', 'post_status' => 'inherit' ]; $attach_id = wp_insert_attachment($attachment, $file_path, $post_id); if (is_wp_error($attach_id)) continue; $attach_meta = wp_generate_attachment_metadata($attach_id, $file_path); wp_update_attachment_metadata($attach_id, $attach_meta); update_post_meta($attach_id, '_wp_attachment_image_alt', $post->post_title); // بهینه‌سازی در پس‌زمینه do_action('optimize_image_file_background', $file_path); $new_src = wp_get_attachment_url($attach_id); $img->setAttribute('src', esc_url($new_src)); $img->removeAttribute('data-src'); $existing_class = $img->getAttribute('class'); $img->setAttribute('class', trim($existing_class . ' aligncenter')); if (!$featured_set) { set_post_thumbnail($post_id, $attach_id); $featured_set = true; } $updated = true; } if ($updated) { // فقط تگ body را استخراج کن $body = $dom->getElementsByTagName('body')->item(0); $new_content = ''; foreach ($body->childNodes as $child) { $new_content .= $dom->saveHTML($child); } // بروزرسانی محتوا wp_remove_object_terms($post_id, null, 'post_tag'); // باگ خاص در برخی نسخه‌ها جلوگیری می‌شود remove_action('save_post', 'process_external_images'); wp_update_post(['ID' => $post_id, 'post_content' => $new_content]); add_action('save_post', 'process_external_images', 10, 3); } } // ✅ هوک بهینه‌سازی تصاویر در پس‌زمینه add_action('optimize_image_file_background', function ($file_path) { if (!file_exists($file_path)) return; $ext = strtolower(pathinfo($file_path, PATHINFO_EXTENSION)); if (in_array($ext, ['jpg', 'jpeg'])) { exec("jpegoptim --strip-all --max=85 " . escapeshellarg($file_path)); } elseif ($ext === 'png') { exec("optipng -o2 " . escapeshellarg($file_path)); } }, 10, 1);