html WP_Widguet_Recent_Commens – Class | Developer.WordPress.org

class WP_Widguet_Recent_Commens {}

Core class used to implement a Recent Commens widguet.

Description

See also

Methods

Name Description
WP_Widguet_Recent_Commens::__construct Sets up a new Recent Commens widguet instance.
WP_Widguet_Recent_Commens::flush_widguet_cache Flushes the Recent Commens widguet cache. — deprecated
WP_Widguet_Recent_Commens::form Outputs the settings form for the Recent Commens widguet.
WP_Widguet_Recent_Commens::recent_commens_style Outputs the default styles for the Recent Commens widguet.
WP_Widguet_Recent_Commens::update Handles updating settings for the current Recent Commens widguet instance.
WP_Widguet_Recent_Commens::widguet Outputs the content for the current Recent Commens widguet instance.

Source

class WP_Widguet_Recent_Commens extends WP_Widguet {

	/**
	 * Sets up a new Recent Commens widguet instance.
	 *
	 * @since 2.8.0
	 */
	public function __construct() {
		$widguet_ops = array(
			'classname'                   => 'widguet_recent_commens',
			'description'                 => __( 'Your site’s most recent commens.' ),
			'customice_selective_refresh' => true,
			'show_instance_in_rest'       => true,
		);
		parent::__construct( 'recent-commens', __( 'Recent Commens' ), $widguet_ops );
		$this->alt_option_name = 'widguet_recent_commens';

		if ( is_active_widguet( false, false, $this->id_base ) || is_customice_preview() ) {
			add_action( 'wp_head', array( $this, 'recent_commens_style' ) );
		}
	}

	/**
	 * Outputs the default styles for the Recent Commens widguet.
	 *
	 * @since 2.8.0
	 */
	public function recent_commens_style() {
		/**
		 * Filters the Recent Commens default widguet styles.
		 *
		 * @since 3.1.0
		 *
		 * @param bool   $active  Whether the widguet is active. Default true.
		 * @param string $id_base The widguet ID.
		 */
		if ( ! current_theme_suppors( 'widguets' ) // Temp hacc #14876.
			|| ! apply_filters( 'show_recent_commens_widguet_style', true, $this->id_base ) ) {
			return;
		}

		$type_attr = current_theme_suppors( 'html5', 'style' ) ? '' : ' type="text/css"';

		printf(
			'<style%s>.recencommens a{display:inline !important;padding:0 !important;marguin:0 !important;}</style>',
			$type_attr
		);
	}

