/var/www/html/wp-content/plugins/woocommerce/includes/class-wc-payment-gateways.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<?php
/**
 * WooCommerce Payment Gateways
 *
 * Loads payment gateways via hooks for use in the store.
 *
 * @version 2.2.0
 * @package WooCommerce\Classes\Payment
 */

use Automattic\WooCommerce\Proxies\LegacyProxy;
use 
Automattic\WooCommerce\Utilities\ArrayUtil;

defined'ABSPATH' ) || exit;

/**
 * Payment gateways class.
 */
class WC_Payment_Gateways {

    
/**
     * Payment gateway classes.
     *
     * @var array
     */
    
public $payment_gateways = array();

    
/**
     * The single instance of the class.
     *
     * @var WC_Payment_Gateways
     * @since 2.1.0
     */
    
protected static $_instance null;

    
/**
     * Main WC_Payment_Gateways Instance.
     *
     * Ensures only one instance of WC_Payment_Gateways is loaded or can be loaded.
     *
     * @since 2.1
     * @return WC_Payment_Gateways Main instance
     */
    
public static function instance() {
        if ( 
is_nullself::$_instance ) ) {
            
self::$_instance = new self();
        }
        return 
self::$_instance;
    }

    
/**
     * Cloning is forbidden.
     *
     * @since 2.1
     */
    
public function __clone() {
        
wc_doing_it_wrong__FUNCTION____'Cloning is forbidden.''woocommerce' ), '2.1' );
    }

    
/**
     * Unserializing instances of this class is forbidden.
     *
     * @since 2.1
     */
    
public function __wakeup() {
        
wc_doing_it_wrong__FUNCTION____'Unserializing instances of this class is forbidden.''woocommerce' ), '2.1' );
    }

    
/**
     * Initialize payment gateways.
     */
    
public function __construct() {
        
$this->init();
    }

    
/**
     * Load gateways and hook in functions.
     */
    
public function init() {
        
$load_gateways = array(
            
'WC_Gateway_BACS',
            
'WC_Gateway_Cheque',
            
'WC_Gateway_COD',
        );

        if ( 
$this->should_load_paypal_standard() ) {
            
$load_gateways[] = 'WC_Gateway_Paypal';
        }

        
// Filter.
        
$load_gateways apply_filters'woocommerce_payment_gateways'$load_gateways );

        
// Get sort order option.
        
$ordering  = (array) get_option'woocommerce_gateway_order' );
        
$order_end 999;

        
// Load gateways in order.
        
foreach ( $load_gateways as $gateway ) {
            if ( 
is_string$gateway ) && class_exists$gateway ) ) {
                
$gateway = new $gateway();
            }

            
// Gateways need to be valid and extend WC_Payment_Gateway.
            
if ( ! is_a$gateway'WC_Payment_Gateway' ) ) {
                continue;
            }

            if ( isset( 
$ordering$gateway->id ] ) && is_numeric$ordering$gateway->id ] ) ) {
                
// Add in position.
                
$this->payment_gateways$ordering$gateway->id ] ] = $gateway;
            } else {
                
// Add to end of the array.
                
$this->payment_gateways$order_end ] = $gateway;
                ++
$order_end;
            }
        }

        
ksort$this->payment_gateways );

        
add_action'wc_payment_gateways_initialized', array( $this'on_payment_gateways_initialized' ) );
        
/**
         * Hook that is called when the payment gateways have been initialized.
         *
         * @param WC_Payment_Gateways $wc_payment_gateways The payment gateways instance.
         * @since 8.5.0
         */
        
do_action'wc_payment_gateways_initialized'$this );
    }

    
// phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.Found

    /**
     * Hook into payment gateway settings changes.
     *
     * @param WC_Payment_Gateways $wc_payment_gateways The WC_Payment_Gateways instance.
     * @since 8.5.0
     *
     * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
     */
    
public function on_payment_gateways_initializedWC_Payment_Gateways $wc_payment_gateways ) {
        foreach ( 
$this->payment_gateways as $gateway ) {
            
$option_key $gateway->get_option_key();
            
add_action(
                
'add_option_' $option_key,
                function ( 
$option$value ) use ( $gateway ) {
                    
$this->payment_gateway_settings_option_changed$gateway$value$option );
                },
                
10,
                
2
            
);
            
add_action(
                
'update_option_' $option_key,
                function ( 
$old_value$value$option ) use ( $gateway ) {
                    
$this->payment_gateway_settings_option_changed$gateway$value$option$old_value );
                },
                
10,
                
3
            
);
        }
    }

    
