'AA', 'best_practice' => false, 'roles' => array( 'administrator', 'editor', 'author', 'contributor' ), 'check_on_save' => false, ); } /** * Turns any stored or submitted value into valid settings; unknown or broken fields fall back to the defaults. * * @param mixed $raw Value of the option or of the submitted form. * @return array{level: string, best_practice: bool, roles: string[], check_on_save: bool} */ function waysill_sanitize_settings( $raw ): array { $settings = waysill_default_settings(); if ( ! is_array( $raw ) ) { return $settings; } if ( isset( $raw['level'] ) && in_array( $raw['level'], array( 'A', 'AA', 'AAA' ), true ) ) { $settings['level'] = $raw['level']; } // Checkboxes: a submitted form leaves out unchecked boxes, so a missing key means off. $settings['best_practice'] = ! empty( $raw['best_practice'] ); $settings['check_on_save'] = ! empty( $raw['check_on_save'] ); $roles = array(); if ( isset( $raw['roles'] ) && is_array( $raw['roles'] ) ) { foreach ( $raw['roles'] as $role ) { if ( is_string( $role ) && '' !== sanitize_key( $role ) ) { $roles[] = sanitize_key( $role ); } } } $settings['roles'] = array_slice( array_values( array_unique( $roles ) ), 0, 100 ); return $settings; } /** * The current settings, always complete and valid. * * @return array{level: string, best_practice: bool, roles: string[], check_on_save: bool} */ function waysill_settings(): array { $stored = get_option( 'waysill_settings', null ); return null === $stored ? waysill_default_settings() : waysill_sanitize_settings( $stored ); } /** * The settings in the shape of the shared core's `Settings`, as the editor scripts expect them. * * @return array{level: string, bestPractice: bool} */ function waysill_core_settings(): array { $settings = waysill_settings(); return array( 'level' => $settings['level'], 'bestPractice' => $settings['best_practice'], ); } /** * Whether check on save is on for a user: the user's own choice, else the site default. * * @param int $user_id User id. */ function waysill_user_check_on_save( int $user_id ): bool { $own = get_user_meta( $user_id, 'waysill_check_on_save', true ); if ( '1' === $own || '0' === $own ) { return '1' === $own; } return waysill_settings()['check_on_save']; } /** Registers the option with its sanitiser, so options.php stores only valid settings. */ function waysill_register_settings(): void { register_setting( 'waysill', 'waysill_settings', array( 'type' => 'object', 'sanitize_callback' => 'waysill_sanitize_settings', 'default' => waysill_default_settings(), ) ); } add_action( 'admin_init', 'waysill_register_settings' ); /** Adds Settings → Waysill. Freemius shows its opt-in screen on this page until the admin allows or skips. */ function waysill_settings_menu(): void { add_options_page( __( 'Waysill', 'waysill' ), __( 'Waysill', 'waysill' ), 'manage_options', 'waysill', 'waysill_settings_page' ); } add_action( 'admin_menu', 'waysill_settings_menu' ); /** Deletes every stored check result (admin-post action from the settings page). */ function waysill_handle_delete_results(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to delete Waysill results.', 'waysill' ), 403 ); } check_admin_referer( 'waysill_delete_results' ); waysill_delete_all_results(); add_settings_error( 'waysill_settings', 'waysill_deleted', __( 'All stored Waysill results were deleted.', 'waysill' ), 'success' ); // The same hand-over options.php uses: the settings page shows the message once. set_transient( 'settings_errors', get_settings_errors(), 30 ); wp_safe_redirect( add_query_arg( 'settings-updated', 'true', admin_url( 'options-general.php?page=waysill' ) ) ); exit; } add_action( 'admin_post_waysill_delete_results', 'waysill_handle_delete_results' ); /** Renders Settings → Waysill. */ function waysill_settings_page(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } $settings = waysill_settings(); $levels = array( 'A' => __( 'Level A', 'waysill' ), 'AA' => __( 'Level AA (recommended)', 'waysill' ), 'AAA' => __( 'Level AAA', 'waysill' ), ); ?>