| 1 |
<?php |
| 2 |
|
| 3 |
class Logtivity_Meta extends Logtivity_Abstract_Logger |
| 4 |
{ |
| 5 |
protected $ignoreKeys = [ |
| 6 |
'logtivity_last_logged', |
| 7 |
'_edit_lock', |
| 8 |
'_edit_last', |
| 9 |
'_wp_attachment_metadata', |
| 10 |
'_encloseme', |
| 11 |
'_wp_trash_meta_status', |
| 12 |
'_wp_trash_meta_time', |
| 13 |
'_wp_desired_post_slug', |
| 14 |
'_wp_trash_meta_comments_status', |
| 15 |
'_pingme', |
| 16 |
]; |
| 17 |
|
| 18 |
protected $ignorePostTypes = [ |
| 19 |
'nav_menu_item', |
| 20 |
'edd_payment', |
| 21 |
'edd_log', |
| 22 |
]; |
| 23 |
|
| 24 |
protected $ignorePostTypesIfNotInAdminArea = [ |
| 25 |
'shop_order', |
| 26 |
'dlm_download', |
| 27 |
'dlm_download_version', |
| 28 |
]; |
| 29 |
|
| 30 |
public function __construct() |
| 31 |
{ |
| 32 |
add_action('update_post_meta', [$this, 'updatingPostMeta'], 10, 4); |
| 33 |
add_action( "added_post_meta", [$this, 'addedPostMeta'], 10, 4); |
| 34 |
add_action( "delete_post_meta", [$this, 'deletingPostMeta'], 10, 4); |
| 35 |
// add_action('update_comment_meta', [$this, 'updatingCommentMeta'], 10, 4); |
| 36 |
// add_action('update_user_meta', [$this, 'updatingUserMeta'], 10, 4); |
| 37 |
// add_action('update_term_meta', [$this, 'updatingTermMeta'], 10, 4); |
| 38 |
} |
| 39 |
|
| 40 |
public function deletingPostMeta($meta_id, $object_id, $meta_key, $_meta_value) |
| 41 |
{ |
| 42 |
return $this->recordPostMetaChanges($meta_id, $object_id, $meta_key, $_meta_value, 'Deleted'); |
| 43 |
} |
| 44 |
|
| 45 |
public function addedPostMeta($meta_id, $object_id, $meta_key, $_meta_value) |
| 46 |
{ |
| 47 |
return $this->recordPostMetaChanges($meta_id, $object_id, $meta_key, $_meta_value, 'Added'); |
| 48 |
} |
| 49 |
|
| 50 |
public function updatingPostMeta($meta_id, $object_id, $meta_key, $_meta_value) |
| 51 |
{ |
| 52 |
return $this->recordPostMetaChanges($meta_id, $object_id, $meta_key, $_meta_value, 'Updated'); |
| 53 |
} |
| 54 |
|
| 55 |
public function recordPostMetaChanges($meta_id, $object_id, $meta_key, $_meta_value, $keyword) |
| 56 |
{ |
| 57 |
if (in_array($meta_key, $this->ignoreKeys)) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
$post_type = get_post_type($object_id); |
| 62 |
|
| 63 |
if (in_array($post_type, $this->ignorePostTypes)) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
if ((in_array($post_type, $this->ignorePostTypesIfNotInAdminArea)) && (!is_admin())) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
if ($meta_key === '_thumbnail_id') { |
| 72 |
return $this->postThumbnailChanged($meta_id, $object_id, $meta_key, $_meta_value, $keyword); |
| 73 |
} |
| 74 |
|
| 75 |
$previousValue = get_post_meta($object_id, $meta_key, true); |
| 76 |
|
| 77 |
if ($previousValue == $_meta_value) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
return Logtivity_Logger::log() |
| 82 |
->setAction($this->getPostTypeLabel($object_id) . ' Meta '. $keyword) |
| 83 |
->setContext($meta_key) |
| 84 |
->setPostType(get_post_type($object_id)) |
| 85 |
->setPostId($object_id) |
| 86 |
->addMeta('Meta Key', $meta_key) |
| 87 |
->addMeta('Meta Value', $_meta_value) |
| 88 |
->addMeta('Previous Value', $previousValue) |
| 89 |
->addMeta('View Post', get_edit_post_link($object_id)) |
| 90 |
->send(); |
| 91 |
} |
| 92 |
|
| 93 |
public function postThumbnailChanged($meta_id, $object_id, $meta_key, $_meta_value, $keyword) |
| 94 |
{ |
| 95 |
$previousThumbnailId = get_post_meta($object_id, $meta_key, true); |
| 96 |
|
| 97 |
return Logtivity_Logger::log() |
| 98 |
->setAction('Post Thumbnail ' . ( $keyword == 'Deleted' ? 'Removed' : 'Changed')) |
| 99 |
->setContext(get_the_title($object_id)) |
| 100 |
->setPostType(get_post_type($object_id)) |
| 101 |
->setPostId($object_id) |
| 102 |
->addMetaIf($_meta_value != '', 'New Thumbnail ID', $_meta_value) |
| 103 |
->addMetaIf($_meta_value != '', 'New Filename', basename(get_attached_file($_meta_value))) |
| 104 |
->addMetaIf( |
| 105 |
$previousThumbnailId != '' && $previousThumbnailId != $_meta_value, |
| 106 |
'Previous Thumbnail ID', |
| 107 |
$previousThumbnailId |
| 108 |
) |
| 109 |
->addMetaIf( |
| 110 |
$previousThumbnailId != '' && $previousThumbnailId != $_meta_value, |
| 111 |
'Previous Filename', |
| 112 |
basename(get_attached_file($previousThumbnailId)) |
| 113 |
) |
| 114 |
->addMeta('View Post', get_edit_post_link($object_id)) |
| 115 |
->send(); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
$Logtivity_Meta = new Logtivity_Meta; |