diff options
Diffstat (limited to 'plugins/jetpack/modules/post-by-email/post-by-email.js')
-rw-r--r-- | plugins/jetpack/modules/post-by-email/post-by-email.js | 110 |
1 files changed, 63 insertions, 47 deletions
diff --git a/plugins/jetpack/modules/post-by-email/post-by-email.js b/plugins/jetpack/modules/post-by-email/post-by-email.js index a8141684..c2fdc0ad 100644 --- a/plugins/jetpack/modules/post-by-email/post-by-email.js +++ b/plugins/jetpack/modules/post-by-email/post-by-email.js @@ -1,6 +1,6 @@ -/* global jetpack_post_by_email:true, ajaxurl, pbeVars */ +/* global jetpack_post_by_email:true, pbeVars */ -( function( $ ) { +( function ( $ ) { var $pbeDisable, $pbeEmail, $pbeEmailWrapper, @@ -11,37 +11,35 @@ $pbeSpinner; jetpack_post_by_email = { - init: function() { + init: function () { $pbeEnable.click( jetpack_post_by_email.enable ); $pbeRegenerate.click( jetpack_post_by_email.regenerate ); $pbeDisable.click( jetpack_post_by_email.disable ); }, - enable: function() { + enable: function () { $pbeEnable.attr( 'disabled', 'disabled' ); $pbeError.fadeOut(); $pbeSpinner.fadeIn(); - var data = { - action: 'jetpack_post_by_email_enable', - pbe_nonce: pbeVars.nonces.enable, - }; - - $.post( ajaxurl, data, jetpack_post_by_email.handle_enabled ); + jetpack_post_by_email.send_request( + { post_by_email_address: 'create' }, + jetpack_post_by_email.handle_enabled + ); }, - handle_enabled: function( response ) { + handle_enabled: function ( response ) { $pbeRegenerate.removeAttr( 'disabled' ); $pbeDisable.removeAttr( 'disabled' ); - if ( response.success ) { - $pbeEnable.fadeOut( 400, function() { + if ( response.code === 'success' ) { + $pbeEnable.fadeOut( 400, function () { $pbeEnable.removeAttr( 'disabled' ); - $pbeEmail.val( response.data ); + $pbeEmail.val( response.post_by_email_address ); $pbeInfo.fadeIn(); } ); } else { - $pbeError.text( response.data ); + $pbeError.text( jetpack_post_by_email.parse_error_message( response ) ); $pbeError.fadeIn(); $pbeEnable.removeAttr( 'disabled' ); } @@ -49,28 +47,23 @@ $pbeSpinner.fadeOut(); }, - regenerate: function() { - $pbeRegenerate.attr( 'disabled', 'disabled' ); - $pbeDisable.attr( 'disabled', 'disabled' ); - $pbeError.fadeOut(); - $pbeSpinner.fadeIn(); - - var data = { - action: 'jetpack_post_by_email_regenerate', - pbe_nonce: pbeVars.nonces.regenerate, - }; + regenerate: function () { + jetpack_post_by_email.before_request(); - $.post( ajaxurl, data, jetpack_post_by_email.handle_regenerated ); + jetpack_post_by_email.send_request( + { post_by_email_address: 'regenerate' }, + jetpack_post_by_email.handle_regenerated + ); }, - handle_regenerated: function( response ) { - if ( response.success ) { - $pbeEmailWrapper.fadeOut( 400, function() { - $pbeEmail.val( response.data ); + handle_regenerated: function ( response ) { + if ( response.code === 'success' ) { + $pbeEmailWrapper.fadeOut( 400, function () { + $pbeEmail.val( response.post_by_email_address ); $pbeEmailWrapper.fadeIn(); } ); } else { - $pbeError.text( response.data ); + $pbeError.text( jetpack_post_by_email.parse_error_message( response ) ); $pbeError.fadeIn(); } @@ -79,24 +72,19 @@ $pbeSpinner.fadeOut(); }, - disable: function() { - $pbeRegenerate.attr( 'disabled', 'disabled' ); - $pbeDisable.attr( 'disabled', 'disabled' ); - $pbeError.fadeOut(); - $pbeSpinner.fadeIn(); - - var data = { - action: 'jetpack_post_by_email_disable', - pbe_nonce: pbeVars.nonces.disable, - }; + disable: function () { + jetpack_post_by_email.before_request(); - $.post( ajaxurl, data, jetpack_post_by_email.handle_disabled ); + jetpack_post_by_email.send_request( + { post_by_email_address: 'delete' }, + jetpack_post_by_email.handle_disabled + ); }, - handle_disabled: function( response ) { - if ( response.success ) { + handle_disabled: function ( response ) { + if ( response.code === 'success' ) { $pbeEnable.removeAttr( 'disabled' ); - $pbeInfo.fadeOut( 400, function() { + $pbeInfo.fadeOut( 400, function () { $pbeRegenerate.removeAttr( 'disabled' ); $pbeDisable.removeAttr( 'disabled' ); $pbeEnable.fadeIn(); @@ -105,15 +93,43 @@ $pbeRegenerate.removeAttr( 'disabled' ); $pbeDisable.removeAttr( 'disabled' ); - $pbeError.text( response.data ); + $pbeError.text( jetpack_post_by_email.parse_error_message( response ) ); $pbeError.fadeIn(); } $pbeSpinner.fadeOut(); }, + + send_request: function ( data, callback ) { + var request = new XMLHttpRequest(); + request.open( 'POST', '/wp-json/jetpack/v4/settings/' ); + request.setRequestHeader( 'Content-Type', 'application/json' ); + request.setRequestHeader( 'X-WP-Nonce', pbeVars.rest_nonce ); + request.onreadystatechange = function () { + if ( this.readyState === XMLHttpRequest.DONE ) { + callback( JSON.parse( this.response ) ); + } + }; + request.send( JSON.stringify( data ) ); + }, + + parse_error_message: function ( data ) { + if ( data.message ) { + return data.message.replace( /^.*?:/, '' ); + } + + return ''; + }, + + before_request: function () { + $pbeRegenerate.attr( 'disabled', 'disabled' ); + $pbeDisable.attr( 'disabled', 'disabled' ); + $pbeError.fadeOut(); + $pbeSpinner.fadeIn(); + }, }; - $( function() { + $( function () { $pbeDisable = $( '#jp-pbe-disable' ); $pbeEmail = $( '#jp-pbe-email' ); $pbeEmailWrapper = $( '#jp-pbe-email-wrapper' ); |