Skip to content

Commit

Permalink
Saving payment terms and modes as transients
Browse files Browse the repository at this point in the history
This would speed things up and make us depend less on fetching the same uncached data from DK several times.
  • Loading branch information
aldavigdis committed Jun 26, 2024
1 parent b30586c commit 5f7b0b7
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/Import/SalesPayments.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,28 @@ public static function get_payment_term_name( string $key ): string {
* @return array<string>
*/
public static function get_payment_terms(): array {
if ( ! empty( Config::get_dk_api_key() ) ) {
$terms_transient = get_transient( '1984_woo_dk_payment_terms' );

if ( is_array( $terms_transient ) ) {
return $terms_transient;
}

if ( is_string( Config::get_dk_api_key() ) ) {
$terms = self::get_payment_terms_from_dk();
if ( is_array( $terms ) ) {
return array_column( $terms, 'CODE' );
$plucked_terms = array_column( $terms, 'CODE' );

set_transient(
'1984_woo_dk_payment_terms',
$plucked_terms,
self::TRANSIENT_EXPIRY
);

return $plucked_terms;
}
}

return self::DK_PAYMENT_TERMS;
return array();
}

/**
Expand Down Expand Up @@ -176,9 +190,23 @@ public static function get_payment_mode_name( string $key ): string {
* contents of DK_PAYMENT_MODES will be returned.
*/
public static function get_payment_modes(): array {
if ( ! empty( Config::get_dk_api_key() ) ) {
$modes = self::get_payment_modes_from_dk();
return array_column( $modes, 'CODE' );
$modes_transient = get_transient( '1984_woo_dk_payment_modes' );

if ( is_array( $modes_transient ) ) {
return $modes_transient;
}

if ( is_string( Config::get_dk_api_key() ) ) {
$modes = self::get_payment_modes_from_dk();
$plucked_modes = array_column( $modes, 'CODE' );

set_transient(
'1984_woo_dk_payment_modes',
$plucked_modes,
self::TRANSIENT_EXPIRY
);

return $plucked_modes;
}

return self::DK_PAYMENT_MODES;
Expand Down

0 comments on commit 5f7b0b7

Please sign in to comment.