'Compact', 'standard' => 'Standard', 'large' => 'Large' ); } public static function current() { $value = get_user_meta( get_current_user_id(), self::KEY, true ); return is_string( $value ) && isset( self::profiles()[ $value ] ) ? $value : 'standard'; } public static function register() { add_filter( 'admin_body_class', array( __CLASS__, 'body_class' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'assets' ) ); add_action( 'admin_post_edrad_text_profile', array( __CLASS__, 'save' ) ); add_action( 'wp_ajax_edrad_text_profile', array( __CLASS__, 'save_ajax' ) ); } public static function body_class( $classes ) { $screen = get_current_screen(); return $screen && EDRAD_Admin::is_plugin_screen( $screen->id ) ? $classes . ' edrad-text-' . self::current() : $classes; } public static function assets( $hook ) { if ( ! EDRAD_Admin::is_plugin_screen( $hook ) ) { return; } wp_enqueue_style( 'edrad-text-size', plugins_url( 'assets/text-size.css', EDRAD_FILE ), array( 'edcr-admin' ), EDRAD_VERSION ); wp_enqueue_script( 'edrad-text-size', plugins_url( 'assets/text-size.js', EDRAD_FILE ), array(), EDRAD_VERSION . '-autosave1', true ); wp_localize_script( 'edrad-text-size', 'EDRAD_TEXT_SIZE', array( 'url' => admin_url( 'admin-ajax.php' ), 'saving' => EDRAD_Language::t( 'Saving text size…' ), 'saved' => EDRAD_Language::t( 'Text size saved.' ), 'failed' => EDRAD_Language::t( 'Text size could not be saved. Please try again.' ), ) ); } public static function save() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Access denied.', 'error-division-radar' ) ); } check_admin_referer( 'edrad_text_profile' ); $value = isset( $_POST['text_profile'] ) && is_string( $_POST['text_profile'] ) ? sanitize_key( wp_unslash( $_POST['text_profile'] ) ) : ''; if ( ! isset( self::profiles()[ $value ] ) ) { wp_die( esc_html__( 'Invalid text size.', 'error-division-radar' ) ); } update_user_meta( get_current_user_id(), self::KEY, $value ); wp_safe_redirect( admin_url( 'admin.php?page=error-division-radar-settings' ) ); exit; } public static function save_ajax() { if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'code' => 'forbidden' ), 403 ); return; } check_ajax_referer( 'edrad_text_profile', '_wpnonce' ); $value = isset( $_POST['text_profile'] ) && is_string( $_POST['text_profile'] ) ? sanitize_key( wp_unslash( $_POST['text_profile'] ) ) : ''; if ( ! isset( self::profiles()[ $value ] ) ) { wp_send_json_error( array( 'code' => 'invalid_profile' ), 400 ); return; } update_user_meta( get_current_user_id(), self::KEY, $value ); if ( self::current() !== $value ) { wp_send_json_error( array( 'code' => 'save_failed' ), 500 ); return; } wp_send_json_success( array( 'profile' => $value ) ); } public static function render() { ?>
$label ) : ?>