| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles media-related audit events in WordPress. |
| 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 |
* Handle media-related audit events such as uploading, updating, and deleting media files. |
| 15 |
*/ |
| 16 |
class Media_Audit extends Audit_Event { |
| 17 |
|
| 18 |
use User; |
| 19 |
|
| 20 |
/** |
| 21 |
* Constant for the 'uploaded' action type. |
| 22 |
*/ |
| 23 |
public const ACTION_UPLOADED = 'uploaded'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Returns an array of hooks associated with media actions. |
| 27 |
* This method maps WordPress hooks to their respective event types, action types, and other parameters needed for |
| 28 |
* logging. |
| 29 |
* |
| 30 |
* @return array An associative array where keys are WordPress hooks and values are arrays detailing the hook |
| 31 |
* handling. |
| 32 |
*/ |
| 33 |
public function get_hooks(): array { |
| 34 |
return array( |
| 35 |
'add_attachment' => array( |
| 36 |
'args' => array( 'post_ID' ), |
| 37 |
'event_type' => Audit_Log::EVENT_TYPE_MEDIA, |
| 38 |
'action_type' => self::ACTION_UPLOADED, |
| 39 |
'text' => sprintf( |
| 40 |
/* translators: 1: Blog name, 2: Source of action. For e.g. Hub or a logged-in user, 3: File path */ |
| 41 |
esc_html__( '%1$s %2$s uploaded a file: "%3$s" to Media Library', 'defender-security' ), |
| 42 |
'{{blog_name}}', |
| 43 |
'{{wp_user}}', |
| 44 |
'{{file_path}}' |
| 45 |
), |
| 46 |
'program_args' => array( |
| 47 |
'file_path' => array( |
| 48 |
'callable' => 'get_post_meta', |
| 49 |
'params' => array( |
| 50 |
'{{post_ID}}', |
| 51 |
'_wp_attached_file', |
| 52 |
true, |
| 53 |
), |
| 54 |
), |
| 55 |
'mime_type' => array( |
| 56 |
'callable' => array( self::class, 'get_mime_type' ), |
| 57 |
'params' => array( |
| 58 |
'{{post_ID}}', |
| 59 |
), |
| 60 |
), |
| 61 |
), |
| 62 |
'context' => '{{mime_type}}', |
| 63 |
), |
| 64 |
'attachment_updated' => array( |
| 65 |
'args' => array( 'post_ID' ), |
| 66 |
'action_type' => self::ACTION_UPDATED, |
| 67 |
'event_type' => Audit_Log::EVENT_TYPE_MEDIA, |
| 68 |
'text' => sprintf( |
| 69 |
/* translators: 1: Blog name, 2: Source of action. For e.g. Hub or a logged-in user, 3: File path */ |
| 70 |
esc_html__( '%1$s %2$s updated a file: "%3$s" from Media Library', 'defender-security' ), |
| 71 |
'{{blog_name}}', |
| 72 |
'{{wp_user}}', |
| 73 |
'{{file_path}}' |
| 74 |
), |
| 75 |
'program_args' => array( |
| 76 |
'file_path' => array( |
| 77 |
'callable' => 'get_post_meta', |
| 78 |
'params' => array( |
| 79 |
'{{post_ID}}', |
| 80 |
'_wp_attached_file', |
| 81 |
true, |
| 82 |
), |
| 83 |
), |
| 84 |
'mime_type' => array( |
| 85 |
'callable' => array( self::class, 'get_mime_type' ), |
| 86 |
'params' => array( |
| 87 |
'{{post_ID}}', |
| 88 |
), |
| 89 |
), |
| 90 |
), |
| 91 |
'context' => '{{mime_type}}', |
| 92 |
), |
| 93 |
'delete_attachment' => array( |
| 94 |
'args' => array( 'post_ID' ), |
| 95 |
'action_type' => self::ACTION_DELETED, |
| 96 |
'event_type' => Audit_Log::EVENT_TYPE_MEDIA, |
| 97 |
'text' => sprintf( |
| 98 |
/* translators: 1: Blog name, 2: Source of action. For e.g. Hub or a logged-in user, 3: File path */ |
| 99 |
esc_html__( '%1$s %2$s deleted a file: "%3$s" from Media Library', 'defender-security' ), |
| 100 |
'{{blog_name}}', |
| 101 |
'{{wp_user}}', |
| 102 |
'{{file_path}}' |
| 103 |
), |
| 104 |
'program_args' => array( |
| 105 |
'file_path' => array( |
| 106 |
'callable' => 'get_post_meta', |
| 107 |
'params' => array( |
| 108 |
'{{post_ID}}', |
| 109 |
'_wp_attached_file', |
| 110 |
true, |
| 111 |
), |
| 112 |
), |
| 113 |
'mime_type' => array( |
| 114 |
'callable' => array( self::class, 'get_mime_type' ), |
| 115 |
'params' => array( |
| 116 |
'{{post_ID}}', |
| 117 |
), |
| 118 |
), |
| 119 |
), |
| 120 |
'context' => '{{mime_type}}', |
| 121 |
), |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Provides a dictionary for translating action types into human-readable formats. |
| 127 |
* This method is used to provide localized strings for action types. |
| 128 |
* |
| 129 |
* @return array An associative array where keys are action types and values are their localized strings. |
| 130 |
*/ |
| 131 |
public function dictionary(): array { |
| 132 |
return array( |
| 133 |
self::ACTION_UPLOADED => esc_html__( 'Uploaded', 'defender-security' ), |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Retrieves the MIME type of file based on its post ID. |
| 139 |
* This method uses the WordPress function get_post_meta to fetch the file path from post meta and then determines |
| 140 |
* the MIME type. |
| 141 |
* |
| 142 |
* @param int $post_ID The ID of the post to retrieve the MIME type for. |
| 143 |
* |
| 144 |
* @return string The MIME type of the file. |
| 145 |
*/ |
| 146 |
public function get_mime_type( $post_ID ) { |
| 147 |
$file_path = get_post_meta( $post_ID, '_wp_attached_file', true ); |
| 148 |
|
| 149 |
return pathinfo( $file_path, PATHINFO_EXTENSION ); |
| 150 |
} |
| 151 |
} |
| 152 |
|