__( 'Display a featured artist.', 'wp-fedistream' ), 'classname' => 'fedistream-widget fedistream-widget--featured-artist', ) ); } /** * Front-end display. * * @param array $args Widget arguments. * @param array $instance Saved values from database. * @return void */ public function widget( $args, $instance ): void { $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Featured Artist', 'wp-fedistream' ); $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); $artist_id = ! empty( $instance['artist_id'] ) ? absint( $instance['artist_id'] ) : 0; $random = ! empty( $instance['random'] ) && $instance['random']; $post = null; if ( $random ) { // Get a random artist. $posts = get_posts( array( 'post_type' => 'fedistream_artist', 'posts_per_page' => 1, 'orderby' => 'rand', 'post_status' => 'publish', ) ); if ( ! empty( $posts ) ) { $post = $posts[0]; } } elseif ( $artist_id ) { $post = get_post( $artist_id ); if ( $post && 'fedistream_artist' !== $post->post_type ) { $post = null; } } if ( ! $post ) { return; } $artist_data = TemplateLoader::get_artist_data( $post ); echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped if ( $title ) { echo $args['before_title'] . esc_html( $title ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } try { $plugin = Plugin::get_instance(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $plugin->render( 'widgets/featured-artist', array( 'post' => $artist_data, ) ); } catch ( \Exception $e ) { if ( WP_DEBUG ) { echo '
' . esc_html( $e->getMessage() ) . '
'; } } echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** * Back-end widget form. * * @param array $instance Previously saved values from database. * @return void */ public function form( $instance ): void { $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Featured Artist', 'wp-fedistream' ); $artist_id = ! empty( $instance['artist_id'] ) ? absint( $instance['artist_id'] ) : 0; $random = ! empty( $instance['random'] ) && $instance['random']; // Get all artists for dropdown. $artists = get_posts( array( 'post_type' => 'fedistream_artist', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'post_status' => 'publish', ) ); ?>