admin.js
131 lines
| 1 | jQuery(function() { |
| 2 | var el_notice = jQuery( ".frash-notice" ), |
| 3 | type = el_notice.find( "input[name=type]" ).val(), |
| 4 | plugin_id = el_notice.find( "input[name=plugin_id]" ).val(), |
| 5 | url_wp = el_notice.find( "input[name=url_wp]" ).val(), |
| 6 | drip_plugin = el_notice.find( "input[name=drip_plugin]" ).val(), |
| 7 | inp_email = el_notice.find( "input[name=email]" ) |
| 8 | btn_act = el_notice.find( ".frash-notice-act" ), |
| 9 | btn_dismiss = el_notice.find( ".frash-notice-dismiss" ) |
| 10 | ajax_data = {}; |
| 11 | |
| 12 | ajax_data.plugin_id = plugin_id; |
| 13 | ajax_data.type = type; |
| 14 | |
| 15 | function init_email() { |
| 16 | if ( ! inp_email.length ) { return; } |
| 17 | |
| 18 | // Adjust the size of the email field to its contents. |
| 19 | function adjust_email_size() { |
| 20 | var width, tmp = jQuery( "<span></span>" ); |
| 21 | |
| 22 | tmp.addClass( "input-field" ).text( inp_email.val() ); |
| 23 | tmp.appendTo( "body" ); |
| 24 | width = parseInt( tmp.width() ); |
| 25 | tmp.remove(); |
| 26 | |
| 27 | inp_email.width( width + 34 ); |
| 28 | } |
| 29 | |
| 30 | function email_keycheck( ev ) { |
| 31 | if ( 13 === ev.keyCode ) { |
| 32 | btn_act.click(); |
| 33 | } else { |
| 34 | adjust_email_size(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | inp_email.keyup( email_keycheck ).focus().select(); |
| 39 | adjust_email_size(); |
| 40 | } |
| 41 | |
| 42 | // Display the notice after the page was loaded. |
| 43 | function initialize() { |
| 44 | el_notice.fadeIn( 500 ); |
| 45 | init_email(); |
| 46 | } |
| 47 | |
| 48 | // Hide the notice after a CTA button was clicked |
| 49 | function remove_notice() { |
| 50 | el_notice.fadeTo( 100 , 0, function() { |
| 51 | el_notice.slideUp( 100, function() { |
| 52 | el_notice.remove(); |
| 53 | }); |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | // Open a tab to rate the plugin. |
| 58 | function act_rate() { |
| 59 | var url = url_wp.replace( /\/plugins\//, "/support/view/plugin-reviews/" ) + "?rate=5#postform", |
| 60 | link = jQuery( '<a href="' + url + '" target="_blank">Rate</a>' ); |
| 61 | |
| 62 | link.appendTo( "body" ); |
| 63 | link[0].click(); |
| 64 | link.remove(); |
| 65 | } |
| 66 | |
| 67 | // Submit the user to our email list. |
| 68 | function act_email() { |
| 69 | var email = inp_email.val(); |
| 70 | |
| 71 | // First create a new subscriber. |
| 72 | _dcq.push([ |
| 73 | "identify", |
| 74 | { email: email } |
| 75 | ]); |
| 76 | |
| 77 | // Then trigger the specified rule. |
| 78 | _dcq.push([ |
| 79 | "track", |
| 80 | "Free plugin email course", |
| 81 | {"Plugin": drip_plugin} |
| 82 | ]); |
| 83 | } |
| 84 | |
| 85 | // Notify WordPress about the users choice and close the message. |
| 86 | function notify_wordpress( action, message ) { |
| 87 | el_notice.attr( "data-message", message ); |
| 88 | el_notice.addClass( "loading" ); |
| 89 | |
| 90 | ajax_data.action = action; |
| 91 | jQuery.post( |
| 92 | window.ajaxurl, |
| 93 | ajax_data, |
| 94 | remove_notice |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | // Handle click on the primary CTA button. |
| 99 | // Either open the wp.org page or submit the email address. |
| 100 | btn_act.click(function( ev ) { |
| 101 | ev.preventDefault(); |
| 102 | |
| 103 | switch ( type ) { |
| 104 | case 'rate': act_rate(); break; |
| 105 | case 'email': act_email(); break; |
| 106 | } |
| 107 | |
| 108 | notify_wordpress( "frash_act", btn_act.data( "msg" ) ); |
| 109 | }); |
| 110 | |
| 111 | // Dismiss the notice without any action. |
| 112 | btn_dismiss.click(function( ev ) { |
| 113 | ev.preventDefault(); |
| 114 | |
| 115 | notify_wordpress( "frash_dismiss", btn_dismiss.data( "msg" ) ); |
| 116 | }); |
| 117 | |
| 118 | window.setTimeout( initialize, 500 ); |
| 119 | }); |
| 120 | |
| 121 | // Drip integration |
| 122 | var _dcq = _dcq || []; |
| 123 | var _dcs = _dcs || {}; |
| 124 | |
| 125 | _dcs.account = '6994213'; |
| 126 | var dc = document.createElement( 'script' ); |
| 127 | dc.type = 'text/javascript'; dc.async = true; |
| 128 | dc.src = '//tag.getdrip.com/6994213.js'; |
| 129 | var s = document.getElementsByTagName('script')[0]; |
| 130 | s.parentNode.insertBefore(dc, s); |
| 131 | // End of drip integration |