Created
July 10, 2025 17:13
-
-
Save stingray82/d8283689ce3613fe89ee15df046aa93b to your computer and use it in GitHub Desktop.
Example - Custom Rest API End Point - FluentCRM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Register the REST API route | |
| add_action('rest_api_init', function () { | |
| register_rest_route('custom-fluentcrm-api/v1', '/add-note', [ | |
| 'methods' => 'POST', | |
| 'callback' => 'handle_custom_fluentcrm_add_note', | |
| 'permission_callback' => function () { | |
| return current_user_can('edit_users'); // Adjust capability if needed | |
| }, | |
| 'args' => [ | |
| 'user_input' => [ | |
| 'required' => true, | |
| 'type' => 'string' | |
| ], | |
| 'note_content' => [ | |
| 'required' => true, | |
| 'type' => 'string' | |
| ], | |
| 'note_type' => [ | |
| 'required' => false, | |
| 'type' => 'string', | |
| 'default' => 'custom' | |
| ], | |
| 'note_title' => [ | |
| 'required' => false, | |
| 'type' => 'string', | |
| 'default' => 'Note' | |
| ] | |
| ] | |
| ]); | |
| }); | |
| // Callback for the REST API | |
| function handle_custom_fluentcrm_add_note(WP_REST_Request $request) { | |
| $user_input = $request->get_param('user_input'); | |
| $note_content = $request->get_param('note_content'); | |
| $note_type = $request->get_param('note_type'); | |
| $note_title = $request->get_param('note_title'); | |
| $result = add_custom_note_to_fluentcrm_user($user_input, $note_content, $note_type, $note_title); | |
| if (is_wp_error($result)) { | |
| return new WP_REST_Response([ | |
| 'success' => false, | |
| 'message' => $result->get_error_message(), | |
| ], 400); | |
| } | |
| return new WP_REST_Response([ | |
| 'success' => true, | |
| 'message' => 'Note successfully added.', | |
| ], 200); | |
| } | |
| // Your provided helper function | |
| function add_custom_note_to_fluentcrm_user($user_input, $note_content, $note_type = 'custom', $note_title = 'Note') { | |
| if (!class_exists('\FluentCrm\App\Models\Subscriber')) { | |
| return new WP_Error('fluentcrm_missing', 'FluentCRM is not active.'); | |
| } | |
| $email = ''; | |
| if (is_numeric($user_input)) { | |
| $user = get_user_by('ID', $user_input); | |
| $email = $user ? $user->user_email : ''; | |
| } elseif (is_object($user_input) && isset($user_input->user_email)) { | |
| $email = $user_input->user_email; | |
| } elseif (is_string($user_input) && is_email($user_input)) { | |
| $email = $user_input; | |
| } | |
| if (!$email) { | |
| return new WP_Error('invalid_user', 'Invalid user input provided.'); | |
| } | |
| $contact = \FluentCrm\App\Models\Subscriber::where('email', $email)->first(); | |
| if (!$contact) { | |
| return new WP_Error('no_contact', "No FluentCRM contact found for email: $email"); | |
| } | |
| \FluentCrm\App\Models\SubscriberNote::create([ | |
| 'subscriber_id' => $contact->id, | |
| 'type' => $note_type, | |
| 'title' => $note_title, | |
| 'description' => $note_content, | |
| 'created_by' => get_current_user_id(), | |
| ]); | |
| return true; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment