'widget_swiftling_switcher', 'description' => __( 'Lets visitors open the page in another language.', 'swiftling' ), 'customize_selective_refresh' => true, 'show_instance_in_rest' => true, ) ); } /** * Registers the widget. */ public static function registerWidget(): void { register_widget( self::class ); } /** * Prints the widget. * * @param array $args Sidebar markup. * @param array $instance Settings of this widget. */ public function widget( $args, $instance ): void { $instance = $this->complete( $instance ); $switcher = Switcher::render( array( 'style' => $instance['style'], 'flag' => $instance['flag'], 'name' => $instance['name'], 'code' => $instance['code'], 'flag_size' => $instance['flag_size'], 'class' => 'is-widget', ) ); if ( '' === $switcher ) { return; } /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); echo wp_kses_post( $args['before_widget'] ); if ( '' !== (string) $title ) { echo wp_kses_post( $args['before_title'] ) . esc_html( $title ) . wp_kses_post( $args['after_title'] ); } echo wp_kses( $switcher, Switcher::allowedHtml() ); echo wp_kses_post( $args['after_widget'] ); } /** * Prints the settings of the widget. * * @param array $instance Settings of this widget. * @return string */ public function form( $instance ) { $instance = $this->complete( $instance ); $styles = array( 'dropdown' => __( 'Dropdown', 'swiftling' ), 'horizontal' => __( 'Horizontal list', 'swiftling' ), 'vertical' => __( 'Vertical list', 'swiftling' ), ); $parts = array( 'flag' => __( 'Flag', 'swiftling' ), 'name' => __( 'Language name', 'swiftling' ), 'code' => __( 'Language code', 'swiftling' ), ); $sizes = array( 'small' => __( 'Small', 'swiftling' ), 'medium' => __( 'Medium', 'swiftling' ), 'large' => __( 'Large', 'swiftling' ), ); ?>

$label ) : ?> >

$new_instance Settings as submitted. * @param array $old_instance Settings before. * @return array */ public function update( $new_instance, $old_instance ) { return array( 'title' => sanitize_text_field( isset( $new_instance['title'] ) ? (string) $new_instance['title'] : '' ), 'style' => Settings::style( isset( $new_instance['style'] ) ? $new_instance['style'] : '', 'vertical' ), 'flag' => ! empty( $new_instance['flag'] ), 'name' => ! empty( $new_instance['name'] ), 'code' => ! empty( $new_instance['code'] ), 'flag_size' => Settings::flagSize( isset( $new_instance['flag_size'] ) ? $new_instance['flag_size'] : '' ), ); } /** * Fills in what a new widget has not saved yet: a vertical list showing what the Switcher * screen shows. * * @param array $instance Settings of this widget. * @return array{title: string, style: string, flag: bool, name: bool, code: bool, flag_size: string} */ private function complete( $instance ): array { $instance = is_array( $instance ) ? $instance : array(); $show = Settings::get()['show']; return array( 'title' => isset( $instance['title'] ) ? (string) $instance['title'] : '', 'style' => Settings::style( isset( $instance['style'] ) ? $instance['style'] : '', 'vertical' ), 'flag' => isset( $instance['flag'] ) ? (bool) $instance['flag'] : $show['flag'], 'name' => isset( $instance['name'] ) ? (bool) $instance['name'] : $show['name'], 'code' => isset( $instance['code'] ) ? (bool) $instance['code'] : $show['code'], 'flag_size' => isset( $instance['flag_size'] ) ? Settings::flagSize( $instance['flag_size'] ) : $show['flag_size'], ); } }