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