# makecommerce/3.0.11/payment/gateway/woocommerce/banklink.php

MakeCommerce for WooCommerce, version 3.0.11. 191 lines.

- Page: https://pluginprobe.com/plugins/makecommerce/3.0.11/code/payment/gateway/woocommerce/banklink.php
- Raw: https://pluginprobe.com/plugins/makecommerce/3.0.11/raw/payment/gateway/woocommerce/banklink.php
- Modified: 2021-11-10T14:39:54+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/makecommerce/3.0.11/code/payment/gateway/woocommerce/banklink.php#L10-L20`.

```php
<?php

namespace MakeCommerce\Payment\Gateway\WooCommerce;

trait Banklink {

    /**
     * Reloads banklinks
     * 
     * @since 3.0.0
     */
    public function mc_banklinks_reload( $force = false ) {

        if ( $force || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {

            $updated = $this->banklinks_update();

            if ( $force ) {
                $this->methods->load_methods();

                return $updated;
            }
            
            if ( $updated ) {
                wp_send_json( array( 'success' => 1, 'data' => __( 'Update successfully completed!', 'wc_makecommerce_domain' ) ) );

                exit;
            }
            
            wp_send_json( array( 'success' => 0, 'data' => __( 'There was an error with your update. Please try again.', 'wc_makecommerce_domain' ) ) );

            exit; 
        }

        die();
    }

    /**
     * Updates banklinks database and db structure if needed
     * 
     * @since 3.0.9
     */
    public function banklinks_update() {

        global $wp_version;
        global $wpdb;
        
        if ( !$this->MK ) {
            return false;
        }
        
        try {
            $shopConfig = $this->MK->getShopConfig( \MakeCommerce::config_request_parameters( $this->id.' '.$this->version ) );
            $methods = $shopConfig->paymentMethods;	
        } catch ( \Exception $e ) {
            error_log( print_r( $e, 1 ) );
            return false;
        }

        $tableName = $wpdb->prefix . MAKECOMMERCE_TABLENAME;
        $sql = "SELECT COUNT(*) AS `isLogoUrlSet` FROM `information_schema`.`columns` WHERE `table_name`='".$tableName."' AND `column_name`='logo_url'";
        $methodstruct = $wpdb->get_results( $sql );
        if ( $methodstruct[0]->isLogoUrlSet == 0 ) {
            $wpdb->query( 'ALTER TABLE '.$tableName.' ADD COLUMN `logo_url` varchar(250)' );
        }

        $sql = "SELECT COUNT(*) AS `min_amount` FROM `information_schema`.`columns` WHERE `table_name`='".$tableName."' AND `column_name`='min_amount'";
        $methodstruct = $wpdb->get_results( $sql );
        if ( $methodstruct[0]->min_amount == 0 ) {
            $wpdb->query( 'ALTER TABLE '.$tableName.' ADD COLUMN `min_amount` mediumint(9) default 0' );
        }

        $sql = "SELECT COUNT(*) AS `max_amount` FROM `information_schema`.`columns` WHERE `table_name`='".$tableName."' AND `column_name`='max_amount'";
        $methodstruct = $wpdb->get_results( $sql );
        if ( $methodstruct[0]->max_amount == 0 ) {
            $wpdb->query( 'ALTER TABLE '.$tableName.' ADD COLUMN `max_amount` mediumint(9) default 0' );
        }

        $sql = "SELECT COUNT(*) AS `display_name` FROM `information_schema`.`columns` WHERE `table_name`='".$tableName."' AND `column_name`='display_name'";
        $methodstruct = $wpdb->get_results( $sql );
        if ( $methodstruct[0]->display_name == 0 ) {
            $wpdb->query( 'ALTER TABLE '.$tableName.' ADD COLUMN `display_name` varchar(250)' );
        }

        $sql = "SELECT COUNT(*) AS `channel` FROM `information_schema`.`columns` WHERE `table_name`='".$tableName."' AND `column_name`='channel'";
        $methodstruct = $wpdb->get_results( $sql );
        if ( $methodstruct[0]->channel == 0 ) {
            $wpdb->query( 'ALTER TABLE '.$tableName.' ADD COLUMN `channel` varchar(250)' );
        }

        $wpdb->query( 'TRUNCATE TABLE `'.$tableName.'`' );
        
        if ( isset( $methods->banklinks ) ) {
            foreach( $methods->banklinks as $method ) {
                $wpdb->insert( $tableName, array( 'type' => 'banklink', 'country' => $method->country, 'name' => $method->name, 'url' => $method->url, 'logo_url' => $method->logo_url, 'channel' => $method->channel, 'display_name' => $method->display_name ) );
            }

            $updated = true;
        }
        
        if ( isset( $methods->cards ) ) {
            foreach( $methods->cards as $method ) {
                $wpdb->insert( $tableName, array( 'type' => 'card', 'name' => $method->name, 'logo_url' => $method->logo_url, 'channel' => $method->channel, 'display_name' => $method->display_name ) );
            }

            $updated = true;
        }

        if ( isset( $methods->other ) ) {
            foreach( $methods->other as $method ) {
                $wpdb->insert( $tableName, array( 'type' => 'other', 'country' => $method->country, 'name' => $method->name, 'url' => $method->url, 'logo_url' => $method->logo_url, 'channel' => $method->channel, 'display_name' => $method->display_name ) );
            }

            $updated = true;
        }

        if ( isset( $methods->payLater ) ) {
            foreach( $methods->payLater as $method ) {
                //not all payLater accounts have a max_amount parameter. Set them to NULL to avoid notice's
                if ( !isset( $method->max_amount ) ) {
                    $method->max_amount = NULL;
                }

                $wpdb->insert( $tableName, array( 'type' => 'payLater', 'country' => $method->country, 'name' => $method->name, 'url' => $method->url, 'logo_url' => $method->logo_url, 'min_amount' => $method->min_amount, 'max_amount' => $method->max_amount, 'channel' => $method->channel, 'display_name' => $method->display_name ) );
            }

            $updated = true;
        }

        if ( isset( $shopConfig ) && strlen( $shopConfig->name ) ) {
            update_option( 'mc_shop_name', $shopConfig->name );
        }

        if ( $updated ) {
            update_option( 'mc_banklinks_api_type', get_option( 'mk_api_type', false) );
        }

        return $updated;
    }

    /**
     * Creates banklinks reload button
     * 
     * @since 3.0.0
     */
    public function generate_mc_banklinks_reload_html( $key, $data ) {

        $field    = $this->get_field_key( $key );
        $defaults = array(
            'title'             => '',
            'disabled'          => false,
            'class'             => '',
            'css'               => '',
            'placeholder'       => '',
            'type'              => 'text',
            'desc_tip'          => false,
            'description'       => '',
            'custom_attributes' => array()
        );
    
        $data = wp_parse_args( $data, $defaults );
        
        \MakeCommerce::mc_enqueue_script( 
            'MC_BANKLINKS_RELOAD', 
            dirname( __FILE__ ) . '/js/mc_banklinks_reload.js', 
            [
                'site_url' => get_site_url(),
                'error' => __( 'There was an error with your update. Please try again.', 'wc_makecommerce_domain' )
            ], 
            [ 'jquery' ]
        );

        ob_start();
        ?>
        <tr valign="top">
            <th scope="row" class="titledesc">
                <label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
                <?php echo $this->get_tooltip_html( $data ); ?>
            </th>
            <td class="forminp">
                <fieldset>
                    <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
                    <input id="mc_banklinks_reload" class="button <?php echo esc_attr( $data['class'] ); ?>" type="button" name="<?php echo esc_attr( $field ); ?>" id="<?php echo esc_attr( $field ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( $data['description'] ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> />
                </fieldset>
            </td>
        </tr>
        <?php
    
        return ob_get_clean();
    }
}
```
