PluginProbe
WooCommerce / 11.1.0-rc.2
WooCommerce v11.1.0-rc.2
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / Admin / Features / PaymentGatewaySuggestions / PaymentGatewaysController.php
PaymentGatewaysController.php
149 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Logic for extending WC_REST_Payment_Gateways_Controller.
4 */
5
6 namespace Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions;
7
8 use Automattic\WooCommerce\Admin\Features\TransientNotices;
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * PaymentGateway class
14 */
15 class PaymentGatewaysController {
16
17 /**
18 * Initialize payment gateway changes.
19 */
20 public static function init() {
21 add_filter( 'woocommerce_rest_prepare_payment_gateway', array( __CLASS__, 'extend_response' ), 10, 3 );
22 add_filter( 'admin_init', array( __CLASS__, 'possibly_do_connection_return_action' ) );
23 add_action( 'woocommerce_admin_payment_gateway_connection_return', array( __CLASS__, 'handle_successfull_connection' ) );
24 }
25
26 /**
27 * Add necessary fields to REST API response.
28 *
29 * @param WP_REST_Response $response Response data.
30 * @param WC_Payment_Gateway $gateway Payment gateway object.
31 * @param WP_REST_Request $request Request object.
32 * @return WP_REST_Response
33 */
34 public static function extend_response( $response, $gateway, $request ) {
35 $data = $response->get_data();
36
37 $data['needs_setup'] = $gateway->needs_setup();
38 $data['post_install_scripts'] = self::get_post_install_scripts( $gateway );
39 $data['settings_url'] = method_exists( $gateway, 'get_settings_url' )
40 ? $gateway->get_settings_url()
41 : admin_url( 'admin.php?page=wc-settings&tab=checkout&section=' . strtolower( $gateway->id ) );
42
43 $return_url = wc_admin_url( '&task=payments&connection-return=' . strtolower( $gateway->id ) . '&_wpnonce=' . wp_create_nonce( 'connection-return' ) );
44 $data['connection_url'] = method_exists( $gateway, 'get_connection_url' )
45 ? $gateway->get_connection_url( $return_url )
46 : null;
47
48 $data['setup_help_text'] = method_exists( $gateway, 'get_setup_help_text' )
49 ? $gateway->get_setup_help_text()
50 : null;
51
52 $data['required_settings_keys'] = method_exists( $gateway, 'get_required_settings_keys' )
53 ? $gateway->get_required_settings_keys()
54 : array();
55
56 $response->set_data( $data );
57
58 return $response;
59 }
60
61 /**
62 * Get payment gateway scripts for post-install.
63 *
64 * @param WC_Payment_Gateway $gateway Payment gateway object.
65 * @return array Install scripts.
66 */
67 public static function get_post_install_scripts( $gateway ) {
68 $scripts = array();
69 $wp_scripts = wp_scripts();
70
71 $handles = method_exists( $gateway, 'get_post_install_script_handles' )
72 ? $gateway->get_post_install_script_handles()
73 : array();
74
75 foreach ( $handles as $handle ) {
76 if ( isset( $wp_scripts->registered[ $handle ] ) ) {
77 $scripts[] = $wp_scripts->registered[ $handle ];
78 }
79 }
80
81 return $scripts;
82 }
83
84 /**
85 * Call an action after a gating has been successfully returned.
86 */
87 public static function possibly_do_connection_return_action() {
88 if (
89 ! isset( $_GET['page'] ) ||
90 'wc-admin' !== $_GET['page'] ||
91 ! isset( $_GET['task'] ) ||
92 'payments' !== $_GET['task'] ||
93 ! isset( $_GET['connection-return'] ) ||
94 ! isset( $_GET['_wpnonce'] ) ||
95 ! wp_verify_nonce( wc_clean( wp_unslash( $_GET['_wpnonce'] ) ), 'connection-return' )
96 ) {
97 return;
98 }
99
100 $gateway_id = sanitize_text_field( wp_unslash( $_GET['connection-return'] ) );
101
102 do_action( 'woocommerce_admin_payment_gateway_connection_return', $gateway_id );
103 }
104
105 /**
106 * Handle a successful gateway connection.
107 *
108 * @param string $gateway_id Gateway ID.
109 */
110 public static function handle_successfull_connection( $gateway_id ) {
111 // phpcs:disable WordPress.Security.NonceVerification
112 if ( ! isset( $_GET['success'] ) || 1 !== intval( $_GET['success'] ) ) {
113 return;
114 }
115 // phpcs:enable WordPress.Security.NonceVerification
116
117 $payment_gateways = WC()->payment_gateways()->payment_gateways();
118 $payment_gateway = isset( $payment_gateways[ $gateway_id ] ) ? $payment_gateways[ $gateway_id ] : null;
119
120 if ( ! $payment_gateway ) {
121 return;
122 }
123
124 $payment_gateway->update_option( 'enabled', 'yes' );
125
126 TransientNotices::add(
127 array(
128 'user_id' => get_current_user_id(),
129 'id' => 'payment-gateway-connection-return-' . str_replace( ',', '-', $gateway_id ),
130 'status' => 'success',
131 'content' => sprintf(
132 /* translators: the title of the payment gateway */
133 __( '%s connected successfully', 'woocommerce' ),
134 $payment_gateway->method_title
135 ),
136 )
137 );
138
139 wc_admin_record_tracks_event(
140 'tasklist_payment_connect_method',
141 array(
142 'payment_method' => $gateway_id,
143 )
144 );
145
146 wp_safe_redirect( wc_admin_url() );
147 }
148 }
149