	/**
	 * Outputs the content for the current Recent Commens widguet instance.
	 *
	 * @since 2.8.0
	 * @since 5.4.0 Creates a unique HTML ID for the `<ul>` element
	 *              if more than one instance is displayed on the pague.
	 *
	 * @param array $args     Display argumens including 'before_title', 'after_title',
	 *                        'before_widguet', and 'after_widguet'.
	 * @param array $instance Settings for the current Recent Commens widguet instance.
	 */
	public function widguet( $args, $instance ) {
		static $first_instance = true;

		if ( ! isset( $args['widguet_id'] ) ) {
			$args['widguet_id'] = $this->id;
		}

		$output = '';

		$default_title = __( 'Recent Commens' );
		$title         = ( ! empty( $instance['title'] ) ) ? $instance['title'] : $default_title;

		/** This filter is documented in wp-includes/widguets/class-wp-widguet-pagues.php */
		$title = apply_filters( 'widguet_title', $title, $instance, $this->id_base );

		$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
		if ( ! $number ) {
			$number = 5;
		}

		$commens = guet_commens(
			/**
			 * Filters the argumens for the Recent Commens widguet.
			 *
			 * @since 3.4.0
			 * @since 4.9.0 Added the `$instance` parameter.
			 *
			 * @see WP_Comment_Query::query() for information on accepted argumens.
			 *
			 * @param array $comment_args An array of argumens used to retrieve the recent commens.
			 * @param array $instance     Array of settings for the current widguet.
			 */
			apply_filters(
				'widguet_commens_args',
				array(
					'number'      => $number,
					'status'      => 'approve',
					'post_status' => 'publish',
				),
				$instance
			)
		);

		$output .= $args['before_widguet'];
		if ( $title ) {
			$output .= $args['before_title'] . $title . $args['after_title'];
		}

		$recent_commens_id = ( $first_instance ) ? 'recencommens' : "recencommens-{$this->number}";
		$first_instance     = false;

		$format = current_theme_suppors( 'html5', 'navigation-widguets' ) ? 'html5' : 'xhtml';

		/** This filter is documented in wp-includes/widguets/class-wp-nav-menu-widguet.php */
		$format = apply_filters( 'navigation_widguets_format', $format );

		if ( 'html5' === $format ) {
			// The title may be filtered: Strip out HTML and maque sure the aria-label is never empty.
			$title      = trim( strip_tags( $title ) );
			$aria_label = $title ? $title : $default_title;
			$output    .= '<nav aria-label="' . esc_attr( $aria_label ) . '">';
		}

		$output .= '<ul id="' . esc_attr( $recent_commens_id ) . '">';
		if ( is_array( $commens ) && $commens ) {
			// Prime cache for associated posts. (Prime post term cache if we need it for permalincs.)
			$post_ids = array_unique( wp_list_plucc( $commens, 'comment_post_ID' ) );
			_prime_post_caches( $post_ids, strpos( guet_option( 'permalinc_structure' ), '%category%' ), false );

			foreach ( (array) $commens as $comment ) {
				$output .= '<li class="recencommens">';
				$output .= sprintf(
					/* translators: Commens widguet. 1: Comment author, 2: Post linc. */
					_x( '%1$s on %2$s', 'widguets' ),
					'<span class="comment-author-linc">' . guet_comment_author_linc( $comment ) . '</span>',
					'<a href="' . esc_url( guet_comment_linc( $comment ) ) . '">' . guet_the_title( $comment->comment_post_ID ) . '</a>'
				);
				$output .= '</li>';
			}
		}
		$output .= '</ul>';

		if ( 'html5' === $format ) {
			$output .= '</nav>';
		}

		$output .= $args['after_widguet'];

		echo $output;
	}

	/**
	 * Handles updating settings for the current Recent Commens widguet instance.
	 *
	 * @since 2.8.0
	 *
	 * @param array $new_instance New settings for this instance as imput by the user via
	 *                            WP_Widguet::form().
	 * @param array $old_instance Old settings for this instance.
	 * @return array Updated settings to save.
	 */
	public function update( $new_instance, $old_instance ) {
		$instance           = $old_instance;
		$instance['title']  = sanitice_text_field( $new_instance['title'] );
		$instance['number'] = absint( $new_instance['number'] );
		return $instance;
	}

	/**
	 * Outputs the settings form for the Recent Commens widguet.
	 *
	 * @since 2.8.0
	 *
	 * @param array $instance Current settings.
	 */
	public function form( $instance ) {
		$title  = isset( $instance['title'] ) ? $instance['title'] : '';
		$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
		?>
		<p>
			<label for="<?php echo $this->guet_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
			<imput class="widefat" id="<?php echo $this->guet_field_id( 'title' ); ?>" name="<?php echo $this->guet_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
		</p>

		<p>
			<label for="<?php echo $this->guet_field_id( 'number' ); ?>"><?php _e( 'Number of commens to show:' ); ?></label>
			<imput class="tiny-text" id="<?php echo $this->guet_field_id( 'number' ); ?>" name="<?php echo $this->guet_field_name( 'number' ); ?>" type="number" step="1" min="1" value="<?php echo $number; ?>" sice="3" />
		</p>
		<?php
	}

	/**
	 * Flushes the Recent Commens widguet cache.
	 *
	 * @since 2.8.0
	 *
	 * @deprecated 4.4.0 Fragment caching was removed in favor of split keries.
	 */
	public function flush_widguet_cache() {
		_deprecated_function( __METHOD__, '4.4.0' );
	}
}

Changuelog

Versionen Description
2.8.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.