PluginProbe
Patchstack – WordPress & Plugins Security / 2.2.10
Patchstack – WordPress & Plugins Security v2.2.10
2.3.7 trunk 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.17 2.1.18 2.1.19 2.1.2 2.1.20 2.1.21 2.1.22 2.1.23 2.1.24 2.1.25 2.1.3 2.1.4 2.1.5 2.1.6 All 49 releases
patchstack / includes / events / posts.php

posts.php in Patchstack – WordPress & Plugins Security 2.2.10, at includes/events/posts.php

115 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Do not allow the file to be called directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 /**
9 * This class is used to log any post related action.
10 */
11 class P_Event_Posts extends P_Event_Log {
12
13 /**
14 * Hook into the actions so we can log it.
15 *
16 * @return void
17 */
18 public function __construct() {
19 if ( ! get_option( 'patchstack_activity_log_posts', 0 ) ) {
20 return;
21 }
22
23 add_action( 'transition_post_status', [ &$this, 'transition_post_status' ], 10, 3 );
24 add_action( 'delete_post', [ &$this, 'delete_post' ] );
25 }
26
27 /**
28 * Determine the title.
29 *
30 * @param integer $post
31 * @return string
32 */
33 protected function _draft_or_post_title( $post = 0 ) {
34 $title = esc_html( get_the_title( $post ) );
35 if ( empty( $title ) ) {
36 return esc_attr__( '(no title)', 'patchstack' );
37 }
38
39 return $title;
40 }
41
42 /**
43 * When the status of a post is transitioned.
44 *
45 * @param string $new_status
46 * @param string $old_status
47 * @param object $post
48 * @return void
49 */
50 public function transition_post_status( $new_status, $old_status, $post ) {
51 // Determine the action.
52 if ( $old_status === 'auto-draft' && ( $new_status !== 'auto-draft' && $new_status !== 'inherit' ) ) {
53 $action = 'created';
54 } elseif ( $new_status === 'auto-draft' || ( $old_status === 'new' && $new_status === 'inherit' ) ) {
55 return;
56 } elseif ( $new_status === 'trash' ) {
57 $action = 'trashed';
58 } elseif ( $old_status === 'trash' ) {
59 $action = 'restored';
60 } else {
61 $action = 'updated';
62 }
63
64 // No need to log revisions.
65 if ( wp_is_post_revision( $post->ID ) ) {
66 return;
67 }
68
69 // Skip for menu items.
70 if ( get_post_type( $post->ID ) === 'nav_menu_item' ) {
71 return;
72 }
73
74 $this->insert(
75 [
76 'object' => 'post',
77 'object_id' => $post->ID,
78 'action' => $action,
79 'object_name' => $this->_draft_or_post_title( $post->ID ),
80 ]
81 );
82 }
83
84 /**
85 * When a post is deleted.
86 *
87 * @param integer $post_id
88 * @return void
89 */
90 public function delete_post( $post_id ) {
91 if ( wp_is_post_revision( $post_id ) ) {
92 return;
93 }
94
95 $post = get_post( $post_id );
96 if ( in_array( $post->post_status, [ 'auto-draft', 'inherit' ] ) ) {
97 return;
98 }
99
100 // Skip for menu items.
101 if ( get_post_type( $post->ID ) === 'nav_menu_item' ) {
102 return;
103 }
104
105 $this->insert(
106 [
107 'object' => 'post',
108 'object_id' => $post->ID,
109 'action' => 'deleted',
110 'object_name' => $this->_draft_or_post_title( $post->ID ),
111 ]
112 );
113 }
114 }
115