PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.5.1
Defender Security – Malware Scanner, Login Security & Firewall v5.5.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / integrations / class-woocommerce.php

class-woocommerce.php in Defender Security – Malware Scanner, Login Security & Firewall 5.5.1, at src/integrations/class-woocommerce.php

109 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles interactions with Woocommerce.
4 *
5 * @package WP_Defender\Integrations
6 */
7
8 namespace WP_Defender\Integrations;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 die;
12 }
13
14 /**
15 * Woocommerce integration module.
16 *
17 * @since 2.6.1
18 * @since 3.3.0 Add locations.
19 */
20 class Woocommerce {
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
27 /**
28 * Check if Woo is activated.
29 *
30 * @return bool
31 */
32 public function is_activated(): bool {
33 return class_exists( 'woocommerce' );
34 }
35
36 /**
37 * Detects if the request is coming from a WooCommerce login context.
38 *
39 * Checks in the following order of reliability:
40 * 1. Direct WooCommerce page detection (most reliable).
41 * 2. WooCommerce endpoint detection.
42 * 3. Form submission detection (more reliable than referer).
43 * 4. AJAX detection.
44 * 5. Fallback: Referer check (least reliable).
45 *
46 * @return bool
47 */
48 public function is_wc_login_context(): bool {
49 if ( ! $this->is_activated() ) {
50 return false;
51 }
52
53 // 1. Direct WooCommerce page detection (most reliable).
54 if ( function_exists( 'is_account_page' ) && is_account_page() ) {
55 return true;
56 }
57
58 // 2. WooCommerce endpoint detection.
59 if ( function_exists( 'is_wc_endpoint_url' ) &&
60 ( is_wc_endpoint_url( 'lost-password' ) || is_wc_endpoint_url( 'customer-logout' )
61 ) ) {
62 return true;
63 }
64
65 // 3. Form submission detection (more reliable than referer).
66 $post_data = defender_get_data_from_request( null, 'p' );
67 if ( isset( $post_data['woocommerce-login-nonce'] ) ||
68 isset( $post_data ['woocommerce_checkout_login'] ) ||
69 ( isset( $post_data ['login'] ) && is_checkout() ) ||
70 isset( $post_data['wc_reset_password'] ) ) {
71 return true;
72 }
73
74 // 4. AJAX detection.
75 if ( wp_doing_ajax() && isset( $post_data['wc-ajax'] ) ) {
76 return true;
77 }
78
79 // 5. Fallback: Referer check (least reliable).
80 $referer = wp_get_referer();
81 if ( false !== $referer && function_exists( 'wc_get_page_id' ) ) {
82 $my_account_page_id = wc_get_page_id( 'myaccount' );
83 if ( $my_account_page_id > 0 ) {
84 $my_account_url = get_permalink( $my_account_page_id );
85 if ( $my_account_url && strpos( $referer, $my_account_url ) === 0 ) {
86 return true;
87 }
88 }
89 }
90
91 return false;
92 }
93
94 /**
95 * Retrieves an array of WooCommerce forms with their respective translations.
96 *
97 * @return array An associative array where the keys are the form identifiers and the values are the translated
98 * form names.
99 */
100 public static function get_forms(): array {
101 return array(
102 self::WOO_LOGIN_FORM => esc_html__( 'Login', 'defender-security' ),
103 self::WOO_REGISTER_FORM => esc_html__( 'Registration', 'defender-security' ),
104 self::WOO_LOST_PASSWORD_FORM => esc_html__( 'Lost Password', 'defender-security' ),
105 self::WOO_CHECKOUT_FORM => esc_html__( 'Checkout', 'defender-security' ),
106 );
107 }
108 }
109