| @@ -1,0 +1,144 @@ | ||
| 1 | +/* global jetpack_post_by_email:true, pbeVars */ | |
| 2 | + | |
| 3 | +( function ( $ ) { | |
| 4 | + var $pbeDisable, | |
| 5 | + $pbeEmail, | |
| 6 | + $pbeEmailWrapper, | |
| 7 | + $pbeEnable, | |
| 8 | + $pbeError, | |
| 9 | + $pbeInfo, | |
| 10 | + $pbeRegenerate, | |
| 11 | + $pbeSpinner; | |
| 12 | + | |
| 13 | + jetpack_post_by_email = { | |
| 14 | + init: function () { | |
| 15 | + $pbeEnable.click( jetpack_post_by_email.enable ); | |
| 16 | + $pbeRegenerate.click( jetpack_post_by_email.regenerate ); | |
| 17 | + $pbeDisable.click( jetpack_post_by_email.disable ); | |
| 18 | + }, | |
| 19 | + | |
| 20 | + enable: function () { | |
| 21 | + $pbeEnable.attr( 'disabled', 'disabled' ); | |
| 22 | + $pbeError.fadeOut(); | |
| 23 | + $pbeSpinner.fadeIn(); | |
| 24 | + | |
| 25 | + jetpack_post_by_email.send_request( | |
| 26 | + { post_by_email_address: 'create' }, | |
| 27 | + jetpack_post_by_email.handle_enabled | |
| 28 | + ); | |
| 29 | + }, | |
| 30 | + | |
| 31 | + handle_enabled: function ( response ) { | |
| 32 | + $pbeRegenerate.removeAttr( 'disabled' ); | |
| 33 | + $pbeDisable.removeAttr( 'disabled' ); | |
| 34 | + | |
| 35 | + if ( response.code === 'success' ) { | |
| 36 | + $pbeEnable.fadeOut( 400, function () { | |
| 37 | + $pbeEnable.removeAttr( 'disabled' ); | |
| 38 | + $pbeEmail.val( response.post_by_email_address ); | |
| 39 | + $pbeInfo.fadeIn(); | |
| 40 | + } ); | |
| 41 | + } else { | |
| 42 | + $pbeError.text( jetpack_post_by_email.parse_error_message( response ) ); | |
| 43 | + $pbeError.fadeIn(); | |
| 44 | + $pbeEnable.removeAttr( 'disabled' ); | |
| 45 | + } | |
| 46 | + | |
| 47 | + $pbeSpinner.fadeOut(); | |
| 48 | + }, | |
| 49 | + | |
| 50 | + regenerate: function () { | |
| 51 | + jetpack_post_by_email.before_request(); | |
| 52 | + | |
| 53 | + jetpack_post_by_email.send_request( | |
| 54 | + { post_by_email_address: 'regenerate' }, | |
| 55 | + jetpack_post_by_email.handle_regenerated | |
| 56 | + ); | |
| 57 | + }, | |
| 58 | + | |
| 59 | + handle_regenerated: function ( response ) { | |
| 60 | + if ( response.code === 'success' ) { | |
| 61 | + $pbeEmailWrapper.fadeOut( 400, function () { | |
| 62 | + $pbeEmail.val( response.post_by_email_address ); | |
| 63 | + $pbeEmailWrapper.fadeIn(); | |
| 64 | + } ); | |
| 65 | + } else { | |
| 66 | + $pbeError.text( jetpack_post_by_email.parse_error_message( response ) ); | |
| 67 | + $pbeError.fadeIn(); | |
| 68 | + } | |
| 69 | + | |
| 70 | + $pbeRegenerate.removeAttr( 'disabled' ); | |
| 71 | + $pbeDisable.removeAttr( 'disabled' ); | |
| 72 | + $pbeSpinner.fadeOut(); | |
| 73 | + }, | |
| 74 | + | |
| 75 | + disable: function () { | |
| 76 | + jetpack_post_by_email.before_request(); | |
| 77 | + | |
| 78 | + jetpack_post_by_email.send_request( | |
| 79 | + { post_by_email_address: 'delete' }, | |
| 80 | + jetpack_post_by_email.handle_disabled | |
| 81 | + ); | |
| 82 | + }, | |
| 83 | + | |
| 84 | + handle_disabled: function ( response ) { | |
| 85 | + if ( response.code === 'success' ) { | |
| 86 | + $pbeEnable.removeAttr( 'disabled' ); | |
| 87 | + $pbeInfo.fadeOut( 400, function () { | |
| 88 | + $pbeRegenerate.removeAttr( 'disabled' ); | |
| 89 | + $pbeDisable.removeAttr( 'disabled' ); | |
| 90 | + $pbeEnable.fadeIn(); | |
| 91 | + } ); | |
| 92 | + } else { | |
| 93 | + $pbeRegenerate.removeAttr( 'disabled' ); | |
| 94 | + $pbeDisable.removeAttr( 'disabled' ); | |
| 95 | + | |
| 96 | + $pbeError.text( jetpack_post_by_email.parse_error_message( response ) ); | |
| 97 | + $pbeError.fadeIn(); | |
| 98 | + } | |
| 99 | + | |
| 100 | + $pbeSpinner.fadeOut(); | |
| 101 | + }, | |
| 102 | + | |
| 103 | + send_request: function ( data, callback ) { | |
| 104 | + var request = new XMLHttpRequest(); | |
| 105 | + request.open( 'POST', '/wp-json/jetpack/v4/settings/' ); | |
| 106 | + request.setRequestHeader( 'Content-Type', 'application/json' ); | |
| 107 | + request.setRequestHeader( 'X-WP-Nonce', pbeVars.rest_nonce ); | |
| 108 | + request.onreadystatechange = function () { | |
| 109 | + if ( this.readyState === XMLHttpRequest.DONE ) { | |
| 110 | + callback( JSON.parse( this.response ) ); | |
| 111 | + } | |
| 112 | + }; | |
| 113 | + request.send( JSON.stringify( data ) ); | |
| 114 | + }, | |
| 115 | + | |
| 116 | + parse_error_message: function ( data ) { | |
| 117 | + if ( data.message ) { | |
| 118 | + return data.message.replace( /^.*?:/, '' ); | |
| 119 | + } | |
| 120 | + | |
| 121 | + return ''; | |
| 122 | + }, | |
| 123 | + | |
| 124 | + before_request: function () { | |
| 125 | + $pbeRegenerate.attr( 'disabled', 'disabled' ); | |
| 126 | + $pbeDisable.attr( 'disabled', 'disabled' ); | |
| 127 | + $pbeError.fadeOut(); | |
| 128 | + $pbeSpinner.fadeIn(); | |
| 129 | + }, | |
| 130 | + }; | |
| 131 | + | |
| 132 | + $( function () { | |
| 133 | + $pbeDisable = $( '#jp-pbe-disable' ); | |
| 134 | + $pbeEmail = $( '#jp-pbe-email' ); | |
| 135 | + $pbeEmailWrapper = $( '#jp-pbe-email-wrapper' ); | |
| 136 | + $pbeEnable = $( '#jp-pbe-enable' ); | |
| 137 | + $pbeError = $( '#jp-pbe-error' ); | |
| 138 | + $pbeInfo = $( '#jp-pbe-info' ); | |
| 139 | + $pbeRegenerate = $( '#jp-pbe-regenerate' ); | |
| 140 | + $pbeSpinner = $( '#jp-pbe-spinner' ); | |
| 141 | + | |
| 142 | + jetpack_post_by_email.init(); | |
| 143 | + } ); | |
| 144 | +} )( jQuery ); | |