You've already forked wp-fedistream
148 lines
6.0 KiB
PHP
148 lines
6.0 KiB
PHP
|
|
<?php
|
||
|
|
/**
|
||
|
|
* Recent Releases 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 recent album releases.
|
||
|
|
*/
|
||
|
|
class RecentReleasesWidget extends \WP_Widget {
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Constructor.
|
||
|
|
*/
|
||
|
|
public function __construct() {
|
||
|
|
parent::__construct(
|
||
|
|
'fedistream_recent_releases',
|
||
|
|
__( 'FediStream: Recent Releases', 'wp-fedistream' ),
|
||
|
|
array(
|
||
|
|
'description' => __( 'Display recent album releases.', 'wp-fedistream' ),
|
||
|
|
'classname' => 'fedistream-widget fedistream-widget--recent-releases',
|
||
|
|
)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 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'] : __( 'Recent Releases', 'wp-fedistream' );
|
||
|
|
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
|
||
|
|
$count = ! empty( $instance['count'] ) ? absint( $instance['count'] ) : 5;
|
||
|
|
$type = ! empty( $instance['type'] ) ? $instance['type'] : '';
|
||
|
|
|
||
|
|
$query_args = array(
|
||
|
|
'post_type' => 'fedistream_album',
|
||
|
|
'posts_per_page' => $count,
|
||
|
|
'orderby' => 'meta_value',
|
||
|
|
'meta_key' => '_fedistream_release_date',
|
||
|
|
'order' => 'DESC',
|
||
|
|
'post_status' => 'publish',
|
||
|
|
);
|
||
|
|
|
||
|
|
if ( ! empty( $type ) ) {
|
||
|
|
$query_args['meta_query'][] = array(
|
||
|
|
'key' => '_fedistream_album_type',
|
||
|
|
'value' => $type,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
$query = new \WP_Query( $query_args );
|
||
|
|
$posts = array();
|
||
|
|
|
||
|
|
if ( $query->have_posts() ) {
|
||
|
|
while ( $query->have_posts() ) {
|
||
|
|
$query->the_post();
|
||
|
|
$posts[] = TemplateLoader::get_album_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/recent-releases',
|
||
|
|
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'] : __( 'Recent Releases', 'wp-fedistream' );
|
||
|
|
$count = ! empty( $instance['count'] ) ? absint( $instance['count'] ) : 5;
|
||
|
|
$type = ! empty( $instance['type'] ) ? $instance['type'] : '';
|
||
|
|
?>
|
||
|
|
<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 releases:', '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>
|
||
|
|
<p>
|
||
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'type' ) ); ?>"><?php esc_html_e( 'Release type:', 'wp-fedistream' ); ?></label>
|
||
|
|
<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'type' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'type' ) ); ?>">
|
||
|
|
<option value="" <?php selected( $type, '' ); ?>><?php esc_html_e( 'All types', 'wp-fedistream' ); ?></option>
|
||
|
|
<option value="album" <?php selected( $type, 'album' ); ?>><?php esc_html_e( 'Album', 'wp-fedistream' ); ?></option>
|
||
|
|
<option value="ep" <?php selected( $type, 'ep' ); ?>><?php esc_html_e( 'EP', 'wp-fedistream' ); ?></option>
|
||
|
|
<option value="single" <?php selected( $type, 'single' ); ?>><?php esc_html_e( 'Single', 'wp-fedistream' ); ?></option>
|
||
|
|
<option value="compilation" <?php selected( $type, 'compilation' ); ?>><?php esc_html_e( 'Compilation', 'wp-fedistream' ); ?></option>
|
||
|
|
</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['count'] = ! empty( $new_instance['count'] ) ? absint( $new_instance['count'] ) : 5;
|
||
|
|
$instance['type'] = ! empty( $new_instance['type'] ) ? sanitize_key( $new_instance['type'] ) : '';
|
||
|
|
return $instance;
|
||
|
|
}
|
||
|
|
}
|