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

Description #

Widget output.

Parameters #

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

Source #

File: addons/categories/widget.php

	public function widget( $args, $instance ) {
		$instance = wp_parse_args(
			$instance,
			array(
				'title'       => __( 'Categories', 'anspress-question-answer' ),
				'hide_empty'  => false,
				'parent'      => 0,
				'number'      => 10,
				'orderby'     => 'count',
				'order'       => 'DESC',
				'icon_width'  => 32,
				'icon_height' => 32,
			)
		);

		/**
		 * This filter is documented in widgets/question_stats.php
		 */
		$title = apply_filters( 'widget_title', $instance['title'] );

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

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

		$cat_args = array(
			'taxonomy'   => 'question_category',
			'parent'     => $instance['parent'],
			'number'     => $instance['number'],
			'hide_empty' => $instance['hide_empty'],
			'orderby'    => $instance['orderby'],
			'order'      => $instance['order'],
		);

		$icon_width  = ( ! empty( $instance['icon_width'] ) && is_numeric( $instance['icon_width'] ) ) ? $instance['icon_width'] : 32;
		$icon_height = ( ! empty( $instance['icon_height'] ) && is_numeric( $instance['icon_height'] ) ) ? $instance['icon_height'] : 32;

		$categories = get_terms( $cat_args );
		?>
		<ul id="ap-categories-widget" class="ap-cat-wid clearfix">
		<?php
		foreach ( (array) $categories as $key => $category ) :
			$ap_category   = get_term_meta( $category->term_id, 'ap_category', true );
			$ap_category   = wp_parse_args(
				$ap_category,
				array(
					'color' => '#333',
					'icon'  => 'apicon-category',
				)
			);
			$cat_color     = ! empty( $ap_category['color'] ) ? $ap_category['color'] : '#333';
			$sub_cat_count = count( get_term_children( $category->term_id, 'question_category' ) );
			?>
			<li class="clearfix">
			<a class="ap-cat-image" style="height:<?php echo esc_attr( $icon_height ); ?>px;width:<?php echo esc_attr( $icon_width ); ?>px;background: <?php echo esc_attr( $cat_color ); ?>" href="<?php echo esc_url( get_category_link( $category ) ); ?>">
				<span class="ap-category-icon <?php echo esc_attr( $ap_category['icon'] ); ?>"></span>
			</a>
			<a class="ap-cat-wid-title" href="<?php echo esc_url( get_category_link( $category ) ); ?>">
				<?php echo esc_html( $category->name ); ?>
			</a>
			<div class="ap-cat-count">
				<span>
					<?php
						// translators: Placeholder contains questions count.
						echo esc_attr( sprintf( _n( '%d Question', '%d Questions', $category->count, 'anspress-question-answer' ), (int) $category->count ) );
					?>
				</span>
				<?php if ( $sub_cat_count > 0 ) : ?>
					<span>
						<?php
							// translators: Placeholder contains child category count.
							printf( esc_attr__( '%d Child', 'anspress-question-answer' ), (int) $sub_cat_count );
						?>
					</span>
				<?php endif; ?>
			</div>
			</li>
			<?php endforeach; ?>
		</ul>
		<?php
		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