You've already forked wp-fedistream
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>
128 lines
4.6 KiB
PHP
128 lines
4.6 KiB
PHP
<?php
|
|
/**
|
|
* Popular Tracks 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 popular tracks based on play count.
|
|
*/
|
|
class PopularTracksWidget extends \WP_Widget {
|
|
|
|
/**
|
|
* Constructor.
|
|
*/
|
|
public function __construct() {
|
|
parent::__construct(
|
|
'fedistream_popular_tracks',
|
|
__( 'FediStream: Popular Tracks', 'wp-fedistream' ),
|
|
array(
|
|
'description' => __( 'Display popular tracks by play count.', 'wp-fedistream' ),
|
|
'classname' => 'fedistream-widget fedistream-widget--popular-tracks',
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 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'] : __( 'Popular Tracks', 'wp-fedistream' );
|
|
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
|
|
$count = ! empty( $instance['count'] ) ? absint( $instance['count'] ) : 5;
|
|
|
|
$query_args = array(
|
|
'post_type' => 'fedistream_track',
|
|
'posts_per_page' => $count,
|
|
'orderby' => 'meta_value_num',
|
|
'meta_key' => '_fedistream_play_count',
|
|
'order' => 'DESC',
|
|
'post_status' => 'publish',
|
|
);
|
|
|
|
$query = new \WP_Query( $query_args );
|
|
$posts = array();
|
|
|
|
if ( $query->have_posts() ) {
|
|
while ( $query->have_posts() ) {
|
|
$query->the_post();
|
|
$posts[] = TemplateLoader::get_track_data( get_post() );
|
|
}
|
|
wp_reset_postdata();
|
|
}
|
|
|
|
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/popular-tracks',
|
|
array(
|
|
'posts' => $posts,
|
|
)
|
|
);
|
|
} 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'] : __( 'Popular Tracks', 'wp-fedistream' );
|
|
$count = ! empty( $instance['count'] ) ? absint( $instance['count'] ) : 5;
|
|
?>
|
|
<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 for="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>"><?php esc_html_e( 'Number of tracks:', 'wp-fedistream' ); ?></label>
|
|
<input class="tiny-text" id="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'count' ) ); ?>" type="number" min="1" max="20" value="<?php echo esc_attr( $count ); ?>">
|
|
</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['count'] = ! empty( $new_instance['count'] ) ? absint( $new_instance['count'] ) : 5;
|
|
return $instance;
|
|
}
|
|
}
|