| 1 |
<?php |
| 2 |
|
| 3 |
// Do not allow the file to be called directly. |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* This class is used to log any attachment related event. |
| 10 |
*/ |
| 11 |
class P_Event_Attachment extends P_Event_Log { |
| 12 |
|
| 13 |
/** |
| 14 |
* Hook into the actions so we can log it. |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
public function __construct() { |
| 19 |
add_action( 'add_attachment', [ &$this, 'addAttachment' ] ); |
| 20 |
add_action( 'edit_attachment', [ &$this, 'editAttachment' ] ); |
| 21 |
add_action( 'delete_attachment', [ &$this, 'deleteAttachment' ] ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Log the action regarding the attachment. |
| 26 |
* |
| 27 |
* @param string $action |
| 28 |
* @param integer $attachment_id |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
protected function _addLogAttachment( $action, $attachment_id ) { |
| 32 |
$attachment = get_post( $attachment_id ); |
| 33 |
$this->insert( |
| 34 |
[ |
| 35 |
'action' => $action, |
| 36 |
'object' => 'attachment', |
| 37 |
'object_id' => $attachment_id, |
| 38 |
'object_name' => esc_html( get_the_title( $attachment->ID ) ), |
| 39 |
] |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* When an attachment is deleted. |
| 45 |
* |
| 46 |
* @param integer $attachment_id |
| 47 |
* @return void |
| 48 |
*/ |
| 49 |
public function deleteAttachment( $attachment_id ) { |
| 50 |
$this->_addLogAttachment( 'deleted', $attachment_id ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* When an attachment is modified. |
| 55 |
* |
| 56 |
* @param integer $attachment_id |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function editAttachment( $attachment_id ) { |
| 60 |
$this->_addLogAttachment( 'updated', $attachment_id ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* When an attachment is added. |
| 65 |
* |
| 66 |
* @param integer $attachment_id |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function addAttachment( $attachment_id ) { |
| 70 |
$this->_addLogAttachment( 'added', $attachment_id ); |
| 71 |
} |
| 72 |
} |
| 73 |
|