Skip to content

Commit 02b39b5

Browse files
committed
code fixes
1 parent df56c45 commit 02b39b5

File tree

3 files changed

+128
-94
lines changed

3 files changed

+128
-94
lines changed

includes.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ function hookpress_print_edit_webhook( $id ){
5858
<input type="hidden" name="enabled" id="enabled" value="<?php echo $desc['enabled']; ?>" />
5959
<table>
6060
<tr><td><label style='font-weight: bold' for='edithook'><?php _e("WordPress hook type",'hookpress');?>: </label></td>
61-
<td><input type='radio' id='action' class='newtype' name='newtype' checked='checked'> <?php _e("action","hookpress");?></input>
62-
<input type='radio' id='filter' class='newtype' name='newtype'> <?php _e("filter","hookpress");?></input></td></tr>
61+
<td><input type='radio' id='action' class='newtype' name='newtype' <?php checked('action',$desc['type']);?>> <?php _e("action","hookpress");?></input>
62+
<input type='radio' id='filter' class='newtype' name='newtype' <?php checked('filter',$desc['type']);?>> <?php _e("filter","hookpress");?></input></td></tr>
6363
<tr>
6464
<td><label style='font-weight: bold' for='edithook' id='action_or_filter'>
6565
<?php

options.php

+122-91
Original file line numberDiff line numberDiff line change
@@ -9,121 +9,131 @@
99
}
1010

1111
var getHooks = function getHooks() {
12-
var type = getTb().find('.newtype:checked').attr('id');
12+
console.log('getHooks');
13+
var tb = getTb();
14+
var type = tb.find('.newtype:checked').attr('id');
1315
if (type == 'action') {
14-
getTb().find('#action_or_filter').text('<?php _e("Action:",'hookpress');?> ');
15-
getTb().find('#filtermessage').hide();
16+
tb.find('#action_or_filter').text('<?php _e("Action:",'hookpress');?> ');
17+
tb.find('#filtermessage').hide();
1618
}
1719
if (type == 'filter') {
18-
getTb().find('#action_or_filter').text('<?php _e("Filter:",'hookpress');?> ');
19-
getTb().find('#filtermessage').show();
20+
tb.find('#action_or_filter').text('<?php _e("Filter:",'hookpress');?> ');
21+
tb.find('#filtermessage').show();
2022
}
2123
$.ajax({type:'POST',
2224
url:'admin-ajax.php',
2325
data:'action=hookpress_get_hooks&type='+type,
2426
beforeSend:function(){
25-
getTb().find('#newhook').html('<div class="webhooks-spinner">&nbsp;</div>');
27+
tb.find('#newhook').html('<div class="webhooks-spinner">&nbsp;</div>');
2628
},
2729
success:function(html){
28-
getTb().find('#newhook').html(html);
30+
tb.find('#newhook').html(html);
2931
getFields();
3032
},
3133
dataType:'html'}
3234
)
3335
}
3436

3537
var getFields = function getFields() {
36-
var hook = getTb().find('#newhook').val();
37-
var type = getTb().find('.newtype:checked').attr('id');
38+
console.log('getFields');
39+
var tb = getTb();
40+
var hook = tb.find('#newhook').val();
41+
var type = tb.find('.newtype:checked').attr('id');
3842
$.ajax({type:'POST',
3943
url:'admin-ajax.php',
4044
data:'action=hookpress_get_fields&hook='+hook+'&type='+type,
4145
beforeSend:function(){
42-
getTb().find('#newfields').html('<div class="webhooks-spinner">&nbsp;</div>');
46+
tb.find('#newfields').html('<div class="webhooks-spinner">&nbsp;</div>');
4347
},
4448
success:function(html){
45-
getTb().find('#newfields').html(html)
49+
tb.find('#newfields').html(html)
4650
},
4751
dataType:'html'}
4852
)
4953
};
5054

5155
var getEditHooks = function getEditHooks() {
52-
var type = getTb().find('.newtype:checked').attr('id');
56+
console.log('getEditHooks');
57+
var tb = getTb();
58+
var type = tb.find('.newtype:checked').attr('id');
5359
if (type == 'action') {
54-
getTb().find('#action_or_filter').text('<?php _e("Action:",'hookpress');?> ');
55-
getTb().find('#filtermessage').hide();
60+
tb.find('#action_or_filter').text('<?php _e("Action:",'hookpress');?> ');
61+
tb.find('#filtermessage').hide();
5662
}
5763
if (type == 'filter') {
58-
getTb().find('#action_or_filter').text('<?php _e("Filter:",'hookpress');?> ');
59-
getTb().find('#filtermessage').show();
64+
tb.find('#action_or_filter').text('<?php _e("Filter:",'hookpress');?> ');
65+
tb.find('#filtermessage').show();
6066
}
6167
$.ajax({type:'POST',
6268
url:'admin-ajax.php',
6369
data:'action=hookpress_get_hooks&type='+type,
6470
beforeSend:function(){
65-
getTb().find('#edithook').html('<div class="webhooks-spinner">&nbsp;</div>');
71+
tb.find('#edithook').html('<div class="webhooks-spinner">&nbsp;</div>');
6672
},
6773
success:function(html){
68-
getTb().find('#edithook').html(html);
74+
tb.find('#edithook').html(html);
6975
getEditFields();
7076
},
7177
dataType:'html'}
7278
)
7379
}
7480

