api_key = $api_key; $this->did = $did; error_log('[Bluesky Connector] Initializing API with DID: ' . $did); } public function create_post($post_data) { error_log('[Bluesky Connector] Creating post with data: ' . wp_json_encode($post_data)); $headers = array( 'Authorization' => 'Bearer ' . $this->api_key, 'Content-Type' => 'application/json', ); $response = wp_remote_post($this->api_url . '/com.atproto.repo.createRecord', array( 'headers' => $headers, 'body' => wp_json_encode(array( 'repo' => $this->did, 'collection' => 'app.bsky.feed.post', 'record' => $post_data, )), 'timeout' => 30, )); if (is_wp_error($response)) { error_log('[Bluesky Connector] Create post error: ' . $response->get_error_message()); return array('error' => $response->get_error_message()); } $response_code = wp_remote_retrieve_response_code($response); $response_body = wp_remote_retrieve_body($response); error_log('[Bluesky Connector] Create post response code: ' . $response_code); error_log('[Bluesky Connector] Create post response: ' . $response_body); return json_decode($response_body, true); } public function resolve_handle($handle) { error_log('[Bluesky Connector] Resolving handle: ' . $handle); $response = wp_remote_get($this->api_url . '/com.atproto.identity.resolveHandle', array( 'query' => array( 'handle' => $handle, ), 'timeout' => 30, )); if (is_wp_error($response)) { error_log('[Bluesky Connector] Resolve handle error: ' . $response->get_error_message()); return array('error' => $response->get_error_message()); } return json_decode(wp_remote_retrieve_body($response), true); } public function upload_blob($file_path_or_data, $mime_type) { error_log('[Bluesky Connector] Starting blob upload'); error_log('[Bluesky Connector] Mime type: ' . $mime_type); $headers = array( 'Authorization' => 'Bearer ' . $this->api_key, 'Content-Type' => $mime_type, ); // Determine if input is a file path or raw data if (is_string($file_path_or_data) && file_exists($file_path_or_data)) { error_log('[Bluesky Connector] Reading from file path: ' . $file_path_or_data); $file_contents = file_get_contents($file_path_or_data); if ($file_contents === false) { error_log('[Bluesky Connector] Failed to read file'); return array('error' => 'Failed to read file'); } } else { error_log('[Bluesky Connector] Using provided data directly'); $file_contents = $file_path_or_data; } // Check content size $content_size = strlen($file_contents); error_log('[Bluesky Connector] Content size: ' . $content_size . ' bytes'); if ($content_size > 1000000) { error_log('[Bluesky Connector] Content size exceeds 1MB limit'); return array('error' => 'File size exceeds 1MB limit'); } $response = wp_remote_post($this->api_url . '/com.atproto.repo.uploadBlob', array( 'headers' => $headers, 'body' => $file_contents, 'timeout' => 30, )); if (is_wp_error($response)) { error_log('[Bluesky Connector] Upload blob error: ' . $response->get_error_message()); return array('error' => $response->get_error_message()); } $response_code = wp_remote_retrieve_response_code($response); $response_body = wp_remote_retrieve_body($response); error_log('[Bluesky Connector] Upload blob response code: ' . $response_code); error_log('[Bluesky Connector] Upload blob response: ' . $response_body); if ($response_code !== 200) { return array('error' => 'Upload failed with status ' . $response_code); } return json_decode($response_body, true); } }