getInputs(); $id = (int) ( $inputs['customer_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, [ 'customer_id' => true ] ) ); $what = $fields ? implode( ', ', $fields ) : 'nothing'; return "Update customer {$label}: change {$what}"; } public function get_approval_inputs(): array { $inputs = $this->getInputs(); if ( isset( $inputs['customer_id'] ) ) { $id = (int) $inputs['customer_id']; $name = get_the_author_meta( 'display_name', $id ); $inputs['customer_id'] = '' !== $name ? "{$name} (ID: {$id})" : (string) $id; } return $inputs; } public function __invoke( int $customer_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 ( $customer_id <= 0 || ! class_exists( '\Timetics\Core\Customers\Customer' ) ) { return [ 'error' => 'A valid customer_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.' ]; } $customer = new \Timetics\Core\Customers\Customer( $customer_id ); if ( ! $customer->is_customer() ) { return [ 'error' => "Customer #{$customer_id} was not found." ]; } if ( null !== $first_name && '' === trim( $first_name ) ) { return [ 'error' => 'first_name cannot be empty.' ]; } if ( null !== $last_name && '' === trim( $last_name ) ) { return [ 'error' => 'last_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 the image, so the controller's // blanket rewrite does not blank the phone or photo. $body = [ 'first_name' => null !== $first_name ? sanitize_text_field( $first_name ) : $customer->get_first_name(), 'last_name' => null !== $last_name ? sanitize_text_field( $last_name ) : $customer->get_last_name(), 'email' => null !== $email ? sanitize_email( $email ) : $customer->get_email(), 'phone' => null !== $phone ? sanitize_text_field( $phone ) : $customer->get_phone(), 'image' => get_user_meta( $customer_id, '_timetics_customer_image', true ), ]; $request = $this->build_request( 'PUT', '/timetics/v1/customers/' . $customer_id, [], $body, [ 'customer_id' => $customer_id ] ); if ( is_array( $request ) ) { return $request; } $controller = \Timetics\Core\Customers\Api_Customer::instance(); $data = $this->extract_response( $controller->update_item( $request ), "Failed to update customer #{$customer_id}." ); if ( isset( $data['error'] ) ) { return $data; } return [ 'success' => true, 'customer_id' => $customer_id, 'customer' => $this->shape_customer( $data ), 'message' => "Customer #{$customer_id} updated successfully.", ]; } catch ( \Throwable $e ) { Log::error( 'Timetics_Update_Customer_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Failed to update customer: ' . $e->getMessage() ]; } } }