| 1 |
<?php |
| 2 |
|
| 3 |
namespace MakeCommerce\Payment\Gateway\WooCommerce; |
| 4 |
|
| 5 |
trait Banklink { |
| 6 |
|
| 7 |
/** |
| 8 |
* Reloads banklinks |
| 9 |
* |
| 10 |
* @since 3.0.0 |
| 11 |
*/ |
| 12 |
public function mc_banklinks_reload( $force = false ) { |
| 13 |
|
| 14 |
if ( $force || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
| 15 |
|
| 16 |
$updated = $this->banklinks_update(); |
| 17 |
|
| 18 |
if ( $force ) { |
| 19 |
$this->methods->load_methods(); |
| 20 |
|
| 21 |
return $updated; |
| 22 |
} |
| 23 |
|
| 24 |
if ( $updated ) { |
| 25 |
wp_send_json( array( 'success' => 1, 'data' => __( 'Update successfully completed!', 'wc_makecommerce_domain' ) ) ); |
| 26 |
|
| 27 |
exit; |
| 28 |
} |
| 29 |
|
| 30 |
wp_send_json( array( 'success' => 0, 'data' => __( 'There was an error with your update. Please try again.', 'wc_makecommerce_domain' ) ) ); |
| 31 |
|
| 32 |
exit; |
| 33 |
} |
| 34 |
|
| 35 |
die(); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Updates banklinks database and db structure if needed |
| 40 |
* |
| 41 |
* @since 3.0.9 |
| 42 |
*/ |
| 43 |
public function banklinks_update() { |
| 44 |
|
| 45 |
global $wp_version; |
| 46 |
global $wpdb; |
| 47 |
|
| 48 |
if ( !$this->MK ) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
try { |
| 53 |
$shopConfig = $this->MK->getShopConfig( \MakeCommerce::config_request_parameters( $this->id.' '.$this->version ) ); |
| 54 |
$methods = $shopConfig->paymentMethods; |
| 55 |
} catch ( \Exception $e ) { |
| 56 |
error_log( print_r( $e, 1 ) ); |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
$tableName = $wpdb->prefix . MAKECOMMERCE_TABLENAME; |
| 61 |
$sql = "SELECT COUNT(*) AS `isLogoUrlSet` FROM `information_schema`.`columns` WHERE `table_name`='".$tableName."' AND `column_name`='logo_url'"; |
| 62 |
$methodstruct = $wpdb->get_results( $sql ); |
| 63 |
if ( $methodstruct[0]->isLogoUrlSet == 0 ) { |
| 64 |
$wpdb->query( 'ALTER TABLE '.$tableName.' ADD COLUMN `logo_url` varchar(250)' ); |
| 65 |
} |
| 66 |
|
| 67 |
$sql = "SELECT COUNT(*) AS `min_amount` FROM `information_schema`.`columns` WHERE `table_name`='".$tableName."' AND `column_name`='min_amount'"; |
| 68 |
$methodstruct = $wpdb->get_results( $sql ); |
| 69 |
if ( $methodstruct[0]->min_amount == 0 ) { |
| 70 |
$wpdb->query( 'ALTER TABLE '.$tableName.' ADD COLUMN `min_amount` mediumint(9) default 0' ); |
| 71 |
} |
| 72 |
|
| 73 |
$sql = "SELECT COUNT(*) AS `max_amount` FROM `information_schema`.`columns` WHERE `table_name`='".$tableName."' AND `column_name`='max_amount'"; |
| 74 |
$methodstruct = $wpdb->get_results( $sql ); |
| 75 |
if ( $methodstruct[0]->max_amount == 0 ) { |
| 76 |
$wpdb->query( 'ALTER TABLE '.$tableName.' ADD COLUMN `max_amount` mediumint(9) default 0' ); |
| 77 |
} |
| 78 |
|
| 79 |
$sql = "SELECT COUNT(*) AS `display_name` FROM `information_schema`.`columns` WHERE `table_name`='".$tableName."' AND `column_name`='display_name'"; |
| 80 |
$methodstruct = $wpdb->get_results( $sql ); |
| 81 |
if ( $methodstruct[0]->display_name == 0 ) { |
| 82 |
$wpdb->query( 'ALTER TABLE '.$tableName.' ADD COLUMN `display_name` varchar(250)' ); |
| 83 |
} |
| 84 |
|
| 85 |
$sql = "SELECT COUNT(*) AS `channel` FROM `information_schema`.`columns` WHERE `table_name`='".$tableName."' AND `column_name`='channel'"; |
| 86 |
$methodstruct = $wpdb->get_results( $sql ); |
| 87 |
if ( $methodstruct[0]->channel == 0 ) { |
| 88 |
$wpdb->query( 'ALTER TABLE '.$tableName.' ADD COLUMN `channel` varchar(250)' ); |
| 89 |
} |
| 90 |
|
| 91 |
$wpdb->query( 'TRUNCATE TABLE `'.$tableName.'`' ); |
| 92 |
|
| 93 |
if ( isset( $methods->banklinks ) ) { |
| 94 |
foreach( $methods->banklinks as $method ) { |
| 95 |
$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 ) ); |
| 96 |
} |
| 97 |
|
| 98 |
$updated = true; |
| 99 |
} |
| 100 |
|
| 101 |
if ( isset( $methods->cards ) ) { |
| 102 |
foreach( $methods->cards as $method ) { |
| 103 |
$wpdb->insert( $tableName, array( 'type' => 'card', 'name' => $method->name, 'logo_url' => $method->logo_url, 'channel' => $method->channel, 'display_name' => $method->display_name ) ); |
| 104 |
} |
| 105 |
|
| 106 |
$updated = true; |
| 107 |
} |
| 108 |
|
| 109 |
if ( isset( $methods->other ) ) { |
| 110 |
foreach( $methods->other as $method ) { |
| 111 |
$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 ) ); |
| 112 |
} |
| 113 |
|
| 114 |
$updated = true; |
| 115 |
} |
| 116 |
|
| 117 |
if ( isset( $methods->payLater ) ) { |
| 118 |
foreach( $methods->payLater as $method ) { |
| 119 |
//not all payLater accounts have a max_amount parameter. Set them to NULL to avoid notice's |
| 120 |
if ( !isset( $method->max_amount ) ) { |
| 121 |
$method->max_amount = NULL; |
| 122 |
} |
| 123 |
|
| 124 |
$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 ) ); |
| 125 |
} |
| 126 |
|
| 127 |
$updated = true; |
| 128 |
} |
| 129 |
|
| 130 |
if ( isset( $shopConfig ) && strlen( $shopConfig->name ) ) { |
| 131 |
update_option( 'mc_shop_name', $shopConfig->name ); |
| 132 |
} |
| 133 |
|
| 134 |
if ( $updated ) { |
| 135 |
update_option( 'mc_banklinks_api_type', get_option( 'mk_api_type', false) ); |
| 136 |
} |
| 137 |
|
| 138 |
return $updated; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Creates banklinks reload button |
| 143 |
* |
| 144 |
* @since 3.0.0 |
| 145 |
*/ |
| 146 |
public function generate_mc_banklinks_reload_html( $key, $data ) { |
| 147 |
|
| 148 |
$field = $this->get_field_key( $key ); |
| 149 |
$defaults = array( |
| 150 |
'title' => '', |
| 151 |
'disabled' => false, |
| 152 |
'class' => '', |
| 153 |
'css' => '', |
| 154 |
'placeholder' => '', |
| 155 |
'type' => 'text', |
| 156 |
'desc_tip' => false, |
| 157 |
'description' => '', |
| 158 |
'custom_attributes' => array() |
| 159 |
); |
| 160 |
|
| 161 |
$data = wp_parse_args( $data, $defaults ); |
| 162 |
|
| 163 |
\MakeCommerce::mc_enqueue_script( |
| 164 |
'MC_BANKLINKS_RELOAD', |
| 165 |
dirname( __FILE__ ) . '/js/mc_banklinks_reload.js', |
| 166 |
[ |
| 167 |
'site_url' => get_site_url(), |
| 168 |
'error' => __( 'There was an error with your update. Please try again.', 'wc_makecommerce_domain' ) |
| 169 |
], |
| 170 |
[ 'jquery' ] |
| 171 |
); |
| 172 |
|
| 173 |
ob_start(); |
| 174 |
?> |
| 175 |
<tr valign="top"> |
| 176 |
<th scope="row" class="titledesc"> |
| 177 |
<label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> |
| 178 |
<?php echo $this->get_tooltip_html( $data ); ?> |
| 179 |
</th> |
| 180 |
<td class="forminp"> |
| 181 |
<fieldset> |
| 182 |
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> |
| 183 |
<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 ); ?> /> |
| 184 |
</fieldset> |
| 185 |
</td> |
| 186 |
</tr> |
| 187 |
<?php |
| 188 |
|
| 189 |
return ob_get_clean(); |
| 190 |
} |
| 191 |
} |