class-wc-settings-payment-gateways.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php // @codingStandardsIgnoreLine.
  2. /**
  3. * WooCommerce Checkout Settings
  4. *
  5. * @package WooCommerce/Admin
  6. */
  7. defined( 'ABSPATH' ) || exit;
  8. if ( class_exists( 'WC_Settings_Payment_Gateways', false ) ) {
  9. return new WC_Settings_Payment_Gateways();
  10. }
  11. /**
  12. * WC_Settings_Payment_Gateways.
  13. */
  14. class WC_Settings_Payment_Gateways extends WC_Settings_Page {
  15. /**
  16. * Constructor.
  17. */
  18. public function __construct() {
  19. $this->id = 'checkout'; // @todo In future versions this may make more sense as 'payment' however to avoid breakage lets leave this alone until we refactor settings APIs in general.
  20. $this->label = _x( 'Payments', 'Settings tab label', 'woocommerce' );
  21. add_action( 'woocommerce_admin_field_payment_gateways', array( $this, 'payment_gateways_setting' ) );
  22. parent::__construct();
  23. }
  24. /**
  25. * Get sections.
  26. *
  27. * @return array
  28. */
  29. public function get_sections() {
  30. $sections = array(
  31. '' => __( 'Payment methods', 'woocommerce' ),
  32. );
  33. return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections );
  34. }
  35. /**
  36. * Get settings array.
  37. *
  38. * @param string $current_section Section being shown.
  39. * @return array
  40. */
  41. public function get_settings( $current_section = '' ) {
  42. $settings = array();
  43. if ( '' === $current_section ) {
  44. $settings = apply_filters(
  45. 'woocommerce_payment_gateways_settings', array(
  46. array(
  47. 'title' => __( 'Payment methods', 'woocommerce' ),
  48. 'desc' => __( 'Installed payment methods are listed below. Drag and drop gateways to control their display order on the frontend.', 'woocommerce' ),
  49. 'type' => 'title',
  50. 'id' => 'payment_gateways_options',
  51. ),
  52. array(
  53. 'type' => 'payment_gateways',
  54. ),
  55. array(
  56. 'type' => 'sectionend',
  57. 'id' => 'payment_gateways_options',
  58. ),
  59. )
  60. );
  61. }
  62. return apply_filters( 'woocommerce_get_settings_' . $this->id, $settings, $current_section );
  63. }
  64. /**
  65. * Output the settings.
  66. */
  67. public function output() {
  68. global $current_section;
  69. // Load gateways so we can show any global options they may have.
  70. $payment_gateways = WC()->payment_gateways->payment_gateways();
  71. if ( $current_section ) {
  72. foreach ( $payment_gateways as $gateway ) {
  73. if ( in_array( $current_section, array( $gateway->id, sanitize_title( get_class( $gateway ) ) ), true ) ) {
  74. if ( isset( $_GET['toggle_enabled'] ) ) { // WPCS: input var ok, CSRF ok.
  75. $enabled = $gateway->get_option( 'enabled' );
  76. if ( $enabled ) {
  77. $gateway->settings['enabled'] = wc_string_to_bool( $enabled ) ? 'no' : 'yes';
  78. }
  79. }
  80. $gateway->admin_options();
  81. break;
  82. }
  83. }
  84. } else {
  85. $settings = $this->get_settings();
  86. WC_Admin_Settings::output_fields( $settings );
  87. }
  88. }
  89. /**
  90. * Output payment gateway settings.
  91. */
  92. public function payment_gateways_setting() {
  93. ?>
  94. <tr valign="top">
  95. <td class="wc_payment_gateways_wrapper" colspan="2">
  96. <table class="wc_gateways widefat" cellspacing="0">
  97. <thead>
  98. <tr>
  99. <?php
  100. $default_columns = array(
  101. 'sort' => '',
  102. 'name' => __( 'Method', 'woocommerce' ),
  103. 'status' => __( 'Enabled', 'woocommerce' ),
  104. 'description' => __( 'Description', 'woocommerce' ),
  105. 'action' => '',
  106. );
  107. $columns = apply_filters( 'woocommerce_payment_gateways_setting_columns', $default_columns );
  108. foreach ( $columns as $key => $column ) {
  109. echo '<th class="' . esc_attr( $key ) . '">' . esc_html( $column ) . '</th>';
  110. }
  111. ?>
  112. </tr>
  113. </thead>
  114. <tbody>
  115. <?php
  116. foreach ( WC()->payment_gateways->payment_gateways() as $gateway ) {
  117. echo '<tr data-gateway_id="' . esc_attr( $gateway->id ) . '">';
  118. foreach ( $columns as $key => $column ) {
  119. if ( ! array_key_exists( $key, $default_columns ) ) {
  120. do_action( 'woocommerce_payment_gateways_setting_column_' . $key, $gateway );
  121. continue;
  122. }
  123. $width = '';
  124. if ( in_array( $key, array( 'sort', 'status', 'action' ), true ) ) {
  125. $width = '1%';
  126. }
  127. echo '<td class="' . esc_attr( $key ) . '" width="' . esc_attr( $width ) . '">';
  128. switch ( $key ) {
  129. case 'sort':
  130. echo '<input type="hidden" name="gateway_order[]" value="' . esc_attr( $gateway->id ) . '" />';
  131. break;
  132. case 'name':
  133. $method_title = $gateway->get_title() ? $gateway->get_title() : __( '(no title)', 'woocommerce' );
  134. echo '<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout&section=' . strtolower( $gateway->id ) ) ) . '" class="wc-payment-gateway-method-title">' . wp_kses_post( $gateway->get_method_title() ) . '</a>';
  135. if ( $method_title !== $gateway->get_method_title() ) {
  136. echo '<span class="wc-payment-gateway-method-name">&nbsp;&ndash;&nbsp;' . esc_html( $method_title ) . '</span>';
  137. }
  138. break;
  139. case 'description':
  140. echo wp_kses_post( $gateway->get_method_description() );
  141. break;
  142. case 'action':
  143. if ( wc_string_to_bool( $gateway->enabled ) ) {
  144. echo '<a class="button alignright" href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout&section=' . strtolower( $gateway->id ) ) ) . '">' . esc_html__( 'Manage', 'woocommerce' ) . '</a>';
  145. } else {
  146. echo '<a class="button alignright" href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout&section=' . strtolower( $gateway->id ) ) ) . '">' . esc_html__( 'Set up', 'woocommerce' ) . '</a>';
  147. }
  148. break;
  149. case 'status':
  150. echo '<a class="wc-payment-gateway-method-toggle-enabled" href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout&section=' . strtolower( $gateway->id ) ) ) . '">';
  151. if ( wc_string_to_bool( $gateway->enabled ) ) {
  152. echo '<span class="woocommerce-input-toggle woocommerce-input-toggle--enabled">' . esc_attr__( 'Yes', 'woocommerce' ) . '</span>';
  153. } else {
  154. echo '<span class="woocommerce-input-toggle woocommerce-input-toggle--disabled">' . esc_attr__( 'No', 'woocommerce' ) . '</span>';
  155. }
  156. echo '</a>';
  157. break;
  158. }
  159. echo '</td>';
  160. }
  161. echo '</tr>';
  162. }
  163. ?>
  164. </tbody>
  165. </table>
  166. </td>
  167. </tr>
  168. <?php
  169. }
  170. /**
  171. * Save settings.
  172. */
  173. public function save() {
  174. global $current_section;
  175. $wc_payment_gateways = WC_Payment_Gateways::instance();
  176. if ( ! $current_section ) {
  177. WC_Admin_Settings::save_fields( $this->get_settings() );
  178. $wc_payment_gateways->process_admin_options();
  179. $wc_payment_gateways->init();
  180. } else {
  181. foreach ( $wc_payment_gateways->payment_gateways() as $gateway ) {
  182. if ( in_array( $current_section, array( $gateway->id, sanitize_title( get_class( $gateway ) ) ), true ) ) {
  183. do_action( 'woocommerce_update_options_payment_gateways_' . $gateway->id );
  184. $wc_payment_gateways->init();
  185. }
  186. }
  187. }
  188. if ( $current_section ) {
  189. do_action( 'woocommerce_update_options_' . $this->id . '_' . $current_section );
  190. }
  191. }
  192. }
  193. return new WC_Settings_Payment_Gateways();