PluginProbe
Patchstack – WordPress & Plugins Security / 2.2.4
Patchstack – WordPress & Plugins Security v2.2.4
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.4, at includes/events/posts.php

111 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 add_action( 'transition_post_status', [ &$this, 'transition_post_status' ], 10, 3 );
20 add_action( 'delete_post', [ &$this, 'delete_post' ] );
21 }
22
23 /**
24 * Determine the title.
25 *
26 * @param integer $post
27 * @return string
28 */
29 protected function _draft_or_post_title( $post = 0 ) {
30 $title = esc_html( get_the_title( $post ) );
31 if ( empty( $title ) ) {
32 return __( '(no title)', 'patchstack' );
33 }
34
35 return $title;
36 }
37
38 /**
39 * When the status of a post is transitioned.
40 *
41 * @param string $new_status
42 * @param string $old_status
43 * @param object $post
44 * @return void
45 */
46 public function transition_post_status( $new_status, $old_status, $post ) {
47 // Determine the action.
48 if ( $old_status === 'auto-draft' && ( $new_status !== 'auto-draft' && $new_status !== 'inherit' ) ) {
49 $action = 'created';
50 } elseif ( $new_status === 'auto-draft' || ( $old_status === 'new' && $new_status === 'inherit' ) ) {
51 return;
52 } elseif ( $new_status === 'trash' ) {
53 $action = 'trashed';
54 } elseif ( $old_status === 'trash' ) {
55 $action = 'restored';
56 } else {
57 $action = 'updated';
58 }
59
60 // No need to log revisions.
61 if ( wp_is_post_revision( $post->ID ) ) {
62 return;
63 }
64
65 // Skip for menu items.
66 if ( get_post_type( $post->ID ) === 'nav_menu_item' ) {
67 return;
68 }
69
70 $this->insert(
71 [
72 'object' => 'post',
73 'object_id' => $post->ID,
74 'action' => $action,
75 'object_name' => $this->_draft_or_post_title( $post->ID ),
76 ]
77 );
78 }
79
80 /**
81 * When a post is deleted.
82 *
83 * @param integer $post_id
84 * @return void
85 */
86 public function delete_post( $post_id ) {
87 if ( wp_is_post_revision( $post_id ) ) {
88 return;
89 }
90
91 $post = get_post( $post_id );
92 if ( in_array( $post->post_status, [ 'auto-draft', 'inherit' ] ) ) {
93 return;
94 }
95
96 // Skip for menu items.
97 if ( get_post_type( $post->ID ) === 'nav_menu_item' ) {
98 return;
99 }
100
101 $this->insert(
102 [
103 'object' => 'post',
104 'object_id' => $post->ID,
105 'action' => 'deleted',
106 'object_name' => $this->_draft_or_post_title( $post->ID ),
107 ]
108 );
109 }
110 }
111