Completed
Pull Request — master (#1267)
by
unknown
01:55
created

woocommerce-gateway-stripe.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Plugin Name: WooCommerce Stripe Gateway
4
 * Plugin URI: https://wordpress.org/plugins/woocommerce-gateway-stripe/
5
 * Description: Take credit card payments on your store using Stripe.
6
 * Author: WooCommerce
7
 * Author URI: https://woocommerce.com/
8
 * Version: 4.5.0
9
 * Requires at least: 4.4
10
 * Tested up to: 5.4
11
 * WC requires at least: 3.0
12
 * WC tested up to: 4.2
13
 * Text Domain: woocommerce-gateway-stripe
14
 * Domain Path: /languages
15
 *
16
 */
17
18
if ( ! defined( 'ABSPATH' ) ) {
19
	exit;
20
}
21
22
/**
23
 * Required minimums and constants
24
 */
25
define( 'WC_STRIPE_VERSION', '4.5.0' );
26
define( 'WC_STRIPE_MIN_PHP_VER', '5.6.0' );
27
define( 'WC_STRIPE_MIN_WC_VER', '3.0' );
28
define( 'WC_STRIPE_FUTURE_MIN_WC_VER', '3.0' );
29
define( 'WC_STRIPE_MAIN_FILE', __FILE__ );
30
define( 'WC_STRIPE_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
31
define( 'WC_STRIPE_PLUGIN_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
32
33
// phpcs:disable WordPress.Files.FileName
34
35
/**
36
 * WooCommerce fallback notice.
37
 *
38
 * @since 4.1.2
39
 * @return string
40
 */
41
function woocommerce_stripe_missing_wc_notice() {
42
	/* translators: 1. URL link. */
43
	echo '<div class="error"><p><strong>' . sprintf( esc_html__( 'Stripe requires WooCommerce to be installed and active. You can download %s here.', 'woocommerce-gateway-stripe' ), '<a href="https://woocommerce.com/" target="_blank">WooCommerce</a>' ) . '</strong></p></div>';
44
}
45
46
/**
47
 * WooCommerce not supported fallback notice.
48
 *
49
 * @since 4.4.0
50
 * @return string
51
 */
52
function woocommerce_stripe_wc_not_supported() {
53
	/* translators: $1. Minimum WooCommerce version. $2. Current WooCommerce version. */
54
	echo '<div class="error"><p><strong>' . sprintf( esc_html__( 'Stripe requires WooCommerce %1$s or greater to be installed and active. WooCommerce %2$s is no longer supported.', 'woocommerce-gateway-stripe' ), WC_STRIPE_MIN_WC_VER, WC_VERSION ) . '</strong></p></div>';
55
}
56
57
add_action( 'plugins_loaded', 'woocommerce_gateway_stripe_init' );
58
59
function woocommerce_gateway_stripe_init() {
60
	load_plugin_textdomain( 'woocommerce-gateway-stripe', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
61
62
	if ( ! class_exists( 'WooCommerce' ) ) {
63
		add_action( 'admin_notices', 'woocommerce_stripe_missing_wc_notice' );
64
		return;
65
	}
66
67
	if ( version_compare( WC_VERSION, WC_STRIPE_MIN_WC_VER, '<' ) ) {
68
		add_action( 'admin_notices', 'woocommerce_stripe_wc_not_supported' );
69
		return;
70
	}
71
72
	if ( ! class_exists( 'WC_Stripe' ) ) :
73
74
		class WC_Stripe {
75
76
			/**
77
			 * @var Singleton The reference the *Singleton* instance of this class
78
			 */
79
			private static $instance;
80
81
			/**
82
			 * Returns the *Singleton* instance of this class.
83
			 *
84
			 * @return Singleton The *Singleton* instance.
85
			 */
86
			public static function get_instance() {
87
				if ( null === self::$instance ) {
88
					self::$instance = new self();
0 ignored issues
show
Documentation Bug introduced by
It seems like new self() of type object<WC_Stripe> is incompatible with the declared type object<Singleton> of property $instance.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
89
				}
90
				return self::$instance;
0 ignored issues
show
Bug Compatibility introduced by
The expression self::$instance; of type WC_Stripe|Singleton adds the type WC_Stripe to the return on line 90 which is incompatible with the return type documented by WC_Stripe::get_instance of type Singleton.
Loading history...
91
			}
92
93
			/**
94
			 * Stripe Connect API
95
			 *
96
			 * @var WC_Stripe_Connect_API
97
			 */
98
			private $api;
99
100
			/**
101
			 * Stripe Connect
102
			 *
103
			 * @var WC_Stripe_Connect
104
			 */
105
			private $connect;
106
107
			/**
108
			 * Private clone method to prevent cloning of the instance of the
109
			 * *Singleton* instance.
110
			 *
111
			 * @return void
112
			 */
113
			private function __clone() {}
114
115
			/**
116
			 * Private unserialize method to prevent unserializing of the *Singleton*
117
			 * instance.
118
			 *
119
			 * @return void
120
			 */
121
			private function __wakeup() {}
122
123
			/**
124
			 * Protected constructor to prevent creating a new instance of the
125
			 * *Singleton* via the `new` operator from outside of this class.
126
			 */
127
			private function __construct() {
128
				add_action( 'admin_init', array( $this, 'install' ) );
129
130
				$this->init();
131
132
				$this->api     = new WC_Stripe_Connect_API();
133
				$this->connect = new WC_Stripe_Connect( $this->api );
134
135
				add_action( 'rest_api_init', array( $this, 'register_connect_routes' ) );
136
137
				if ( get_option( 'stripe_state', false ) ) {
138
					add_action( 'admin_enqueue_scripts', array( $this->connect, 'maybe_connect_oauth' ) );
139
				}
140
			}
141
142
			/**
143
			 * Init the plugin after plugins_loaded so environment variables are set.
144
			 *
145
			 * @since 1.0.0
146
			 * @version 4.0.0
147
			 */
148
			public function init() {
149
				if ( is_admin() ) {
150
					require_once dirname( __FILE__ ) . '/includes/admin/class-wc-stripe-privacy.php';
151
				}
152
153
				require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-exception.php';
154
				require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-logger.php';
155
				require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-helper.php';
156
				include_once dirname( __FILE__ ) . '/includes/class-wc-stripe-api.php';
157
				require_once dirname( __FILE__ ) . '/includes/abstracts/abstract-wc-stripe-payment-gateway.php';
158
				require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-webhook-handler.php';
159
				require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-sepa-payment-token.php';
160
				require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-apple-pay-registration.php';
161
				require_once dirname( __FILE__ ) . '/includes/compat/class-wc-stripe-pre-orders-compat.php';
162
				require_once dirname( __FILE__ ) . '/includes/class-wc-gateway-stripe.php';
163
				require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-bancontact.php';
164
				require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-sofort.php';
165
				require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-giropay.php';
166
				require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-eps.php';
167
				require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-ideal.php';
168
				require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-p24.php';
169
				require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-alipay.php';
170
				require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-sepa.php';
171
				require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-multibanco.php';
172
				require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-payment-request.php';
173
				require_once dirname( __FILE__ ) . '/includes/compat/class-wc-stripe-subs-compat.php';
174
				require_once dirname( __FILE__ ) . '/includes/compat/class-wc-stripe-sepa-subs-compat.php';
175
				require_once dirname( __FILE__ ) . '/includes/connect/class-wc-stripe-connect.php';
176
				require_once dirname( __FILE__ ) . '/includes/connect/class-wc-stripe-connect-api.php';
177
				require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-order-handler.php';
178
				require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-payment-tokens.php';
179
				require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-customer.php';
180
				require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-intent-controller.php';
181
182
				if ( is_admin() ) {
183
					require_once dirname( __FILE__ ) . '/includes/admin/class-wc-stripe-admin-notices.php';
184
					require_once dirname( __FILE__ ) . '/includes/connect/class-wc-stripe-connect.php';
185
					require_once dirname( __FILE__ ) . '/includes/connect/class-wc-stripe-connect-api.php';
186
				}
187
188
				// REMOVE IN THE FUTURE.
189
				require_once dirname( __FILE__ ) . '/includes/deprecated/class-wc-stripe-apple-pay.php';
190
191
				add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateways' ) );
192
				add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) );
193
				add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 );
194
195
				// Modify emails emails.
196
				add_filter( 'woocommerce_email_classes', array( $this, 'add_emails' ), 20 );
197
198
				if ( version_compare( WC_VERSION, '3.4', '<' ) ) {
199
					add_filter( 'woocommerce_get_sections_checkout', array( $this, 'filter_gateway_order_admin' ) );
200
				}
201
			}
202
203
			/**
204
			 * Updates the plugin version in db
205
			 *
206
			 * @since 3.1.0
207
			 * @version 4.0.0
208
			 */
209
			public function update_plugin_version() {
210
				delete_option( 'wc_stripe_version' );
211
				update_option( 'wc_stripe_version', WC_STRIPE_VERSION );
212
			}
213
214
			/**
215
			 * Handles upgrade routines.
216
			 *
217
			 * @since 3.1.0
218
			 * @version 3.1.0
219
			 */
220
			public function install() {
221
				if ( ! is_plugin_active( plugin_basename( __FILE__ ) ) ) {
222
					return;
223
				}
224
225
				if ( ! defined( 'IFRAME_REQUEST' ) && ( WC_STRIPE_VERSION !== get_option( 'wc_stripe_version' ) ) ) {
226
					do_action( 'woocommerce_stripe_updated' );
227
228
					if ( ! defined( 'WC_STRIPE_INSTALLING' ) ) {
229
						define( 'WC_STRIPE_INSTALLING', true );
230
					}
231
232
					$this->update_plugin_version();
233
				}
234
			}
235
236
			/**
237
			 * Add plugin action links.
238
			 *
239
			 * @since 1.0.0
240
			 * @version 4.0.0
241
			 */
242
			public function plugin_action_links( $links ) {
243
				$plugin_links = array(
244
					'<a href="admin.php?page=wc-settings&tab=checkout&section=stripe">' . esc_html__( 'Settings', 'woocommerce-gateway-stripe' ) . '</a>',
245
				);
246
				return array_merge( $plugin_links, $links );
247
			}
248
249
			/**
250
			 * Add plugin action links.
251
			 *
252
			 * @since 4.3.4
253
			 * @param  array  $links Original list of plugin links.
254
			 * @param  string $file  Name of current file.
255
			 * @return array  $links Update list of plugin links.
256
			 */
257
			public function plugin_row_meta( $links, $file ) {
258
				if ( plugin_basename( __FILE__ ) === $file ) {
259
					$row_meta = array(
260
						'docs'    => '<a href="' . esc_url( apply_filters( 'woocommerce_gateway_stripe_docs_url', 'https://docs.woocommerce.com/document/stripe/' ) ) . '" title="' . esc_attr( __( 'View Documentation', 'woocommerce-gateway-stripe' ) ) . '">' . __( 'Docs', 'woocommerce-gateway-stripe' ) . '</a>',
261
						'support' => '<a href="' . esc_url( apply_filters( 'woocommerce_gateway_stripe_support_url', 'https://woocommerce.com/my-account/create-a-ticket?select=18627' ) ) . '" title="' . esc_attr( __( 'Open a support request at WooCommerce.com', 'woocommerce-gateway-stripe' ) ) . '">' . __( 'Support', 'woocommerce-gateway-stripe' ) . '</a>',
262
					);
263
					return array_merge( $links, $row_meta );
264
				}
265
				return (array) $links;
266
			}
267
268
			/**
269
			 * Add the gateways to WooCommerce.
270
			 *
271
			 * @since 1.0.0
272
			 * @version 4.0.0
273
			 */
274
			public function add_gateways( $methods ) {
275
				if ( class_exists( 'WC_Subscriptions_Order' ) && function_exists( 'wcs_create_renewal_order' ) ) {
276
					$methods[] = 'WC_Stripe_Subs_Compat';
277
					$methods[] = 'WC_Stripe_Sepa_Subs_Compat';
278
				} else {
279
					$methods[] = 'WC_Gateway_Stripe';
280
					$methods[] = 'WC_Gateway_Stripe_Sepa';
281
				}
282
283
				$methods[] = 'WC_Gateway_Stripe_Bancontact';
284
				$methods[] = 'WC_Gateway_Stripe_Sofort';
285
				$methods[] = 'WC_Gateway_Stripe_Giropay';
286
				$methods[] = 'WC_Gateway_Stripe_Eps';
287
				$methods[] = 'WC_Gateway_Stripe_Ideal';
288
				$methods[] = 'WC_Gateway_Stripe_P24';
289
				$methods[] = 'WC_Gateway_Stripe_Alipay';
290
				$methods[] = 'WC_Gateway_Stripe_Multibanco';
291
292
				return $methods;
293
			}
294
295
			/**
296
			 * Modifies the order of the gateways displayed in admin.
297
			 *
298
			 * @since 4.0.0
299
			 * @version 4.0.0
300
			 */
301
			public function filter_gateway_order_admin( $sections ) {
302
				unset( $sections['stripe'] );
303
				unset( $sections['stripe_bancontact'] );
304
				unset( $sections['stripe_sofort'] );
305
				unset( $sections['stripe_giropay'] );
306
				unset( $sections['stripe_eps'] );
307
				unset( $sections['stripe_ideal'] );
308
				unset( $sections['stripe_p24'] );
309
				unset( $sections['stripe_alipay'] );
310
				unset( $sections['stripe_sepa'] );
311
				unset( $sections['stripe_multibanco'] );
312
313
				$sections['stripe']            = 'Stripe';
314
				$sections['stripe_bancontact'] = __( 'Stripe Bancontact', 'woocommerce-gateway-stripe' );
315
				$sections['stripe_sofort']     = __( 'Stripe SOFORT', 'woocommerce-gateway-stripe' );
316
				$sections['stripe_giropay']    = __( 'Stripe Giropay', 'woocommerce-gateway-stripe' );
317
				$sections['stripe_eps']        = __( 'Stripe EPS', 'woocommerce-gateway-stripe' );
318
				$sections['stripe_ideal']      = __( 'Stripe iDeal', 'woocommerce-gateway-stripe' );
319
				$sections['stripe_p24']        = __( 'Stripe P24', 'woocommerce-gateway-stripe' );
320
				$sections['stripe_alipay']     = __( 'Stripe Alipay', 'woocommerce-gateway-stripe' );
321
				$sections['stripe_sepa']       = __( 'Stripe SEPA Direct Debit', 'woocommerce-gateway-stripe' );
322
				$sections['stripe_multibanco'] = __( 'Stripe Multibanco', 'woocommerce-gateway-stripe' );
323
324
				return $sections;
325
			}
326
327
			/**
328
			 * Adds the failed SCA auth email to WooCommerce.
329
			 *
330
			 * @param WC_Email[] $email_classes All existing emails.
331
			 * @return WC_Email[]
332
			 */
333
			public function add_emails( $email_classes ) {
334
				require_once WC_STRIPE_PLUGIN_PATH . '/includes/compat/class-wc-stripe-email-failed-authentication.php';
335
				require_once WC_STRIPE_PLUGIN_PATH . '/includes/compat/class-wc-stripe-email-failed-renewal-authentication.php';
336
				require_once WC_STRIPE_PLUGIN_PATH . '/includes/compat/class-wc-stripe-email-failed-preorder-authentication.php';
337
				require_once WC_STRIPE_PLUGIN_PATH . '/includes/compat/class-wc-stripe-email-failed-authentication-retry.php';
338
339
				// Add all emails, generated by the gateway.
340
				$email_classes['WC_Stripe_Email_Failed_Renewal_Authentication']  = new WC_Stripe_Email_Failed_Renewal_Authentication( $email_classes );
341
				$email_classes['WC_Stripe_Email_Failed_Preorder_Authentication'] = new WC_Stripe_Email_Failed_Preorder_Authentication( $email_classes );
342
				$email_classes['WC_Stripe_Email_Failed_Authentication_Retry'] = new WC_Stripe_Email_Failed_Authentication_Retry( $email_classes );
343
344
				return $email_classes;
345
			}
346
347
			/**
348
			 * Register Stripe connect rest routes.
349
			 */
350
			public function register_connect_routes() {
351
352
				require_once WC_STRIPE_PLUGIN_PATH . '/includes/connect/class-wc-stripe-connect-rest-controller.php';
353
				require_once WC_STRIPE_PLUGIN_PATH . '/includes/connect/class-wc-stripe-connect-rest-oauth-init-controller.php';
354
				require_once WC_STRIPE_PLUGIN_PATH . '/includes/connect/class-wc-stripe-connect-rest-oauth-connect-controller.php';
355
				require_once WC_STRIPE_PLUGIN_PATH . '/includes/connect/class-wc-stripe-connect-rest-deauthorize-controller.php';
356
357
				$oauth_init    = new WC_Stripe_Connect_REST_Oauth_Init_Controller( $this->connect, $this->api );
358
				$oauth_connect = new WC_Stripe_Connect_REST_Oauth_Connect_Controller( $this->connect, $this->api );
359
				$deauthorize   = new WC_Stripe_Connect_REST_Deauthorize_Controller( $this->connect, $this->api );
360
361
				$oauth_init->register_routes();
362
				$oauth_connect->register_routes();
363
				$deauthorize->register_routes();
364
			}
365
		}
366
367
		WC_Stripe::get_instance();
368
	endif;
369
}
370