PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 1.6.0
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v1.6.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 / Logtivity_Abstract_Logger.php

Logtivity_Abstract_Logger.php in Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity 1.6.0, at Logs/Logtivity_Abstract_Logger.php

129 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 abstract class Logtivity_Abstract_Logger
4 {
5 protected $logger;
6
7 protected $ignoredPostTypes = [
8 'revision',
9 'customize_changeset',
10 'nav_menu_item',
11 'edd_log',
12 'edd_payment',
13 ];
14
15 protected $ignoredPostTitles = [
16 'Auto Draft',
17 ];
18
19 protected $ignoredPostStatuses = ['trash'];
20
21 public function __construct()
22 {
23 $this->registerHooks();
24 }
25
26 /**
27 * Check against certain rules on whether we should ignore the logging of a certain post
28 *
29 * @param WP_Post $post
30 * @return bool
31 */
32 protected function shouldIgnore($post, $action = null)
33 {
34 if ($this->ignoringPostType($post->post_type)) {
35 return true;
36 }
37
38 if ($this->ignoringPostTitle($post->post_title)) {
39 return true;
40 }
41
42 if ($this->ignoringPostStatus($post->post_status)) {
43 return true;
44 }
45
46 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
47 return true;
48 }
49
50 return false;
51 }
52
53 /**
54 * Check to see if the request is Gutenbergs second request
55 *
56 * @return bool
57 */
58 protected function loggedRecently($postId)
59 {
60 $date = get_post_meta($postId, 'logtivity_last_logged', true);
61
62 if (!$date) {
63 return false;
64 }
65
66 $now = new DateTime();
67 $lastLogged = $this->sanitizeDate($date);
68
69 $diffInSeconds = $now->getTimestamp() - $lastLogged->getTimestamp();
70
71 return $diffInSeconds < 5;
72 }
73
74 protected function sanitizeDate( $date )
75 {
76 try {
77 return new \DateTime( $date );
78 } catch ( \Exception $e ) {
79 return new \DateTime( '1970-01-01' );
80 }
81 }
82
83 /**
84 * Ignoring certain post statuses. Example: trash.
85 * We already have a postWasTrashed hook so
86 * don't need to log twice.
87 *
88 * @param string $post_status
89 * @return bool
90 */
91 protected function ignoringPostStatus($post_status)
92 {
93 return in_array($post_status, $this->ignoredPostStatuses);
94 }
95
96 /**
97 * Ignoring certain post types. Particularly system generated
98 * that are not directly triggered by the user.
99 *
100 * @param string $post_type
101 * @return bool
102 */
103 protected function ignoringPostType($post_type)
104 {
105 return in_array($post_type, $this->ignoredPostTypes);
106 }
107
108 /**
109 * Ignore certain system generated post titles
110 *
111 * @param string $title
112 * @return bool
113 */
114 protected function ignoringPostTitle($title)
115 {
116 return in_array($title, $this->ignoredPostTitles);
117 }
118
119 /**
120 * Generate a label version of the given post ids post type
121 *
122 * @param integer $post_id
123 * @return string
124 */
125 protected function getPostTypeLabel($post_id)
126 {
127 return ucwords( str_replace(['_', '-'], ' ', get_post_type($post_id)) );
128 }
129 }