Vabelon: the list of languages. * * @package Vabelon */ declare( strict_types=1 ); namespace Vabelon\Admin; use Vabelon\Plugin; use Vabelon\Language\Languages; defined( 'ABSPATH' ) || exit; /** * Settings page, built on the WordPress Settings API. */ final class Settings { public const PAGE = 'vabelon'; public const GROUP = 'vabelon'; /** * Hook the page into WordPress. * * @return void */ public function register(): void { add_action( 'admin_menu', array( $this, 'menu' ) ); add_action( 'admin_init', array( $this, 'setting' ) ); } /** * Add the page under Settings. * * @return void */ public function menu(): void { add_options_page( 'Vabelon', 'Vabelon', 'manage_options', self::PAGE, array( $this, 'render' ) ); } /** * Register the option with the Settings API. * * @return void */ public function setting(): void { register_setting( self::GROUP, Languages::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize' ), 'show_in_rest' => false, ) ); } /** * Validate and sanitize the value before it is stored. * * Accepts the settings form (rows and default) or the stored shape (items), which * Languages::save() passes through update_option(). Both are validated the same way. * * @param mixed $input Submitted form or stored shape. * @return mixed The value to store. */ public function sanitize( $input ) { $current = get_option( Languages::OPTION, array() ); if ( ! is_array( $input ) ) { return $current; } $rows = isset( $input['items'] ) ? ( is_array( $input['items'] ) ? $input['items'] : array() ) : $this->form_rows( $input ); $clean = Plugin::languages()->validate( $rows ); if ( is_wp_error( $clean ) ) { add_settings_error( Languages::OPTION, (string) $clean->get_error_code(), $clean->get_error_message() ); return $current; } return array( 'version' => 1, 'items' => $clean, ); } /** * Language rows from the settings form: removed and empty rows are dropped, and * the chosen radio button marks the default. * * @param array $input Submitted form. * @return array */ private function form_rows( array $input ): array { $posted = isset( $input['rows'] ) && is_array( $input['rows'] ) ? $input['rows'] : array(); $default = isset( $input['default'] ) && is_scalar( $input['default'] ) ? (string) $input['default'] : ''; $rows = array(); foreach ( $posted as $index => $row ) { if ( ! is_array( $row ) || ! empty( $row['remove'] ) ) { continue; } if ( '' === trim( Languages::text( $row['slug'] ?? '' ) ) && '' === trim( Languages::text( $row['name'] ?? '' ) ) ) { continue; } $row['is_default'] = (string) $index === $default; $rows[] = $row; } return $rows; } /** * Print the page. * * @return void */ public function render(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } $rows = array(); foreach ( Plugin::languages()->all() as $language ) { $rows[] = get_object_vars( $language ); } $rows[] = array_fill_keys( array( 'slug', 'wp_locale', 'hreflang', 'name', 'native_name' ), '' ) + array( 'is_default' => false ); $columns = array( 'slug' => __( 'Address prefix', 'vabelon' ), 'wp_locale' => __( 'WordPress locale', 'vabelon' ), 'hreflang' => __( 'hreflang', 'vabelon' ), 'name' => __( 'Name', 'vabelon' ), 'native_name' => __( 'Name in that language', 'vabelon' ), ); echo '

' . esc_html__( 'Vabelon languages', 'vabelon' ) . '

'; settings_errors( Languages::OPTION ); echo '
'; settings_fields( self::GROUP ); echo ''; foreach ( $columns as $label ) { echo ''; } echo ''; foreach ( $rows as $index => $row ) { $name = Languages::OPTION . '[rows][' . $index . ']'; echo ''; foreach ( array_keys( $columns ) as $key ) { echo ''; } echo ''; } echo '
' . esc_html__( 'Default', 'vabelon' ) . '' . esc_html( $label ) . '' . esc_html__( 'Remove', 'vabelon' ) . '
'; if ( ! $row['is_default'] && '' !== $row['slug'] ) { echo ''; } echo '

' . esc_html__( 'Content that has no language is shown as the default language. If you change the default, that content moves with it.', 'vabelon' ) . '

'; submit_button(); echo '
'; } }