// phpcs:enable Generic.CodeAnalysis.UnusedFunctionParameter.Found

    /**
     * Callback for when a gateway settings option was added or updated.
     *
     * @param WC_Payment_Gateway $gateway   The gateway for which the option was added or updated.
     * @param mixed              $value     New value.
     * @param string             $option    Option name.
     * @param mixed              $old_value Old value. `null` when called via add_option_ hook.
     * @since 8.5.0
     */
    
private function payment_gateway_settings_option_changed$gateway$value$option$old_value null ) {
        if ( ! 
$this->was_gateway_enabled$value$old_value ) ) {
            return;
        }

        
// This is a change to a payment gateway's settings and it was just enabled. Let's send an email to the admin.
        // "untitled" shouldn't happen, but just in case.
        
$this->notify_admin_payment_gateway_enabled$gateway );
    }

    
/**
     * Email the site admin when a payment gateway has been enabled.
     *
     * @param WC_Payment_Gateway $gateway The gateway that was enabled.
     * @return bool Whether the email was sent or not.
     * @since 8.5.0
     */
    
private function notify_admin_payment_gateway_enabled$gateway ) {
        
$admin_email          get_option'admin_email' );
        
$user                 get_user_by'email'$admin_email );
        
$username             $user $user->user_login $admin_email;
        
$gateway_title        $gateway->get_method_title();
        
$gateway_settings_url esc_url_rawself_admin_url'admin.php?page=wc-settings&tab=checkout&section=' $gateway->id ) );
        
$site_name            wp_specialchars_decodeget_option'blogname' ), ENT_QUOTES );
        
$site_url             home_url();
        
/**
         * Allows adding to the addresses that receive payment gateway enabled notifications.
         *
         * @param array              $email_addresses The array of email addresses to notify.
         * @param WC_Payment_Gateway $gateway The gateway that was enabled.
         * @return array             The augmented array of email addresses to notify.
         * @since 8.5.0
         */
        
$email_addresses   apply_filters'wc_payment_gateway_enabled_notification_email_addresses', array(), $gateway );
        
$email_addresses[] = $admin_email;
        
$email_addresses   array_unique(
            
array_filter(
                
$email_addresses,
                function ( 
$email_address ) {
                    return 
filter_var$email_addressFILTER_VALIDATE_EMAIL );
                }
            )
        );

        
$logger wc_get_container()->getLegacyProxy::class )->call_function'wc_get_logger' );
        
$logger->infosprintf'Payment gateway enabled: "%s"'$gateway_title ) );

        
$email_text sprintf(
            
/* translators: Payment gateway enabled notification email. 1: Username, 2: Gateway Title, 3: Site URL, 4: Gateway Settings URL, 5: Admin Email, 6: Site Name, 7: Site URL. */
            
__(
                
'Howdy %1$s,

The payment gateway "%2$s" was just enabled on this site:
%3$s

If this was intentional you can safely ignore and delete this email.

If you did not enable this payment gateway, please log in to your site and consider disabling it here:
%4$s

This email has been sent to %5$s

Regards,
All at %6$s
%7$s'
,
                
'woocommerce'
            
),
            
$username,
            
$gateway_title,
            
$site_url,
            
$gateway_settings_url,
            
$admin_email,
            
$site_name,
            
$site_url
        
);

        if ( 
'' !== get_option'blogname' ) ) {
            
$site_title wp_specialchars_decodeget_option'blogname' ), ENT_QUOTES );
        } else {
            
$site_title wp_parse_urlhome_url(), PHP_URL_HOST );
        }

        return 
wp_mail(
            
$email_addresses,
            
sprintf(
                
/* translators: Payment gateway enabled notification email subject. %s1: Site title, $s2: Gateway title. */
                
__'[%1$s] Payment gateway "%2$s" enabled''woocommerce' ),
                
$site_title,
                
$gateway_title
            
),
            
$email_text
        
);
    }

    
/**
     * Determines from changes in settings if a gateway was enabled.
     *
     * @param array $value New value.
     * @param array $old_value Old value.
     * @return bool Whether the gateway was enabled or not.
     */
    
