WP_REST_Server::CREATABLE, 'callback' => array( $this, 'handle' ), 'permission_callback' => array( 'MYBOT_REST_Controller', 'public_permission' ), 'args' => array( 'name' => array( 'required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'phone' => array( 'required' => false, 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ), 'email' => array( 'required' => false, 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_email', ), 'note' => array( 'required' => false, 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_textarea_field', ), 'consent' => array( 'required' => false, 'type' => 'boolean', 'default' => false, ), 'session_id' => array( 'required' => false, 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ), 'lang' => array( 'required' => false, 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_key', ), 'source' => array( 'required' => false, 'type' => 'string', 'default' => '', ), /* * The honeypot. It is declared here like every other field so that WordPress accepts it — * and the widget renders it hidden and `tabindex="-1"`, so a person never sees it and a * screen reader never reads it. */ 'website' => array( 'required' => false, 'type' => 'string', 'default' => '', ), ), ) ); } /** * Stores one card. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function handle( $request ) { $plugin = mybot(); $limit = $plugin->security->enforce_rate_limit(); if ( is_wp_error( $limit ) ) { return $limit; } $lang = $plugin->languages->resolve( (string) $request->get_param( 'lang' ) ); $ui = MYBOT_Languages::ui_strings( $lang ); /* The honeypot: answer like a saved card, store nothing, say nothing. */ if ( '' !== trim( (string) $request->get_param( 'website' ) ) ) { return new WP_REST_Response( array( 'saved' => true, 'message' => isset( $ui['contactDone'] ) ? $ui['contactDone'] : '', ), 200 ); } $stored = MYBOT_Contacts::create( array( 'name' => (string) $request->get_param( 'name' ), 'phone' => (string) $request->get_param( 'phone' ), 'email' => (string) $request->get_param( 'email' ), 'note' => (string) $request->get_param( 'note' ), 'consent' => (bool) $request->get_param( 'consent' ), 'session_id' => (string) $request->get_param( 'session_id' ), 'lang' => $lang, 'source' => (string) $request->get_param( 'source' ), ) ); if ( is_wp_error( $stored ) ) { $stored->add_data( array( 'status' => 400 ) ); return $stored; } /** * Fires after a visitor left their details. * * The card is NOT a request: nobody is waiting for an answer, so this event does not notify anyone by * itself. It exists so that a site can push the card to the place its team actually works — the same * door `mybot_support_request_created` opens for questions. * * @param array $card The stored card. */ do_action( 'mybot_contact_saved', $stored ); return new WP_REST_Response( array( 'saved' => true, 'id' => (int) $stored['id'], 'channel' => (string) $stored['channel'], 'message' => isset( $ui['contactDone'] ) ? $ui['contactDone'] : '', ), 200 ); } }