$data["email"] ); // Inicjalizacja sesji cURL $ch = curl_init(); // Ustawienia opcji cURL curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Accept: application/json', 'Authorization: Bearer ' . $token ) ); // Wykonanie żądania $response = curl_exec($ch); // Pobranie HTTP Code $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // // Sprawdzenie błędów // if (curl_errno($ch)) { // echo 'Błąd cURL: ' . curl_error($ch); // } curl_close($ch); if ($http_code == 201) { // dodano prawidłowo wp_send_json([ "message" => $email_created ], 201); } else { // email już istnieje wp_send_json([ "message" => $email_exists ], 200); } } else { // obsługa e-maila za pomocą naszego post: custom_post_type $isAlredy = get_page_by_title($email, OBJECT, 'email'); if ($isAlredy > 0) { wp_send_json([ "message" => $email_exists ], 200); } else { try { $postarr = [ "post_content" => $data["email"], "post_title" => $data["email"], "post_type" => "email", "post_status" => "publish" ]; wp_insert_post($postarr, true); wp_send_json([ "message" => $email_created ], 201); } catch (Exception $e) { wp_send_json_error($e->getMessage()); } } wp_die(); } } add_action("rest_api_init", function () { register_rest_route( "tsu/v1", "/newsletter/add_email", array( "methods" => "POST", "callback" => "add_email_from_newsletter" ) ); });