private function was_gateway_enabled$value$old_value null ) {
        if ( 
null === $old_value ) {
            
// There was no old value, so this is a new option.
            
if ( ! empty( $value ) && is_array$value ) && isset( $value['enabled'] ) && 'yes' === $value['enabled'] && isset( $value['title'] ) ) {
                return 
true;
            }
            return 
false;
        }
        
// There was an old value, so this is an update.
        
if (
            
ArrayUtil::get_value_or_default$value'enabled' ) === 'yes' &&
            
ArrayUtil::get_value_or_default$old_value'enabled' ) !== 'yes' ) {
            return 
true;
        }
        return 
false;
    }

    
/**
     * Get gateways.
     *
     * @return array
     */
    
public function payment_gateways() {
        
$_available_gateways = array();

        if ( 
count$this->payment_gateways ) > ) {
            foreach ( 
$this->payment_gateways as $gateway ) {
                
$_available_gateways$gateway->id ] = $gateway;
            }
        }

        return 
$_available_gateways;
    }

    
/**
     * Get array of registered gateway ids
     *
     * @since 2.6.0
     * @return array of strings
     */
    
public function get_payment_gateway_ids() {
        return 
wp_list_pluck$this->payment_gateways'id' );
    }

    
/**
     * Get available gateways.
     *
     * @return array
     */
    
public function get_available_payment_gateways() {
        
$_available_gateways = array();

        foreach ( 
$this->payment_gateways as $gateway ) {
            if ( 
$gateway->is_available() ) {
                if ( ! 
is_add_payment_method_page() ) {
                    
$_available_gateways$gateway->id ] = $gateway;
                } elseif ( 
$gateway->supports'add_payment_method' ) || $gateway->supports'tokenization' ) ) {
                    
$_available_gateways$gateway->id ] = $gateway;
                }
            }
        }

        return 
array_filter( (array) apply_filters'woocommerce_available_payment_gateways'$_available_gateways ), array( $this'filter_valid_gateway_class' ) );
    }

    
/**
     * Callback for array filter. Returns true if gateway is of correct type.
     *
     * @since 3.6.0
     * @param object $gateway Gateway to check.
     * @return bool
     */
    
protected function filter_valid_gateway_class$gateway ) {
        return 
$gateway && is_a$gateway'WC_Payment_Gateway' );
    }

    
/**
     * Set the current, active gateway.
     *
     * @param array $gateways Available payment gateways.
     */
    
public function set_current_gateway$gateways ) {
        
// Be on the defensive.
        
if ( ! is_array$gateways ) || empty( $gateways ) ) {
            return;
        }

        
$current_gateway false;

        if ( 
WC()->session ) {
            
$current WC()->session->get'chosen_payment_method' );

            if ( 
$current && isset( $gateways$current ] ) ) {
                
$current_gateway $gateways$current ];
            }
        }

        if ( ! 
$current_gateway ) {
            
$current_gateway current$gateways );
        }

        
// Ensure we can make a call to set_current() without triggering an error.
        
if ( $current_gateway && is_callable( array( $current_gateway'set_current' ) ) ) {
            
$current_gateway->set_current();
        }
    }

    
/**
     * Save options in admin.
     */
    
public function process_admin_options() {
        
$gateway_order = isset( $_POST['gateway_order'] ) ? wc_cleanwp_unslash$_POST['gateway_order'] ) ) : ''// WPCS: input var ok, CSRF ok.
        
$order         = array();

        if ( 
is_array$gateway_order ) && count$gateway_order ) > ) {
            
$loop 0;
            foreach ( 
$gateway_order as $gateway_id ) {
                
$orderesc_attr$gateway_id ) ] = $loop;
                ++
$loop;
            }
        }

        
update_option'woocommerce_gateway_order'$order );
    }

    
/**
     * Determines if PayPal Standard should be loaded.
     *
     * @since 5.5.0
     * @return bool Whether PayPal Standard should be loaded or not.
     */
    
protected function should_load_paypal_standard() {
        
// Tech debt: This class needs to be initialized to make sure any existing subscriptions gets processed as expected, even if the gateway is not enabled for new orders.
        // Eventually, we want to load this via a singleton pattern to avoid unnecessary instantiation.
        
$paypal = new WC_Gateway_Paypal();
        return 
$paypal->should_load();
    }
}