Skip to content

Commit 580b2b1

Browse files
authored
Merge pull request #27 from visual-framework/SCTASK0023997-add-post-type-in-hookpress
Add post type is hookpress
2 parents 1520424 + 5c34ab6 commit 580b2b1

3 files changed

Lines changed: 61 additions & 2 deletions

File tree

hookpress/includes.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ function hookpress_print_edit_webhook( $id ){
4646

4747
$webhooks = hookpress_get_hooks( );
4848
$desc = $webhooks[$id];
49-
49+
5050
if ($desc['type'] == 'action')
5151
$hooks = array_keys($hookpress_actions);
5252
if ($desc['type'] == 'filter')
5353
$hooks = array_keys($hookpress_filters);
54+
$post_types = get_post_types(['public'=> true ]);
55+
5456
?>
5557
<div id='hookpress-webhook' style='display:block;'>
5658
<form id='editform'>
@@ -61,6 +63,21 @@ function hookpress_print_edit_webhook( $id ){
6163
<td><input type='radio' id='action' class='newtype' name='newtype' <?php checked('action',$desc['type']);?>> <?php _e("action","hookpress");?></input>
6264
<input type='radio' id='filter' class='newtype' name='newtype' <?php checked('filter',$desc['type']);?>> <?php _e("filter","hookpress");?></input></td></tr>
6365
<tr>
66+
<tr><td><label style='font-weight: bold' for='edithook'><?php _e("Post type",'hookpress');?>: </label></td>
67+
<td>
68+
<select name="post_type" id="post_type" multiple>
69+
<?php
70+
sort($post_types);
71+
foreach ($post_types as $post_type) {
72+
if($desc['post_type']){
73+
$selected = in_array($post_type,$desc['post_type'])?'selected="true"':'';
74+
}
75+
$post_type = esc_html( $post_type );
76+
echo "<option value='$post_type' $selected>$post_type</option>";
77+
}?>
78+
</select>
79+
</td></tr>
80+
<tr>
6481
<td><label style='font-weight: bold' for='edithook' id='action_or_filter'>
6582
<?php
6683
if ($desc['type'] == 'action')
@@ -166,12 +183,20 @@ function hookpress_print_webhook_row( $id ) {
166183
$activeornot = $desc['enabled'] ? 'active' : 'inactive';
167184

168185
$html_safe['hook'] = esc_html( $desc['hook'] );
186+
if( count( $desc['post_type'] ) > 1 ) {
187+
$desc['post_type'] = array_map( 'esc_html', $desc['post_type'] );
188+
$html_safe['post_type'] = implode(',', $desc['post_type'] );
189+
} else{
190+
$html_safe['post_type'] = esc_html( $desc['post_type'][0] );
191+
}
192+
169193
$html_safe['url'] = esc_html( $desc['url'] );
170194

171195
echo "
172196
<tr id='$id' class='$activeornot'>
173197
<td class='webhook-title'><strong>{$html_safe['hook']}</strong>
174198
<div class='row-actions'>$nonce_action $nonce_delete<span class='edit'>$edit | <span class='delete'>$delete | </span><span class='action'>$action</span></div></td>
199+
<td class='desc'><p>{$html_safe['post_type']}</p></td>
175200
<td class='desc'><p>{$html_safe['url']}</p></td>
176201
<td class='desc'><code ".($desc['type'] == 'filter' ? " style='background-color:#ECEC9D' title='".__('The data in the highlighted field is expected to be returned from the webhook, with modification.','hookpress')."'":"").">$fields</code></td>
177202
</tr>\n";
@@ -188,6 +213,7 @@ function hookpress_print_webhooks_table() {
188213
<thead>
189214
<tr>
190215
<th scope="col" class="manage-column" style="width:15%"><?php _e("Hook","hookpress");?></th>
216+
<th scope="col" class="manage-column" style="width:25%"><?php _e("Post Type","hookpress");?></th>
191217
<th scope="col" class="manage-column" style="width:25%"><?php _e("URL","hookpress");?></th>
192218
<th scope="col" class="manage-column"><?php _e("Fields","hookpress");?></th>
193219
</tr>
@@ -196,6 +222,7 @@ function hookpress_print_webhooks_table() {
196222
<tfoot>
197223
<tr>
198224
<th scope="col" class="manage-column"><?php _e("Hook","hookpress");?></th>
225+
<th scope="col" class="manage-column"><?php _e("Post Type","hookpress");?></th>
199226
<th scope="col" class="manage-column"><?php _e("URL","hookpress");?></th>
200227
<th scope="col" class="manage-column"><?php _e("Fields","hookpress");?></th>
201228
</tr>
@@ -268,6 +295,10 @@ function hookpress_generic_action($id,$args) {
268295
$newobj = array();
269296
switch($arg_names[$i]) {
270297
case 'POST':
298+
299+
if(in_array($args[1]->post_type,$desc['post_type']) != 1){
300+
return;
301+
}
271302
case 'ATTACHMENT':
272303
$newobj = get_post($arg,ARRAY_A);
273304

hookpress/options.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,19 @@
107107
tb.find('#editindicator').html('<small><?php _e("Please enter a valid URL.","hookpress");?></small>');
108108
return;
109109
}
110+
if (tb.find('#post_type').val() == '') {
111+
tb.find('#editindicator').html('<small><?php _e("You must select at least one post type.","hookpress");?></small>');
112+
return;
113+
}
110114

111115
tb.find('#editindicator').html('<div class="webhooks-spinner">&nbsp;</div>');
112-
116+
113117
id = tb.find('#edit-hook-id').val();
114118

115119
$.ajax({type: 'POST',
116120
url:'admin-ajax.php',
117121
data:'action=hookpress_add_fields'
122+
+'&post_type='+tb.find('#post_type').val().join()
118123
+'&fields='+tb.find('#editfields').val().join()
119124
+'&url='+tb.find('#editurl').val()
120125
+'&type='+tb.find('.newtype:checked').attr('id')
@@ -164,12 +169,17 @@
164169
tb.find('#newindicator').html('<small><?php _e("Please enter a valid URL.","hookpress");?></small>');
165170
return;
166171
}
172+
if (tb.find('#post_type').val() == '') {
173+
tb.find('#newindicator').html('<small><?php _e("You must select at least one post type.","hookpress");?></small>');
174+
return;
175+
}
167176

168177
tb.find('#newindicator').html('<div class="webhooks-spinner">&nbsp;</div>');
169178

170179
$.ajax({type: 'POST',
171180
url:'admin-ajax.php',
172181
data:'action=hookpress_add_fields'
182+
+'&post_type='+tb.find('#post_type').val().join()
173183
+'&fields='+tb.find('#newfields').val().join()
174184
+'&url='+tb.find('#newurl').val()
175185
+'&type='+tb.find('.newtype:checked').attr('id')
@@ -349,6 +359,7 @@ function(){newhook.css('background-color','transparent')});
349359
$pos = strpos($display_version,'.')+2;
350360
$display_version = substr($display_version,0,$pos).'.'.substr($display_version,$pos);
351361
}
362+
$post_types = get_post_types(['public'=> true ]);
352363
echo $display_version;
353364
?></small>
354365
</h2>
@@ -365,6 +376,21 @@ function(){newhook.css('background-color','transparent')});
365376
<tr>
366377
<td><label style='font-weight: bold' for='newhook'><?php _e("WordPress hook type",'hookpress');?>: </label></td><td><input type='radio' id='action' class='newtype' name='newtype' checked='checked'> <?php _e("action","hookpress");?></input> <input type='radio' id='filter' class='newtype' name='newtype'> <?php _e("filter","hookpress");?></input></td>
367378
</tr>
379+
<tr>
380+
<td><label style='font-weight: bold' for='edithook'><?php _e("Post type",'hookpress');?>: </label></td>
381+
<td>
382+
<select name="post_type" id="post_type" multiple>
383+
<?php
384+
sort($post_types);
385+
foreach ($post_types as $post_type) {
386+
if($desc['post_type']){
387+
$selected = in_array($post_type,$desc['post_type'])?'selected="true"':'';
388+
}
389+
$post_type = esc_html( $post_type );
390+
echo "<option value='$post_type' $selected>$post_type</option>";
391+
}?>
392+
</select>
393+
</td></tr>
368394
<tr>
369395
<td><label style='font-weight: bold' for='newhook' id='action_or_filter'></label></td>
370396
<td><select name='newhook' id='newhook'></select></td>

hookpress/services.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function hookpress_ajax_add_fields() {
4646
'type' => sanitize_text_field($_POST['type']),
4747
'hook' => sanitize_text_field($_POST['hook']),
4848
'enabled' => sanitize_text_field($_POST['enabled']),
49+
'post_type' => explode(',', sanitize_text_field($_POST['post_type'])),
4950
'fields' => explode(',', sanitize_text_field($_POST['fields']))
5051
);
5152
hookpress_update_hook( $id, $edithook );
@@ -57,6 +58,7 @@ function hookpress_ajax_add_fields() {
5758
'type' => sanitize_text_field($_POST['type']),
5859
'hook' => sanitize_text_field($_POST['hook']),
5960
'fields' => explode(',', sanitize_text_field($_POST['fields'])),
61+
'post_type' => explode(',', sanitize_text_field($_POST['post_type'])),
6062
'enabled' => true
6163
);
6264
$id = hookpress_add_hook($newhook);

0 commit comments

Comments
 (0)