Ticket #38368: 38368.diff
File 38368.diff, 7.1 KB (added by , 8 years ago) |
---|
-
wp-includes/admin-bar.php
diff --git a/wp-includes/admin-bar.php b/wp-includes/admin-bar.php index 83deb96..d50ae11 100644
a b function _get_admin_bar_pref( $context = 'front', $user = 0 ) { 1003 1003 1004 1004 return 'true' === $pref; 1005 1005 } 1006 1007 //Load revisions browser 1008 add_action( 'wp_enqueue_scripts', 'admin_bar_revisions_browser' ); 1009 1010 /** 1011 * Main function for loading the front-end revision browser. 1012 * 1013 * @uses "wp_enqueue_scripts" action 1014 * @since 4.7.0 1015 */ 1016 function admin_bar_revisions_browser() { 1017 if ( ! current_user_can( 'edit_posts' ) ) { 1018 return; 1019 } 1020 1021 if ( ! is_single() ) { 1022 return; 1023 } 1024 1025 add_action( 'admin_bar_menu', 'admin_bar_revisions_browser_toolbar', 999 ); 1026 1027 wp_enqueue_script( 'revision-browser', '/wp-includes/js/revision-browser.js', array( 'jquery', 'wp-api' ) ); 1028 wp_enqueue_style( 'revision-browser', '/wp-includes/css/revision-browser.css' ); 1029 1030 $selectors = wp_parse_args( get_theme_support( 'revision-browser-selectors' ), array( 1031 'content' => 'entry-content', 1032 'title' => 'entry-title', 1033 ) ); 1034 1035 global $post; 1036 if ( ! is_object( $post ) ) { 1037 return; 1038 } 1039 1040 // REVBROWSER object. 1041 wp_localize_script( 'revision-browser', 'REVBROWSER', [ 1042 'post' => absint( $post->ID ), 1043 'content' => $selectors[ 'content' ], 1044 'title' => $selectors[ 'title' ], 1045 'none' => esc_html__( 'No Revisions') 1046 1047 ]); 1048 }; 1049 1050 /** 1051 * Add revision browser to toolbar 1052 * 1053 * @uses "admin_bar_menu" action 1054 * @since 4.7.0 1055 * @param WP_Admin_Bar $wp_admin_bar 1056 */ 1057 function admin_bar_revisions_browser_toolbar( $wp_admin_bar ) { 1058 $parent = 'revisions-browser'; 1059 $meta = array( 'class' => 'revisions-browser' ); 1060 $args = array( 1061 'id' => $parent, 1062 'title' => __( 'Browse Revisions' ), 1063 'href' => '#revisions', 1064 'meta' => $meta 1065 ); 1066 1067 $wp_admin_bar->add_node( $args ); 1068 1069 $wp_admin_bar->add_node( array( 1070 'parent' => $parent, 1071 'id' => $parent . '-previous', 1072 'title' => __( 'Previous' ), 1073 'href' => '#previous-revision', 1074 'meta' => $meta, 1075 ) ); 1076 1077 $wp_admin_bar->add_node( array( 1078 'parent' => $parent, 1079 'id' => $parent . '-next', 1080 'title' => __( 'Next' ), 1081 'href' => '#next-revision', 1082 'meta' => $meta, 1083 ) ); 1084 1085 } 1086 1087 -
new file wp-includes/css/revision-browser.css
diff --git a/wp-includes/css/revision-browser.css b/wp-includes/css/revision-browser.css new file mode 100644 index 0000000..9dd19d6
- + 1 #wpadminbar #wp-admin-bar-revisions-browser>.ab-item:before { 2 content: '\f321'; 3 } -
new file wp-includes/js/revision-browser.js
diff --git a/wp-includes/js/revision-browser.js b/wp-includes/js/revision-browser.js new file mode 100644 index 0000000..7e80bb6
- + 1 /** 2 * Revision Browser JS 3 * 4 * JS responsible for frontend revision browsing. 5 * 6 * @since 0.1.0 7 * @package RBR 8 */ 9 jQuery(document).ready(function ($) { 10 var $previous = $('#wp-admin-bar-revisions-browser-previous a'); 11 var $next = $('#wp-admin-bar-revisions-browser-next a'); 12 $previous.hide().css('visibility', 'hidden').attr('aria-hidden', true); 13 $next.hide().css('visibility', 'hidden').attr('aria-hidden', true); 14 15 wp.api.loadPromise.done(function () { 16 var loaded = false; 17 var revision; 18 19 $('.revisions-browser').on('click hover mouseover', function (e) { 20 e.preventDefault(); 21 initRevisions(); 22 }); 23 24 //load revisions and set up system, but only once. 25 function initRevisions() { 26 var $menu = $('#wp-admin-bar-revisions-browser'); 27 if (false === loaded) { 28 var revisions = new wp.api.collections.PostRevisions({}, {parent: REVBROWSER.post }); 29 var post = new wp.api.models.Post({id: REVBROWSER.post }); 30 $.when(revisions.fetch(), post.fetch()).then(function (d1, d2) { 31 if ('object' == typeof d1 && 0 < d1[0].length) { 32 33 revisions = d1[0].reverse(); 34 var count = d1[0].length; 35 var current = count; 36 37 if( 38 d2[0].content.rendered != revisions[count - 1].content.rendered || 39 d2[0].title.rendered != revisions[count - 1].title.rendered 40 ) { 41 revisions[current] = d2[0]; 42 } else { 43 current--; 44 } 45 46 var $content = $('.' + REVBROWSER.content); 47 var $title = $('.' + REVBROWSER.title); 48 49 updateNav(); 50 51 $previous.on('click', function (e) { 52 e.preventDefault(); 53 if (hasPrevious()) { 54 current = current - 1; 55 revision = revisions[current]; 56 placeRevision(revision); 57 updateNav(); 58 } 59 60 }); 61 62 $next.on('click', function (e) { 63 e.preventDefault(); 64 if (current + 1 in revisions) { 65 current = current + 1; 66 revision = revisions[current]; 67 placeRevision(revision); 68 updateNav(); 69 } 70 71 72 }); 73 74 function hasPrevious() { 75 if (current - 1 in revisions) { 76 return true; 77 } 78 } 79 80 function hasNext() { 81 if (current + 1 in revisions) { 82 return true; 83 } 84 } 85 86 function updateNav() { 87 if (hasNext()) { 88 $next.show().css('visibility', 'visible').attr('aria-hidden', false); 89 } else { 90 $next.hide().css('visibility', 'hidden').attr('aria-hidden', true); 91 } 92 93 if (hasPrevious()) { 94 $previous.show().css('visibility', 'visible').attr('aria-hidden', false); 95 } else { 96 $previous.hide().css('visibility', 'hidden').attr('aria-hidden', true); 97 } 98 } 99 100 101 function placeRevision(revision) { 102 103 $title.html(revision.title.rendered); 104 $content.html(revision.content.rendered); 105 106 } 107 108 } else { 109 $menu.first('a').html( REVBROWSER.none); 110 $menu.find('.ab-sub-wrapper').remove(); 111 } 112 113 }); 114 115 } 116 117 loaded = true; 118 } 119 120 121 }); 122 123 });