| 1 |
<?php |
| 2 |
|
| 3 |
class Logtivity_Post extends Logtivity_Abstract_Logger |
| 4 |
{ |
| 5 |
protected $disableUpdateLog = false; |
| 6 |
|
| 7 |
protected $action; |
| 8 |
|
| 9 |
public function registerHooks() |
| 10 |
{ |
| 11 |
add_action( 'transition_post_status', [$this, 'postStatusChanged'], 10, 3); |
| 12 |
add_action( 'save_post', array( $this, 'postWasUpdated' ), 10, 3 ); |
| 13 |
add_action( 'wp_trash_post', [$this, 'postWasTrashed'], 10, 1 ); |
| 14 |
add_filter('wp_handle_upload', [$this, 'mediaUploaded'], 10, 2); |
| 15 |
add_action('delete_post', [$this, 'postPermanentlyDeleted'], 10, 1); |
| 16 |
add_filter( 'wp_ajax_save-attachment', [$this, 'mediaMetaUpdated'], -1 ); |
| 17 |
} |
| 18 |
|
| 19 |
public function postStatusChanged($new_status, $old_status, $post) |
| 20 |
{ |
| 21 |
if ($this->shouldIgnore($post)) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
if ($old_status == 'trash') { |
| 26 |
$this->disableUpdateLog = true; |
| 27 |
return $this->postWasRestored($post); |
| 28 |
} |
| 29 |
|
| 30 |
if ($old_status != 'publish' && $new_status == 'publish') { |
| 31 |
$this->action = $this->getPostTypeLabel($post->ID) . ' Published'; |
| 32 |
} |
| 33 |
|
| 34 |
if ($old_status == 'publish' && $new_status == 'draft') { |
| 35 |
$this->action = $this->getPostTypeLabel($post->ID) . ' Unpublished'; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Post was updated or created. ignoring certain auto save system actions |
| 41 |
* |
| 42 |
* @param integer $post_id |
| 43 |
* @param WP_Post $post |
| 44 |
* @param bool $update |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
public function postWasUpdated($post_id, $post, $update) |
| 48 |
{ |
| 49 |
if ($this->disableUpdateLog) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
if ($this->shouldIgnore($post)) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
if ($this->loggedRecently($post->ID)) { |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
$log = Logtivity_Logger::log() |
| 62 |
->setAction($this->action ?? $this->getPostTypeLabel($post->ID) . ' Updated') |
| 63 |
->setContext($post->post_title) |
| 64 |
->addMeta('Post ID', $post->ID) |
| 65 |
->addMeta('Post Title', $post->post_title) |
| 66 |
->addMeta('Post Type', $post->post_type) |
| 67 |
->addMeta('Post Status', $post->post_status); |
| 68 |
|
| 69 |
if ($revision = $this->getRevision($post_id)) { |
| 70 |
$log->addMeta('View Revision', $revision); |
| 71 |
} |
| 72 |
|
| 73 |
$log->send(); |
| 74 |
|
| 75 |
update_post_meta($post_id, 'logtivity_last_logged', (new \DateTime())->format('Y-m-d H:i:s')); |
| 76 |
} |
| 77 |
|
| 78 |
private function getRevision( $post_id ) |
| 79 |
{ |
| 80 |
$revisions = wp_get_post_revisions( $post_id ); |
| 81 |
if ( ! empty( $revisions ) ) { |
| 82 |
$revision = array_shift( $revisions ); |
| 83 |
return $this->getRevisionLink( $revision->ID ); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
private function getRevisionLink( $revision_id ) |
| 88 |
{ |
| 89 |
return ! empty( $revision_id ) ? add_query_arg( 'revision', $revision_id, admin_url( 'revision.php' ) ) : null; |
| 90 |
} |
| 91 |
|
| 92 |
public function postWasTrashed($post_id) |
| 93 |
{ |
| 94 |
if (get_post_type($post_id) == 'customize_changeset') { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
return Logtivity_Logger::log() |
| 99 |
->setAction($this->getPostTypeLabel($post_id) . ' Trashed') |
| 100 |
->setContext(logtivity_get_the_title($post_id)) |
| 101 |
->addMeta('Post ID', $post_id) |
| 102 |
->addMeta('Post Type', get_post_type($post_id)) |
| 103 |
->addMeta('Post Title', logtivity_get_the_title($post_id)) |
| 104 |
->send(); |
| 105 |
} |
| 106 |
|
| 107 |
public function postWasRestored($post) |
| 108 |
{ |
| 109 |
return Logtivity_Logger::log() |
| 110 |
->setAction( |
| 111 |
$this->getPostTypeLabel($post->ID) . ' Restored from Trash' |
| 112 |
) |
| 113 |
->setContext($post->post_title) |
| 114 |
->addMeta('Post ID', $post->ID) |
| 115 |
->addMeta('Post Type', $post->post_type) |
| 116 |
->addMeta('Post Title', $post->post_title) |
| 117 |
->send(); |
| 118 |
} |
| 119 |
|
| 120 |
public function postPermanentlyDeleted($post_id) |
| 121 |
{ |
| 122 |
if ($this->ignoringPostType(get_post_type($post_id))) { |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
if ($this->ignoringPostTitle(logtivity_get_the_title($post_id))) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
return Logtivity_Logger::log() |
| 131 |
->setAction( |
| 132 |
$this->getPostTypeLabel($post_id) . ' Permanently Deleted' |
| 133 |
) |
| 134 |
->setContext(logtivity_get_the_title($post_id)) |
| 135 |
->addMeta('Post ID', $post_id) |
| 136 |
->addMeta('Post Type', get_post_type($post_id)) |
| 137 |
->addMeta('Post Title', logtivity_get_the_title($post_id)) |
| 138 |
->send(); |
| 139 |
} |
| 140 |
|
| 141 |
public function mediaUploaded($upload, $context) |
| 142 |
{ |
| 143 |
Logtivity_Logger::log() |
| 144 |
->setAction('Attachment Uploaded') |
| 145 |
->setContext(basename($upload['file'])) |
| 146 |
->addMeta('Url', $upload['url']) |
| 147 |
->addMeta('Type', $upload['type']) |
| 148 |
->addMeta('Context', $context) |
| 149 |
->send(); |
| 150 |
|
| 151 |
return $upload; |
| 152 |
} |
| 153 |
|
| 154 |
public function mediaMetaUpdated() |
| 155 |
{ |
| 156 |
$post_id = absint($_POST['id']); |
| 157 |
|
| 158 |
if ($post_id) { |
| 159 |
Logtivity_Logger::log() |
| 160 |
->setAction('Attachment Meta Updated.') |
| 161 |
->addMeta("Media ID", $post_id) |
| 162 |
->addMeta("Changes", ( isset($_POST['changes']) ? $_POST['changes'] : null)) |
| 163 |
->send(); |
| 164 |
} |
| 165 |
|
| 166 |
return $post; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
$Logtivity_Post = new Logtivity_Post; |