From 29cebffb4f99cc50961bd10f644fad87c3fc96d5 Mon Sep 17 00:00:00 2001 From: Shahbaz Date: Tue, 6 Feb 2024 23:45:52 +0500 Subject: [PATCH] Move duplicate code to helper functions --- altapay.php | 45 ++++++++++++++++---------------------- helpers/AltapayHelpers.php | 18 +++++++++++++++ 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/altapay.php b/altapay.php index 1f7d7348..8d8cad5f 100755 --- a/altapay.php +++ b/altapay.php @@ -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/'; @@ -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'; diff --git a/helpers/AltapayHelpers.php b/helpers/AltapayHelpers.php index 4fce8398..a13fb068 100755 --- a/helpers/AltapayHelpers.php +++ b/helpers/AltapayHelpers.php @@ -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' ); + } + } + } }