/* Plugin Name: Image Processor & Optimizer Description: Downloads external images, sets featured image, centers images, optimizes them in background. Version: 1.1 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(); @$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $images_nodes = $dom->getElementsByTagName('img'); $images = []; foreach ($images_nodes as $img) { $src = $img->getAttribute('src'); $data_src = $img->getAttribute('data-src'); $image_url = !empty($data_src) ? $data_src : $src; if (strpos($image_url, home_url()) === 0) continue; $parsed_url = strtok($image_url, '?#'); $path = parse_url($parsed_url, PHP_URL_PATH); $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION)); $allowed = ['jpg', 'jpeg', 'png', 'gif', 'webp']; if (!in_array($extension, $allowed)) continue; $images[] = ['node' => $img, 'url' => $parsed_url, 'extension' => $extension]; } if (empty($images)) return; $upload_dir = wp_upload_dir(); $new_content = $content; $featured_set = false; $success = true; foreach ($images as $image) { $img_node = $image['node']; $image_url = $image['url']; require_once ABSPATH . 'wp-admin/includes/image.php'; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; $tmp = download_url($image_url); if (is_wp_error($tmp)) { $success = false; continue; } $file = ['name' => basename($image_url), 'tmp_name' => $tmp]; $overrides = ['test_form' => false]; $results = wp_handle_sideload($file, $overrides); if (isset($results['error'])) { @unlink($file['tmp_name']); $success = false; continue; } $url = $results['url']; $type = $results['type']; $file = $results['file']; $filename = basename($file); $attachment = [ 'post_mime_type' => $type, 'post_title' => sanitize_file_name($filename), 'post_content' => '', 'post_status' => 'inherit' ]; $attach_id = wp_insert_attachment($attachment, $file, $post_id); if (is_wp_error($attach_id)) { @unlink($file); $success = false; continue; } wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $file)); update_post_meta($attach_id, '_wp_attachment_image_alt', $post->post_title); // ✅ بهینه‌سازی در پس‌زمینه do_action('optimize_image_file_background', $file); // 🔁 جایگزینی تصویر در محتوا $new_src = wp_get_attachment_url($attach_id); $img_node->setAttribute('src', esc_url($new_src)); $img_node->removeAttribute('data-src'); $img_node->setAttribute('class', 'aligncenter'); $new_img_tag = $dom->saveHTML($img_node); $new_content = str_replace($dom->saveHTML($img_node), $new_img_tag, $new_content); if (!$featured_set) { set_post_thumbnail($post_id, $attach_id); $featured_set = true; } } if ($success) { remove_action('save_post', 'process_external_images'); wp_update_post([ 'ID' => $post_id, 'post_content' => $dom->saveHTML() ]); add_action('save_post', 'process_external_images', 10, 3); } else { wp_update_post(['ID' => $post_id, 'post_status' => 'draft']); } } // ✅ تعریف هوک بهینه‌سازی تصاویر 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);