Aucune description

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 own sections.
  26. *
  27. * @return array
  28. */
  29. protected function get_own_sections() {
  30. return array(
  31. '' => __( 'Payment methods', 'woocommerce' ),
  32. );
  33. }
  34. /**
  35. * Get settings array.
  36. *
  37. * @return array
  38. */
  39. protected function get_settings_for_default_section() {
  40. $settings =
  41. array(
  42. array(
  43. 'title' => __( 'Payment methods', 'woocommerce' ),
  44. 'desc' => __( 'Installed payment methods are listed below and can be sorted to control their display order on the frontend.', 'woocommerce' ),
  45. 'type' => 'title',
  46. 'id' => 'payment_gateways_options',
  47. ),
  48. array(
  49. 'type' => 'payment_gateways',
  50. ),
  51. array(
  52. 'type' => 'sectionend',
  53. 'id' => 'payment_gateways_options',
  54. ),
  55. );
  56. return apply_filters( 'woocommerce_payment_gateways_settings', $settings );
  57. }
  58. /**
  59. * Output the settings.
  60. */
  61. public function output() {
  62. //phpcs:disable WordPress.Security.NonceVerification.Recommended
  63. global $current_section;
  64. // Load gateways so we can show any global options they may have.
  65. $payment_gateways = WC()->payment_gateways->payment_gateways();
  66. if ( $current_section ) {
  67. foreach ( $payment_gateways as $gateway ) {
  68. if ( in_array( $current_section, array( $gateway->id, sanitize_title( get_class( $gateway ) ) ), true ) ) {
  69. if ( isset( $_GET['toggle_enabled'] ) ) {
  70. $enabled = $gateway->get_option( 'enabled' );
  71. if ( $enabled ) {
  72. $gateway->settings['enabled'] = wc_string_to_bool( $enabled ) ? 'no' : 'yes';
  73. }
  74. }
  75. $this->run_gateway_admin_options( $gateway );
  76. break;
  77. }
  78. }
  79. }
  80. parent::output();
  81. //phpcs:enable
  82. }
  83. /**
  84. * Run the 'admin_options' method on a given gateway.
  85. * This method exists to easy unit testing.
  86. *
  87. * @param object $gateway The gateway object to run the method on.
  88. */
  89. protected function run_gateway_admin_options( $gateway ) {
  90. $gateway->admin_options();
  91. }
  92. /**
  93. * Output payment gateway settings.
  94. */
  95. public function payment_gateways_setting() {
  96. ?>
  97. <tr valign="top">
  98. <td class="wc_payment_gateways_wrapper" colspan="2">
  99. <table class="wc_gateways widefat" cellspacing="0" aria-describedby="payment_gateways_options-description">
  100. <thead>
  101. <tr>
  102. <?php
  103. $default_columns = array(
  104. 'sort' => '',
  105. 'name' => __( 'Method', 'woocommerce' ),
  106. 'status' => __( 'Enabled', 'woocommerce' ),
  107. 'description' => __( 'Description', 'woocommerce' ),
  108. 'action' => '',
  109. );
  110. $columns = apply_filters( 'woocommerce_payment_gateways_setting_columns', $default_columns );
  111. foreach ( $columns as $key => $column ) {
  112. echo '<th class="' . esc_attr( $key ) . '">' . esc_html( $column ) . '</th>';
  113. }
  114. ?>
  115. </tr>
  116. </thead>
  117. <tbody>
  118. <?php
  119. foreach ( WC()->payment_gateways->payment_gateways() as $gateway ) {
  120. echo '<tr data-gateway_id="' . esc_attr( $gateway->id ) . '">';
  121. foreach ( $columns as $key => $column ) {
  122. if ( ! array_key_exists( $key, $default_columns ) ) {
  123. do_action( 'woocommerce_payment_gateways_setting_column_' . $key, $gateway );
  124. continue;
  125. }
  126. $width = '';
  127. if ( in_array( $key, array( 'sort', 'status', 'action' ), true ) ) {
  128. $width = '1%';
  129. }
  130. $method_title = $gateway->get_method_title() ? $gateway->get_method_title() : $gateway->get_title();
  131. $custom_title = $gateway->get_title();
  132. echo '<td class="' . esc_attr( $key ) . '" width="' . esc_attr( $width ) . '">';
  133. switch ( $key ) {
  134. case 'sort':
  135. ?>
  136. <div class="wc-item-reorder-nav">
  137. <button type="button" class="wc-move-up" tabindex="0" aria-hidden="false" aria-label="<?php /* Translators: %s Payment gateway name. */ echo esc_attr( sprintf( __( 'Move the "%s" payment method up', 'woocommerce' ), $method_title ) ); ?>"><?php esc_html_e( 'Move up', 'woocommerce' ); ?></button>
  138. <button type="button" class="wc-move-down" tabindex="0" aria-hidden="false" aria-label="<?php /* Translators: %s Payment gateway name. */ echo esc_attr( sprintf( __( 'Move the "%s" payment method down', 'woocommerce' ), $method_title ) ); ?>"><?php esc_html_e( 'Move down', 'woocommerce' ); ?></button>
  139. <input type="hidden" name="gateway_order[]" value="<?php echo esc_attr( $gateway->id ); ?>" />
  140. </div>
  141. <?php
  142. break;
  143. case 'name':
  144. 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( $method_title ) . '</a>';
  145. if ( $method_title !== $custom_title ) {
  146. echo '<span class="wc-payment-gateway-method-name">&nbsp;&ndash;&nbsp;' . wp_kses_post( $custom_title ) . '</span>';
  147. }
  148. break;
  149. case 'description':
  150. echo wp_kses_post( $gateway->get_method_description() );
  151. break;
  152. case 'action':
  153. if ( wc_string_to_bool( $gateway->enabled ) ) {
  154. /* Translators: %s Payment gateway name. */
  155. echo '<a class="button alignright" aria-label="' . esc_attr( sprintf( __( 'Manage the "%s" payment method', 'woocommerce' ), $method_title ) ) . '" href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout&section=' . strtolower( $gateway->id ) ) ) . '">' . esc_html__( 'Manage', 'woocommerce' ) . '</a>';
  156. } else {
  157. /* Translators: %s Payment gateway name. */
  158. echo '<a class="button alignright" aria-label="' . esc_attr( sprintf( __( 'Set up the "%s" payment method', 'woocommerce' ), $method_title ) ) . '" href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout&section=' . strtolower( $gateway->id ) ) ) . '">' . esc_html__( 'Set up', 'woocommerce' ) . '</a>';
  159. }
  160. break;
  161. case 'status':
  162. 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 ) ) ) . '">';
  163. if ( wc_string_to_bool( $gateway->enabled ) ) {
  164. /* Translators: %s Payment gateway name. */
  165. echo '<span class="woocommerce-input-toggle woocommerce-input-toggle--enabled" aria-label="' . esc_attr( sprintf( __( 'The "%s" payment method is currently enabled', 'woocommerce' ), $method_title ) ) . '">' . esc_attr__( 'Yes', 'woocommerce' ) . '</span>';
  166. } else {
  167. /* Translators: %s Payment gateway name. */
  168. echo '<span class="woocommerce-input-toggle woocommerce-input-toggle--disabled" aria-label="' . esc_attr( sprintf( __( 'The "%s" payment method is currently disabled', 'woocommerce' ), $method_title ) ) . '">' . esc_attr__( 'No', 'woocommerce' ) . '</span>';
  169. }
  170. echo '</a>';
  171. break;
  172. }
  173. echo '</td>';
  174. }
  175. echo '</tr>';
  176. }
  177. ?>
  178. </tbody>
  179. </table>
  180. </td>
  181. </tr>
  182. <?php
  183. }
  184. /**
  185. * Save settings.
  186. */
  187. public function save() {
  188. global $current_section;
  189. $wc_payment_gateways = WC_Payment_Gateways::instance();
  190. $this->save_settings_for_current_section();
  191. if ( ! $current_section ) {
  192. // If section is empty, we're on the main settings page. This makes sure 'gateway ordering' is saved.
  193. $wc_payment_gateways->process_admin_options();
  194. $wc_payment_gateways->init();
  195. } else {
  196. // There is a section - this may be a gateway or custom section.
  197. foreach ( $wc_payment_gateways->payment_gateways() as $gateway ) {
  198. if ( in_array( $current_section, array( $gateway->id, sanitize_title( get_class( $gateway ) ) ), true ) ) {
  199. do_action( 'woocommerce_update_options_payment_gateways_' . $gateway->id );
  200. $wc_payment_gateways->init();
  201. }
  202. }
  203. $this->do_update_options_action();
  204. }
  205. }
  206. }
  207. return new WC_Settings_Payment_Gateways();