* @package AnyComment\Widgets */ class CommentList extends \WP_Widget { /** * Sets up a new Recent Comments widget instance. * * @since 2.8.0 */ public function __construct () { $widget_ops = array( 'classname' => 'CommentList', 'description' => __( 'Your site’s most recent comments displayed using AnyComment.' ), 'customize_selective_refresh' => true, ); parent::__construct( 'anycomment-comment-list', __( 'Comments', 'anycomment' ), $widget_ops ); $this->alt_option_name = 'anycomment-comment-list'; } /** * Outputs the default styles for the Recent Comments widget. * * @param array $instance * * @return string */ public function compile_styles ( $instance ) { $mtime = filemtime( AnyComment()->plugin_path() . '/assets/widgets/comment-list/comment-list.scss' ); $array_hash = []; $array_hash[] = $mtime; $array_hash[] = $instance; $hash = md5( serialize( $array_hash ) ); $scss_widget_cache = AnyCommentCore::cache()->getItem( 'anycomment/widgets/comment-list/' . $hash ); $template = ''; if ( $scss_widget_cache->isHit() ) { return sprintf( $template, $scss_widget_cache->get() ); } $variables = []; if ( ! empty( $instance ) ) { foreach ( $instance as $key => $value ) { if ( false !== strpos( $key, 'scss_' ) ) { $clean_key = trim( str_replace( 'scss_', '', $key ) ); $clean_key = str_replace( '_', '-', $clean_key ); $variables[ $clean_key ] = $value; } } } $compiler = new ScssCompiler(); $compiled_css = $compiler ->set_scss( [ AnyComment()->plugin_path() . '/assets/widgets/comment-list/comment-list.scss' ] ) ->set_variables( $variables ) ->compile(); $scss_widget_cache->set( $compiled_css )->save(); return sprintf( $template, $compiled_css ); } /** * Outputs the content for the current Recent Comments widget instance. * * @since 2.8.0 * * @param array $args Display arguments including 'before_title', 'after_title', * 'before_widget', and 'after_widget'. * @param array $instance Settings for the current Recent Comments widget instance. */ public function widget ( $args, $instance ) { $output = $this->compile_styles( $instance ); if ( ! isset( $args['widget_id'] ) ) { $args['widget_id'] = $this->id; } $title = ! empty( $instance['title'] ) ? $instance['title'] : ''; $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5; $read_more_limit = ( ! empty( $instance['read_more_limit'] ) ) ? absint( $instance['read_more_limit'] ) : 10; if ( ! $number ) { $number = 5; } /** * Filters the arguments for the Recent Comments widget. * * @see WP_Comment_Query::query() for information on accepted arguments. * * @param array $comment_args An array of arguments used to retrieve the recent comments. * @param array $instance Array of settings for the current widget. */ $comments = get_comments( apply_filters( 'widget_comments_args', array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish', ), $instance ) ); $output .= $args['before_widget']; if ( $title ) { $output .= $args['before_title'] . $title . $args['after_title']; } $output .= '