maybe_filter_legacy( $settings ); /** * Filter the language switcher settings. * * @since 3.9 * * @param array $settings Settings. */ $settings = apply_filters( 'pll_language_switcher_settings', $settings ); foreach ( $this->validate( $settings ) as $name => $value ) { $this->$name = $value; } if ( '' === $this->unique_id ) { ++self::$increment; $this->unique_id = 'pll-switcher-' . self::$increment; } } /** * Returns an instance of the switcher. * * @since 3.9 * * @return string * * @phpstan-return class-string|'' */ public function get_switcher_class(): string { switch ( $this->layout ) { case 'horizontal': case 'vertical': return Layout\Nav::class; case 'dropdown': return Layout\Dropdown::class; case 'select': return Layout\Select::class; default: return ''; } } /** * Validates the settings (value and type). * This removes additional keys. * * @since 3.9 * * @param array $settings Switcher settings. * @return array */ protected function validate( array $settings ): array { $choices = array( 'layout' => array( 'horizontal', 'vertical', 'dropdown', 'select' ), 'alignment' => array( 'left', 'center', 'right', 'stretched', 'inherit' ), 'flag_aspect_ratio' => array( '3:2', '1:1' ), 'show_labels' => array( '', 'names', 'codes' ), ); $validated = array_diff_key( get_class_vars( self::class ), array( 'increment' => 0 ) ); $validated['alignment'] = is_rtl() ? 'right' : 'left'; foreach ( $choices as $key => $setting_choices ) { if ( isset( $settings[ $key ] ) && in_array( $settings[ $key ], $setting_choices, true ) ) { $validated[ $key ] = $settings[ $key ]; } } foreach ( array( 'show_flags', 'hide_current', 'hide_if_empty', 'hide_if_no_translation', 'force_home', 'preserve_spacing', 'show_wrapper' ) as $key ) { if ( isset( $settings[ $key ] ) ) { $validated[ $key ] = ! empty( $settings[ $key ] ); } } if ( ! empty( $settings['post_id'] ) && is_numeric( $settings['post_id'] ) ) { $validated['post_id'] = absint( $settings['post_id'] ); } foreach ( array( 'wrapper_classes', 'item_classes', 'link_classes' ) as $key ) { if ( isset( $settings[ $key ] ) && is_array( $settings[ $key ] ) ) { $validated[ $key ] = array_filter( $settings[ $key ], static function ( $class ) { return is_string( $class ) && ! empty( trim( $class ) ); } ); } } if ( ! empty( $settings['unique_id'] ) && is_string( $settings['unique_id'] ) ) { $validated['unique_id'] = sanitize_key( $settings['unique_id'] ); } // Mandatory settings for the `select` layout. if ( 'select' === $validated['layout'] ) { $validated['show_flags'] = false; $validated['show_labels'] = 'names'; } // Make sure something is displayed. if ( ! $validated['show_flags'] && empty( $validated['show_labels'] ) ) { $validated['show_labels'] = 'names'; } return $validated; } }