From d72b7bbd547551f87e5ae1832e2932a101b2706a Mon Sep 17 00:00:00 2001 From: Phil Cable Date: Wed, 12 Apr 2017 15:13:12 -0700 Subject: [PATCH 1/6] Post changes made on a secondary site back to people.wsu.edu This is super basic - just want to get it commited before making more substantial changes. --- includes/class-wsuwp-people-post-type.php | 12 ++-- js/admin-person.min.js | 2 +- src/js/admin-person.js | 70 +++++++++++++++++++++++ 3 files changed, 78 insertions(+), 6 deletions(-) diff --git a/includes/class-wsuwp-people-post-type.php b/includes/class-wsuwp-people-post-type.php index 34ba293..a719a8d 100644 --- a/includes/class-wsuwp-people-post-type.php +++ b/includes/class-wsuwp-people-post-type.php @@ -418,19 +418,21 @@ public function admin_enqueue_scripts( $hook_suffix ) { 'rest_url' => WSUWP_People_Directory::REST_URL(), ); - // Make a REST request for data from people.wsu.edu when editing a person. + // Additional variables for editing a profile from sites other than the primary directory. if ( 'post.php' === $hook_suffix && apply_filters( 'wsuwp_people_display', true ) ) { $profile_vars['make_request'] = true; + $profile_vars['nonce'] = WSUWP_People_Directory::create_rest_nonce(); + $profile_vars['uid'] = wp_get_current_user()->ID; } wp_enqueue_style( 'wsuwp-people-admin', plugins_url( 'css/admin-person.css', dirname( __FILE__ ) ), array(), WSUWP_People_Directory::$version ); wp_enqueue_script( 'wsuwp-people-admin', plugins_url( 'js/admin-person.min.js', dirname( __FILE__ ) ), array( 'jquery-ui-tabs', 'underscore' ), WSUWP_People_Directory::$version, true ); wp_localize_script( 'wsuwp-people-admin', 'wsupeople', $profile_vars ); - } - // Disable autosaving on new people posts if this isn't people.wsu.edu. - if ( 'post-new.php' === $hook_suffix && apply_filters( 'wsuwp_people_display', true ) ) { - wp_dequeue_script( 'autosave' ); + // Disable autosaving for sites other than the primary directory. + if ( apply_filters( 'wsuwp_people_display', true ) ) { + wp_dequeue_script( 'autosave' ); + } } if ( 'edit.php' === $hook_suffix ) { diff --git a/js/admin-person.min.js b/js/admin-person.min.js index 959c63c..c09ba46 100644 --- a/js/admin-person.min.js +++ b/js/admin-person.min.js @@ -1 +1 @@ -!function(a,b,c){a(c).ready(function(){function c(){return a(".wsuwp-profile-photo-id").map(function(){return parseInt(a(this).val())}).get()}function d(c){var d=a(".wsuwp-profile-titles .wsuwp-profile-add-repeatable"),g=d.find("a").data("label"),h=d.find("a").data("name"),i=a(".wsuwp-profile-degrees .wsuwp-profile-add-repeatable"),k=i.find("a").data("label"),l=i.find("a").data("name");if(j.focus().val(c.title.rendered),m.html(c.first_name),n.html(c.last_name),o.html(c.position_title),p.html(c.office),q.html(c.address),r.html(c.phone),s.html(c.email),a("#_wsuwp_profile_alt_office").val(c.office_alt),a("#_wsuwp_profile_alt_phone").val(c.phone_alt),a("#_wsuwp_profile_alt_email").val(c.email_alt),a("#_wsuwp_profile_website").val(c.website),b.tinymce.get("content").setContent(c.content.rendered),b.tinymce.get("_wsuwp_profile_bio_unit").setContent(c.bio_unit),b.tinymce.get("_wsuwp_profile_bio_university").setContent(c.bio_university),a.each(c.working_titles,function(b,c){var e=a("[name='_wsuwp_profile_title[]']")[b];e?a(e).val(c):d.before(f({label:g,name:h,value:c}))}),""!==a(".use-title").val()){var t=a(".use-title").val().split(" ");a.each(t,function(b,c){a(".wsuwp-profile-titles p").eq(c).addClass("selected").find(".screen-reader-text").text("Deselect")})}a.each(c.degree,function(b,c){var d=a("[name='_wsuwp_profile_degree[]']")[b];d?a(d).val(c):i.before(f({label:k,name:l,value:c}))}),0!==c._embedded["wp:photos"]&&a.each(c._embedded["wp:photos"],function(a,b){e(b)}),c._embedded["wp:featuredmedia"]&&0!==c._embedded["wp:featuredmedia"]&&e(c._embedded["wp:featuredmedia"][0]),""!==a(".use-photo").val()?a(".wsuwp-profile-photo-wrapper").eq(a(".use-photo").val()).addClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Deselect"):a(".wsuwp-profile-photo-wrapper").eq(0).addClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Deselect"),c._embedded["wp:term"]&&0!==c._embedded["wp:term"]&&(a.each(c._embedded["wp:term"],function(b,c){c&&a.each(c,function(b,c){"post_tag"===c.taxonomy?a("#new-tag-post_tag").val(function(a,b){return b+c.name+", "}):a("#"+c.taxonomy+"-all").find(".selectit:contains('"+c.name+"')").find("input[type='checkbox']").prop("checked",!0)})}),a(".tagadd").trigger("click"),j.focus())}function e(b){var d=b.media_details,e=d.sizes.thumbnail,f=e?d.sizes.thumbnail.source_url:b.source_url,h=e?d.sizes.thumbnail.width:d.width,i=e?d.sizes.thumbnail.height:d.height;-1===a.inArray(b.id,c())&&u.append(g({src:f,width:h,height:i,id:b.id,alt:b.alt,url:b.source_url,title:b.title.rendered,full_width:d.width,full_height:d.height}))}var f=_.template(a(".wsuwp-profile-repeatable-field-template").html()),g=_.template(a("#photo-template").html()),h=a("#publishing-action .spinner"),i=a(".refresh-card .spinner"),j=a("#title"),k=a("#_wsuwp_profile_ad_nid"),l=a(".profile-card-data"),m=a("#_wsuwp_profile_ad_name_first"),n=a("#_wsuwp_profile_ad_name_last"),o=a("#_wsuwp_profile_ad_title"),p=a("#_wsuwp_profile_ad_office"),q=a("#_wsuwp_profile_ad_address"),r=a("#_wsuwp_profile_ad_phone"),s=a("#_wsuwp_profile_ad_email"),t=a("#confirm-ad-hash"),u=a(".wsuwp-profile-photo-collection"),v=a("#load-ad-data"),w=a("#confirm-ad-data"),x=a("#refresh-ad-data"),y=a("#undo-ad-data-refresh");a("#wsuwp-profile-about-wrapper").tabs({active:0}),a(".wsuwp-profile-about-tabs").on("click",".select",function(){var b=a(this).closest("li"),c=a(".use-bio");b.toggleClass("selected"),b.hasClass("selected")?(c.val(b.data("bio")),b.find(".screen-reader-text").text("Deselect"),b.siblings().removeClass("selected").find(".screen-reader-text").text("Select")):(c.val(""),b.find(".screen-reader-text").text("Select"))}),b.wsupeople.make_request&&jQuery.ajax({url:b.wsupeople.rest_url,data:{_embed:!0,wsu_nid:k.val()}}).done(function(a){0!==a.length&&d(a[0])}),a(".wsuwp-profile-add-repeatable").on("click","a",function(b){b.preventDefault(),a(this).closest("p").before(f({label:a(this).data("label"),name:a(this).data("name"),value:""}))}),a(".wsuwp-profile-repeatable-field").on("click",".remove",function(){a(this).closest("p").remove()}),a(".wsuwp-profile-titles").on("click",".select",function(){var b=a(this).closest("p");b.toggleClass("selected");var c=a(".wsuwp-profile-titles .selected").map(function(){return a(this).index()}).get();a(".use-title").val(c.join(" ")),b.hasClass("selected")?b.find(".screen-reader-text").text("Deselect"):b.find(".screen-reader-text").text("Select")}),a("#load-ad-data, #refresh-ad-data").on("click",function(c){if(c.preventDefault(),c.target.disabled=!0,""===k.val())return void b.alert("Please enter a Network ID");a(c.target).is("#refresh-ad-data")?(i.css("visibility","visible"),l.each(function(){a(this).data("original",a(this).html())})):h.css("visibility","visible");var e={action:"wsu_people_get_data_by_nid",_ajax_nonce:b.wsupeople.nid_nonce,network_id:k.val(),request_from:b.wsupeople.request_from,is_refresh:a(c.target).is("#refresh-ad-data")?"true":"false"};a.post(b.ajaxurl,e,function(e){if(a(".spinner").css("visibility","hidden"),c.target.disabled=!1,!e.success)return void b.alert(e.data);e.data.id?d(e.data):(m.html(e.data.given_name),n.html(e.data.surname),o.html(e.data.title),p.html(e.data.office),q.html(e.data.street_address),r.html(e.data.telephone_number),s.html(e.data.email),t.val(e.data.confirm_ad_hash)),w.removeClass("profile-hide-button"),y.removeClass("profile-hide-button"),x.addClass("profile-hide-button")})}),w.on("click",function(c){c.preventDefault(),c.target.disabled=!0,a(c.target).hasClass("refresh")?(i.css("visibility","visible"),y.addClass("profile-hide-button")):(h.css("visibility","visible"),v.addClass("profile-hide-button"));var d={action:"wsu_people_confirm_nid_data",_ajax_nonce:b.wsupeople.nid_nonce,network_id:k.val(),confirm_ad_hash:t.val(),post_id:a("#post_ID").val(),request_from:b.wsupeople.request_from},e=a(".load-ad-container .description"),f=a("#publish");a.post(b.ajaxurl,d,function(b){b.success&&(""===j.val()&&(j.focus(),j.val(a("#_wsuwp_profile_ad_name_first").html()+" "+a("#_wsuwp_profile_ad_name_last").html())),k.attr("readonly",!0),e.html("The WSU Network ID used to populate this profile's data from Active Directory."),a(".spinner").css("visibility","hidden"),w.addClass("profile-hide-button"),f.removeClass("profile-hide-button"))})}),y.on("click",function(b){b.preventDefault(),l.each(function(){a(this).html(a(this).data("original"))}),w.addClass("profile-hide-button"),y.addClass("profile-hide-button"),x.removeClass("profile-hide-button").disabled=!1});var z,A=a(".wsuwp-profile-add-photo"),B=a(".wsuwp-profile-photo-controls-tooltip");A.on("click",function(d){if(d.preventDefault(),z)return void z.open();z=b.wp.media({title:"Select or Upload Your Photos",multiple:!0,library:{type:"image",uploadedTo:b.wsupeople.post_id},button:{text:"Use photo(s)"}}),z.on("select",function(){var b=z.state().get("selection");a.each(b.models,function(b,d){var e=d.toJSON(),f=e.sizes.hasOwnProperty("thumbnail"),h=f?e.sizes.thumbnail.url:e.url,i=f?e.sizes.thumbnail.width:e.width,j=f?e.sizes.thumbnail.height:e.height;-1===a.inArray(e.id,c())&&u.append(g({src:h,width:i,height:j,id:e.id,alt:e.alt,url:e.url,title:e.title,full_width:e.width,full_height:e.height}))})}),z.open()}),u.on("mouseover",".wsuwp-profile-photo-controls button",function(){var a=this.getAttribute("aria-label"),b=this.getBoundingClientRect(),c=u[0].getBoundingClientRect();B.css({top:b.bottom-c.top+"px",left:b.right-c.left-B.width()/2-6+"px"}).show().find(".wsuwp-profile-photo-controls-tooltip-inner").html(a)}),u.on("mouseleave",".wsuwp-profile-photo-controls button",function(){B.hide()}),u.on("click",".wsuwp-profile-photo-remove",function(){B.hide(),a(this).closest(".wsuwp-profile-photo-wrapper").remove()}),u.on("click",".wsuwp-profile-photo-select",function(){var b=a(this),c=b.closest(".wsuwp-profile-photo-wrapper"),d=a(".use-photo");c.toggleClass("selected").siblings().removeClass("selected"),c.hasClass("selected")?(d.val(a(".wsuwp-profile-photo-wrapper").index(c)),b.attr("aria-label","Deselect"),c.siblings().removeClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Select")):(d.val(""),b.attr("aria-label","Select"))})})}(jQuery,window,document); \ No newline at end of file +!function(a,b,c){a(c).ready(function(){function c(){return a(".wsuwp-profile-photo-id").map(function(){return parseInt(a(this).val())}).get()}function d(c){var d=a(".wsuwp-profile-titles .wsuwp-profile-add-repeatable"),g=d.find("a").data("label"),h=d.find("a").data("name"),i=a(".wsuwp-profile-degrees .wsuwp-profile-add-repeatable"),l=i.find("a").data("label"),t=i.find("a").data("name");if(k.data("post-id",c.id),j.focus().val(c.title.rendered),m.html(c.first_name),n.html(c.last_name),o.html(c.position_title),p.html(c.office),q.html(c.address),r.html(c.phone),s.html(c.email),a("#_wsuwp_profile_alt_office").val(c.office_alt),a("#_wsuwp_profile_alt_phone").val(c.phone_alt),a("#_wsuwp_profile_alt_email").val(c.email_alt),a("#_wsuwp_profile_website").val(c.website),b.tinymce.get("content").setContent(c.content.rendered),b.tinymce.get("_wsuwp_profile_bio_unit").setContent(c.bio_unit),b.tinymce.get("_wsuwp_profile_bio_university").setContent(c.bio_university),a.each(c.working_titles,function(b,c){var e=a("[name='_wsuwp_profile_title[]']")[b];e?a(e).val(c):d.before(f({label:g,name:h,value:c}))}),""!==a(".use-title").val()){var u=a(".use-title").val().split(" ");a.each(u,function(b,c){a(".wsuwp-profile-titles p").eq(c).addClass("selected").find(".screen-reader-text").text("Deselect")})}a.each(c.degree,function(b,c){var d=a("[name='_wsuwp_profile_degree[]']")[b];d?a(d).val(c):i.before(f({label:l,name:t,value:c}))}),0!==c._embedded["wp:photos"]&&a.each(c._embedded["wp:photos"],function(a,b){e(b)}),c._embedded["wp:featuredmedia"]&&0!==c._embedded["wp:featuredmedia"]&&e(c._embedded["wp:featuredmedia"][0]),""!==a(".use-photo").val()?a(".wsuwp-profile-photo-wrapper").eq(a(".use-photo").val()).addClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Deselect"):a(".wsuwp-profile-photo-wrapper").eq(0).addClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Deselect"),c._embedded["wp:term"]&&0!==c._embedded["wp:term"]&&(a.each(c._embedded["wp:term"],function(b,c){c&&a.each(c,function(b,c){"post_tag"===c.taxonomy?a("#new-tag-post_tag").val(function(a,b){return b+c.name+", "}):a("#"+c.taxonomy+"-all").find(".selectit:contains('"+c.name+"')").find("input[type='checkbox']").prop("checked",!0)})}),a(".tagadd").trigger("click"),j.focus())}function e(b){var d=b.media_details,e=d.sizes.thumbnail,f=e?d.sizes.thumbnail.source_url:b.source_url,h=e?d.sizes.thumbnail.width:d.width,i=e?d.sizes.thumbnail.height:d.height;-1===a.inArray(b.id,c())&&u.append(g({src:f,width:h,height:i,id:b.id,alt:b.alt,url:b.source_url,title:b.title.rendered,full_width:d.width,full_height:d.height}))}var f=_.template(a(".wsuwp-profile-repeatable-field-template").html()),g=_.template(a("#photo-template").html()),h=a("#publishing-action .spinner"),i=a(".refresh-card .spinner"),j=a("#title"),k=a("#_wsuwp_profile_ad_nid"),l=a(".profile-card-data"),m=a("#_wsuwp_profile_ad_name_first"),n=a("#_wsuwp_profile_ad_name_last"),o=a("#_wsuwp_profile_ad_title"),p=a("#_wsuwp_profile_ad_office"),q=a("#_wsuwp_profile_ad_address"),r=a("#_wsuwp_profile_ad_phone"),s=a("#_wsuwp_profile_ad_email"),t=a("#confirm-ad-hash"),u=a(".wsuwp-profile-photo-collection"),v=a("#load-ad-data"),w=a("#confirm-ad-data"),x=a("#refresh-ad-data"),y=a("#undo-ad-data-refresh");a("#wsuwp-profile-about-wrapper").tabs({active:0}),a(".wsuwp-profile-about-tabs").on("click",".select",function(){var b=a(this).closest("li"),c=a(".use-bio");b.toggleClass("selected"),b.hasClass("selected")?(c.val(b.data("bio")),b.find(".screen-reader-text").text("Deselect"),b.siblings().removeClass("selected").find(".screen-reader-text").text("Select")):(c.val(""),b.find(".screen-reader-text").text("Select"))}),b.wsupeople.make_request&&jQuery.ajax({url:b.wsupeople.rest_url,data:{_embed:!0,wsu_nid:k.val()}}).done(function(a){0!==a.length&&d(a[0])}),a(".wsuwp-profile-add-repeatable").on("click","a",function(b){b.preventDefault(),a(this).closest("p").before(f({label:a(this).data("label"),name:a(this).data("name"),value:""}))}),a(".wsuwp-profile-repeatable-field").on("click",".remove",function(){a(this).closest("p").remove()}),a(".wsuwp-profile-titles").on("click",".select",function(){var b=a(this).closest("p");b.toggleClass("selected");var c=a(".wsuwp-profile-titles .selected").map(function(){return a(this).index()}).get();a(".use-title").val(c.join(" ")),b.hasClass("selected")?b.find(".screen-reader-text").text("Deselect"):b.find(".screen-reader-text").text("Select")}),a("#load-ad-data, #refresh-ad-data").on("click",function(c){if(c.preventDefault(),c.target.disabled=!0,""===k.val())return void b.alert("Please enter a Network ID");a(c.target).is("#refresh-ad-data")?(i.css("visibility","visible"),l.each(function(){a(this).data("original",a(this).html())})):h.css("visibility","visible");var e={action:"wsu_people_get_data_by_nid",_ajax_nonce:b.wsupeople.nid_nonce,network_id:k.val(),request_from:b.wsupeople.request_from,is_refresh:a(c.target).is("#refresh-ad-data")?"true":"false"};a.post(b.ajaxurl,e,function(e){if(a(".spinner").css("visibility","hidden"),c.target.disabled=!1,!e.success)return void b.alert(e.data);e.data.id?d(e.data):(m.html(e.data.given_name),n.html(e.data.surname),o.html(e.data.title),p.html(e.data.office),q.html(e.data.street_address),r.html(e.data.telephone_number),s.html(e.data.email),t.val(e.data.confirm_ad_hash)),w.removeClass("profile-hide-button"),y.removeClass("profile-hide-button"),x.addClass("profile-hide-button")})}),w.on("click",function(c){c.preventDefault(),c.target.disabled=!0,a(c.target).hasClass("refresh")?(i.css("visibility","visible"),y.addClass("profile-hide-button")):(h.css("visibility","visible"),v.addClass("profile-hide-button"));var d={action:"wsu_people_confirm_nid_data",_ajax_nonce:b.wsupeople.nid_nonce,network_id:k.val(),confirm_ad_hash:t.val(),post_id:a("#post_ID").val(),request_from:b.wsupeople.request_from},e=a(".load-ad-container .description"),f=a("#publish");a.post(b.ajaxurl,d,function(b){b.success&&(""===j.val()&&(j.focus(),j.val(a("#_wsuwp_profile_ad_name_first").html()+" "+a("#_wsuwp_profile_ad_name_last").html())),k.attr("readonly",!0),e.html("The WSU Network ID used to populate this profile's data from Active Directory."),a(".spinner").css("visibility","hidden"),w.addClass("profile-hide-button"),f.removeClass("profile-hide-button"))})}),y.on("click",function(b){b.preventDefault(),l.each(function(){a(this).html(a(this).data("original"))}),w.addClass("profile-hide-button"),y.addClass("profile-hide-button"),x.removeClass("profile-hide-button").disabled=!1});var z,A=a(".wsuwp-profile-add-photo"),B=a(".wsuwp-profile-photo-controls-tooltip");A.on("click",function(d){if(d.preventDefault(),z)return void z.open();z=b.wp.media({title:"Select or Upload Your Photos",multiple:!0,library:{type:"image",uploadedTo:b.wsupeople.post_id},button:{text:"Use photo(s)"}}),z.on("select",function(){var b=z.state().get("selection");a.each(b.models,function(b,d){var e=d.toJSON(),f=e.sizes.hasOwnProperty("thumbnail"),h=f?e.sizes.thumbnail.url:e.url,i=f?e.sizes.thumbnail.width:e.width,j=f?e.sizes.thumbnail.height:e.height;-1===a.inArray(e.id,c())&&u.append(g({src:h,width:i,height:j,id:e.id,alt:e.alt,url:e.url,title:e.title,full_width:e.width,full_height:e.height}))})}),z.open()}),u.on("mouseover",".wsuwp-profile-photo-controls button",function(){var a=this.getAttribute("aria-label"),b=this.getBoundingClientRect(),c=u[0].getBoundingClientRect();B.css({top:b.bottom-c.top+"px",left:b.right-c.left-B.width()/2-6+"px"}).show().find(".wsuwp-profile-photo-controls-tooltip-inner").html(a)}),u.on("mouseleave",".wsuwp-profile-photo-controls button",function(){B.hide()}),u.on("click",".wsuwp-profile-photo-remove",function(){B.hide(),a(this).closest(".wsuwp-profile-photo-wrapper").remove()}),u.on("click",".wsuwp-profile-photo-select",function(){var b=a(this),c=b.closest(".wsuwp-profile-photo-wrapper"),d=a(".use-photo");c.toggleClass("selected").siblings().removeClass("selected"),c.hasClass("selected")?(d.val(a(".wsuwp-profile-photo-wrapper").index(c)),b.attr("aria-label","Deselect"),c.siblings().removeClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Select")):(d.val(""),b.attr("aria-label","Select"))}),b.wsupeople.make_request&&a("#publish").on("click",function(){var c={},d=a("#title").val(),e=a("#_wsuwp_profile_alt_office").val(),f=a("#_wsuwp_profile_alt_phone").val(),g=a("#_wsuwp_profile_alt_email").val(),h=a("#_wsuwp_profile_website").val(),i=b.tinymce.get("content").getContent(),j=b.tinymce.get("_wsuwp_profile_bio_unit").getContent(),l=b.tinymce.get("_wsuwp_profile_bio_university").getContent();d&&(c.title=d),e&&(c.office_alt=e),f&&(c.phone_alt=f),g&&(c.email_alt=g),h&&(c.website=h),i&&(c.content=i),j&&(c.bio_unit=j),l&&(c.bio_university=l),a.ajax({url:b.wsupeople.rest_url+"/"+k.data("post-id"),method:"POST",beforeSend:function(a){a.setRequestHeader("X-WP-Nonce",b.wsupeople.nonce),a.setRequestHeader("X-WSUWP-UID",b.wsupeople.uid)},data:c})})})}(jQuery,window,document); \ No newline at end of file diff --git a/src/js/admin-person.js b/src/js/admin-person.js index beebd17..2c810c7 100644 --- a/src/js/admin-person.js +++ b/src/js/admin-person.js @@ -41,6 +41,9 @@ degree_label = $degrees.find( "a" ).data( "label" ), degree_name = $degrees.find( "a" ).data( "name" ); + // Store the post ID as a data attribute of the NID field. + $nid.data( "post-id", data.id ); + // Populate AD data. $post_title.focus().val( data.title.rendered ); $given_name.html( data.first_name ); @@ -469,5 +472,72 @@ $button.attr( "aria-label", "Select" ); } } ); + + // Post data to the user's people.wsu.edu profile. + if ( window.wsupeople.make_request ) { + $( "#publish" ).on( "click", function() { + var data = {}, + name = $( "#title" ).val(), + office = $( "#_wsuwp_profile_alt_office" ).val(), + phone = $( "#_wsuwp_profile_alt_phone" ).val(), + email = $( "#_wsuwp_profile_alt_email" ).val(), + website = $( "#_wsuwp_profile_website" ).val(), + titles = false, + degrees = false, + personal_bio = window.tinymce.get( "content" ).getContent(), + unit_bio = window.tinymce.get( "_wsuwp_profile_bio_unit" ).getContent(), + university_bio = window.tinymce.get( "_wsuwp_profile_bio_university" ).getContent(); + + if ( name ) { + data.title = name; + } + + if ( office ) { + data.office_alt = office; + } + + if ( phone ) { + data.phone_alt = phone; + } + + if ( email ) { + data.email_alt = email; + } + + if ( website ) { + data.website = website; + } + + if ( titles ) { + data.working_titles = titles; + } + + if ( degrees ) { + data.degree = degrees; + } + + if ( personal_bio ) { + data.content = personal_bio; + } + + if ( unit_bio ) { + data.bio_unit = unit_bio; + } + + if ( university_bio ) { + data.bio_university = university_bio; + } + + $.ajax( { + url: window.wsupeople.rest_url + "/" + $nid.data( "post-id" ), + method: "POST", + beforeSend: function( xhr ) { + xhr.setRequestHeader( "X-WP-Nonce", window.wsupeople.nonce ); + xhr.setRequestHeader( "X-WSUWP-UID", window.wsupeople.uid ); + }, + data: data + } ); + } ); + } } ); }( jQuery, window, document ) ); From 6e806af52404ceb1d9c9cc670ccbe90b3c5d1f41 Mon Sep 17 00:00:00 2001 From: Phil Cable Date: Thu, 13 Apr 2017 11:14:09 -0700 Subject: [PATCH 2/6] Split the "Edit Profile" JavaScript into two files This might be madness, but it seemed like the right course yesterday afternoon, so I'm going to stick to it. The code in `admin-edit-profile-secondary.js` is only needed on sites other than the primary directory - even though some of its functions are referenced in `admin-edit-profile.js`. But, those functions will only be called on sites other than the primary directory. I've occasionally seen an error where the script is trying to `setContent` for the editors before TinyMCE is initialized, which informed the decision to enqueue `admin-edit-profile-secondary.js` from within `wp_enqueue_editor`. I haven't seen it since, so I'm hoping this has made it more reliable. The `secondary` script checks for changes before posting back to people.wsu.edu. This is probably overkill, but it seemed like a good idea at the time. Lastly, this is the start of an effort to (re)name JavaScript files in a way that better indicates where they're used. --- Gruntfile.js | 12 +- includes/class-wsuwp-people-post-type.php | 47 +- js/admin-edit-profile-secondary.min.js | 1 + js/admin-edit-profile.min.js | 1 + js/admin-person.min.js | 1 - src/js/admin-edit-profile-secondary.js | 334 +++++++++++++ src/js/admin-edit-profile.js | 262 +++++++++++ src/js/admin-person.js | 543 ---------------------- 8 files changed, 642 insertions(+), 559 deletions(-) create mode 100644 js/admin-edit-profile-secondary.min.js create mode 100644 js/admin-edit-profile.min.js delete mode 100644 js/admin-person.min.js create mode 100644 src/js/admin-edit-profile-secondary.js create mode 100644 src/js/admin-edit-profile.js delete mode 100644 src/js/admin-person.js diff --git a/Gruntfile.js b/Gruntfile.js index 1936b42..678b8b8 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -104,6 +104,14 @@ module.exports = function( grunt ) { src: "src/js/admin-card-shortcode.js", dest: "js/admin-card-shortcode.min.js" }, + admin_edit_profile: { + src: "src/js/admin-edit-profile.js", + dest: "js/admin-edit-profile.min.js" + }, + admin_edit_profile_secondary: { + src: "src/js/admin-edit-profile-secondary.js", + dest: "js/admin-edit-profile-secondary.min.js" + }, admin_page: { src: "src/js/admin-page.js", dest: "js/admin-page.min.js" @@ -116,10 +124,6 @@ module.exports = function( grunt ) { src: "src/js/admin-people-sync.js", dest: "js/admin-people-sync.min.js" }, - admin_person: { - src: "src/js/admin-person.js", - dest: "js/admin-person.min.js" - }, admin_user_profile: { src: "src/js/admin-user-profile.js", dest: "js/admin-user-profile.min.js" diff --git a/includes/class-wsuwp-people-post-type.php b/includes/class-wsuwp-people-post-type.php index a719a8d..fc87b23 100644 --- a/includes/class-wsuwp-people-post-type.php +++ b/includes/class-wsuwp-people-post-type.php @@ -321,6 +321,7 @@ public function setup_hooks() { add_filter( 'manage_taxonomies_for_' . self::$post_type_slug . '_columns', array( $this, 'manage_people_taxonomy_columns' ) ); if ( apply_filters( 'wsuwp_people_display', true ) ) { + add_action( 'wp_enqueue_editor', array( $this, 'admin_enqueue_secondary_scripts' ) ); add_filter( 'manage_' . self::$post_type_slug . '_posts_columns', array( $this, 'add_people_bio_column' ) ); add_action( 'manage_posts_custom_column', array( $this, 'bio_column' ), 10, 2 ); add_action( 'quick_edit_custom_box', array( $this, 'display_bio_edit' ), 10, 2 ); @@ -415,18 +416,10 @@ public function admin_enqueue_scripts( $hook_suffix ) { 'nid_nonce' => wp_create_nonce( 'wsu-people-nid-lookup' ), 'post_id' => $post->ID, 'request_from' => ( apply_filters( 'wsuwp_people_display', true ) ) ? 'rest' : 'ad', - 'rest_url' => WSUWP_People_Directory::REST_URL(), ); - // Additional variables for editing a profile from sites other than the primary directory. - if ( 'post.php' === $hook_suffix && apply_filters( 'wsuwp_people_display', true ) ) { - $profile_vars['make_request'] = true; - $profile_vars['nonce'] = WSUWP_People_Directory::create_rest_nonce(); - $profile_vars['uid'] = wp_get_current_user()->ID; - } - wp_enqueue_style( 'wsuwp-people-admin', plugins_url( 'css/admin-person.css', dirname( __FILE__ ) ), array(), WSUWP_People_Directory::$version ); - wp_enqueue_script( 'wsuwp-people-admin', plugins_url( 'js/admin-person.min.js', dirname( __FILE__ ) ), array( 'jquery-ui-tabs', 'underscore' ), WSUWP_People_Directory::$version, true ); + wp_enqueue_script( 'wsuwp-people-admin', plugins_url( 'js/admin-edit-profile.min.js', dirname( __FILE__ ) ), array( 'jquery-ui-tabs', 'underscore' ), WSUWP_People_Directory::$version, true ); wp_localize_script( 'wsuwp-people-admin', 'wsupeople', $profile_vars ); // Disable autosaving for sites other than the primary directory. @@ -435,14 +428,46 @@ public function admin_enqueue_scripts( $hook_suffix ) { } } - if ( 'edit.php' === $hook_suffix ) { + if ( 'edit.php' === $hook_suffix && apply_filters( 'wsuwp_people_display', true ) ) { wp_enqueue_style( 'wsuwp-people-admin', plugins_url( 'css/admin-people.css', dirname( __FILE__ ) ), array(), WSUWP_People_Directory::$version ); wp_enqueue_script( 'wsuwp-people-admin', plugins_url( 'js/admin-people.min.js', dirname( __FILE__ ) ), array( 'jquery' ), WSUWP_People_Directory::$version ); wp_localize_script( 'wsuwp-people-admin', 'wsupeople', array( 'nonce' => wp_create_nonce( 'person-meta' ), ) ); } + } + + /** + * Enqueue the scripts and styles used in the admin for sites other than the primary directory. + * + * @since 0.3.0 + * + * @param array $to_load Contains boolean values whether TinyMCE and Quicktags are being loaded. + */ + public function admin_enqueue_secondary_scripts( $to_load ) { + $screen = get_current_screen(); + + if ( self::$post_type_slug !== $screen->post_type ) { + return; + } + + // Make sure TinyMCE is loaded before loading the script. + if ( empty( $to_load['tinymce'] ) ) { + return; + } + + $profile_vars = array( + 'rest_url' => WSUWP_People_Directory::REST_URL(), + ); + + if ( 'add' !== $screen->action ) { + $profile_vars['load_data'] = true; + $profile_vars['nonce'] = WSUWP_People_Directory::create_rest_nonce(); + $profile_vars['uid'] = wp_get_current_user()->ID; + } + wp_enqueue_script( 'wsuwp-people-edit-profile-secondary', plugins_url( 'src/js/admin-edit-profile-secondary.js', dirname( __FILE__ ) ), array( 'jquery', 'underscore' ), WSUWP_People_Directory::$version, true ); + wp_localize_script( 'wsuwp-people-edit-profile-secondary', 'wsuwp_people_edit_profile_secondary', $profile_vars ); } /** @@ -565,7 +590,7 @@ public function edit_form_after_editor( $post ) {
' . wp_kses_post( apply_filters( 'the_content', $value ) ) . '
'; } else { wp_editor( $value, $args['meta_key'] ); } diff --git a/js/admin-edit-profile-secondary.min.js b/js/admin-edit-profile-secondary.min.js new file mode 100644 index 0000000..7e58a70 --- /dev/null +++ b/js/admin-edit-profile-secondary.min.js @@ -0,0 +1 @@ +function populate_from_people_directory(a){var b=jQuery,c=_.template(b(".wsuwp-profile-repeatable-field-template").html()),d=b(".wsuwp-profile-titles .wsuwp-profile-add-repeatable"),e=d.find("a").data("label"),f=d.find("a").data("name"),g=b(".wsuwp-profile-degrees .wsuwp-profile-add-repeatable"),h=g.find("a").data("label"),i=g.find("a").data("name"),j=window.tinymce.get("_wsuwp_profile_bio_unit"),k=window.tinymce.get("_wsuwp_profile_bio_university");if(b("#_wsuwp_profile_ad_nid").data("post-id",a.id),b("#title").focus().val(a.title.rendered).data("original",a.title.rendered),b("#_wsuwp_profile_ad_name_first").html(a.first_name),b("#_wsuwp_profile_ad_name_last").html(a.last_name),b("#_wsuwp_profile_ad_title").html(a.position_title),b("#_wsuwp_profile_ad_office").html(a.office),b("#_wsuwp_profile_ad_address").html(a.address),b("#_wsuwp_profile_ad_phone").html(a.phone),b("#_wsuwp_profile_ad_email").html(a.email),b("#_wsuwp_profile_alt_office").val(a.office_alt).data("original",a.office_alt),b("#_wsuwp_profile_alt_phone").val(a.phone_alt).data("original",a.phone_alt),b("#_wsuwp_profile_alt_email").val(a.email_alt).data("original",a.email_alt),b("#_wsuwp_profile_website").val(a.website).data("original",a.website),window.tinymce.get("content").setContent(a.content.rendered),profile_original_personal_bio=a.content.rendered,j?(j.setContent(a.bio_unit),profile_original_unit_bio=a.bio_unit):b("#bio_unit .readonly").html(a.bio_unit),k?(k.setContent(a.bio_university),profile_original_university_bio=a.bio_university):b("#bio_university .readonly").html(a.bio_university),b.each(a.working_titles,function(a,g){var h=b("[name='_wsuwp_profile_title[]']")[a];h?b(h).val(g):d.before(c({label:e,name:f,value:g}))}),profile_original_titles=a.working_titles,""!==b(".use-title").val()){var l=b(".use-title").val().split(" ");b.each(l,function(a,c){b(".wsuwp-profile-titles p").eq(c).addClass("selected").find(".screen-reader-text").text("Deselect")})}b.each(a.degree,function(a,d){var e=b("[name='_wsuwp_profile_degree[]']")[a];e?b(e).val(d):g.before(c({label:h,name:i,value:d}))}),profile_original_degrees=a.degree,0!==a._embedded["wp:photos"]&&b.each(a._embedded["wp:photos"],function(a,b){populate_photos(b)}),a._embedded["wp:featuredmedia"]&&0!==a._embedded["wp:featuredmedia"]&&populate_photos(a._embedded["wp:featuredmedia"][0]),""!==b(".use-photo").val()?b(".wsuwp-profile-photo-wrapper").eq(b(".use-photo").val()).addClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Deselect"):b(".wsuwp-profile-photo-wrapper").eq(0).addClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Deselect"),a._embedded["wp:term"]&&0!==a._embedded["wp:term"]&&(b.each(a._embedded["wp:term"],function(a,c){c&&b.each(c,function(a,c){"post_tag"===c.taxonomy?b("#new-tag-post_tag").val(function(a,b){return b+c.name+", "}):b("#"+c.taxonomy+"-all").find(".selectit:contains('"+c.name+"')").find("input[type='checkbox']").prop("checked",!0)})}),b(".tagadd").trigger("click"),b("#title").focus())}function populate_photos(a){var b=jQuery,c=_.template(b("#photo-template").html()),d=a.media_details,e=d.sizes.thumbnail,f=e?d.sizes.thumbnail.source_url:a.source_url,g=e?d.sizes.thumbnail.width:d.width,h=e?d.sizes.thumbnail.height:d.height;-1===b.inArray(a.id,existing_photos())&&b(".wsuwp-profile-photo-collection").append(c({src:f,width:g,height:h,id:a.id,alt:a.alt,url:a.source_url,title:a.title.rendered,full_width:d.width,full_height:d.height}))}var profile_original_titles,profile_original_degrees,profile_original_personal_bio,profile_original_unit_bio,profile_original_university_bio;!function(a,b,c){a(c).ready(function(){var c=a("#_wsuwp_profile_ad_nid");b.wsuwp_people_edit_profile_secondary.load_data&&jQuery.ajax({url:b.wsuwp_people_edit_profile_secondary.rest_url,data:{_embed:!0,wsu_nid:c.val()}}).done(function(a){0!==a.length&&populate_from_people_directory(a[0])}),a(".wsuwp-profile-about-tabs").on("click",".select",function(){var b=a(this).closest("li"),c=a(".use-bio");b.toggleClass("selected"),b.hasClass("selected")?(c.val(b.data("bio")),b.find(".screen-reader-text").text("Deselect"),b.siblings().removeClass("selected").find(".screen-reader-text").text("Select")):(c.val(""),b.find(".screen-reader-text").text("Select"))}),a(".wsuwp-profile-titles").on("click",".select",function(){var b=a(this).closest("p");b.toggleClass("selected");var c=a(".wsuwp-profile-titles .selected").map(function(){return a(this).index()}).get();a(".use-title").val(c.join(" ")),b.hasClass("selected")?b.find(".screen-reader-text").text("Deselect"):b.find(".screen-reader-text").text("Select")}),a(".wsuwp-profile-photo-collection").on("click",".wsuwp-profile-photo-select",function(){var b=a(this),c=b.closest(".wsuwp-profile-photo-wrapper"),d=a(".use-photo");c.toggleClass("selected").siblings().removeClass("selected"),c.hasClass("selected")?(d.val(a(".wsuwp-profile-photo-wrapper").index(c)),b.attr("aria-label","Deselect"),c.siblings().removeClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Select")):(d.val(""),b.attr("aria-label","Select"))}),a("#publish").on("click",function(){var d={},e=a("#title"),f=a("#_wsuwp_profile_alt_office"),g=a("#_wsuwp_profile_alt_phone"),h=a("#_wsuwp_profile_alt_email"),i=a("#_wsuwp_profile_website"),j=a("[name='_wsuwp_profile_title[]']"),k=a("[name='_wsuwp_profile_degree[]']"),l=b.tinymce.get("content"),m=b.tinymce.get("_wsuwp_profile_bio_unit"),n=b.tinymce.get("_wsuwp_profile_bio_university");if(e.val()!==e.data("original")&&(d.title=e.val()),f.val()!==f.data("original")&&(d.office_alt=f.val()),g.val()!==g.data("original")&&(d.phone_alt=g.val()),h.val()!==h.data("original")&&(d.email_alt=h.val()),i.val()!==i.data("original")&&(d.website=i.val()),j){var o=j.map(function(){return a(this).val()}).get();o.join(",")!==profile_original_titles.join(",")&&(d.working_titles=o)}if(k){var p=k.map(function(){return a(this).val()}).get();p.join(",")!==profile_original_degrees.join(",")&&(d.degree=p)}l&&l.getContent()!==profile_original_personal_bio&&(d.content=l.getContent()),m&&m.getContent()!==profile_original_unit_bio&&(d.bio_unit=m.getContent()),n&&n.getContent()!==profile_original_university_bio&&(d.bio_university=n.getContent()),a.isEmptyObject(d)||a.ajax({url:b.wsuwp_people_edit_profile_secondary.rest_url+"/"+c.data("post-id"),method:"POST",beforeSend:function(a){a.setRequestHeader("X-WP-Nonce",b.wsuwp_people_edit_profile_secondary.nonce),a.setRequestHeader("X-WSUWP-UID",b.wsuwp_people_edit_profile_secondary.uid)},data:d})})})}(jQuery,window,document); \ No newline at end of file diff --git a/js/admin-edit-profile.min.js b/js/admin-edit-profile.min.js new file mode 100644 index 0000000..1131ccd --- /dev/null +++ b/js/admin-edit-profile.min.js @@ -0,0 +1 @@ +function existing_photos(){var a=jQuery;return a(".wsuwp-profile-photo-id").map(function(){return parseInt(a(this).val())}).get()}!function(a,b,c){a(c).ready(function(){var c,d=_.template(a(".wsuwp-profile-repeatable-field-template").html()),e=_.template(a("#photo-template").html()),f=a(".wsuwp-profile-photo-controls-tooltip"),g=a("#publishing-action .spinner"),h=a(".refresh-card .spinner"),i=a("#title"),j=a("#_wsuwp_profile_ad_nid"),k=a(".profile-card-data"),l=a("#confirm-ad-hash"),m=a(".wsuwp-profile-photo-collection"),n=a("#confirm-ad-data"),o=a("#refresh-ad-data"),p=a("#undo-ad-data-refresh");a("#wsuwp-profile-about-wrapper").tabs({active:0}),a(".wsuwp-profile-add-repeatable").on("click","a",function(b){b.preventDefault(),a(this).closest("p").before(d({label:a(this).data("label"),name:a(this).data("name"),value:""}))}),a(".wsuwp-profile-repeatable-field").on("click",".remove",function(){a(this).closest("p").remove()}),a("#load-ad-data, #refresh-ad-data").on("click",function(c){if(c.preventDefault(),c.target.disabled=!0,""===j.val())return void b.alert("Please enter a Network ID");a(c.target).is("#refresh-ad-data")?(h.css("visibility","visible"),k.each(function(){a(this).data("original",a(this).html())})):g.css("visibility","visible");var d={action:"wsu_people_get_data_by_nid",_ajax_nonce:b.wsupeople.nid_nonce,network_id:j.val(),request_from:b.wsupeople.request_from,is_refresh:a(c.target).is("#refresh-ad-data")?"true":"false"};a.post(b.ajaxurl,d,function(d){if(a(".spinner").css("visibility","hidden"),c.target.disabled=!1,!d.success)return void b.alert(d.data);d.data.id?populate_from_people_directory(d.data):(a("#_wsuwp_profile_ad_name_first").html(d.data.given_name),a("#_wsuwp_profile_ad_name_last").html(d.data.surname),a("#_wsuwp_profile_ad_title").html(d.data.title),a("#_wsuwp_profile_ad_office").html(d.data.office),a("#_wsuwp_profile_ad_address").html(d.data.street_address),a("#_wsuwp_profile_ad_phone").html(d.data.telephone_number),a("#_wsuwp_profile_ad_email").html(d.data.email),l.val(d.data.confirm_ad_hash)),n.removeClass("profile-hide-button"),p.removeClass("profile-hide-button"),o.addClass("profile-hide-button")})}),n.on("click",function(c){c.preventDefault(),c.target.disabled=!0,a(c.target).hasClass("refresh")?(h.css("visibility","visible"),p.addClass("profile-hide-button")):(g.css("visibility","visible"),a("#load-ad-data").addClass("profile-hide-button"));var d={action:"wsu_people_confirm_nid_data",_ajax_nonce:b.wsupeople.nid_nonce,network_id:j.val(),confirm_ad_hash:l.val(),post_id:a("#post_ID").val(),request_from:b.wsupeople.request_from},e=a(".load-ad-container .description"),f=a("#publish");a.post(b.ajaxurl,d,function(b){b.success&&(""===i.val()&&(i.focus(),i.val(a("#_wsuwp_profile_ad_name_first").html()+" "+a("#_wsuwp_profile_ad_name_last").html())),j.attr("readonly",!0),e.html("The WSU Network ID used to populate this profile's data from Active Directory."),a(".spinner").css("visibility","hidden"),n.addClass("profile-hide-button"),f.removeClass("profile-hide-button"))})}),p.on("click",function(b){b.preventDefault(),k.each(function(){a(this).html(a(this).data("original"))}),n.addClass("profile-hide-button"),p.addClass("profile-hide-button"),o.removeClass("profile-hide-button").disabled=!1}),a(".wsuwp-profile-add-photo").on("click",function(d){if(d.preventDefault(),c)return void c.open();c=b.wp.media({title:"Select or Upload Your Photos",multiple:!0,library:{type:"image",uploadedTo:b.wsupeople.post_id},button:{text:"Use photo(s)"}}),c.on("select",function(){var b=c.state().get("selection");a.each(b.models,function(b,c){var d=c.toJSON(),f=d.sizes.hasOwnProperty("thumbnail"),g=f?d.sizes.thumbnail.url:d.url,h=f?d.sizes.thumbnail.width:d.width,i=f?d.sizes.thumbnail.height:d.height;-1===a.inArray(d.id,existing_photos())&&m.append(e({src:g,width:h,height:i,id:d.id,alt:d.alt,url:d.url,title:d.title,full_width:d.width,full_height:d.height}))})}),c.open()}),m.on("mouseover",".wsuwp-profile-photo-controls button",function(){var a=this.getAttribute("aria-label"),b=this.getBoundingClientRect(),c=m[0].getBoundingClientRect();f.css({top:b.bottom-c.top+"px",left:b.right-c.left-f.width()/2-6+"px"}).show().find(".wsuwp-profile-photo-controls-tooltip-inner").html(a)}),m.on("mouseleave",".wsuwp-profile-photo-controls button",function(){f.hide()}),m.on("click",".wsuwp-profile-photo-remove",function(){f.hide(),a(this).closest(".wsuwp-profile-photo-wrapper").remove()}),m.on("click",".wsuwp-profile-photo-select",function(){var b=a(this),c=b.closest(".wsuwp-profile-photo-wrapper"),d=a(".use-photo");c.toggleClass("selected").siblings().removeClass("selected"),c.hasClass("selected")?(d.val(a(".wsuwp-profile-photo-wrapper").index(c)),b.attr("aria-label","Deselect"),c.siblings().removeClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Select")):(d.val(""),b.attr("aria-label","Select"))})})}(jQuery,window,document); \ No newline at end of file diff --git a/js/admin-person.min.js b/js/admin-person.min.js deleted file mode 100644 index c09ba46..0000000 --- a/js/admin-person.min.js +++ /dev/null @@ -1 +0,0 @@ -!function(a,b,c){a(c).ready(function(){function c(){return a(".wsuwp-profile-photo-id").map(function(){return parseInt(a(this).val())}).get()}function d(c){var d=a(".wsuwp-profile-titles .wsuwp-profile-add-repeatable"),g=d.find("a").data("label"),h=d.find("a").data("name"),i=a(".wsuwp-profile-degrees .wsuwp-profile-add-repeatable"),l=i.find("a").data("label"),t=i.find("a").data("name");if(k.data("post-id",c.id),j.focus().val(c.title.rendered),m.html(c.first_name),n.html(c.last_name),o.html(c.position_title),p.html(c.office),q.html(c.address),r.html(c.phone),s.html(c.email),a("#_wsuwp_profile_alt_office").val(c.office_alt),a("#_wsuwp_profile_alt_phone").val(c.phone_alt),a("#_wsuwp_profile_alt_email").val(c.email_alt),a("#_wsuwp_profile_website").val(c.website),b.tinymce.get("content").setContent(c.content.rendered),b.tinymce.get("_wsuwp_profile_bio_unit").setContent(c.bio_unit),b.tinymce.get("_wsuwp_profile_bio_university").setContent(c.bio_university),a.each(c.working_titles,function(b,c){var e=a("[name='_wsuwp_profile_title[]']")[b];e?a(e).val(c):d.before(f({label:g,name:h,value:c}))}),""!==a(".use-title").val()){var u=a(".use-title").val().split(" ");a.each(u,function(b,c){a(".wsuwp-profile-titles p").eq(c).addClass("selected").find(".screen-reader-text").text("Deselect")})}a.each(c.degree,function(b,c){var d=a("[name='_wsuwp_profile_degree[]']")[b];d?a(d).val(c):i.before(f({label:l,name:t,value:c}))}),0!==c._embedded["wp:photos"]&&a.each(c._embedded["wp:photos"],function(a,b){e(b)}),c._embedded["wp:featuredmedia"]&&0!==c._embedded["wp:featuredmedia"]&&e(c._embedded["wp:featuredmedia"][0]),""!==a(".use-photo").val()?a(".wsuwp-profile-photo-wrapper").eq(a(".use-photo").val()).addClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Deselect"):a(".wsuwp-profile-photo-wrapper").eq(0).addClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Deselect"),c._embedded["wp:term"]&&0!==c._embedded["wp:term"]&&(a.each(c._embedded["wp:term"],function(b,c){c&&a.each(c,function(b,c){"post_tag"===c.taxonomy?a("#new-tag-post_tag").val(function(a,b){return b+c.name+", "}):a("#"+c.taxonomy+"-all").find(".selectit:contains('"+c.name+"')").find("input[type='checkbox']").prop("checked",!0)})}),a(".tagadd").trigger("click"),j.focus())}function e(b){var d=b.media_details,e=d.sizes.thumbnail,f=e?d.sizes.thumbnail.source_url:b.source_url,h=e?d.sizes.thumbnail.width:d.width,i=e?d.sizes.thumbnail.height:d.height;-1===a.inArray(b.id,c())&&u.append(g({src:f,width:h,height:i,id:b.id,alt:b.alt,url:b.source_url,title:b.title.rendered,full_width:d.width,full_height:d.height}))}var f=_.template(a(".wsuwp-profile-repeatable-field-template").html()),g=_.template(a("#photo-template").html()),h=a("#publishing-action .spinner"),i=a(".refresh-card .spinner"),j=a("#title"),k=a("#_wsuwp_profile_ad_nid"),l=a(".profile-card-data"),m=a("#_wsuwp_profile_ad_name_first"),n=a("#_wsuwp_profile_ad_name_last"),o=a("#_wsuwp_profile_ad_title"),p=a("#_wsuwp_profile_ad_office"),q=a("#_wsuwp_profile_ad_address"),r=a("#_wsuwp_profile_ad_phone"),s=a("#_wsuwp_profile_ad_email"),t=a("#confirm-ad-hash"),u=a(".wsuwp-profile-photo-collection"),v=a("#load-ad-data"),w=a("#confirm-ad-data"),x=a("#refresh-ad-data"),y=a("#undo-ad-data-refresh");a("#wsuwp-profile-about-wrapper").tabs({active:0}),a(".wsuwp-profile-about-tabs").on("click",".select",function(){var b=a(this).closest("li"),c=a(".use-bio");b.toggleClass("selected"),b.hasClass("selected")?(c.val(b.data("bio")),b.find(".screen-reader-text").text("Deselect"),b.siblings().removeClass("selected").find(".screen-reader-text").text("Select")):(c.val(""),b.find(".screen-reader-text").text("Select"))}),b.wsupeople.make_request&&jQuery.ajax({url:b.wsupeople.rest_url,data:{_embed:!0,wsu_nid:k.val()}}).done(function(a){0!==a.length&&d(a[0])}),a(".wsuwp-profile-add-repeatable").on("click","a",function(b){b.preventDefault(),a(this).closest("p").before(f({label:a(this).data("label"),name:a(this).data("name"),value:""}))}),a(".wsuwp-profile-repeatable-field").on("click",".remove",function(){a(this).closest("p").remove()}),a(".wsuwp-profile-titles").on("click",".select",function(){var b=a(this).closest("p");b.toggleClass("selected");var c=a(".wsuwp-profile-titles .selected").map(function(){return a(this).index()}).get();a(".use-title").val(c.join(" ")),b.hasClass("selected")?b.find(".screen-reader-text").text("Deselect"):b.find(".screen-reader-text").text("Select")}),a("#load-ad-data, #refresh-ad-data").on("click",function(c){if(c.preventDefault(),c.target.disabled=!0,""===k.val())return void b.alert("Please enter a Network ID");a(c.target).is("#refresh-ad-data")?(i.css("visibility","visible"),l.each(function(){a(this).data("original",a(this).html())})):h.css("visibility","visible");var e={action:"wsu_people_get_data_by_nid",_ajax_nonce:b.wsupeople.nid_nonce,network_id:k.val(),request_from:b.wsupeople.request_from,is_refresh:a(c.target).is("#refresh-ad-data")?"true":"false"};a.post(b.ajaxurl,e,function(e){if(a(".spinner").css("visibility","hidden"),c.target.disabled=!1,!e.success)return void b.alert(e.data);e.data.id?d(e.data):(m.html(e.data.given_name),n.html(e.data.surname),o.html(e.data.title),p.html(e.data.office),q.html(e.data.street_address),r.html(e.data.telephone_number),s.html(e.data.email),t.val(e.data.confirm_ad_hash)),w.removeClass("profile-hide-button"),y.removeClass("profile-hide-button"),x.addClass("profile-hide-button")})}),w.on("click",function(c){c.preventDefault(),c.target.disabled=!0,a(c.target).hasClass("refresh")?(i.css("visibility","visible"),y.addClass("profile-hide-button")):(h.css("visibility","visible"),v.addClass("profile-hide-button"));var d={action:"wsu_people_confirm_nid_data",_ajax_nonce:b.wsupeople.nid_nonce,network_id:k.val(),confirm_ad_hash:t.val(),post_id:a("#post_ID").val(),request_from:b.wsupeople.request_from},e=a(".load-ad-container .description"),f=a("#publish");a.post(b.ajaxurl,d,function(b){b.success&&(""===j.val()&&(j.focus(),j.val(a("#_wsuwp_profile_ad_name_first").html()+" "+a("#_wsuwp_profile_ad_name_last").html())),k.attr("readonly",!0),e.html("The WSU Network ID used to populate this profile's data from Active Directory."),a(".spinner").css("visibility","hidden"),w.addClass("profile-hide-button"),f.removeClass("profile-hide-button"))})}),y.on("click",function(b){b.preventDefault(),l.each(function(){a(this).html(a(this).data("original"))}),w.addClass("profile-hide-button"),y.addClass("profile-hide-button"),x.removeClass("profile-hide-button").disabled=!1});var z,A=a(".wsuwp-profile-add-photo"),B=a(".wsuwp-profile-photo-controls-tooltip");A.on("click",function(d){if(d.preventDefault(),z)return void z.open();z=b.wp.media({title:"Select or Upload Your Photos",multiple:!0,library:{type:"image",uploadedTo:b.wsupeople.post_id},button:{text:"Use photo(s)"}}),z.on("select",function(){var b=z.state().get("selection");a.each(b.models,function(b,d){var e=d.toJSON(),f=e.sizes.hasOwnProperty("thumbnail"),h=f?e.sizes.thumbnail.url:e.url,i=f?e.sizes.thumbnail.width:e.width,j=f?e.sizes.thumbnail.height:e.height;-1===a.inArray(e.id,c())&&u.append(g({src:h,width:i,height:j,id:e.id,alt:e.alt,url:e.url,title:e.title,full_width:e.width,full_height:e.height}))})}),z.open()}),u.on("mouseover",".wsuwp-profile-photo-controls button",function(){var a=this.getAttribute("aria-label"),b=this.getBoundingClientRect(),c=u[0].getBoundingClientRect();B.css({top:b.bottom-c.top+"px",left:b.right-c.left-B.width()/2-6+"px"}).show().find(".wsuwp-profile-photo-controls-tooltip-inner").html(a)}),u.on("mouseleave",".wsuwp-profile-photo-controls button",function(){B.hide()}),u.on("click",".wsuwp-profile-photo-remove",function(){B.hide(),a(this).closest(".wsuwp-profile-photo-wrapper").remove()}),u.on("click",".wsuwp-profile-photo-select",function(){var b=a(this),c=b.closest(".wsuwp-profile-photo-wrapper"),d=a(".use-photo");c.toggleClass("selected").siblings().removeClass("selected"),c.hasClass("selected")?(d.val(a(".wsuwp-profile-photo-wrapper").index(c)),b.attr("aria-label","Deselect"),c.siblings().removeClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Select")):(d.val(""),b.attr("aria-label","Select"))}),b.wsupeople.make_request&&a("#publish").on("click",function(){var c={},d=a("#title").val(),e=a("#_wsuwp_profile_alt_office").val(),f=a("#_wsuwp_profile_alt_phone").val(),g=a("#_wsuwp_profile_alt_email").val(),h=a("#_wsuwp_profile_website").val(),i=b.tinymce.get("content").getContent(),j=b.tinymce.get("_wsuwp_profile_bio_unit").getContent(),l=b.tinymce.get("_wsuwp_profile_bio_university").getContent();d&&(c.title=d),e&&(c.office_alt=e),f&&(c.phone_alt=f),g&&(c.email_alt=g),h&&(c.website=h),i&&(c.content=i),j&&(c.bio_unit=j),l&&(c.bio_university=l),a.ajax({url:b.wsupeople.rest_url+"/"+k.data("post-id"),method:"POST",beforeSend:function(a){a.setRequestHeader("X-WP-Nonce",b.wsupeople.nonce),a.setRequestHeader("X-WSUWP-UID",b.wsupeople.uid)},data:c})})})}(jQuery,window,document); \ No newline at end of file diff --git a/src/js/admin-edit-profile-secondary.js b/src/js/admin-edit-profile-secondary.js new file mode 100644 index 0000000..447ccbf --- /dev/null +++ b/src/js/admin-edit-profile-secondary.js @@ -0,0 +1,334 @@ +/* global _, existing_photos */ +var profile_original_titles, + profile_original_degrees, + profile_original_personal_bio, + profile_original_unit_bio, + profile_original_university_bio; + +( function( $, window, document ) { + $( document ).ready( function() { + var $nid = $( "#_wsuwp_profile_ad_nid" ); + + // Make a REST request to for profile data from people.wsu.edu. + if ( window.wsuwp_people_edit_profile_secondary.load_data ) { + jQuery.ajax( { + url: window.wsuwp_people_edit_profile_secondary.rest_url, + data: { + _embed: true, + wsu_nid: $nid.val() + } + } ).done( function( response ) { + if ( response.length !== 0 ) { + populate_from_people_directory( response[ 0 ] ); + } + } ); + } + + // Select a bio for display on the front end. + $( ".wsuwp-profile-about-tabs" ).on( "click", ".select", function() { + var $tab = $( this ).closest( "li" ), + $input = $( ".use-bio" ); + + $tab.toggleClass( "selected" ); + + if ( $tab.hasClass( "selected" ) ) { + $input.val( $tab.data( "bio" ) ); + $tab.find( ".screen-reader-text" ).text( "Deselect" ); + $tab.siblings().removeClass( "selected" ).find( ".screen-reader-text" ).text( "Select" ); + } else { + $input.val( "" ); + $tab.find( ".screen-reader-text" ).text( "Select" ); + } + } ); + + // Select a working title for display on the front end. + $( ".wsuwp-profile-titles" ).on( "click", ".select", function() { + var $title = $( this ).closest( "p" ); + + $title.toggleClass( "selected" ); + + var selected_titles = $( ".wsuwp-profile-titles .selected" ).map( function() { return $( this ).index(); } ).get(); + + $( ".use-title" ).val( selected_titles.join( " " ) ); + + if ( $title.hasClass( "selected" ) ) { + $title.find( ".screen-reader-text" ).text( "Deselect" ); + } else { + $title.find( ".screen-reader-text" ).text( "Select" ); + } + } ); + + // Select a photo for display on the front end. + $( ".wsuwp-profile-photo-collection" ).on( "click", ".wsuwp-profile-photo-select", function() { + var $button = $( this ), + $photo = $button.closest( ".wsuwp-profile-photo-wrapper" ), + $input = $( ".use-photo" ); + + $photo.toggleClass( "selected" ).siblings().removeClass( "selected" ); + + if ( $photo.hasClass( "selected" ) ) { + $input.val( $( ".wsuwp-profile-photo-wrapper" ).index( $photo ) ); + $button.attr( "aria-label", "Deselect" ); + $photo.siblings().removeClass( "selected" ).find( ".wsuwp-profile-photo-select" ).attr( "aria-label", "Select" ); + } else { + $input.val( "" ); + $button.attr( "aria-label", "Select" ); + } + } ); + + // Post data to the user's people.wsu.edu profile. + $( "#publish" ).on( "click", function() { + var data = {}, + name = $( "#title" ), + office = $( "#_wsuwp_profile_alt_office" ), + phone = $( "#_wsuwp_profile_alt_phone" ), + email = $( "#_wsuwp_profile_alt_email" ), + website = $( "#_wsuwp_profile_website" ), + titles = $( "[name='_wsuwp_profile_title[]']" ), + degrees = $( "[name='_wsuwp_profile_degree[]']" ), + personal_bio = window.tinymce.get( "content" ), + unit_bio = window.tinymce.get( "_wsuwp_profile_bio_unit" ), + university_bio = window.tinymce.get( "_wsuwp_profile_bio_university" ); + + // Only add changed values to the data array. + if ( name.val() !== name.data( "original" ) ) { + data.title = name.val(); + } + + if ( office.val() !== office.data( "original" ) ) { + data.office_alt = office.val(); + } + + if ( phone.val() !== phone.data( "original" ) ) { + data.phone_alt = phone.val(); + } + + if ( email.val() !== email.data( "original" ) ) { + data.email_alt = email.val(); + } + + if ( website.val() !== website.data( "original" ) ) { + data.website = website.val(); + } + + if ( titles ) { + var new_titles = titles.map( function() { return $( this ).val(); } ).get(); + if ( new_titles.join( "," ) !== profile_original_titles.join( "," ) ) { + data.working_titles = new_titles; + } + } + + if ( degrees ) { + var new_degrees = degrees.map( function() { return $( this ).val(); } ).get(); + if ( new_degrees.join( "," ) !== profile_original_degrees.join( "," ) ) { + data.degree = new_degrees; + } + } + + if ( personal_bio && ( personal_bio.getContent() !== profile_original_personal_bio ) ) { + data.content = personal_bio.getContent(); + } + + if ( unit_bio && ( unit_bio.getContent() !== profile_original_unit_bio ) ) { + data.bio_unit = unit_bio.getContent(); + } + + if ( university_bio && ( university_bio.getContent() !== profile_original_university_bio ) ) { + data.bio_university = university_bio.getContent(); + } + + // Only push data if values have changed. + if ( !$.isEmptyObject( data ) ) { + $.ajax( { + url: window.wsuwp_people_edit_profile_secondary.rest_url + "/" + $nid.data( "post-id" ), + method: "POST", + beforeSend: function( xhr ) { + xhr.setRequestHeader( "X-WP-Nonce", window.wsuwp_people_edit_profile_secondary.nonce ); + xhr.setRequestHeader( "X-WSUWP-UID", window.wsuwp_people_edit_profile_secondary.uid ); + }, + data: data + } ); + } + } ); + + } ); +}( jQuery, window, document ) ); + +// Populate profile with data from people.wsu.edu. +function populate_from_people_directory( data ) { + var $ = jQuery, + repeatable_field_template = _.template( $( ".wsuwp-profile-repeatable-field-template" ).html() ), + $working_titles = $( ".wsuwp-profile-titles .wsuwp-profile-add-repeatable" ), + working_title_label = $working_titles.find( "a" ).data( "label" ), + working_title_name = $working_titles.find( "a" ).data( "name" ), + $degrees = $( ".wsuwp-profile-degrees .wsuwp-profile-add-repeatable" ), + degree_label = $degrees.find( "a" ).data( "label" ), + degree_name = $degrees.find( "a" ).data( "name" ), + unit_bio = window.tinymce.get( "_wsuwp_profile_bio_unit" ), + university_bio = window.tinymce.get( "_wsuwp_profile_bio_university" ); + + // Store the post ID as a data attribute of the NID field. + $( "#_wsuwp_profile_ad_nid" ).data( "post-id", data.id ); + + $( "#title" ).focus().val( data.title.rendered ).data( "original", data.title.rendered ); + + // Populate AD data. + $( "#_wsuwp_profile_ad_name_first" ).html( data.first_name ); + $( "#_wsuwp_profile_ad_name_last" ).html( data.last_name ); + $( "#_wsuwp_profile_ad_title" ).html( data.position_title ); + $( "#_wsuwp_profile_ad_office" ).html( data.office ); + $( "#_wsuwp_profile_ad_address" ).html( data.address ); + $( "#_wsuwp_profile_ad_phone" ).html( data.phone ); + $( "#_wsuwp_profile_ad_email" ).html( data.email ); + + // Populate additional/alternative fields. + $( "#_wsuwp_profile_alt_office" ).val( data.office_alt ).data( "original", data.office_alt ); + $( "#_wsuwp_profile_alt_phone" ).val( data.phone_alt ).data( "original", data.phone_alt ); + $( "#_wsuwp_profile_alt_email" ).val( data.email_alt ).data( "original", data.email_alt ); + $( "#_wsuwp_profile_website" ).val( data.website ).data( "original", data.website ); + + // Populate biographies. + window.tinymce.get( "content" ).setContent( data.content.rendered ); + profile_original_personal_bio = data.content.rendered; + + // Depending on the user's permissions, the unit biography might not be editable. + if ( unit_bio ) { + unit_bio.setContent( data.bio_unit ); + profile_original_unit_bio = data.bio_unit; + } else { + $( "#bio_unit .readonly" ).html( data.bio_unit ); + } + + // Depending on the user's permissions, the university biography might not be editable. + if ( university_bio ) { + university_bio.setContent( data.bio_university ); + profile_original_university_bio = data.bio_university; + } else { + $( "#bio_university .readonly" ).html( data.bio_university ); + } + + // Populate working title(s). + $.each( data.working_titles, function( i, value ) { + var field = $( "[name='_wsuwp_profile_title[]']" )[ i ]; + + if ( field ) { + $( field ).val( value ); + } else { + $working_titles.before( repeatable_field_template( { + label: working_title_label, + name: working_title_name, + value: value + } ) ); + } + } ); + + profile_original_titles = data.working_titles; + + // Add the `selected` class to working titles accordingly. + if ( "" !== $( ".use-title" ).val() ) { + var titles = $( ".use-title" ).val().split( " " ); + + $.each( titles, function( i, value ) { + $( ".wsuwp-profile-titles p" ) + .eq( value ) + .addClass( "selected" ) + .find( ".screen-reader-text" ) + .text( "Deselect" ); + } ); + } + + // Populate degree(s). + $.each( data.degree, function( i, value ) { + var field = $( "[name='_wsuwp_profile_degree[]']" )[ i ]; + + if ( field ) { + $( field ).val( value ); + } else { + $degrees.before( repeatable_field_template( { + label: degree_label, + name: degree_name, + value: value + } ) ); + } + } ); + + profile_original_degrees = data.degree; + + // Populate photo collection. + if ( data._embedded[ "wp:photos" ] !== 0 ) { + $.each( data._embedded[ "wp:photos" ], function( i, photo ) { + populate_photos( photo ); + } ); + } + + // Populate featured image as part of the photo collection. + if ( data._embedded[ "wp:featuredmedia" ] && data._embedded[ "wp:featuredmedia" ] !== 0 ) { + populate_photos( data._embedded[ "wp:featuredmedia" ][ 0 ] ); + } + + // Add the `selected` class to a photo accordingly. + if ( "" !== $( ".use-photo" ).val() ) { + $( ".wsuwp-profile-photo-wrapper" ) + .eq( $( ".use-photo" ).val() ) + .addClass( "selected" ) + .find( ".wsuwp-profile-photo-select" ) + .attr( "aria-label", "Deselect" ); + } else { + $( ".wsuwp-profile-photo-wrapper" ) + .eq( 0 ) + .addClass( "selected" ) + .find( ".wsuwp-profile-photo-select" ) + .attr( "aria-label", "Deselect" ); + } + + // Populate taxonomy data. + if ( data._embedded[ "wp:term" ] && data._embedded[ "wp:term" ] !== 0 ) { + $.each( data._embedded[ "wp:term" ], function( i, taxonomy ) { + if ( taxonomy ) { + $.each( taxonomy, function( i, term ) { + if ( "post_tag" === term.taxonomy ) { + $( "#new-tag-post_tag" ).val( function( index, val ) { + return val + term.name + ", "; + } ); + } else { + $( "#" + term.taxonomy + "-all" ) + .find( ".selectit:contains('" + term.name + "')" ) + .find( "input[type='checkbox']" ).prop( "checked", true ); + } + } ); + } + } ); + + // Add the tags. + $( ".tagadd" ).trigger( "click" ); + + // Change focus to the post title field (the trigger above focuses the tag input). + $( "#title" ).focus(); + } +} + +// Populate the photo collection with data from people.wsu.edu. +function populate_photos( data ) { + var $ = jQuery, + photo_template = _.template( $( "#photo-template" ).html() ), + photo = data.media_details, + has_thumbnail = photo.sizes.thumbnail, + url = has_thumbnail ? photo.sizes.thumbnail.source_url : data.source_url, + width = has_thumbnail ? photo.sizes.thumbnail.width : photo.width, + height = has_thumbnail ? photo.sizes.thumbnail.height : photo.height; + + // Avoid inserting duplicate images. + if ( -1 === $.inArray( data.id, existing_photos() ) ) { + $( ".wsuwp-profile-photo-collection" ).append( photo_template( { + src: url, + width: width, + height: height, + id: data.id, + alt: data.alt, + url: data.source_url, + title: data.title.rendered, + full_width: photo.width, + full_height: photo.height + } ) ); + } +} diff --git a/src/js/admin-edit-profile.js b/src/js/admin-edit-profile.js new file mode 100644 index 0000000..23b5445 --- /dev/null +++ b/src/js/admin-edit-profile.js @@ -0,0 +1,262 @@ +/* global _, populate_from_people_directory */ +( function( $, window, document ) { + $( document ).ready( function() { + var repeatable_field_template = _.template( $( ".wsuwp-profile-repeatable-field-template" ).html() ), + photo_template = _.template( $( "#photo-template" ).html() ), + media_frame, + $tooltip = $( ".wsuwp-profile-photo-controls-tooltip" ), + $publishing_spinner = $( "#publishing-action .spinner" ), + $refresh_spinner = $( ".refresh-card .spinner" ), + $post_title = $( "#title" ), + $nid = $( "#_wsuwp_profile_ad_nid" ), + $all_card_data = $( ".profile-card-data" ), + $hash = $( "#confirm-ad-hash" ), + $collection = $( ".wsuwp-profile-photo-collection" ), + $confirm = $( "#confirm-ad-data" ), + $refresh = $( "#refresh-ad-data" ), + $undo = $( "#undo-ad-data-refresh" ); + + // Initialize tabs. + $( "#wsuwp-profile-about-wrapper" ).tabs( { + active: 0 + } ); + + // Add a repeatable field. + $( ".wsuwp-profile-add-repeatable" ).on( "click", "a", function( e ) { + e.preventDefault(); + + $( this ).closest( "p" ).before( repeatable_field_template( { + label: $( this ).data( "label" ), + name: $( this ).data( "name" ), + value: "" + } ) ); + } ); + + // Remove a repeatable field. + $( ".wsuwp-profile-repeatable-field" ).on( "click", ".remove", function() { + $( this ).closest( "p" ).remove(); + } ); + + // Capture data. + $( "#load-ad-data, #refresh-ad-data" ).on( "click", function( e ) { + e.preventDefault(); + e.target.disabled = true; + + // Don't let the user get too far without entering a NID. + if ( "" === $nid.val() ) { + window.alert( "Please enter a Network ID" ); + return; + } + + // Provide an indication that data is being loaded. + // If this is a refresh, store current information in case the user wants to undo. + if ( $( e.target ).is( "#refresh-ad-data" ) ) { + $refresh_spinner.css( "visibility", "visible" ); + $all_card_data.each( function() { + $( this ).data( "original", $( this ).html() ); + } ); + } else { + $publishing_spinner.css( "visibility", "visible" ); + } + + var data = { + "action": "wsu_people_get_data_by_nid", + "_ajax_nonce": window.wsupeople.nid_nonce, + "network_id": $nid.val(), + "request_from": window.wsupeople.request_from, + "is_refresh": ( $( e.target ).is( "#refresh-ad-data" ) ) ? "true" : "false" + }; + + $.post( window.ajaxurl, data, function( response ) { + $( ".spinner" ).css( "visibility", "hidden" ); + e.target.disabled = false; + + if ( response.success ) { + + // If the response has an id property, it's almost certainly from people.wsu.edu. + if ( response.data.id ) { + populate_from_people_directory( response.data ); + } else { + $( "#_wsuwp_profile_ad_name_first" ).html( response.data.given_name ); + $( "#_wsuwp_profile_ad_name_last" ).html( response.data.surname ); + $( "#_wsuwp_profile_ad_title" ).html( response.data.title ); + $( "#_wsuwp_profile_ad_office" ).html( response.data.office ); + $( "#_wsuwp_profile_ad_address" ).html( response.data.street_address ); + $( "#_wsuwp_profile_ad_phone" ).html( response.data.telephone_number ); + $( "#_wsuwp_profile_ad_email" ).html( response.data.email ); + $hash.val( response.data.confirm_ad_hash ); + } + } else { + window.alert( response.data ); + return; + } + + $confirm.removeClass( "profile-hide-button" ); + $undo.removeClass( "profile-hide-button" ); + $refresh.addClass( "profile-hide-button" ); + } ); + } ); + + // Confirm/save retrieved data. + $confirm.on( "click", function( e ) { + e.preventDefault(); + e.target.disabled = true; + + // Provide an indication that data is being loaded. + if ( $( e.target ).hasClass( "refresh" ) ) { + $refresh_spinner.css( "visibility", "visible" ); + $undo.addClass( "profile-hide-button" ); + } else { + $publishing_spinner.css( "visibility", "visible" ); + $( "#load-ad-data" ).addClass( "profile-hide-button" ); + } + + var data = { + "action": "wsu_people_confirm_nid_data", + "_ajax_nonce": window.wsupeople.nid_nonce, + "network_id": $nid.val(), + "confirm_ad_hash": $hash.val(), + "post_id": $( "#post_ID" ).val(), + "request_from": window.wsupeople.request_from + }; + + var $description = $( ".load-ad-container .description" ), + $publish = $( "#publish" ); + + $.post( window.ajaxurl, data, function( response ) { + if ( response.success ) { + + // If a title has not yet been entered, use the given and surname from AD. + if ( "" === $post_title.val() ) { + $post_title.focus(); + $post_title.val( $( "#_wsuwp_profile_ad_name_first" ).html() + " " + $( "#_wsuwp_profile_ad_name_last" ).html() ); + } + + $nid.attr( "readonly", true ); + $description.html( "The WSU Network ID used to populate this profile's data from Active Directory." ); + + $( ".spinner" ).css( "visibility", "hidden" ); + + $confirm.addClass( "profile-hide-button" ); + $publish.removeClass( "profile-hide-button" ); + } + } ); + } ); + + // Undo a refresh. + $undo.on( "click", function( e ) { + e.preventDefault(); + + $all_card_data.each( function() { + $( this ).html( $( this ).data( "original" ) ); + } ); + + $confirm.addClass( "profile-hide-button" ); + $undo.addClass( "profile-hide-button" ); + $refresh.removeClass( "profile-hide-button" ).disabled = false; + } ); + + // Handle photo adding. + $( ".wsuwp-profile-add-photo" ).on( "click", function( e ) { + e.preventDefault(); + + if ( media_frame ) { + media_frame.open(); + return; + } + + media_frame = window.wp.media( { + title: "Select or Upload Your Photos", + multiple: true, + library: { + type: "image", + uploadedTo: window.wsupeople.post_id + }, + button: { + text: "Use photo(s)" + } + } ); + + media_frame.on( "select", function() { + var photos = media_frame.state().get( "selection" ); + + $.each( photos.models, function( i, attachment ) { + + var photo = attachment.toJSON(), + has_thumbnail = photo.sizes.hasOwnProperty( "thumbnail" ), + url = has_thumbnail ? photo.sizes.thumbnail.url : photo.url, + width = has_thumbnail ? photo.sizes.thumbnail.width : photo.width, + height = has_thumbnail ? photo.sizes.thumbnail.height : photo.height; + + // An image doesn't need to be added more than once. + if ( -1 === $.inArray( photo.id, existing_photos() ) ) { + $collection.append( photo_template( { + src: url, + width: width, + height: height, + id: photo.id, + alt: photo.alt, + url: photo.url, + title: photo.title, + full_width: photo.width, + full_height: photo.height + } ) ); + } + } ); + } ); + + media_frame.open(); + } ); + + // Show control buttons tooltip. + $collection.on( "mouseover", ".wsuwp-profile-photo-controls button", function() { + var text = this.getAttribute( "aria-label" ), + button = this.getBoundingClientRect(), + collection = $collection[ 0 ].getBoundingClientRect(); + + $tooltip.css( { + top: button.bottom - collection.top + "px", + left: button.right - collection.left - $tooltip.width() / 2 - 6 + "px" + } ).show().find( ".wsuwp-profile-photo-controls-tooltip-inner" ).html( text ); + } ); + + // Hide control buttons tooltip. + $collection.on( "mouseleave", ".wsuwp-profile-photo-controls button", function() { + $tooltip.hide(); + } ); + + // Delete a photo. + $collection.on( "click", ".wsuwp-profile-photo-remove", function() { + $tooltip.hide(); + + $( this ).closest( ".wsuwp-profile-photo-wrapper" ).remove(); + } ); + + // Select a photo for display on the front-end (non-people.wsu.edu sites only). + $collection.on( "click", ".wsuwp-profile-photo-select", function() { + var $button = $( this ), + $photo = $button.closest( ".wsuwp-profile-photo-wrapper" ), + $input = $( ".use-photo" ); + + $photo.toggleClass( "selected" ).siblings().removeClass( "selected" ); + + if ( $photo.hasClass( "selected" ) ) { + $input.val( $( ".wsuwp-profile-photo-wrapper" ).index( $photo ) ); + $button.attr( "aria-label", "Deselect" ); + $photo.siblings().removeClass( "selected" ).find( ".wsuwp-profile-photo-select" ).attr( "aria-label", "Select" ); + } else { + $input.val( "" ); + $button.attr( "aria-label", "Select" ); + } + } ); + } ); + +}( jQuery, window, document ) ); + +// Create an array of photo IDs already in the collection. +function existing_photos() { + var $ = jQuery, + photos = $( ".wsuwp-profile-photo-id" ).map( function() { return parseInt( $( this ).val() ); } ).get(); + + return photos; +} diff --git a/src/js/admin-person.js b/src/js/admin-person.js deleted file mode 100644 index 2c810c7..0000000 --- a/src/js/admin-person.js +++ /dev/null @@ -1,543 +0,0 @@ -/* global _ */ -( function( $, window, document ) { - $( document ).ready( function() { - - var repeatable_field_template = _.template( $( ".wsuwp-profile-repeatable-field-template" ).html() ), - photo_template = _.template( $( "#photo-template" ).html() ), - $publishing_spinner = $( "#publishing-action .spinner" ), - $refresh_spinner = $( ".refresh-card .spinner" ), - $post_title = $( "#title" ), - $nid = $( "#_wsuwp_profile_ad_nid" ), - $all_card_data = $( ".profile-card-data" ), - $given_name = $( "#_wsuwp_profile_ad_name_first" ), - $surname = $( "#_wsuwp_profile_ad_name_last" ), - $title = $( "#_wsuwp_profile_ad_title" ), - $office = $( "#_wsuwp_profile_ad_office" ), - $address = $( "#_wsuwp_profile_ad_address" ), - $phone = $( "#_wsuwp_profile_ad_phone" ), - $email = $( "#_wsuwp_profile_ad_email" ), - $hash = $( "#confirm-ad-hash" ), - $collection = $( ".wsuwp-profile-photo-collection" ), - $load = $( "#load-ad-data" ), - $confirm = $( "#confirm-ad-data" ), - $refresh = $( "#refresh-ad-data" ), - $undo = $( "#undo-ad-data-refresh" ); - - // Create an array of photo IDs already in the collection. - function existing_photos() { - var existing_photos = $( ".wsuwp-profile-photo-id" ).map( function() { - return parseInt( $( this ).val() ); - } ).get(); - - return existing_photos; - } - - // Populate profile with data from people.wsu.edu. - function populate_from_people_directory( data ) { - var $working_titles = $( ".wsuwp-profile-titles .wsuwp-profile-add-repeatable" ), - working_title_label = $working_titles.find( "a" ).data( "label" ), - working_title_name = $working_titles.find( "a" ).data( "name" ), - $degrees = $( ".wsuwp-profile-degrees .wsuwp-profile-add-repeatable" ), - degree_label = $degrees.find( "a" ).data( "label" ), - degree_name = $degrees.find( "a" ).data( "name" ); - - // Store the post ID as a data attribute of the NID field. - $nid.data( "post-id", data.id ); - - // Populate AD data. - $post_title.focus().val( data.title.rendered ); - $given_name.html( data.first_name ); - $surname.html( data.last_name ); - $title.html( data.position_title ); - $office.html( data.office ); - $address.html( data.address ); - $phone.html( data.phone ); - $email.html( data.email ); - - // Populate additional/alternative fields. - $( "#_wsuwp_profile_alt_office" ).val( data.office_alt ); - $( "#_wsuwp_profile_alt_phone" ).val( data.phone_alt ); - $( "#_wsuwp_profile_alt_email" ).val( data.email_alt ); - $( "#_wsuwp_profile_website" ).val( data.website ); - - // Populate biographies. - window.tinymce.get( "content" ).setContent( data.content.rendered ); - window.tinymce.get( "_wsuwp_profile_bio_unit" ).setContent( data.bio_unit ); - window.tinymce.get( "_wsuwp_profile_bio_university" ).setContent( data.bio_university ); - - // Populate working title(s). - $.each( data.working_titles, function( i, value ) { - var field = $( "[name='_wsuwp_profile_title[]']" )[ i ]; - - if ( field ) { - $( field ).val( value ); - } else { - $working_titles.before( repeatable_field_template( { - label: working_title_label, - name: working_title_name, - value: value - } ) ); - } - } ); - - // Add the `selected` class to working titles accordingly. - if ( "" !== $( ".use-title" ).val() ) { - var titles = $( ".use-title" ).val().split( " " ); - - $.each( titles, function( i, value ) { - $( ".wsuwp-profile-titles p" ) - .eq( value ) - .addClass( "selected" ) - .find( ".screen-reader-text" ) - .text( "Deselect" ); - } ); - } - - // Populate degree(s). - $.each( data.degree, function( i, value ) { - var field = $( "[name='_wsuwp_profile_degree[]']" )[ i ]; - - if ( field ) { - $( field ).val( value ); - } else { - $degrees.before( repeatable_field_template( { - label: degree_label, - name: degree_name, - value: value - } ) ); - } - } ); - - // Populate photo collection. - if ( data._embedded[ "wp:photos" ] !== 0 ) { - $.each( data._embedded[ "wp:photos" ], function( i, photo ) { - populate_photos( photo ); - } ); - } - - // Populate featured image as part of the photo collection. - if ( data._embedded[ "wp:featuredmedia" ] && data._embedded[ "wp:featuredmedia" ] !== 0 ) { - populate_photos( data._embedded[ "wp:featuredmedia" ][ 0 ] ); - } - - // Add the `selected` class to a photo accordingly. - if ( "" !== $( ".use-photo" ).val() ) { - $( ".wsuwp-profile-photo-wrapper" ) - .eq( $( ".use-photo" ).val() ) - .addClass( "selected" ) - .find( ".wsuwp-profile-photo-select" ) - .attr( "aria-label", "Deselect" ); - } else { - $( ".wsuwp-profile-photo-wrapper" ) - .eq( 0 ) - .addClass( "selected" ) - .find( ".wsuwp-profile-photo-select" ) - .attr( "aria-label", "Deselect" ); - } - - // Populate taxonomy data. - if ( data._embedded[ "wp:term" ] && data._embedded[ "wp:term" ] !== 0 ) { - $.each( data._embedded[ "wp:term" ], function( i, taxonomy ) { - if ( taxonomy ) { - $.each( taxonomy, function( i, term ) { - if ( "post_tag" === term.taxonomy ) { - $( "#new-tag-post_tag" ).val( function( index, val ) { - return val + term.name + ", "; - } ); - } else { - $( "#" + term.taxonomy + "-all" ) - .find( ".selectit:contains('" + term.name + "')" ) - .find( "input[type='checkbox']" ).prop( "checked", true ); - } - } ); - } - } ); - - // Add the tags. - $( ".tagadd" ).trigger( "click" ); - - // Change focus to the post title field (the trigger above focuses the tag input). - $post_title.focus(); - } - } - - // Populate the photo collection with data from people.wsu.edu. - function populate_photos( data ) { - var photo = data.media_details, - has_thumbnail = photo.sizes.thumbnail, - url = has_thumbnail ? photo.sizes.thumbnail.source_url : data.source_url, - width = has_thumbnail ? photo.sizes.thumbnail.width : photo.width, - height = has_thumbnail ? photo.sizes.thumbnail.height : photo.height; - - // Avoid inserting duplicate images. - if ( -1 === $.inArray( data.id, existing_photos() ) ) { - $collection.append( photo_template( { - src: url, - width: width, - height: height, - id: data.id, - alt: data.alt, - url: data.source_url, - title: data.title.rendered, - full_width: photo.width, - full_height: photo.height - } ) ); - } - } - - // Initialize tabs. - $( "#wsuwp-profile-about-wrapper" ).tabs( { - active: 0 - } ); - - // Select a bio for display on the front-end (non-people.wsu.edu sites only). - $( ".wsuwp-profile-about-tabs" ).on( "click", ".select", function() { - var $tab = $( this ).closest( "li" ), - $input = $( ".use-bio" ); - - $tab.toggleClass( "selected" ); - - if ( $tab.hasClass( "selected" ) ) { - $input.val( $tab.data( "bio" ) ); - $tab.find( ".screen-reader-text" ).text( "Deselect" ); - $tab.siblings().removeClass( "selected" ).find( ".screen-reader-text" ).text( "Select" ); - } else { - $input.val( "" ); - $tab.find( ".screen-reader-text" ).text( "Select" ); - } - } ); - - // Make a REST request to populate a person with data from people.wsu.edu - if ( window.wsupeople.make_request ) { - jQuery.ajax( { - url: window.wsupeople.rest_url, - data: { - _embed: true, - wsu_nid: $nid.val() - } - } ).done( function( response ) { - if ( response.length !== 0 ) { - populate_from_people_directory( response[ 0 ] ); - } - } ); - } - - // Add a repeatable field. - $( ".wsuwp-profile-add-repeatable" ).on( "click", "a", function( e ) { - e.preventDefault(); - - $( this ).closest( "p" ).before( repeatable_field_template( { - label: $( this ).data( "label" ), - name: $( this ).data( "name" ), - value: "" - } ) ); - } ); - - // Remove a repeatable field. - $( ".wsuwp-profile-repeatable-field" ).on( "click", ".remove", function() { - $( this ).closest( "p" ).remove(); - } ); - - // Select a working title for display on the front-end (non-people.wsu.edu sites only). - $( ".wsuwp-profile-titles" ).on( "click", ".select", function() { - var $title = $( this ).closest( "p" ); - - $title.toggleClass( "selected" ); - - var selected_titles = $( ".wsuwp-profile-titles .selected" ).map( function() { return $( this ).index(); } ).get(); - - $( ".use-title" ).val( selected_titles.join( " " ) ); - - if ( $title.hasClass( "selected" ) ) { - $title.find( ".screen-reader-text" ).text( "Deselect" ); - } else { - $title.find( ".screen-reader-text" ).text( "Select" ); - } - } ); - - // Capture data. - $( "#load-ad-data, #refresh-ad-data" ).on( "click", function( e ) { - e.preventDefault(); - e.target.disabled = true; - - // Don't let the user get too far without entering a NID. - if ( "" === $nid.val() ) { - window.alert( "Please enter a Network ID" ); - return; - } - - // Provide an indication that data is being loaded. - // If this is a refresh, store current information in case the user wants to undo. - if ( $( e.target ).is( "#refresh-ad-data" ) ) { - $refresh_spinner.css( "visibility", "visible" ); - $all_card_data.each( function() { - $( this ).data( "original", $( this ).html() ); - } ); - } else { - $publishing_spinner.css( "visibility", "visible" ); - } - - var data = { - "action": "wsu_people_get_data_by_nid", - "_ajax_nonce": window.wsupeople.nid_nonce, - "network_id": $nid.val(), - "request_from": window.wsupeople.request_from, - "is_refresh": ( $( e.target ).is( "#refresh-ad-data" ) ) ? "true" : "false" - }; - - $.post( window.ajaxurl, data, function( response ) { - $( ".spinner" ).css( "visibility", "hidden" ); - e.target.disabled = false; - - if ( response.success ) { - - // If the response has an id property, it's almost certainly from people.wsu.edu. - if ( response.data.id ) { - populate_from_people_directory( response.data ); - } else { - $given_name.html( response.data.given_name ); - $surname.html( response.data.surname ); - $title.html( response.data.title ); - $office.html( response.data.office ); - $address.html( response.data.street_address ); - $phone.html( response.data.telephone_number ); - $email.html( response.data.email ); - $hash.val( response.data.confirm_ad_hash ); - } - } else { - window.alert( response.data ); - return; - } - - $confirm.removeClass( "profile-hide-button" ); - $undo.removeClass( "profile-hide-button" ); - $refresh.addClass( "profile-hide-button" ); - } ); - } ); - - // Confirm/save retrieved data. - $confirm.on( "click", function( e ) { - e.preventDefault(); - e.target.disabled = true; - - // Provide an indication that data is being loaded. - if ( $( e.target ).hasClass( "refresh" ) ) { - $refresh_spinner.css( "visibility", "visible" ); - $undo.addClass( "profile-hide-button" ); - } else { - $publishing_spinner.css( "visibility", "visible" ); - $load.addClass( "profile-hide-button" ); - } - - var data = { - "action": "wsu_people_confirm_nid_data", - "_ajax_nonce": window.wsupeople.nid_nonce, - "network_id": $nid.val(), - "confirm_ad_hash": $hash.val(), - "post_id": $( "#post_ID" ).val(), - "request_from": window.wsupeople.request_from - }; - - var $description = $( ".load-ad-container .description" ), - $publish = $( "#publish" ); - - $.post( window.ajaxurl, data, function( response ) { - if ( response.success ) { - - // If a title has not yet been entered, use the given and surname from AD. - if ( "" === $post_title.val() ) { - $post_title.focus(); - $post_title.val( $( "#_wsuwp_profile_ad_name_first" ).html() + " " + $( "#_wsuwp_profile_ad_name_last" ).html() ); - } - - $nid.attr( "readonly", true ); - $description.html( "The WSU Network ID used to populate this profile's data from Active Directory." ); - - $( ".spinner" ).css( "visibility", "hidden" ); - - $confirm.addClass( "profile-hide-button" ); - $publish.removeClass( "profile-hide-button" ); - } - } ); - } ); - - // Undo a refresh. - $undo.on( "click", function( e ) { - e.preventDefault(); - - $all_card_data.each( function() { - $( this ).html( $( this ).data( "original" ) ); - } ); - - $confirm.addClass( "profile-hide-button" ); - $undo.addClass( "profile-hide-button" ); - $refresh.removeClass( "profile-hide-button" ).disabled = false; - } ); - - // Photo collection handling. - var media_frame, - $add_photo = $( ".wsuwp-profile-add-photo" ), - $tooltip = $( ".wsuwp-profile-photo-controls-tooltip" ); - - // Handle photo adding. - $add_photo.on( "click", function( e ) { - e.preventDefault(); - - if ( media_frame ) { - media_frame.open(); - return; - } - - media_frame = window.wp.media( { - title: "Select or Upload Your Photos", - multiple: true, - library: { - type: "image", - uploadedTo: window.wsupeople.post_id - }, - button: { - text: "Use photo(s)" - } - } ); - - media_frame.on( "select", function() { - var photos = media_frame.state().get( "selection" ); - - $.each( photos.models, function( i, attachment ) { - - var photo = attachment.toJSON(), - has_thumbnail = photo.sizes.hasOwnProperty( "thumbnail" ), - url = has_thumbnail ? photo.sizes.thumbnail.url : photo.url, - width = has_thumbnail ? photo.sizes.thumbnail.width : photo.width, - height = has_thumbnail ? photo.sizes.thumbnail.height : photo.height; - - // An image doesn't need to be added more than once. - if ( -1 === $.inArray( photo.id, existing_photos() ) ) { - $collection.append( photo_template( { - src: url, - width: width, - height: height, - id: photo.id, - alt: photo.alt, - url: photo.url, - title: photo.title, - full_width: photo.width, - full_height: photo.height - } ) ); - } - } ); - } ); - - media_frame.open(); - } ); - - // Show control buttons tooltip. - $collection.on( "mouseover", ".wsuwp-profile-photo-controls button", function() { - var text = this.getAttribute( "aria-label" ), - button = this.getBoundingClientRect(), - collection = $collection[ 0 ].getBoundingClientRect(); - - $tooltip.css( { - top: button.bottom - collection.top + "px", - left: button.right - collection.left - $tooltip.width() / 2 - 6 + "px" - } ).show().find( ".wsuwp-profile-photo-controls-tooltip-inner" ).html( text ); - } ); - - // Hide control buttons tooltip. - $collection.on( "mouseleave", ".wsuwp-profile-photo-controls button", function() { - $tooltip.hide(); - } ); - - // Delete a photo. - $collection.on( "click", ".wsuwp-profile-photo-remove", function() { - $tooltip.hide(); - - $( this ).closest( ".wsuwp-profile-photo-wrapper" ).remove(); - } ); - - // Select a photo for display on the front-end (non-people.wsu.edu sites only). - $collection.on( "click", ".wsuwp-profile-photo-select", function() { - var $button = $( this ), - $photo = $button.closest( ".wsuwp-profile-photo-wrapper" ), - $input = $( ".use-photo" ); - - $photo.toggleClass( "selected" ).siblings().removeClass( "selected" ); - - if ( $photo.hasClass( "selected" ) ) { - $input.val( $( ".wsuwp-profile-photo-wrapper" ).index( $photo ) ); - $button.attr( "aria-label", "Deselect" ); - $photo.siblings().removeClass( "selected" ).find( ".wsuwp-profile-photo-select" ).attr( "aria-label", "Select" ); - } else { - $input.val( "" ); - $button.attr( "aria-label", "Select" ); - } - } ); - - // Post data to the user's people.wsu.edu profile. - if ( window.wsupeople.make_request ) { - $( "#publish" ).on( "click", function() { - var data = {}, - name = $( "#title" ).val(), - office = $( "#_wsuwp_profile_alt_office" ).val(), - phone = $( "#_wsuwp_profile_alt_phone" ).val(), - email = $( "#_wsuwp_profile_alt_email" ).val(), - website = $( "#_wsuwp_profile_website" ).val(), - titles = false, - degrees = false, - personal_bio = window.tinymce.get( "content" ).getContent(), - unit_bio = window.tinymce.get( "_wsuwp_profile_bio_unit" ).getContent(), - university_bio = window.tinymce.get( "_wsuwp_profile_bio_university" ).getContent(); - - if ( name ) { - data.title = name; - } - - if ( office ) { - data.office_alt = office; - } - - if ( phone ) { - data.phone_alt = phone; - } - - if ( email ) { - data.email_alt = email; - } - - if ( website ) { - data.website = website; - } - - if ( titles ) { - data.working_titles = titles; - } - - if ( degrees ) { - data.degree = degrees; - } - - if ( personal_bio ) { - data.content = personal_bio; - } - - if ( unit_bio ) { - data.bio_unit = unit_bio; - } - - if ( university_bio ) { - data.bio_university = university_bio; - } - - $.ajax( { - url: window.wsupeople.rest_url + "/" + $nid.data( "post-id" ), - method: "POST", - beforeSend: function( xhr ) { - xhr.setRequestHeader( "X-WP-Nonce", window.wsupeople.nonce ); - xhr.setRequestHeader( "X-WSUWP-UID", window.wsupeople.uid ); - }, - data: data - } ); - } ); - } - } ); -}( jQuery, window, document ) ); From 81022b4c54557248e2661c8b29d455e170e5d8c1 Mon Sep 17 00:00:00 2001 From: Phil Cable Date: Thu, 13 Apr 2017 11:24:59 -0700 Subject: [PATCH 3/6] Add styles for read-only biographies --- css/admin-person.css | 10 ++++++++++ src/css/admin-person.css | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/css/admin-person.css b/css/admin-person.css index e647c9e..22b18f7 100644 --- a/css/admin-person.css +++ b/css/admin-person.css @@ -246,6 +246,16 @@ display: block; } +.ui-tabs-panel .readonly { + color: #717171; + font-family: "Open Sans", "Lucida Sans", "Lucida Grande", "Lucida Sans Unicode", sans-serif; + padding: 9px 10px; +} + +.ui-tabs-panel .readonly p { + font-size: 14px; +} + .ui-tabs-panel[aria-hidden="true"] { border: 0; clip: rect(0 0 0 0); diff --git a/src/css/admin-person.css b/src/css/admin-person.css index 9270f88..84a78d4 100644 --- a/src/css/admin-person.css +++ b/src/css/admin-person.css @@ -237,6 +237,16 @@ display: block; } +.ui-tabs-panel .readonly { + color: #717171; + font-family: "Open Sans", "Lucida Sans", "Lucida Grande", "Lucida Sans Unicode", sans-serif; + padding: 9px 10px; +} + +.ui-tabs-panel .readonly p { + font-size: 14px; +} + .ui-tabs-panel[aria-hidden="true"] { border: 0; clip: rect(0 0 0 0); From b46ce55b4d52775a03781bcae5b81057b314995f Mon Sep 17 00:00:00 2001 From: Phil Cable Date: Thu, 13 Apr 2017 11:30:03 -0700 Subject: [PATCH 4/6] Update variable name I've been horribly inconsistent with my syntax in the JavaScript for this plugin, so this is an attempt to start rectifying that. --- includes/class-wsuwp-people-post-type.php | 6 +++--- js/admin-edit-profile.min.js | 2 +- src/js/admin-edit-profile.js | 10 +++++----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/includes/class-wsuwp-people-post-type.php b/includes/class-wsuwp-people-post-type.php index fc87b23..714d9b4 100644 --- a/includes/class-wsuwp-people-post-type.php +++ b/includes/class-wsuwp-people-post-type.php @@ -418,9 +418,9 @@ public function admin_enqueue_scripts( $hook_suffix ) { 'request_from' => ( apply_filters( 'wsuwp_people_display', true ) ) ? 'rest' : 'ad', ); - wp_enqueue_style( 'wsuwp-people-admin', plugins_url( 'css/admin-person.css', dirname( __FILE__ ) ), array(), WSUWP_People_Directory::$version ); - wp_enqueue_script( 'wsuwp-people-admin', plugins_url( 'js/admin-edit-profile.min.js', dirname( __FILE__ ) ), array( 'jquery-ui-tabs', 'underscore' ), WSUWP_People_Directory::$version, true ); - wp_localize_script( 'wsuwp-people-admin', 'wsupeople', $profile_vars ); + wp_enqueue_style( 'wsuwp-people-edit-profile', plugins_url( 'css/admin-person.css', dirname( __FILE__ ) ), array(), WSUWP_People_Directory::$version ); + wp_enqueue_script( 'wsuwp-people-edit-profile', plugins_url( 'js/admin-edit-profile.min.js', dirname( __FILE__ ) ), array( 'jquery-ui-tabs', 'underscore' ), WSUWP_People_Directory::$version, true ); + wp_localize_script( 'wsuwp-people-edit-profile', 'wsuwp_people_edit_profile', $profile_vars ); // Disable autosaving for sites other than the primary directory. if ( apply_filters( 'wsuwp_people_display', true ) ) { diff --git a/js/admin-edit-profile.min.js b/js/admin-edit-profile.min.js index 1131ccd..8e817b3 100644 --- a/js/admin-edit-profile.min.js +++ b/js/admin-edit-profile.min.js @@ -1 +1 @@ -function existing_photos(){var a=jQuery;return a(".wsuwp-profile-photo-id").map(function(){return parseInt(a(this).val())}).get()}!function(a,b,c){a(c).ready(function(){var c,d=_.template(a(".wsuwp-profile-repeatable-field-template").html()),e=_.template(a("#photo-template").html()),f=a(".wsuwp-profile-photo-controls-tooltip"),g=a("#publishing-action .spinner"),h=a(".refresh-card .spinner"),i=a("#title"),j=a("#_wsuwp_profile_ad_nid"),k=a(".profile-card-data"),l=a("#confirm-ad-hash"),m=a(".wsuwp-profile-photo-collection"),n=a("#confirm-ad-data"),o=a("#refresh-ad-data"),p=a("#undo-ad-data-refresh");a("#wsuwp-profile-about-wrapper").tabs({active:0}),a(".wsuwp-profile-add-repeatable").on("click","a",function(b){b.preventDefault(),a(this).closest("p").before(d({label:a(this).data("label"),name:a(this).data("name"),value:""}))}),a(".wsuwp-profile-repeatable-field").on("click",".remove",function(){a(this).closest("p").remove()}),a("#load-ad-data, #refresh-ad-data").on("click",function(c){if(c.preventDefault(),c.target.disabled=!0,""===j.val())return void b.alert("Please enter a Network ID");a(c.target).is("#refresh-ad-data")?(h.css("visibility","visible"),k.each(function(){a(this).data("original",a(this).html())})):g.css("visibility","visible");var d={action:"wsu_people_get_data_by_nid",_ajax_nonce:b.wsupeople.nid_nonce,network_id:j.val(),request_from:b.wsupeople.request_from,is_refresh:a(c.target).is("#refresh-ad-data")?"true":"false"};a.post(b.ajaxurl,d,function(d){if(a(".spinner").css("visibility","hidden"),c.target.disabled=!1,!d.success)return void b.alert(d.data);d.data.id?populate_from_people_directory(d.data):(a("#_wsuwp_profile_ad_name_first").html(d.data.given_name),a("#_wsuwp_profile_ad_name_last").html(d.data.surname),a("#_wsuwp_profile_ad_title").html(d.data.title),a("#_wsuwp_profile_ad_office").html(d.data.office),a("#_wsuwp_profile_ad_address").html(d.data.street_address),a("#_wsuwp_profile_ad_phone").html(d.data.telephone_number),a("#_wsuwp_profile_ad_email").html(d.data.email),l.val(d.data.confirm_ad_hash)),n.removeClass("profile-hide-button"),p.removeClass("profile-hide-button"),o.addClass("profile-hide-button")})}),n.on("click",function(c){c.preventDefault(),c.target.disabled=!0,a(c.target).hasClass("refresh")?(h.css("visibility","visible"),p.addClass("profile-hide-button")):(g.css("visibility","visible"),a("#load-ad-data").addClass("profile-hide-button"));var d={action:"wsu_people_confirm_nid_data",_ajax_nonce:b.wsupeople.nid_nonce,network_id:j.val(),confirm_ad_hash:l.val(),post_id:a("#post_ID").val(),request_from:b.wsupeople.request_from},e=a(".load-ad-container .description"),f=a("#publish");a.post(b.ajaxurl,d,function(b){b.success&&(""===i.val()&&(i.focus(),i.val(a("#_wsuwp_profile_ad_name_first").html()+" "+a("#_wsuwp_profile_ad_name_last").html())),j.attr("readonly",!0),e.html("The WSU Network ID used to populate this profile's data from Active Directory."),a(".spinner").css("visibility","hidden"),n.addClass("profile-hide-button"),f.removeClass("profile-hide-button"))})}),p.on("click",function(b){b.preventDefault(),k.each(function(){a(this).html(a(this).data("original"))}),n.addClass("profile-hide-button"),p.addClass("profile-hide-button"),o.removeClass("profile-hide-button").disabled=!1}),a(".wsuwp-profile-add-photo").on("click",function(d){if(d.preventDefault(),c)return void c.open();c=b.wp.media({title:"Select or Upload Your Photos",multiple:!0,library:{type:"image",uploadedTo:b.wsupeople.post_id},button:{text:"Use photo(s)"}}),c.on("select",function(){var b=c.state().get("selection");a.each(b.models,function(b,c){var d=c.toJSON(),f=d.sizes.hasOwnProperty("thumbnail"),g=f?d.sizes.thumbnail.url:d.url,h=f?d.sizes.thumbnail.width:d.width,i=f?d.sizes.thumbnail.height:d.height;-1===a.inArray(d.id,existing_photos())&&m.append(e({src:g,width:h,height:i,id:d.id,alt:d.alt,url:d.url,title:d.title,full_width:d.width,full_height:d.height}))})}),c.open()}),m.on("mouseover",".wsuwp-profile-photo-controls button",function(){var a=this.getAttribute("aria-label"),b=this.getBoundingClientRect(),c=m[0].getBoundingClientRect();f.css({top:b.bottom-c.top+"px",left:b.right-c.left-f.width()/2-6+"px"}).show().find(".wsuwp-profile-photo-controls-tooltip-inner").html(a)}),m.on("mouseleave",".wsuwp-profile-photo-controls button",function(){f.hide()}),m.on("click",".wsuwp-profile-photo-remove",function(){f.hide(),a(this).closest(".wsuwp-profile-photo-wrapper").remove()}),m.on("click",".wsuwp-profile-photo-select",function(){var b=a(this),c=b.closest(".wsuwp-profile-photo-wrapper"),d=a(".use-photo");c.toggleClass("selected").siblings().removeClass("selected"),c.hasClass("selected")?(d.val(a(".wsuwp-profile-photo-wrapper").index(c)),b.attr("aria-label","Deselect"),c.siblings().removeClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Select")):(d.val(""),b.attr("aria-label","Select"))})})}(jQuery,window,document); \ No newline at end of file +function existing_photos(){var a=jQuery;return a(".wsuwp-profile-photo-id").map(function(){return parseInt(a(this).val())}).get()}!function(a,b,c){a(c).ready(function(){var c,d=_.template(a(".wsuwp-profile-repeatable-field-template").html()),e=_.template(a("#photo-template").html()),f=a(".wsuwp-profile-photo-controls-tooltip"),g=a("#publishing-action .spinner"),h=a(".refresh-card .spinner"),i=a("#title"),j=a("#_wsuwp_profile_ad_nid"),k=a(".profile-card-data"),l=a("#confirm-ad-hash"),m=a(".wsuwp-profile-photo-collection"),n=a("#confirm-ad-data"),o=a("#refresh-ad-data"),p=a("#undo-ad-data-refresh");a("#wsuwp-profile-about-wrapper").tabs({active:0}),a(".wsuwp-profile-add-repeatable").on("click","a",function(b){b.preventDefault(),a(this).closest("p").before(d({label:a(this).data("label"),name:a(this).data("name"),value:""}))}),a(".wsuwp-profile-repeatable-field").on("click",".remove",function(){a(this).closest("p").remove()}),a("#load-ad-data, #refresh-ad-data").on("click",function(c){if(c.preventDefault(),c.target.disabled=!0,""===j.val())return void b.alert("Please enter a Network ID");a(c.target).is("#refresh-ad-data")?(h.css("visibility","visible"),k.each(function(){a(this).data("original",a(this).html())})):g.css("visibility","visible");var d={action:"wsu_people_get_data_by_nid",_ajax_nonce:b.wsuwp_people_edit_profile.nid_nonce,network_id:j.val(),request_from:b.wsuwp_people_edit_profile.request_from,is_refresh:a(c.target).is("#refresh-ad-data")?"true":"false"};a.post(b.ajaxurl,d,function(d){if(a(".spinner").css("visibility","hidden"),c.target.disabled=!1,!d.success)return void b.alert(d.data);d.data.id?populate_from_people_directory(d.data):(a("#_wsuwp_profile_ad_name_first").html(d.data.given_name),a("#_wsuwp_profile_ad_name_last").html(d.data.surname),a("#_wsuwp_profile_ad_title").html(d.data.title),a("#_wsuwp_profile_ad_office").html(d.data.office),a("#_wsuwp_profile_ad_address").html(d.data.street_address),a("#_wsuwp_profile_ad_phone").html(d.data.telephone_number),a("#_wsuwp_profile_ad_email").html(d.data.email),l.val(d.data.confirm_ad_hash)),n.removeClass("profile-hide-button"),p.removeClass("profile-hide-button"),o.addClass("profile-hide-button")})}),n.on("click",function(c){c.preventDefault(),c.target.disabled=!0,a(c.target).hasClass("refresh")?(h.css("visibility","visible"),p.addClass("profile-hide-button")):(g.css("visibility","visible"),a("#load-ad-data").addClass("profile-hide-button"));var d={action:"wsu_people_confirm_nid_data",_ajax_nonce:b.wsuwp_people_edit_profile.nid_nonce,network_id:j.val(),confirm_ad_hash:l.val(),post_id:a("#post_ID").val(),request_from:b.wsuwp_people_edit_profile.request_from},e=a(".load-ad-container .description"),f=a("#publish");a.post(b.ajaxurl,d,function(b){b.success&&(""===i.val()&&(i.focus(),i.val(a("#_wsuwp_profile_ad_name_first").html()+" "+a("#_wsuwp_profile_ad_name_last").html())),j.attr("readonly",!0),e.html("The WSU Network ID used to populate this profile's data from Active Directory."),a(".spinner").css("visibility","hidden"),n.addClass("profile-hide-button"),f.removeClass("profile-hide-button"))})}),p.on("click",function(b){b.preventDefault(),k.each(function(){a(this).html(a(this).data("original"))}),n.addClass("profile-hide-button"),p.addClass("profile-hide-button"),o.removeClass("profile-hide-button").disabled=!1}),a(".wsuwp-profile-add-photo").on("click",function(d){if(d.preventDefault(),c)return void c.open();c=b.wp.media({title:"Select or Upload Your Photos",multiple:!0,library:{type:"image",uploadedTo:b.wsuwp_people_edit_profile.post_id},button:{text:"Use photo(s)"}}),c.on("select",function(){var b=c.state().get("selection");a.each(b.models,function(b,c){var d=c.toJSON(),f=d.sizes.hasOwnProperty("thumbnail"),g=f?d.sizes.thumbnail.url:d.url,h=f?d.sizes.thumbnail.width:d.width,i=f?d.sizes.thumbnail.height:d.height;-1===a.inArray(d.id,existing_photos())&&m.append(e({src:g,width:h,height:i,id:d.id,alt:d.alt,url:d.url,title:d.title,full_width:d.width,full_height:d.height}))})}),c.open()}),m.on("mouseover",".wsuwp-profile-photo-controls button",function(){var a=this.getAttribute("aria-label"),b=this.getBoundingClientRect(),c=m[0].getBoundingClientRect();f.css({top:b.bottom-c.top+"px",left:b.right-c.left-f.width()/2-6+"px"}).show().find(".wsuwp-profile-photo-controls-tooltip-inner").html(a)}),m.on("mouseleave",".wsuwp-profile-photo-controls button",function(){f.hide()}),m.on("click",".wsuwp-profile-photo-remove",function(){f.hide(),a(this).closest(".wsuwp-profile-photo-wrapper").remove()}),m.on("click",".wsuwp-profile-photo-select",function(){var b=a(this),c=b.closest(".wsuwp-profile-photo-wrapper"),d=a(".use-photo");c.toggleClass("selected").siblings().removeClass("selected"),c.hasClass("selected")?(d.val(a(".wsuwp-profile-photo-wrapper").index(c)),b.attr("aria-label","Deselect"),c.siblings().removeClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Select")):(d.val(""),b.attr("aria-label","Select"))})})}(jQuery,window,document); \ No newline at end of file diff --git a/src/js/admin-edit-profile.js b/src/js/admin-edit-profile.js index 23b5445..1bd76f9 100644 --- a/src/js/admin-edit-profile.js +++ b/src/js/admin-edit-profile.js @@ -61,9 +61,9 @@ var data = { "action": "wsu_people_get_data_by_nid", - "_ajax_nonce": window.wsupeople.nid_nonce, + "_ajax_nonce": window.wsuwp_people_edit_profile.nid_nonce, "network_id": $nid.val(), - "request_from": window.wsupeople.request_from, + "request_from": window.wsuwp_people_edit_profile.request_from, "is_refresh": ( $( e.target ).is( "#refresh-ad-data" ) ) ? "true" : "false" }; @@ -113,11 +113,11 @@ var data = { "action": "wsu_people_confirm_nid_data", - "_ajax_nonce": window.wsupeople.nid_nonce, + "_ajax_nonce": window.wsuwp_people_edit_profile.nid_nonce, "network_id": $nid.val(), "confirm_ad_hash": $hash.val(), "post_id": $( "#post_ID" ).val(), - "request_from": window.wsupeople.request_from + "request_from": window.wsuwp_people_edit_profile.request_from }; var $description = $( ".load-ad-container .description" ), @@ -170,7 +170,7 @@ multiple: true, library: { type: "image", - uploadedTo: window.wsupeople.post_id + uploadedTo: window.wsuwp_people_edit_profile.post_id }, button: { text: "Use photo(s)" From 6a6264db23ba0966164a01e0825f8914f4fc056a Mon Sep 17 00:00:00 2001 From: Phil Cable Date: Tue, 18 Apr 2017 16:53:20 -0700 Subject: [PATCH 5/6] Implement a proof of concept permissions check This is pretty bad, but it gets the general idea across. Even if a user doesn't have sufficient permissions to edit a profile, they should still be able to select which Working Title, Biography, and Photo to display on the front end for their particular site. So, this demonstrates how that could work - though I'm sure there's a nicer way to do it. Like one that doesn't throw a console error. I'm also wondering if doing it the other way around would make more sense (that is, disable all fields by default and only enable them if a user has permissions to edit the profile). --- js/admin-edit-profile-secondary.min.js | 2 +- src/js/admin-edit-profile-secondary.js | 30 +++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/js/admin-edit-profile-secondary.min.js b/js/admin-edit-profile-secondary.min.js index 7e58a70..18514e4 100644 --- a/js/admin-edit-profile-secondary.min.js +++ b/js/admin-edit-profile-secondary.min.js @@ -1 +1 @@ -function populate_from_people_directory(a){var b=jQuery,c=_.template(b(".wsuwp-profile-repeatable-field-template").html()),d=b(".wsuwp-profile-titles .wsuwp-profile-add-repeatable"),e=d.find("a").data("label"),f=d.find("a").data("name"),g=b(".wsuwp-profile-degrees .wsuwp-profile-add-repeatable"),h=g.find("a").data("label"),i=g.find("a").data("name"),j=window.tinymce.get("_wsuwp_profile_bio_unit"),k=window.tinymce.get("_wsuwp_profile_bio_university");if(b("#_wsuwp_profile_ad_nid").data("post-id",a.id),b("#title").focus().val(a.title.rendered).data("original",a.title.rendered),b("#_wsuwp_profile_ad_name_first").html(a.first_name),b("#_wsuwp_profile_ad_name_last").html(a.last_name),b("#_wsuwp_profile_ad_title").html(a.position_title),b("#_wsuwp_profile_ad_office").html(a.office),b("#_wsuwp_profile_ad_address").html(a.address),b("#_wsuwp_profile_ad_phone").html(a.phone),b("#_wsuwp_profile_ad_email").html(a.email),b("#_wsuwp_profile_alt_office").val(a.office_alt).data("original",a.office_alt),b("#_wsuwp_profile_alt_phone").val(a.phone_alt).data("original",a.phone_alt),b("#_wsuwp_profile_alt_email").val(a.email_alt).data("original",a.email_alt),b("#_wsuwp_profile_website").val(a.website).data("original",a.website),window.tinymce.get("content").setContent(a.content.rendered),profile_original_personal_bio=a.content.rendered,j?(j.setContent(a.bio_unit),profile_original_unit_bio=a.bio_unit):b("#bio_unit .readonly").html(a.bio_unit),k?(k.setContent(a.bio_university),profile_original_university_bio=a.bio_university):b("#bio_university .readonly").html(a.bio_university),b.each(a.working_titles,function(a,g){var h=b("[name='_wsuwp_profile_title[]']")[a];h?b(h).val(g):d.before(c({label:e,name:f,value:g}))}),profile_original_titles=a.working_titles,""!==b(".use-title").val()){var l=b(".use-title").val().split(" ");b.each(l,function(a,c){b(".wsuwp-profile-titles p").eq(c).addClass("selected").find(".screen-reader-text").text("Deselect")})}b.each(a.degree,function(a,d){var e=b("[name='_wsuwp_profile_degree[]']")[a];e?b(e).val(d):g.before(c({label:h,name:i,value:d}))}),profile_original_degrees=a.degree,0!==a._embedded["wp:photos"]&&b.each(a._embedded["wp:photos"],function(a,b){populate_photos(b)}),a._embedded["wp:featuredmedia"]&&0!==a._embedded["wp:featuredmedia"]&&populate_photos(a._embedded["wp:featuredmedia"][0]),""!==b(".use-photo").val()?b(".wsuwp-profile-photo-wrapper").eq(b(".use-photo").val()).addClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Deselect"):b(".wsuwp-profile-photo-wrapper").eq(0).addClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Deselect"),a._embedded["wp:term"]&&0!==a._embedded["wp:term"]&&(b.each(a._embedded["wp:term"],function(a,c){c&&b.each(c,function(a,c){"post_tag"===c.taxonomy?b("#new-tag-post_tag").val(function(a,b){return b+c.name+", "}):b("#"+c.taxonomy+"-all").find(".selectit:contains('"+c.name+"')").find("input[type='checkbox']").prop("checked",!0)})}),b(".tagadd").trigger("click"),b("#title").focus())}function populate_photos(a){var b=jQuery,c=_.template(b("#photo-template").html()),d=a.media_details,e=d.sizes.thumbnail,f=e?d.sizes.thumbnail.source_url:a.source_url,g=e?d.sizes.thumbnail.width:d.width,h=e?d.sizes.thumbnail.height:d.height;-1===b.inArray(a.id,existing_photos())&&b(".wsuwp-profile-photo-collection").append(c({src:f,width:g,height:h,id:a.id,alt:a.alt,url:a.source_url,title:a.title.rendered,full_width:d.width,full_height:d.height}))}var profile_original_titles,profile_original_degrees,profile_original_personal_bio,profile_original_unit_bio,profile_original_university_bio;!function(a,b,c){a(c).ready(function(){var c=a("#_wsuwp_profile_ad_nid");b.wsuwp_people_edit_profile_secondary.load_data&&jQuery.ajax({url:b.wsuwp_people_edit_profile_secondary.rest_url,data:{_embed:!0,wsu_nid:c.val()}}).done(function(a){0!==a.length&&populate_from_people_directory(a[0])}),a(".wsuwp-profile-about-tabs").on("click",".select",function(){var b=a(this).closest("li"),c=a(".use-bio");b.toggleClass("selected"),b.hasClass("selected")?(c.val(b.data("bio")),b.find(".screen-reader-text").text("Deselect"),b.siblings().removeClass("selected").find(".screen-reader-text").text("Select")):(c.val(""),b.find(".screen-reader-text").text("Select"))}),a(".wsuwp-profile-titles").on("click",".select",function(){var b=a(this).closest("p");b.toggleClass("selected");var c=a(".wsuwp-profile-titles .selected").map(function(){return a(this).index()}).get();a(".use-title").val(c.join(" ")),b.hasClass("selected")?b.find(".screen-reader-text").text("Deselect"):b.find(".screen-reader-text").text("Select")}),a(".wsuwp-profile-photo-collection").on("click",".wsuwp-profile-photo-select",function(){var b=a(this),c=b.closest(".wsuwp-profile-photo-wrapper"),d=a(".use-photo");c.toggleClass("selected").siblings().removeClass("selected"),c.hasClass("selected")?(d.val(a(".wsuwp-profile-photo-wrapper").index(c)),b.attr("aria-label","Deselect"),c.siblings().removeClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Select")):(d.val(""),b.attr("aria-label","Select"))}),a("#publish").on("click",function(){var d={},e=a("#title"),f=a("#_wsuwp_profile_alt_office"),g=a("#_wsuwp_profile_alt_phone"),h=a("#_wsuwp_profile_alt_email"),i=a("#_wsuwp_profile_website"),j=a("[name='_wsuwp_profile_title[]']"),k=a("[name='_wsuwp_profile_degree[]']"),l=b.tinymce.get("content"),m=b.tinymce.get("_wsuwp_profile_bio_unit"),n=b.tinymce.get("_wsuwp_profile_bio_university");if(e.val()!==e.data("original")&&(d.title=e.val()),f.val()!==f.data("original")&&(d.office_alt=f.val()),g.val()!==g.data("original")&&(d.phone_alt=g.val()),h.val()!==h.data("original")&&(d.email_alt=h.val()),i.val()!==i.data("original")&&(d.website=i.val()),j){var o=j.map(function(){return a(this).val()}).get();o.join(",")!==profile_original_titles.join(",")&&(d.working_titles=o)}if(k){var p=k.map(function(){return a(this).val()}).get();p.join(",")!==profile_original_degrees.join(",")&&(d.degree=p)}l&&l.getContent()!==profile_original_personal_bio&&(d.content=l.getContent()),m&&m.getContent()!==profile_original_unit_bio&&(d.bio_unit=m.getContent()),n&&n.getContent()!==profile_original_university_bio&&(d.bio_university=n.getContent()),a.isEmptyObject(d)||a.ajax({url:b.wsuwp_people_edit_profile_secondary.rest_url+"/"+c.data("post-id"),method:"POST",beforeSend:function(a){a.setRequestHeader("X-WP-Nonce",b.wsuwp_people_edit_profile_secondary.nonce),a.setRequestHeader("X-WSUWP-UID",b.wsuwp_people_edit_profile_secondary.uid)},data:d})})})}(jQuery,window,document); \ No newline at end of file +function populate_from_people_directory(a){var b=jQuery,c=_.template(b(".wsuwp-profile-repeatable-field-template").html()),d=b(".wsuwp-profile-titles .wsuwp-profile-add-repeatable"),e=d.find("a").data("label"),f=d.find("a").data("name"),g=b(".wsuwp-profile-degrees .wsuwp-profile-add-repeatable"),h=g.find("a").data("label"),i=g.find("a").data("name"),j=window.tinymce.get("_wsuwp_profile_bio_unit"),k=window.tinymce.get("_wsuwp_profile_bio_university");if(b("#_wsuwp_profile_ad_nid").data("post-id",a.id),b("#title").focus().val(a.title.rendered).data("original",a.title.rendered),b("#_wsuwp_profile_ad_name_first").html(a.first_name),b("#_wsuwp_profile_ad_name_last").html(a.last_name),b("#_wsuwp_profile_ad_title").html(a.position_title),b("#_wsuwp_profile_ad_office").html(a.office),b("#_wsuwp_profile_ad_address").html(a.address),b("#_wsuwp_profile_ad_phone").html(a.phone),b("#_wsuwp_profile_ad_email").html(a.email),b("#_wsuwp_profile_alt_office").val(a.office_alt).data("original",a.office_alt),b("#_wsuwp_profile_alt_phone").val(a.phone_alt).data("original",a.phone_alt),b("#_wsuwp_profile_alt_email").val(a.email_alt).data("original",a.email_alt),b("#_wsuwp_profile_website").val(a.website).data("original",a.website),window.tinymce.get("content").setContent(a.content.rendered),profile_original_personal_bio=a.content.rendered,j?(j.setContent(a.bio_unit),profile_original_unit_bio=a.bio_unit):b("#bio_unit .readonly").html(a.bio_unit),k?(k.setContent(a.bio_university),profile_original_university_bio=a.bio_university):b("#bio_university .readonly").html(a.bio_university),b.each(a.working_titles,function(a,g){var h=b("[name='_wsuwp_profile_title[]']")[a];h?b(h).val(g):d.before(c({label:e,name:f,value:g}))}),profile_original_titles=a.working_titles,""!==b(".use-title").val()){var l=b(".use-title").val().split(" ");b.each(l,function(a,c){b(".wsuwp-profile-titles p").eq(c).addClass("selected").find(".screen-reader-text").text("Deselect")})}b.each(a.degree,function(a,d){var e=b("[name='_wsuwp_profile_degree[]']")[a];e?b(e).val(d):g.before(c({label:h,name:i,value:d}))}),profile_original_degrees=a.degree,0!==a._embedded["wp:photos"]&&b.each(a._embedded["wp:photos"],function(a,b){populate_photos(b)}),a._embedded["wp:featuredmedia"]&&0!==a._embedded["wp:featuredmedia"]&&populate_photos(a._embedded["wp:featuredmedia"][0]),""!==b(".use-photo").val()?b(".wsuwp-profile-photo-wrapper").eq(b(".use-photo").val()).addClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Deselect"):b(".wsuwp-profile-photo-wrapper").eq(0).addClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Deselect"),a._embedded["wp:term"]&&0!==a._embedded["wp:term"]&&(b.each(a._embedded["wp:term"],function(a,c){c&&b.each(c,function(a,c){"post_tag"===c.taxonomy?b("#new-tag-post_tag").val(function(a,b){return b+c.name+", "}):b("#"+c.taxonomy+"-all").find(".selectit:contains('"+c.name+"')").find("input[type='checkbox']").prop("checked",!0)})}),b(".tagadd").trigger("click"),b("#title").focus()),b.ajax({url:window.wsuwp_people_edit_profile_secondary.rest_url+"/"+a.id,type:"POST",beforeSend:function(a){a.setRequestHeader("X-WP-Nonce",window.wsuwp_people_edit_profile_secondary.nonce),a.setRequestHeader("X-WSUWP-UID",window.wsuwp_people_edit_profile_secondary.uid)},error:function(){b("#wsuwp_profile_additional_info input").prop("disabled",!0),b(".wsuwp-profile-button.remove").prop("disabled",!0),b("#new-tag-post_tag").prop("disabled",!0),b(".tabs-panel input").prop("disabled",!0),b(".wsuwp-profile-add-photo").prop("disabled",!0),b(".wsuwp-profile-photo-remove").prop("disabled",!0),window.tinymce.get("content").setMode("readonly"),j&&j.setMode("readonly"),k&&k.setMode("readonly")}})}function populate_photos(a){var b=jQuery,c=_.template(b("#photo-template").html()),d=a.media_details,e=d.sizes.thumbnail,f=e?d.sizes.thumbnail.source_url:a.source_url,g=e?d.sizes.thumbnail.width:d.width,h=e?d.sizes.thumbnail.height:d.height;-1===b.inArray(a.id,existing_photos())&&b(".wsuwp-profile-photo-collection").append(c({src:f,width:g,height:h,id:a.id,alt:a.alt,url:a.source_url,title:a.title.rendered,full_width:d.width,full_height:d.height}))}var profile_original_titles,profile_original_degrees,profile_original_personal_bio,profile_original_unit_bio,profile_original_university_bio;!function(a,b,c){a(c).ready(function(){var c=a("#_wsuwp_profile_ad_nid");b.wsuwp_people_edit_profile_secondary.load_data&&a.ajax({url:b.wsuwp_people_edit_profile_secondary.rest_url,data:{_embed:!0,wsu_nid:c.val()}}).done(function(a){0!==a.length&&populate_from_people_directory(a[0])}),a(".wsuwp-profile-about-tabs").on("click",".select",function(){var b=a(this).closest("li"),c=a(".use-bio");b.toggleClass("selected"),b.hasClass("selected")?(c.val(b.data("bio")),b.find(".screen-reader-text").text("Deselect"),b.siblings().removeClass("selected").find(".screen-reader-text").text("Select")):(c.val(""),b.find(".screen-reader-text").text("Select"))}),a(".wsuwp-profile-titles").on("click",".select",function(){var b=a(this).closest("p");b.toggleClass("selected");var c=a(".wsuwp-profile-titles .selected").map(function(){return a(this).index()}).get();a(".use-title").val(c.join(" ")),b.hasClass("selected")?b.find(".screen-reader-text").text("Deselect"):b.find(".screen-reader-text").text("Select")}),a(".wsuwp-profile-photo-collection").on("click",".wsuwp-profile-photo-select",function(){var b=a(this),c=b.closest(".wsuwp-profile-photo-wrapper"),d=a(".use-photo");c.toggleClass("selected").siblings().removeClass("selected"),c.hasClass("selected")?(d.val(a(".wsuwp-profile-photo-wrapper").index(c)),b.attr("aria-label","Deselect"),c.siblings().removeClass("selected").find(".wsuwp-profile-photo-select").attr("aria-label","Select")):(d.val(""),b.attr("aria-label","Select"))}),a("#publish").on("click",function(){var d={},e=a("#title"),f=a("#_wsuwp_profile_alt_office"),g=a("#_wsuwp_profile_alt_phone"),h=a("#_wsuwp_profile_alt_email"),i=a("#_wsuwp_profile_website"),j=a("[name='_wsuwp_profile_title[]']"),k=a("[name='_wsuwp_profile_degree[]']"),l=b.tinymce.get("content"),m=b.tinymce.get("_wsuwp_profile_bio_unit"),n=b.tinymce.get("_wsuwp_profile_bio_university");if(e.val()!==e.data("original")&&(d.title=e.val()),f.val()!==f.data("original")&&(d.office_alt=f.val()),g.val()!==g.data("original")&&(d.phone_alt=g.val()),h.val()!==h.data("original")&&(d.email_alt=h.val()),i.val()!==i.data("original")&&(d.website=i.val()),j){var o=j.map(function(){return a(this).val()}).get();o.join(",")!==profile_original_titles.join(",")&&(d.working_titles=o)}if(k){var p=k.map(function(){return a(this).val()}).get();p.join(",")!==profile_original_degrees.join(",")&&(d.degree=p)}l&&l.getContent()!==profile_original_personal_bio&&(d.content=l.getContent()),m&&m.getContent()!==profile_original_unit_bio&&(d.bio_unit=m.getContent()),n&&n.getContent()!==profile_original_university_bio&&(d.bio_university=n.getContent()),a.isEmptyObject(d)||a.ajax({url:b.wsuwp_people_edit_profile_secondary.rest_url+"/"+c.data("post-id"),method:"POST",beforeSend:function(a){a.setRequestHeader("X-WP-Nonce",b.wsuwp_people_edit_profile_secondary.nonce),a.setRequestHeader("X-WSUWP-UID",b.wsuwp_people_edit_profile_secondary.uid)},data:d})})})}(jQuery,window,document); \ No newline at end of file diff --git a/src/js/admin-edit-profile-secondary.js b/src/js/admin-edit-profile-secondary.js index 447ccbf..14030e2 100644 --- a/src/js/admin-edit-profile-secondary.js +++ b/src/js/admin-edit-profile-secondary.js @@ -11,7 +11,7 @@ var profile_original_titles, // Make a REST request to for profile data from people.wsu.edu. if ( window.wsuwp_people_edit_profile_secondary.load_data ) { - jQuery.ajax( { + $.ajax( { url: window.wsuwp_people_edit_profile_secondary.rest_url, data: { _embed: true, @@ -305,6 +305,34 @@ function populate_from_people_directory( data ) { // Change focus to the post title field (the trigger above focuses the tag input). $( "#title" ).focus(); } + + // Disable inputs if a user doesn't have adequate permissions to edit the profile. + $.ajax( { + url: window.wsuwp_people_edit_profile_secondary.rest_url + "/" + data.id, + type: "POST", + beforeSend: function( xhr ) { + xhr.setRequestHeader( "X-WP-Nonce", window.wsuwp_people_edit_profile_secondary.nonce ); + xhr.setRequestHeader( "X-WSUWP-UID", window.wsuwp_people_edit_profile_secondary.uid ); + }, + error: function() { + $( "#wsuwp_profile_additional_info input" ).prop( "disabled", true ); + $( ".wsuwp-profile-button.remove" ).prop( "disabled", true ); + $( "#new-tag-post_tag" ).prop( "disabled", true ); + $( ".tabs-panel input" ).prop( "disabled", true ); + $( ".wsuwp-profile-add-photo" ).prop( "disabled", true ); + $( ".wsuwp-profile-photo-remove" ).prop( "disabled", true ); + + window.tinymce.get( "content" ).setMode( "readonly" ); + + if ( unit_bio ) { + unit_bio.setMode( "readonly" ); + } + + if ( university_bio ) { + university_bio.setMode( "readonly" ); + } + } + } ); } // Populate the photo collection with data from people.wsu.edu. From 5173ecea572cfc52a9651a6cd1f4139f7a274b6b Mon Sep 17 00:00:00 2001 From: Phil Cable Date: Wed, 19 Apr 2017 08:47:42 -0700 Subject: [PATCH 6/6] Add styles for disabled inputs --- css/admin-person.css | 7 ++++++- src/css/admin-person.css | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/css/admin-person.css b/css/admin-person.css index 22b18f7..664b914 100644 --- a/css/admin-person.css +++ b/css/admin-person.css @@ -75,7 +75,8 @@ } .wsuwp-profile-degrees p:hover .wsuwp-profile-button.select, -.wsuwp-profile-repeatable-field .selected:hover .wsuwp-profile-button.remove { +.wsuwp-profile-repeatable-field .selected:hover .wsuwp-profile-button.remove, +.wsuwp-profile-repeatable-field p:hover button:disabled { display: none; } @@ -91,6 +92,10 @@ text-decoration: none; } +input:disabled { + background: #eee; +} + /* "Card" display */ .profile-card { display: table; diff --git a/src/css/admin-person.css b/src/css/admin-person.css index 84a78d4..3927485 100644 --- a/src/css/admin-person.css +++ b/src/css/admin-person.css @@ -68,7 +68,8 @@ } .wsuwp-profile-degrees p:hover .wsuwp-profile-button.select, -.wsuwp-profile-repeatable-field .selected:hover .wsuwp-profile-button.remove { +.wsuwp-profile-repeatable-field .selected:hover .wsuwp-profile-button.remove, +.wsuwp-profile-repeatable-field p:hover button:disabled { display: none; } @@ -84,6 +85,10 @@ text-decoration: none; } +input:disabled { + background: #eee; +} + /* "Card" display */ .profile-card { display: table;