forked from OpenPublicMedia/nprapi-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.php
373 lines (307 loc) · 18.3 KB
/
settings.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
/**
* NPR API Settings Page and related control methods
*
* Also includes the cron jobs.
*/
require_once( NPRSTORY_PLUGIN_DIR . 'settings_ui.php' );
/**
* add the options page
*
* @see nprstory_publish_meta_box_prompt
*/
function nprstory_add_options_page() {
add_options_page( 'NPR API', 'NPR API', 'manage_options', 'ds_npr_api', 'nprstory_api_options_page' );
}
add_action( 'admin_menu', 'nprstory_add_options_page' );
function nprstory_add_query_page() {
$num = get_option( 'ds_npr_num' );
if ( empty( $num ) ) {
$num = 1;
}
$k = $num;
$opt = get_option( 'ds_npr_query_' . $k );
while ( $k < NPR_MAX_QUERIES ) {
delete_option( 'ds_npr_query_' . $k );
delete_option( 'ds_npr_query_category_' . $k );
delete_option( 'ds_npr_query_tags_' . $k );
delete_option( 'ds_npr_query_publish_' . $k );
delete_option( 'ds_npr_query_profileTypeID_' . $k );
$k++;
$opt = get_option( 'ds_npr_query_' . $k );
}
//make sure we remove any queries we didn't want to use
if ( !empty( $num ) && $num < NPR_MAX_QUERIES ) {
$k = $num;
$opt = get_option( 'ds_npr_query_' . $k );
while ( $k < NPR_MAX_QUERIES ) {
delete_option( 'ds_npr_query_' . $k );
delete_option( 'ds_npr_query_category_' . $k );
delete_option( 'ds_npr_query_tags_' . $k );
delete_option( 'ds_npr_query_publish_' . $k );
delete_option( 'ds_npr_query_profileTypeID_' . $k );
$k++;
}
}
add_options_page( 'Auto Fetch from the NPR API settings',
'NPR API Get Multi', 'manage_options',
'ds_npr_api_get_multi_settings',
'nprstory_api_get_multi_options_page' );
}
add_action( 'admin_menu', 'nprstory_add_query_page' );
function nprstory_settings_init() {
add_settings_section( 'ds_npr_api_settings', 'NPR API settings', 'nprstory_api_settings_callback', 'ds_npr_api' );
add_settings_field( 'ds_npr_api_key', 'API KEY', 'nprstory_api_key_callback', 'ds_npr_api', 'ds_npr_api_settings' );
register_setting( 'ds_npr_api', 'ds_npr_api_key', 'nprstory_validation_callback_api_key' );
add_settings_field( 'ds_npr_api_pull_url', 'Pull URL', 'nprstory_api_pull_url_callback', 'ds_npr_api', 'ds_npr_api_settings' );
register_setting( 'ds_npr_api', 'ds_npr_api_pull_url', 'nprstory_validation_callback_pull_url' );
add_settings_field( 'ds_npr_api_push_url', 'Push URL', 'nprstory_api_push_url_callback', 'ds_npr_api', 'ds_npr_api_settings' );
register_setting( 'ds_npr_api', 'ds_npr_api_push_url', 'nprstory_validation_callback_push_url' );
add_settings_field( 'ds_npr_api_org_id', 'Org ID', 'nprstory_api_org_id_callback', 'ds_npr_api', 'ds_npr_api_settings' );
register_setting( 'ds_npr_api', 'ds_npr_api_org_id', 'nprstory_validation_callback_api_key' );
add_settings_section( 'ds_npr_api_get_multi_settings', 'NPR API multiple get settings', 'nprstory_api_get_multi_settings_callback', 'ds_npr_api_get_multi_settings' );
add_settings_field( 'ds_npr_num', 'Number of things to get', 'nprstory_api_num_multi_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings' );
add_settings_field( 'ds_npr_num', 'Number of things to get', 'nprstory_api_num_multi_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings' );
register_setting( 'ds_npr_api_get_multi_settings', 'ds_npr_num', 'intval' );
/**
* Create a number of forms based off of the number of queries that are registered
*/
$num = get_option( 'ds_npr_num' );
if ( empty( $num ) ) {
$num = 5;
}
$optionType = get_option( 'ds_npr_pull_post_type' );
for ( $i = 0; $i < $num; $i++ ) {
add_settings_field( 'ds_npr_query_' . $i, 'Query String ' . $i, 'nprstory_api_query_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings', $i );
register_setting( 'ds_npr_api_get_multi_settings', 'ds_npr_query_' . $i , 'nprstory_validation_callback_url');
// Add ProfileTypeIDs
add_settings_field( 'ds_npr_query_profileTypeID_' . $i, 'Add Profile Type IDs for querystring ' . $i, 'ds_npr_api_query_profileTypeID_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings', $i );
register_setting( 'ds_npr_api_get_multi_settings', 'ds_npr_query_profileTypeID_' . $i, 'nprstory_validation_callback_url' );
//ds_npr_query_publish_
add_settings_field( 'ds_npr_query_publish_' . $i, 'Publish Stories ' . $i, 'nprstory_api_query_publish_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings', $i );
register_setting( 'ds_npr_api_get_multi_settings', 'ds_npr_query_publish_' . $i , 'nprstory_validation_callback_select');
if ( $optionType == "post" ) {
// Select Category
add_settings_field( 'ds_npr_query_category_' . $i, 'Select a default Category if using Post', 'nprstory_api_select_category_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings', $i );
register_setting( 'ds_npr_api_get_multi_settings', 'ds_npr_query_category_' . $i );
}
// Add tags
add_settings_field( 'ds_npr_query_tags_' . $i, 'Add Tags ' . $i, 'ds_npr_api_query_tags_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings', $i );
register_setting( 'ds_npr_api_get_multi_settings', 'ds_npr_query_tags_' . $i );
}
add_settings_field( 'dp_npr_query_run_multi', 'Run the queries on saving changes', 'nprstory_query_run_multi_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings' );
register_setting( 'ds_npr_api_get_multi_settings', 'dp_npr_query_run_multi' , 'nprstory_validation_callback_checkbox');
add_settings_field( 'dp_npr_query_multi_cron_interval', 'Interval to run Get Multi cron', 'nprstory_query_multi_cron_interval_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings' );
register_setting( 'ds_npr_api_get_multi_settings', 'dp_npr_query_multi_cron_interval', 'intval' );
add_settings_field( 'dp_npr_query_use_layout', 'Use rich layout on pulled posts if available', 'nprstory_query_use_layout_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings' );
register_setting( 'ds_npr_api_get_multi_settings', 'dp_npr_query_use_layout' , 'nprstory_validation_callback_checkbox');
add_settings_field( 'dp_npr_query_use_featured', 'Theme uses Featured Image, so remove lead image from rich layout on pulled posts if available', 'nprstory_query_use_featured_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings' );
register_setting( 'ds_npr_api_get_multi_settings', 'dp_npr_query_use_featured' , 'nprstory_validation_callback_checkbox');
add_settings_field( 'ds_npr_pull_post_type', 'NPR Pull Post Type', 'nprstory_pull_post_type_callback', 'ds_npr_api', 'ds_npr_api_settings' );
register_setting( 'ds_npr_api', 'ds_npr_pull_post_type', 'nprstory_validation_callback_select' );
add_settings_field( 'ds_npr_push_post_type', 'NPR Push Post Type', 'nprstory_push_post_type_callback', 'ds_npr_api', 'ds_npr_api_settings' );
register_setting( 'ds_npr_api', 'ds_npr_push_post_type' , 'nprstory_validation_callback_select');
add_settings_field( 'ds_npr_story_default_permission', 'NPR Permissions', 'nprstory_push_story_permissions_callback', 'ds_npr_api', 'ds_npr_api_settings' );
register_setting( 'ds_npr_api', 'ds_npr_story_default_permission' );
}
add_action( 'admin_init', 'nprstory_settings_init' );
function nprstory_api_settings_callback() { }
function nprstory_add_cron_interval( $schedules ) {
$ds_interval = get_option( 'dp_npr_query_multi_cron_interval' );
//if for some reason we don't get a number in the option, use 60 minutes as the default.
if ( !is_numeric( $ds_interval ) || $ds_interval < 1 ) {
$ds_interval = 60;
update_option( 'dp_npr_query_multi_cron_interval', 60 );
}
$new_interval = $ds_interval * 60;
$schedules['ds_interval'] = [
'interval' => $new_interval,
'display' => __( 'DS Cron, run Once every ' . $ds_interval . ' minutes' )
];
return $schedules;
}
add_filter( 'cron_schedules', 'nprstory_add_cron_interval' );
function nprstory_api_get_multi_settings_callback() {
$run_multi = get_option( 'dp_npr_query_run_multi' );
if ( $run_multi ) {
DS_NPR_API::nprstory_cron_pull();
}
//change the cron timer
if ( wp_next_scheduled( 'npr_ds_hourly_cron' ) ) {
wp_clear_scheduled_hook( 'npr_ds_hourly_cron' );
}
nprstory_error_log( 'NPR Story API plugin: updating the npr_ds_hourly_cron event timer' );
wp_schedule_event( time(), 'ds_interval', 'npr_ds_hourly_cron');
}
function nprstory_query_run_multi_callback() {
$run_multi = get_option( 'dp_npr_query_run_multi' );
$check_box_string = "<input id='dp_npr_query_run_multi' name='dp_npr_query_run_multi' type='checkbox' value='true' ";
if ( $run_multi ) {
$check_box_string .= ' checked="checked" ';
}
$check_box_string .= "/>";
echo nprstory_esc_html( $check_box_string );
wp_nonce_field( 'nprstory_nonce_ds_npr_query_run_multi', 'nprstory_nonce_ds_npr_query_run_multi_name', true, true );
}
function nprstory_query_use_layout_callback() {
$use_layout = get_option( 'dp_npr_query_use_layout' );
$check_box_string = '<input id="dp_npr_query_use_layout" name="dp_npr_query_use_layout" type="checkbox" value="true"';
if ( $use_layout ) {
$check_box_string .= ' checked="checked" ';
}
$check_box_string .= "/>";
echo nprstory_esc_html( $check_box_string . "<p>If 'layout' is available in the NPR Story API output for your key, checking this box will import posts with more complex HTML to render any images, YouTube videos, Tweets, iframes, or JavaScript-based widgets within the post in the order they appeared on the NPR website. Only the 'primary' image for the story will be sideloaded into the Media Library, all other images will be linked from NPR.</p><p>If 'layout' is not available in your API results and you want to use this feature, open a ticket with NPR requesting 'layout permissions' for your Story API Key, and include the key in the ticket request.</p><p><em><strong>CAUTION:</strong> Checking this box will disables the normal 'wp_kses' filtering for imported posts that prevents any JavaScript from being included in the post. There is still some sanitization happening, but we assume that NPR Story API posts will not have malicious scripts.</em></p>" );
wp_nonce_field( 'nprstory_nonce_ds_npr_query_use_layout', 'nprstory_nonce_ds_npr_query_use_layout_name', true, true );
}
function nprstory_query_use_featured_callback() {
$use_featured = get_option( 'dp_npr_query_use_featured' );
$check_box_string = '<input id="dp_npr_query_use_feature" name="dp_npr_query_use_featured" type="checkbox" value="true"';
if ( $use_featured ) {
$check_box_string .= ' checked="checked" ';
}
$check_box_string .= "/>";
echo nprstory_esc_html( $check_box_string . "<p>If your theme uses the featured image, checking this box will remove the lead image from imported posts with more complex HTML to render</p>" );
wp_nonce_field( 'nprstory_nonce_ds_npr_query_use_feature', 'nprstory_nonce_ds_npr_query_use_feature_name', true, true );
}
function nprstory_query_multi_cron_interval_callback() {
$option = get_option( 'dp_npr_query_multi_cron_interval' );
echo nprstory_esc_html( "<input type='number' value='$option' name='dp_npr_query_multi_cron_interval' id='dp_npr_query_multi_cron_interval' /> <p> How often, in minutes, should the Get Multi function run? (default = 60)" );
wp_nonce_field( 'nprstory_nonce_ds_npr_query_multi_cron_interval', 'nprstory_nonce_ds_npr_query_multi_cron_interval_name', true, true );
}
function nprstory_api_query_publish_callback( $i ) {
$selected = get_option( 'ds_npr_query_publish_' . $i );
echo nprstory_esc_html( "<div>Publish or Draft the returns from Query " . $i . "? <select id=" . 'ds_npr_query_publish_' . $i . " name=" . 'ds_npr_query_publish_' . $i . ">" );
// echo '<option value=""> — Select — </option>';
$keys = [ "Publish", "Draft" ];
foreach ( $keys as $key ) {
$option_string = "\n<option ";
if ($key == $selected) {
$option_string .= " selected ";
}
$option_string .= "value='" . esc_attr( $key ) . "'>" . esc_html( $key ) . " </option>";
echo nprstory_esc_html( $option_string );
}
$option_string .= wp_nonce_field( 'nprstory_nonce_ds_npr_query_publish_' . $i, 'nprstory_nonce_ds_npr_query_publish_' . $i . '_name', true, false );
echo "</select> </div>";
}
function nprstory_api_select_category_callback( $i ) {
$selected = get_option( 'ds_npr_query_category_' . $i );
settype( $selected, "integer" );
$args = [
'show_option_none' => __( 'Select category', '' ),
'name' => 'ds_npr_query_category_' . $i,
'hierarchical' => true,
'show_count' => 0,
'orderby' => 'name',
'echo' => 0,
'selected' => $selected,
'hide_empty' => 0,
'multiple' => true
];
$select = wp_dropdown_categories( $args );
echo nprstory_esc_html( $select );
}
function nprstory_api_query_callback( $i ) {
$option = get_option( 'ds_npr_query_' . $i );
$name = 'ds_npr_query_' . $i;
echo nprstory_esc_html( "<input type='text' value='$option' name='$name' style='width: 300px;' />" );
wp_nonce_field( 'nprstory_nonce_ds_npr_query_' . $i, 'nprstory_nonce_ds_npr_query_' . $i . '_name', true, true );
}
function ds_npr_api_query_tags_callback( $i ) {
$name = 'ds_npr_query_tags_' . $i;
$option = get_option( $name );
echo nprstory_esc_html( "<input type='text' value='$option' name='$name' style='width: 300px;' /> <p> Add tag(s) to each story pulled from NPR (comma separated).</p>" );
wp_nonce_field( 'nprstory_nonce_ds_npr_tags_' . $i, 'nprstory_nonce_ds_npr_tags_' . $i . '_name', true, true );
echo "<p><hr></p>";
}
// profile type id
function ds_npr_api_query_profileTypeID_callback( $i ) {
$name = 'ds_npr_query_profileTypeID_' . $i;
$option = get_option( $name, '1' );
echo nprstory_esc_html( "<input type='text' value='$option' name='$name' style='width: 300px;' /> <p>***Optional Profile ID Type(s) to each story pulled from NPR (comma separated). Default is 1 (story), 15 (podcast episodes) also available.</p>" );
wp_nonce_field( 'nprstory_nonce_ds_npr_profileidtype_' . $i, 'nprstory_nonce_ds_npr_profileidtype_' . $i . '_name', true, true );
echo "<p><hr></p>";
}
function nprstory_api_num_multi_callback() {
$option = get_option( 'ds_npr_num' );
echo nprstory_esc_html( "<input type='number' value='$option' name='ds_npr_num' /> <p> Increase the number of queries by changing the number in the field above." );
wp_nonce_field( 'nprstory_nonce_ds_npr_num', 'nprstory_nonce_ds_npr_num_name', true, true );
}
function nprstory_api_key_callback() {
$option = get_option( 'ds_npr_api_key' );
echo nprstory_esc_html( "<input type='text' value='$option' name='ds_npr_api_key' style='width: 300px;' />" );
wp_nonce_field( 'nprstory_nonce_ds_npr_api_key', 'nprstory_nonce_ds_npr_api_key_name', true, true );
}
function nprstory_api_pull_url_callback() {
$option = get_option( 'ds_npr_api_pull_url' );
echo nprstory_esc_html( "<input type='text' value='$option' name='ds_npr_api_pull_url' style='width: 300px;' />" );
wp_nonce_field( 'nprstory_nonce_ds_npr_api_pull_url', 'nprstory_nonce_ds_npr_api_pull_url_name', true, true );
}
function nprstory_api_push_url_callback() {
$option = get_option( 'ds_npr_api_push_url' );
echo nprstory_esc_html( "<input type='text' value='$option' name='ds_npr_api_push_url' style='width: 300px;' />" );
wp_nonce_field( 'nprstory_nonce_ds_npr_api_push_url', 'nprstory_nonce_ds_npr_api_push_url_name', true, true );
}
function nprstory_api_org_id_callback() {
$option = get_option( 'ds_npr_api_org_id' );
echo nprstory_esc_html( "<input type='text' value='$option' name='ds_npr_api_org_id' style='width: 300px;' />" );
wp_nonce_field( 'nprstory_nonce_ds_npr_api_org_id', 'nprstory_nonce_ds_npr_api_org_id_name', true, true );
}
function nprstory_pull_post_type_callback() {
$post_types = get_post_types();
nprstory_show_post_types_select( 'ds_npr_pull_post_type', $post_types );
}
function nprstory_push_post_type_callback() {
$post_types = get_post_types();
nprstory_show_post_types_select( 'ds_npr_push_post_type', $post_types );
echo nprstory_esc_html( '<div> If you change the Push Post Type setting remember to update the mappings for API Fields at <a href="' . admin_url( 'options-general.php?page=ds_npr_api_push_mapping' ) . '">NPR API Field Mapping</a> page.</div>' );
}
function nprstory_push_story_permissions_callback() {
$permissions_groups = nprstory_get_permission_groups();
if ( !empty( $permissions_groups ) ) {
nprstory_show_perms_select( 'ds_npr_story_default_permission', $permissions_groups );
echo '<div>This is where you select the default permissions group to use when pushing stories to the NPR API.</div>';
} else {
echo '<div>You have no Permission Groups defined with the NPR API.</div>';
}
}
/**
* create the select widget where the Id is the value in the array
* @param $field_name
* @param $keys - an array like (1=>'Value1', 2=>'Value2', 3=>'Value3');
*/
function nprstory_show_post_types_select( $field_name, $keys ) {
$selected = get_option( $field_name );
echo nprstory_esc_html( "<div><select id=" . $field_name . " name=" . $field_name . ">" );
echo '<option value=""> — Select — </option>';
foreach ( $keys as $key ) {
$option_string = "\n<option ";
if ( $key == $selected ) {
$option_string .= " selected ";
}
$option_string .= "value='" . esc_attr( $key ) . "'>" . esc_html( $key ) . " </option>";
echo nprstory_esc_html( $option_string );
}
echo "</select> </div>";
wp_nonce_field( 'nprstory_nonce_' . $field_name, 'nprstory_nonce_' . $field_name . '_name', true, true );
}
/**
* create the select widget where the ID for an element is the index to the array
* @param $field_name
* @param $keys an array like (id1=>'Value1', id2=>'Value2', id3=>'Value3');
*/
function nprstory_show_perms_select( $field_name, $keys ) {
$selected = get_option( $field_name );
echo nprstory_esc_html( "<div><select id=" . $field_name . " name=" . $field_name . ">" );
echo '<option value=""> — Select — </option>';
foreach ( $keys as $id => $key ) {
$option_string = "\n<option ";
if ( $id == $selected ) {
$option_string .= " selected ";
}
$option_string .= "value='" . esc_attr( $id ) . "'>" . esc_html( $key['name'] ) . " </option>";
echo nprstory_esc_html( $option_string );
}
echo "</select> </div>";
wp_nonce_field( 'nprstory_nonce_' . $field_name, 'nprstory_nonce_' . $field_name . '_name', true, true );
}