Admin
2 weeks ago
Ajax
2 weeks ago
Asset
2 weeks ago
Context
2 weeks ago
Customizer
2 weeks ago
Debug_Bar
2 weeks ago
Dialog
2 weeks ago
Documentation
2 weeks ago
Duplicate
2 weeks ago
Editor
2 weeks ago
Image
2 weeks ago
JSON_LD
2 weeks ago
Languages
2 weeks ago
Log
2 weeks ago
Meta
2 weeks ago
Models
2 weeks ago
PUE
2 weeks ago
Process
2 weeks ago
Promoter
2 weeks ago
REST
2 weeks ago
Repository
2 weeks ago
Service_Providers
2 weeks ago
Shortcode
2 weeks ago
Support
2 weeks ago
Tabbed_View
2 weeks ago
Tooltip
2 weeks ago
Traits
2 weeks ago
Utils
2 weeks ago
Validator
2 weeks ago
Widget
2 weeks ago
Abstract_Deactivation.php
2 weeks ago
Abstract_Plugin_Register.php
2 weeks ago
App_Shop.php
2 weeks ago
Assets.php
2 weeks ago
Assets_Pipeline.php
2 weeks ago
Autoloader.php
2 weeks ago
Cache.php
2 weeks ago
Cache_Listener.php
2 weeks ago
Changelog_Reader.php
2 weeks ago
Container.php
2 weeks ago
Context.php
2 weeks ago
Cost_Utils.php
2 weeks ago
Credits.php
2 weeks ago
Customizer.php
2 weeks ago
DB_Lock.php
2 weeks ago
Data.php
2 weeks ago
Date_Utils.php
2 weeks ago
Db.php
2 weeks ago
Debug.php
2 weeks ago
Dependency.php
2 weeks ago
Deprecation.php
2 weeks ago
Editor.php
2 weeks ago
Error.php
2 weeks ago
Exception.php
2 weeks ago
Extension.php
2 weeks ago
Extension_Loader.php
2 weeks ago
Feature_Detection.php
2 weeks ago
Field.php
2 weeks ago
Field_Conditional.php
2 weeks ago
Freemius.php
2 weeks ago
Log.php
2 weeks ago
Main.php
2 weeks ago
Notices.php
2 weeks ago
Plugin_Meta_Links.php
2 weeks ago
Plugins.php
2 weeks ago
Plugins_API.php
2 weeks ago
Post_History.php
2 weeks ago
Post_Transient.php
2 weeks ago
Promise.php
2 weeks ago
Repository.php
2 weeks ago
Rewrite.php
2 weeks ago
Settings.php
2 weeks ago
Settings_Manager.php
2 weeks ago
Settings_Tab.php
2 weeks ago
Simple_Table.php
2 weeks ago
Support.php
2 weeks ago
Tabbed_View.php
2 weeks ago
Template.php
2 weeks ago
Template_Factory.php
2 weeks ago
Template_Part_Cache.php
2 weeks ago
Templates.php
2 weeks ago
Terms.php
2 weeks ago
Timezones.php
2 weeks ago
Tracker.php
2 weeks ago
Updater.php
2 weeks ago
Validate.php
2 weeks ago
View_Helpers.php
2 weeks ago
Post_History.php
135 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Used for maintaining post-level histories/audit trails. |
| 4 | * |
| 5 | * @internal |
| 6 | * @since 4.3 |
| 7 | */ |
| 8 | class Tribe__Post_History { |
| 9 | /** |
| 10 | * Used to identify history/audit trail post meta records. |
| 11 | */ |
| 12 | const HISTORY_KEY = '_tribe_post_history'; |
| 13 | |
| 14 | /** |
| 15 | * The post this history object is concerned with. |
| 16 | * |
| 17 | * @var int |
| 18 | */ |
| 19 | protected $post_id; |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * Returns a Tribe__Post_History object for the specified post. |
| 24 | * |
| 25 | * @param int $post_id |
| 26 | * |
| 27 | * @return Tribe__Post_History |
| 28 | */ |
| 29 | public static function load( $post_id ) { |
| 30 | return new self( $post_id ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Returns a Tribe__Post_History object for the specified post. |
| 35 | * |
| 36 | * @param int $post_id |
| 37 | * |
| 38 | * @return Tribe__Post_History |
| 39 | */ |
| 40 | public function __construct( $post_id ) { |
| 41 | $this->post_id = $post_id; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Records a new history entry for the current post. |
| 46 | * |
| 47 | * @param string $message |
| 48 | * @param array $data |
| 49 | */ |
| 50 | public function add_entry( $message, array $data = [] ) { |
| 51 | $datetime = current_time( 'mysql' ); |
| 52 | $checksum = uniqid( substr( hash( 'md5', $datetime . $message . serialize( $data ) ), 0, 8 ) . '_' ); |
| 53 | |
| 54 | $log_entry = wp_slash( json_encode( [ |
| 55 | 'datetime' => $datetime, |
| 56 | 'message' => $message, |
| 57 | 'data' => $data, |
| 58 | 'checksum' => $checksum, |
| 59 | ] ) ); |
| 60 | |
| 61 | add_post_meta( $this->post_id, self::HISTORY_KEY, $log_entry ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Indicates if any history exists for the current post. |
| 66 | * |
| 67 | * @return bool |
| 68 | */ |
| 69 | public function has_entries() { |
| 70 | $first_available_entry = get_post_meta( $this->post_id, self::HISTORY_KEY, true ); |
| 71 | return ! empty( $first_available_entry ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns all historical records for the current post as an array |
| 76 | * of objects, each object taking the form: |
| 77 | * |
| 78 | * { |
| 79 | * "datetime": "yyyy-mm-dd hh:ii:ss", |
| 80 | * "message": "...", |
| 81 | * "data": [] |
| 82 | * } |
| 83 | * |
| 84 | * @return array |
| 85 | */ |
| 86 | public function get_entries() { |
| 87 | $entries = []; |
| 88 | |
| 89 | foreach ( get_post_meta( $this->post_id, self::HISTORY_KEY ) as $log_entry ) { |
| 90 | $log_entry = json_decode( $log_entry ); |
| 91 | |
| 92 | if ( ! $log_entry ) { |
| 93 | continue; |
| 94 | } |
| 95 | |
| 96 | $entries[] = $log_entry; |
| 97 | } |
| 98 | |
| 99 | return $entries; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Deletes all entries for the current post that match the provided datetime |
| 104 | * string and (optionally) also match the provided checksum. |
| 105 | * |
| 106 | * Returns the total number of deleted entries, which may be zero if none were matched; |
| 107 | * can also be more than one if multiple entries were logged at the same time and no |
| 108 | * checksum is provided. |
| 109 | * |
| 110 | * @param string $datetime |
| 111 | * @param string $checksum optional value to more precisely specify the entry to be deleted |
| 112 | * |
| 113 | * @return int |
| 114 | */ |
| 115 | public function delete_entry( $datetime, $checksum = null ) { |
| 116 | $deleted = 0; |
| 117 | |
| 118 | foreach ( $this->get_entries() as $entry ) { |
| 119 | if ( $entry->datetime !== $datetime ) { |
| 120 | continue; |
| 121 | } |
| 122 | |
| 123 | if ( null !== $checksum && $entry->checksum !== $checksum ) { |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | if ( delete_post_meta( $this->post_id, self::HISTORY_KEY, json_encode( $entry ) ) ) { |
| 128 | $deleted++; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return $deleted; |
| 133 | } |
| 134 | } |
| 135 |