Get invoice prefix service in new settings ui

This commit is contained in:
Emili Castells Guasch 2025-06-02 16:27:47 +02:00
parent 12df58b31c
commit 55199ed7d5
No known key found for this signature in database
4 changed files with 29 additions and 12 deletions

View file

@ -121,7 +121,8 @@ return array(
},
'settings.data.settings' => static function ( ContainerInterface $container ) : SettingsModel {
return new SettingsModel(
$container->get( 'settings.service.sanitizer' )
$container->get( 'settings.service.sanitizer' ),
$container->has( 'wcgateway.settings.invoice-prefix' ) ? $container->get( 'wcgateway.settings.invoice-prefix' ) : ''
);
},
'settings.data.paylater-messaging' => static function ( ContainerInterface $container ) : array {

View file

@ -47,14 +47,24 @@ class SettingsModel extends AbstractDataModel {
*/
protected DataSanitizer $sanitizer;
/**
* Invoice prefix.
*
* @var string
*/
private string $invoice_prefix;
/**
* Constructor.
*
* @param DataSanitizer $sanitizer Data sanitizer service.
* @param string $invoice_prefix Invoice prefix.
* @throws RuntimeException If the OPTION_KEY is not defined in the child class.
*/
public function __construct( DataSanitizer $sanitizer ) {
$this->sanitizer = $sanitizer;
public function __construct( DataSanitizer $sanitizer, string $invoice_prefix ) {
$this->sanitizer = $sanitizer;
$this->invoice_prefix = $invoice_prefix;
parent::__construct();
}
@ -66,7 +76,7 @@ class SettingsModel extends AbstractDataModel {
protected function get_defaults() : array {
return array(
// Free-form string values.
'invoice_prefix' => '',
'invoice_prefix' => $this->invoice_prefix,
'brand_name' => '',
'soft_descriptor' => '',

View file

@ -2093,4 +2093,16 @@ return array(
'wcgateway.settings.admin-settings-enabled' => static function( ContainerInterface $container ): bool {
return $container->has( 'settings.url' ) && ! SettingsModule::should_use_the_old_ui();
},
/**
* Returns a prefix for the site, ensuring the same site always gets the same prefix (unless the URL changes).
*/
'wcgateway.settings.invoice-prefix' => static function( ContainerInterface $container ): string {
$site_url = get_site_url( get_current_blog_id() );
$hash = md5( $site_url );
$letters = preg_replace( '~\d~', '', $hash ) ?? '';
$prefix = substr( $letters, 0, 6 );
return $prefix ? $prefix . '-' : '';
},
);

View file

@ -510,13 +510,7 @@ return function ( ContainerInterface $container, array $fields ): array {
'custom_attributes' => array(
'pattern' => '[a-zA-Z_\\-]+',
),
'default' => ( static function (): string {
$site_url = get_site_url( get_current_blog_id() );
$hash = md5( $site_url );
$letters = preg_replace( '~\d~', '', $hash ) ?? '';
$prefix = substr( $letters, 0, 6 );
return $prefix ? $prefix . '-' : '';
} )(),
'default' => $container->get( 'wcgateway.settings.invoice-prefix' ),
'screens' => array(
State::STATE_START,
State::STATE_ONBOARDED,
@ -539,7 +533,7 @@ return function ( ContainerInterface $container, array $fields ): array {
'requirements' => array(),
'gateway' => Settings::CONNECTION_TAB_ID,
),
'stay_updated' => array(
'stay_updated' => array(
'title' => __( 'Stay Updated', 'woocommerce-paypal-payments' ),
'type' => 'checkbox',
'desc_tip' => true,