PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 1.13.0
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v1.13.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.13.0, at Logs/Core/Logtivity_Post.php

184 lines 4.7 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 ->addMeta('Post ID', $post->ID)
47 ->addMeta('Post Type', $post->post_type)
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 $log = Logtivity_Logger::log()
76 ->setAction($this->action ?? $this->getPostTypeLabel($post->ID) . ' Updated')
77 ->setContext($post->post_title)
78 ->addMeta('Post ID', $post->ID)
79 ->addMeta('Post Title', $post->post_title)
80 ->addMeta('Post Type', $post->post_type)
81 ->addMeta('Post Status', $post->post_status);
82
83 if ($revision = $this->getRevision($post_id)) {
84 $log->addMeta('View Revision', $revision);
85 }
86
87 $log->send();
88
89 update_post_meta($post_id, 'logtivity_last_logged', (new \DateTime())->format('Y-m-d H:i:s'));
90 }
91
92 private function getRevision( $post_id )
93 {
94 $revisions = wp_get_post_revisions( $post_id );
95 if ( ! empty( $revisions ) ) {
96 $revision = array_shift( $revisions );
97 return $this->getRevisionLink( $revision->ID );
98 }
99 }
100
101 private function getRevisionLink( $revision_id )
102 {
103 return ! empty( $revision_id ) ? add_query_arg( 'revision', $revision_id, admin_url( 'revision.php' ) ) : null;
104 }
105
106 public function postWasTrashed($post_id)
107 {
108 if (get_post_type($post_id) == 'customize_changeset') {
109 return;
110 }
111
112 return Logtivity_Logger::log()
113 ->setAction($this->getPostTypeLabel($post_id) . ' Trashed')
114 ->setContext(logtivity_get_the_title($post_id))
115 ->addMeta('Post ID', $post_id)
116 ->addMeta('Post Type', get_post_type($post_id))
117 ->addMeta('Post Title', logtivity_get_the_title($post_id))
118 ->send();
119 }
120
121 public function postWasRestored($post)
122 {
123 return Logtivity_Logger::log()
124 ->setAction(
125 $this->getPostTypeLabel($post->ID) . ' Restored from Trash'
126 )
127 ->setContext($post->post_title)
128 ->addMeta('Post ID', $post->ID)
129 ->addMeta('Post Type', $post->post_type)
130 ->addMeta('Post Title', $post->post_title)
131 ->send();
132 }
133
134 public function postPermanentlyDeleted($post_id)
135 {
136 if ($this->ignoringPostType(get_post_type($post_id))) {
137 return;
138 }
139
140 if ($this->ignoringPostTitle(logtivity_get_the_title($post_id))) {
141 return;
142 }
143
144 return Logtivity_Logger::log()
145 ->setAction(
146 $this->getPostTypeLabel($post_id) . ' Permanently Deleted'
147 )
148 ->setContext(logtivity_get_the_title($post_id))
149 ->addMeta('Post ID', $post_id)
150 ->addMeta('Post Type', get_post_type($post_id))
151 ->addMeta('Post Title', logtivity_get_the_title($post_id))
152 ->send();
153 }
154
155 public function mediaUploaded($upload, $context)
156 {
157 Logtivity_Logger::log()
158 ->setAction('Attachment Uploaded')
159 ->setContext(basename($upload['file']))
160 ->addMeta('Url', $upload['url'])
161 ->addMeta('Type', $upload['type'])
162 ->addMeta('Context', $context)
163 ->send();
164
165 return $upload;
166 }
167
168 public function mediaMetaUpdated()
169 {
170 $post_id = absint($_POST['id']);
171
172 if ($post_id) {
173 Logtivity_Logger::log()
174 ->setAction('Attachment Meta Updated.')
175 ->addMeta("Media ID", $post_id)
176 ->addMeta("Changes", ( isset($_POST['changes']) ? $_POST['changes'] : null))
177 ->send();
178 }
179
180 return $post;
181 }
182 }
183
184 $Logtivity_Post = new Logtivity_Post;