| 1 |
<?php |
| 2 |
|
| 3 |
class Logtivity_Post extends Logtivity_Abstract_Logger |
| 4 |
{ |
| 5 |
public function registerHooks() |
| 6 |
{ |
| 7 |
add_action( 'transition_post_status', [$this, 'postStatusChanged'], 10, 3); |
| 8 |
add_action( 'wp_trash_post', [$this, 'postWasTrashed'], 10, 1 ); |
| 9 |
add_filter('wp_handle_upload', [$this, 'mediaUploaded'], 10, 2); |
| 10 |
// add_action( 'delete_attachment', [$this, 'mediaDeleted'], 1, 1 ); |
| 11 |
add_action('delete_post', [$this, 'postPermenantelyDeleted'], 10, 1); |
| 12 |
add_filter( 'wp_ajax_save-attachment', [$this, 'mediaMetaUpdated'], -1 ); |
| 13 |
} |
| 14 |
|
| 15 |
public function postStatusChanged($new_status, $old_status, $post) |
| 16 |
{ |
| 17 |
if ($this->shouldIgnore($post)) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
if ($old_status == 'trash') { |
| 22 |
return $this->postWasRestored($post); |
| 23 |
} |
| 24 |
|
| 25 |
if ($old_status != 'publish' && $new_status == 'publish') { |
| 26 |
return $this->postWasPublished($post); |
| 27 |
} |
| 28 |
|
| 29 |
return $this->postWasUpdated($post, $old_status); |
| 30 |
} |
| 31 |
|
| 32 |
public function postWasPublished($post) |
| 33 |
{ |
| 34 |
return Logtivity_Logger::log() |
| 35 |
->setAction($this->getPostTypeLabel($post->ID) . ' Published. [' . $post->post_title . ']') |
| 36 |
->addMeta('Post ID', $post->ID) |
| 37 |
->addMeta('Post Type', $post->post_type) |
| 38 |
->addMeta('Post Status', $post->post_status) |
| 39 |
->addMeta('Post Title', $post->post_title) |
| 40 |
->send(); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Post was updated or created. ignoring certain auto save system actions |
| 45 |
* |
| 46 |
* @param integer $post_id |
| 47 |
* @param WP_Post $post |
| 48 |
* @param bool $update |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function postWasUpdated($post, $old_status) |
| 52 |
{ |
| 53 |
return Logtivity_Logger::log() |
| 54 |
->setAction($this->getPostTypeLabel($post->ID) . ' Updated. [' . $post->post_title . ']') |
| 55 |
->addMeta('Post ID', $post->ID) |
| 56 |
->addMeta('Post Title', $post->post_title) |
| 57 |
->addMeta('Post Type', $post->post_type) |
| 58 |
->addMeta('Post Status', $post->post_status) |
| 59 |
->addMeta('Old Status', $old_status) |
| 60 |
->send(); |
| 61 |
} |
| 62 |
|
| 63 |
public function postWasTrashed($post_id) |
| 64 |
{ |
| 65 |
if (get_post_type($post_id) == 'customize_changeset') { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
return Logtivity_Logger::log() |
| 70 |
->setAction($this->getPostTypeLabel($post_id) . ' Trashed. [' . get_the_title($post_id) . ']') |
| 71 |
->addMeta('Post ID', $post_id) |
| 72 |
->addMeta('Post Type', get_post_type($post_id)) |
| 73 |
->addMeta('Post Title', get_the_title($post_id)) |
| 74 |
->send(); |
| 75 |
} |
| 76 |
|
| 77 |
public function postWasRestored($post) |
| 78 |
{ |
| 79 |
return Logtivity_Logger::log() |
| 80 |
->setAction( |
| 81 |
$this->getPostTypeLabel($post->ID) . ' Restored from Trash. [' . $post->post_title . ']' |
| 82 |
) |
| 83 |
->addMeta('Post ID', $post->ID) |
| 84 |
->addMeta('Post Type', $post->post_type) |
| 85 |
->addMeta('Post Title', $post->post_title) |
| 86 |
->send(); |
| 87 |
} |
| 88 |
|
| 89 |
public function postPermenantelyDeleted($post_id) |
| 90 |
{ |
| 91 |
if ($this->ignoringPostType(get_post_type($post_id))) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
if ($this->ignoringPostTitle(get_the_title($post_id))) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
return Logtivity_Logger::log() |
| 100 |
->setAction( |
| 101 |
$this->getPostTypeLabel($post_id) . ' Permenantly Deleted. [' . get_the_title($post_id) . ']' |
| 102 |
) |
| 103 |
->addMeta('Post ID', $post_id) |
| 104 |
->addMeta('Post Type', get_post_type($post_id)) |
| 105 |
->addMeta('Post Title', get_the_title($post_id)) |
| 106 |
->send(); |
| 107 |
} |
| 108 |
|
| 109 |
public function mediaUploaded($upload, $context) |
| 110 |
{ |
| 111 |
Logtivity_Logger::log() |
| 112 |
->setAction('Attachment Uploaded') |
| 113 |
->addMeta('File', $upload['file']) |
| 114 |
->addMeta('Url', $upload['url']) |
| 115 |
->addMeta('Type', $upload['type']) |
| 116 |
->addMeta('Context', $context) |
| 117 |
->send(); |
| 118 |
|
| 119 |
return $upload; |
| 120 |
} |
| 121 |
|
| 122 |
// public function mediaDeleted($post_id) |
| 123 |
// { |
| 124 |
// return Logtivity_Logger::log() |
| 125 |
// ->setAction('Attachment was deleted.') |
| 126 |
// ->addMeta('Post ID: ' . $post_id) |
| 127 |
// ->send(); |
| 128 |
// } |
| 129 |
|
| 130 |
public function mediaMetaUpdated() |
| 131 |
{ |
| 132 |
$post_id = absint($_POST['id']); |
| 133 |
|
| 134 |
if ($post_id) { |
| 135 |
|
| 136 |
Logtivity_Logger::log() |
| 137 |
->setAction('Attachment Meta Updated.') |
| 138 |
->addMeta("Media ID", $post_id) |
| 139 |
->send(); |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
return $post; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
$Logtivity_Post = new Logtivity_Post; |