Files
wp-fedistream/includes/Frontend/Widgets/FeaturedArtistWidget.php
magdev 4a5d7b9f4d feat: Initial release v0.1.0
WP FediStream - Stream music over ActivityPub

Features:
- Custom post types: Artist, Album, Track, Playlist
- Custom taxonomies: Genre, Mood, License
- User roles: Artist, Label
- Admin dashboard with statistics
- Frontend templates and shortcodes
- Audio player with queue management
- ActivityPub integration with actor support
- WooCommerce product types for albums/tracks
- User library with favorites and history
- Notification system (in-app and email)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-28 23:23:05 +01:00

163 lines
6.0 KiB
PHP

<?php
/**
* Featured Artist Widget.
*
* @package WP_FediStream
*/
namespace WP_FediStream\Frontend\Widgets;
use WP_FediStream\Frontend\TemplateLoader;
use WP_FediStream\Plugin;
// Prevent direct file access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Displays a featured artist.
*/
class FeaturedArtistWidget extends \WP_Widget {
/**
* Constructor.
*/
public function __construct() {
parent::__construct(
'fedistream_featured_artist',
__( 'FediStream: Featured Artist', 'wp-fedistream' ),
array(
'description' => __( '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 '<p class="fedistream-error">' . esc_html( $e->getMessage() ) . '</p>';
}
}
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',
)
);
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'wp-fedistream' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<p>
<label>
<input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'random' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'random' ) ); ?>" value="1" <?php checked( $random ); ?>>
<?php esc_html_e( 'Show random artist', 'wp-fedistream' ); ?>
</label>
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'artist_id' ) ); ?>"><?php esc_html_e( 'Or select specific artist:', 'wp-fedistream' ); ?></label>
<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'artist_id' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'artist_id' ) ); ?>">
<option value=""><?php esc_html_e( '-- Select Artist --', 'wp-fedistream' ); ?></option>
<?php foreach ( $artists as $artist ) : ?>
<option value="<?php echo esc_attr( $artist->ID ); ?>" <?php selected( $artist_id, $artist->ID ); ?>>
<?php echo esc_html( $artist->post_title ); ?>
</option>
<?php endforeach; ?>
</select>
</p>
<?php
}
/**
* Sanitize widget form values as they are saved.
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ): array {
$instance = array();
$instance['title'] = ! empty( $new_instance['title'] ) ? sanitize_text_field( $new_instance['title'] ) : '';
$instance['artist_id'] = ! empty( $new_instance['artist_id'] ) ? absint( $new_instance['artist_id'] ) : 0;
$instance['random'] = ! empty( $new_instance['random'] );
return $instance;
}
}