-
Notifications
You must be signed in to change notification settings - Fork 17
/
shiny-updates.js
113 lines (87 loc) · 2.96 KB
/
shiny-updates.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
window.wp = window.wp || {};
(function( $, wp ) {
wp.ShinyUpdates = {};
wp.ShinyUpdates.updatePlugin = function( plugin, slug ) {
var $message = $( '#' + slug ).next().find( '.update-message' );
var data = {
'action': 'shiny_plugin_update',
'_ajax_nonce': shinyUpdates.ajax_nonce,
'plugin': plugin,
'slug': slug
};
$.ajax({
type: 'post',
dataType: 'json',
url: ajaxurl,
data: data,
success: wp.ShinyUpdates.updateSuccess,
error: wp.ShinyUpdates.updateError
});
$message.addClass( 'updating-message' );
$message.text( shinyUpdates.updatingText );
};
wp.ShinyUpdates.updateSuccess = function( response, status, xhr ) {
if ( response.success ) {
var $message = $( '#' + response.data.slug ).next().find( '.update-message' );
$message.removeClass( 'updating-message' ).addClass( 'updated-message' );
$message.text( shinyUpdates.updatedText );
}
};
wp.ShinyUpdates.updateError = function( xhr, status, error ) {
console.log( error );
};
wp.ShinyUpdates.installPlugin = function( slug ) {
var data = {
'action': 'shiny_plugin_install',
'_ajax_nonce': shinyUpdates.ajax_nonce,
'slug': slug
};
$.ajax({
type: 'post',
dataType: 'json',
url: ajaxurl,
data: data,
success: wp.ShinyUpdates.installSuccess,
error: wp.ShinyUpdates.installError
});
$container = $( '#' + slug + '-card .install-now' ).parent();
$container.html( '<span class="button button-disabled installing">' + shinyUpdates.installingText + '</span>' );
}
wp.ShinyUpdates.installSuccess = function( response, status, xhr ) {
if ( response.success ) {
$button = $( '#' + response.data.slug + '-card .installing' );
$button.removeClass( 'installing' ).addClass( 'installed' );
$button.text( shinyUpdates.installedText );
}
};
wp.ShinyUpdates.installError = function( xhr, status, error ) {
console.log( error );
};
$( document ).ready( function() {
$( '.update-message a' ).on( 'click', function( e ) {
var link = e.target.href;
var $row = $( e.target ).parents( 'tr' ).prev();
// TODO: This can obviously be nicer when incorporated into core.
var re = /\/update.php\?action=upgrade-plugin&plugin=([^&]+)/;
var found = link.match( re );
if ( ! found || found.length < 2 ) {
return;
}
e.preventDefault();
wp.ShinyUpdates.updatePlugin( found[1], $row.prop( 'id' ) );
});
$( '.plugin-card .install-now' ).on( 'click', function( e ) {
var link = e.target.href;
var $row = $( e.target ).parents( 'tr' ).prev();
// TODO: This can obviously be nicer when incorporated into core.
var re = /\/update.php\?action=install-plugin&plugin=([^&]+)/;
var found = link.match( re );
if ( ! found || found.length < 2 ) {
return;
}
$( e.target ).parents( '.plugin-card' ).attr( 'id', found[1] + '-card' );
e.preventDefault();
wp.ShinyUpdates.installPlugin( found[1] );
});
});
})( jQuery, window.wp );