PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.2
Defender Security – Malware Scanner, Login Security & Firewall v6.2.2
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 / component / audit / class-feature-audit.php

class-feature-audit.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.2, at src/component/audit/class-feature-audit.php

90 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles defender feature activation/deactivation events for audit logging.
4 *
5 * @package WP_Defender\Component\Audit
6 */
7
8 namespace WP_Defender\Component\Audit;
9
10 use WP_Defender\Traits\User;
11 use WP_Defender\Model\Audit_Log;
12
13 /**
14 * Handles defender feature activation/deactivation events for audit logging.
15 *
16 * @since 5.8.0
17 */
18 class Feature_Audit extends Audit_Event {
19
20 use User;
21
22 const CONTEXT_DEFENDER_FEATURE = 'feature';
23 const ACTION_FEATURE_DEACTIVATED = 'feature_deactivated';
24
25 /**
26 * Return an array of hooks.
27 *
28 * @return array
29 */
30 public function get_hooks(): array {
31 return array(
32 'update_site_option' => array(
33 'args' => array( 'option', 'value', 'old_value', 'network_id' ),
34 'event_type' => Audit_Log::EVENT_TYPE_SETTINGS,
35 'context' => self::CONTEXT_DEFENDER_FEATURE,
36 'action_type' => self::ACTION_FEATURE_DEACTIVATED,
37 'callback' => array( $this, 'audit_logging' ),
38 ),
39 );
40 }
41
42 /**
43 * Callback for audit logging when audit logging feature is deactivated.
44 *
45 * @param string $hook_name The name of the hook.
46 * @param array $params The parameters passed to the hook.
47 *
48 * @return array|false
49 */
50 public function audit_logging( string $hook_name, array $params ): bool|array {
51 $option = $params['option'] ?? '';
52 if ( 'wd_audit_settings' !== $option ) {
53 return false;
54 }
55
56 $old_raw = $params['old_value'] ?? null;
57 $new_raw = $params['value'] ?? null;
58 $old_value = is_string( $old_raw ) ? json_decode( $old_raw, true ) : ( is_array( $old_raw ) ? $old_raw : null );
59 $new_value = is_string( $new_raw ) ? json_decode( $new_raw, true ) : ( is_array( $new_raw ) ? $new_raw : null );
60 $blog_name = is_multisite() ? '[' . get_bloginfo( 'name' ) . ']' : '';
61 $username = $this->get_source_of_action();
62 if ( is_array( $old_value ) && isset( $old_value['enabled'] ) ) {
63 $old_enabled = (bool) $old_value['enabled'];
64 } else {
65 $old_enabled = false;
66 }
67
68 if ( is_array( $new_value ) && isset( $new_value['enabled'] ) ) {
69 $new_enabled = (bool) $new_value['enabled'];
70 } else {
71 $new_enabled = false;
72 }
73 // Only log when the feature is deactivated.
74 if ( $old_enabled && ! $new_enabled ) {
75 return array(
76 sprintf(
77 /* translators: 1: Site name, 2: Username */
78 esc_html__( '%1$s Audit Logging feature deactivated by %2$s', 'defender-security' ),
79 $blog_name,
80 $username
81 ),
82 self::CONTEXT_DEFENDER_FEATURE,
83 self::ACTION_FEATURE_DEACTIVATED,
84 );
85 }
86
87 return false;
88 }
89 }
90