| @@ -33,13 +33,14 @@ | ||
| 33 | 33 | private static function check_for_stripe_connect_ajax_actions() { |
| 34 | 34 | $action = FrmAppHelper::get_param( 'action', '', 'post', 'sanitize_text_field' ); |
| 35 | 35 | $prefix = 'frm_stripe_connect_'; |
| 36 | 36 | |
| 37 | - if ( ! $action || 0 !== strpos( $action, $prefix ) ) { | |
| 37 | + if ( ! $action || ! str_starts_with( $action, $prefix ) ) { | |
| 38 | 38 | if ( 'frm_strp_connect_get_settings_button' === $action ) { |
| 39 | 39 | FrmAppHelper::permission_check( 'frm_change_settings' ); |
| 40 | 40 | self::render_settings(); |
| 41 | 41 | } |
| 42 | + | |
| 42 | 43 | return; |
| 43 | 44 | } |
| 44 | 45 | |
| 45 | 46 | FrmAppHelper::permission_check( 'frm_change_settings' ); |
| @@ -98,9 +99,9 @@ | ||
| 98 | 99 | private static function initialize() { |
| 99 | 100 | $mode = self::get_mode_value_from_post(); |
| 100 | 101 | |
| 101 | 102 | if ( self::get_account_id( $mode ) ) { |
| 102 | - // do not allow for initialize if there is already a configured account id | |
| 103 | + // Do not allow for initialize if there is already a configured account id. | |
| 103 | 104 | return 'Cannot initialize another account'; |
| 104 | 105 | } |
| 105 | 106 | |
| 106 | 107 | $additional_body = array( |
| @@ -114,13 +115,13 @@ | ||
| 114 | 115 | return $data; |
| 115 | 116 | } |
| 116 | 117 | |
| 117 | 118 | if ( ! empty( $data->password ) ) { |
| 118 | - update_option( self::get_server_side_token_option_name( $mode ), $data->password, 'no' ); | |
| 119 | + update_option( self::get_server_side_token_option_name( $mode ), $data->password, false ); | |
| 119 | 120 | } |
| 120 | 121 | |
| 121 | 122 | if ( ! empty( $data->account_id ) ) { |
| 122 | - update_option( self::get_account_id_option_name( $mode ), $data->account_id, 'no' ); | |
| 123 | + update_option( self::get_account_id_option_name( $mode ), $data->account_id, false ); | |
| 123 | 124 | } |
| 124 | 125 | |
| 125 | 126 | return $data; |
| 126 | 127 | } |
| @@ -128,21 +129,21 @@ | ||
| 128 | 129 | /** |
| 129 | 130 | * Generate a new client password for authenticating with Connect Service and save it locally as an option. |
| 130 | 131 | * |
| 131 | 132 | * @param string $mode 'live' or 'test'. |
| 133 | + * | |
| 132 | 134 | * @return string the client password. |
| 133 | 135 | */ |
| 134 | 136 | private static function generate_client_password( $mode ) { |
| 135 | 137 | $client_password = wp_generate_password(); |
| 136 | - update_option( self::get_client_side_token_option_name( $mode ), $client_password, 'no' ); | |
| 138 | + update_option( self::get_client_side_token_option_name( $mode ), $client_password, false ); | |
| 137 | 139 | return $client_password; |
| 138 | 140 | } |
| 139 | 141 | |
| 140 | 142 | /** |
| 141 | - * @return array|string | |
| 142 | - * | |
| 143 | 143 | * @param string $action |
| 144 | 144 | * @param array $additional_body |
| 145 | + * | |
| 145 | 146 | * @return object|string |
| 146 | 147 | */ |
| 147 | 148 | private static function post_to_connect_server( $action, $additional_body = array() ) { |
| 148 | 149 | $body = array( |
| @@ -152,13 +153,11 @@ | ||
| 152 | 153 | $body = array_merge( $body, $additional_body ); |
| 153 | 154 | $url = self::get_url_to_connect_server(); |
| 154 | 155 | $headers = self::build_headers_for_post(); |
| 155 | 156 | |
| 156 | - if ( ! $headers ) { | |
| 157 | - return 'Unable to build headers for post. Is your pro license configured properly?'; | |
| 158 | - } | |
| 157 | + // (Seconds) default timeout is 5. we want a bit more time to work with. | |
| 158 | + $timeout = 45; | |
| 159 | 159 | |
| 160 | - $timeout = 45; // (seconds) default timeout is 5. we want a bit more time to work with. | |
| 161 | 160 | self::try_to_extend_server_timeout( $timeout ); |
| 162 | 161 | |
| 163 | 162 | $args = compact( 'body', 'headers', 'timeout' ); |
| 164 | 163 | $response = wp_remote_post( $url, $args ); |
| @@ -167,16 +166,18 @@ | ||
| 167 | 166 | return 'Response from server is invalid'; |
| 168 | 167 | } |
| 169 | 168 | |
| 170 | 169 | $body = self::pull_response_body( $response ); |
| 170 | + | |
| 171 | 171 | if ( empty( $body->success ) ) { |
| 172 | 172 | if ( ! empty( $body->data ) && is_string( $body->data ) ) { |
| 173 | 173 | return $body->data; |
| 174 | 174 | } |
| 175 | + | |
| 175 | 176 | return 'Response from server was not successful'; |
| 176 | 177 | } |
| 177 | 178 | |
| 178 | - return isset( $body->data ) ? $body->data : array(); | |
| 179 | + return $body->data ?? array(); | |
| 179 | 180 | } |
| 180 | 181 | |
| 181 | 182 | /** |
| 182 | 183 | * Try to make sure the server time limit exceeds the request time limit. |
| @@ -185,9 +186,9 @@ | ||
| 185 | 186 | * |
| 186 | 187 | * @return void |
| 187 | 188 | */ |
| 188 | 189 | private static function try_to_extend_server_timeout( $timeout ) { |
| 189 | - if ( ! ini_get( 'safe_mode' ) ) { | |
| 190 | + if ( function_exists( 'set_time_limit' ) ) { | |
| 190 | 191 | set_time_limit( $timeout + 10 ); |
| 191 | 192 | } |
| 192 | 193 | } |
| 193 | 194 | |
| @@ -192,8 +193,9 @@ | ||
| 192 | 193 | } |
| 193 | 194 | |
| 194 | 195 | /** |
| 195 | 196 | * @param string $mode either 'auto', 'live', or 'test'. |
| 197 | + * | |
| 196 | 198 | * @return string either _test or _live. |
| 197 | 199 | */ |
| 198 | 200 | private static function get_active_mode_option_name_suffix( $mode = 'auto' ) { |
| 199 | 201 | if ( 'auto' !== $mode ) { |
| @@ -204,8 +206,9 @@ | ||
| 204 | 206 | |
| 205 | 207 | /** |
| 206 | 208 | * @param string $key 'account_id', 'client_password', 'server_password', 'details_submitted'. |
| 207 | 209 | * @param string $mode either 'auto', 'live', or 'test'. |
| 210 | + * | |
| 208 | 211 | * @return string |
| 209 | 212 | */ |
| 210 | 213 | private static function get_strp_connect_option_name( $key, $mode = 'auto' ) { |
| 211 | 214 | return 'frm_strp_connect_' . $key . self::get_active_mode_option_name_suffix( $mode ); |
| @@ -212,8 +215,9 @@ | ||
| 212 | 215 | } |
| 213 | 216 | |
| 214 | 217 | /** |
| 215 | 218 | * @param string $mode either 'auto', 'live', or 'test'. |
| 219 | + * | |
| 216 | 220 | * @return string |
| 217 | 221 | */ |
| 218 | 222 | private static function get_account_id_option_name( $mode = 'auto' ) { |
| 219 | 223 | return self::get_strp_connect_option_name( 'account_id', $mode ); |
| @@ -220,8 +224,9 @@ | ||
| 220 | 224 | } |
| 221 | 225 | |
| 222 | 226 | /** |
| 223 | 227 | * @param string $mode either 'auto', 'live', or 'test'. |
| 228 | + * | |
| 224 | 229 | * @return string |
| 225 | 230 | */ |
| 226 | 231 | private static function get_client_side_token_option_name( $mode = 'auto' ) { |
| 227 | 232 | return self::get_strp_connect_option_name( 'client_password', $mode ); |
| @@ -228,8 +233,9 @@ | ||
| 228 | 233 | } |
| 229 | 234 | |
| 230 | 235 | /** |
| 231 | 236 | * @param string $mode either 'auto', 'live', or 'test'. |
| 237 | + * | |
| 232 | 238 | * @return string |
| 233 | 239 | */ |
| 234 | 240 | private static function get_server_side_token_option_name( $mode = 'auto' ) { |
| 235 | 241 | return self::get_strp_connect_option_name( 'server_password', $mode ); |
| @@ -236,8 +242,9 @@ | ||
| 236 | 242 | } |
| 237 | 243 | |
| 238 | 244 | /** |
| 239 | 245 | * @param string $mode either 'auto', 'live', or 'test'. |
| 246 | + * | |
| 240 | 247 | * @return string |
| 241 | 248 | */ |
| 242 | 249 | private static function get_stripe_details_submitted_option_name( $mode = 'auto' ) { |
| 243 | 250 | return self::get_strp_connect_option_name( 'details_submitted', $mode ); |
| @@ -255,9 +262,9 @@ | ||
| 255 | 262 | */ |
| 256 | 263 | private static function handle_disconnect() { |
| 257 | 264 | self::disconnect(); |
| 258 | 265 | self::reset_stripe_connect_integration(); |
| 259 | - self::maybe_unschedule_crons(); | |
| 266 | + FrmTransLiteAppHelper::trigger_gateway_disconnected_hook( 'stripe', self::get_mode_value_from_post() ); | |
| 260 | 267 | wp_send_json_success(); |
| 261 | 268 | } |
| 262 | 269 | |
| 263 | 270 | /** |
| @@ -276,39 +283,16 @@ | ||
| 276 | 283 | /** |
| 277 | 284 | * @return false|object |
| 278 | 285 | */ |
| 279 | 286 | private static function disconnect() { |
| 280 | - $additional_body = array( | |
| 281 | - 'frm_strp_connect_mode' => self::get_mode_value_from_post(), | |
| 282 | - ); | |
| 287 | + $mode = self::get_mode_value_from_post(); | |
| 288 | + $additional_body = self::get_body_for_mode( $mode ); | |
| 283 | 289 | return self::post_with_authenticated_body( 'disconnect', $additional_body ); |
| 284 | 290 | } |
| 285 | 291 | |
| 286 | 292 | /** |
| 287 | - * Stop the payment cron once all Stripe connections have been disconnected. | |
| 288 | - * | |
| 289 | 293 | * @since 6.5 |
| 290 | 294 | * |
| 291 | - * @return void | |
| 292 | - */ | |
| 293 | - private static function maybe_unschedule_crons() { | |
| 294 | - $mode = self::get_mode_value_from_post(); | |
| 295 | - | |
| 296 | - if ( self::at_least_one_mode_is_setup() ) { | |
| 297 | - // Don't unschedule if a mode is still on. | |
| 298 | - return; | |
| 299 | - } | |
| 300 | - | |
| 301 | - $event = 'frm_payment_cron'; | |
| 302 | - $timestamp = wp_next_scheduled( $event ); | |
| 303 | - if ( false !== $timestamp ) { | |
| 304 | - wp_unschedule_event( $timestamp, $event ); | |
| 305 | - } | |
| 306 | - } | |
| 307 | - | |
| 308 | - /** | |
| 309 | - * @since 6.5 | |
| 310 | - * | |
| 311 | 295 | * @return bool |
| 312 | 296 | */ |
| 313 | 297 | public static function at_least_one_mode_is_setup() { |
| 314 | 298 | return self::stripe_connect_is_setup( 'test' ) || self::stripe_connect_is_setup( 'live' ); |
| @@ -317,18 +301,18 @@ | ||
| 317 | 301 | /** |
| 318 | 302 | * @return void |
| 319 | 303 | */ |
| 320 | 304 | private static function handle_reauth() { |
| 321 | - $additional_body = array( | |
| 322 | - 'frm_strp_connect_mode' => self::get_mode_value_from_post(), | |
| 323 | - ); | |
| 305 | + $mode = self::get_mode_value_from_post(); | |
| 306 | + $additional_body = self::get_body_for_mode( $mode ); | |
| 324 | 307 | $data = self::post_with_authenticated_body( 'reauth', $additional_body ); |
| 325 | 308 | |
| 326 | 309 | if ( false === $data ) { |
| 327 | - // check account status | |
| 328 | - if ( self::check_server_for_connected_account_status() ) { | |
| 310 | + // Check account status. | |
| 311 | + if ( self::check_server_for_connected_account_status( $mode ) ) { | |
| 329 | 312 | wp_send_json_success(); |
| 330 | 313 | } |
| 314 | + | |
| 331 | 315 | wp_send_json_error(); |
| 332 | 316 | } |
| 333 | 317 | |
| 334 | 318 | $response_data = array( |
| @@ -340,25 +324,38 @@ | ||
| 340 | 324 | /** |
| 341 | 325 | * @return array |
| 342 | 326 | */ |
| 343 | 327 | private static function get_standard_authenticated_body() { |
| 344 | - $mode = self::get_mode_value_from_post(); | |
| 328 | + return self::get_body_for_mode( FrmStrpLiteAppHelper::active_mode() ); | |
| 329 | + } | |
| 330 | + | |
| 331 | + /** | |
| 332 | + * Get the standard body with account id, mode, and passwords to send to the connect server. | |
| 333 | + * | |
| 334 | + * @since 6.32.1 | |
| 335 | + * | |
| 336 | + * @param string $mode 'live' or 'test'. | |
| 337 | + * | |
| 338 | + * @return array | |
| 339 | + */ | |
| 340 | + private static function get_body_for_mode( $mode ) { | |
| 345 | 341 | return array( |
| 346 | - 'account_id' => get_option( self::get_account_id_option_name( $mode ) ), | |
| 347 | - 'server_password' => get_option( self::get_server_side_token_option_name( $mode ) ), | |
| 348 | - 'client_password' => get_option( self::get_client_side_token_option_name( $mode ) ), | |
| 342 | + 'account_id' => get_option( self::get_account_id_option_name( $mode ) ), | |
| 343 | + 'server_password' => get_option( self::get_server_side_token_option_name( $mode ) ), | |
| 344 | + 'client_password' => get_option( self::get_client_side_token_option_name( $mode ) ), | |
| 345 | + 'frm_strp_connect_mode' => $mode, | |
| 349 | 346 | ); |
| 350 | 347 | } |
| 351 | 348 | |
| 352 | 349 | private static function redirect() { |
| 353 | 350 | $connected = self::check_server_for_connected_account_status(); |
| 354 | - header( 'Location: ' . self::get_url_for_stripe_settings( $connected ), true, 302 ); | |
| 351 | + wp_safe_redirect( self::get_url_for_stripe_settings( $connected ) ); | |
| 355 | 352 | exit; |
| 356 | 353 | } |
| 357 | 354 | |
| 358 | 355 | private static function redirect_oauth() { |
| 359 | 356 | $connected = self::check_server_for_oauth_account_id(); |
| 360 | - header( 'Location: ' . self::get_url_for_stripe_settings( $connected ), true, 302 ); | |
| 357 | + wp_safe_redirect( self::get_url_for_stripe_settings( $connected ) ); | |
| 361 | 358 | exit; |
| 362 | 359 | } |
| 363 | 360 | |
| 364 | 361 | /** |
| @@ -365,14 +362,15 @@ | ||
| 365 | 362 | * @return bool |
| 366 | 363 | */ |
| 367 | 364 | private static function check_server_for_oauth_account_id() { |
| 368 | 365 | $mode = FrmAppHelper::get_param( 'mode', '', 'get', 'sanitize_text_field' ); |
| 366 | + | |
| 369 | 367 | if ( 'live' !== $mode ) { |
| 370 | 368 | $mode = 'test'; |
| 371 | 369 | } |
| 372 | 370 | |
| 373 | 371 | if ( self::get_account_id( $mode ) ) { |
| 374 | - // do not allow for initialize if there is already a configured account id | |
| 372 | + // Do not allow for initialize if there is already a configured account id. | |
| 375 | 373 | return false; |
| 376 | 374 | } |
| 377 | 375 | |
| 378 | 376 | $body = array( |
| @@ -382,9 +380,9 @@ | ||
| 382 | 380 | ); |
| 383 | 381 | $data = self::post_to_connect_server( 'oauth_account_status', $body ); |
| 384 | 382 | |
| 385 | 383 | if ( is_object( $data ) && ! empty( $data->account_id ) ) { |
| 386 | - update_option( self::get_account_id_option_name( $mode ), $data->account_id, 'no' ); | |
| 384 | + update_option( self::get_account_id_option_name( $mode ), $data->account_id, false ); | |
| 387 | 385 | |
| 388 | 386 | if ( ! empty( $data->details_submitted ) ) { |
| 389 | 387 | self::set_stripe_details_as_submitted( $mode ); |
| 390 | 388 | } |
| @@ -398,12 +396,13 @@ | ||
| 398 | 396 | /** |
| 399 | 397 | * On a successful account status check, set details_submitted option. |
| 400 | 398 | * |
| 401 | 399 | * @param string $mode 'live' or 'test'. |
| 400 | + * | |
| 402 | 401 | * @return void |
| 403 | 402 | */ |
| 404 | 403 | private static function set_stripe_details_as_submitted( $mode ) { |
| 405 | - update_option( self::get_stripe_details_submitted_option_name( $mode ), true, 'no' ); | |
| 404 | + update_option( self::get_stripe_details_submitted_option_name( $mode ), true, false ); | |
| 406 | 405 | FrmTransLiteAppController::install(); |
| 407 | 406 | } |
| 408 | 407 | |
| 409 | 408 | /** |
| @@ -416,15 +415,15 @@ | ||
| 416 | 415 | wp_send_json_success( $response_data ); |
| 417 | 416 | } |
| 418 | 417 | |
| 419 | 418 | /** |
| 420 | - * @return string|false | |
| 419 | + * @return false|string | |
| 421 | 420 | */ |
| 422 | 421 | private static function get_oauth_redirect_url() { |
| 423 | 422 | $mode = self::get_mode_value_from_post(); |
| 424 | 423 | |
| 425 | 424 | if ( self::get_account_id( $mode ) ) { |
| 426 | - // do not allow for initialize if there is already a configured account id | |
| 425 | + // Do not allow for initialize if there is already a configured account id. | |
| 427 | 426 | return false; |
| 428 | 427 | } |
| 429 | 428 | |
| 430 | 429 | $additional_body = array( |
| @@ -432,9 +431,10 @@ | ||
| 432 | 431 | 'user_id' => get_current_user_id(), |
| 433 | 432 | 'frm_strp_connect_mode' => $mode, |
| 434 | 433 | ); |
| 435 | 434 | |
| 436 | - delete_option( 'frm_stripe_lite_last_verify_attempt' ); // Clear the transient so it doesn't fail. | |
| 435 | + // Clear the transient so it doesn't fail. | |
| 436 | + delete_option( 'frm_stripe_lite_last_verify_attempt' ); | |
| 437 | 437 | $data = self::post_to_connect_server( 'oauth_request', $additional_body ); |
| 438 | 438 | |
| 439 | 439 | if ( is_string( $data ) ) { |
| 440 | 440 | return false; |
| @@ -440,9 +440,9 @@ | ||
| 440 | 440 | return false; |
| 441 | 441 | } |
| 442 | 442 | |
| 443 | 443 | if ( ! empty( $data->password ) ) { |
| 444 | - update_option( self::get_server_side_token_option_name( $mode ), $data->password, 'no' ); | |
| 444 | + update_option( self::get_server_side_token_option_name( $mode ), $data->password, false ); | |
| 445 | 445 | } |
| 446 | 446 | |
| 447 | 447 | if ( ! is_object( $data ) || empty( $data->redirect_url ) ) { |
| 448 | 448 | return false; |
| @@ -451,28 +451,39 @@ | ||
| 451 | 451 | return $data->redirect_url; |
| 452 | 452 | } |
| 453 | 453 | |
| 454 | 454 | /** |
| 455 | + * @param string $mode | |
| 456 | + * | |
| 455 | 457 | * @return bool true if our account is onboarded |
| 456 | 458 | */ |
| 457 | - public static function check_server_for_connected_account_status() { | |
| 458 | - $mode = FrmAppHelper::get_param( 'mode', '', 'get', 'sanitize_text_field' ); | |
| 459 | - if ( 'live' !== $mode ) { | |
| 460 | - $mode = 'test'; | |
| 459 | + public static function check_server_for_connected_account_status( $mode = 'check_get' ) { | |
| 460 | + if ( 'check_get' === $mode ) { | |
| 461 | + $mode = FrmAppHelper::get_param( 'mode', '', 'get', 'sanitize_text_field' ); | |
| 462 | + | |
| 463 | + if ( 'live' !== $mode ) { | |
| 464 | + $mode = 'test'; | |
| 465 | + } | |
| 461 | 466 | } |
| 462 | - $additional_body = array( | |
| 463 | - 'frm_strp_connect_mode' => $mode, | |
| 464 | - ); | |
| 467 | + | |
| 468 | + if ( self::stripe_connect_is_setup( $mode ) ) { | |
| 469 | + return false; | |
| 470 | + } | |
| 471 | + | |
| 472 | + $additional_body = self::get_body_for_mode( $mode ); | |
| 465 | 473 | $data = self::post_with_authenticated_body( 'account_status', $additional_body ); |
| 466 | 474 | $success = false !== $data && ! empty( $data->details_submitted ); |
| 475 | + | |
| 467 | 476 | if ( $success ) { |
| 468 | 477 | self::set_stripe_details_as_submitted( $mode ); |
| 469 | 478 | } |
| 479 | + | |
| 470 | 480 | return $success; |
| 471 | 481 | } |
| 472 | 482 | |
| 473 | 483 | /** |
| 474 | 484 | * @param string $mode |
| 485 | + * | |
| 475 | 486 | * @return bool |
| 476 | 487 | */ |
| 477 | 488 | public static function stripe_connect_is_setup( $mode = 'auto' ) { |
| 478 | 489 | return get_option( self::get_stripe_details_submitted_option_name( $mode ) ); |
| @@ -479,8 +490,9 @@ | ||
| 479 | 490 | } |
| 480 | 491 | |
| 481 | 492 | /** |
| 482 | 493 | * @param mixed $response |
| 494 | + * | |
| 483 | 495 | * @return bool |
| 484 | 496 | */ |
| 485 | 497 | private static function validate_response( $response ) { |
| 486 | 498 | return ! is_wp_error( $response ) && is_array( $response ) && isset( $response['http_response'] ); |
| @@ -485,8 +497,13 @@ | ||
| 485 | 497 | private static function validate_response( $response ) { |
| 486 | 498 | return ! is_wp_error( $response ) && is_array( $response ) && isset( $response['http_response'] ); |
| 487 | 499 | } |
| 488 | 500 | |
| 501 | + /** | |
| 502 | + * @param array $response | |
| 503 | + * | |
| 504 | + * @return object|null | |
| 505 | + */ | |
| 489 | 506 | private static function pull_response_body( $response ) { |
| 490 | 507 | $http_response = $response['http_response']; |
| 491 | 508 | $response_object = $http_response->get_response_object(); |
| 492 | 509 | return json_decode( $response_object->body ); |
| @@ -496,8 +513,9 @@ | ||
| 496 | 513 | * @return array |
| 497 | 514 | */ |
| 498 | 515 | private static function build_headers_for_post() { |
| 499 | 516 | $password = self::maybe_get_pro_license(); |
| 517 | + | |
| 500 | 518 | if ( false === $password ) { |
| 501 | 519 | $password = 'lite_' . self::get_uuid(); |
| 502 | 520 | } |
| 503 | 521 | |
| @@ -502,10 +520,12 @@ | ||
| 502 | 520 | } |
| 503 | 521 | |
| 504 | 522 | $site_url = home_url(); |
| 505 | 523 | $site_url = self::maybe_fix_wpml_url( $site_url ); |
| 506 | - $site_url = preg_replace( '#^https?://#', '', $site_url ); // remove protocol from url (our url cannot include the colon). | |
| 507 | - $site_url = preg_replace( '/:[0-9]+/', '', $site_url ); // remove port from url (mostly helpful in development) | |
| 524 | + // Remove protocol from url (our url cannot include the colon). | |
| 525 | + $site_url = preg_replace( '#^https?://#', '', $site_url ); | |
| 526 | + // Remove port from url (mostly helpful in development). | |
| 527 | + $site_url = preg_replace( '/:[0-9]+/', '', $site_url ); | |
| 508 | 528 | $site_url = self::strip_lang_from_url( $site_url ); |
| 509 | 529 | |
| 510 | 530 | // $password is either a Pro license or a uuid (See FrmUsage::uuid). |
| 511 | 531 | return array( |
| @@ -517,8 +537,9 @@ | ||
| 517 | 537 | * WPML alters the output of home_url. |
| 518 | 538 | * If it is active, use the WPML "absolute home" URL which is not modified. |
| 519 | 539 | * |
| 520 | 540 | * @param string $url |
| 541 | + * | |
| 521 | 542 | * @return string |
| 522 | 543 | */ |
| 523 | 544 | private static function maybe_fix_wpml_url( $url ) { |
| 524 | 545 | if ( defined( 'ICL_SITEPRESS_VERSION' ) && ! ICL_PLUGIN_INACTIVE && class_exists( 'SitePress' ) ) { |
| @@ -529,15 +550,16 @@ | ||
| 529 | 550 | } |
| 530 | 551 | |
| 531 | 552 | /** |
| 532 | 553 | * WPML might add a language to the url. Don't send that to the server. |
| 554 | + * | |
| 555 | + * @param string $url URL. | |
| 556 | + * | |
| 557 | + * @return string | |
| 533 | 558 | */ |
| 534 | 559 | private static function strip_lang_from_url( $url ) { |
| 535 | 560 | $split_on_language = explode( '/?lang=', $url ); |
| 536 | - if ( 2 === count( $split_on_language ) ) { | |
| 537 | - $url = $split_on_language[0]; | |
| 538 | - } | |
| 539 | - return $url; | |
| 561 | + return 2 === count( $split_on_language ) ? $split_on_language[0] : $url; | |
| 540 | 562 | } |
| 541 | 563 | |
| 542 | 564 | /** |
| 543 | 565 | * Get a Pro license when Pro is active. |
| @@ -542,17 +564,19 @@ | ||
| 542 | 564 | /** |
| 543 | 565 | * Get a Pro license when Pro is active. |
| 544 | 566 | * Otherwise we'll use a uuid to support Lite. |
| 545 | 567 | * |
| 546 | - * @return string|false | |
| 568 | + * @return false|string | |
| 547 | 569 | */ |
| 548 | 570 | private static function maybe_get_pro_license() { |
| 549 | 571 | if ( FrmAppHelper::pro_is_installed() ) { |
| 550 | 572 | $pro_license = FrmAddonsController::get_pro_license(); |
| 573 | + | |
| 551 | 574 | if ( $pro_license ) { |
| 552 | 575 | $password = $pro_license; |
| 553 | 576 | } |
| 554 | 577 | } |
| 578 | + | |
| 555 | 579 | return ! empty( $password ) ? $password : false; |
| 556 | 580 | } |
| 557 | 581 | |
| 558 | 582 | /** |
| @@ -566,8 +590,9 @@ | ||
| 566 | 590 | } |
| 567 | 591 | |
| 568 | 592 | /** |
| 569 | 593 | * @param bool $connected |
| 594 | + * | |
| 570 | 595 | * @return string |
| 571 | 596 | */ |
| 572 | 597 | private static function get_url_for_stripe_settings( $connected ) { |
| 573 | 598 | return admin_url( 'admin.php?page=formidable-settings&t=stripe_settings&connected=' . intval( $connected ) ); |
| @@ -577,8 +602,9 @@ | ||
| 577 | 602 | * @return void |
| 578 | 603 | */ |
| 579 | 604 | public static function render_stripe_connect_settings_container() { |
| 580 | 605 | self::register_settings_scripts(); |
| 606 | + // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 581 | 607 | ?> |
| 582 | 608 | <tr> |
| 583 | 609 | <td> |
| 584 | 610 | <?php esc_html_e( 'Connection Status', 'formidable' ); ?> |
| @@ -587,8 +613,9 @@ | ||
| 587 | 613 | <div id="frm_strp_settings_container"></div> |
| 588 | 614 | </td> |
| 589 | 615 | </tr> |
| 590 | 616 | <?php |
| 617 | + // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 591 | 618 | } |
| 592 | 619 | |
| 593 | 620 | /** |
| 594 | 621 | * @return void |
| @@ -595,8 +622,9 @@ | ||
| 595 | 622 | */ |
| 596 | 623 | private static function render_settings() { |
| 597 | 624 | $modes = array( 'test', 'live' ); |
| 598 | 625 | $html = ''; |
| 626 | + | |
| 599 | 627 | foreach ( $modes as $mode ) { |
| 600 | 628 | $account_id = self::get_account_id( $mode ); |
| 601 | 629 | $connected = self::stripe_connect_is_setup( $mode ); |
| 602 | 630 | $test = 'test' === $mode; |
| @@ -603,10 +631,9 @@ | ||
| 603 | 631 | $title = $test ? __( 'TEST', 'formidable' ) : __( 'LIVE', 'formidable' ); |
| 604 | 632 | |
| 605 | 633 | ob_start(); |
| 606 | 634 | require FrmStrpLiteAppHelper::plugin_path() . '/views/settings/connect.php'; |
| 607 | - $html .= ob_get_contents(); | |
| 608 | - ob_end_clean(); | |
| 635 | + $html .= ob_get_clean(); | |
| 609 | 636 | } |
| 610 | 637 | |
| 611 | 638 | $response_data = array( |
| 612 | 639 | 'html' => $html, |
| @@ -614,15 +641,16 @@ | ||
| 614 | 641 | wp_send_json_success( $response_data ); |
| 615 | 642 | } |
| 616 | 643 | |
| 617 | 644 | /** |
| 618 | - * @todo I can probably remove this. | |
| 619 | 645 | * @return void |
| 620 | 646 | */ |
| 621 | 647 | public static function stripe_icon() { |
| 648 | + // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 622 | 649 | ?> |
| 623 | - <svg height="16" aria-hidden="true" style="vertical-align:text-bottom" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><path fill="currentColor" d="M155.3 154.6c0-22.3 18.6-30.9 48.4-30.9a320 320 0 01141.9 36.7V26.1A376.2 376.2 0 00203.8 0C88.1 0 11 60.4 11 161.4c0 157.9 216.8 132.3 216.8 200.4 0 26.4-22.9 34.9-54.7 34.9-47.2 0-108.2-19.5-156.1-45.5v128.5a396 396 0 00156 32.4c118.6 0 200.3-51 200.3-153.6 0-170.2-218-139.7-218-203.9z"/></svg> | |
| 650 | + <svg height="16" aria-hidden="true" style="vertical-align:text-bottom" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><path fill="currentColor" d="M155.3 154.6c0-22.3 18.6-30.9 48.4-30.9a320 320 0 01141.9 36.7V26.1A376.2 376.2 0 00203.8 0C88.1 0 11 60.4 11 161.4c0 157.9 216.8 132.3 216.8 200.4 0 26.4-22.9 34.9-54.7 34.9-47.2 0-108.2-19.5-156.1-45.5v128.5a396 396 0 00156 32.4c118.6 0 200.3-51 200.3-153.6 0-170.2-218-139.7-218-203.9z"/></svg><?php // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong ?> | |
| 624 | 651 | <?php |
| 652 | + // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 625 | 653 | } |
| 626 | 654 | |
| 627 | 655 | /** |
| 628 | 656 | * Check $_POST for live or test mode value as it can be updated in real time from Stripe Settings and can be configured before the update is saved. |
| @@ -633,8 +661,9 @@ | ||
| 633 | 661 | // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 634 | 662 | if ( empty( $_POST ) || ! array_key_exists( 'testMode', $_POST ) ) { |
| 635 | 663 | return FrmStrpLiteAppHelper::active_mode(); |
| 636 | 664 | } |
| 665 | + | |
| 637 | 666 | $test_mode = FrmAppHelper::get_param( 'testMode', '', 'post', 'absint' ); |
| 638 | 667 | return $test_mode ? 'test' : 'live'; |
| 639 | 668 | } |
| 640 | 669 | |
| @@ -641,16 +670,16 @@ | ||
| 641 | 670 | /** |
| 642 | 671 | * @return void |
| 643 | 672 | */ |
| 644 | 673 | private static function register_settings_scripts() { |
| 645 | - $version = FrmAppHelper::plugin_version(); | |
| 646 | - wp_register_script( 'formidable_stripe_settings', FrmStrpLiteAppHelper::plugin_url() . '/js/connect_settings.js', array( 'formidable_dom' ), FrmAppHelper::plugin_version(), true ); | |
| 674 | + wp_register_script( 'formidable_stripe_settings', FrmStrpLiteAppHelper::plugin_url() . '/js/connect_settings.js', array( 'formidable_dom' ), FrmAppHelper::plugin_version(), true ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 647 | 675 | wp_enqueue_script( 'formidable_stripe_settings' ); |
| 648 | 676 | } |
| 649 | 677 | |
| 650 | 678 | /** |
| 651 | 679 | * @param string $mode |
| 652 | - * @return string|bool | |
| 680 | + * | |
| 681 | + * @return bool|string | |
| 653 | 682 | */ |
| 654 | 683 | public static function get_account_id( $mode = 'auto' ) { |
| 655 | 684 | return get_option( self::get_account_id_option_name( $mode ) ); |
| 656 | 685 | } |
| @@ -656,24 +685,25 @@ | ||
| 656 | 685 | } |
| 657 | 686 | |
| 658 | 687 | /** |
| 659 | 688 | * @param array $options |
| 660 | - * @return string|false | |
| 689 | + * | |
| 690 | + * @return false|string | |
| 661 | 691 | */ |
| 662 | 692 | public static function get_customer_id( $options ) { |
| 663 | 693 | $data = self::post_with_authenticated_body( 'get_customer', compact( 'options' ) ); |
| 664 | 694 | $success = false !== $data; |
| 695 | + | |
| 665 | 696 | if ( ! $success ) { |
| 666 | - return ! empty( self::$latest_error_from_stripe_connect ) ? self::$latest_error_from_stripe_connect : false; | |
| 697 | + return self::$latest_error_from_stripe_connect ? self::$latest_error_from_stripe_connect : false; | |
| 667 | 698 | } |
| 668 | - if ( empty( $data->customer_id ) ) { | |
| 669 | - return false; | |
| 670 | - } | |
| 671 | - return $data->customer_id; | |
| 699 | + | |
| 700 | + return ! empty( $data->customer_id ) ? $data->customer_id : false; | |
| 672 | 701 | } |
| 673 | 702 | |
| 674 | 703 | /** |
| 675 | 704 | * @param string $customer_id |
| 705 | + * | |
| 676 | 706 | * @return bool |
| 677 | 707 | */ |
| 678 | 708 | public static function validate_customer( $customer_id ) { |
| 679 | 709 | $data = self::post_with_authenticated_body( 'validate_customer', compact( 'customer_id' ) ); |
| @@ -683,21 +713,24 @@ | ||
| 683 | 713 | /** |
| 684 | 714 | * @param string $action |
| 685 | 715 | * @param array $additional_body |
| 686 | 716 | * |
| 687 | - * @return object|false | |
| 717 | + * @return false|object | |
| 688 | 718 | */ |
| 689 | 719 | private static function post_with_authenticated_body( $action, $additional_body = array() ) { |
| 690 | 720 | $body = array_merge( self::get_standard_authenticated_body(), $additional_body ); |
| 691 | 721 | $response = self::post_to_connect_server( $action, $body ); |
| 722 | + | |
| 692 | 723 | if ( is_object( $response ) ) { |
| 693 | 724 | return $response; |
| 694 | 725 | } |
| 726 | + | |
| 695 | 727 | if ( is_array( $response ) ) { |
| 696 | - // reformat empty arrays as empty objects | |
| 697 | - // if the response is an array, it's because it's empty. Everything with data is already an object. | |
| 728 | + // Reformat empty arrays as empty objects | |
| 729 | + // If the response is an array, it's because it's empty. Everything with data is already an object. | |
| 698 | 730 | return new stdClass(); |
| 699 | 731 | } |
| 732 | + | |
| 700 | 733 | if ( is_string( $response ) ) { |
| 701 | 734 | self::$latest_error_from_stripe_connect = $response; |
| 702 | 735 | FrmTransLiteLog::log_message( 'Stripe Connect Error', $response ); |
| 703 | 736 | } else { |
| @@ -702,45 +735,47 @@ | ||
| 702 | 735 | FrmTransLiteLog::log_message( 'Stripe Connect Error', $response ); |
| 703 | 736 | } else { |
| 704 | 737 | self::$latest_error_from_stripe_connect = ''; |
| 705 | 738 | } |
| 739 | + | |
| 706 | 740 | return false; |
| 707 | 741 | } |
| 708 | 742 | |
| 709 | 743 | /** |
| 710 | 744 | * @param array $new_charge |
| 745 | + * | |
| 711 | 746 | * @return mixed |
| 712 | 747 | */ |
| 713 | 748 | public static function create_intent( $new_charge ) { |
| 714 | 749 | $data = self::post_with_authenticated_body( 'create_intent', compact( 'new_charge' ) ); |
| 715 | 750 | $success = false !== $data; |
| 716 | - if ( ! $success ) { | |
| 717 | - return false; | |
| 718 | - } | |
| 719 | - return $data; | |
| 751 | + | |
| 752 | + return $success ? $data : false; | |
| 720 | 753 | } |
| 721 | 754 | |
| 722 | 755 | /** |
| 723 | 756 | * @param string $payment_id |
| 757 | + * | |
| 724 | 758 | * @return bool |
| 725 | 759 | */ |
| 726 | 760 | public static function refund_payment( $payment_id ) { |
| 727 | - $data = self::post_with_authenticated_body( 'refund_payment', compact( 'payment_id' ) ); | |
| 728 | - $refunded = is_object( $data ); | |
| 729 | - return $refunded; | |
| 761 | + $data = self::post_with_authenticated_body( 'refund_payment', compact( 'payment_id' ) ); | |
| 762 | + return is_object( $data ); | |
| 730 | 763 | } |
| 731 | 764 | |
| 732 | 765 | /** |
| 733 | 766 | * @param array $new_charge |
| 767 | + * | |
| 734 | 768 | * @return mixed |
| 735 | 769 | */ |
| 736 | 770 | public static function create_subscription( $new_charge ) { |
| 737 | 771 | $data = self::post_with_authenticated_body( 'create_subscription', compact( 'new_charge' ) ); |
| 772 | + | |
| 738 | 773 | if ( is_object( $data ) ) { |
| 739 | 774 | return $data; |
| 740 | 775 | } |
| 741 | 776 | |
| 742 | - if ( isset( self::$latest_error_from_stripe_connect ) && 0 === strpos( self::$latest_error_from_stripe_connect, 'No such plan: ' ) ) { | |
| 777 | + if ( isset( self::$latest_error_from_stripe_connect ) && str_starts_with( self::$latest_error_from_stripe_connect, 'No such plan: ' ) ) { | |
| 743 | 778 | return self::$latest_error_from_stripe_connect; |
| 744 | 779 | } |
| 745 | 780 | |
| 746 | 781 | return false; |
| @@ -747,19 +782,21 @@ | ||
| 747 | 782 | } |
| 748 | 783 | |
| 749 | 784 | /** |
| 750 | 785 | * @param string $sub_id |
| 751 | - * @param string|false $customer_id if specified, this will enforce a customer id match (bypassed for users with administrator permission). | |
| 786 | + * @param false|string $customer_id if specified, this will enforce a customer id match (bypassed for users with administrator permission). | |
| 787 | + * | |
| 752 | 788 | * @return bool |
| 753 | 789 | */ |
| 754 | 790 | public static function cancel_subscription( $sub_id, $customer_id = false ) { |
| 755 | - $data = self::post_with_authenticated_body( 'cancel_subscription', compact( 'sub_id', 'customer_id' ) ); | |
| 756 | - $canceled = false !== $data; | |
| 757 | - return $canceled; | |
| 791 | + $cancel_at_period_end = FrmStrpLiteSubscriptionHelper::should_cancel_at_period_end(); | |
| 792 | + $data = self::post_with_authenticated_body( 'cancel_subscription', compact( 'sub_id', 'customer_id', 'cancel_at_period_end' ) ); | |
| 793 | + return false !== $data; | |
| 758 | 794 | } |
| 759 | 795 | |
| 760 | 796 | /** |
| 761 | 797 | * @param string $payment_id |
| 798 | + * | |
| 762 | 799 | * @return mixed |
| 763 | 800 | */ |
| 764 | 801 | public static function get_intent( $payment_id ) { |
| 765 | 802 | return self::post_with_authenticated_body( 'get_intent', compact( 'payment_id' ) ); |
| @@ -765,34 +802,32 @@ | ||
| 765 | 802 | return self::post_with_authenticated_body( 'get_intent', compact( 'payment_id' ) ); |
| 766 | 803 | } |
| 767 | 804 | |
| 768 | 805 | /** |
| 769 | - * @return object|false | |
| 806 | + * @return false|object | |
| 770 | 807 | */ |
| 771 | 808 | public static function get_customer_subscriptions() { |
| 772 | - $user_id = get_current_user_id(); | |
| 773 | 809 | $meta_name = FrmStrpLiteAppHelper::get_customer_id_meta_name(); |
| 774 | - $customer_id = get_user_meta( $user_id, $meta_name, true ); | |
| 810 | + $customer_id = get_user_meta( get_current_user_id(), $meta_name, true ); | |
| 775 | 811 | $data = self::post_with_authenticated_body( 'get_customer_subscriptions', compact( 'customer_id' ) ); |
| 776 | 812 | |
| 777 | - if ( false === $data ) { | |
| 778 | - return false; | |
| 779 | - } | |
| 780 | - | |
| 781 | - return $data->subscriptions; | |
| 813 | + return false === $data ? false : $data->subscriptions; | |
| 782 | 814 | } |
| 783 | 815 | |
| 784 | 816 | /** |
| 785 | 817 | * @param string $event_id |
| 786 | - * @return object|false | |
| 818 | + * | |
| 819 | + * @return false|object | |
| 787 | 820 | */ |
| 788 | 821 | public static function get_event( $event_id ) { |
| 789 | 822 | $event = wp_cache_get( $event_id, 'frm_strp' ); |
| 823 | + | |
| 790 | 824 | if ( is_object( $event ) ) { |
| 791 | 825 | return $event; |
| 792 | 826 | } |
| 793 | 827 | |
| 794 | 828 | $event = self::post_with_authenticated_body( 'get_event', compact( 'event_id' ) ); |
| 829 | + | |
| 795 | 830 | if ( false === $event || empty( $event->event ) ) { |
| 796 | 831 | return false; |
| 797 | 832 | } |
| 798 | 833 | |
| @@ -801,8 +836,9 @@ | ||
| 801 | 836 | } |
| 802 | 837 | |
| 803 | 838 | /** |
| 804 | 839 | * @param string $event_id |
| 840 | + * | |
| 805 | 841 | * @return mixed |
| 806 | 842 | */ |
| 807 | 843 | public static function process_event( $event_id ) { |
| 808 | 844 | return self::post_with_authenticated_body( 'process_event', compact( 'event_id' ) ); |
| @@ -809,20 +845,24 @@ | ||
| 809 | 845 | } |
| 810 | 846 | |
| 811 | 847 | /** |
| 812 | 848 | * @param array $plan |
| 813 | - * @return string|false | |
| 849 | + * | |
| 850 | + * @return false|string | |
| 814 | 851 | */ |
| 815 | 852 | public static function maybe_create_plan( $plan ) { |
| 816 | 853 | $data = self::post_with_authenticated_body( 'maybe_create_plan', compact( 'plan' ) ); |
| 854 | + | |
| 817 | 855 | if ( false === $data || empty( $data->plan_id ) ) { |
| 818 | 856 | return false; |
| 819 | 857 | } |
| 858 | + | |
| 820 | 859 | return $data->plan_id; |
| 821 | 860 | } |
| 822 | 861 | |
| 823 | 862 | /** |
| 824 | 863 | * @param array $plan |
| 864 | + * | |
| 825 | 865 | * @return mixed |
| 826 | 866 | */ |
| 827 | 867 | public static function create_plan( $plan ) { |
| 828 | 868 | return self::post_with_authenticated_body( 'create_plan', compact( 'plan' ) ); |
| @@ -830,14 +870,14 @@ | ||
| 830 | 870 | |
| 831 | 871 | /** |
| 832 | 872 | * @param string $intent_id |
| 833 | 873 | * @param array $data |
| 874 | + * | |
| 834 | 875 | * @return bool |
| 835 | 876 | */ |
| 836 | 877 | public static function update_intent( $intent_id, $data ) { |
| 837 | - $data = self::post_with_authenticated_body( 'update_intent', compact( 'intent_id', 'data' ) ); | |
| 838 | - $success = false !== $data; | |
| 839 | - return $success; | |
| 878 | + $data = self::post_with_authenticated_body( 'update_intent', compact( 'intent_id', 'data' ) ); | |
| 879 | + return false !== $data; | |
| 840 | 880 | } |
| 841 | 881 | |
| 842 | 882 | /** |
| 843 | 883 | * @return array |
| @@ -843,11 +883,13 @@ | ||
| 843 | 883 | * @return array |
| 844 | 884 | */ |
| 845 | 885 | public static function get_unprocessed_event_ids() { |
| 846 | 886 | $data = self::post_with_authenticated_body( 'get_unprocessed_event_ids' ); |
| 887 | + | |
| 847 | 888 | if ( false === $data || empty( $data->event_ids ) ) { |
| 848 | 889 | return array(); |
| 849 | 890 | } |
| 891 | + | |
| 850 | 892 | return $data->event_ids; |
| 851 | 893 | } |
| 852 | 894 | |
| 853 | 895 | /** |
| @@ -857,9 +899,10 @@ | ||
| 857 | 899 | * @since 6.5, introduced in v3.0 of the Stripe add on. |
| 858 | 900 | * |
| 859 | 901 | * @param string $customer_id |
| 860 | 902 | * @param array|false $payment_method_types |
| 861 | - * @return object|string|false | |
| 903 | + * | |
| 904 | + * @return false|object|string | |
| 862 | 905 | */ |
| 863 | 906 | public static function create_setup_intent( $customer_id, $payment_method_types = false ) { |
| 864 | 907 | $charge_data = array( 'customer' => $customer_id ); |
| 865 | 908 | |
| @@ -879,9 +922,10 @@ | ||
| 879 | 922 | * |
| 880 | 923 | * @since 6.5, introduced in v3.0 of the Stripe add on. |
| 881 | 924 | * |
| 882 | 925 | * @param string $setup_id |
| 883 | - * @return object|string|false | |
| 926 | + * | |
| 927 | + * @return false|object|string | |
| 884 | 928 | */ |
| 885 | 929 | public static function get_setup_intent( $setup_id ) { |
| 886 | 930 | return self::post_with_authenticated_body( 'get_setup_intent', compact( 'setup_id' ) ); |
| 887 | 931 | } |
| @@ -898,14 +942,14 @@ | ||
| 898 | 942 | } |
| 899 | 943 | |
| 900 | 944 | $site_identifier = FrmAppHelper::get_post_param( 'site_identifier' ); |
| 901 | 945 | $usage = new FrmUsage(); |
| 902 | - $uuid = $usage->uuid(); | |
| 903 | 946 | |
| 904 | 947 | update_option( $option_name, time() ); |
| 905 | 948 | |
| 906 | - if ( $site_identifier === $uuid ) { | |
| 949 | + if ( $site_identifier === $usage->uuid() ) { | |
| 907 | 950 | wp_send_json_success(); |
| 908 | 951 | } |
| 952 | + | |
| 909 | 953 | wp_send_json_error(); |
| 910 | 954 | } |
| 911 | 955 | } |