7581
var getEditFields = function getEditFields() {
76-
var hook = getTb().find('#edithook').val();
77-
var type = getTb().find('.newtype:checked').attr('id');
82+
console.log('getEditFields');
83+
var tb = getTb();
84+
var hook = tb.find('#edithook').val();
85+
var type = tb.find('.newtype:checked').attr('id');
7886
$.ajax({type:'POST',
7987
url:'admin-ajax.php',
8088
data:'action=hookpress_get_fields&hook='+hook+'&type='+type,
8189
beforeSend:function(){
82-
getTb().find('#editfields').html('<div class="webhooks-spinner">&nbsp;</div>');
90+
tb.find('#editfields').html('<div class="webhooks-spinner">&nbsp;</div>');
8391
},
8492
success:function(html){
85-
getTb().find('#editfields').html(html);
93+
tb.find('#editfields').html(html);
8694
},
8795
dataType:'html'}
8896
)
8997
};
9098

9199
var editSubmit = function editSubmit() {
92-
if (!getTb().find('#editfields').val()) {
93-
getTb().find('#editindicator').html('<small><?php _e("You must select at least one field to send.","hookpress");?></small>');
100+
console.log('editSubmit');
101+
var tb = getTb();
102+
if (!tb.find('#editfields').val()) {
103+
tb.find('#editindicator').html('<small><?php _e("You must select at least one field to send.","hookpress");?></small>');
94104
return;
95105
}
96-
if (!/^https?:\/\/\w+/.test(getTb().find('#editurl').val())) {
97-
getTb().find('#editindicator').html('<small><?php _e("Please enter a valid URL.","hookpress");?></small>');
106+
if (!/^https?:\/\/\w+/.test(tb.find('#editurl').val())) {
107+
tb.find('#editindicator').html('<small><?php _e("Please enter a valid URL.","hookpress");?></small>');
98108
return;
99109
}
100110

101-
getTb().find('#editindicator').html('<div class="webhooks-spinner">&nbsp;</div>');
111+
tb.find('#editindicator').html('<div class="webhooks-spinner">&nbsp;</div>');
102112

103-
id = getTb().find('#edit-hook-id').val();
113+
id = tb.find('#edit-hook-id').val();
104114

105115
$.ajax({type: 'POST',
106116
url:'admin-ajax.php',
107117
data:'action=hookpress_add_fields'
108-
+'&fields='+getTb().find('#editfields').val().join()
109-
+'&url='+getTb().find('#editurl').val()
110-
+'&type='+getTb().find('.newtype:checked').attr('id')
111-
+'&hook='+getTb().find('#edithook').val()
112-
+'&enabled='+getTb().find('#enabled').val()
118+
+'&fields='+tb.find('#editfields').val().join()
119+
+'&url='+tb.find('#editurl').val()
120+
+'&type='+tb.find('.newtype:checked').attr('id')
121+
+'&hook='+tb.find('#edithook').val()
122+
+'&enabled='+tb.find('#enabled').val()
113123
+'&id='+id
114-
+'&_nonce='+getTb().find('#submit-nonce').val(),
124+
+'&_nonce='+tb.find('#submit-nonce').val(),
115125
beforeSend:function(){
116-
getTb().find('#editsubmit').hide();
117-
getTb().find('#editcancel').hide()
126+
tb.find('#editsubmit').hide();
127+
tb.find('#editcancel').hide()
118128
},
119129
success:function(html){
120-
getTb().find('#editsubmit').show();
121-
getTb().find('#editcancel').show()
122-
getTb().find('#editindicator').html('');
130+
tb.find('#editsubmit').show();
131+
tb.find('#editcancel').show()
132+
tb.find('#editindicator').html('');
123133
if (/^ERROR/.test(html))
124-
getTb().find('#editindicator').html(html);
134+
tb.find('#editindicator').html(html);
125135
else if (!html)
126-
getTb().find('#editindicator').html('<?php _e("There was an unknown error.","hookpress");?>');
136+
tb.find('#editindicator').html('<?php _e("There was an unknown error.","hookpress");?>');
127137
else {
128138
$('#'+id).replaceWith(html);
129139
tb_init('a.thickbox, area.thickbox, input.thickbox');
@@ -135,44 +145,48 @@
135145
};
136146

137147
var enforceFirst = function enforceFirst() {
138-
var type = getTb().find('.newtype:checked').attr('id');
148+
console.log('enforceFirst');
149+
var tb = getTb();
150+
var type = tb.find('.newtype:checked').attr('id');
139151
if (type == 'action')
140152
return;
141-
getTb().find('option.first').attr('selected',true);
153+
tb.find('option.first').attr('selected',true);
142154
}
143155

144156
var newSubmit = function newSubmit() {
145-
if (!getTb().find('#newfields').val()) {
146-
getTb().find('#newindicator').html('<small><?php _e("You must select at least one field to send.","hookpress");?></small>');
157+
console.log('newSubmit');
158+
var tb = getTb();
159+
if (!tb.find('#newfields').val()) {
160+
tb.find('#newindicator').html('<small><?php _e("You must select at least one field to send.","hookpress");?></small>');
147161
return;
148162
}
149-
if (!/^https?:\/\/\w+/.test(getTb().find('#newurl').val())) {
150-
getTb().find('#newindicator').html('<small><?php _e("Please enter a valid URL.","hookpress");?></small>');
163+
if (!/^https?:\/\/\w+/.test(tb.find('#newurl').val())) {
164+
tb.find('#newindicator').html('<small><?php _e("Please enter a valid URL.","hookpress");?></small>');
151165
return;
152166
}
153167

154-
getTb().find('#newindicator').html('<div class="webhooks-spinner">&nbsp;</div>');
168+
tb.find('#newindicator').html('<div class="webhooks-spinner">&nbsp;</div>');
155169

156170
$.ajax({type: 'POST',
157171
url:'admin-ajax.php',
158172
data:'action=hookpress_add_fields'
159-
+'&fields='+getTb().find('#newfields').val().join()
160-
+'&url='+getTb().find('#newurl').val()
161-
+'&type='+getTb().find('.newtype:checked').attr('id')
162-
+'&hook='+getTb().find('#newhook').val()
163-
+'&_nonce='+getTb().find('#submit-nonce').val(),
173+
+'&fields='+tb.find('#newfields').val().join()
174+
+'&url='+tb.find('#newurl').val()
175+
+'&type='+tb.find('.newtype:checked').attr('id')
176+
+'&hook='+tb.find('#newhook').val()
177+
+'&_nonce='+tb.find('#submit-nonce').val(),
164178
beforeSend:function(){
165-
getTb().find('#newsubmit').hide();
166-
getTb().find('#newcancel').hide()
179+
tb.find('#newsubmit').hide();
180+
tb.find('#newcancel').hide()
167181
},
168182
success:function(html){
169-
getTb().find('#newsubmit').show();
170-
getTb().find('#newcancel').show()
171-
getTb().find('#newindicator').html('');
183+
tb.find('#newsubmit').show();
184+
tb.find('#newcancel').show()
185+
tb.find('#newindicator').html('');
172186
if (/^ERROR/.test(html))
173-
getTb().find('#newindicator').html(html);
187+
tb.find('#newindicator').html(html);
174188
else if (!html)
175-
getTb().find('#newindicator').html('<?php _e("There was an unknown error.","hookpress");?>');
189+
tb.find('#newindicator').html('<?php _e("There was an unknown error.","hookpress");?>');
176190
else {
177191
var newhook = $(html);
178192
newhook.css('background-color','rgb(255, 251, 204)');
@@ -189,6 +203,7 @@ function(){newhook.css('background-color','transparent')});
189203
};
190204

191205
var deleteHook = function deleteHook(id) {
206+
console.log('deleteHook');
192207
var nonce = $('#delete-nonce-' + id).val();
193208
$.ajax({type: 'POST',
194209
url:'admin-ajax.php',
@@ -206,6 +221,7 @@ function(){newhook.css('background-color','transparent')});
206221
}
207222

208223
var setHookEnabled = function setHookEnabled(id, nonce, boolean) {
224+
console.log('setHookEnabled');
209225
$.ajax({type: 'POST',
210226
url:'admin-ajax.php',
211227
beforeSend:function(){$('#' + id + ' span.edit').html('<div class="webhooks-spinner">&nbsp;</div>')},
@@ -226,15 +242,29 @@ function(){newhook.css('background-color','transparent')});
226242
}
227243

228244
var setupEditHook = function setupEditHook(id) {
245+
console.log('setupEditHook');
246+
var tb = getTb();
229247
$.ajax({type: 'POST',
230248
url:'admin-ajax.php',
231249
data:'action=hookpress_edit_hook&id='+id,
232-
success:function(html){
233-
$('#TB_ajaxContent').html(html);
234-
getTb().find('#edithook').change(getEditFields);
235-
getTb().find('#editfields').change(enforceFirst);
236-
getTb().find('#editsubmit').click(editSubmit);
237-
getTb().find('#editcancel').click(tb_remove);
250+
success: function(html){
251+
$('#TB_ajaxContent').html(html)
252+
.find('.newtype').change(getEditHooks).end()
253+
.find('#edithook').change(getEditFields).end()
254+
.find('#editfields').change(enforceFirst).end()
255+
.find('#editsubmit').click(editSubmit).end()
256+
.find('#editcancel').click(tb_remove);
257+
258+
var type = $('#TB_ajaxContent').find('.newtype:checked').attr('id');
259+
if (type == 'action') {
260+
$('#TB_ajaxContent').find('#action_or_filter').text('<?php _e("Action:",'hookpress');?> ');
261+
$('#TB_ajaxContent').find('#filtermessage').hide();
262+
}
263+
if (type == 'filter') {
264+
$('#TB_ajaxContent').find('#action_or_filter').text('<?php _e("Filter:",'hookpress');?> ');
265+
$('#TB_ajaxContent').find('#filtermessage').show();
266+
}
267+
238268
},
239269
dataType:'html'}
240270
);
@@ -253,31 +283,32 @@ function(){newhook.css('background-color','transparent')});
253283
});
254284

255285
var setEvents = function setEvents() {
256-
$(document).on('change','#TB_window .newtype',getHooks);
257-
$(document).on('change','#TB_window #newhook',getFields);
258-
$(document).on('change','#TB_window #newfields',enforceFirst);
259-
$(document).on('click','#TB_window #newsubmit',newSubmit);
260-
$(document).on('click','#TB_window #newcancel',tb_remove);
261-
262-
$(document).on('click', '#webhooks .delete', function(e){
263-
var id = e.currentTarget.id.replace('delete','');
264-
deleteHook(id);
265-
});
266-
$(document).on('click', '#webhooks .edit', function(e){
267-
var id = e.currentTarget.id.replace('edit','');
268-
if(id){setupEditHook(id);}
269-
});
270-
271-
$(document).on('click', '#webhooks .on', function(e){
272-
var id = e.currentTarget.id.replace('on','');
273-
var nonce = $('#action-nonce-' + id).val();
274-
if(id && nonce){setHookEnabled(id, nonce, 'false');}
275-
});
276-
$(document).on('click', '#webhooks .off', function(e){
277-
var id = e.currentTarget.id.replace('off','');
278-
var nonce = $('#action-nonce-' + id).val();
279-
if(id&&nonce){setHookEnabled(id, nonce, 'true');}
280-
});
286+
$(document.body)
287+
.on('change','#TB_window .newtype',getHooks)
288+
.on('change','#TB_window #newhook',getFields)
289+
.on('change','#TB_window #newfields',enforceFirst)
290+
.on('click','#TB_window #newsubmit',newSubmit)
291+
.on('click','#TB_window #newcancel',tb_remove);
292+
293+
$('#webhooks')
294+
.on('click', '.delete', function(e){
295+
var id = e.currentTarget.id.replace('delete','');
296+
deleteHook(id);
297+
})
298+
.on('click', '.edit', function(e){
299+
var id = e.currentTarget.id.replace('edit','');
300+
if(id){setupEditHook(id);}
301+
})
302+
.on('click', '.on', function(e){
303+
var id = e.currentTarget.id.replace('on','');
304+
var nonce = $('#action-nonce-' + id).val();
305+
if(id && nonce){setHookEnabled(id, nonce, 'false');}
306+
})
307+
.on('click', '.off', function(e){
308+
var id = e.currentTarget.id.replace('off','');
309+
var nonce = $('#action-nonce-' + id).val();
310+
if(id&&nonce){setHookEnabled(id, nonce, 'true');}
311+
});
281312
}
282313

283314
})(jQuery);

readme.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Donate link: http://tinyurl.com/donatetomitcho
77
Tags: hook, filter, action, plugin, webhook, webhooks, notification, internal
88
Requires at least: 3.6
99
Tested up to: 4.0
10-
Stable tag: 1.12
10+
Stable tag: 1.13
1111

1212
HookPress turns your WordPress-internal hooks into webhooks. Possible uses include generating push notifications or extending WordPress with non-PHP.
1313

@@ -40,6 +40,9 @@ HookPress currently makes requests synchronously so can measurably affect perfor
4040

4141
== Changelog ==
4242

43+
= 1.13 =
44+
* Code cleanup, fix bugs introduced in previous version.
45+
4346
= 1.12 =
4447
* Upgraded to work properly with jQuery 1.9+. [Props liquidgecha](https://github.com/mitcho/hookpress/commit/0b21dfec8136d51971a21fb6cbdd4ff2b8d60753).
4548
* Dropped the initial 0. in the version number, as that was just ridiculous. :)

0 commit comments

Comments
 (0)