| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPAdminify\Inc\Modules\ActivityLogs\Hooks; |
| 4 |
|
| 5 |
use WPAdminify\Inc\Modules\ActivityLogs\Inc\Hooks_Base; |
| 6 |
|
| 7 |
// no direct access allowed |
| 8 |
if (!defined('ABSPATH')) exit; |
| 9 |
|
| 10 |
class WP_Adminify_Logs_Posts extends Hooks_Base |
| 11 |
{ |
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
parent::__construct(); |
| 15 |
add_action('transition_post_status', [$this, 'hooks_transition_post_status'], 10, 3); |
| 16 |
add_action('delete_post', [$this, 'hooks_delete_post']); |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
protected function _draft_or_post_title($post = 0) |
| 21 |
{ |
| 22 |
$title = esc_html(get_the_title($post)); |
| 23 |
|
| 24 |
if (empty($title)) |
| 25 |
$title = __('(no title)', WP_ADMINIFY_TD); |
| 26 |
|
| 27 |
return $title; |
| 28 |
} |
| 29 |
|
| 30 |
public function hooks_transition_post_status($new_status, $old_status, $post) |
| 31 |
{ |
| 32 |
if ('auto-draft' === $old_status && ('auto-draft' !== $new_status && 'inherit' !== $new_status)) { |
| 33 |
// page created |
| 34 |
$action = 'created'; |
| 35 |
} elseif ('auto-draft' === $new_status || ('new' === $old_status && 'inherit' === $new_status)) { |
| 36 |
// nvm.. ignore it. |
| 37 |
return; |
| 38 |
} elseif ('trash' === $new_status) { |
| 39 |
// page was deleted. |
| 40 |
$action = 'trashed'; |
| 41 |
} elseif ('trash' === $old_status) { |
| 42 |
$action = 'restored'; |
| 43 |
} else { |
| 44 |
// page updated. I guess. |
| 45 |
$action = 'updated'; |
| 46 |
} |
| 47 |
|
| 48 |
if (wp_is_post_revision($post->ID)) |
| 49 |
return; |
| 50 |
|
| 51 |
// Skip for menu items. |
| 52 |
if ('nav_menu_item' === get_post_type($post->ID)) |
| 53 |
return; |
| 54 |
|
| 55 |
adminify_activity_logs( |
| 56 |
array( |
| 57 |
'action' => $action, |
| 58 |
'object_type' => 'Post', |
| 59 |
'object_subtype' => $post->post_type, |
| 60 |
'object_id' => $post->ID, |
| 61 |
'object_name' => $this->_draft_or_post_title($post->ID), |
| 62 |
) |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
public function hooks_delete_post($post_id) |
| 67 |
{ |
| 68 |
if (wp_is_post_revision($post_id)) |
| 69 |
return; |
| 70 |
|
| 71 |
$post = get_post($post_id); |
| 72 |
|
| 73 |
if (!$post) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
if (in_array($post->post_status, array('auto-draft', 'inherit'))) |
| 78 |
return; |
| 79 |
|
| 80 |
// Skip for menu items. |
| 81 |
if ('nav_menu_item' === get_post_type($post->ID)) |
| 82 |
return; |
| 83 |
|
| 84 |
adminify_activity_logs( |
| 85 |
array( |
| 86 |
'action' => 'deleted', |
| 87 |
'object_type' => 'Post', |
| 88 |
'object_subtype' => $post->post_type, |
| 89 |
'object_id' => $post->ID, |
| 90 |
'object_name' => $this->_draft_or_post_title($post->ID), |
| 91 |
) |
| 92 |
); |
| 93 |
} |
| 94 |
} |
| 95 |
|