summaryrefslogtreecommitdiff
blob: 9c54bc9d11536262fb7126177b830611aac69340 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*****************************************************************************************************/
/*                                                                                                   */
/*                                    	'DISCONNECT FROM APPTICLES'                                  */
/*                                                                                                   */
/*****************************************************************************************************/

function WMP_DISCONNECT(){

    var JSObject = this;

    this.type = "wmp_disconnect";

    this.DOMDoc;

    this.send_btn;
	this.redirectTo;
	this.submitUrl;

	/*****************************************************************************************************/
    /*                                                                                                   */
    /*                              FUNCTION INIT - called from WMPJSInterface                           */
    /*                                                                                                   */
    /*****************************************************************************************************/
    this.init = function(){

        // save a reference to WMPJSInterface Object
        WMPJSInterface = window.parent.WMPJSInterface;

        // save a reference to "SEND" Button
        this.send_btn = jQuery('#'+this.type+'_send_btn',this.DOMDoc).get(0);
       // add actions to send, cancel, ... buttons
        this.addButtonsActions();

        
    }




	/*****************************************************************************************************/
    /*                                                                                                   */
    /*                                  FUNCTION ADD BUTTONS ACTIONS                                     */
    /*                                                                                                   */
    /*****************************************************************************************************/
    this.addButtonsActions = function(){

        /*******************************************************/
        /*                     SEND "BUTTON"                   */
        /*******************************************************/
        jQuery(this.send_btn).unbind("click");
        jQuery(this.send_btn).bind("click",function(){
            JSObject.disableButton(this);
            JSObject.sendData();
        })
        JSObject.enableButton(this.send_btn);

    }


    /*****************************************************************************************************/
    /*                                                                                                   */
    /*                                 FUNCTION ENABLE BUTTON                                            */
    /*                                                                                                   */
    /*****************************************************************************************************/
    this.enableButton = function(btn){
        jQuery(btn).css('cursor','pointer');
        jQuery(btn).animate({opacity:1},100);
    }


    /*****************************************************************************************************/
    /*                                                                                                   */
    /*                                 FUNCTION DISABLE BUTTON                                           */
    /*                                                                                                   */
    /*****************************************************************************************************/
    this.disableButton = function(btn){
        jQuery(btn).unbind("click");
        jQuery(btn).animate({opacity:0.4},100);
        jQuery(btn).css('cursor','default');
    }

    
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                          FUNCTION SEND DATA   								                     */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.sendData = function(){
		
		var isConfirmed = confirm("Are you sure you want to disconnect from Appticles?");
	
		if (isConfirmed) {
		
			WMPJSInterface.Preloader.start();
			
			jQuery.ajax({
				url: JSObject.submitURL,
				type: 'get',
				data: { 
					'apiKey':    jQuery("#"+JSObject.type+"_apikey", JSObject.DOMDoc).val()
				},
                dataType: "jsonp",
				success: function(responseJSON){

                    WMPJSInterface.Preloader.remove(100);
					
					var JSON = eval (responseJSON);
					var response = Boolean(Number(String(JSON.status)));

					/*if (response == 0) {
						
                        if (JSON.message != undefined) {
                            
                            WMPJSInterface.Loader.display({message: JSON.message});
                        }
					}*/

                    // Make the request to reset the API key even if the response from the API was an error
                    // (ex. the Wordpress settings might be already deleted from Appticles)
                    jQuery.post(

                        ajaxurl,
                        {
                            'action': 'wmp_premium_disconnect',
                            'api_key': jQuery("#"+JSObject.type+"_apikey", JSObject.DOMDoc).val(),
                            'active': '0'
                        },
                        function(response1){

                            response1 = Boolean(Number(String(response1)));

                            if(response1 == 1) {
                                window.location.href = JSObject.redirectTo;
                            } else {
                                var message = 'There was an error. Please reload the page and try again in few seconds or contact the plugin administrator if the problem persists.';
                                WMPJSInterface.Loader.display({message: message});
                            }
                        }
                    );

							
				},
				error: function(responseJSON){
				    
                     // API endpoint is turned off
                    WMPJSInterface.Preloader.remove(100);
                    WMPJSInterface.Loader.display({message: "Disconnect endpoint is unreachable. Please contact support."});
				}
			});
		}
		
	}

}