Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revision diff UI #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 85 additions & 10 deletions wp-20564.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@

class WP_20564 {

private static $instance;
private $revisioned_meta = array();

/**
* Set up the plugin actions
*/
public function __construct() {

// Actions
add_action( 'wp_restore_post_revision', array( $this, '_wp_restore_post_revision_meta'), 10, 2 );
add_action( '_wp_creating_autosave', array( $this, '_wp_autosave_post_revisioned_meta_fields' ) );
Expand All @@ -22,6 +26,20 @@ public function __construct() {
add_filter( 'get_post_metadata', array( $this, '_wp_preview_meta_filter'), 10, 4 );
add_filter( 'wp_save_post_revision_additional_check_for_changes', array( $this, '_wp_check_revisioned_meta_fields_have_changed' ), 10, 3 );

add_filter( 'wp_get_revision_ui_diff', array( $this, '_wp_get_revision_ui_diff' ), 10, 3 );

}

/**
* Creates or returns an instance of this class.
*
* @return HM_Post_Meta_Revisions A single instance of this class.
*/
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}

/**
Expand All @@ -47,23 +65,24 @@ private static function _wp_autosave_post_revisioned_meta_fields( $new_autosave
}
}

public function add_revisioned_meta( $key, $label, $display_callback = null ) {
$this->revisioned_meta[ $key ] = array(
'key' => $key,
'label' => $label,
'display_callback' => $display_callback
);
}

/**
* Determine which post meta fields should be revisioned.
* Get meta keys that should be revisioned.
*
* @access private
* @since 4.1.0
*
* @return array An array of meta keys to be revisioned.
*/
function _wp_post_revision_meta_keys() {
/**
* Filter the list of post meta keys to be revisioned.
*
* @since 4.1.0
*
* @param array $keys An array of default meta fields to be revisioned.
*/
return apply_filters( 'wp_post_revision_meta_keys', array() );
return array_keys( $this->revisioned_meta );
}

/**
Expand Down Expand Up @@ -159,7 +178,63 @@ function _wp_preview_meta_filter( $value, $object_id, $meta_key, $single ) {

return get_post_meta( $preview->ID, $meta_key, $single );
}

function _wp_get_revision_ui_diff( $return, $compare_from, $compare_to ) {

foreach ( $this->_wp_post_revision_meta_keys() as $key ) {

$meta_1 = get_metadata( 'post', $compare_from->ID, $key, false );
$meta_2 = get_metadata( 'post', $compare_to->ID, $key, false );

if ( ! empty( $this->revisioned_meta[ $key ]['display_callback'] ) ) {
$meta_1 = call_user_func( $this->revisioned_meta[ $key ]['display_callback'], $meta_1, $field, $compare_from, $compare_to );
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how this incorporates a potential display callback.

$meta_2 = call_user_func( $this->revisioned_meta[ $key ]['display_callback'], $meta_2, $field, $compare_from, $compare_to );
} else {
$meta_1 = $this->multi_implode( "\r", $meta_1 );
$meta_2 = $this->multi_implode( "\r", $meta_2 );
}

$diff = wp_text_diff( $meta_1, $meta_2, array( 'show_split_view' => true ) );

$return[] = array(
'id' => $key,
'name' => $this->revisioned_meta[ $key ]['label'],
'diff' => $diff,
);

}

return $return;

}

function multi_implode( $glue, $array ) {

if ( ! is_array( $array ) ) {
return $array;
}

$return = '';

foreach ( $array as $item ) {
if ( is_array( $item ) ) {
$return .= $this->multi_implode( $glue, $item ) . $glue;
} else {
$return .= $item . $glue;
}
}

$return = substr( $return, 0, 0 - strlen( $glue ) );

return $return;

}

}

$wp_20564 = new WP_20564;
$wp_20564 = WP_20564::get_instance();

function wp_add_revisioned_meta( $key, $label, $display_callback = null ) {
$wp_20564 = WP_20564::get_instance();
$wp_20564->add_revisioned_meta( $key, $label, $display_callback );
}