| 1 |
<?php |
| 2 |
/** |
| 3 |
* Author: Hoang Ngo |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WP_Defender\Component\Audit; |
| 7 |
|
| 8 |
use WP_Defender\Traits\User; |
| 9 |
use WP_Defender\Model\Audit_Log; |
| 10 |
|
| 11 |
class Media_Audit extends Audit_Event { |
| 12 |
use User; |
| 13 |
|
| 14 |
public const ACTION_UPLOADED = 'uploaded'; |
| 15 |
|
| 16 |
public function get_hooks() { |
| 17 |
|
| 18 |
return array( |
| 19 |
'add_attachment' => array( |
| 20 |
'args' => array( 'post_ID' ), |
| 21 |
'event_type' => Audit_Log::EVENT_TYPE_MEDIA, |
| 22 |
'action_type' => self::ACTION_UPLOADED, |
| 23 |
'text' => sprintf( |
| 24 |
/* translators: */ |
| 25 |
__( '%1$s %2$s uploaded a file: "%3$s" to Media Library', 'wpdef' ), |
| 26 |
'{{blog_name}}', |
| 27 |
'{{wp_user}}', |
| 28 |
'{{file_path}}' |
| 29 |
), |
| 30 |
'program_args' => array( |
| 31 |
'file_path' => array( |
| 32 |
'callable' => 'get_post_meta', |
| 33 |
'params' => array( |
| 34 |
'{{post_ID}}', |
| 35 |
'_wp_attached_file', |
| 36 |
true, |
| 37 |
), |
| 38 |
), |
| 39 |
'mime_type' => array( |
| 40 |
'callable' => array( self::class, 'get_mime_type' ), |
| 41 |
'params' => array( |
| 42 |
'{{post_ID}}', |
| 43 |
), |
| 44 |
), |
| 45 |
), |
| 46 |
'context' => '{{mime_type}}', |
| 47 |
), |
| 48 |
'attachment_updated' => array( |
| 49 |
'args' => array( 'post_ID' ), |
| 50 |
'action_type' => self::ACTION_UPDATED, |
| 51 |
'event_type' => Audit_Log::EVENT_TYPE_MEDIA, |
| 52 |
'text' => sprintf( |
| 53 |
/* translators: */ |
| 54 |
__( '%1$s %2$s updated a file: "%3$s" from Media Library', 'wpdef' ), |
| 55 |
'{{blog_name}}', |
| 56 |
'{{wp_user}}', |
| 57 |
'{{file_path}}' |
| 58 |
), |
| 59 |
'program_args' => array( |
| 60 |
'file_path' => array( |
| 61 |
'callable' => 'get_post_meta', |
| 62 |
'params' => array( |
| 63 |
'{{post_ID}}', |
| 64 |
'_wp_attached_file', |
| 65 |
true, |
| 66 |
), |
| 67 |
), |
| 68 |
'mime_type' => array( |
| 69 |
'callable' => array( self::class, 'get_mime_type' ), |
| 70 |
'params' => array( |
| 71 |
'{{post_ID}}', |
| 72 |
), |
| 73 |
), |
| 74 |
), |
| 75 |
'context' => '{{mime_type}}', |
| 76 |
), |
| 77 |
'delete_attachment' => array( |
| 78 |
'args' => array( 'post_ID' ), |
| 79 |
'action_type' => self::ACTION_DELETED, |
| 80 |
'event_type' => Audit_Log::EVENT_TYPE_MEDIA, |
| 81 |
'text' => sprintf( |
| 82 |
/* translators: */ |
| 83 |
__( '%1$s %2$s deleted a file: "%3$s" from Media Library', 'wpdef' ), |
| 84 |
'{{blog_name}}', |
| 85 |
'{{wp_user}}', |
| 86 |
'{{file_path}}' |
| 87 |
), |
| 88 |
'program_args' => array( |
| 89 |
'file_path' => array( |
| 90 |
'callable' => 'get_post_meta', |
| 91 |
'params' => array( |
| 92 |
'{{post_ID}}', |
| 93 |
'_wp_attached_file', |
| 94 |
true, |
| 95 |
), |
| 96 |
), |
| 97 |
'mime_type' => array( |
| 98 |
'callable' => array( self::class, 'get_mime_type' ), |
| 99 |
'params' => array( |
| 100 |
'{{post_ID}}', |
| 101 |
), |
| 102 |
), |
| 103 |
), |
| 104 |
'context' => '{{mime_type}}', |
| 105 |
), |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
public function dictionary() { |
| 110 |
|
| 111 |
return array( |
| 112 |
self::ACTION_UPLOADED => esc_html__( 'Uploaded', 'wpdef' ), |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
public function get_mime_type( $post_ID ) { |
| 117 |
$file_path = get_post_meta( $post_ID, '_wp_attached_file', true ); |
| 118 |
|
| 119 |
return pathinfo( $file_path, PATHINFO_EXTENSION ); |
| 120 |
} |
| 121 |
} |
| 122 |
|