WordPress.org

Make WordPress Core

Ticket #10303: admin-post-diff.php

File admin-post-diff.php, 2.3 KB (added by tott, 6 years ago)

possible plugin that implements text diff when content is filtered. put in wp-content/plugins/ and activate

Line 
1<?php   
2/*
3Plugin name: Admin-Post-Diff
4Plugin URI:
5Description: Adding a diff output to the status messages displayed in post/pages editor.
6Author: Thorsten Ott
7Author URI: http://thorsten-ott.de/
8*/
9
10
11/**
12 * check if the content of the current post is different from the post_content in $_POST and store the diff in _filtered_diff post_meta
13 *
14 * @param int $post_id the post id of the post that is edited
15 * @return bool returns false if no post_id was given or $_POST['post_content'] is not set,otherwise true
16 */
17function check_post_diff( $post_id = '' ) {
18        if ( isset( $_POST['post_content'] ) && !empty( $post_id ) ) {
19                $posted_content = stripslashes_deep( $_POST['post_content'] );
20                $this_post = get_post( $post_id );
21                if ( $this_post && $content = wp_text_diff( $posted_content, $this_post->post_content ) )
22                        add_post_meta( $post_id, '_filtered_diff', $content );
23        else
24                delete_post_meta( $post_id, '_filtered_diff' );
25                return false;
26        }
27        return true;
28}
29
30/**
31 * Filter function that alters the admin message to include the diff stored in _filtered_diff post meta
32 *
33 * @param string $message the message text that should be altered. text is appended to the message
34 * @return string the altered message
35 */
36function print_post_diff( $message = '' ) {
37        global $post;
38        if ( $diff = get_post_meta( $post->ID, '_filtered_diff' ) )
39                delete_post_meta( $post->ID, '_filtered_diff' ); // make sure we show it only once
40               
41        if( !is_array( $diff ) )
42                return $message;
43       
44        $diff_txt = array_pop($diff);
45        if ( empty( $diff_txt ) ) // should not happen, but who knows
46                return $message;
47               
48        return $message . '<div class="admin-message-diff-container" style="word-wrap: break-word; word-break: break-all;"><p>' . __( 'Parts of your post have been altered. Usually this happens due to filtering prohibited or insecure code such as embedded scripts from your content.' ) . '</p><p>' . __( 'See the changes below :' ) . '</p>' . $diff_txt . '</div>';
49}
50
51/**
52 * Initialize filters and actions.
53 */
54function init_admin_post_diff() {
55        add_filter( 'post_admin_message', 'print_post_diff', 10 );
56        add_action( 'pre_post_redirect', 'check_post_diff', 10, 1);
57
58        add_filter( 'page_admin_message', 'print_post_diff', 10 );
59        add_action( 'pre_page_redirect', 'check_post_diff', 10, 1);
60}
61
62add_action( 'init', 'init_admin_post_diff' );
63
64?>