getInputs(); $id = (int) ( $inputs['staff_id'] ?? 0 ); $name = $id > 0 ? get_the_author_meta( 'display_name', $id ) : ''; $label = '' !== $name ? "\"{$name}\" (ID: {$id})" : "#{$id}"; $fields = array_keys( array_diff_key( $inputs, [ 'staff_id' => true ] ) ); $what = $fields ? implode( ', ', $fields ) : 'nothing'; return "Update staff {$label}: change {$what}"; } public function get_approval_inputs(): array { $inputs = $this->getInputs(); if ( isset( $inputs['staff_id'] ) ) { $id = (int) $inputs['staff_id']; $name = get_the_author_meta( 'display_name', $id ); $inputs['staff_id'] = '' !== $name ? "{$name} (ID: {$id})" : (string) $id; } return $inputs; } public function __invoke( int $staff_id, ?string $first_name = null, ?string $last_name = null, ?string $email = null, ?string $phone = null ): array { $check = $this->guard_admin(); if ( $check ) { return $check; } if ( $staff_id <= 0 || ! class_exists( '\Timetics\Core\Staffs\Staff' ) ) { return [ 'error' => 'A valid staff_id is required.' ]; } if ( null === $first_name && null === $last_name && null === $email && null === $phone ) { return [ 'error' => 'No changes were specified. Provide first_name, last_name, email, or phone.' ]; } $staff = new \Timetics\Core\Staffs\Staff( $staff_id ); if ( ! $staff->is_staff() ) { return [ 'error' => "Staff #{$staff_id} was not found." ]; } if ( null !== $first_name && '' === trim( $first_name ) ) { return [ 'error' => 'first_name cannot be empty.' ]; } if ( null !== $email && ! is_email( $email ) ) { return [ 'error' => 'email must be a valid email address.' ]; } try { // Resend the full profile, preserving schedule and image, so the // controller's blanket rewrite does not wipe them. $body = [ 'first_name' => null !== $first_name ? sanitize_text_field( $first_name ) : $staff->get_first_name(), 'last_name' => null !== $last_name ? sanitize_text_field( $last_name ) : $staff->get_last_name(), 'email' => null !== $email ? sanitize_email( $email ) : $staff->get_email(), 'phone' => null !== $phone ? sanitize_text_field( $phone ) : $staff->get_phone(), 'schedule' => $staff->get_schedule(), 'image' => get_user_meta( $staff_id, '_staff_image', true ), ]; $request = $this->build_request( 'PUT', '/timetics/v1/staffs/' . $staff_id, [], $body, [ 'staff_id' => $staff_id ] ); if ( is_array( $request ) ) { return $request; } $controller = \Timetics\Core\Staffs\Api_Staff::instance(); $data = $this->extract_response( $controller->update_item( $request ), "Failed to update staff #{$staff_id}." ); if ( isset( $data['error'] ) ) { return $data; } return [ 'success' => true, 'staff_id' => $staff_id, 'staff' => $this->shape_staff( $data ), 'message' => "Staff #{$staff_id} updated successfully.", ]; } catch ( \Throwable $e ) { Log::error( 'Timetics_Update_Staff_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Failed to update staff: ' . $e->getMessage() ]; } } }