PluginProbe
Parse.ly / 3.14.1
Parse.ly v3.14.1
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
← All changes | src/UI/class-settings-page.php +36 -332 3.16.33.14.1 View file →
@@ -9,11 +9,9 @@
9 9 declare(strict_types=1);
10 10
11 11 namespace Parsely\UI;
12 12
13 -use Parsely\Content_Helper\Excerpt_Generator;
14 13 use Parsely\Parsely;
15 -use Parsely\Permissions;
16 14 use Parsely\Validator;
17 15
18 16 use function Parsely\Utils\get_asset_info;
19 17
@@ -26,10 +24,8 @@
26 24 *
27 25 * @phpstan-import-type Parsely_Options from Parsely
28 26 *
29 27 * @phpstan-type Setting_Arguments array{
30 - * add_fieldset?: bool,
31 - * legend?: string,
32 28 * option_key: string,
33 29 * label_for: string,
34 30 * title?: string,
35 31 * help_text?: string,
@@ -63,27 +59,12 @@
63 59 * content_id_prefix?: string,
64 60 * use_top_level_cats?:bool|string,
65 61 * custom_taxonomy_section?: string,
66 62 * cats_as_tags?: bool|string,
67 - * content_helper: Parsely_Settings_Options_Content_Helper,
68 63 * lowercase_tags?: bool,
69 64 * force_https_canonicals?: bool,
70 65 * disable_autotrack?: bool|string,
71 66 * }
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 67 */
87 68 final class Settings_Page {
88 69 /**
89 70 * Instance of Parsely class.
@@ -110,21 +91,8 @@
110 91 */
111 92 private $managed_options_badge = array();
112 93
113 94 /**
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 95 * Constructor.
128 96 *
129 97 * @param Parsely $parsely Instance of Parsely class.
130 98 */
@@ -157,35 +125,33 @@
157 125
158 126 /**
159 127 * Enqueues all needed scripts and styles for Parse.ly plugin settings page.
160 128 *
161 - * @param string|null $hook_suffix The current page being loaded.
129 + * @param string $hook_suffix The current page being loaded.
162 130 */
163 - public function enqueue_settings_assets( ?string $hook_suffix ): void {
164 - if ( ! is_string( $hook_suffix ) || 'settings_page_parsely' !== $hook_suffix ) {
165 - return;
166 - }
131 + public function enqueue_settings_assets( string $hook_suffix ): void {
132 + if ( 'settings_page_parsely' === $hook_suffix ) {
133 + add_filter( 'media_library_months_with_files', '__return_empty_array' );
134 + wp_enqueue_media();
167 135
168 - add_filter( 'media_library_months_with_files', '__return_empty_array' );
169 - wp_enqueue_media();
136 + $admin_settings_asset = get_asset_info( 'build/admin-settings.asset.php' );
137 + $built_assets_url = plugin_dir_url( PARSELY_FILE ) . '/build/';
170 138
171 - $admin_settings_asset = get_asset_info( 'build/admin-settings.asset.php' );
172 - $built_assets_url = plugin_dir_url( PARSELY_FILE ) . '/build/';
139 + wp_enqueue_script(
140 + 'parsely-admin-settings',
141 + $built_assets_url . 'admin-settings.js',
142 + $admin_settings_asset['dependencies'],
143 + $admin_settings_asset['version'],
144 + true
145 + );
173 146
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 - );
147 + wp_enqueue_style(
148 + 'parsely-admin-settings',
149 + $built_assets_url . 'admin-settings.css',
150 + $admin_settings_asset['dependencies'],
151 + $admin_settings_asset['version']
152 + );
153 + }
188 154 }
189 155
190 156 /**
191 157 * Adds the Parse.ly settings page in WordPress settings menu.
@@ -259,9 +225,8 @@
259 225 )
260 226 );
261 227
262 228 $this->initialize_basic_section();
263 - $this->initialize_content_helper_section();
264 229 $this->initialize_recrawl_section();
265 230 $this->initialize_advanced_section();
266 231 }
267 232
@@ -428,91 +393,8 @@
428 393 }
429 394 }
430 395
431 396 /**
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 397 * Registers section and settings for Recrawl section.
516 398 *
517 399 * @since 3.2.0
518 400 */
@@ -863,129 +745,33 @@
863 745 $this->print_description_text( $args );
864 746 }
865 747
866 748 /**
867 - * Prints a checkbox tag.
749 + * Prints a checkbox tag in the settings page.
868 750 *
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.
751 + * @param Setting_Arguments $args Arguments to print to checkbox tag.
873 752 */
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'] ?? '';
753 + public function print_checkbox_tag( $args ): void {
754 + $options = $this->parsely->get_options();
755 + $name = $args['option_key'];
756 + $value = $options[ $name ];
757 + $id = esc_attr( $name );
758 + $name = Parsely::OPTIONS_KEY . "[$id]";
759 + $yes_text = $args['yes_text'] ?? '';
885 760
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 - );
761 + $is_managed = key_exists( $id, $this->parsely->managed_options );
762 + echo '<fieldset', $is_managed ? ' disabled>' : '>';
763 + printf( "<input type='checkbox' name='%s' id='%s_true' value='true' ", esc_attr( $name ), esc_attr( $id ) );
909 764 if ( isset( $args['help_text'] ) ) {
910 - echo ' aria-describedby="' . esc_attr( $html_id ) . '-description"';
765 + echo ' aria-describedby="' . esc_attr( $id ) . '-description"';
911 766 }
912 - checked( true === $value, true );
913 - printf( ' /> %s</label>', esc_html( $yes_text ) );
767 + echo checked( true === $value, true, false );
768 + printf( " /> <label for='%s_true'>%s</label><fieldset>", esc_attr( $id ), esc_html( $yes_text ) );
914 769
915 - // Fieldset end.
916 - if ( $has_fieldset ) {
917 - echo '</fieldset>';
918 - }
919 -
920 770 $this->print_description_text( $args );
921 771 }
922 772
923 773 /**
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 774 * Prints out the select tags
989 775 *
990 776 * @param Setting_Arguments $args The arguments for the select dropdowns.
991 777 */
@@ -1187,9 +973,8 @@
1187 973 * @return ParselySettingOptions
1188 974 */
1189 975 public function validate_options( $input ) {
1190 976 $input = $this->validate_basic_section( $input );
1191 - $input = $this->validate_content_helper_section( $input );
1192 977 $input = $this->validate_recrawl_section( $input );
1193 978 $input = $this->validate_advanced_section( $input );
1194 979
1195 980 return $input;
@@ -1285,89 +1070,8 @@
1285 1070 );
1286 1071 } else {
1287 1072 $input['disable_amp'] = 'true' === $input['disable_amp'];
1288 1073 }
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 1074
1371 1075 return $input;
1372 1076 }
1373 1077