register_hooks(); } /** * Register runtime and admin hooks. * * @return void */ private function register_hooks() { add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_styles' ), 20 ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ), 20 ); add_action( 'login_enqueue_scripts', array( $this, 'enqueue_login_styles' ), 20 ); add_action( 'admin_init', array( $this, 'register_settings' ) ); add_action( 'admin_menu', array( $this, 'register_settings_page' ) ); add_filter( 'plugin_action_links_' . plugin_basename( AETOS_RTL_INPUT_DIRECTION_FILE ), array( $this, 'add_settings_link' ) ); } /** * Enqueue the technical-field styles on the front end when enabled. * * @return void */ public function enqueue_frontend_styles() { $settings = $this->get_settings(); if ( ! is_rtl() || ! $settings['frontend'] ) { return; } $this->enqueue_direction_styles( $settings ); } /** * Enqueue the technical-field styles in wp-admin when enabled. * * The plugin settings screen always loads the base stylesheet on RTL sites so * its field preview remains useful even when global admin styling is disabled. * * @param string $hook_suffix Current admin page hook suffix. * @return void */ public function enqueue_admin_styles( $hook_suffix ) { if ( ! is_rtl() ) { return; } $settings = $this->get_settings(); $is_settings_page = 'settings_page_' . self::SETTINGS_PAGE_SLUG === $hook_suffix; if ( ! $settings['admin'] && ! $is_settings_page ) { return; } $this->enqueue_direction_styles( $settings ); } /** * Enqueue the technical-field styles on the WordPress login screen. * * @return void */ public function enqueue_login_styles() { $settings = $this->get_settings(); if ( ! is_rtl() || ! $settings['login'] ) { return; } $this->enqueue_direction_styles( $settings ); } /** * Enqueue static styles without constructing dynamic CSS. * * @param array $settings Current settings. * @return void */ private function enqueue_direction_styles( $settings ) { wp_enqueue_style( 'aetos-rtl-input-direction-technical-fields', plugins_url( 'assets/css/technical-fields.css', AETOS_RTL_INPUT_DIRECTION_FILE ), array(), AETOS_RTL_INPUT_DIRECTION_VERSION ); if ( $settings['username_fields'] ) { wp_enqueue_style( 'aetos-rtl-input-direction-username-fields', plugins_url( 'assets/css/username-fields.css', AETOS_RTL_INPUT_DIRECTION_FILE ), array( 'aetos-rtl-input-direction-technical-fields' ), AETOS_RTL_INPUT_DIRECTION_VERSION ); } } /** * Register the settings array and its sanitization callback. * * @return void */ public function register_settings() { register_setting( self::SETTINGS_GROUP, self::OPTION_NAME, array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize_settings' ), 'default' => $this->get_default_settings(), 'show_in_rest' => false, ) ); } /** * Sanitize checkbox settings into a fixed boolean shape. * * @param mixed $input Raw settings value. * @return array */ public function sanitize_settings( $input ) { if ( ! is_array( $input ) ) { $input = array(); } return array( 'frontend' => ! empty( $input['frontend'] ), 'admin' => ! empty( $input['admin'] ), 'login' => ! empty( $input['login'] ), 'username_fields' => ! empty( $input['username_fields'] ), ); } /** * Register the plugin settings page under Settings. * * @return void */ public function register_settings_page() { add_options_page( esc_html__( 'Aetos RTL Input Direction Fixer', 'aetos-rtl-input-direction-fixer' ), esc_html__( 'Aetos RTL Input Direction', 'aetos-rtl-input-direction-fixer' ), 'manage_options', self::SETTINGS_PAGE_SLUG, array( $this, 'render_settings_page' ) ); } /** * Add a Settings shortcut on the Plugins screen. * * @param array $links Existing plugin action links. * @return array */ public function add_settings_link( $links ) { $settings_link = sprintf( '%2$s', esc_url( admin_url( 'options-general.php?page=' . self::SETTINGS_PAGE_SLUG ) ), esc_html__( 'Settings', 'aetos-rtl-input-direction-fixer' ) ); array_unshift( $links, $settings_link ); return $links; } /** * Render the settings page. * * @return void */ public function render_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $settings = $this->get_settings(); ?>

get_checkbox_fields() as $setting_key => $setting_field ) { $this->render_checkbox_row( $settings, $setting_key, $setting_field['label'], $setting_field['description'] ); } $this->render_locale_status_row(); $this->render_preview_row(); ?>
> */ private function get_checkbox_fields() { return array( 'frontend' => array( 'label' => __( 'Front-end forms', 'aetos-rtl-input-direction-fixer' ), 'description' => __( 'Apply the fix to public-facing email, URL, telephone, and password fields.', 'aetos-rtl-input-direction-fixer' ), ), 'admin' => array( 'label' => __( 'WordPress admin', 'aetos-rtl-input-direction-fixer' ), 'description' => __( 'Apply the same narrow fix to technical fields inside wp-admin.', 'aetos-rtl-input-direction-fixer' ), ), 'login' => array( 'label' => __( 'Login screen', 'aetos-rtl-input-direction-fixer' ), 'description' => __( 'Keep password and other technical login fields left to right on RTL login screens.', 'aetos-rtl-input-direction-fixer' ), ), 'username_fields' => array( 'label' => __( 'Username-like fields', 'aetos-rtl-input-direction-fixer' ), 'description' => __( 'Also target fields identified as usernames. Leave this off when Arabic usernames are expected.', 'aetos-rtl-input-direction-fixer' ), ), ); } /** * Render the active-locale status row. * * @return void */ private function render_locale_status_row() { ?>

render_field_preview(); ?> $settings Current settings. * @param string $key Setting key. * @param string $label Field label. * @param string $description Field description. * @return void */ private function render_checkbox_row( $settings, $key, $label, $description ) { $field_id = 'aetos-rtl-input-direction-' . $key; $description_id = $field_id . '-description'; ?> />

get_preview_fields() as $input_type => $preview_field ) { $this->render_preview_input( $input_type, $preview_field ); } if ( ! is_rtl() ) { ?>

> */ private function get_preview_fields() { return array( 'email' => array( 'label' => __( 'Email', 'aetos-rtl-input-direction-fixer' ), 'value' => 'hello@example.com', ), 'tel' => array( 'label' => __( 'Telephone', 'aetos-rtl-input-direction-fixer' ), 'value' => '+966 50 123 4567', ), 'url' => array( 'label' => __( 'URL', 'aetos-rtl-input-direction-fixer' ), 'value' => 'https://example.com/path', ), 'password' => array( 'label' => __( 'Password', 'aetos-rtl-input-direction-fixer' ), 'value' => 'Example-1234', ), ); } /** * Render one static preview input. * * @param string $input_type HTML input type. * @param array $preview_field Preview label and value. * @return void */ private function render_preview_input( $input_type, $preview_field ) { $field_id = 'aetos-rtl-input-direction-preview-' . $input_type; ?>


*/ private function get_settings() { $settings = get_option( self::OPTION_NAME, array() ); if ( ! is_array( $settings ) ) { $settings = array(); } $settings = wp_parse_args( $settings, $this->get_default_settings() ); return array( 'frontend' => (bool) $settings['frontend'], 'admin' => (bool) $settings['admin'], 'login' => (bool) $settings['login'], 'username_fields' => (bool) $settings['username_fields'], ); } /** * Return conservative default settings. * * @return array */ private function get_default_settings() { return array( 'frontend' => true, 'admin' => true, 'login' => true, 'username_fields' => false, ); } }