PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 1.18.0
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v1.18.0
3.3.9 3.3.8 trunk 1.0 1.1.0 1.10.0 1.11.0 1.11.1 1.12.0 1.13.0 1.14.0 1.15.0 1.16.0 1.17.0 1.17.1 1.18.0 1.19.0 1.2.0 1.20.0 1.20.1 1.3.0 1.3.1 1.4.0 1.5.0 1.6.0 All 67 releases
logtivity / Logs / Core / Logtivity_Post.php

Logtivity_Post.php in Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity 1.18.0, at Logs/Core/Logtivity_Post.php

182 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 return;
33 }
34
35 if ($old_status == 'publish' && $new_status == 'draft') {
36 $this->action = $this->getPostTypeLabel($post->ID) . ' Unpublished';
37 return;
38 }
39
40 if ($old_status != $new_status) {
41 Logtivity_Logger::log()
42 ->setAction(
43 $this->getPostTypeLabel($post->ID) . ' Status changed from '.$old_status.' to '.$new_status
44 )
45 ->setContext($post->post_title)
46 ->setPostType($post->post_type)
47 ->setPostId($post->ID)
48 ->send();
49 }
50
51 }
52
53 /**
54 * Post was updated or created. ignoring certain auto save system actions
55 *
56 * @param integer $post_id
57 * @param WP_Post $post
58 * @param bool $update
59 * @return void
60 */
61 public function postWasUpdated($post_id, $post, $update)
62 {
63 if ($this->disableUpdateLog) {
64 return;
65 }
66
67 if ($this->shouldIgnore($post)) {
68 return;
69 }
70
71 if ($this->loggedRecently($post->ID)) {
72 return true;
73 }
74
75 $revision = $this->getRevision($post_id);
76
77 Logtivity_Logger::log()
78 ->setAction($this->action ?? $this->getPostTypeLabel($post->ID) . ' Updated')
79 ->setContext($post->post_title)
80 ->setPostType($post->post_type)
81 ->setPostId($post->ID)
82 ->addMeta('Post Title', $post->post_title)
83 ->addMeta('Post Status', $post->post_status)
84 ->addMetaIf($revision, 'View Revision', $revision)
85 ->send();
86
87 update_post_meta($post_id, 'logtivity_last_logged', (new \DateTime())->format('Y-m-d H:i:s'));
88 }
89
90 private function getRevision( $post_id )
91 {
92 $revisions = wp_get_post_revisions( $post_id );
93 if ( ! empty( $revisions ) ) {
94 $revision = array_shift( $revisions );
95 return $this->getRevisionLink( $revision->ID );
96 }
97 }
98
99 private function getRevisionLink( $revision_id )
100 {
101 return ! empty( $revision_id ) ? add_query_arg( 'revision', $revision_id, admin_url( 'revision.php' ) ) : null;
102 }
103
104 public function postWasTrashed($post_id)
105 {
106 if (get_post_type($post_id) == 'customize_changeset') {
107 return;
108 }
109
110 return Logtivity_Logger::log()
111 ->setAction($this->getPostTypeLabel($post_id) . ' Trashed')
112 ->setContext(logtivity_get_the_title($post_id))
113 ->setPostType(get_post_type($post_id))
114 ->setPostId($post_id)
115 ->addMeta('Post Title', logtivity_get_the_title($post_id))
116 ->send();
117 }
118
119 public function postWasRestored($post)
120 {
121 return Logtivity_Logger::log()
122 ->setAction(
123 $this->getPostTypeLabel($post->ID) . ' Restored from Trash'
124 )
125 ->setContext($post->post_title)
126 ->setPostType($post->post_type)
127 ->setPostId($post->ID)
128 ->addMeta('Post Title', $post->post_title)
129 ->send();
130 }
131
132 public function postPermanentlyDeleted($post_id)
133 {
134 if ($this->ignoringPostType(get_post_type($post_id))) {
135 return;
136 }
137
138 if ($this->ignoringPostTitle(logtivity_get_the_title($post_id))) {
139 return;
140 }
141
142 return Logtivity_Logger::log()
143 ->setAction(
144 $this->getPostTypeLabel($post_id) . ' Permanently Deleted'
145 )
146 ->setContext(logtivity_get_the_title($post_id))
147 ->setPostType(get_post_type($post_id))
148 ->setPostId($post_id)
149 ->addMeta('Post Title', logtivity_get_the_title($post_id))
150 ->send();
151 }
152
153 public function mediaUploaded($upload, $context)
154 {
155 Logtivity_Logger::log()
156 ->setAction('Attachment Uploaded')
157 ->setContext(basename($upload['file']))
158 ->addMeta('Url', $upload['url'])
159 ->addMeta('Type', $upload['type'])
160 ->addMeta('Context', $context)
161 ->send();
162
163 return $upload;
164 }
165
166 public function mediaMetaUpdated()
167 {
168 $post_id = absint($_POST['id']);
169
170 if ($post_id) {
171 Logtivity_Logger::log()
172 ->setAction('Attachment Meta Updated.')
173 ->addMeta("Media ID", $post_id)
174 ->addMeta("Changes", ( isset($_POST['changes']) ? $_POST['changes'] : null))
175 ->send();
176 }
177
178 return $post;
179 }
180 }
181
182 $Logtivity_Post = new Logtivity_Post;