| @@ -1,5 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Handles interactions with Woocommerce. | |
| 4 | + * | |
| 5 | + * @package WP_Defender\Integrations | |
| 6 | + */ | |
| 2 | 7 | |
| 3 | 8 | namespace WP_Defender\Integrations; |
| 4 | 9 | |
| 5 | 10 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -7,21 +12,105 @@ | ||
| 7 | 12 | } |
| 8 | 13 | |
| 9 | 14 | /** |
| 10 | 15 | * Woocommerce integration module. |
| 11 | - * Class Woocommerce | |
| 12 | 16 | * |
| 13 | 17 | * @since 2.6.1 |
| 14 | - * @package WP_Defender\Integrations | |
| 18 | + * @since 3.3.0 Add locations. | |
| 15 | 19 | */ |
| 16 | 20 | class Woocommerce { |
| 17 | 21 | |
| 22 | + public const WOO_LOGIN_FORM = 'woo_login', | |
| 23 | + WOO_REGISTER_FORM = 'woo_register', | |
| 24 | + WOO_LOST_PASSWORD_FORM = 'woo_lost_password', | |
| 25 | + WOO_CHECKOUT_FORM = 'woo_checkout'; | |
| 26 | + | |
| 18 | 27 | /** |
| 19 | 28 | * Check if Woo is activated. |
| 20 | 29 | * |
| 21 | 30 | * @return bool |
| 22 | 31 | */ |
| 23 | - public function is_activated() { | |
| 32 | + public function is_activated(): bool { | |
| 33 | + return class_exists( 'woocommerce' ); | |
| 34 | + } | |
| 24 | 35 | |
| 25 | - return class_exists( 'woocommerce' ); | |
| 36 | + /** | |
| 37 | + * Detects if the request is coming from a WooCommerce login context. | |
| 38 | + * | |
| 39 | + * @return bool | |
| 40 | + */ | |
| 41 | + public function is_wc_login_context(): bool { | |
| 42 | + if ( ! $this->is_activated() ) { | |
| 43 | + return false; | |
| 44 | + } | |
| 45 | + | |
| 46 | + // Problem: REST_REQUEST constant is false during Store API requests, so we check the rest_route parameter instead. | |
| 47 | + $request_uri = defender_get_data_from_request( 'REQUEST_URI', 's' ); | |
| 48 | + if ( '' !== $request_uri && str_contains( $request_uri, 'rest_route=/wc/store/v1/checkout' ) ) { | |
| 49 | + return true; | |
| 50 | + } | |
| 51 | + | |
| 52 | + $post_data = defender_get_data_from_request( null, 'p' ); | |
| 53 | + | |
| 54 | + if ( 0 === count( $post_data ) ) { | |
| 55 | + return false; | |
| 56 | + } | |
| 57 | + | |
| 58 | + if ( | |
| 59 | + isset( $post_data['woocommerce-login-nonce'] ) || | |
| 60 | + isset( $post_data['woocommerce-register-nonce'] ) || | |
| 61 | + isset( $post_data['woocommerce_checkout_login'] ) || | |
| 62 | + ( isset( $post_data['login'] ) && is_checkout() ) || | |
| 63 | + ( isset( $post_data['register'] ) && isset( $post_data['email'] ) ) || | |
| 64 | + isset( $post_data['wc_reset_password'] ) | |
| 65 | + ) { | |
| 66 | + return true; | |
| 67 | + } | |
| 68 | + | |
| 69 | + $referer = wp_get_referer(); | |
| 70 | + if ( $referer && function_exists( 'wc_get_page_id' ) ) { | |
| 71 | + $my_account_page_id = wc_get_page_id( 'myaccount' ); | |
| 72 | + if ( $my_account_page_id > 0 ) { | |
| 73 | + $my_account_url = get_permalink( $my_account_page_id ); | |
| 74 | + | |
| 75 | + return str_starts_with( $referer, $my_account_url ); | |
| 76 | + } | |
| 77 | + } | |
| 78 | + | |
| 79 | + return false; | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * Retrieves an array of WooCommerce forms with their respective translations. | |
| 84 | + * | |
| 85 | + * @return array An associative array where the keys are the form identifiers and the values are the translated | |
| 86 | + * form names. | |
| 87 | + */ | |
| 88 | + public static function get_forms(): array { | |
| 89 | + return array( | |
| 90 | + self::WOO_LOGIN_FORM => esc_html__( 'Login', 'defender-security' ), | |
| 91 | + self::WOO_LOST_PASSWORD_FORM => esc_html__( 'Lost Password', 'defender-security' ), | |
| 92 | + self::WOO_REGISTER_FORM => esc_html__( 'Registration', 'defender-security' ), | |
| 93 | + self::WOO_CHECKOUT_FORM => esc_html__( 'Checkout', 'defender-security' ), | |
| 94 | + ); | |
| 95 | + } | |
| 96 | + | |
| 97 | + /** | |
| 98 | + * Detects if current request originates from WooCommerce code path. | |
| 99 | + * | |
| 100 | + * @return bool | |
| 101 | + */ | |
| 102 | + public function is_woocommerce_page(): bool { | |
| 103 | + if ( ! $this->is_activated() ) { | |
| 104 | + return false; | |
| 105 | + } | |
| 106 | + | |
| 107 | + $traces = debug_backtrace(); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace | |
| 108 | + foreach ( $traces as $trace ) { | |
| 109 | + if ( isset( $trace['file'] ) && str_contains( $trace['file'], 'woocommerce' ) ) { | |
| 110 | + return true; | |
| 111 | + } | |
| 112 | + } | |
| 113 | + | |
| 114 | + return false; | |
| 26 | 115 | } |
| 27 | 116 | } |