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