forked from akirk/friends
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin-installer.js
152 lines (135 loc) · 3.47 KB
/
plugin-installer.js
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
var friends_plugin_installer = friends_plugin_installer || {};
jQuery(document).ready(function($) {
"use strict";
var is_loading = false;
/**
* Install the plugin
*
* @since 1.0
* @param el object Button element
* @param plugin string Plugin slug
*/
friends_plugin_installer.install_plugin = function(el, plugin) {
// Confirm activation
var r = confirm(friends_plugin_installer_localize.install_now);
if (r) {
is_loading = true;
el.addClass('installing');
$.ajax( {
type: 'POST',
url: friends_plugin_installer_localize.ajax_url,
data: {
action: 'friends_plugin_installer',
plugin: plugin,
nonce: friends_plugin_installer_localize.admin_nonce,
dataType: 'json'
},
success: function(data) {
if( data ) {
if ( data.status === 'success' ) {
el.attr('class', 'activate button button-primary');
el.text(friends_plugin_installer_localize.activate_btn);
} else {
el.removeClass('installing');
}
} else {
el.removeClass('installing');
}
is_loading = false;
},
error: function(xhr, status, error) {
el.removeClass('installing');
is_loading = false;
}
} );
}
}
/**
* Activate the plugin
*
* @since 1.0
* @param el object Button element
* @param plugin string Plugin slug
*/
friends_plugin_installer.activate_plugin = function(el, plugin) {
$.ajax( {
type: 'POST',
url: friends_plugin_installer_localize.ajax_url,
data: {
action: 'friends_plugin_activation',
plugin: plugin,
nonce: friends_plugin_installer_localize.admin_nonce,
dataType: 'json'
},
success: function(data) {
if ( data ) {
if ( data.status === 'success' ) {
el.attr('class', 'installed button disabled');
el.text(friends_plugin_installer_localize.installed_btn);
el.closest('div').find('.deactivate').toggleClass('hidden');
}
}
is_loading = false;
},
error: function(xhr, status, error) {
is_loading = false;
}
} );
};
/**
* Deactivate the plugin
*
* @since 1.0
* @param el object Button element
* @param plugin string Plugin slug
*/
friends_plugin_installer.deactivate_plugin = function(el, plugin) {
$.ajax( {
type: 'POST',
url: friends_plugin_installer_localize.ajax_url,
data: {
action: 'friends_plugin_deactivation',
plugin: plugin,
nonce: friends_plugin_installer_localize.admin_nonce,
dataType: 'json'
},
success: function(data) {
if ( data ) {
if ( data.status === 'success' ) {
el.toggleClass('hidden');
el.closest('div').find('.installed').attr('class', 'activate button button-primary').text(friends_plugin_installer_localize.activate_btn);
}
}
is_loading = false;
},
error: function(xhr, status, error) {
is_loading = false;
}
} );
};
/**
* Install/Activate Button Click
*
* @since 1.0
*/
$(document).on('click', '.friends-plugin-installer a.button:not(.details)', function(e) {
var el = $(this),
plugin = el.data('slug');
e.preventDefault();
if ( !el.hasClass('disabled') ) {
if ( is_loading ) return false;
// Installation
if ( el.hasClass('install') ) {
friends_plugin_installer.install_plugin(el, plugin);
}
// Activation
if ( el.hasClass('activate') ) {
friends_plugin_installer.activate_plugin(el, plugin);
}
// Activation
if ( el.hasClass('deactivate') ) {
friends_plugin_installer.deactivate_plugin(el, plugin);
}
}
} );
});