KroneMultimedia /
plugin-timeshift
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /* |
||
| 3 | * |
||
| 4 | * |
||
| 5 | * inspired by https://github.com/adamsilverstein/wp-post-meta-revisions/blob/master/wp-post-meta-revisions.php |
||
| 6 | * many thx @adamsilverstein |
||
| 7 | * |
||
| 8 | */ |
||
| 9 | |||
| 10 | namespace KMM\Timeshift; |
||
| 11 | |||
| 12 | class Core |
||
| 13 | { |
||
| 14 | private $plugin_dir; |
||
| 15 | private $last_author = false; |
||
| 16 | private $timeshift_cached_meta; |
||
| 17 | |||
| 18 | 12 | public function __construct($i18n) |
|
| 19 | { |
||
| 20 | global $wpdb; |
||
| 21 | 12 | $this->i18n = $i18n; |
|
| 22 | 12 | $this->wpdb = $wpdb; |
|
| 23 | 12 | $this->plugin_dir = plugin_dir_url(__FILE__) . '../'; |
|
| 24 | 12 | $this->add_filters(); |
|
| 25 | 12 | $this->add_actions(); |
|
| 26 | 12 | $this->add_metabox(); |
|
| 27 | //Disable WP's own revision system |
||
| 28 | 12 | remove_post_type_support('post', 'revisions'); |
|
| 29 | } |
||
| 30 | |||
| 31 | 2 | public function hasTimeshifts($post_id) |
|
| 32 | { |
||
| 33 | 2 | $post_type = get_post_type($post_id); |
|
| 34 | 2 | $table_name = $this->wpdb->prefix . 'timeshift_' . $post_type; |
|
| 35 | 2 | $this->checkTable($post_type); |
|
| 36 | 2 | $sql = "select count(1) as amount from $table_name where post_id=" . $post_id; |
|
| 37 | 2 | $r = $this->wpdb->get_results($sql); |
|
| 38 | |||
| 39 | 2 | if ($r && count($r) == 1) { |
|
| 40 | 1 | if (intval($r[0]->amount) > 0) { |
|
| 41 | 1 | return true; |
|
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | 1 | return false; |
|
| 46 | } |
||
| 47 | |||
| 48 | 12 | public function timeshiftVisible() |
|
| 49 | { |
||
| 50 | 12 | $check = apply_filters('krn_timeshift_visible', true); |
|
| 51 | |||
| 52 | 12 | return $check; |
|
| 53 | } |
||
| 54 | |||
| 55 | 12 | public function add_metabox() |
|
| 56 | { |
||
| 57 | 12 | $cl = $this; |
|
| 58 | 12 | if (! $this->timeshiftVisible()) { |
|
| 59 | return; |
||
| 60 | } |
||
| 61 | 12 | if (! isset($_GET['post']) || ! $this->hasTimeshifts($_GET['post'])) { |
|
| 62 | 12 | return; |
|
| 63 | } |
||
| 64 | add_action('add_meta_boxes', function () use ($cl) { |
||
| 65 | add_meta_box('krn-timeshift', __('Timeshift', 'kmm-timeshift'), [$cl, 'timeshift_metabox'], null, 'normal', 'core'); |
||
| 66 | }); |
||
| 67 | } |
||
| 68 | |||
| 69 | 2 | public function timeshift_metabox() |
|
| 70 | { |
||
| 71 | global $post; |
||
| 72 | 2 | if (! isset($_GET['post'])) { |
|
| 73 | 1 | return; |
|
| 74 | } |
||
| 75 | 1 | ||
| 76 | 1 | if (! isset($_GET['timeshift_paged'])) { |
|
| 77 | 1 | $_GET['timeshift_paged'] = 1; |
|
| 78 | } |
||
| 79 | 1 | $start = ($_GET['timeshift_paged'] - 1) * 10; |
|
| 80 | 1 | ||
| 81 | 1 | $prod_post = get_post($_GET['post']); |
|
| 82 | 1 | $table_name = $this->wpdb->prefix . 'timeshift_' . $post->post_type; |
|
| 83 | 1 | $sql = "select * from $table_name where post_id=" . $post->ID . ' order by create_date desc limit ' . $start . ',10'; |
|
| 84 | 1 | ||
| 85 | 1 | $table_postmeta = $this->wpdb->prefix . 'postmeta'; |
|
| 86 | 1 | $sql_last_editor = 'select meta_value from ' . $table_postmeta . ' where post_id=' . $post->ID . " AND meta_key='_edit_last'"; |
|
| 87 | 1 | $last_editor = $this->wpdb->get_var($sql_last_editor); |
|
| 88 | 1 | ||
| 89 | 1 | $row = $this->wpdb->get_results($sql); |
|
| 90 | 1 | ||
| 91 | 1 | $sqlcount = "select count(1) as cnt from $table_name where post_id=" . $post->ID; |
|
| 92 | 1 | $maxrows = $this->wpdb->get_results($sqlcount); |
|
| 93 | 1 | $allrows = (int)$maxrows[0]->{'cnt'}; |
|
| 94 | 1 | $big = 9999; |
|
|
0 ignored issues
–
show
|
|||
| 95 | 1 | $paged = $_GET['timeshift_paged']; |
|
| 96 | 1 | $max_page = ceil($allrows / 10); |
|
| 97 | 1 | echo paginate_links([ |
|
| 98 | 1 | 'base' => 'post.php%_%', |
|
| 99 | 'format' => '?timeshift_paged=%#%', |
||
| 100 | 1 | 'current' => max(1, $paged), |
|
| 101 | 'total' => $max_page, |
||
| 102 | 'mid_size' => 1, |
||
| 103 | 'prev_text' => __('«'), |
||
| 104 | 'next_text' => __('»'), |
||
| 105 | //'type' => 'list' |
||
| 106 | ]); |
||
| 107 | echo '<table class="widefat fixed">'; |
||
| 108 | echo '<thead>'; |
||
| 109 | echo '<tr>'; |
||
| 110 | echo '<th width=30></th>'; |
||
| 111 | echo '<th width="40%" id="columnname" class="manage-column column-columnname" scope="col">' . __('Title', 'kmm-timeshift') . '</th>'; |
||
| 112 | echo '<th width="30%" id="columnname" class="manage-column column-columnname" scope="col">' . __('Snapshot Date', 'kmm-timeshift') . '</th>'; |
||
| 113 | echo '<th width="10%" id="columnname" class="manage-column column-columnname" scope="col">' . __('Author', 'kmm-timeshift') . '</th>'; |
||
| 114 | 1 | echo '<th width="10%" id="columnname" class="manage-column column-columnname" scope="col">' . __('Actions', 'kmm-timeshift') . '</th>'; |
|
| 115 | 1 | echo '</tr>'; |
|
| 116 | echo ' </thead>'; |
||
| 117 | echo '<tbody>'; |
||
| 118 | 12 | echo '<tr style="font-weight: 800;">'; |
|
| 119 | echo '<td>' . get_avatar($last_editor, 30) . '</td>'; |
||
| 120 | echo '<td>' . $prod_post->post_title . '</td>'; |
||
| 121 | echo '<td>' . $prod_post->post_modified . '</td>'; |
||
| 122 | echo '<td>' . get_the_author_meta('display_name', $last_editor) . '</td>'; |
||
| 123 | 12 | echo "<td><a href='post.php?post=" . $_GET['post'] . "&action=edit'><span class='dashicons dashicons-admin-site'></span></A></td>"; |
|
| 124 | 12 | echo '</tr>'; |
|
| 125 | |||
| 126 | foreach ($row as $rev) { |
||
| 127 | 1 | $timeshift = unserialize($rev->post_payload); |
|
| 128 | $style = ''; |
||
| 129 | 1 | if (isset($_GET['timeshift']) && $_GET['timeshift'] == $rev->id) { |
|
| 130 | 1 | $style = 'style="font-style:italic;background-color: lightblue;"'; |
|
| 131 | 1 | } |
|
| 132 | if (! isset($timeshift->meta['_edit_last']) || is_null($timeshift->meta['_edit_last'])) { |
||
| 133 | $timeshift->meta['_edit_last'] = 0; // some images dont have _edit_last field |
||
| 134 | 1 | } |
|
| 135 | echo '<tr ' . $style . '>'; |
||
| 136 | echo '<td>' . get_avatar($timeshift->meta['_edit_last'][0], 30) . '</td>'; |
||
| 137 | 3 | echo '<td>' . $timeshift->post->post_title . '</td>'; |
|
| 138 | echo '<td>' . $timeshift->post->post_modified . '</td>'; |
||
| 139 | 3 | echo '<td>' . get_the_author_meta('display_name', $timeshift->meta['_edit_last'][0]) . '</td>'; |
|
| 140 | 3 | echo "<td><a href='post.php?post=" . $_GET['post'] . '&action=edit×hift=' . $rev->id . "'><span class='dashicons dashicons-backup'></span></a></td>"; |
|
| 141 | echo '</tr>'; |
||
| 142 | } |
||
| 143 | echo '</tbody>'; |
||
| 144 | echo '</table>'; |
||
| 145 | } |
||
| 146 | |||
| 147 | public function add_filters() |
||
| 148 | { |
||
| 149 | // When revisioned post meta has changed, trigger a revision save. |
||
| 150 | //add_filter('wp_save_post_revision_post_has_changed', [$this, '_wp_check_revisioned_meta_fields_have_changed'], 10, 3); |
||
| 151 | |||
| 152 | add_filter('get_post_metadata', [$this, 'inject_metadata_timeshift'], 1, 4); |
||
| 153 | add_filter('update_post_metadata', [$this, 'update_post_metadata'], 1, 5); |
||
| 154 | } |
||
| 155 | |||
| 156 | public function update_post_metadata($check, int $object_id, string $meta_key, $meta_value, $prev_value) |
||
| 157 | { |
||
| 158 | if ($meta_key == '_edit_last') { |
||
| 159 | $lo = get_post_meta($object_id, '_edit_last', true); |
||
| 160 | $this->last_author = $lo; |
||
| 161 | } |
||
| 162 | |||
| 163 | 2 | return null; |
|
| 164 | } |
||
| 165 | |||
| 166 | 2 | public function inject_metadata_timeshift($value, $post_id, $key, $single) |
|
| 167 | 1 | { |
|
| 168 | if (! isset($_GET['timeshift'])) { |
||
| 169 | return; |
||
| 170 | 1 | } |
|
| 171 | 1 | //Load timeshift |
|
| 172 | 1 | if (! $this->timeshift_cached_meta) { |
|
| 173 | 1 | $post_type = get_post_type($post_id); |
|
| 174 | $table_name = $this->wpdb->prefix . 'timeshift_' . $post_type; |
||
| 175 | $sql = "select * from $table_name where id=" . intval($_GET['timeshift']); |
||
| 176 | $r = $this->wpdb->get_results($sql); |
||
| 177 | View Code Duplication | if ($r && count($r) == 1) { |
|
| 178 | $payload = unserialize($r[0]->post_payload); |
||
| 179 | 12 | $this->timeshift_cached_meta = $payload->meta; |
|
| 180 | } |
||
| 181 | 12 | } |
|
| 182 | 12 | //is the requested meta data in the stored snapshot |
|
| 183 | 12 | if ($this->timeshift_cached_meta && isset($this->timeshift_cached_meta[$key])) { |
|
| 184 | return $this->timeshift_cached_meta[$key]; |
||
| 185 | } else { |
||
| 186 | //Otherwise return default value, like acf core fields. |
||
| 187 | return $value; |
||
| 188 | } |
||
| 189 | 12 | } |
|
| 190 | 12 | ||
| 191 | public function inject_timeshift($p) |
||
| 192 | { |
||
| 193 | 4 | global $post; |
|
| 194 | if (! isset($_GET['timeshift'])) { |
||
| 195 | 4 | return; |
|
| 196 | } |
||
| 197 | 4 | //Load timeshift |
|
| 198 | $table_name = $this->wpdb->prefix . 'timeshift_' . $post->post_type; |
||
| 199 | 4 | $sql = "select * from $table_name where id=" . intval($_GET['timeshift']); |
|
| 200 | $r = $this->wpdb->get_results($sql); |
||
| 201 | View Code Duplication | if ($r && count($r) == 1) { |
|
| 202 | $payload = unserialize($r[0]->post_payload); |
||
| 203 | $post = $payload->post; |
||
| 204 | } |
||
| 205 | 4 | } |
|
| 206 | |||
| 207 | 4 | public function add_actions() |
|
| 208 | 4 | { |
|
| 209 | add_action('edit_form_top', [$this, 'inject_timeshift'], 1, 1); |
||
| 210 | 4 | add_action('pre_post_update', [$this, 'pre_post_update'], 2, 1); |
|
| 211 | add_action('admin_notices', function () { |
||
| 212 | if (isset($_GET['timeshift']) && $_GET['timeshift']) { |
||
| 213 | 2 | echo '<div class="notice notice-warning is-dismissible"> |
|
| 214 | <p style="font-weight: 800; color: red">' . __('You are editing a historical version! if you save or publish, this will replace the current live one', 'kmm-timeshift') . '</p> |
||
| 215 | 2 | </div>'; |
|
| 216 | 2 | } |
|
| 217 | 2 | }); |
|
| 218 | 2 | add_action('krn_timeshift_create_snapshot', [$this, 'create_snapshot'], 1, 1); |
|
| 219 | } |
||
| 220 | |||
| 221 | public function checkTable($postType) |
||
| 222 | { |
||
| 223 | $table_name = $this->wpdb->prefix . 'timeshift_' . $postType; |
||
| 224 | |||
| 225 | $charset_collate = $this->wpdb->get_charset_collate(); |
||
| 226 | 1 | ||
| 227 | $sql = "CREATE TABLE IF NOT EXISTS $table_name ( |
||
| 228 | 1 | id int(12) NOT NULL AUTO_INCREMENT, |
|
| 229 | post_id int(12) NOT NULL, |
||
| 230 | create_date datetime default CURRENT_TIMESTAMP, |
||
| 231 | 1 | post_payload TEXT |
|
| 232 | ,PRIMARY KEY (id) |
||
| 233 | ) $charset_collate;"; |
||
| 234 | 1 | ||
| 235 | 1 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
|
| 236 | $a = dbDelta($sql); |
||
| 237 | 1 | ||
| 238 | 1 | return true; |
|
| 239 | } |
||
| 240 | 1 | ||
| 241 | public function storeTimeshift($timeshift) |
||
| 242 | { |
||
| 243 | 1 | $table_name = $this->wpdb->prefix . 'timeshift_' . $timeshift->post->post_type; |
|
| 244 | $sql = "insert into $table_name (post_id, post_payload) VALUES(%d, '%s')"; |
||
| 245 | 1 | $query = $this->wpdb->prepare($sql, $timeshift->post->ID, serialize($timeshift)); |
|
| 246 | 1 | $this->wpdb->query($query); |
|
| 247 | } |
||
| 248 | |||
| 249 | public function create_snapshot($postID) |
||
| 250 | { |
||
| 251 | $this->pre_post_update($postID); |
||
| 252 | } |
||
| 253 | |||
| 254 | public function pre_post_update(int $post_ID, array $data = null) |
||
| 255 | { |
||
| 256 | if (wp_is_post_autosave($post_ID)) { |
||
| 257 | return; |
||
| 258 | } |
||
| 259 | if (get_post_status($post_ID) == 'auto-draft') { |
||
| 260 | return; |
||
| 261 | } |
||
| 262 | $post_type = get_post_type($post_ID); |
||
| 263 | $this->checkTable($post_type); |
||
| 264 | |||
| 265 | $mdata = get_metadata('post', $post_ID); |
||
| 266 | $post = get_post($post_ID); |
||
| 267 | |||
| 268 | if ($this->last_author) { |
||
| 269 | $mdata['_edit_last'][0] = $this->last_author; |
||
| 270 | } |
||
| 271 | unset($mdata['_edit_lock']); |
||
| 272 | |||
| 273 | $timeshift = (object) ['post' => $post, 'meta' => $mdata]; |
||
| 274 | $this->storeTimeshift($timeshift); |
||
| 275 | } |
||
| 276 | } |
||
| 277 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.