AnsPress_Stats_Widget::widget( array $args, array $instance )

Description #

Widget render method.

Parameters #

  • $args
    array (Required) Widget arguments.
  • $instance
    array (Required) Widget instance.

Source #

File: widgets/question_stats.php

	public function widget( $args, $instance ) {
		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';

		/**
		 * Filters the widget title.
		 *
		 * @param string $title Widget title.
		 * @since 1.0.0
		 */
		$title = apply_filters( 'widget_title', $title );

		echo wp_kses_post( $args['before_widget'] );

		if ( ! empty( $title ) ) {
			echo wp_kses_post( $args['before_title'] . $title . $args['after_title'] );
		}

		$ans_count   = ap_get_answers_count( get_question_id() );
		$last_active = ap_get_last_active( get_question_id() );
		$view_count  = ap_get_post_field( 'views', get_question_id() );
		$view_count  = is_numeric( $view_count ) ? (int) $view_count : 0;

		echo '<div class="ap-widget-inner">';

		if ( is_question() ) {
			echo '<ul class="ap-stats-widget">';

			echo '<li><span class="stat-label apicon-pulse">' . esc_attr__( 'Active', 'anspress-question-answer' ) . '</span><span class="stat-value"><time class="published updated" itemprop="dateModified" datetime="' . esc_attr( (string) mysql2date( 'c', $last_active ) ) . '">' . esc_html( $last_active ) . '</time></span></li>';

			$views_count = sprintf(
				// translators: Placeholder contains view count.
				_n( '%d time', '%d times', $view_count, 'anspress-question-answer' ),
				$view_count
			);

			echo '<li><span class="stat-label apicon-eye">' . esc_attr__( 'Views', 'anspress-question-answer' ) . '</span><span class="stat-value">' . esc_attr( $views_count ) . '</span></li>';

			$answers_count = sprintf(
				// translators: First and last contain span and middle is answer count.
				_n( '%2$s1%3$s answer', '%2$s%1$d%3$s answers', $ans_count, 'anspress-question-answer' ), // phpcs:ignore WordPress.WP.I18n
				$ans_count,
				'<span data-view="answer_count">',
				'</span>'
			);

			echo '<li><span class="stat-label apicon-answer">' . esc_attr__( 'Answers', 'anspress-question-answer' ) . '</span><span class="stat-value">' . wp_kses_post( $answers_count ) . '</span></li>';

			echo '</ul>';
		} else {
			esc_attr_e( 'This widget can only be used in single question page', 'anspress-question-answer' );
		}

		echo '</div>';

		echo wp_kses_post( $args['after_widget'] );
	}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment