Skip to content

Commit

Permalink
Move duplicate code to helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
shahbaz-mehar committed Feb 6, 2024
1 parent 41f4969 commit 29cebff
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
45 changes: 19 additions & 26 deletions altapay.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ function altapay_add_gateway( $methods ) {
return $methods;
}

$helper = new Helpers\AltapayHelpers();

$pluginDir = plugin_dir_path( __FILE__ );
// Directory for the terminals
$terminalDir = $pluginDir . 'terminals/';
Expand Down Expand Up @@ -131,34 +133,25 @@ function altapay_add_gateway( $methods ) {
}

$terminal_file_blocks = $terminalDir . $terminal . '.blocks.class.php';

if ( ! file_exists( $terminal_file_blocks ) ) {
// Create file
$template = file_get_contents( $pluginDir . 'views/paymentClassBlocks.tpl' );
// Replace patterns
$content = str_replace( array( '{key}', '{terminal_id}' ), array( $terminal, strtolower( $terminal ) ), $template );

$ok = @\file_put_contents( $terminal_file_blocks, $content );

if ( $ok === \false ) {
set_transient( 'terminals_directory_error', 'show' );
}
}
$helper->create_file_from_tpl(
$terminal_file_blocks,
$pluginDir . 'views/paymentClassBlocks.tpl',
array(
'{key}' => $terminal,
'{terminal_id}' => strtolower( $terminal ),
)
);

$terminal_js_file_blocks = $terminalDir . strtolower( $terminal ) . '.blocks.js';

if ( ! file_exists( $terminal_js_file_blocks ) ) {
// Create file
$template = file_get_contents( $pluginDir . 'views/blocksJs.tpl' );
// Replace patterns
$content = str_replace( array( '{key}', '{name}', '{terminal_id}' ), array( $terminal, $terminalName, strtolower( $terminal ) ), $template );

$ok = @\file_put_contents( $terminal_js_file_blocks, $content );

if ( $ok === \false ) {
set_transient( 'terminals_directory_error', 'show' );
}
}
$helper->create_file_from_tpl(
$terminal_js_file_blocks,
$pluginDir . 'views/blocksJs.tpl',
array(
'{key}' => $terminal,
'{name}' => $terminalName,
'{terminal_id}' => strtolower( $terminal ),
)
);

// Check if file exists
$terminal_class_file = $terminalDir . $terminal . '.class.php';
Expand Down
18 changes: 18 additions & 0 deletions helpers/AltapayHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,22 @@ public function calculateChecksum( $input_data, $shared_secret ) {

return md5( join( ',', $data ) );
}

/**
* @return void
*/
public function create_file_from_tpl( $file_path, $template_path, $placeholders ) {
if ( ! file_exists( $file_path ) ) {
// Create file
$template = file_get_contents( $template_path );
// Replace patterns
$content = str_replace( array_keys( $placeholders ), array_values( $placeholders ), $template );

$ok = @\file_put_contents( $file_path, $content );

if ( $ok === \false ) {
set_transient( 'terminals_directory_error', 'show' );
}
}
}
}

0 comments on commit 29cebff

Please sign in to comment.