PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.8.23.5
Pods – Custom Content Types and Fields v2.8.23.5
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / tribe-common / src / Tribe / Post_History.php
pods / tribe-common / src / Tribe Last commit date
Admin 1 week ago Ajax 1 week ago Asset 1 week ago Context 1 week ago Customizer 1 week ago Debug_Bar 1 week ago Dialog 1 week ago Documentation 1 week ago Duplicate 1 week ago Editor 1 week ago Image 1 week ago JSON_LD 1 week ago Languages 1 week ago Log 1 week ago Meta 1 week ago Models 1 week ago PUE 1 week ago Process 1 week ago Promoter 1 week ago REST 1 week ago Repository 1 week ago Service_Providers 1 week ago Shortcode 1 week ago Support 1 week ago Tabbed_View 1 week ago Tooltip 1 week ago Traits 1 week ago Utils 1 week ago Validator 1 week ago Widget 1 week ago Abstract_Deactivation.php 1 week ago Abstract_Plugin_Register.php 1 week ago App_Shop.php 1 week ago Assets.php 1 week ago Assets_Pipeline.php 1 week ago Autoloader.php 1 week ago Cache.php 1 week ago Cache_Listener.php 1 week ago Changelog_Reader.php 1 week ago Container.php 1 week ago Context.php 1 week ago Cost_Utils.php 1 week ago Credits.php 1 week ago Customizer.php 1 week ago DB_Lock.php 1 week ago Data.php 1 week ago Date_Utils.php 1 week ago Db.php 1 week ago Debug.php 1 week ago Dependency.php 1 week ago Deprecation.php 1 week ago Editor.php 1 week ago Error.php 1 week ago Exception.php 1 week ago Extension.php 1 week ago Extension_Loader.php 1 week ago Feature_Detection.php 1 week ago Field.php 1 week ago Field_Conditional.php 1 week ago Freemius.php 1 week ago Log.php 1 week ago Main.php 1 week ago Notices.php 1 week ago Plugin_Meta_Links.php 1 week ago Plugins.php 1 week ago Plugins_API.php 1 week ago Post_History.php 1 week ago Post_Transient.php 1 week ago Promise.php 1 week ago Repository.php 1 week ago Rewrite.php 1 week ago Settings.php 1 week ago Settings_Manager.php 1 week ago Settings_Tab.php 1 week ago Simple_Table.php 1 week ago Support.php 1 week ago Tabbed_View.php 1 week ago Template.php 1 week ago Template_Factory.php 1 week ago Template_Part_Cache.php 1 week ago Templates.php 1 week ago Terms.php 1 week ago Timezones.php 1 week ago Tracker.php 1 week ago Updater.php 1 week ago Validate.php 1 week ago View_Helpers.php 1 week 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