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

164 lines 4.1 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 }
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 $log = Logtivity_Logger::log()
58 ->setAction($this->action ?? $this->getPostTypeLabel($post->ID) . ' Updated')
59 ->setContext($post->post_title)
60 ->addMeta('Post ID', $post->ID)
61 ->addMeta('Post Title', $post->post_title)
62 ->addMeta('Post Type', $post->post_type)
63 ->addMeta('Post Status', $post->post_status);
64
65 if ($revision = $this->getRevision($post_id)) {
66 $log->addMeta('View Revision', $revision);
67 }
68
69 $log->send();
70 }
71
72 private function getRevision( $post_id )
73 {
74 $revisions = wp_get_post_revisions( $post_id );
75 if ( ! empty( $revisions ) ) {
76 $revision = array_shift( $revisions );
77 return $this->getRevisionLink( $revision->ID );
78 }
79 }
80
81 private function getRevisionLink( $revision_id )
82 {
83 return ! empty( $revision_id ) ? add_query_arg( 'revision', $revision_id, admin_url( 'revision.php' ) ) : null;
84 }
85
86 public function postWasTrashed($post_id)
87 {
88 if (get_post_type($post_id) == 'customize_changeset') {
89 return;
90 }
91
92 return Logtivity_Logger::log()
93 ->setAction($this->getPostTypeLabel($post_id) . ' Trashed')
94 ->setContext(get_the_title($post_id))
95 ->addMeta('Post ID', $post_id)
96 ->addMeta('Post Type', get_post_type($post_id))
97 ->addMeta('Post Title', get_the_title($post_id))
98 ->send();
99 }
100
101 public function postWasRestored($post)
102 {
103 return Logtivity_Logger::log()
104 ->setAction(
105 $this->getPostTypeLabel($post->ID) . ' Restored from Trash'
106 )
107 ->setContext($post->post_title)
108 ->addMeta('Post ID', $post->ID)
109 ->addMeta('Post Type', $post->post_type)
110 ->addMeta('Post Title', $post->post_title)
111 ->send();
112 }
113
114 public function postPermanentlyDeleted($post_id)
115 {
116 if ($this->ignoringPostType(get_post_type($post_id))) {
117 return;
118 }
119
120 if ($this->ignoringPostTitle(get_the_title($post_id))) {
121 return;
122 }
123
124 return Logtivity_Logger::log()
125 ->setAction(
126 $this->getPostTypeLabel($post_id) . ' Permanently Deleted'
127 )
128 ->setContext(get_the_title($post_id))
129 ->addMeta('Post ID', $post_id)
130 ->addMeta('Post Type', get_post_type($post_id))
131 ->addMeta('Post Title', get_the_title($post_id))
132 ->send();
133 }
134
135 public function mediaUploaded($upload, $context)
136 {
137 Logtivity_Logger::log()
138 ->setAction('Attachment Uploaded')
139 ->setContext(basename($upload['file']))
140 ->addMeta('Url', $upload['url'])
141 ->addMeta('Type', $upload['type'])
142 ->addMeta('Context', $context)
143 ->send();
144
145 return $upload;
146 }
147
148 public function mediaMetaUpdated()
149 {
150 $post_id = absint($_POST['id']);
151
152 if ($post_id) {
153 Logtivity_Logger::log()
154 ->setAction('Attachment Meta Updated.')
155 ->addMeta("Media ID", $post_id)
156 ->addMeta("Changes", ( isset($_POST['changes']) ? $_POST['changes'] : null))
157 ->send();
158 }
159
160 return $post;
161 }
162 }
163
164 $Logtivity_Post = new Logtivity_Post;