'string', 'default' => 'auto', 'sanitize_callback' => 'adminight_sanitize_mode', 'show_in_rest' => false, ) ); register_setting( 'adminight', ADMINIGHT_AREAS_OPTION, array( 'type' => 'array', 'default' => array(), 'sanitize_callback' => 'adminight_sanitize_areas', 'show_in_rest' => false, ) ); register_setting( 'adminight', ADMINIGHT_PLUGINS_OPTION, array( 'type' => 'array', 'default' => array(), 'sanitize_callback' => 'adminight_sanitize_plugins', 'show_in_rest' => false, ) ); } add_action( 'admin_init', 'adminight_register_settings' ); /** * Keeps the stored value to one of the three modes. * * @param mixed $value Submitted value. * @return string */ function adminight_sanitize_mode( $value ) { $value = sanitize_key( (string) $value ); return array_key_exists( $value, adminight_modes() ) ? $value : 'auto'; } /** * Areas: one on/off value per known area. * * @param mixed $value Submitted value. * @return array */ function adminight_sanitize_areas( $value ) { $value = is_array( $value ) ? $value : array(); $out = array(); foreach ( array( 'core', 'editor', 'appearance', 'login', 'woocommerce' ) as $key ) { $out[ $key ] = isset( $value[ $key ] ) ? ! empty( $value[ $key ] ) : true; } return $out; } /** * Plugins: one mode per menu key. Choices for plugins that are not active right now are kept. * * @param mixed $value Submitted value. * @return array */ function adminight_sanitize_plugins( $value ) { $out = adminight_site_plugins(); $value = is_array( $value ) ? $value : array(); foreach ( $value as $key => $mode ) { $key = sanitize_key( (string) $key ); $mode = sanitize_key( (string) $mode ); if ( '' === $key || ! array_key_exists( $mode, adminight_plugin_modes() ) ) { continue; } if ( 'off' === $mode ) { unset( $out[ $key ] ); } else { $out[ $key ] = $mode; } } return $out; } /** * Settings → Adminight. */ function adminight_settings_menu() { add_options_page( __( 'Adminight', 'adminight' ), __( 'Adminight', 'adminight' ), 'manage_options', 'adminight', 'adminight_settings_page' ); } add_action( 'admin_menu', 'adminight_settings_menu' ); /** * Radio buttons for one of the three modes. * * @param string $name Field name. * @param string $current Selected mode. */ function adminight_mode_radios( $name, $current ) { foreach ( adminight_modes() as $key => $label ) { printf( '
', esc_attr( $name ), esc_attr( $key ), checked( $current, $key, false ), esc_html( $label ) ); } } /** * The settings screen. */ function adminight_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $areas = adminight_site_areas(); $plugins = adminight_site_plugins(); $found = adminight_detected_plugins(); ?>

$area ) : ?>

$item ) : ?>

?adminight=off' ); ?>

label, for the profile screen. * * @return array */ function adminight_enabled_places() { $places = array(); $areas = adminight_site_areas(); foreach ( adminight_areas() as $key => $area ) { if ( 'login' !== $key && ! empty( $areas[ $key ] ) ) { $places[ $key ] = $area['label']; } } $found = adminight_detected_plugins(); foreach ( adminight_site_plugins() as $key => $mode ) { if ( 'off' !== $mode && isset( $found[ $key ] ) ) { $places[ 'p-' . $key ] = $found[ $key ]['label']; } } return $places; } /** * Users → Profile: the mode, and the places this user wants it in. * * @param WP_User $user The user being edited. */ function adminight_profile_field( $user ) { $places = adminight_enabled_places(); $off = adminight_user_off( $user->ID ); ?>
ID ) ); ?>
$label ) : ?>
$on ) { $key = sanitize_key( (string) $key ); if ( isset( $valid[ $key ] ) && '1' !== $on ) { $off[] = $key; } } $off = array_values( array_unique( $off ) ); if ( $off ) { update_user_meta( $user_id, ADMINIGHT_USER_OFF_META, $off ); } else { delete_user_meta( $user_id, ADMINIGHT_USER_OFF_META ); } } // phpcs:enable WordPress.Security.NonceVerification.Missing } add_action( 'personal_options_update', 'adminight_profile_save' ); add_action( 'edit_user_profile_update', 'adminight_profile_save' ); /** * "Settings" link on the Plugins screen. * * @param string[] $links Existing links. * @return string[] */ function adminight_action_links( $links ) { array_unshift( $links, '' . esc_html__( 'Settings', 'adminight' ) . '' ); return $links; } add_filter( 'plugin_action_links_' . plugin_basename( ADMINIGHT_FILE ), 'adminight_action_links' );