-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsettings.php
More file actions
1745 lines (1658 loc) · 72.4 KB
/
settings.php
File metadata and controls
1745 lines (1658 loc) · 72.4 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* The class NPR_CDS and related functions for getting stories from the API
* Also includes the settings pages and cron jobs
*/
if ( ! defined( 'ABSPATH' ) ) exit;
require_once( NPR_CDS_PLUGIN_DIR . 'classes/NPR_CDS_WP.php' );
/**
* Push/Pull URLs:
* Production: https://content.api.npr.org/
* Staging: https://stage-content.api.npr.org/
*/
class NPR_CDS {
/**
* Class constructor that hooks up the menu and settings pages.
*/
public function __construct() {
if ( !is_admin() ) {
return;
}
$post_type_option = get_option( 'npr_cds_pull_post_type', 'post' );
// Allow customization of the post type used for the loading screen
$post_type = ( $post_type_option === 'post' ? 'posts' : $post_type_option ) . '_page';
add_action( 'admin_menu', [ $this, 'admin_menu' ] );
add_filter( 'manage_edit-' . $this->get_pull_post_type() . '_columns', [ $this, 'add_new_story_columns' ] );
add_action( 'admin_print_footer_scripts', [ $this, 'bulk_action_update_dropdown' ] );
add_action( 'load-edit.php', [ $this, 'bulk_action_update_action' ] );
add_action( 'manage_posts_custom_column', [ $this, 'update_column_content' ], 10, 2 );
add_action( 'load-' . $post_type . '_get-npr-stories', [ $this, 'load_page_hook' ] );
add_action( 'admin_init', [ $this, 'settings_init' ] );
add_action( 'admin_init', [ $this, 'restore_page_hook' ] );
}
/**
* Register the admin menu for "Get NPR Stories"
*/
public function admin_menu(): void {
$post_type = get_option( 'npr_cds_pull_post_type', 'post' );
if ( ! post_type_exists( $post_type ) ) {
$post_type = 'post';
}
$required_capability = apply_filters( 'npr_cds_get_stories_capability', 'edit_posts' );
add_menu_page(
esc_html__( 'View Stories Uploaded to the CDS', 'npr-content-distribution-service' ),
esc_html__( 'NPR CDS', 'npr-content-distribution-service' ),
$required_capability,
'npr-cds-overview',
[ $this, 'view_uploads' ],
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
'data:image/svg+xml;base64,' . base64_encode( '<svg id="Layer_1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 650.54 215.48"><polygon points="218.17 213.48 218.17 2 2 2 2 213.48 218.17 213.48 218.17 213.48" fill="none" stroke="#231f20" stroke-miterlimit="10" stroke-width="4"/><polygon points="435.43 213.48 435.43 2 218.17 2 218.17 213.48 435.43 213.48 435.43 213.48" fill="none" stroke="#231f20" stroke-miterlimit="10" stroke-width="4"/><polygon points="649.04 213.48 649.04 2 435.43 2 435.43 213.48 649.04 213.48 649.04 213.48" fill="none" stroke="#231f20" stroke-miterlimit="10" stroke-width="3"/><path d="M132.76,165.05v-64.1c.61-7.32-1.32-14.63-5.47-20.7-4.66-4.74-11.21-7.14-17.84-6.51-8.68.46-16.78,4.52-22.35,11.2v80.12h-26.04V53.69h18.71l4.77,10.42c7.07-8.16,17.36-12.28,31.29-12.28,11.65-.59,23.01,3.73,31.33,11.89,7.64,7.94,11.46,18.97,11.46,33.16v68.18h-25.87Z"/><path d="M324.76,167.26c16.95,0,30.34-4.92,40.14-14.76,9.85-9.85,14.71-23.87,14.71-42.1,0-39.06-17.74-58.59-53.21-58.59-8.94-.21-17.6,3.16-24.04,9.37v-7.51h-26.04v140.96h26.04v-32.29c6.99,3.32,14.65,5,22.39,4.9h0ZM318.94,74.04c11.98,0,20.53,2.69,25.74,8.03s7.73,14.58,7.73,27.73c0,12.33-2.6,21.32-7.81,26.99-5.21,5.73-13.8,8.68-25.74,8.68-6,.11-11.84-1.9-16.49-5.69v-58.37c4.3-4.58,10.29-7.2,16.58-7.25v-.13Z"/><path d="M572.74,78.12c-4.52-2.9-9.78-4.41-15.15-4.34-6.29.13-12.18,3.13-15.97,8.16-4.72,5.58-7.21,12.7-6.99,20.01v63.15h-26.04V53.69h26.04v10.68c7.19-8.36,17.8-12.98,28.82-12.54,6.99-.41,13.98,1,20.27,4.08l-10.98,22.22Z"/></svg>' ),
50.887
);
add_submenu_page(
'npr-cds-overview',
'Get NPR Stories',
'Get NPR Stories',
$required_capability,
'get-npr-stories',
[ $this, 'get_stories' ]
);
add_submenu_page(
'npr-cds-overview',
'NPR CDS Main Settings',
'Main Settings',
$required_capability,
'npr-cds-settings',
[ $this, 'general_settings' ]
);
add_submenu_page(
'npr-cds-overview',
'NPR CDS Get Multi Settings',
'Get Multi Settings',
$required_capability,
'npr-cds-get-multi',
[ $this, 'get_multi' ]
);
add_submenu_page(
'npr-cds-overview',
'NPR CDS Push Mapping',
'Push Mapping',
$required_capability,
'npr-cds-push-mapping',
[ $this, 'push_mapping' ]
);
add_submenu_page(
'edit.php' . ( $post_type !== 'post' ? '?post_type=' . $post_type : '' ),
'Get NPR Stories',
'Get NPR Stories',
$required_capability,
'get-npr-stories',
[ $this, 'get_stories' ]
);
}
public function general_settings(): void { ?>
<style>
h1 {
line-height: 1.25;
}
.form-table td input[type="text"] {
display: inline-block;
max-width: 100%;
width: 66%;
}
@media screen and (max-width: 500px) {
.form-table td input[type="text"] {
width: 100%;
}
}
</style>
<h1>NPR CDS: General Settings</h1>
<form action="<?php echo admin_url( 'options.php' ); ?>" method="post">
<?php settings_fields( 'npr_cds' ); ?>
<?php echo $this->restore_old(); ?>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-2">
<div id="post-body-content">
<div>
<div class="postbox">
<div class="postbox-header"><h2><?php _e('CDS Settings', 'npr-content-distribution-service' ); ?></h2></div>
<div class="inside">
<table class="form-table">
<?php do_settings_fields( 'npr_cds', 'npr_cds_settings' ); ?>
</table>
</div>
</div>
</div>
<div>
<div class="postbox">
<div class="postbox-header"><h2><?php _e('Organization Settings', 'npr-content-distribution-service' ); ?></h2></div>
<div class="inside">
<table class="form-table">
<?php do_settings_fields( 'npr_cds', 'npr_cds_org_settings' ); ?>
</table>
</div>
</div>
</div>
<div>
<div class="postbox">
<div class="postbox-header"><h2><?php _e('Theme Settings', 'npr-content-distribution-service' ); ?></h2></div>
<div class="inside">
<table class="form-table">
<?php do_settings_fields( 'npr_cds', 'npr_cds_theme_settings' ); ?>
</table>
</div>
</div>
</div>
<div>
<div class="postbox">
<div class="postbox-header"><h2><?php _e('Image Settings', 'npr-content-distribution-service' ); ?></h2></div>
<div class="inside">
<table class="form-table">
<?php do_settings_fields( 'npr_cds', 'npr_cds_image_settings' ); ?>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<br class="clear" />
<?php submit_button(); ?>
</form><?php
}
public function get_multi(): void { ?>
<style>
h1 {
line-height: 1.25;
}
.form-table td input[type="text"] {
display: inline-block;
max-width: 100%;
width: 66%;
}
.npr-cds-query h4 {
margin: 0;
text-align: right;
}
.npr-cds-query {
display: grid;
grid-template-columns: 10rem auto;
gap: 1rem;
align-items: center;
}
.cds-query-details .form-table tr + tr {
border-top: 1px solid #808080;
}
@media screen and (max-width: 500px) {
.npr-cds-query h4 {
text-align: left;
}
.npr-cds-query {
grid-template-columns: 1fr;
}
.form-table td input[type="text"] {
width: 100%;
}
}
</style>
<h1>NPR CDS: Get Multi Settings</h1>
<p><?php echo __( 'Create an NPR CDS query. Enter your queries into one of the rows below to have stories on that query automatically publish to your site. Please note, you do not need to include your CDS token in the query.', 'npr-content-distribution-service' ); ?></p>
<?php echo $this->restore_old(); ?>
<form action="<?php echo admin_url( 'options.php' ); ?>" method="post">
<?php settings_fields( 'npr_cds_get_multi_settings' ); ?>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-2">
<div id="post-body-content">
<div>
<div class="postbox">
<div class="postbox-header"><h2><?php _e('Query Settings', 'npr-content-distribution-service' ); ?></h2></div>
<div class="inside">
<table class="form-table">
<?php do_settings_fields( 'npr_cds_get_multi_settings', 'npr_cds_get_multi_settings' ); ?>
</table>
</div>
</div>
</div>
<div>
<div class="postbox cds-query-details">
<div class="postbox-header"><h2><?php _e('Query Details', 'npr-content-distribution-service' ); ?></h2></div>
<div class="inside">
<table class="form-table">
<?php do_settings_fields( 'npr_cds_get_multi_settings', 'npr_cds_query_details' ); ?>
</table>
</div>
</div>
</div>
<div>
<div class="postbox">
<div class="postbox-header"><h2><?php _e('Cron Settings', 'npr-content-distribution-service' ); ?></h2></div>
<div class="inside">
<table class="form-table">
<?php do_settings_fields( 'npr_cds_get_multi_settings', 'npr_cds_cron_settings' ); ?>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<br class="clear" />
<?php submit_button(); ?>
</form><?php
}
public function push_mapping(): void { ?>
<style>
h1 {
line-height: 1.25;
}
.form-table td input[type="text"] {
display: inline-block;
max-width: 100%;
width: 66%;
}
@media screen and (max-width: 500px) {
.form-table td input[type="text"] {
width: 100%;
}
}
</style>
<h1>NPR CDS: Push Mapping</h1>
<?php echo $this->restore_old(); ?>
<form action="<?php echo admin_url( 'options.php' ); ?>" method="post">
<?php settings_fields( 'npr_cds_push_mapping' ); ?>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-2">
<div id="post-body-content">
<div>
<div class="postbox">
<div class="postbox-header"><h2><?php _e('CDS Push Mapping', 'npr-content-distribution-service' ); ?></h2></div>
<div class="inside">
<table class="form-table">
<?php do_settings_fields( 'npr_cds_push_mapping', 'npr_cds_push_settings' ); ?>
</table>
</div>
</div>
</div>
<div>
</div>
</div>
<br class="clear" />
<?php submit_button(); ?>
</form><?php
}
public function settings_init(): void {
if ( is_plugin_active( 'npr-story-api/ds-npr-api.php' ) ) {
deactivate_plugins( 'npr-story-api/ds-npr-api.php' );
error_log( 'NPR Story API plugin deactivated by NPR CDS plugin. The NPR Story API plugin has been deprecated and cannot coexist with the newer CDS plugin.' );
}
/**
* CDS: General Settings
*/
add_settings_section( 'npr_cds_settings', 'General Settings', [ $this, 'settings_callback' ], 'npr_cds' );
add_settings_field( 'npr_cds_token', 'CDS Token', [ $this, 'token_callback' ], 'npr_cds', 'npr_cds_settings' );
register_setting( 'npr_cds', 'npr_cds_token' );
add_settings_field( 'npr_cds_pull_url', 'Pull URL', [ $this, 'pull_url_callback' ], 'npr_cds', 'npr_cds_settings' );
register_setting( 'npr_cds', 'npr_cds_pull_url', [ 'sanitize_callback' => [ $this, 'validation_callback_pull_url' ] ] );
add_settings_field( 'npr_cds_push_url', 'Push URL', [ $this, 'push_url_callback' ], 'npr_cds', 'npr_cds_settings' );
register_setting( 'npr_cds', 'npr_cds_push_url', [ 'sanitize_callback' => [ $this, 'validation_callback_push_url' ] ] );
/**
* CDS: Org Settings
*/
add_settings_section( 'npr_cds_org_settings', 'Organization Settings', [ $this, 'settings_callback' ], 'npr_cds' );
add_settings_field( 'npr_cds_org_id', 'Service ID', [ $this, 'org_id_callback' ], 'npr_cds', 'npr_cds_org_settings' );
register_setting( 'npr_cds', 'npr_cds_org_id', [ 'sanitize_callback' => [ $this, 'validation_callback_org_id' ] ] );
add_settings_field( 'npr_cds_prefix', 'Document Prefix', [ $this, 'prefix_callback' ], 'npr_cds', 'npr_cds_org_settings' );
register_setting( 'npr_cds', 'npr_cds_prefix', [ 'sanitize_callback' => [ $this, 'validation_callback_prefix' ] ] );
/**
* CDS: Theme Settings
*/
add_settings_section( 'npr_cds_theme_settings', 'Theme Settings', [ $this, 'settings_callback' ], 'npr_cds' );
add_settings_field( 'npr_cds_query_use_featured', 'Theme uses Featured Image', [ $this, 'query_use_featured_callback' ], 'npr_cds', 'npr_cds_theme_settings' );
register_setting( 'npr_cds', 'npr_cds_query_use_featured', [ 'sanitize_callback' => [ $this, 'validation_callback_checkbox' ] ] );
add_settings_field( 'npr_cds_pull_post_type', 'NPR Default Pull Post Type', [ $this, 'pull_post_type_callback' ], 'npr_cds', 'npr_cds_theme_settings' );
register_setting( 'npr_cds', 'npr_cds_pull_post_type' );
add_settings_field( 'npr_cds_push_post_type', 'NPR Push Post Type', [ $this, 'push_post_type_callback' ], 'npr_cds', 'npr_cds_theme_settings' );
register_setting( 'npr_cds', 'npr_cds_push_post_type' );
add_settings_field( 'npr_cds_push_default', 'Push to CDS Default', [ $this, 'push_default_callback' ], 'npr_cds', 'npr_cds_theme_settings' );
register_setting( 'npr_cds', 'npr_cds_push_default' );
add_settings_field( 'npr_cds_push_one_homepage_default', 'Include for NPR One/ Homepage Default', [ $this, 'push_one_homepage_default_callback' ], 'npr_cds', 'npr_cds_theme_settings' );
register_setting( 'npr_cds', 'npr_cds_push_one_homepage_default' );
add_settings_field( 'npr_cds_import_tags', 'Import Tags from CDS?', [ $this, 'import_tags_callback' ], 'npr_cds', 'npr_cds_theme_settings' );
register_setting( 'npr_cds', 'npr_cds_import_tags' );
add_settings_field( 'npr_cds_display_attribution', 'Append Article Attribution?', [ $this, 'display_attribution_callback' ], 'npr_cds', 'npr_cds_theme_settings' );
register_setting( 'npr_cds', 'npr_cds_display_attribution' );
add_settings_field( 'npr_cds_skip_promo_cards', 'Skip NPR promos', [ $this, 'skip_promo_cards_callback' ], 'npr_cds', 'npr_cds_theme_settings' );
register_setting( 'npr_cds', 'npr_cds_skip_promo_cards', [ 'sanitize_callback' => [ $this, 'validation_callback_checkbox'] ] );
/**
* CDS: Image Settings
*/
add_settings_section( 'npr_cds_image_settings', 'Image Settings', [ $this, 'settings_callback' ], 'npr_cds' );
add_settings_field( 'npr_cds_image_width', 'Max Image Width', [ $this, 'image_width_callback' ], 'npr_cds', 'npr_cds_image_settings' );
register_setting( 'npr_cds', 'npr_cds_image_width' );
add_settings_field( 'npr_cds_image_quality', 'Image Quality', [ $this, 'image_quality_callback' ], 'npr_cds', 'npr_cds_image_settings' );
register_setting( 'npr_cds', 'npr_cds_image_quality' );
add_settings_field( 'npr_cds_image_format', 'Image Format', [ $this, 'image_format_callback' ], 'npr_cds', 'npr_cds_image_settings' );
register_setting( 'npr_cds', 'npr_cds_image_format' );
/**
* CDS: Get Multi Settings
*/
add_settings_section( 'npr_cds_get_multi_settings', 'Multiple Get Settings', [ $this, 'get_multi_settings_callback' ], 'npr_cds_get_multi_settings' );
add_settings_field( 'npr_cds_num', 'Number of things to get', [ $this, 'num_multi_callback' ], 'npr_cds_get_multi_settings', 'npr_cds_get_multi_settings' );
register_setting( 'npr_cds_get_multi_settings', 'npr_cds_num', [ 'type' => 'integer', 'sanitize_callback' => [ $this, 'validation_num' ] ] );
/**
* CDS: Query Details
*/
add_settings_section( 'npr_cds_query_details', 'Query Details', [ $this, 'query_details_callback' ], 'npr_cds_get_multi_settings' );
$num = get_option( 'npr_cds_num', 1 );
for ( $i = 0; $i < $num; $i++ ) {
add_settings_field( 'npr_cds_query_' . $i, 'Query ' . $i, [ $this, 'query_callback' ], 'npr_cds_get_multi_settings', 'npr_cds_query_details', $i );
register_setting( 'npr_cds_get_multi_settings', 'npr_cds_query_' . $i, [ 'type' => 'array', 'default' => [ 'filters' => '', 'sorting' => '', 'publish' => '', 'category' => '', 'tags' => '', 'import_tags' => '1' ] ] );
}
/**
* CDS: Cron Settings
*/
add_settings_section( 'npr_cds_cron_settings', 'Cron Settings', [ $this, 'settings_callback' ], 'npr_cds_get_multi_settings' );
add_settings_field( 'npr_cds_query_run_multi', 'Run the queries on saving changes', [ $this, 'query_run_multi_callback' ], 'npr_cds_get_multi_settings', 'npr_cds_cron_settings' );
register_setting( 'npr_cds_get_multi_settings', 'npr_cds_query_run_multi', [ 'sanitize_callback' => [ $this, 'validation_callback_checkbox' ] ] );
add_settings_field( 'npr_cds_query_multi_cron_interval', 'Interval to run Get Multi cron', [ $this, 'query_multi_cron_interval_callback' ], 'npr_cds_get_multi_settings', 'npr_cds_cron_settings' );
register_setting( 'npr_cds_get_multi_settings', 'npr_cds_query_multi_cron_interval', [ 'type' => 'integer' ] );
/**
* CDS: Push Settings
*/
add_settings_section( 'npr_cds_push_settings', 'Metadata Settings', [ $this, 'push_settings_callback' ], 'npr_cds_push_mapping' );
add_settings_field( 'npr_cds_push_use_custom_map', 'Use Custom Settings', [ $this, 'use_custom_mapping_callback' ], 'npr_cds_push_mapping', 'npr_cds_push_settings' );
register_setting( 'npr_cds_push_mapping', 'npr_cds_push_use_custom_map', [ 'sanitize_callback' => [ $this, 'validation_callback_checkbox' ] ] );
add_settings_field( 'npr_cds_mapping_title', 'Story Title', [ $this, 'mapping_title_callback' ], 'npr_cds_push_mapping', 'npr_cds_push_settings' );
register_setting( 'npr_cds_push_mapping', 'npr_cds_mapping_title' );
add_settings_field( 'npr_cds_mapping_body', 'Story Body', [ $this, 'mapping_body_callback' ], 'npr_cds_push_mapping', 'npr_cds_push_settings' );
register_setting( 'npr_cds_push_mapping', 'npr_cds_mapping_body' );
add_settings_field( 'npr_cds_mapping_byline', 'Story Byline', [ $this, 'mapping_byline_callback' ], 'npr_cds_push_mapping', 'npr_cds_push_settings' );
register_setting( 'npr_cds_push_mapping', 'npr_cds_mapping_byline' );
add_settings_field( 'npr_cds_mapping_media_credit', 'Media Credit Field', [ $this, 'mapping_media_credit_callback' ], 'npr_cds_push_mapping', 'npr_cds_push_settings' );
register_setting( 'npr_cds_push_mapping', 'npr_cds_mapping_media_credit' );
add_settings_field( 'npr_cds_mapping_media_agency', 'Media Agency Field', [ $this, 'mapping_media_agency_callback' ], 'npr_cds_push_mapping', 'npr_cds_push_settings' );
register_setting( 'npr_cds_push_mapping', 'npr_cds_mapping_media_agency' );
}
/**
* Settings group callback functions
*/
public function settings_callback() { }
public function get_multi_settings_callback() { }
public function push_settings_callback(): void { ?>
<p>Use this page to map your custom WordPress Meta fields to fields sent to the NPR CDS, and vice versa. Clicking the <strong>Use Custom Settings</strong> checkbox will enable these mappings. If you wish to use the default mapping for a field, select — default — and we will use the obvious WordPress field.</p>
<p>Select for the Meta fields for the <strong><?php echo esc_html( npr_cds_get_push_post_type() ); ?></strong> post
type.</p> <?php
}
/**
* NPR General Settings Group Callbacks
*/
public function token_callback(): void {
$attr = '';
if ( defined( 'NPR_CDS_TOKEN' ) ) {
$option = 'This field managed in wp-config.php';
$attr = 'disabled ';
} else {
$option = get_option( 'npr_cds_token' );
}
echo npr_cds_esc_html( '<p><input type="text" value="' . $option . '" name="npr_cds_token" ' . $attr . '/></p><p><em>This is a bearer token provided by NPR. If you do not already have one, you can request one through <a href="https://studio.npr.org/">NPR Studio</a>.<br /><br />You can also manage your token by adding the following to wp-config.php: <code>define( \'NPR_CDS_TOKEN\', \'{ TOKEN STRING }\' );</code></em></p>' );
}
public function pull_url_callback(): void {
$option = get_option( 'npr_cds_pull_url' );
$output = '<p><label><input type="radio" name="npr_cds_pull_url" value="https://stage-content.api.npr.org"' . ( $option == 'https://stage-content.api.npr.org' ? ' checked="checked"' : '' ) . ' /> Staging</label></p>';
$output .= '<p><label><input type="radio" name="npr_cds_pull_url" value="https://content.api.npr.org"' . ( $option == 'https://content.api.npr.org' ? ' checked="checked"' : '' ) . ' /> Production</label></p>';
$output .= '<p><label><input type="radio" name="npr_cds_pull_url" value="other"' . ( $option !== 'https://stage-content.api.npr.org' && $option !== 'https://content.api.npr.org' ? ' checked="checked"' : '' ) . ' /> Other</label> <input type="text" name="npr_cds_pull_url_other" id="npr_cds_pull_url_other" value="' . ( $option !== 'https://stage-content.api.npr.org' && $option !== 'https://content.api.npr.org' ? $option : '' ) . '" placeholder="Type other URL here" /></p>';
echo npr_cds_esc_html( $output );
}
public function push_url_callback(): void {
$option = get_option( 'npr_cds_push_url' );
$output = '<p><label><input type="radio" name="npr_cds_push_url" value="https://stage-content.api.npr.org"' . ( $option == 'https://stage-content.api.npr.org' ? ' checked="checked"' : '' ) . ' /> Staging</label></p>';
$output .= '<p><label><input type="radio" name="npr_cds_push_url" value="https://content.api.npr.org"' . ( $option == 'https://content.api.npr.org' ? ' checked="checked"' : '' ) . ' /> Production</label></p>';
$output .= '<p><label><input type="radio" name="npr_cds_push_url" value="other"' . ( $option !== 'https://stage-content.api.npr.org' && $option !== 'https://content.api.npr.org' ? ' checked="checked"' : '' ) . ' /> Other</label> <input type="text" name="npr_cds_push_url_other" id="npr_cds_push_url_other" value="' . ( $option !== 'https://stage-content.api.npr.org' && $option !== 'https://content.api.npr.org' ? $option : '' ) . '" placeholder="Type other URL here" /></p>';
echo npr_cds_esc_html( $output );
}
public function org_id_callback(): void {
$option = get_option( 'npr_cds_org_id' );
echo npr_cds_esc_html( '<p><input type="text" value="' . $option . '" name="npr_cds_org_id" /></p><p><em>Enter the service ID provided by NPR. If your stories will be co-owned with another organization, you can include all of the service IDs as a comma-separated list.</em></p>' );
}
public function prefix_callback(): void {
$option = get_option( 'npr_cds_prefix' );
echo npr_cds_esc_html( '<p><input type="text" value="' . $option . '" name="npr_cds_prefix" placeholder="callletters" /></p><p><em>When given write permission to the CDS, NPR will assign a code that will be prefixed on all of your document IDs (e.g. "kuhf-12345").</em></p>' );
}
public function query_use_featured_callback(): void {
$use_featured = get_option( 'npr_cds_query_use_featured' );
$check_box_string = '<input id="npr_cds_query_use_featured" name="npr_cds_query_use_featured" type="checkbox" value="true"' .
( $use_featured ? ' checked="checked"' : '' ) . ' />';
echo npr_cds_esc_html( '<p>' . $check_box_string . " If your theme uses the featured image, checking this box will remove the lead image from imported posts.</p>" );
}
public function pull_post_type_callback(): void {
$post_types = get_post_types();
$this->show_post_types_select( 'npr_cds_pull_post_type', $post_types );
}
public function push_post_type_callback(): void {
$post_types = get_post_types();
$this->show_post_types_select( 'npr_cds_push_post_type', $post_types );
echo npr_cds_esc_html( '<p><em>If you change the Push Post Type setting remember to update the mappings for CDS Fields at <a href="' . admin_url( 'options-general.php?page=npr_cds#npr-fields' ) . '">NPR CDS Field Mapping</a> tab.</em></p>' );
}
public function push_default_callback(): void {
$push_default = get_option( 'npr_cds_push_default', '1' );
$check_box_string = '<select id="npr_cds_push_default" name="npr_cds_push_default"><option value="1"' . ( $push_default === '1' ? ' selected' : '' ) . '>Checked</option>' .
'<option value="0"' . ( $push_default === '0' ? ' selected' : '' ) . '>Not Checked</option>' .
'</select>';
echo npr_cds_esc_html( '<p>' . $check_box_string . '</p><p><em>When creating a new post in your NPR Push Post Type, do you want the "Push to NPR CDS" box to be checked by default or not?</em></p>' );
}
public function push_one_homepage_default_callback(): void {
$push_default = get_option( 'npr_cds_push_one_homepage_default', '0' );
$check_box_string = '<select id="npr_cds_push_one_homepage_default" name="npr_cds_push_one_homepage_default"><option value="0"' . ( $push_default === '0' ? ' selected' : '' ) . '>Not Checked</option>' .
'<option value="1"' . ( $push_default === '1' ? ' selected' : '' ) . '>Checked</option>' .
'</select>';
echo npr_cds_esc_html( '<p>' . $check_box_string . '</p><p><em>When creating a new post in your NPR Push Post Type, do you want the "Include for NPR One and NPR Homepage" box to be checked by default or not?</em></p>' );
}
public function import_tags_callback(): void {
$import_tags_default = get_option( 'npr_cds_import_tags', '1' );
$check_box_string = '<select id="npr_cds_import_tags" name="npr_cds_import_tags"><option value="1"' . ( $import_tags_default === '1' ? ' selected' : '' ) . '>Import</option>' .
'<option value="0"' . ( $import_tags_default === '0' ? ' selected' : '' ) . '>Do Not Import</option>' .
'</select>';
echo npr_cds_esc_html( '<p>' . $check_box_string . '</p><p><em>When importing an article from the NPR CDS, do you want to import all of the article\'s tags into WordPress?</em></p>' );
}
public function display_attribution_callback(): void {
$attribution_default = get_option( 'npr_cds_display_attribution', '0' );
$check_box_string = '<select id="npr_cds_display_attribution" name="npr_cds_display_attribution"><option value="0"' . ( $attribution_default === '0' ? ' selected' : '' ) . '>Do Not Append</option>' .
'<option value="1"' . ( $attribution_default === '1' ? ' selected' : '' ) . '>Append</option>' .
'</select>';
echo npr_cds_esc_html( '<p>' . $check_box_string . '</p><p><em>Do you want to append an attribution message to the bottom of imported articles? (e.g. "Copyright © ' . date( 'Y' ) . ' NPR")</em></p>' );
}
public function skip_promo_cards_callback(): void {
$skip_promos = get_option( 'npr_cds_skip_promo_cards' );
$check_box_string = '<input id="npr_cds_skip_promo_cards" name="npr_cds_skip_promo_cards" type="checkbox" value="true"' . ( $skip_promos ? ' checked="checked"' : '' ) . ' />';
echo npr_cds_esc_html( '<p>' . $check_box_string . " Filter out any NPR promo cards embedded in posts.</p>" );
}
public function image_format_callback(): void {
$this->show_post_types_select( 'npr_cds_image_format', [ 'jpeg', 'png', 'webp' ] );
}
public function image_quality_callback(): void {
$option = get_option( 'npr_cds_image_quality', 75 );
echo npr_cds_esc_html( '<p><input type="number" value="' . $option . '" name="npr_cds_image_quality" min="1" max="100" /></p><p><em>Set the quality level of the images from the NPR CDS (default: 75).</em></p>' );
}
public function image_width_callback(): void {
$option = get_option( 'npr_cds_image_width', 1200 );
echo npr_cds_esc_html( '<p><input type="number" value="' . $option . '" name="npr_cds_image_width" min="500" max="3000" /></p><p><em>Maximum width of images pulled in from the NPR CDS (default: 1200).</em></p>' );
}
/**
* NPR Get Multi Settings Group Callbacks
*/
public function num_multi_callback(): void {
$run_multi = get_option( 'npr_cds_query_run_multi' );
$num = get_option( 'npr_cds_num', 5 );
$enable = false;
for ( $i = 0; $i < $num; $i++ ) {
$option = get_option( 'npr_cds_query_' . $i );
if ( !empty( $option['filters'] ) || !empty( $options['sorting'] ) ) {
$enable = true;
}
}
if ( $run_multi && $enable ) {
update_option( 'npr_cds_query_run_multi', false );
$this->cron_pull();
}
if ( $enable && !wp_next_scheduled( 'npr_cds_hourly_cron') ) {
wp_schedule_event( time(), 'npr_cds_interval', 'npr_cds_hourly_cron' );
}
echo npr_cds_esc_html( '<p><input type="number" value="' . $num . '" min="0" max="' . NPR_MAX_QUERIES . '" name="npr_cds_num" /></p><p><em>Increase the number of queries by changing the number in the field above, to a maximum of 10.</em></p>' );
}
public function query_callback( int $i ): void {
$query = get_option( 'npr_cds_query_' . $i );
$optionType = get_option( 'npr_cds_pull_post_type', 'post' );
if ( !empty( $query['pull_type'] ) ) {
$optionType = $query['pull_type'];
}
if ( empty( $query['import_tags'] ) ) {
$query['import_tags'] = '1';
}
$post_types = get_post_types();
$output = '<div class="npr-cds-query"><h4>Filters</h4><div><p><input type="text" value="' . $query['filters'] . '" name="npr_cds_query_' . $i . '[filters]" placeholder="profileIds=renderable&collectionIds=1002" /></p>' .
'<p><em>A list of available filtering options can be found <a href="https://npr.github.io/content-distribution-service/api-reference/core-concepts/querying/#filtering">in the CDS documentation</a></em></p></div>' .
'<h4>Sorting</h4><div><p><input type="text" value="' . $query['sorting'] . '" name="npr_cds_query_' . $i . '[sorting]" placeholder="sort=<type>[:<direction>]" /></p>' .
'<p><em>A list of available sorting query parameters can be found <a href="https://npr.github.io/content-distribution-service/api-reference/core-concepts/querying/#sorting">in the CDS documentation</a></em></p></div>' .
'<h4>Publish or Save as Draft?</h4> ' .
'<div><select id="npr_cds_query_' . $i . '[publish]" name="npr_cds_query_' . $i . '[publish]">' .
'<option value="Publish"' . ( $query['publish'] == 'Publish' ? ' selected' : '' ) . '>Publish</option>' .
'<option value="Draft"' . ( $query['publish'] == 'Draft' ? ' selected' : '' ) . '>Draft</option>' .
'</select></div>' .
'<h4>Save as post type?</h4>' .
$this->show_post_types_select( 'npr_cds_query_' . $i . '[pull_type]', $post_types, true );
if ( $optionType == 'post' ) {
$args = [
'show_option_none' => __( 'Select category', 'npr-content-distribution-service' ),
'id' => 'npr_cds_query_' . $i . '[category]',
'name' => 'npr_cds_query_' . $i . '[category]',
'hierarchical' => true,
'show_count' => 0,
'orderby' => 'name',
'echo' => 0,
'selected' => ( !empty( $query['category'] ) ? (int)$query['category'] : 0 ),
'hide_empty' => 0,
'multiple' => true
];
$select = wp_dropdown_categories( $args );
$output .= '<h4>Add Category</h4><div>' . $select . '<p><em>This option applies to posts only</em></p></div>';
}
$output .= '<h4>Add Tags</h4><div><p><input type="text" value="' . $query['tags'] . '" name="npr_cds_query_' . $i . '[tags]" placeholder="pepperoni,pineapple,mozzarella" /></p>' .
'<p><em>Add tag(s) to each story pulled from NPR (comma separated).</em></p></div>';
$output .= '<h4>Import CDS Tags?</h4><div><p><select id="npr_cds_query_' . $i . '[import_tags]" name="npr_cds_query_' . $i . '[import_tags]">'.
'<option value="1"' . ( $query['import_tags'] === '1' ? ' selected' : '' ) . '>Import</option>' .
'<option value="0"' . ( $query['import_tags'] === '0' ? ' selected' : '' ) . '>Do Not Import</option>' .
'</select></p></div>';
echo npr_cds_esc_html( $output );
}
public function query_run_multi_callback(): void {
$run_multi = get_option( 'npr_cds_query_run_multi' );
$num = get_option( 'npr_cds_num', 5 );
$enable = false;
for ( $i = 0; $i < $num; $i++ ) {
$option = get_option( 'npr_cds_query_' . $i );
if ( !empty( $option['filters'] ) || !empty( $options['sorting'] ) ) {
$enable = true;
}
}
if ( $enable ) {
$check_box_string = '<p><input id="npr_cds_query_run_multi" name="npr_cds_query_run_multi" type="checkbox" value="true"' . ( $run_multi ? ' checked="checked"' : '' ) . ' /></p>';
} else {
$check_box_string = '<p><input id="npr_cds_query_run_multi" name="npr_cds_query_run_multi" type="checkbox" value="true" disabled /> <em>Add filters or sorting to the queries above to enable this option</em></p>';
}
echo npr_cds_esc_html( $check_box_string );
}
public function query_multi_cron_interval_callback(): void {
$option = get_option( 'npr_cds_query_multi_cron_interval' );
if ( !wp_next_scheduled( 'npr_cds_hourly_cron' ) ) {
npr_cds_error_log( 'turning on cron event for NPR CDS plugin' );
wp_schedule_event( time(), 'npr_cds_interval', 'npr_cds_hourly_cron' );
}
echo npr_cds_esc_html( '<p><input type="number" value="' . $option . '" name="npr_cds_query_multi_cron_interval" id="npr_cds_query_multi_cron_interval" /></p><p><em>How often, in minutes, should the Get Multi function run? (default = 60)</em></p>' );
}
/**
* NPR Push Settings Group Callbacks
*/
public function use_custom_mapping_callback(): void {
$use_custom = get_option( 'npr_cds_push_use_custom_map' );
$check_box_string = '<input id="npr_cds_push_use_custom_map" name="npr_cds_push_use_custom_map" type="checkbox" value="true"' .
( $use_custom ? ' checked="checked"' : '' ) . ' />';
echo npr_cds_esc_html( $check_box_string );
}
public function mapping_title_callback(): void {
$push_post_type = npr_cds_get_push_post_type();
$keys = npr_cds_get_post_meta_keys( $push_post_type );
$this->show_keys_select( 'npr_cds_mapping_title', $keys );
}
public function mapping_body_callback(): void {
$push_post_type = npr_cds_get_push_post_type();
$keys = npr_cds_get_post_meta_keys( $push_post_type );
$this->show_keys_select( 'npr_cds_mapping_body', $keys );
}
public function mapping_byline_callback(): void {
$push_post_type = npr_cds_get_push_post_type();
$keys = npr_cds_get_post_meta_keys( $push_post_type );
$this->show_keys_select( 'npr_cds_mapping_byline', $keys );
}
public function mapping_media_credit_callback(): void {
$keys = npr_cds_get_post_meta_keys( 'attachment' );
$this->show_keys_select( 'npr_cds_mapping_media_credit', $keys );
}
public function mapping_media_agency_callback(): void {
$keys = npr_cds_get_post_meta_keys( 'attachment' );
$this->show_keys_select( 'npr_cds_mapping_media_agency', $keys );
}
/**
* create the select widget where the ID is the value in the array
*
* @param string $field_name
* @param array $keys - an array like (1=>'Value1', 2=>'Value2', 3=>'Value3');
* @param bool $return
*
* @return string
*/
public function show_post_types_select( string $field_name, array $keys, bool $return = false ): string {
$selected = $output = '';
$first_label = 'Select';
if ( str_contains( $field_name, 'npr_cds_query_' ) ) {
$first_label = 'Default';
preg_match( '/(npr_cds_query_[0-9]+)\[(.+)\]/', $field_name, $match );
if ( !empty( $match ) ) {
$option = get_option( $match[1] );
if ( !empty( $option[ $match[2] ] ) ) {
$selected = $option[ $match[2] ];
}
}
} else {
$selected = get_option( $field_name );
}
$output .= npr_cds_esc_html( '<div><select id="' . $field_name . '" name="' . $field_name . '">' );
$output .= '<option value=""> — ' . $first_label . ' — </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>";
$output .= npr_cds_esc_html( $option_string );
}
$output .= "</select> </div>";
if ( !$return ) {
echo $output;
}
return $output;
}
/**
* checkbox validation callback
*/
public function validation_callback_checkbox( $value ): bool {
return (bool) $value;
}
/**
* Prefix validation callback. We only want to save the prefix without the hyphen
*/
public function validation_callback_prefix( $value ): string {
if ( empty( $_GET['page'] ) || !str_contains( $_GET['page'], 'npr_cds' ) || empty( $_GET['cds_action'] ) || $_GET['cds_action'] !== 'restore' ) {
if ( !isset( $_POST[ '_wpnonce' ] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ '_wpnonce' ] ) ), sanitize_text_field( $_POST[ 'option_page' ] ) . '-options' ) ) {
return '';
}
}
$value = strtolower( $value );
preg_match( '/([a-z0-9]+)/', $value, $match );
if ( !empty( $match ) ) {
return $match[1];
}
add_settings_error(
'npr_cds_prefix',
'prefix-is-invalid',
esc_html( $value ) . __( ' is not a valid value for the NPR CDS Prefix. It can only contain lowercase alphanumeric characters.', 'npr-content-distribution-service' )
);
return '';
}
/**
* URL validation callbacks for the CDS URLs
*/
public function validation_callback_pull_url( string $value ): string {
if ( empty( $_GET['page'] ) || !str_contains( $_GET['page'], 'npr_cds' ) || empty( $_GET['cds_action'] ) || $_GET['cds_action'] !== 'restore' ) {
if ( !isset( $_POST[ '_wpnonce' ] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ '_wpnonce' ] ) ), sanitize_text_field( $_POST[ 'option_page' ] ) . '-options' ) ) {
return '';
}
}
if ( $value == 'https://stage-content.api.npr.org' || $value == 'https://content.api.npr.org' ) {
return esc_attr( $value );
} elseif ( $value == 'other' ) {
$value = rtrim( sanitize_url( $_POST['npr_cds_pull_url_other'] ), "/" );
if ( !preg_match( '/https:\/\/[a-z0-9\.\-]+/', $value ) ) {
add_settings_error(
'npr_cds_pull_url',
'not-https-url',
esc_url( $value ) . __( ' is not a valid value for the NPR CDS Pull URL. It must be a URL starting with <code>https</code>.', 'npr-content-distribution-service' )
);
$value = '';
}
}
return esc_attr( $value );
}
public function validation_callback_push_url( string $value ): string {
if ( empty( $_GET['page'] ) || !str_contains( $_GET['page'], 'npr_cds' ) || empty( $_GET['cds_action'] ) || $_GET['cds_action'] !== 'restore' ) {
if ( !isset( $_POST[ '_wpnonce' ] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ '_wpnonce' ] ) ), sanitize_text_field( $_POST[ 'option_page' ] ) . '-options' ) ) {
return '';
}
}
if ( $value == 'https://stage-content.api.npr.org' || $value == 'https://content.api.npr.org' ) {
return esc_attr( $value );
} elseif ( $value == 'other' ) {
$value = sanitize_url( $_POST['npr_cds_push_url_other'] );
if ( !preg_match( '/https:\/\/[a-z0-9\.\-]+/', $value ) ) {
add_settings_error(
'npr_cds_push_url',
'not-https-url',
esc_url( $value ) . __( ' is not a valid value for the NPR CDS Push URL. It must be a URL starting with <code>https</code>.', 'npr-content-distribution-service' )
);
$value = '';
}
}
return esc_attr( $value );
}
public function validation_num( int $value ): int {
if ( empty( $_GET['page'] ) || !str_contains( $_GET['page'], 'npr_cds' ) || empty( $_GET['cds_action'] ) || $_GET['cds_action'] !== 'restore' ) {
if ( !isset( $_POST[ '_wpnonce' ] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ '_wpnonce' ] ) ), sanitize_text_field( $_POST[ 'option_page' ] ) . '-options' ) ) {
return 0;
}
}
if ( $value < 0 ) {
return 0;
}
if ( $value > NPR_MAX_QUERIES ) {
return NPR_MAX_QUERIES;
}
return $value;
}
public function validation_callback_org_id( $value ): string {
if ( empty( $_GET['page'] ) || !str_contains( $_GET['page'], 'npr_cds' ) || empty( $_GET['cds_action'] ) || $_GET['cds_action'] !== 'restore' ) {
if ( !isset( $_POST[ '_wpnonce' ] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ '_wpnonce' ] ) ), sanitize_text_field( $_POST[ 'option_page' ] ) . '-options' ) ) {
return '';
}
}
$value = str_replace( 's', '', $value );
if ( preg_match( '/[0-9,]+/', $value ) ) {
$value_x = explode( ',', $value );
foreach( $value_x as $k => $v ) {
$value_x[ $k ] = 's' . $v;
}
$value = implode( ',', $value_x );
}
return esc_attr( $value );
}
/**
* Create the select widget of all meta fields
*
* @param string $field_name
* @param array $keys
* @param bool $return
*
* @return string
*/
public function show_keys_select( string $field_name, array $keys ): void {
$output = '';
$selected = get_option( $field_name );
$output .= '<div><select id="' . esc_attr( $field_name ) . '" name="' . esc_attr( $field_name ) . '">';
$output .= '<option value="#NONE#"> — default — </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>";
$output .= npr_cds_esc_html( $option_string );
}
$output .= "</select> </div>";
echo $output;
}
/**
* Functions to restore previous settings if they're available in the database
*/
public function restore_old(): string {
$output = '';
$old_options = get_option( 'npr_cds_old_options' );
$page = 'npr_cds';
if ( !empty( $old_options ) ) {
if ( ! empty( $_GET['page'] ) ) {
$page = $_GET['page'];
}
$output = '<div class="notice notice-warning"><p style="display: inline-flex; align-items: center; gap: 1rem;">You have previous stored options for this plugin. What would you like to do? <a class="button-secondary" href="' . admin_url( 'admin.php?page=' . $page . '&cds_action=restore' ) . '">Restore Previous Options</a> <a class="button-secondary" href="' . admin_url( 'admin.php?page=' . $page . '&cds_action=delete' ) . '">Delete Previous Options</a></p></div>';
} elseif ( !empty( $_GET['cds_result'] ) ) {
if ( $_GET['cds_result'] === 'restored' ) {
$output = '<div class="notice notice-warning"><p>The previous options have been restored.</p></div>';
} elseif ( $_GET['cds_result'] === 'deleted' ) {
$output = '<div class="notice notice-warning"><p>The previous options have been deleted.</p></div>';
}
}
return $output;
}
public function restore_page_hook(): void {
$page = $post_link = '';
if ( !empty( $_GET['page'] ) ) {
$page = $_GET['page'];
}
if ( empty( $page ) || !str_contains( $page, 'npr_cds' ) ) {
return;
}
$old_options = get_option( 'npr_cds_old_options' );
if ( !empty( $old_options ) ) {
if ( !empty( $_GET['cds_action'] ) ) {
if ( $_GET['cds_action'] === 'restore' ) {
foreach ( $old_options as $key => $value ) {
update_option( $key, $value );
}
delete_option( 'npr_cds_old_options' );
$post_link = admin_url( 'admin.php?page=' . $page . '&cds_result=restored' );
} elseif ( $_GET['cds_action'] === 'delete' ) {
delete_option( 'npr_cds_old_options' );
$post_link = admin_url( 'admin.php?page=' . $page . '&cds_result=deleted' );
}
if ( !empty( $post_link ) ) {
wp_redirect( $post_link );
}
}
}
}
/**
* What is the post type that pulled stories should be created as?
*
* @return string The post type
*/
public static function get_pull_post_type(): string {
return get_option( 'npr_cds_pull_post_type', 'post' );
}
/**
* The cron job to pull stories from the API
*/
public static function cron_pull(): void {
// here we should get the list of IDs/full urls that need to be checked hourly
//because this is run on cron, and may be fired off by an non-admin, we need to load a bunch of stuff
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$pull_url = NPR_CDS_PULL_URL;
// This is debug code. It may be save future devs some time; please keep it around.
/*
$now = gmDate("D, d M Y G:i:s O ");
error_log("right now the time is -- ".$now); // debug use
*/
// here we go.
$num = get_option( 'npr_cds_num' );
for ( $i = 0; $i < $num; $i++ ) {
$api = new NPR_CDS_WP();
$query = get_option( 'npr_cds_query_' . $i );
if ( !empty( $query ) ) {
npr_cds_error_log( 'Cron '. $i . ' querying NPR CDS for ' . $query['filters'] );
//if the query string contains the pull url and 'query', just make request from the API
$url = $pull_url . '/' . NPR_CDS_WP::NPR_CDS_VERSION . '/documents?';
$query_array = [];
if ( empty( $query['filters'] ) && empty( $query['sorting'] ) ) {
continue;
}
if ( !empty( $query['filters'] ) ) {
$filters = explode( '&', $query['filters'] );
if ( !empty( $filters ) ) {
foreach ( $filters as $filter ) {
$filt = explode( '=', $filter );
if ( !empty( $filt[1] ) ) {
$query_array[] = $filter;
}
}
}
}
if ( !empty( $query['sorting'] ) ) {
$sorting = explode( '&', $query['sorting'] );
if ( !empty( $sorting ) ) {
foreach ( $sorting as $sort ) {
$sort_x = explode( '=', $sort );
if ( !empty( $sort_x[1] ) ) {
$query_array[] = $sort;
}
}
}
}
$url .= implode( '&', $query_array );
$api->query_by_url( $url );
$api->parse();
try {
$pub_flag = FALSE;
if ( $query['publish'] == 'Publish' ) {
$pub_flag = TRUE;
}
$api->update_posts_from_stories( $pub_flag, $i );
if ( !empty( $api->message ) ) {
error_log( 'NPR CDS: not going to save story. Query ' . implode( '&', $query_array ) . ' returned an error ' . $api->message . ' error' ); // debug use
}
} catch( Exception $e ) {
error_log( 'NPR CDS: error in ' . __FUNCTION__ . ' like this :'. $e ); // debug use
}
}
}
}
/**
* Function to convert an alleged NPR story URL or ID into a story ID, then request it
* @throws Exception
*/
public function load_page_hook(): void {
$required_capability = apply_filters( 'npr_cds_get_stories_capability', 'edit_posts' );
// if the current user shouldn't be doing this, fail
if ( !current_user_can( $required_capability ) ) {
wp_die(
__( 'You do not have permission to edit posts, and therefore you do not have permission to pull posts from the NPR CDS', 'npr-content-distribution-service' ),
__( 'NPR CDS Error', 'npr-content-distribution-service' ),
403
);
}
$publish = $valid = false;
$story_id = 0;
// find the input that is allegedly a story id
// We validate these later
if ( isset( $_POST['story_id'] ) ) {
if ( !check_admin_referer( 'npr_cds_nonce_story_id', 'npr_cds_nonce_story_id_field' ) ) {
wp_die(
__( 'Nonce did not verify in NPR_CDS::load_page_hook. Are you sure you should be doing this?', 'npr-content-distribution-service' ),
__( 'NPR CDS Error', 'npr-content-distribution-service' ),
403
);
}
$story_id = trim( sanitize_text_field( $_POST[ 'story_id' ] ) );
if ( isset( $_POST['publishNow'] ) ) {
$publish = true;
}
if ( isset( $_POST['createDraft'] ) ) {
$publish = false;
}
} elseif ( isset( $_GET['story_id'] ) && isset( $_GET['create_draft'] ) ) {
$story_id = trim( sanitize_text_field( $_GET['story_id'] ) );
}
// try to get the ID of the story from the URL
if ( !empty( $story_id ) ) {
//check to see if we got an ID or a URL
if ( is_numeric( $story_id ) && strlen( $story_id ) >= 8 ) {
$valid = true;
} elseif ( preg_match( '/^[a-z0-9\-]+$/', $story_id ) ) {
$valid = true;
} elseif ( wp_http_validate_url( $story_id ) ) {
$story_id = sanitize_url( $story_id );
// url format: /yyyy/mm/dd/id