-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathclass-settings-page.php
More file actions
1729 lines (1562 loc) · 55.7 KB
/
class-settings-page.php
File metadata and controls
1729 lines (1562 loc) · 55.7 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
/**
* UI: Settings page class
*
* @package Parsely
* @since 3.0.0
*/
declare(strict_types=1);
namespace Parsely\UI;
use Parsely\Content_Helper\Excerpt_Suggestions;
use Parsely\Parsely;
use Parsely\Permissions;
use Parsely\Utils\Utils;
use Parsely\Validator;
use const Parsely\PARSELY_FILE;
/**
* Renders the wp-admin Parse.ly plugin settings page.
*
* @since 3.0.0
*
* @phpstan-type Setting_Arguments array{
* add_fieldset?: bool,
* legend?: string,
* option_key: string,
* label_for: string,
* title?: string,
* help_text?: string,
* yes_text?: string,
* filter?: string,
* optional_args?: Setting_Optional_Args,
* select_options?: array<string, string>,
* radio_options?: array<string, string>,
* }
*
* @phpstan-type Setting_Optional_Args array{
* type?: string,
* placeholder?: string,
* required?: string,
* is_obfuscated_value: bool,
* }
*
* @phpstan-type ParselySettingOptions array{
* apikey: string,
* api_secret: string,
* metadata_secret: string,
* meta_type?: string,
* logo: string,
* track_authenticated_users: bool|string,
* disable_javascript: bool|string,
* disable_amp?: bool,
* track_post_types_as?: array<string, string>,
* track_post_types: string[],
* track_page_types: string[],
* full_metadata_in_non_posts: ?bool,
* content_id_prefix?: string,
* use_top_level_cats?:bool|string,
* custom_taxonomy_section?: string,
* cats_as_tags?: bool|string,
* content_helper: Parsely_Settings_Options_Content_Helper,
* lowercase_tags?: bool,
* force_https_canonicals?: bool,
* disable_autotrack?: bool|string,
* }
*
* @phpstan-type Parsely_Settings_Options_Content_Helper array{
* ai_features_enabled?: bool,
* smart_linking?: Parsely_Settings_Options_Content_Helper_Feature,
* title_suggestions?: Parsely_Settings_Options_Content_Helper_Feature,
* excerpt_suggestions?: Parsely_Settings_Options_Content_Helper_Feature,
* traffic_boost?: Parsely_Settings_Options_Content_Helper_Feature,
* }
*
* @phpstan-type Parsely_Settings_Options_Content_Helper_Feature array{
* enabled?: bool,
* allowed_user_roles?: array<string, string>|array<string, bool>
* }
*
* @phpstan-import-type Parsely_Options from Parsely
*/
final class Settings_Page {
/**
* Instance of Parsely class.
*
* @var Parsely
*/
private $parsely;
/**
* Admin page name used for hook suffixes.
*
* @since 3.2.0
*
* @var string
*/
private $hook_suffix;
/**
* Options for badges that are displayed for managed options.
*
* @since 3.9.0
*
* @var array<string, mixed>
*/
private $managed_options_badge = array();
/**
* The Content Intelligence features that can be configured in the settings
* page.
*
* @since 3.16.0
*
* @var string[]
*/
private $configurable_pch_features = array(
'smart_linking',
'title_suggestions',
'excerpt_suggestions',
'traffic_boost',
);
/**
* Constructor.
*
* @param Parsely $parsely Instance of Parsely class.
*/
public function __construct( Parsely $parsely ) {
$this->parsely = $parsely;
$managed_options_badge = apply_filters(
'wp_parsely_managed_options_badge',
array(
'text' => __( 'Upgrade', 'wp-parsely' ),
'url' => 'https://www.parse.ly/getdemo/',
)
);
if ( is_array( $managed_options_badge ) ) {
$this->managed_options_badge = $managed_options_badge;
}
}
/**
* Registers settings page.
*
* @since 3.0.0
*/
public function run(): void {
add_action( 'admin_menu', array( $this, 'add_settings_sub_menu' ) );
add_action( 'admin_init', array( $this, 'initialize_settings' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_settings_assets' ) );
}
/**
* Enqueues all needed scripts and styles for Parse.ly plugin settings page.
*
* @param string|null $hook_suffix The current page being loaded.
*/
public function enqueue_settings_assets( ?string $hook_suffix ): void {
if ( ! is_string( $hook_suffix ) || $this->hook_suffix !== $hook_suffix ) {
return;
}
add_filter( 'media_library_months_with_files', '__return_empty_array' );
wp_enqueue_media();
$admin_settings_asset = Utils::get_asset_info( 'build/admin-settings.asset.php' );
$built_assets_url = plugin_dir_url( PARSELY_FILE ) . 'build/';
wp_enqueue_script(
'parsely-admin-settings',
$built_assets_url . 'admin-settings.js',
$admin_settings_asset['dependencies'],
$admin_settings_asset['version'],
true
);
wp_enqueue_style(
'parsely-admin-settings',
$built_assets_url . 'admin-settings.css',
array(),
$admin_settings_asset['version']
);
}
/**
* Adds the Parse.ly settings page in WordPress settings menu.
*/
public function add_settings_sub_menu(): void {
$suffix = add_submenu_page(
'parsely-dashboard-page',
__( 'Parse.ly Settings', 'wp-parsely' ),
__( 'Settings', 'wp-parsely' ),
Parsely::CAPABILITY, // phpcs:ignore WordPress.WP.Capabilities.Undetermined
Parsely::MENU_SLUG,
array( $this, 'display_settings' )
);
if ( is_string( $suffix ) ) {
$this->hook_suffix = $suffix;
// Adds help text when admin page loads.
add_action( 'load-' . $this->hook_suffix, array( $this, 'add_help_text' ) );
}
}
/**
* Adds the help tab to the settings page.
*
* @since 3.1.0
*/
public function add_help_text(): void {
$screen = get_current_screen();
if ( null === $screen ) {
return;
}
$screen->add_help_tab(
array(
'id' => 'overview',
'title' => __( 'Overview', 'wp-parsely' ),
'content' => '<p>' . __( 'The only required setting on this page is the Site ID. All of the other settings are optional.', 'wp-parsely' ) . '</p>' .
'<p>' . __( 'You must click the Save Changes button at the bottom of the screen for new settings to take effect.', 'wp-parsely' ) . '</p>',
)
);
}
/**
* Displays the Parse.ly settings screen (admin.php?page=[SLUG]).
*/
public function display_settings(): void {
// phpcs:ignore WordPress.WP.Capabilities.Undetermined
if ( ! current_user_can( Parsely::CAPABILITY ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'wp-parsely' ) );
}
include_once plugin_dir_path( PARSELY_FILE ) . 'src/UI/settings-page.php';
}
/**
* Initializes the settings for Parse.ly.
*/
public function initialize_settings(): void {
// Add the option first, to prevent double sanitization of the uninitialized option as reported
// in https://core.trac.wordpress.org/ticket/21989.
// The option will be initialized with the default values.
add_option( Parsely::OPTIONS_KEY, $this->parsely->get_options() );
// All our options are actually stored in one single array to reduce DB queries.
register_setting(
Parsely::OPTIONS_KEY,
Parsely::OPTIONS_KEY,
array(
'type' => 'array',
'sanitize_callback' => array( $this, 'validate_options' ),
)
);
$this->initialize_basic_section();
$this->initialize_content_helper_section();
$this->initialize_recrawl_section();
$this->initialize_advanced_section();
}
/**
* Registers section and settings for Basic section.
*
* @since 3.2.0
*/
private function initialize_basic_section(): void {
$are_credentials_managed = $this->parsely->are_credentials_managed;
$section_key = 'basic-section';
add_settings_section(
$section_key,
__( 'Basic', 'wp-parsely' ),
'__return_null',
Parsely::MENU_SLUG
);
// Site ID.
$field_id = 'apikey';
$field_args = array(
'option_key' => $field_id,
'help_text' => __( 'Your Site ID is typically your own site domain without <code>http(s)://</code> prefixes or trailing <code>/</code> (e.g. <code>mydomain.com</code>).', 'wp-parsely' ),
'label_for' => $field_id,
'optional_args' => array(
'required' => 'required',
'placeholder' => 'mydomain.com',
'disabled' => $are_credentials_managed,
),
);
add_settings_field(
$field_id,
__( 'Site ID <em>(required)</em>', 'wp-parsely' ),
array( $this, 'print_text_tag' ),
Parsely::MENU_SLUG,
$section_key,
$field_args
);
if ( ! $are_credentials_managed ) {
// API Secret.
$field_id = 'api_secret';
$field_args = array(
'option_key' => $field_id,
'help_text' => __( 'Your API secret is your secret code to <a href="https://docs.parse.ly/the-parsely-api/">access our API</a>. It can be found at <code>dash.parsely.com/<var>yoursitedomain</var>/settings/api</code> (replace <var>yoursitedomain</var> with your domain name, e.g. <samp>mydomain.com</samp>).<br />If you haven\'t purchased access to the API and would like to do so, email your account manager or <a href="mailto:support@parsely.com">support@parsely.com</a>.', 'wp-parsely' ),
'label_for' => $field_id,
'optional_args' => array(
'type' => 'password',
'is_obfuscated_value' => true,
),
);
add_settings_field(
$field_id,
__( 'API Secret', 'wp-parsely' ),
array( $this, 'print_text_tag' ),
Parsely::MENU_SLUG,
$section_key,
$field_args
);
}
// Metadata Format.
$field_id = 'meta_type';
$field_args = array(
'title' => __( 'Metadata Format', 'wp-parsely' ),
'option_key' => $field_id,
'help_text' => __( 'Choose the metadata format for our crawlers to access. Most publishers are fine with <a href="https://docs.parse.ly/metadata-jsonld/">JSON-LD</a>, but if you prefer to use our proprietary metadata format then you can do so here.', 'wp-parsely' ),
'radio_options' => array(
'json_ld' => 'json_ld',
'repeated_metas' => 'repeated_metas',
),
'label_for' => Parsely::OPTIONS_KEY . "[$field_id]",
'filter' => 'wp_parsely_metadata',
);
add_settings_field(
$field_id,
$this->set_field_label_contents( __( 'Metadata Format', 'wp-parsely' ), $field_id ),
array( $this, 'print_radio_tags' ),
Parsely::MENU_SLUG,
$section_key,
$field_args
);
// Logo.
$field_help = __( 'Here you can specify your logo\'s URL by using the "Browse" button or typing the URL manually.', 'wp-parsely' );
$field_id = 'logo';
add_settings_field(
$field_id,
$this->set_field_label_contents( __( 'Logo', 'wp-parsely' ), $field_id ),
array( $this, 'print_media_single_image' ),
Parsely::MENU_SLUG,
$section_key,
array(
'title' => __( 'Logo', 'wp-parsely' ), // Passed for legend element.
'option_key' => $field_id,
'label_for' => $field_id,
'help_text' => $field_help,
)
);
// Track logged-in users.
$field_id = 'track_authenticated_users';
add_settings_field(
$field_id,
$this->set_field_label_contents( __( 'Track Logged-in Users', 'wp-parsely' ), $field_id ),
array( $this, 'print_radio_tags' ),
Parsely::MENU_SLUG,
$section_key,
array(
'title' => __( 'Track Logged-in Users', 'wp-parsely' ), // Passed for legend element.
'option_key' => $field_id,
'radio_options' => array(
'true' => __( 'Yes, track logged-in users.', 'wp-parsely' ),
'false' => __( 'No, do not track logged-in users. I do not want to see the Parse.ly tracking code on my site when browsing while logged in.', 'wp-parsely' ),
),
'help_text' => (
is_multisite() ?
__( ' Note: For WordPress multisite, a user must be logged-in to the current site to be considered logged-in.', 'wp-parsely' ) :
null
),
)
);
// Disable JavaScript.
$field_id = 'disable_javascript';
add_settings_field(
$field_id,
$this->set_field_label_contents( __( 'Disable JavaScript', 'wp-parsely' ), $field_id ),
array( $this, 'print_radio_tags' ),
Parsely::MENU_SLUG,
$section_key,
array(
'title' => __( 'Disable JavaScript', 'wp-parsely' ), // Passed for legend element.
'option_key' => $field_id,
'radio_options' => array(
'true' => __( 'Yes, disable JavaScript tracking. I want to use a separate system for tracking instead of the Parse.ly plugin.', 'wp-parsely' ),
'false' => __( 'No, do not disable JavaScript tracking. I want the Parse.ly plugin to load the tracker.', 'wp-parsely' ),
),
'help_text' => __( '<span style="color:#d63638">WARNING:</span> Changing this setting to "Yes" will prevent the plugin from injecting the Parse.ly tracker. Only do so if you are setting the tracker elsewhere (e.g. hardcoded, GTM, another tag manager, etc.).', 'wp-parsely' ),
'filter' => 'wp_parsely_load_js_tracker',
)
);
if ( defined( 'AMP__VERSION' ) ) {
// Disable AMP tracking.
$field_id = 'disable_amp';
add_settings_field(
$field_id,
$this->set_field_label_contents( __( 'Disable AMP Tracking', 'wp-parsely' ), $field_id ),
array( $this, 'print_radio_tags' ),
Parsely::MENU_SLUG,
$section_key,
array(
'title' => __( 'Disable AMP Tracking', 'wp-parsely' ), // Passed for legend element.
'option_key' => $field_id,
'radio_options' => array(
'true' => __( 'Yes, disable Parse.ly tracking on AMP pages. I use a different system for JavaScript tracking on AMP pages.', 'wp-parsely' ),
'false' => __( 'No, do not disable Parse.ly tracking on AMP pages.', 'wp-parsely' ),
),
)
);
}
}
/**
* Registers the Content Intelligence section and its settings.
*
* @since 3.16.0
*/
private function initialize_content_helper_section(): void {
$section_key = 'content-intelligence-section';
add_settings_section(
$section_key,
__( 'Content Intelligence', 'wp-parsely' ),
'__return_null',
Parsely::MENU_SLUG
);
// AI Features.
$field_id = 'content_helper[ai_features_enabled]';
$field_args = array(
'option_key' => $field_id,
'label_for' => $field_id,
'yes_text' => __( 'Enabled', 'wp-parsely' ),
'add_fieldset' => true,
'legend' => __( 'AI Features', 'wp-parsely' ),
);
add_settings_field(
$field_id,
__( 'AI Features', 'wp-parsely' ),
array( $this, 'print_checkbox_tag' ),
Parsely::MENU_SLUG,
$section_key,
$field_args
);
// Smart Linking.
$field_id = 'content_helper[smart_linking]';
$field_args = array(
'option_key' => $field_id,
'label_for' => $field_id,
'legend' => __( 'Smart Linking', 'wp-parsely' ),
);
add_settings_field(
$field_id,
__( 'Smart Linking', 'wp-parsely' ),
array( $this, 'print_content_helper_ai_feature_section' ),
Parsely::MENU_SLUG,
$section_key,
$field_args
);
// Title Suggestions.
$field_id = 'content_helper[title_suggestions]';
$field_args = array(
'option_key' => $field_id,
'label_for' => $field_id,
'legend' => __( 'Title Suggestions', 'wp-parsely' ),
);
add_settings_field(
$field_id,
__( 'Title Suggestions', 'wp-parsely' ),
array( $this, 'print_content_helper_ai_feature_section' ),
Parsely::MENU_SLUG,
$section_key,
$field_args
);
// Excerpt Suggestions.
$field_id = 'content_helper[excerpt_suggestions]';
$field_args = array(
'option_key' => $field_id,
'label_for' => $field_id,
'legend' => __( 'Excerpt Suggestions', 'wp-parsely' ),
'filter' => Excerpt_Suggestions::get_feature_filter_name(),
);
add_settings_field(
$field_id,
__( 'Excerpt Suggestions', 'wp-parsely' ),
array( $this, 'print_content_helper_ai_feature_section' ),
Parsely::MENU_SLUG,
$section_key,
$field_args
);
// Traffic Boost.
$field_id = 'content_helper[traffic_boost]';
$field_args = array(
'option_key' => $field_id,
'label_for' => $field_id,
'legend' => __( 'Engagement Boost (beta)', 'wp-parsely' ),
);
add_settings_field(
$field_id,
__( 'Engagement Boost (beta)', 'wp-parsely' ),
array( $this, 'print_content_helper_ai_feature_section' ),
Parsely::MENU_SLUG,
$section_key,
$field_args
);
}
/**
* Registers section and settings for Recrawl section.
*
* @since 3.2.0
*/
private function initialize_recrawl_section(): void {
$section_key = 'recrawl-section';
add_settings_section(
$section_key,
__( 'Recrawl', 'wp-parsely' ),
function (): void {
echo '<br /><strong>' . wp_kses_post( __( '<span style="color:#d63638">Important:</span> Changing any of these values below on a site currently tracked with Parse.ly will require reprocessing of your Parse.ly data.', 'wp-parsely' ) ) . '</strong><br />';
printf(
/* translators: Mailto link */
esc_html__( 'Once you have changed a value and saved, please contact %s to request a recrawl.', 'wp-parsely' ),
wp_kses_post( '<a href="mailto:support@parsely.com?subject=' . rawurlencode( 'Please reprocess ' . $this->parsely->get_site_id() ) . '">support@parsely.com</a>' )
);
},
Parsely::MENU_SLUG
);
// Allow use of custom taxonomy to populate articleSection in parselyPage; defaults to category.
$field_id = 'track_post_types_as';
$field_help = __( 'By default, Parse.ly only tracks posts and pages. If you want to track other post types, select how you want to track them here.', 'wp-parsely' );
add_settings_field(
$field_id,
__( 'Track Post Types as', 'wp-parsely' ),
array( $this, 'print_track_post_types_table' ),
Parsely::MENU_SLUG,
$section_key,
array(
'title' => __( 'Track Post Types as', 'wp-parsely' ),
'option_key' => $field_id,
'help_text' => $field_help,
)
);
// Use full metadata in non-posts.
$field_id = 'full_metadata_in_non_posts';
add_settings_field(
$field_id,
$this->set_field_label_contents( __( 'Use Full Metadata in Non-Posts', 'wp-parsely' ), $field_id ),
array( $this, 'print_radio_tags' ),
Parsely::MENU_SLUG,
$section_key,
array(
'title' => __( 'Use Full Metadata in Non-Posts', 'wp-parsely' ), // Passed for legend element.
'option_key' => $field_id,
'radio_options' => array(
'true' => __( 'Yes, add full metadata to Post Types being tracked as Non-Posts.', 'wp-parsely' ),
'false' => __( 'No, we have code that modifies metadata and that has not been tested for compatibility yet.', 'wp-parsely' ),
),
'help_text' => __( '<strong><span style="color:#d63638">Important: This setting will be removed in the future, force-enabling this behavior.</span></strong> If you\'re using any code that modifies metadata in a way that conflicts with this setting when it is enabled, please apply any needed fixes.', 'wp-parsely' ),
)
);
// Content ID Prefix.
$field_id = 'content_id_prefix';
$field_args = array(
'option_key' => $field_id,
'optional_args' => array(
'placeholder' => 'WP-',
),
'help_text' => __( 'If you use more than one content management system (e.g. WordPress and Drupal), you may end up with duplicate content IDs. Adding a Content ID Prefix will ensure the content IDs from WordPress will not conflict with other content management systems. We recommend using "WP-" for your prefix.', 'wp-parsely' ),
'label_for' => $field_id,
);
add_settings_field(
$field_id,
$this->set_field_label_contents( __( 'Content ID Prefix', 'wp-parsely' ), $field_id ),
array( $this, 'print_text_tag' ),
Parsely::MENU_SLUG,
$section_key,
$field_args
);
// Use top-level categories.
$field_id = 'use_top_level_cats';
add_settings_field(
$field_id,
$this->set_field_label_contents( __( 'Use Top-Level Categories for Section', 'wp-parsely' ), $field_id ),
array( $this, 'print_radio_tags' ),
Parsely::MENU_SLUG,
$section_key,
array(
'title' => __( 'Use Top-Level Categories for Section', 'wp-parsely' ), // Passed for legend element.
'option_key' => $field_id,
'radio_options' => array(
'true' => __( 'Yes, use the first category assigned to a post as the section name.', 'wp-parsely' ),
'false' => __( 'No, do not use the first category assigned to a post as the section name.', 'wp-parsely' ),
),
'help_text' => __( 'If you choose Yes, and post a story to News > National > Florida, the plugin will use "News" for the section name in your dashboard instead of "Florida".', 'wp-parsely' ),
)
);
// Allow use of custom taxonomy to populate articleSection in parselyPage; defaults to category.
$field_id = 'custom_taxonomy_section';
$field_args = array(
'option_key' => $field_id,
'help_text' => __( 'By default, the section value in your Parse.ly dashboard maps to a post\'s category. You can optionally choose a custom taxonomy, if you\'ve created one, to populate the section value instead.', 'wp-parsely' ),
'select_options' => self::get_section_taxonomies(),
'label_for' => Parsely::OPTIONS_KEY . "[$field_id]",
);
add_settings_field(
$field_id,
$this->set_field_label_contents( __( 'Use Custom Taxonomy for Section', 'wp-parsely' ), $field_id ),
array( $this, 'print_select_tag' ),
Parsely::MENU_SLUG,
$section_key,
$field_args
);
// Use categories and custom taxonomies as tags.
$field_id = 'cats_as_tags';
add_settings_field(
$field_id,
$this->set_field_label_contents( __( 'Add Categories to Tags', 'wp-parsely' ), $field_id ),
array( $this, 'print_radio_tags' ),
Parsely::MENU_SLUG,
$section_key,
array(
'title' => __( 'Add Categories to Tags', 'wp-parsely' ), // Passed for legend element.
'option_key' => $field_id,
'radio_options' => array(
'true' => __( 'Yes, add all assigned categories and taxonomies to my tags.', 'wp-parsely' ),
'false' => __( 'No, do not add all assigned categories and taxonomies to my tags.', 'wp-parsely' ),
),
'help_text' => __( 'If you choose Yes, then a post that has been assigned the categories "Business/Tech" and "Business/Social" will automatically include "Business/Tech" and "Business/Social" as tags, too.', 'wp-parsely' ),
)
);
// Lowercase all tags.
$field_id = 'lowercase_tags';
add_settings_field(
$field_id,
$this->set_field_label_contents( __( 'Lowercase All Tags', 'wp-parsely' ), $field_id ),
array( $this, 'print_radio_tags' ),
Parsely::MENU_SLUG,
$section_key,
array(
'title' => __( 'Lowercase All Tags', 'wp-parsely' ), // Passed for legend element.
'option_key' => $field_id,
'radio_options' => array(
'true' => __( 'Yes, use lowercase versions of my tags to correct for potential misspellings.', 'wp-parsely' ),
'false' => __( 'No, do not use lowercase versions of my tags to correct for potential misspellings.', 'wp-parsely' ),
),
)
);
$field_id = 'force_https_canonicals';
add_settings_field(
$field_id,
$this->set_field_label_contents( __( 'Force HTTPS Canonicals', 'wp-parsely' ), $field_id ),
array( $this, 'print_radio_tags' ),
Parsely::MENU_SLUG,
$section_key,
array(
'title' => __( 'Force HTTPS Canonicals', 'wp-parsely' ), // Passed for legend element.
'option_key' => $field_id,
'radio_options' => array(
'true' => __( 'Yes, force <code>https</code> canonical URLs by default.', 'wp-parsely' ),
'false' => __( 'No, I want to use <code>http</code>.', 'wp-parsely' ),
),
'help_text' => __( 'Note: the plugin uses <code>http</code> by default, and this is fine for most publishers. It is unlikely you will have to change this unless directed to do so by a Parse.ly support representative.', 'wp-parsely' ),
)
);
}
/**
* Registers section and settings for Advanced section.
*
* @since 3.2.0
*/
private function initialize_advanced_section(): void {
$are_credentials_managed = $this->parsely->are_credentials_managed;
$section_key = 'advanced-section';
add_settings_section(
$section_key,
__( 'Advanced', 'wp-parsely' ),
'__return_null',
Parsely::MENU_SLUG
);
if ( ! $are_credentials_managed ) {
// Metadata Secret.
$field_id = 'metadata_secret';
$field_args = array(
'option_key' => $field_id,
'help_text' => __( 'Your metadata secret is given to you by Parse.ly support. DO NOT enter anything here unless given to you by Parse.ly support!', 'wp-parsely' ),
'label_for' => $field_id,
'optional_args' => array(
'type' => 'password',
'is_obfuscated_value' => true,
),
);
add_settings_field(
$field_id,
__( 'Metadata Secret', 'wp-parsely' ),
array( $this, 'print_text_tag' ),
Parsely::MENU_SLUG,
$section_key,
$field_args
);
}
// Disable autotrack.
$field_id = 'disable_autotrack';
add_settings_field(
$field_id,
$this->set_field_label_contents( __( 'Disable Autotracking', 'wp-parsely' ), $field_id ),
array( $this, 'print_radio_tags' ),
Parsely::MENU_SLUG,
$section_key,
array(
'title' => __( 'Disable Autotracking', 'wp-parsely' ), // Passed for legend element.
'option_key' => $field_id,
'radio_options' => array(
'true' => __( 'Yes, disable autotracking. I do not want the tracking code to report an event as soon as the script has finished loading. I plan to implement Dynamic Tracking myself.', 'wp-parsely' ),
'false' => __( 'No, do not disable autotracking. I want to make sure the default behavior of the tracking code is in place. The tracking code should report an event as soon as the script has finished loading.', 'wp-parsely' ),
),
)
);
}
/**
* Shows setting tabs.
*
* @since 3.8.0
*/
public function show_setting_tabs(): void {
global $wp_settings_sections;
?>
<nav class="nav-tab-wrapper">
<?php foreach ( $wp_settings_sections[ Parsely::MENU_SLUG ] as $section ) { ?>
<a
class="nav-tab <?php echo esc_attr( $section['id'] . '-tab' ); ?>"
href=<?php echo esc_url_raw( '?page=' . Parsely::MENU_SLUG . '#' . $section['id'] ); ?>
>
<?php echo esc_html( $section['title'] ); ?>
</a>
<?php } ?>
</nav>
<?php
}
/**
* Shows content of setting tabs.
*
* @since 3.8.0
*/
public function show_setting_tabs_content(): void {
global $wp_settings_sections;
foreach ( $wp_settings_sections[ Parsely::MENU_SLUG ] as $section ) {
?>
<div class="tab-content <?php echo esc_attr( $section['id'] ); ?>">
<?php
if ( $section['callback'] ) {
call_user_func( $section['callback'], $section );
}
?>
<table class="form-table" role="presentation">
<?php do_settings_fields( Parsely::MENU_SLUG, $section['id'] ); ?>
</table>
</div>
<?php
}
}
/**
* Prints out a warning if the filter for the setting is defined, if any.
*
* @since 3.4.0
*
* @param Setting_Arguments $args The arguments for the form field. May contain 'filter'.
*/
private function print_filter_text( $args ): void {
if ( isset( $args['filter'] ) && has_filter( $args['filter'] ) ) {
echo '<p>';
echo '<b><code>' . esc_html( $args['filter'] ) . '</code>' . esc_html__( 'filter hook is in use!', 'wp-parsely' ) . '</b> ';
echo esc_html__( 'A callback is attached to the filter hook that might interfere and override this setting.', 'wp-parsely' );
echo '</p>';
}
}
/**
* Prints out the description text, if there is any.
*
* @since 3.1.0
*
* @param Setting_Arguments $args The arguments for the form field. May contain 'help_text'.
*/
private function print_description_text( $args ): void {
echo isset( $args['help_text'] ) ? '<p class="description" id="' . esc_attr( $args['option_key'] ) . '-description">' . wp_kses_post( $args['help_text'] ) . '</p>' : '';
}
/**
* Prints out an input text tag.
*
* @param Setting_Arguments $args The arguments for text tag.
*/
public function print_text_tag( $args ): void {
$options = $this->parsely->get_options();
$name = $args['option_key'];
/**
* Variable.
*
* @var string
*/
$value = $options[ $name ] ?? '';
$optional_args = $args['optional_args'] ?? array();
$id = esc_attr( $name );
$name = Parsely::OPTIONS_KEY . "[$id]";
$is_obfuscated_value = $optional_args['is_obfuscated_value'] ?? false;
$value = $is_obfuscated_value ? $this->get_obfuscated_value( $value ) : esc_attr( $value );
$accepted_args = array( 'placeholder', 'required', 'disabled' );
$type = $optional_args['type'] ?? 'text';
$is_managed = key_exists( $id, $this->parsely->managed_options );
echo '<fieldset', $is_managed ? ' disabled>' : '>';
printf( "<input type='%s' name='%s' id='%s' value='%s'", esc_attr( $type ), esc_attr( $name ), esc_attr( $id ), esc_attr( $value ) );
if ( isset( $args['help_text'] ) ) {
echo ' aria-describedby="' . esc_attr( $id ) . '-description"';
}
foreach ( $optional_args as $key => $val ) {
if ( \in_array( $key, $accepted_args, true ) && false !== $val ) {
// Don't add a placeholder to managed option fields.
if ( $is_managed && 'placeholder' === $key ) {
continue;
}
// Support attributes without values.
echo ' ' . esc_attr( $key );
if ( true !== $val ) {
echo '="' . esc_attr( $val ) . '"';
}
}
}
echo ' /></fieldset>';
$this->print_description_text( $args );
}
/**
* Prints a checkbox tag.
*
* @since 3.16.0
*
* @param Setting_Arguments $args Arguments for the checkbox tag.
* @param Parsely_Options|null $options The options to use.
*/
public function print_checkbox_tag( $args, $options = null ): void {
$options = $options ?? $this->parsely->get_options();
$name = $args['option_key'];
$has_fieldset = isset( $args['add_fieldset'] ) && true === $args['add_fieldset'];
$html_id = rtrim( str_replace( array( '[', ']', '__' ), '_', $name ), '_' );
$html_name = str_replace(
'content_helper',
'[content_helper]',
Parsely::OPTIONS_KEY . esc_attr( $name )
);
$yes_text = $args['yes_text'] ?? '';
// Get option value.
if ( false === strpos( $name, '[' ) ) {
$value = $options[ $name ];
} else {
$value = Parsely::get_nested_option_value( $name, $options );
}
// Fieldset start.
if ( $has_fieldset ) {
echo '<fieldset>';
if ( isset( $args['legend'] ) ) {
echo '<legend class="screen-reader-text"><span>';
echo esc_html( $args['legend'] ) . '</span></legend>';
}
}
// Label and contained checkbox.
printf( '<label for="%s">', esc_attr( $html_id ) );
printf(
'<input type="checkbox" name="%s" id="%s" value="true" ',
esc_attr( $html_name ),
esc_attr( $html_id )
);
if ( isset( $args['help_text'] ) ) {
echo ' aria-describedby="' . esc_attr( $html_id ) . '-description"';
}
checked( true === $value, true );
printf( ' /> %s</label>', esc_html( $yes_text ) );
// Fieldset end.
if ( $has_fieldset ) {
echo '</fieldset>';
}
$this->print_description_text( $args );
}
/**
* Prints a Content Intelligence AI feature section.
*
* @since 3.16.0
*
* @param Setting_Arguments $args The arguments for the section.
*/
public function print_content_helper_ai_feature_section( $args ): void {
$options = $this->parsely->get_options();
$feature_id = str_replace(
array( 'content_helper[', ']' ),
'',
$args['option_key']
);
// Convert user role settings to make them work in the settings page.
$option = &$options['content_helper'][ $feature_id ];
if ( isset( $option['allowed_user_roles'] ) ) {
foreach ( $option['allowed_user_roles'] as $role ) {
$option['allowed_user_roles'][ $role ] = true;
}
}
// Feature "Enabled" checkbox.
$enabled_args = array(
'option_key' => $args['option_key'] . '[enabled]',
'label_for' => $args['option_key'] . '[enabled]',
'yes_text' => __( 'Enabled', 'wp-parsely' ),
'add_fieldset' => true,
);
if ( isset( $args['legend'] ) ) {
$enabled_args['legend'] = $args['legend'];
}
$this->print_checkbox_tag( $enabled_args, $options );
// User role permissions fieldset.
echo '<p>' . esc_html__( 'User Permissions', 'wp-parsely' ) . '</p>';
echo '<fieldset class="user-role-permissions">';
echo '<legend class="screen-reader-text"><span>';
printf(
/* translators: %s: Feature name */
esc_html__( '%s User Permissions', 'wp-parsely' ),
esc_html( $args['legend'] ?? __( 'Feature', 'wp-parsely' ) )
);
echo '</span></legend>';
// User role checkboxes.
$user_roles = Permissions::get_user_roles_with_edit_posts_cap();
foreach ( $user_roles as $key => $name ) {
$option_key = $args['option_key'] . '[allowed_user_roles][' . $key . ']';
$role_args = array(
'option_key' => $option_key,
'label_for' => $option_key,
'yes_text' => translate_user_role( $name ),
);
$this->print_checkbox_tag( $role_args, $options );
}
echo '</fieldset>';
$this->print_filter_text( $args );