filter_screen_options(); if ( ! $allowed ) { return; } add_action( 'admin_head', [ $this, 'add_screen_options' ] ); add_filter( 'screen_settings', [ $this, 'render_screen_options' ], 10, 2 ); add_filter( "set_screen_option_{$this->screen_options_id}", [ $this, 'save_screen_options' ], 10, 2 ); add_filter( 'screen_options_show_submit', '__return_true' ); } /** * Filter screen options. * * @since 1.8.8 */ public function filter_screen_options() { $options = get_user_option( $this->screen_options_id ); foreach ( $this->screen_options as $group => $options_group ) { foreach ( $options_group['options'] as $option ) { add_filter( "get_user_option_{$this->screen_options_id}_{$group}_{$option['option']}", function () use ( $option, $group, $options ) { $key = $group . '_' . $option['option']; return $options[ $key ] ?? $option['default']; } ); } } } /** * Configure screen options. * * @since 1.8.8 */ public function add_screen_options() { foreach ( $this->screen_options as $group => $options_group ) { foreach ( $options_group['options'] as $option ) { add_screen_option( $group . '_' . $option['option'], [ 'label' => $option['label'], 'option' => $option['option'], 'default' => $option['default'], ] ); } } } /** * Save screen options. * * @since 1.8.8 * * @param mixed $status Status of the screen option. * @param string $option Option name. * * @return mixed */ public function save_screen_options( $status, $option ) { // phpcs:ignore Generic.Metrics.NestingLevel.MaxExceeded if ( $option === $this->screen_options_id ) { $value = []; foreach ( $this->screen_options as $group => $options_group ) { $options = $options_group['options']; foreach ( $options as $group_option ) { $key = $group . '_' . $group_option['option']; if ( ! isset( $_POST[ $key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing $value[ $key ] = false; continue; } $value[ $key ] = ! empty( $_POST[ $key ] ) ? sanitize_text_field( wp_unslash( $_POST[ $key ] ) ) : $group_option['default']; // phpcs:ignore WordPress.Security.NonceVerification.Missing } } return $value; } return $status; } /** * Render screen options. * * @since 1.8.8 * * @param string $screen_settings HTML markup of custom screen settings. * * @return string */ public function render_screen_options( $screen_settings ) { // phpcs:ignore Generic.Metrics.NestingLevel.MaxExceeded, Generic.Metrics.CyclomaticComplexity.TooHigh foreach ( $this->screen_options as $group => $options_group ) { $screen_settings .= '
'; } return $screen_settings; } }