'Nonce verification failed.')); return; } $image_url = isset($_POST['image_url']) ? esc_url_raw($_POST['image_url']) : ''; $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; if (empty($image_url) || !$post_id) { wp_send_json_error(array('message' => 'Invalid image URL or post ID.')); return; } $post_title = get_the_title($post_id); if (empty($post_title)) { wp_send_json_error(array('message' => 'Your post must have a title to save images in an SEO-friendly way. Put a title to your post and try again.')); return; } // Başlığı ASCII karakterlere dönüştür ve özel karakterleri İngilizce karşılıklarıyla değiştir $title_cleaned = iconv('UTF-8', 'ASCII//TRANSLIT', $post_title); $title_cleaned = sanitize_title_with_dashes($title_cleaned); $title_cleaned = str_replace('-', '_', $title_cleaned); $response = wp_remote_get($image_url); if (is_wp_error($response)) { wp_send_json_error(array('message' => 'Error downloading image.')); return; } $image_data = wp_remote_retrieve_body($response); if (empty($image_data)) { wp_send_json_error(array('message' => 'Error downloading image.')); return; } $image_hash = md5($image_data); global $wpdb; $existing_attachment_id = $wpdb->get_var($wpdb->prepare(" SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_image_hash' AND meta_value = %s", $image_hash)); if ($existing_attachment_id) { wp_send_json_success(array('attachment_id' => $existing_attachment_id)); return; } $upload_dir = wp_upload_dir(); $file_name_base = $title_cleaned; $file_name = $file_name_base . '.png'; $i = 0; // Dosya adı benzersiz olana kadar döngü while (file_exists($upload_dir['path'] . '/' . $file_name)) { $i++; $file_name = $file_name_base . '_' . $i . '.png'; } $upload = wp_upload_bits($file_name, null, $image_data); if ($upload['error']) { wp_send_json_error(array('message' => 'File write error: ' . $upload['error'])); return; } $file_path = $upload['file']; $file_type = wp_check_filetype($file_name, null); $attachment = array( 'guid' => $upload['url'], 'post_mime_type' => $file_type['type'], 'post_title' => $post_title, 'post_content' => '', 'post_status' => 'inherit', ); $attach_id = wp_insert_attachment($attachment, $file_path); require_once(ABSPATH . 'wp-admin/includes/image.php'); $attach_data = wp_generate_attachment_metadata($attach_id, $file_path); wp_update_attachment_metadata($attach_id, $attach_data); // Alternatif metni ekleyin (alt text) update_post_meta($attach_id, '_wp_attachment_image_alt', $post_title); add_post_meta($attach_id, '_wp_attachment_image_hash', $image_hash, true); wp_send_json_success(array('attachment_id' => $attach_id)); } public function aipstx_ensure_image_and_set_featured() { // Nonce kontrolü if (!check_ajax_referer('aipstx_nonce', 'nonce', false)) { wp_send_json_error(array('message' => 'Nonce verification failed.')); return; } if (!isset($_POST['post_id']) || !isset($_POST['image_id'])) { wp_send_json_error(array('message' => "Invalid post ID or image ID.")); return; } $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; $attachment_id = isset($_POST['image_id']) ? intval($_POST['image_id']) : 0; $result = set_post_thumbnail($post_id, $attachment_id); if ($result) { // Yeni öne çıkan görselin URL'sini al $featured_image_url = wp_get_attachment_url($attachment_id); // Başarılı yanıtı ve görsel URL'sini gönder wp_send_json_success(array('message' => 'Featured image set with new library image.', 'featured_image_url' => $featured_image_url)); } else { $error_string = 'Failed to set featured image for post ID ' . $post_id . ' after creating new attachment ID ' . $attachment_id; wp_send_json_error(array('message' => $error_string)); } } public function aipstx_add_post_content() { if (!check_ajax_referer('aipstx_nonce', 'nonce', false)) { wp_send_json_error(array('message' => 'Nonce verification failed.')); return; } $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; $media_id = isset($_POST['media_id']) ? intval($_POST['media_id']) : 0; if (!$post_id || !$media_id) { wp_send_json_error('Invalid post ID or media ID.'); return; } // Yazının başlığını al $post_title = get_the_title($post_id); if (!$post_title) { wp_send_json_error('Unable to retrieve post title.'); return; } // Get the media URL $media_url = wp_get_attachment_url($media_id); if (!$media_url) { wp_send_json_error('Invalid media ID.'); return; } // Get the current post content $post_content = get_post_field('post_content', $post_id); // Create the image HTML $image_html = '' . esc_attr($post_title) . ''; // Append the image HTML to the post content $new_post_content = $post_content . $image_html; // Update the post with the new content wp_update_post(array( 'ID' => $post_id, 'post_content' => $new_post_content )); // Return the image HTML so it can be added to the post content in the editor without a page reload wp_send_json_success(array('image_html' => $image_html)); } } aipstx_btn::get_instance(); }