暫無描述

class-wc-payment-gateways.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * WooCommerce Payment Gateways
  4. *
  5. * Loads payment gateways via hooks for use in the store.
  6. *
  7. * @version 2.2.0
  8. * @package WooCommerce\Classes\Payment
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * Payment gateways class.
  13. */
  14. class WC_Payment_Gateways {
  15. /**
  16. * Payment gateway classes.
  17. *
  18. * @var array
  19. */
  20. public $payment_gateways = array();
  21. /**
  22. * The single instance of the class.
  23. *
  24. * @var WC_Payment_Gateways
  25. * @since 2.1.0
  26. */
  27. protected static $_instance = null;
  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. * @return WC_Payment_Gateways Main instance
  35. */
  36. public static function instance() {
  37. if ( is_null( self::$_instance ) ) {
  38. self::$_instance = new self();
  39. }
  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__, __( 'Cloning is forbidden.', '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__, __( 'Unserializing instances of this class is forbidden.', '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. );
  73. if ( $this->should_load_paypal_standard() ) {
  74. $load_gateways[] = 'WC_Gateway_Paypal';
  75. }
  76. // Filter.
  77. $load_gateways = apply_filters( 'woocommerce_payment_gateways', $load_gateways );
  78. // Get sort order option.
  79. $ordering = (array) get_option( 'woocommerce_gateway_order' );
  80. $order_end = 999;
  81. // Load gateways in order.
  82. foreach ( $load_gateways as $gateway ) {
  83. if ( is_string( $gateway ) && class_exists( $gateway ) ) {
  84. $gateway = new $gateway();
  85. }
  86. // Gateways need to be valid and extend WC_Payment_Gateway.
  87. if ( ! is_a( $gateway, 'WC_Payment_Gateway' ) ) {
  88. continue;
  89. }
  90. if ( isset( $ordering[ $gateway->id ] ) && is_numeric( $ordering[ $gateway->id ] ) ) {
  91. // Add in position.
  92. $this->payment_gateways[ $ordering[ $gateway->id ] ] = $gateway;
  93. } else {
  94. // Add to end of the array.
  95. $this->payment_gateways[ $order_end ] = $gateway;
  96. $order_end++;
  97. }
  98. }
  99. ksort( $this->payment_gateways );
  100. }
  101. /**
  102. * Get gateways.
  103. *
  104. * @return array
  105. */
  106. public function payment_gateways() {
  107. $_available_gateways = array();
  108. if ( count( $this->payment_gateways ) > 0 ) {
  109. foreach ( $this->payment_gateways as $gateway ) {
  110. $_available_gateways[ $gateway->id ] = $gateway;
  111. }
  112. }
  113. return $_available_gateways;
  114. }
  115. /**
  116. * Get array of registered gateway ids
  117. *
  118. * @since 2.6.0
  119. * @return array of strings
  120. */
  121. public function get_payment_gateway_ids() {
  122. return wp_list_pluck( $this->payment_gateways, 'id' );
  123. }
  124. /**
  125. * Get available gateways.
  126. *
  127. * @return array
  128. */
  129. public function get_available_payment_gateways() {
  130. $_available_gateways = array();
  131. foreach ( $this->payment_gateways as $gateway ) {
  132. if ( $gateway->is_available() ) {
  133. if ( ! is_add_payment_method_page() ) {
  134. $_available_gateways[ $gateway->id ] = $gateway;
  135. } elseif ( $gateway->supports( 'add_payment_method' ) || $gateway->supports( 'tokenization' ) ) {
  136. $_available_gateways[ $gateway->id ] = $gateway;
  137. }
  138. }
  139. }
  140. return array_filter( (array) apply_filters( 'woocommerce_available_payment_gateways', $_available_gateways ), array( $this, 'filter_valid_gateway_class' ) );
  141. }
  142. /**
  143. * Callback for array filter. Returns true if gateway is of correct type.
  144. *
  145. * @since 3.6.0
  146. * @param object $gateway Gateway to check.
  147. * @return bool
  148. */
  149. protected function filter_valid_gateway_class( $gateway ) {
  150. return $gateway && is_a( $gateway, 'WC_Payment_Gateway' );
  151. }
  152. /**
  153. * Set the current, active gateway.
  154. *
  155. * @param array $gateways Available payment gateways.
  156. */
  157. public function set_current_gateway( $gateways ) {
  158. // Be on the defensive.
  159. if ( ! is_array( $gateways ) || empty( $gateways ) ) {
  160. return;
  161. }
  162. $current_gateway = false;
  163. if ( WC()->session ) {
  164. $current = WC()->session->get( 'chosen_payment_method' );
  165. if ( $current && isset( $gateways[ $current ] ) ) {
  166. $current_gateway = $gateways[ $current ];
  167. }
  168. }
  169. if ( ! $current_gateway ) {
  170. $current_gateway = current( $gateways );
  171. }
  172. // Ensure we can make a call to set_current() without triggering an error.
  173. if ( $current_gateway && is_callable( array( $current_gateway, 'set_current' ) ) ) {
  174. $current_gateway->set_current();
  175. }
  176. }
  177. /**
  178. * Save options in admin.
  179. */
  180. public function process_admin_options() {
  181. $gateway_order = isset( $_POST['gateway_order'] ) ? wc_clean( wp_unslash( $_POST['gateway_order'] ) ) : ''; // WPCS: input var ok, CSRF ok.
  182. $order = array();
  183. if ( is_array( $gateway_order ) && count( $gateway_order ) > 0 ) {
  184. $loop = 0;
  185. foreach ( $gateway_order as $gateway_id ) {
  186. $order[ esc_attr( $gateway_id ) ] = $loop;
  187. $loop++;
  188. }
  189. }
  190. update_option( 'woocommerce_gateway_order', $order );
  191. }
  192. /**
  193. * Determines if PayPal Standard should be loaded.
  194. *
  195. * @since 5.5.0
  196. * @return bool Whether PayPal Standard should be loaded or not.
  197. */
  198. protected function should_load_paypal_standard() {
  199. $paypal = new WC_Gateway_Paypal();
  200. return $paypal->should_load();
  201. }
  202. }