'You do not have permission to set this file.']); } // User Roles restriction $user_roles = ai_tts_get_option('user_roles'); $user_roles_to_enable = ai_tts_get_option('user_roles_to_enable'); if($user_roles == 'specific' && !empty($user_roles_to_enable)) { $current_user = wp_get_current_user(); $current_user_roles = $current_user->roles; $intersect = array_intersect($current_user_roles, $user_roles_to_enable); if(empty($intersect)) { wp_send_json_error(['message' => 'You do not have permission to set this file.']); } } // Nonce $nonce = isset($_POST['nonce']) ? wp_unslash($_POST['nonce']) : ''; if (!$nonce || !wp_verify_nonce($nonce, 'ai_tts_nonce')) { wp_send_json_error(['message' => 'Nonce verification failed.']); } $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; $file_url = isset($_POST['file_url']) ? esc_url_raw(wp_unslash($_POST['file_url'])) : ''; $attachment_id = isset($_POST['attachment_id']) ? intval($_POST['attachment_id']) : 0; if (!$post_id || empty($file_url)) { wp_send_json_error(['message' => 'Missing post or file.']); } if (!get_post($post_id) || !current_user_can('edit_post', $post_id)) { wp_send_json_error(['message' => 'You do not have permission to set this file.']); } // Decide and store location meta $location = 'external'; $plugin_upload_base = content_url('/uploads/ai-text-to-speech/'); if ($attachment_id > 0) { $location = 'wp_media'; update_post_meta($post_id, 'ai_tts_attachment_id', $attachment_id); } elseif (strpos($file_url, $plugin_upload_base) === 0) { $location = 'local'; delete_post_meta($post_id, 'ai_tts_attachment_id'); } else { delete_post_meta($post_id, 'ai_tts_attachment_id'); } update_post_meta($post_id, 'ai_tts_location', $location); update_post_meta($post_id, 'ai_tts_file_url', $file_url); // A custom file has no generated intro, so there is no spoken date to keep in sync delete_post_meta($post_id, 'ai_tts_intro_date'); delete_post_meta($post_id, 'ai_tts_intro_date_stale'); // Cleanup any legacy file name when not locally stored if ($location !== 'local') { delete_post_meta($post_id, 'ai_tts_file_name'); } wp_send_json_success([ 'message' => 'File set successfully.', 'file_url' => $file_url, 'location' => $location, 'attachment_id' => $attachment_id, ]); }