| 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 post related action. |
| 10 |
*/ |
| 11 |
class P_Event_Posts 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( 'transition_post_status', [ &$this, 'transition_post_status' ], 10, 3 ); |
| 20 |
add_action( 'delete_post', [ &$this, 'delete_post' ] ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Determine the title. |
| 25 |
* |
| 26 |
* @param integer $post |
| 27 |
* @return string |
| 28 |
*/ |
| 29 |
protected function _draft_or_post_title( $post = 0 ) { |
| 30 |
$title = esc_html( get_the_title( $post ) ); |
| 31 |
if ( empty( $title ) ) { |
| 32 |
return __( '(no title)', 'patchstack' ); |
| 33 |
} |
| 34 |
|
| 35 |
return $title; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* When the status of a post is transitioned. |
| 40 |
* |
| 41 |
* @param string $new_status |
| 42 |
* @param string $old_status |
| 43 |
* @param object $post |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public function transition_post_status( $new_status, $old_status, $post ) { |
| 47 |
// Determine the action. |
| 48 |
if ( $old_status === 'auto-draft' && ( $new_status !== 'auto-draft' && $new_status !== 'inherit' ) ) { |
| 49 |
$action = 'created'; |
| 50 |
} elseif ( $new_status === 'auto-draft' || ( $old_status === 'new' && $new_status === 'inherit' ) ) { |
| 51 |
return; |
| 52 |
} elseif ( $new_status === 'trash' ) { |
| 53 |
$action = 'trashed'; |
| 54 |
} elseif ( $old_status === 'trash' ) { |
| 55 |
$action = 'restored'; |
| 56 |
} else { |
| 57 |
$action = 'updated'; |
| 58 |
} |
| 59 |
|
| 60 |
// No need to log revisions. |
| 61 |
if ( wp_is_post_revision( $post->ID ) ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
// Skip for menu items. |
| 66 |
if ( get_post_type( $post->ID ) === 'nav_menu_item' ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
$this->insert( |
| 71 |
[ |
| 72 |
'object' => 'post', |
| 73 |
'object_id' => $post->ID, |
| 74 |
'action' => $action, |
| 75 |
'object_name' => $this->_draft_or_post_title( $post->ID ), |
| 76 |
] |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* When a post is deleted. |
| 82 |
* |
| 83 |
* @param integer $post_id |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
public function delete_post( $post_id ) { |
| 87 |
if ( wp_is_post_revision( $post_id ) ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
$post = get_post( $post_id ); |
| 92 |
if ( in_array( $post->post_status, [ 'auto-draft', 'inherit' ] ) ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
// Skip for menu items. |
| 97 |
if ( get_post_type( $post->ID ) === 'nav_menu_item' ) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
$this->insert( |
| 102 |
[ |
| 103 |
'object' => 'post', |
| 104 |
'object_id' => $post->ID, |
| 105 |
'action' => 'deleted', |
| 106 |
'object_name' => $this->_draft_or_post_title( $post->ID ), |
| 107 |
] |
| 108 |
); |
| 109 |
} |
| 110 |
} |
| 111 |
|