getInputs(); $id = $inputs['team_member_id'] ?? '?'; $changes = []; if ( ! empty( $inputs['first_name'] ) || ! empty( $inputs['last_name'] ) ) { $changes[] = 'name'; } if ( ! empty( $inputs['email'] ) ) { $changes[] = 'email'; } if ( ! empty( $inputs['phone'] ) ) { $changes[] = 'phone'; } if ( array_key_exists( 'service_ids', $inputs ) ) { $changes[] = 'assigned services'; } $summary = "Update team member #{$id}"; if ( ! empty( $changes ) ) { $summary .= ': ' . implode( ', ', $changes ); } return $summary; } /** * Human-readable inputs shown in the approval card table. * * @return array */ public function get_approval_inputs(): array { return $this->getInputs(); } /** * Execute the tool after human approval. * * @param int $team_member_id Team-member (WP user) ID. * @param string|null $first_name New first name. * @param string|null $last_name New last name. * @param string|null $email New email. * @param string|null $phone New phone. * @param array|null $service_ids Replacement service IDs. * @return array */ public function __invoke( int $team_member_id, ?string $first_name = null, ?string $last_name = null, ?string $email = null, ?string $phone = null, ?array $service_ids = null ): array { $check = $this->guard_admin(); if ( $check ) { return $check; } $access = $this->assert_team_member( $team_member_id ); if ( $access ) { return $access; } if ( null !== $email && ! is_email( $email ) ) { return [ 'error' => 'email must be a valid email address.' ]; } try { // find() is an instance method on the team-member model, and its // update endpoint requires name/email and rewrites the services list, // so rebuild the full record from current values and merge changes on top. $current = ( new \Booktics\Models\Team_Member_Model() )->find( $team_member_id ); if ( ! $current ) { return [ 'error' => "Team member #{$team_member_id} was not found." ]; } $current = $current->to_array(); $changed = []; $body = [ 'first_name' => $current['first_name'] ?? '', 'last_name' => $current['last_name'] ?? '', 'email' => $current['email'] ?? ( $current['user_email'] ?? '' ), 'phone' => $current['phone'] ?? '', 'services' => array_values( array_map( 'intval', (array) ( $current['services'] ?? [] ) ) ), ]; if ( null !== $first_name ) { if ( '' === trim( $first_name ) ) { return [ 'error' => 'first_name cannot be empty.' ]; } $body['first_name'] = sanitize_text_field( $first_name ); $changed[] = 'first_name'; } if ( null !== $last_name ) { $body['last_name'] = sanitize_text_field( $last_name ); $changed[] = 'last_name'; } if ( null !== $email ) { $body['email'] = sanitize_email( $email ); $changed[] = 'email'; } if ( null !== $phone ) { $body['phone'] = sanitize_text_field( $phone ); $changed[] = 'phone'; } if ( null !== $service_ids ) { $body['services'] = array_values( array_unique( array_map( 'intval', $service_ids ) ) ); $changed[] = 'services'; } if ( empty( $changed ) ) { return [ 'error' => 'No fields provided to update.' ]; } $request = $this->build_json_request( 'PUT', '/booktics/v1/team-members/' . $team_member_id, $body, $team_member_id ); if ( is_array( $request ) ) { return $request; } $controller = new \Booktics\Team_Member\Controllers\Team_Member_Controller(); $data = $this->extract_response( $controller->update_item( $request ), 'Failed to update team member.' ); if ( isset( $data['error'] ) ) { return $data; } return [ 'success' => true, 'team_member' => $this->shape_team_member( $data ), 'updated_fields' => $changed, 'message' => "Team member #{$team_member_id} updated successfully.", ]; } catch ( \Throwable $e ) { Log::error( 'Booktics_Update_Team_Member_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Failed to update team member: ' . $e->getMessage() ]; } } }