PluginProbe
Parse.ly / 3.16.2
Parse.ly v3.16.2
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
wp-parsely / src / UI / class-settings-page.php

class-settings-page.php in Parse.ly 3.16.2, at src/UI/class-settings-page.php

1,711 lines 55.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UI: Settings page class
4 *
5 * @package Parsely
6 * @since 3.0.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\UI;
12
13 use Parsely\Content_Helper\Excerpt_Generator;
14 use Parsely\Parsely;
15 use Parsely\Permissions;
16 use Parsely\Validator;
17
18 use function Parsely\Utils\get_asset_info;
19
20 use const Parsely\PARSELY_FILE;
21
22 /**
23 * Renders the wp-admin Parse.ly plugin settings page.
24 *
25 * @since 3.0.0
26 *
27 * @phpstan-import-type Parsely_Options from Parsely
28 *
29 * @phpstan-type Setting_Arguments array{
30 * add_fieldset?: bool,
31 * legend?: string,
32 * option_key: string,
33 * label_for: string,
34 * title?: string,
35 * help_text?: string,
36 * yes_text?: string,
37 * filter?: string,
38 * optional_args?: Setting_Optional_Args,
39 * select_options?: array<string, string>,
40 * radio_options?: array<string, string>,
41 * }
42 *
43 * @phpstan-type Setting_Optional_Args array{
44 * type?: string,
45 * placeholder?: string,
46 * required?: string,
47 * is_obfuscated_value: bool,
48 * }
49 *
50 * @phpstan-type ParselySettingOptions array{
51 * apikey: string,
52 * api_secret: string,
53 * metadata_secret: string,
54 * meta_type?: string,
55 * logo: string,
56 * track_authenticated_users: bool|string,
57 * disable_javascript: bool|string,
58 * disable_amp?: bool,
59 * track_post_types_as?: array<string, string>,
60 * track_post_types: string[],
61 * track_page_types: string[],
62 * full_metadata_in_non_posts: ?bool,
63 * content_id_prefix?: string,
64 * use_top_level_cats?:bool|string,
65 * custom_taxonomy_section?: string,
66 * cats_as_tags?: bool|string,
67 * content_helper: Parsely_Settings_Options_Content_Helper,
68 * lowercase_tags?: bool,
69 * force_https_canonicals?: bool,
70 * disable_autotrack?: bool|string,
71 * }
72 *
73 * @phpstan-type Parsely_Settings_Options_Content_Helper array{
74 * ai_features_enabled?: bool,
75 * smart_linking?: Parsely_Settings_Options_Content_Helper_Feature,
76 * title_suggestions?: Parsely_Settings_Options_Content_Helper_Feature,
77 * excerpt_suggestions?: Parsely_Settings_Options_Content_Helper_Feature,
78 * }
79 *
80 * @phpstan-type Parsely_Settings_Options_Content_Helper_Feature array{
81 * enabled?: bool,
82 * allowed_user_roles?: array<string, string>|array<string, bool>
83 * }
84 *
85 * @phpstan-import-type Parsely_Options from Parsely
86 */
87 final class Settings_Page {
88 /**
89 * Instance of Parsely class.
90 *
91 * @var Parsely
92 */
93 private $parsely;
94
95 /**
96 * Admin page name used for hook suffixes.
97 *
98 * @since 3.2.0
99 *
100 * @var string
101 */
102 private $hook_suffix;
103
104 /**
105 * Options for badges that are displayed for managed options.
106 *
107 * @since 3.9.0
108 *
109 * @var array<string, mixed>
110 */
111 private $managed_options_badge = array();
112
113 /**
114 * The Content Helper features that can be configured in the settings page.
115 *
116 * @since 3.16.0
117 *
118 * @var string[]
119 */
120 private $configurable_pch_features = array(
121 'smart_linking',
122 'title_suggestions',
123 'excerpt_suggestions',
124 );
125
126 /**
127 * Constructor.
128 *
129 * @param Parsely $parsely Instance of Parsely class.
130 */
131 public function __construct( Parsely $parsely ) {
132 $this->parsely = $parsely;
133
134 $managed_options_badge = apply_filters(
135 'wp_parsely_managed_options_badge',
136 array(
137 'text' => __( 'Upgrade', 'wp-parsely' ),
138 'url' => 'https://www.parse.ly/getdemo/',
139 )
140 );
141
142 if ( is_array( $managed_options_badge ) ) {
143 $this->managed_options_badge = $managed_options_badge;
144 }
145 }
146
147 /**
148 * Registers settings page.
149 *
150 * @since 3.0.0
151 */
152 public function run(): void {
153 add_action( 'admin_menu', array( $this, 'add_settings_sub_menu' ) );
154 add_action( 'admin_init', array( $this, 'initialize_settings' ) );
155 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_settings_assets' ) );
156 }
157
158 /**
159 * Enqueues all needed scripts and styles for Parse.ly plugin settings page.
160 *
161 * @param string|null $hook_suffix The current page being loaded.
162 */
163 public function enqueue_settings_assets( ?string $hook_suffix ): void {
164 if ( ! is_string( $hook_suffix ) || 'settings_page_parsely' !== $hook_suffix ) {
165 return;
166 }
167
168 add_filter( 'media_library_months_with_files', '__return_empty_array' );
169 wp_enqueue_media();
170
171 $admin_settings_asset = get_asset_info( 'build/admin-settings.asset.php' );
172 $built_assets_url = plugin_dir_url( PARSELY_FILE ) . '/build/';
173
174 wp_enqueue_script(
175 'parsely-admin-settings',
176 $built_assets_url . 'admin-settings.js',
177 $admin_settings_asset['dependencies'],
178 $admin_settings_asset['version'],
179 true
180 );
181
182 wp_enqueue_style(
183 'parsely-admin-settings',
184 $built_assets_url . 'admin-settings.css',
185 $admin_settings_asset['dependencies'],
186 $admin_settings_asset['version']
187 );
188 }
189
190 /**
191 * Adds the Parse.ly settings page in WordPress settings menu.
192 */
193 public function add_settings_sub_menu(): void {
194 $suffix = add_options_page(
195 __( 'Parse.ly Settings', 'wp-parsely' ),
196 __( 'Parse.ly', 'wp-parsely' ),
197 Parsely::CAPABILITY, // phpcs:ignore WordPress.WP.Capabilities.Undetermined
198 Parsely::MENU_SLUG,
199 array( $this, 'display_settings' )
200 );
201
202 if ( is_string( $suffix ) ) {
203 $this->hook_suffix = $suffix;
204
205 // Adds help text when admin page loads.
206 add_action( 'load-' . $this->hook_suffix, array( $this, 'add_help_text' ) );
207 }
208 }
209
210 /**
211 * Adds the help tab to the settings page.
212 *
213 * @since 3.1.0
214 */
215 public function add_help_text(): void {
216 $screen = get_current_screen();
217 if ( null === $screen ) {
218 return;
219 }
220
221 $screen->add_help_tab(
222 array(
223 'id' => 'overview',
224 'title' => __( 'Overview', 'wp-parsely' ),
225 'content' => '<p>' . __( 'The only required setting on this page is the Site ID. All of the other settings are optional.', 'wp-parsely' ) . '</p>' .
226 '<p>' . __( 'You must click the Save Changes button at the bottom of the screen for new settings to take effect.', 'wp-parsely' ) . '</p>',
227 )
228 );
229 }
230
231 /**
232 * Displays the Parse.ly settings screen (options-general.php?page=[SLUG]).
233 */
234 public function display_settings(): void {
235 // phpcs:ignore WordPress.WP.Capabilities.Undetermined
236 if ( ! current_user_can( Parsely::CAPABILITY ) ) {
237 wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'wp-parsely' ) );
238 }
239
240 include_once plugin_dir_path( PARSELY_FILE ) . 'views/parsely-settings.php';
241 }
242
243 /**
244 * Initializes the settings for Parse.ly.
245 */
246 public function initialize_settings(): void {
247 // Add the option first, to prevent double sanitization of the uninitialized option as reported
248 // in https://core.trac.wordpress.org/ticket/21989.
249 // The option will be initialized with the default values.
250 add_option( Parsely::OPTIONS_KEY, $this->parsely->get_options() );
251
252 // All our options are actually stored in one single array to reduce DB queries.
253 register_setting(
254 Parsely::OPTIONS_KEY,
255 Parsely::OPTIONS_KEY,
256 array(
257 'type' => 'array',
258 'sanitize_callback' => array( $this, 'validate_options' ),
259 )
260 );
261
262 $this->initialize_basic_section();
263 $this->initialize_content_helper_section();
264 $this->initialize_recrawl_section();
265 $this->initialize_advanced_section();
266 }
267
268 /**
269 * Registers section and settings for Basic section.
270 *
271 * @since 3.2.0
272 */
273 private function initialize_basic_section(): void {
274 $are_credentials_managed = $this->parsely->are_credentials_managed;
275 $section_key = 'basic-section';
276
277 add_settings_section(
278 $section_key,
279 __( 'Basic', 'wp-parsely' ),
280 '__return_null',
281 Parsely::MENU_SLUG
282 );
283
284 // Site ID.
285 $field_id = 'apikey';
286 $field_args = array(
287 'option_key' => $field_id,
288 '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' ),
289 'label_for' => $field_id,
290 'optional_args' => array(
291 'required' => 'required',
292 'placeholder' => 'mydomain.com',
293 'disabled' => $are_credentials_managed,
294 ),
295
296 );
297 add_settings_field(
298 $field_id,
299 __( 'Site ID <em>(required)</em>', 'wp-parsely' ),
300 array( $this, 'print_text_tag' ),
301 Parsely::MENU_SLUG,
302 $section_key,
303 $field_args
304 );
305
306 if ( ! $are_credentials_managed ) {
307 // API Secret.
308 $field_id = 'api_secret';
309 $field_args = array(
310 'option_key' => $field_id,
311 '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' ),
312 'label_for' => $field_id,
313 'optional_args' => array(
314 'type' => 'password',
315 'is_obfuscated_value' => true,
316 ),
317 );
318 add_settings_field(
319 $field_id,
320 __( 'API Secret', 'wp-parsely' ),
321 array( $this, 'print_text_tag' ),
322 Parsely::MENU_SLUG,
323 $section_key,
324 $field_args
325 );
326 }
327
328 // Metadata Format.
329 $field_id = 'meta_type';
330 $field_args = array(
331 'title' => __( 'Metadata Format', 'wp-parsely' ),
332 'option_key' => $field_id,
333 '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' ),
334 'radio_options' => array(
335 'json_ld' => 'json_ld',
336 'repeated_metas' => 'repeated_metas',
337 ),
338 'label_for' => Parsely::OPTIONS_KEY . "[$field_id]",
339 'filter' => 'wp_parsely_metadata',
340 );
341 add_settings_field(
342 $field_id,
343 $this->set_field_label_contents( __( 'Metadata Format', 'wp-parsely' ), $field_id ),
344 array( $this, 'print_radio_tags' ),
345 Parsely::MENU_SLUG,
346 $section_key,
347 $field_args
348 );
349
350 // Logo.
351 $field_help = __( 'Here you can specify your logo\'s URL by using the "Browse" button or typing the URL manually.', 'wp-parsely' );
352 $field_id = 'logo';
353 add_settings_field(
354 $field_id,
355 $this->set_field_label_contents( __( 'Logo', 'wp-parsely' ), $field_id ),
356 array( $this, 'print_media_single_image' ),
357 Parsely::MENU_SLUG,
358 $section_key,
359 array(
360 'title' => __( 'Logo', 'wp-parsely' ), // Passed for legend element.
361 'option_key' => $field_id,
362 'label_for' => $field_id,
363 'help_text' => $field_help,
364 )
365 );
366
367 // Track logged-in users.
368 $field_id = 'track_authenticated_users';
369 add_settings_field(
370 $field_id,
371 $this->set_field_label_contents( __( 'Track Logged-in Users', 'wp-parsely' ), $field_id ),
372 array( $this, 'print_radio_tags' ),
373 Parsely::MENU_SLUG,
374 $section_key,
375 array(
376 'title' => __( 'Track Logged-in Users', 'wp-parsely' ), // Passed for legend element.
377 'option_key' => $field_id,
378 'radio_options' => array(
379 'true' => __( 'Yes, track logged-in users.', 'wp-parsely' ),
380 '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' ),
381 ),
382 'help_text' => (
383 is_multisite() ?
384 __( ' Note: For WordPress multisite, a user must be logged-in to the current site to be considered logged-in.', 'wp-parsely' ) :
385 null
386 ),
387 )
388 );
389
390 // Disable JavaScript.
391 $field_id = 'disable_javascript';
392 add_settings_field(
393 $field_id,
394 $this->set_field_label_contents( __( 'Disable JavaScript', 'wp-parsely' ), $field_id ),
395 array( $this, 'print_radio_tags' ),
396 Parsely::MENU_SLUG,
397 $section_key,
398 array(
399 'title' => __( 'Disable JavaScript', 'wp-parsely' ), // Passed for legend element.
400 'option_key' => $field_id,
401 'radio_options' => array(
402 'true' => __( 'Yes, disable JavaScript tracking. I want to use a separate system for tracking instead of the Parse.ly plugin.', 'wp-parsely' ),
403 'false' => __( 'No, do not disable JavaScript tracking. I want the Parse.ly plugin to load the tracker.', 'wp-parsely' ),
404 ),
405 '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' ),
406 'filter' => 'wp_parsely_load_js_tracker',
407 )
408 );
409
410 if ( defined( 'AMP__VERSION' ) ) {
411 // Disable AMP tracking.
412 $field_id = 'disable_amp';
413 add_settings_field(
414 $field_id,
415 $this->set_field_label_contents( __( 'Disable AMP Tracking', 'wp-parsely' ), $field_id ),
416 array( $this, 'print_radio_tags' ),
417 Parsely::MENU_SLUG,
418 $section_key,
419 array(
420 'title' => __( 'Disable AMP Tracking', 'wp-parsely' ), // Passed for legend element.
421 'option_key' => $field_id,
422 'radio_options' => array(
423 'true' => __( 'Yes, disable Parse.ly tracking on AMP pages. I use a different system for JavaScript tracking on AMP pages.', 'wp-parsely' ),
424 'false' => __( 'No, do not disable Parse.ly tracking on AMP pages.', 'wp-parsely' ),
425 ),
426 )
427 );
428 }
429 }
430
431 /**
432 * Registers the Content Helper section and its settings.
433 *
434 * @since 3.16.0
435 */
436 private function initialize_content_helper_section(): void {
437 $section_key = 'content-helper-section';
438
439 add_settings_section(
440 $section_key,
441 __( 'Content Helper', 'wp-parsely' ),
442 '__return_null',
443 Parsely::MENU_SLUG
444 );
445
446 // AI Features.
447 $field_id = 'content_helper[ai_features_enabled]';
448 $field_args = array(
449 'option_key' => $field_id,
450 'label_for' => $field_id,
451 'yes_text' => __( 'Enabled', 'wp-parsely' ),
452 'add_fieldset' => true,
453 'legend' => __( 'AI Features', 'wp-parsely' ),
454 );
455 add_settings_field(
456 $field_id,
457 __( 'AI Features', 'wp-parsely' ),
458 array( $this, 'print_checkbox_tag' ),
459 Parsely::MENU_SLUG,
460 $section_key,
461 $field_args
462 );
463
464 // Smart Linking.
465 $field_id = 'content_helper[smart_linking]';
466 $field_args = array(
467 'option_key' => $field_id,
468 'label_for' => $field_id,
469 'legend' => __( 'Smart Linking', 'wp-parsely' ),
470 );
471 add_settings_field(
472 $field_id,
473 __( 'Smart Linking', 'wp-parsely' ),
474 array( $this, 'print_content_helper_ai_feature_section' ),
475 Parsely::MENU_SLUG,
476 $section_key,
477 $field_args
478 );
479
480 // Title Suggestions.
481 $field_id = 'content_helper[title_suggestions]';
482 $field_args = array(
483 'option_key' => $field_id,
484 'label_for' => $field_id,
485 'legend' => __( 'Title Suggestions', 'wp-parsely' ),
486 );
487 add_settings_field(
488 $field_id,
489 __( 'Title Suggestions', 'wp-parsely' ),
490 array( $this, 'print_content_helper_ai_feature_section' ),
491 Parsely::MENU_SLUG,
492 $section_key,
493 $field_args
494 );
495
496 // Excerpt Suggestions.
497 $field_id = 'content_helper[excerpt_suggestions]';
498 $field_args = array(
499 'option_key' => $field_id,
500 'label_for' => $field_id,
501 'legend' => __( 'Excerpt Suggestions', 'wp-parsely' ),
502 'filter' => Excerpt_Generator::get_feature_filter_name(),
503 );
504 add_settings_field(
505 $field_id,
506 __( 'Excerpt Suggestions', 'wp-parsely' ),
507 array( $this, 'print_content_helper_ai_feature_section' ),
508 Parsely::MENU_SLUG,
509 $section_key,
510 $field_args
511 );
512 }
513
514 /**
515 * Registers section and settings for Recrawl section.
516 *
517 * @since 3.2.0
518 */
519 private function initialize_recrawl_section(): void {
520 $section_key = 'recrawl-section';
521
522 add_settings_section(
523 $section_key,
524 __( 'Recrawl', 'wp-parsely' ),
525 function (): void {
526 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 />';
527 printf(
528 /* translators: Mailto link */
529 esc_html__( 'Once you have changed a value and saved, please contact %s to request a recrawl.', 'wp-parsely' ),
530 wp_kses_post( '<a href="mailto:support@parsely.com?subject=' . rawurlencode( 'Please reprocess ' . $this->parsely->get_site_id() ) . '">support@parsely.com</a>' )
531 );
532 },
533 Parsely::MENU_SLUG
534 );
535
536 // Allow use of custom taxonomy to populate articleSection in parselyPage; defaults to category.
537 $field_id = 'track_post_types_as';
538 $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' );
539 add_settings_field(
540 $field_id,
541 __( 'Track Post Types as', 'wp-parsely' ),
542 array( $this, 'print_track_post_types_table' ),
543 Parsely::MENU_SLUG,
544 $section_key,
545 array(
546 'title' => __( 'Track Post Types as', 'wp-parsely' ),
547 'option_key' => $field_id,
548 'help_text' => $field_help,
549 'filter' => 'wp_parsely_trackable_statuses',
550 )
551 );
552
553 // Use full metadata in non-posts.
554 $field_id = 'full_metadata_in_non_posts';
555 add_settings_field(
556 $field_id,
557 $this->set_field_label_contents( __( 'Use Full Metadata in Non-Posts', 'wp-parsely' ), $field_id ),
558 array( $this, 'print_radio_tags' ),
559 Parsely::MENU_SLUG,
560 $section_key,
561 array(
562 'title' => __( 'Use Full Metadata in Non-Posts', 'wp-parsely' ), // Passed for legend element.
563 'option_key' => $field_id,
564 'radio_options' => array(
565 'true' => __( 'Yes, add full metadata to Post Types being tracked as Non-Posts.', 'wp-parsely' ),
566 'false' => __( 'No, we have code that modifies metadata and that has not been tested for compatibility yet.', 'wp-parsely' ),
567 ),
568 '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' ),
569 )
570 );
571
572 // Content ID Prefix.
573 $field_id = 'content_id_prefix';
574 $field_args = array(
575 'option_key' => $field_id,
576 'optional_args' => array(
577 'placeholder' => 'WP-',
578 ),
579 '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' ),
580 'label_for' => $field_id,
581 );
582 add_settings_field(
583 $field_id,
584 $this->set_field_label_contents( __( 'Content ID Prefix', 'wp-parsely' ), $field_id ),
585 array( $this, 'print_text_tag' ),
586 Parsely::MENU_SLUG,
587 $section_key,
588 $field_args
589 );
590
591 // Use top-level categories.
592 $field_id = 'use_top_level_cats';
593 add_settings_field(
594 $field_id,
595 $this->set_field_label_contents( __( 'Use Top-Level Categories for Section', 'wp-parsely' ), $field_id ),
596 array( $this, 'print_radio_tags' ),
597 Parsely::MENU_SLUG,
598 $section_key,
599 array(
600 'title' => __( 'Use Top-Level Categories for Section', 'wp-parsely' ), // Passed for legend element.
601 'option_key' => $field_id,
602 'radio_options' => array(
603 'true' => __( 'Yes, use the first category assigned to a post as the section name.', 'wp-parsely' ),
604 'false' => __( 'No, do not use the first category assigned to a post as the section name.', 'wp-parsely' ),
605 ),
606 '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' ),
607 )
608 );
609
610 // Allow use of custom taxonomy to populate articleSection in parselyPage; defaults to category.
611 $field_id = 'custom_taxonomy_section';
612 $field_args = array(
613 'option_key' => $field_id,
614 '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' ),
615 'select_options' => self::get_section_taxonomies(),
616 'label_for' => Parsely::OPTIONS_KEY . "[$field_id]",
617 );
618 add_settings_field(
619 $field_id,
620 $this->set_field_label_contents( __( 'Use Custom Taxonomy for Section', 'wp-parsely' ), $field_id ),
621 array( $this, 'print_select_tag' ),
622 Parsely::MENU_SLUG,
623 $section_key,
624 $field_args
625 );
626
627 // Use categories and custom taxonomies as tags.
628 $field_id = 'cats_as_tags';
629 add_settings_field(
630 $field_id,
631 $this->set_field_label_contents( __( 'Add Categories to Tags', 'wp-parsely' ), $field_id ),
632 array( $this, 'print_radio_tags' ),
633 Parsely::MENU_SLUG,
634 $section_key,
635 array(
636 'title' => __( 'Add Categories to Tags', 'wp-parsely' ), // Passed for legend element.
637 'option_key' => $field_id,
638 'radio_options' => array(
639 'true' => __( 'Yes, add all assigned categories and taxonomies to my tags.', 'wp-parsely' ),
640 'false' => __( 'No, do not add all assigned categories and taxonomies to my tags.', 'wp-parsely' ),
641 ),
642 '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' ),
643 )
644 );
645
646 // Lowercase all tags.
647 $field_id = 'lowercase_tags';
648 add_settings_field(
649 $field_id,
650 $this->set_field_label_contents( __( 'Lowercase All Tags', 'wp-parsely' ), $field_id ),
651 array( $this, 'print_radio_tags' ),
652 Parsely::MENU_SLUG,
653 $section_key,
654 array(
655 'title' => __( 'Lowercase All Tags', 'wp-parsely' ), // Passed for legend element.
656 'option_key' => $field_id,
657 'radio_options' => array(
658 'true' => __( 'Yes, use lowercase versions of my tags to correct for potential misspellings.', 'wp-parsely' ),
659 'false' => __( 'No, do not use lowercase versions of my tags to correct for potential misspellings.', 'wp-parsely' ),
660 ),
661 )
662 );
663
664 $field_id = 'force_https_canonicals';
665 add_settings_field(
666 $field_id,
667 $this->set_field_label_contents( __( 'Force HTTPS Canonicals', 'wp-parsely' ), $field_id ),
668 array( $this, 'print_radio_tags' ),
669 Parsely::MENU_SLUG,
670 $section_key,
671 array(
672 'title' => __( 'Force HTTPS Canonicals', 'wp-parsely' ), // Passed for legend element.
673 'option_key' => $field_id,
674 'radio_options' => array(
675 'true' => __( 'Yes, force <code>https</code> canonical URLs by default.', 'wp-parsely' ),
676 'false' => __( 'No, I want to use <code>http</code>.', 'wp-parsely' ),
677 ),
678 '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' ),
679 )
680 );
681 }
682
683 /**
684 * Registers section and settings for Advanced section.
685 *
686 * @since 3.2.0
687 */
688 private function initialize_advanced_section(): void {
689 $are_credentials_managed = $this->parsely->are_credentials_managed;
690 $section_key = 'advanced-section';
691
692 add_settings_section(
693 $section_key,
694 __( 'Advanced', 'wp-parsely' ),
695 '__return_null',
696 Parsely::MENU_SLUG
697 );
698
699 if ( ! $are_credentials_managed ) {
700 // Metadata Secret.
701 $field_id = 'metadata_secret';
702 $field_args = array(
703 'option_key' => $field_id,
704 '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' ),
705 'label_for' => $field_id,
706 'optional_args' => array(
707 'type' => 'password',
708 'is_obfuscated_value' => true,
709 ),
710 );
711 add_settings_field(
712 $field_id,
713 __( 'Metadata Secret', 'wp-parsely' ),
714 array( $this, 'print_text_tag' ),
715 Parsely::MENU_SLUG,
716 $section_key,
717 $field_args
718 );
719 }
720
721 // Disable autotrack.
722 $field_id = 'disable_autotrack';
723 add_settings_field(
724 $field_id,
725 $this->set_field_label_contents( __( 'Disable Autotracking', 'wp-parsely' ), $field_id ),
726 array( $this, 'print_radio_tags' ),
727 Parsely::MENU_SLUG,
728 $section_key,
729 array(
730 'title' => __( 'Disable Autotracking', 'wp-parsely' ), // Passed for legend element.
731 'option_key' => $field_id,
732 'radio_options' => array(
733 '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' ),
734 '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' ),
735 ),
736 )
737 );
738 }
739
740 /**
741 * Shows setting tabs.
742 *
743 * @since 3.8.0
744 */
745 public function show_setting_tabs(): void {
746 global $wp_settings_sections;
747 ?>
748
749 <nav class="nav-tab-wrapper">
750 <?php foreach ( $wp_settings_sections[ Parsely::MENU_SLUG ] as $section ) { ?>
751 <a
752 class="nav-tab <?php echo esc_attr( $section['id'] . '-tab' ); ?>"
753 href=<?php echo esc_url_raw( '?page=' . Parsely::MENU_SLUG . '#' . $section['id'] ); ?>
754 >
755 <?php echo esc_html( $section['title'] ); ?>
756 </a>
757 <?php } ?>
758 </nav>
759
760 <?php
761 }
762
763 /**
764 * Shows content of setting tabs.
765 *
766 * @since 3.8.0
767 */
768 public function show_setting_tabs_content(): void {
769 global $wp_settings_sections;
770
771 foreach ( $wp_settings_sections[ Parsely::MENU_SLUG ] as $section ) {
772 ?>
773
774 <div class="tab-content <?php echo esc_attr( $section['id'] ); ?>">
775 <?php
776 if ( $section['callback'] ) {
777 call_user_func( $section['callback'], $section );
778 }
779 ?>
780
781 <table class="form-table" role="presentation">
782 <?php do_settings_fields( Parsely::MENU_SLUG, $section['id'] ); ?>
783 </table>
784 </div>
785
786 <?php
787 }
788 }
789
790 /**
791 * Prints out a warning if the filter for the setting is defined, if any.
792 *
793 * @since 3.4.0
794 *
795 * @param Setting_Arguments $args The arguments for the form field. May contain 'filter'.
796 */
797 private function print_filter_text( $args ): void {
798 if ( isset( $args['filter'] ) && has_filter( $args['filter'] ) ) {
799 echo '<p>';
800 echo '<b><code>' . esc_html( $args['filter'] ) . '</code>' . esc_html__( 'filter hook is in use!', 'wp-parsely' ) . '</b> ';
801 echo esc_html__( 'A callback is attached to the filter hook that might interfere and override this setting.', 'wp-parsely' );
802 echo '</p>';
803 }
804 }
805
806 /**
807 * Prints out the description text, if there is any.
808 *
809 * @since 3.1.0
810 *
811 * @param Setting_Arguments $args The arguments for the form field. May contain 'help_text'.
812 */
813 private function print_description_text( $args ): void {
814 echo isset( $args['help_text'] ) ? '<p class="description" id="' . esc_attr( $args['option_key'] ) . '-description">' . wp_kses_post( $args['help_text'] ) . '</p>' : '';
815 }
816
817 /**
818 * Prints out an input text tag.
819 *
820 * @param Setting_Arguments $args The arguments for text tag.
821 */
822 public function print_text_tag( $args ): void {
823 $options = $this->parsely->get_options();
824 $name = $args['option_key'];
825 /**
826 * Variable.
827 *
828 * @var string
829 */
830 $value = $options[ $name ] ?? '';
831 $optional_args = $args['optional_args'] ?? array();
832 $id = esc_attr( $name );
833 $name = Parsely::OPTIONS_KEY . "[$id]";
834 $is_obfuscated_value = $optional_args['is_obfuscated_value'] ?? false;
835 $value = $is_obfuscated_value ? $this->get_obfuscated_value( $value ) : esc_attr( $value );
836 $accepted_args = array( 'placeholder', 'required', 'disabled' );
837 $type = $optional_args['type'] ?? 'text';
838
839 $is_managed = key_exists( $id, $this->parsely->managed_options );
840 echo '<fieldset', $is_managed ? ' disabled>' : '>';
841 printf( "<input type='%s' name='%s' id='%s' value='%s'", esc_attr( $type ), esc_attr( $name ), esc_attr( $id ), esc_attr( $value ) );
842
843 if ( isset( $args['help_text'] ) ) {
844 echo ' aria-describedby="' . esc_attr( $id ) . '-description"';
845 }
846
847 foreach ( $optional_args as $key => $val ) {
848 if ( \in_array( $key, $accepted_args, true ) && false !== $val ) {
849 // Don't add a placeholder to managed option fields.
850 if ( $is_managed && 'placeholder' === $key ) {
851 continue;
852 }
853
854 // Support attributes without values.
855 echo ' ' . esc_attr( $key );
856 if ( true !== $val ) {
857 echo '="' . esc_attr( $val ) . '"';
858 }
859 }
860 }
861 echo ' /></fieldset>';
862
863 $this->print_description_text( $args );
864 }
865
866 /**
867 * Prints a checkbox tag.
868 *
869 * @since 3.16.0
870 *
871 * @param Setting_Arguments $args Arguments for the checkbox tag.
872 * @param Parsely_Options|null $options The options to use.
873 */
874 public function print_checkbox_tag( $args, $options = null ): void {
875 $options = $options ?? $this->parsely->get_options();
876 $name = $args['option_key'];
877 $has_fieldset = isset( $args['add_fieldset'] ) && true === $args['add_fieldset'];
878 $html_id = rtrim( str_replace( array( '[', ']', '__' ), '_', $name ), '_' );
879 $html_name = str_replace(
880 'content_helper',
881 '[content_helper]',
882 Parsely::OPTIONS_KEY . esc_attr( $name )
883 );
884 $yes_text = $args['yes_text'] ?? '';
885
886 // Get option value.
887 if ( false === strpos( $name, '[' ) ) {
888 $value = $options[ $name ];
889 } else {
890 $value = Parsely::get_nested_option_value( $name, $options );
891 }
892
893 // Fieldset start.
894 if ( $has_fieldset ) {
895 echo '<fieldset>';
896 if ( isset( $args['legend'] ) ) {
897 echo '<legend class="screen-reader-text"><span>';
898 echo esc_html( $args['legend'] ) . '</span></legend>';
899 }
900 }
901
902 // Label and contained checkbox.
903 printf( '<label for="%s">', esc_attr( $html_id ) );
904 printf(
905 '<input type="checkbox" name="%s" id="%s" value="true" ',
906 esc_attr( $html_name ),
907 esc_attr( $html_id )
908 );
909 if ( isset( $args['help_text'] ) ) {
910 echo ' aria-describedby="' . esc_attr( $html_id ) . '-description"';
911 }
912 checked( true === $value, true );
913 printf( ' /> %s</label>', esc_html( $yes_text ) );
914
915 // Fieldset end.
916 if ( $has_fieldset ) {
917 echo '</fieldset>';
918 }
919
920 $this->print_description_text( $args );
921 }
922
923 /**
924 * Prints a Content Helper AI feature section.
925 *
926 * @since 3.16.0
927 *
928 * @param Setting_Arguments $args The arguments for the section.
929 */
930 public function print_content_helper_ai_feature_section( $args ): void {
931 $options = $this->parsely->get_options();
932 $feature_id = str_replace(
933 array( 'content_helper[', ']' ),
934 '',
935 $args['option_key']
936 );
937
938 // Convert user role settings to make them work in the settings page.
939 $option = &$options['content_helper'][ $feature_id ];
940 if ( isset( $option['allowed_user_roles'] ) ) {
941 foreach ( $option['allowed_user_roles'] as $role ) {
942 $option['allowed_user_roles'][ $role ] = true;
943 }
944 }
945
946 // Feature "Enabled" checkbox.
947 $enabled_args = array(
948 'option_key' => $args['option_key'] . '[enabled]',
949 'label_for' => $args['option_key'] . '[enabled]',
950 'yes_text' => __( 'Enabled', 'wp-parsely' ),
951 'add_fieldset' => true,
952 );
953 if ( isset( $args['legend'] ) ) {
954 $enabled_args['legend'] = $args['legend'];
955 }
956 $this->print_checkbox_tag( $enabled_args, $options );
957
958 // User role permissions fieldset.
959 echo '<p>' . esc_html__( 'User Permissions', 'wp-parsely' ) . '</p>';
960 echo '<fieldset class="user-role-permissions">';
961 echo '<legend class="screen-reader-text"><span>';
962 printf(
963 /* translators: %s: Feature name */
964 esc_html__( '%s User Permissions', 'wp-parsely' ),
965 esc_html( $args['legend'] ?? __( 'Feature', 'wp-parsely' ) )
966 );
967 echo '</span></legend>';
968
969 // User role checkboxes.
970 $user_roles = Permissions::get_user_roles_with_edit_posts_cap();
971 foreach ( $user_roles as $key => $name ) {
972 $option_key = $args['option_key'] . '[allowed_user_roles][' . $key . ']';
973 $role_args = array(
974 'option_key' => $option_key,
975 'label_for' => $option_key,
976 'yes_text' => translate_user_role( $name ),
977 );
978
979 $this->print_checkbox_tag( $role_args, $options );
980 }
981
982 echo '</fieldset>';
983
984 $this->print_filter_text( $args );
985 }
986
987 /**
988 * Prints out the select tags
989 *
990 * @param Setting_Arguments $args The arguments for the select dropdowns.
991 */
992 public function print_select_tag( $args ): void {
993 $options = $this->parsely->get_options();
994 $name = $args['option_key'];
995 $select_options = $args['select_options'] ?? array();
996 $selected = $options[ $name ] ?? null;
997 $id = esc_attr( $name );
998 $name = Parsely::OPTIONS_KEY . "[$id]";
999
1000 $is_managed = key_exists( $id, $this->parsely->managed_options );
1001 echo '<fieldset', $is_managed ? ' disabled>' : '>';
1002 printf( "<select name='%s' id='%s'", esc_attr( $name ), esc_attr( $name ) );
1003 if ( isset( $args['help_text'] ) ) {
1004 echo ' aria-describedby="' . esc_attr( $id ) . '-description"';
1005 }
1006 echo '>';
1007
1008 foreach ( $select_options as $key => $val ) {
1009 echo '<option value="' . esc_attr( $key ) . '" ';
1010 echo selected( $selected, $key, false ) . '>';
1011 echo esc_html( $val );
1012 echo '</option>';
1013 }
1014 echo '</select></fieldset>';
1015
1016 $this->print_filter_text( $args );
1017 $this->print_description_text( $args );
1018 }
1019
1020 /**
1021 * Prints the radio buttons.
1022 *
1023 * @param Setting_Arguments $args The arguments for the radio buttons.
1024 */
1025 public function print_radio_tags( $args ): void {
1026 $name = $args['option_key'];
1027 $id = esc_attr( $name );
1028 $selected = $this->parsely->get_options()[ $name ];
1029 $title = $args['title'] ?? '';
1030 $radio_options = $args['radio_options'] ?? array();
1031
1032 if ( is_bool( $selected ) ) {
1033 // Converting boolean to string so that we have string type keys for all cases.
1034 $selected = $selected ? 'true' : 'false';
1035 }
1036
1037 $is_managed = key_exists( $name, $this->parsely->managed_options );
1038 ?>
1039 <fieldset <?php echo $is_managed ? 'disabled' : ''; ?>>
1040 <legend class="screen-reader-text"><span><?php echo esc_html( $title ); ?></span></legend>
1041 <p>
1042 <?php foreach ( $radio_options as $value => $text ) { ?>
1043 <label for="<?php echo esc_attr( "{$id}_{$value}" ); ?>">
1044 <input
1045 type="radio"
1046 name="<?php echo esc_attr( Parsely::OPTIONS_KEY . "[$id]" ); ?>"
1047 id="<?php echo esc_attr( "{$id}_{$value}" ); ?>"
1048 value="<?php echo esc_attr( $value ); ?>"
1049 <?php checked( $selected, $value ); ?>
1050 />
1051 <?php echo wp_kses_post( $text ); ?>
1052 </label>
1053 <br />
1054 <?php } ?>
1055 </p>
1056 </fieldset>
1057 <?php
1058 $this->print_filter_text( $args );
1059 $this->print_description_text( $args );
1060 }
1061
1062 /**
1063 * Prints out a "single-image browse control" which includes a text input to
1064 * store image path and a button to browse for images.
1065 *
1066 * @param Setting_Arguments $args The arguments for the control.
1067 */
1068 public function print_media_single_image( $args ): void {
1069 $key = $args['option_key'];
1070 $title = $args['title'] ?? '';
1071 /**
1072 * Variable.
1073 *
1074 * @var string
1075 */
1076 $input_value = $this->parsely->get_options()[ $key ];
1077 $input_name = Parsely::OPTIONS_KEY . "[$key]";
1078 $button_text = __( 'Browse', 'wp-parsely' );
1079
1080 $is_managed = key_exists( $key, $this->parsely->managed_options );
1081 ?>
1082
1083 <fieldset class="media-single-image" id="media-single-image-<?php echo esc_attr( $key ); ?>"<?php echo $is_managed ? ' disabled' : ''; ?>>
1084 <legend class="screen-reader-text"><span><?php echo esc_html( $title ); ?></span></legend>
1085 <input class="file-path" type="text" name="<?php echo esc_attr( $input_name ); ?>" id="logo" value="<?php echo esc_attr( $input_value ); ?>" />
1086 <button data-option="<?php echo esc_attr( $key ); ?>" class="browse button" type="button"><?php echo esc_html( $button_text ); ?></button>
1087 </fieldset>
1088
1089 <?php
1090 $this->print_description_text( $args );
1091 }
1092
1093 /**
1094 * Prints out the post tracking options table.
1095 *
1096 * @since 3.2.0
1097 *
1098 * @param Setting_Arguments $args The arguments used in the output HTML elements.
1099 */
1100 public function print_track_post_types_table( $args ): void {
1101 $option_key = esc_attr( $args['option_key'] );
1102 $title = $args['title'] ?? '';
1103 /**
1104 * Variable.
1105 *
1106 * @var array<string>
1107 */
1108 $post_types = get_post_types( array( 'public' => true ) );
1109 $values = $this->get_tracking_values_for_display();
1110 ?>
1111 <fieldset>
1112 <legend class="screen-reader-text"><span><?php echo esc_html( $title ); ?></span></legend>
1113 <table class="form-table widefat striped" id="track-post-types">
1114 <caption class="screen-reader-text"><?php echo esc_html( $title ); ?></caption>
1115 <thead>
1116 <tr>
1117 <th scope="col"><?php echo esc_html__( 'Post Type', 'wp-parsely' ); ?></th>
1118 <th id="track-post-types--post" scope="col"><?php echo esc_html__( 'Track as Post', 'wp-parsely' ); ?></th>
1119 <th id="track-post-types--page" scope="col"><?php echo esc_html__( 'Track as Non-Post', 'wp-parsely' ); ?></th>
1120 <th id="track-post-types--none" scope="col"><?php echo esc_html__( 'Do not track', 'wp-parsely' ); ?></th>
1121 </tr>
1122 </thead>
1123 <tbody>
1124 <?php
1125 foreach ( $post_types as $post_type ) {
1126 $group_name = "parsely[{$option_key}][{$post_type}]";
1127 $id_post = "{$option_key}_{$post_type}_post";
1128 $id_page = "{$option_key}_{$post_type}_page";
1129 $id_none = "{$option_key}_{$post_type}_none";
1130 $value = $values[ $post_type ] ?? 'none';
1131 ?>
1132 <tr>
1133 <th scope="row"><?php echo esc_html( $post_type ); ?></th>
1134 <td>
1135 <label aria-labelledby="track-post-types--post" for="<?php echo esc_attr( $id_post ); ?>">
1136 <input id="<?php echo esc_attr( $id_post ); ?>" name="<?php echo esc_attr( $group_name ); ?>" type="radio" value="post" <?php checked( $value, 'post' ); ?> />
1137 </label>
1138 </td>
1139 <td>
1140 <label aria-labelledby="track-post-types--page" for="<?php echo esc_attr( $id_page ); ?>">
1141 <input id="<?php echo esc_attr( $id_page ); ?>" name="<?php echo esc_attr( $group_name ); ?>" type="radio" value="page" <?php checked( $value, 'page' ); ?> />
1142 </label>
1143 </td>
1144 <td>
1145 <label aria-labelledby="track-post-types--none" for="<?php echo esc_attr( $id_none ); ?>">
1146 <input id="<?php echo esc_attr( $id_none ); ?>" name="<?php echo esc_attr( $group_name ); ?>" type="radio" value="none" <?php checked( $value, 'none' ); ?> />
1147 </label>
1148 </td>
1149 </tr>
1150 <?php } ?>
1151 </tbody>
1152 </table>
1153 </fieldset>
1154 <?php
1155 $this->print_filter_text( $args );
1156 $this->print_description_text( $args );
1157 }
1158
1159 /**
1160 * Returns the custom post type tracking values in a format that is easily
1161 * consumable by the print_track_post_types_table() function.
1162 *
1163 * @since 3.2.0
1164 *
1165 * @return array<string> Key-value pairs with post type and their 'track as' value.
1166 */
1167 public function get_tracking_values_for_display(): array {
1168 $options = $this->parsely->get_options();
1169 $types = array( 'post', 'page' );
1170 $result = array();
1171
1172 foreach ( $types as $type ) {
1173 $array_value = $options[ "track_{$type}_types" ];
1174
1175 foreach ( $array_value as $post_type ) {
1176 $result[ $post_type ] = $type;
1177 }
1178 }
1179
1180 return $result;
1181 }
1182
1183 /**
1184 * Validates the options provided by the user.
1185 *
1186 * @param ParselySettingOptions $input Options from the settings page.
1187 * @return ParselySettingOptions
1188 */
1189 public function validate_options( $input ) {
1190 $input = $this->validate_basic_section( $input );
1191 $input = $this->validate_content_helper_section( $input );
1192 $input = $this->validate_recrawl_section( $input );
1193 $input = $this->validate_advanced_section( $input );
1194
1195 return $input;
1196 }
1197
1198 /**
1199 * Validates fields of Basic Section.
1200 *
1201 * @param ParselySettingOptions $input Options from the settings page.
1202 * @return ParselySettingOptions Validated inputs.
1203 */
1204 private function validate_basic_section( $input ) {
1205 $are_credentials_managed = $this->parsely->are_credentials_managed;
1206 $options = $this->parsely->get_options();
1207
1208 if ( $are_credentials_managed ) {
1209 $input['apikey'] = '';
1210 $input['api_secret'] = '';
1211 } else {
1212 $site_id = $this->sanitize_site_id( $input['apikey'] );
1213 $api_secret = $this->get_unobfuscated_value( $input['api_secret'], $this->parsely->get_api_secret() );
1214
1215 $valid_credentials = Validator::validate_api_credentials( $this->parsely, $site_id, $api_secret );
1216
1217 // When running e2e tests, we need to update the API keys without validating them, as they will be invalid.
1218 // phpcs:ignore WordPress.Security.NonceVerification.Missing
1219 if ( isset( $_POST['e2e_parsely_skip_api_validate'] ) && 'y' === $_POST['e2e_parsely_skip_api_validate'] ) {
1220 $valid_credentials = true;
1221 }
1222
1223 if ( is_wp_error( $valid_credentials ) && Validator::INVALID_API_CREDENTIALS === $valid_credentials->get_error_code() ) {
1224 add_settings_error(
1225 Parsely::OPTIONS_KEY,
1226 'api_secret',
1227 __( 'The Site ID and API Secret weren\'t saved as they failed to authenticate with the Parse.ly API. Try again with different credentials or contact Parse.ly support', 'wp-parsely' )
1228 );
1229 $input['apikey'] = $options['apikey'];
1230 $input['api_secret'] = $options['api_secret'];
1231 }
1232
1233 // Since the API secret is obfuscated, we need to make sure that the value
1234 // is not changed when the credentials are valid.
1235 if ( true === $valid_credentials && $input['api_secret'] !== $api_secret ) {
1236 $input['api_secret'] = $api_secret;
1237 }
1238 }
1239
1240 if ( ! isset( $input['meta_type'] ) ) {
1241 $input['meta_type'] = $options['meta_type'];
1242 } else {
1243 $input['meta_type'] = sanitize_text_field( $input['meta_type'] );
1244 }
1245
1246 if ( '' === $input['logo'] ) {
1247 $input['logo'] = self::get_logo_default();
1248 }
1249
1250 // Track authenticated users.
1251 if ( 'true' !== $input['track_authenticated_users'] && 'false' !== $input['track_authenticated_users'] ) {
1252 add_settings_error(
1253 Parsely::OPTIONS_KEY,
1254 'track_authenticated_users',
1255 __( 'Value passed for track_authenticated_users must be either "true" or "false".', 'wp-parsely' )
1256 );
1257 } else {
1258 $input['track_authenticated_users'] = 'true' === $input['track_authenticated_users'];
1259 }
1260
1261 if ( 'true' !== $input['disable_javascript'] && 'false' !== $input['disable_javascript'] ) {
1262 add_settings_error(
1263 Parsely::OPTIONS_KEY,
1264 'disable_javascript',
1265 __( 'Value passed for disable_javascript must be either "Yes" or "No".', 'wp-parsely' )
1266 );
1267 } else {
1268 $input['disable_javascript'] = 'true' === $input['disable_javascript'];
1269 }
1270
1271 // Allow for Disable AMP setting to be conditionally included on the page.
1272 // If it's not shown, then set the value as what was previously saved.
1273 if ( ! isset( $input['disable_amp'] ) ) {
1274 $input['disable_amp'] = 'true';
1275 if ( false === $options['disable_amp'] ) {
1276 $input['disable_amp'] = 'false';
1277 }
1278 }
1279
1280 if ( 'true' !== $input['disable_amp'] && 'false' !== $input['disable_amp'] ) {
1281 add_settings_error(
1282 Parsely::OPTIONS_KEY,
1283 'disable_amp',
1284 __( 'Value passed for disable_amp must be either "true" or "false".', 'wp-parsely' )
1285 );
1286 } else {
1287 $input['disable_amp'] = 'true' === $input['disable_amp'];
1288 }
1289
1290 return $input;
1291 }
1292
1293 /**
1294 * Validates the fields of the Content Helper section.
1295 *
1296 * @since 3.16.0
1297 *
1298 * @param ParselySettingOptions $input The settings page options.
1299 * @return ParselySettingOptions The validated input.
1300 */
1301 private function validate_content_helper_section( $input ) {
1302 /**
1303 * Sanitizes the Content Helper data.
1304 *
1305 * @since 3.16.0
1306 */
1307 $sanitize = function ( $input ) use ( &$sanitize ) {
1308 foreach ( $input as $key => $value ) {
1309 if ( is_array( $value ) ) {
1310 if ( 'allowed_user_roles' === $key && count( $input[ $key ] ) > 0 ) {
1311 $passed_roles = array_keys( $input[ $key ] );
1312 $valid_roles = array_keys(
1313 Permissions::get_user_roles_with_edit_posts_cap()
1314 );
1315 $sanitized_roles = array();
1316
1317 // Sanitize passed user roles and remove invalid ones.
1318 foreach ( $passed_roles as $user_role ) {
1319 if ( ! is_string( $user_role ) ) {
1320 continue;
1321 }
1322
1323 $user_role = sanitize_text_field( $user_role );
1324 if ( in_array( $user_role, $valid_roles, true ) ) {
1325 $sanitized_roles[] = $user_role;
1326 }
1327 }
1328
1329 $input[ $key ] = $sanitized_roles;
1330 } else {
1331 // Recurse when we have an array that's not user roles.
1332 $input[ $key ] = $sanitize( $value );
1333 }
1334 } else {
1335 // Enforce a boolean value.
1336 $input[ $key ] = 'true' === $value || true === $value;
1337 }
1338 }
1339
1340 return $input;
1341 };
1342
1343 // Add any missing data due to unchecked checkboxes.
1344 if ( ! isset( $input['content_helper']['ai_features_enabled'] ) ) {
1345 $input['content_helper']['ai_features_enabled'] = false;
1346 }
1347 foreach ( $this->configurable_pch_features as $feature_id ) {
1348 if ( ! isset( $input['content_helper'][ $feature_id ] ) ) {
1349 $input['content_helper'][ $feature_id ] = array(
1350 'enabled' => false,
1351 'allowed_user_roles' => array(),
1352 );
1353 } else {
1354 if ( ! isset( $input['content_helper'][ $feature_id ]['enabled'] ) ) {
1355 // @phpstan-ignore-next-line
1356 $input['content_helper'][ $feature_id ]['enabled'] = false;
1357 }
1358 if ( ! isset( $input['content_helper'][ $feature_id ]['allowed_user_roles'] ) ) {
1359 // @phpstan-ignore-next-line
1360 $input['content_helper'][ $feature_id ]['allowed_user_roles'] = array();
1361 }
1362 }
1363 }
1364
1365 // Produce the final array.
1366 $options = $this->parsely->get_options()['content_helper'];
1367 $merged = array_merge( $options, $input['content_helper'] );
1368
1369 $input['content_helper'] = $sanitize( $merged );
1370
1371 return $input;
1372 }
1373
1374 /**
1375 * Validates fields of Recrawl Section.
1376 *
1377 * @param ParselySettingOptions $input Options from the settings page.
1378 * @return ParselySettingOptions Validated inputs.
1379 */
1380 private function validate_recrawl_section( $input ) {
1381 $options = $this->parsely->get_options();
1382
1383 $this->validate_options_post_type_tracking( $input );
1384
1385 // Content ID prefix.
1386 if ( ! isset( $input['content_id_prefix'] ) ) {
1387 $input['content_id_prefix'] = $options['content_id_prefix'];
1388 } else {
1389 $input['content_id_prefix'] = sanitize_text_field( $input['content_id_prefix'] );
1390 }
1391
1392 // Full metadata in non-posts.
1393 if ( ! isset( $input['full_metadata_in_non_posts'] ) ) {
1394 $input['full_metadata_in_non_posts'] = true;
1395 } else {
1396 // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
1397 $input['full_metadata_in_non_posts'] = 'true' == $input['full_metadata_in_non_posts'];
1398 }
1399
1400 // Allow for Top-level categories setting to be conditionally included on the page.
1401 // If it's not shown, then set the value as what was previously saved.
1402 if ( ! isset( $input['use_top_level_cats'] ) ) {
1403 $input['use_top_level_cats'] = 'true';
1404 if ( false === $options['use_top_level_cats'] ) {
1405 $input['use_top_level_cats'] = 'false';
1406 }
1407 }
1408
1409 // Top-level categories.
1410 if ( 'true' !== $input['use_top_level_cats'] && 'false' !== $input['use_top_level_cats'] ) {
1411 add_settings_error(
1412 Parsely::OPTIONS_KEY,
1413 'use_top_level_cats',
1414 __( 'Value passed for use_top_level_cats must be either "true" or "false".', 'wp-parsely' )
1415 );
1416 } else {
1417 $input['use_top_level_cats'] = 'true' === $input['use_top_level_cats'];
1418 }
1419
1420 // Custom taxonomy as section.
1421 if ( ! isset( $input['custom_taxonomy_section'] ) ) {
1422 $input['custom_taxonomy_section'] = $options['custom_taxonomy_section'];
1423 } else {
1424 $input['custom_taxonomy_section'] = sanitize_text_field( $input['custom_taxonomy_section'] );
1425 }
1426
1427 // Allow for Categories as Tags setting to be conditionally included on the page.
1428 // If it's not shown, then set the value as what was previously saved.
1429 if ( ! isset( $input['cats_as_tags'] ) ) {
1430 $input['cats_as_tags'] = 'true';
1431 if ( false === $options['cats_as_tags'] ) {
1432 $input['cats_as_tags'] = 'false';
1433 }
1434 }
1435
1436 // Child categories as tags.
1437 if ( 'true' !== $input['cats_as_tags'] && 'false' !== $input['cats_as_tags'] ) {
1438 add_settings_error(
1439 Parsely::OPTIONS_KEY,
1440 'cats_as_tags',
1441 __( 'Value passed for cats_as_tags must be either "true" or "false".', 'wp-parsely' )
1442 );
1443 } else {
1444 $input['cats_as_tags'] = 'true' === $input['cats_as_tags'];
1445 }
1446
1447 // Allow for Lowercase Tags setting to be conditionally included on the page.
1448 // If it's not shown, then set the value as what was previously saved.
1449 if ( ! isset( $input['lowercase_tags'] ) ) {
1450 $input['lowercase_tags'] = 'true';
1451 if ( false === $options['lowercase_tags'] ) {
1452 $input['lowercase_tags'] = 'false';
1453 }
1454 }
1455
1456 // Lowercase tags.
1457 if ( 'true' !== $input['lowercase_tags'] && 'false' !== $input['lowercase_tags'] ) {
1458 add_settings_error(
1459 Parsely::OPTIONS_KEY,
1460 'lowercase_tags',
1461 __( 'Value passed for lowercase_tags must be either "true" or "false".', 'wp-parsely' )
1462 );
1463 } else {
1464 $input['lowercase_tags'] = 'true' === $input['lowercase_tags'];
1465 }
1466
1467 // Allow for Force HTTPS Canonical setting to be conditionally included on the page.
1468 // If it's not shown, then set the value as what was previously saved.
1469 if ( ! isset( $input['force_https_canonicals'] ) ) {
1470 $input['force_https_canonicals'] = 'true';
1471 if ( false === $options['force_https_canonicals'] ) {
1472 $input['force_https_canonicals'] = 'false';
1473 }
1474 }
1475
1476 if ( 'true' !== $input['force_https_canonicals'] && 'false' !== $input['force_https_canonicals'] ) {
1477 add_settings_error(
1478 Parsely::OPTIONS_KEY,
1479 'force_https_canonicals',
1480 __( 'Value passed for force_https_canonicals must be either "true" or "false".', 'wp-parsely' )
1481 );
1482 } else {
1483 $input['force_https_canonicals'] = 'true' === $input['force_https_canonicals'];
1484 }
1485
1486 return $input;
1487 }
1488
1489 /**
1490 * Validates fields of Advanced Section.
1491 *
1492 * @param ParselySettingOptions $input Options from the settings page.
1493 * @return ParselySettingOptions Validated inputs.
1494 */
1495 private function validate_advanced_section( $input ) {
1496 $are_credentials_managed = $this->parsely->are_credentials_managed;
1497 $options = $this->parsely->get_options();
1498
1499 if ( $are_credentials_managed ) {
1500 $input['metadata_secret'] = '';
1501 } else {
1502 $input['metadata_secret'] = $this->get_unobfuscated_value( $input['metadata_secret'], $this->parsely->get_options()['metadata_secret'] );
1503 $metadata_secret_length = strlen( $input['metadata_secret'] );
1504 if ( $metadata_secret_length > 0 &&
1505 false === Validator::validate_metadata_secret( $input['metadata_secret'] ) ) {
1506 add_settings_error(
1507 Parsely::OPTIONS_KEY,
1508 'metadata_secret',
1509 __( 'The Metadata Secret was not saved because it is incorrect. Please contact Parse.ly support!', 'wp-parsely' )
1510 );
1511 $input['metadata_secret'] = $options['metadata_secret'];
1512 }
1513 }
1514
1515 if ( ! isset( $input['disable_autotrack'] ) ) {
1516 $input['disable_autotrack'] = $options['disable_autotrack'];
1517 } elseif ( 'true' !== $input['disable_autotrack'] && 'false' !== $input['disable_autotrack'] ) {
1518 add_settings_error(
1519 Parsely::OPTIONS_KEY,
1520 'disable_autotrack',
1521 __( 'Value passed for disable_autotrack must be either "Yes" or "No".', 'wp-parsely' )
1522 );
1523 } else {
1524 $input['disable_autotrack'] = 'true' === $input['disable_autotrack'];
1525 }
1526
1527 return $input;
1528 }
1529
1530 /**
1531 * Sanitizes the passed Site ID.
1532 *
1533 * @since 3.3.0
1534 *
1535 * @param string $site_id The Site ID to be sanitized.
1536 * @return string
1537 */
1538 private function sanitize_site_id( string $site_id ): string {
1539 return strtolower( sanitize_text_field( $site_id ) );
1540 }
1541
1542 /**
1543 * Receives the $input array from the validate_options() function and
1544 * validate post tracking options.
1545 *
1546 * This function will mutate the $input array.
1547 *
1548 * @since 3.2.0
1549 *
1550 * @param ParselySettingOptions $input Array passed to validate_options() function.
1551 */
1552 private function validate_options_post_type_tracking( &$input ): void {
1553 $options = $this->parsely->get_options();
1554 $posts = 'track_post_types';
1555 $pages = 'track_page_types';
1556 $track_as = 'track_post_types_as';
1557 $input[ $posts ] = $options[ $posts ];
1558 $input[ $pages ] = $options[ $pages ];
1559
1560 // @phpstan-ignore-next-line
1561 if ( isset( $input[ $track_as ] ) && is_array( $input[ $track_as ] ) && 0 < count( $input[ $track_as ] ) ) {
1562 $post_types = get_post_types( array( 'public' => true ) );
1563 $temp_posts = array();
1564 $temp_pages = array();
1565
1566 // Create temporary Post and Page arrays, disallowing non-existent post types.
1567 foreach ( $input[ $track_as ] as $key => $value ) {
1568 if ( false === in_array( $key, $post_types, true ) ) {
1569 continue;
1570 }
1571
1572 if ( 'post' === $value ) {
1573 $temp_posts[] = $key;
1574 } elseif ( 'page' === $value ) {
1575 $temp_pages[] = $key;
1576 }
1577 }
1578
1579 // Cleanup and sanitized values assignment.
1580 $input [ $posts ] = self::sanitize_option_array( $temp_posts );
1581 $input [ $pages ] = self::sanitize_option_array( $temp_pages );
1582 }
1583
1584 if ( isset( $input[ $track_as ] ) ) {
1585 unset( $input[ $track_as ] );
1586 }
1587 }
1588
1589 /**
1590 * Returns default logo if one can be found.
1591 *
1592 * @return string
1593 */
1594 private static function get_logo_default(): string {
1595 /**
1596 * Variable.
1597 *
1598 * @var int
1599 */
1600 $custom_logo_id = get_theme_mod( 'custom_logo' );
1601 if ( (bool) $custom_logo_id ) {
1602 $logo_attrs = wp_get_attachment_image_src( $custom_logo_id, 'full' );
1603 if ( isset( $logo_attrs[0] ) ) {
1604 return $logo_attrs[0];
1605 }
1606 }
1607
1608 // get_site_icon_url returns an empty string if one isn't found,
1609 // which is what we want to use as the default anyway.
1610 return get_site_icon_url();
1611 }
1612
1613 /**
1614 * Sanitizes all elements in an option array.
1615 *
1616 * @param array<int, string> $options Array of options to be sanitized.
1617 * @return array<int, string>
1618 */
1619 private static function sanitize_option_array( array $options ): array {
1620 $sanitized_options = $options;
1621 foreach ( $options as $key => $val ) {
1622 $sanitized_options[ $key ] = sanitize_text_field( $val );
1623 }
1624
1625 return $sanitized_options;
1626 }
1627
1628 /**
1629 * Gets obfuscated value.
1630 *
1631 * @param string $current_value Current value of the field.
1632 * @return string
1633 */
1634 private function get_obfuscated_value( $current_value ): string {
1635 return str_repeat( '*', strlen( $current_value ) );
1636 }
1637
1638 /**
1639 * Gets unobfuscated value.
1640 *
1641 * @param string $current_value Current value of the field.
1642 * @param string $previous_value Previous value of the field. If current
1643 * value is obfuscated then we will use this.
1644 * @return string
1645 */
1646 private function get_unobfuscated_value( $current_value, $previous_value ): string {
1647 if ( $current_value === $this->get_obfuscated_value( $current_value ) ) {
1648 return '' === $current_value ? $current_value : $previous_value;
1649 }
1650
1651 return $current_value;
1652 }
1653
1654 /**
1655 * Returns the field label's title. If the option is managed, a badge is
1656 * also included.
1657 *
1658 * @since 3.9.0
1659 *
1660 * @param string $title The field's title.
1661 * @param string $option_id The option's ID.
1662 * @return string The resulting content.
1663 */
1664 public function set_field_label_contents( string $title, string $option_id ): string {
1665 $is_managed = key_exists( $option_id, $this->parsely->managed_options );
1666
1667 if ( $is_managed ) {
1668 $text = $this->managed_options_badge['text'] ?? '';
1669 if ( ! is_string( $text ) || '' === $text ) {
1670 return $title;
1671 }
1672
1673 $url = filter_var( $this->managed_options_badge['url'] ?? '', FILTER_VALIDATE_URL );
1674
1675 if ( false === $url ) {
1676 $badge = '<span class="managed-option-badge">' . esc_html( $text ) . '</span>';
1677 } else {
1678 $badge = '<a class="managed-option-badge" href="' . esc_url( $url ) .
1679 '" target="_blank" rel="noopener">' . esc_html( $text ) . '</a>';
1680 }
1681
1682 return "{$title}&nbsp;&nbsp;{$badge}";
1683 }
1684
1685 return $title;
1686 }
1687
1688 /**
1689 * Returns the taxonomies allowed to be used as custom_taxonomy_section
1690 * option values.
1691 *
1692 * @since 3.9.0
1693 *
1694 * @return array<string> Array of taxonomies.
1695 */
1696 public static function get_section_taxonomies(): array {
1697 return array_diff(
1698 get_taxonomies(),
1699 array(
1700 'post_tag',
1701 'nav_menu',
1702 'author',
1703 'link_category',
1704 'post_format',
1705 'wp_theme',
1706 'wp_template_part_area',
1707 )
1708 );
1709 }
1710 }
1711