-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
225 lines (199 loc) · 6.7 KB
/
index.php
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/**
* Plugin Name: Merit SP Batching
* Plugin URI: http://boomtownig.com
* Description: Batch vetted Service Partners to an SFTP server
* Version: 0.1
* Author: BoomtownIG
* Author URI: http://www.boomtownig.com
* License: MIT
*/
defined('ABSPATH') or die();
register_deactivation_hook( __FILE__, 'sp_deactivate' );
function sp_deactivate() {
unregister_setting('general','sp_host', 'esc_attr');
unregister_setting('general','sp_port', 'esc_attr');
unregister_setting('general','sp_username', 'esc_attr');
unregister_setting('general','sp_password', 'esc_attr');
unregister_setting('general','sp_dfilepath', 'esc_attr');
}
/*****************************************************************************************************
Add a options for sp_batching info
*****************************************************************************************************/
add_action('admin_init', 'sp_settings');
function sp_settings() {
add_settings_section(
'sp_settings_section',
'Service Partner Batching Settings',
'sp_section_options_callback',
'general'
);
add_settings_field(
'sp_host',
'Hostname',
'sp_textbox_callback',
'general',
'sp_settings_section',
array(
'sp_host',
)
);
add_settings_field(
'sp_port',
'Port',
'sp_number_callback',
'general',
'sp_settings_section',
array(
'sp_port',
)
);
add_settings_field(
'sp_username',
'Username',
'sp_textbox_callback',
'general',
'sp_settings_section',
array(
'sp_username',
)
);
add_settings_field(
'sp_password',
'Password',
'sp_password_callback',
'general',
'sp_settings_section',
array(
'sp_password',
)
);
add_settings_field(
'sp_dfilepath',
'Destination File Path',
'sp_textbox_callback',
'general',
'sp_settings_section',
array(
'sp_dfilepath',
)
);
register_setting('general','sp_host', 'esc_attr');
register_setting('general','sp_port', 'esc_attr');
register_setting('general','sp_username', 'esc_attr');
register_setting('general','sp_password', 'esc_attr');
register_setting('general','sp_dfilepath', 'esc_attr');
}
function sp_section_options_callback() { // Section Callback
echo '<p>This information is used to upload vetted Service Partners</p>';
}
function sp_password_callback($args) { // Textbox Callback
$option = get_option($args[0]);
echo '<input type="password" id="'. $args[0] .'" name="'. $args[0] .'" value="' . $option . '" class="regular-text" />';
}
function sp_number_callback($args) { // Number Callback
$option = get_option($args[0]);
echo '<input type="number" id="'. $args[0] .'" name="'. $args[0] .'" value="' . $option . '" class="regular-text" />';
}
function sp_textbox_callback($args) { // Password Callback
$option = get_option($args[0]);
echo '<input type="text" id="'. $args[0] .'" name="'. $args[0] .'" value="' . $option . '" class="regular-text" />';
}
/*****************************************************************************************************
Add a checkbox to declare whether or not the Service Partner has been vetted
*****************************************************************************************************/
add_action('post_submitbox_start', 'sp_add_vetted_checkbox');
function sp_add_vetted_checkbox() {
global $post;
if ($post->post_type !== 'mss_service_partner') {
return;
}
?><div><label><input type="checkbox" name="sp_vetted" value="true" <?=get_post_meta($post->ID, 'sp_vetted', true) ? 'checked' : ''?> /> Vetted</label></div><?php
}
/*****************************************************************************************************
Handle the checkbox declaring whether or not the Service Partner has been vetted
*****************************************************************************************************/
add_action('save_post_mss_service_partner', 'sp_save_vetted_info');
function sp_save_vetted_info($post_id) {
$sp_vetted = false;
if (isset($_REQUEST['sp_vetted'])) {
$sp_vetted = true;
}
update_post_meta($post_id, 'sp_vetted', $sp_vetted);
}
/*****************************************************************************************************
Only show Service Partners which have not yet been vetted
*****************************************************************************************************/
add_filter('posts_where' , 'sp_table_filtering');
function sp_table_filtering($where) {
global $wpdb;
global $typenow;
if (is_admin()) :
if ($typenow == 'mss_service_partner'):
$where .= " AND ID NOT IN (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key='sp_vetted' AND meta_value=1)";
endif;
endif;
return $where;
}
/*****************************************************************
add the button to batch to vetted Service Partners
*****************************************************************/
add_filter( 'views_edit-mss_service_partner', 'sp_add_batch_button' );
function sp_add_batch_button($views) {
$views['batch-button'] = '<button id="batch-vetted-records" type="button" title="Batch Vetted Records" style="margin:5px">Batch Vetted Records</button>';
return $views;
}
/*****************************************************************
move the batch button and hook it to an AJAX endpoint
*****************************************************************/
add_action( 'admin_head-edit.php', 'sp_move_batch_button' );
function sp_move_batch_button() {
global $current_screen;
// Not our post type, exit earlier
if ('mss_service_partner' != $current_screen->post_type)
return;
?>
<script type="text/javascript">
jQuery(document).ready( function($) {
$('span.displaying-num').parents('.no-pages').show();
$('#batch-vetted-records')
.prependTo('span.displaying-num')
.click(function(evt) {
$.ajax({
url: '<?=admin_url('admin-ajax.php');?>',
type: 'POST',
data: {
'action': 'batch_service_partners',
'nonceBatchServicePartners': '<?=wp_create_nonce('nonce-batch-service-partners');?>',
},
})
.always(function(data) {
alert((data.status ? 'Success' : 'Error') + ': ' + data.message);
})
;
evt.preventDefault();
return false;
})
;
});
</script>
<?php
}
/*****************************************************************
register AJAX listener for batch endpoint
*****************************************************************/
add_action('wp_ajax_batch_service_partners', 'sp_batch_service_partners');
function sp_batch_service_partners() {
header('Content-Type: application/json');
if (!wp_verify_nonce($_POST['nonceBatchServicePartners'], 'nonce-batch-service-partners')) {
echo json_encode(
array(
'status' => 0,
'message' => 'Invalid Nonce!',
)
);
} else {
include '_ajax.batch_service_partners.php';
}
exit;
}