diff --git a/modules/ppcp-settings/services.php b/modules/ppcp-settings/services.php index ef9b0c55d..723eaad74 100644 --- a/modules/ppcp-settings/services.php +++ b/modules/ppcp-settings/services.php @@ -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 { diff --git a/modules/ppcp-settings/src/Data/SettingsModel.php b/modules/ppcp-settings/src/Data/SettingsModel.php index 099b7b6a6..a9f587a3a 100644 --- a/modules/ppcp-settings/src/Data/SettingsModel.php +++ b/modules/ppcp-settings/src/Data/SettingsModel.php @@ -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' => '', diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index a149b4913..e81c040fe 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -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 . '-' : ''; + }, ); diff --git a/modules/ppcp-wc-gateway/src/Settings/Fields/connection-tab-fields.php b/modules/ppcp-wc-gateway/src/Settings/Fields/connection-tab-fields.php index a897f78ba..24a1b1505 100644 --- a/modules/ppcp-wc-gateway/src/Settings/Fields/connection-tab-fields.php +++ b/modules/ppcp-wc-gateway/src/Settings/Fields/connection-tab-fields.php @@ -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,