/includes/class-wc-payment-gateways.php

  1. <?php 
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) { 
  4. exit; // Exit if accessed directly 
  5.  
  6. /** 
  7. * WooCommerce Payment Gateways class 
  8. * 
  9. * Loads payment gateways via hooks for use in the store. 
  10. * 
  11. * @class WC_Payment_Gateways 
  12. * @version 2.2.0 
  13. * @package WooCommerce/Classes/Payment 
  14. * @category Class 
  15. * @author WooThemes 
  16. */ 
  17. class WC_Payment_Gateways { 
  18.  
  19. /** @var array Array of payment gateway classes. */ 
  20. public $payment_gateways; 
  21.  
  22. /** 
  23. * @var WC_Payment_Gateways The single instance of the class 
  24. * @since 2.1 
  25. */ 
  26. protected static $_instance = null; 
  27.  
  28. /** 
  29. * Main WC_Payment_Gateways Instance. 
  30. * 
  31. * Ensures only one instance of WC_Payment_Gateways is loaded or can be loaded. 
  32. * 
  33. * @since 2.1 
  34. * @static 
  35. * @return WC_Payment_Gateways Main instance 
  36. */ 
  37. public static function instance() { 
  38. if ( is_null( self::$_instance ) ) { 
  39. self::$_instance = new self(); 
  40. return self::$_instance; 
  41.  
  42. /** 
  43. * Cloning is forbidden. 
  44. * 
  45. * @since 2.1 
  46. */ 
  47. public function __clone() { 
  48. wc_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' ); 
  49.  
  50. /** 
  51. * Unserializing instances of this class is forbidden. 
  52. * 
  53. * @since 2.1 
  54. */ 
  55. public function __wakeup() { 
  56. wc_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' ); 
  57.  
  58. /** 
  59. * Initialize payment gateways. 
  60. */ 
  61. public function __construct() { 
  62. $this->init(); 
  63.  
  64. /** 
  65. * Load gateways and hook in functions. 
  66. */ 
  67. public function init() { 
  68. $load_gateways = array( 
  69. 'WC_Gateway_BACS',  
  70. 'WC_Gateway_Cheque',  
  71. 'WC_Gateway_COD',  
  72. 'WC_Gateway_Paypal',  
  73. ); 
  74.  
  75. /** 
  76. * Simplify Commerce is @deprecated in 2.6.0. Only load when enabled. 
  77. */ 
  78. if ( ! class_exists( 'WC_Gateway_Simplify_Commerce_Loader' ) && in_array( WC()->countries->get_base_country(), apply_filters( 'woocommerce_gateway_simplify_commerce_supported_countries', array( 'US', 'IE' ) ) ) ) { 
  79. $simplify_options = get_option( 'woocommerce_simplify_commerce_settings', array() ); 
  80.  
  81. if ( ! empty( $simplify_options['enabled'] ) && 'yes' === $simplify_options['enabled'] ) { 
  82. if ( function_exists( 'wcs_create_renewal_order' ) ) { 
  83. $load_gateways[] = 'WC_Addons_Gateway_Simplify_Commerce'; 
  84. } else { 
  85. $load_gateways[] = 'WC_Gateway_Simplify_Commerce'; 
  86.  
  87. // Filter 
  88. $load_gateways = apply_filters( 'woocommerce_payment_gateways', $load_gateways ); 
  89.  
  90. // Get sort order option 
  91. $ordering = (array) get_option( 'woocommerce_gateway_order' ); 
  92. $order_end = 999; 
  93.  
  94. // Load gateways in order 
  95. foreach ( $load_gateways as $gateway ) { 
  96. $load_gateway = is_string( $gateway ) ? new $gateway() : $gateway; 
  97.  
  98. if ( isset( $ordering[ $load_gateway->id ] ) && is_numeric( $ordering[ $load_gateway->id ] ) ) { 
  99. // Add in position 
  100. $this->payment_gateways[ $ordering[ $load_gateway->id ] ] = $load_gateway; 
  101. } else { 
  102. // Add to end of the array 
  103. $this->payment_gateways[ $order_end ] = $load_gateway; 
  104. $order_end++; 
  105.  
  106. ksort( $this->payment_gateways ); 
  107.  
  108. /** 
  109. * Get gateways. 
  110. * @return array 
  111. */ 
  112. public function payment_gateways() { 
  113. $_available_gateways = array(); 
  114.  
  115. if ( sizeof( $this->payment_gateways ) > 0 ) { 
  116. foreach ( $this->payment_gateways as $gateway ) { 
  117. $_available_gateways[ $gateway->id ] = $gateway; 
  118.  
  119. return $_available_gateways; 
  120.  
  121. /** 
  122. * Get array of registered gateway ids 
  123. * @since 2.6.0 
  124. * @return array of strings 
  125. */ 
  126. public function get_payment_gateway_ids() { 
  127. return wp_list_pluck( $this->payment_gateways, 'id' ); 
  128.  
  129. /** 
  130. * Get available gateways. 
  131. * 
  132. * @return array 
  133. */ 
  134. public function get_available_payment_gateways() { 
  135. $_available_gateways = array(); 
  136.  
  137. foreach ( $this->payment_gateways as $gateway ) { 
  138. if ( $gateway->is_available() ) { 
  139. if ( ! is_add_payment_method_page() ) { 
  140. $_available_gateways[ $gateway->id ] = $gateway; 
  141. } elseif ( $gateway->supports( 'add_payment_method' ) ) { 
  142. $_available_gateways[ $gateway->id ] = $gateway; 
  143. } elseif ( $gateway->supports( 'tokenization' ) ) { 
  144. $_available_gateways[ $gateway->id ] = $gateway; 
  145.  
  146. return apply_filters( 'woocommerce_available_payment_gateways', $_available_gateways ); 
  147.  
  148. /** 
  149. * Set the current, active gateway. 
  150. * 
  151. * @param array $gateway Available payment gateways. 
  152. */ 
  153. public function set_current_gateway( $gateways ) { 
  154. // Be on the defensive 
  155. if ( ! is_array( $gateways ) || empty( $gateways ) ) { 
  156. return; 
  157.  
  158. if ( is_user_logged_in() ) { 
  159. $default_token = WC_Payment_Tokens::get_customer_default_token( get_current_user_id() ); 
  160. if ( ! is_null( $default_token ) ) { 
  161. $default_token_gateway = $default_token->get_gateway_id(); 
  162.  
  163. $current = ( isset( $default_token_gateway ) ? $default_token_gateway : WC()->session->get( 'chosen_payment_method' ) ); 
  164.  
  165. if ( $current && isset( $gateways[ $current ] ) ) { 
  166. $current_gateway = $gateways[ $current ]; 
  167.  
  168. } else { 
  169. $current_gateway = current( $gateways ); 
  170.  
  171. // Ensure we can make a call to set_current() without triggering an error 
  172. if ( $current_gateway && is_callable( array( $current_gateway, 'set_current' ) ) ) { 
  173. $current_gateway->set_current(); 
  174.  
  175. /** 
  176. * Save options in admin. 
  177. */ 
  178. public function process_admin_options() { 
  179. $gateway_order = isset( $_POST['gateway_order'] ) ? $_POST['gateway_order'] : ''; 
  180. $order = array(); 
  181.  
  182. if ( is_array( $gateway_order ) && sizeof( $gateway_order ) > 0 ) { 
  183. $loop = 0; 
  184. foreach ( $gateway_order as $gateway_id ) { 
  185. $order[ esc_attr( $gateway_id ) ] = $loop; 
  186. $loop++; 
  187.  
  188. update_option( 'woocommerce_gateway_order', $order ); 
.