PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/book-review-library/inc/widgets.php

https://gitlab.com/rayfrek/wordpress.tm
PHP | 278 lines | 219 code | 34 blank | 25 comment | 42 complexity | 2c238ab05ddb8a0e7fa971a6aeb0bb88 MD5 | raw file
  1. <?php
  2. /**
  3. * All the widgets live here
  4. *
  5. * @package Book_Reviews
  6. * @author Chris Reynolds <hello@chrisreynolds.io>
  7. * @license GPL-3.0
  8. * @link http://chrisreynolds.io
  9. * @copyright 2014 Chris Reynolds
  10. */
  11. /**
  12. * Book Review Related Books Widget
  13. *
  14. * @since 1.0.0
  15. */
  16. class Book_Review_Widget extends WP_Widget {
  17. public function __construct() {
  18. $widget_options = array( 'classname' => 'book_review_widget', 'description' => __('Displays a list of related books by common taxonomies. This widget only displays when viewing a single book review or a book review archive.', 'book-review-library') );
  19. $control_options = array( 'id_base' => 'book-review-widget' );
  20. parent::__construct( 'book-review-widget', 'Similar Books', $widget_options, $control_options );
  21. }
  22. public function widget( $args, $instance ) {
  23. global $wp_query;
  24. $this_post = $wp_query->post->ID;
  25. extract($args);
  26. // count is the number of items to show
  27. // image toggles whether to display thumbnails
  28. if ( isset( $instance['title'] ) ) { $title = apply_filters( 'widget_title', $instance['title'] ); } else { $title = __('Similar Books', 'book-review-library'); }
  29. if ( isset( $instance['count'] ) ) { $count = $instance['count']; } else { $count = 3; }
  30. if ( isset( $instance['image'] ) ) { $image = $instance['image']; } else { $image = false; }
  31. if ( is_singular( 'book-review' ) ) {
  32. $genres = wp_get_post_terms($this_post, 'genre');
  33. $subjects = wp_get_post_terms($this_post, 'genre');
  34. if ($subjects || $genres) {
  35. $subject_ids = array();
  36. foreach($subjects as $individual_subject) $subject_ids[] = $individual_subject->term_id;
  37. $genre_ids = array();
  38. foreach($genres as $individual_genre) $genre_ids[] = $individual_genre->term_id;
  39. $query_args=array(
  40. 'tax_query' => array(
  41. array(
  42. 'taxonomy' => 'genre',
  43. 'terms' => $genre_ids,
  44. 'operator' => 'IN'
  45. )
  46. ),
  47. 'post__not_in' => array( $this_post ),
  48. 'posts_per_page' => $count,
  49. 'ignore_sticky_posts' => 1,
  50. 'orderby' => 'rand'
  51. );
  52. $related_query = new WP_Query( $query_args );
  53. }
  54. echo $args['before_widget'];
  55. echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
  56. if( $related_query->have_posts() ) { ?>
  57. <div class="related">
  58. <ul>
  59. <?php while( $related_query->have_posts() ) {
  60. $related_query->the_post(); ?>
  61. <li>
  62. <?php if ( $image && has_post_thumbnail()) : ?>
  63. <a href="<?php the_permalink(); ?>">
  64. <?php the_post_thumbnail( 'tiny', array('class' => 'alignleft') ); ?>
  65. </a>
  66. <?php endif; ?>
  67. <?php /* translators: 1: title, 2: author */ ?>
  68. <?php echo sprintf( __('%1$s by %2$s', 'book-review-library'), '<a href="' . get_permalink() . '">' . get_the_title() . '</a><br />', get_book_author() ); ?>
  69. </li>
  70. <?php } ?>
  71. </ul>
  72. </div>
  73. <?php
  74. } else {
  75. _e ('No similar books were found.', 'book-review-library');
  76. }
  77. echo $args['after_widget'];
  78. } elseif ( is_tax() ) {
  79. $taxonomy = get_query_var('taxonomy');
  80. $term = get_query_var('term');
  81. $term_id = get_term_by( 'slug', $term, $taxonomy )->term_id;
  82. $tax_args = array(
  83. 'tax_query' => array(
  84. array(
  85. 'taxonomy' => $taxonomy,
  86. 'terms' => array($term_id),
  87. 'operator' => 'IN'
  88. )),
  89. 'post__not_in' => array( $this_post ),
  90. 'post_per_page' => $count,
  91. 'ignore_sticky_posts' => 1,
  92. 'orderby' => 'rand'
  93. );
  94. $tax_query = new WP_Query($tax_args);
  95. if ( $tax_query->have_posts() ) {
  96. echo $args['before_widget'];
  97. echo $args['before_title'] . esc_html( $title ) . $args['after_title']; ?>
  98. <div class="related">
  99. <ul>
  100. <?php while( $tax_query->have_posts() ) {
  101. $tax_query->the_post(); ?>
  102. <li>
  103. <?php if ( $image && has_post_thumbnail()) : ?>
  104. <a href="<?php the_permalink(); ?>">
  105. <?php the_post_thumbnail( 'tiny', array('class' => 'alignleft') ); ?>
  106. </a>
  107. <?php endif; ?>
  108. <?php if ( get_book_author() ) {
  109. /* translators: 1: title, 2: author */
  110. echo sprintf( __('%1$s by %2$s', 'book-review-library'), '<a href="' . get_permalink() . '">' . get_the_title() . '</a><br />', get_book_author() );
  111. } else {
  112. echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
  113. } ?>
  114. </li>
  115. <?php } ?>
  116. </ul>
  117. </div>
  118. <?php echo $args['after_widget'];
  119. }
  120. }
  121. }
  122. public function form( $instance ) {
  123. $defaults = array( 'title' => __('Similar Books', 'book-review-library'), 'count' => 3, 'image' => false );
  124. $instance = wp_parse_args((array) $instance, $defaults);
  125. $values = array(
  126. array('id' => false, 'text' => __('No', 'book-review-library')),
  127. array('id' => true, 'text' => __('Yes', 'book-review-library')));
  128. if ( isset( $instance['title'] ) ) { $title = apply_filters( 'widget_title', $instance['title'] ); } else { $title = $defaults['title']; }
  129. if ( isset( $instance['count'] ) ) { $count = $instance['count']; } else { $count = $defaults['count']; }
  130. if ( isset( $instance['image'] ) ) { $image = $instance['image']; } else { $image = $defaults['image']; }
  131. ?>
  132. <p>
  133. <label for="<?php echo $this->get_field_name('title'); ?>"><?php _e( 'Title:', 'book-review-library' ); ?></label>
  134. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
  135. <span class="description"><?php _e('The title that displays above the widget.', 'book-review-library'); ?></span>
  136. </p>
  137. <p>
  138. <label for="<?php echo $this->get_field_name('count'); ?>"><?php _e( 'Count:', 'book-review-library' ); ?></label>
  139. <input id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo esc_attr( $count ); ?>" size="3" /><br />
  140. <span class="description"><?php _e( 'How many reviews to display.','book-review-library'); ?></span>
  141. </p>
  142. <p>
  143. <label for="<?php echo $this->get_field_name('image'); ?>"><?php _e( 'Display Images:', 'book-review-library' ); ?></label>
  144. <?php echo the_select_box($this->get_field_name('image'), $values, $instance['image']); ?><br />
  145. <span class="description"><?php _e('Display thumbnail images next to the titles?', 'book-review-library'); ?></span>
  146. </p>
  147. <?php
  148. }
  149. public function update( $new_instance, $old_instance ) {
  150. $instance = $old_instance;
  151. $instance['title'] = ( !empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
  152. $instance['count'] = ( !empty( $new_instance['count'] ) ) ? strip_tags( $new_instance['count'] ) : '';
  153. $instance['image'] = ( !empty( $new_instance['image'] ) ) ? strip_tags( $new_instance['image'] ) : '';
  154. return $instance;
  155. }
  156. }
  157. /**
  158. * Book Review Recent Books Widget
  159. *
  160. * @since 1.0.0
  161. */
  162. class Book_Review_Recent_Widget extends WP_Widget {
  163. public function __construct() {
  164. $widget_options = array( 'classname' => 'recent_book_review_widget', 'description' => __('Displays a list of recent book reviews.', 'book-review-library') );
  165. $control_options = array( 'id_base' => 'recent-book-review-widget' );
  166. parent::__construct( 'recent-book-review-widget', __('Recent Book Reviews', 'book-review-library'), $widget_options, $control_options );
  167. }
  168. public function widget( $args, $instance ) {
  169. extract($args);
  170. // count is the number of items to show
  171. // image toggles whether to display thumbnails
  172. if ( isset( $instance['title'] ) ) { $title = apply_filters( 'widget_title', $instance['title'] ); } else { $title = __('Recent Book Reviews', 'book-review-library'); }
  173. if ( isset( $instance['count'] ) ) { $count = $instance['count']; } else { $count = 3; }
  174. if ( isset( $instance['image'] ) ) { $image = $instance['image']; } else { $image = false; }
  175. $query_args=array(
  176. 'post_type' => 'book-review',
  177. 'posts_per_page' => $count,
  178. 'ignore_sticky_posts' => 1,
  179. );
  180. $recent_query = new WP_Query( $query_args );
  181. echo $args['before_widget'];
  182. echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
  183. if( $recent_query->have_posts() ) { ?>
  184. <div class="related">
  185. <ul>
  186. <?php while( $recent_query->have_posts() ) {
  187. $recent_query->the_post(); ?>
  188. <li>
  189. <?php if ( $image && has_post_thumbnail()) : ?>
  190. <a href="<?php the_permalink(); ?>">
  191. <?php the_post_thumbnail( 'tiny', array('class' => 'alignleft') ); ?>
  192. </a>
  193. <?php endif; ?>
  194. <?php if ( get_book_author() ) {
  195. /* translators: 1: title, 2: author */
  196. echo sprintf( __('%1$s by %2$s', 'book-review-library'), '<a href="' . get_permalink() . '">' . get_the_title() . '</a><br />', get_book_author() );
  197. } else {
  198. echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
  199. } ?>
  200. </li>
  201. <?php } ?>
  202. </ul>
  203. </div>
  204. <?php
  205. } else {
  206. _e ('No books were found.', 'book-review-library');
  207. }
  208. echo $args['after_widget'];
  209. }
  210. public function form( $instance ) {
  211. $defaults = array( 'title' => __('Recent Book Reviews', 'book-review-library'), 'count' => 3, 'image' => false );
  212. $instance = wp_parse_args((array) $instance, $defaults);
  213. $values = array(
  214. array('id' => false, 'text' => __('No', 'book-review-library')),
  215. array('id' => true, 'text' => __('Yes', 'book-review-library')));
  216. if ( isset( $instance['title'] ) ) { $title = apply_filters( 'widget_title', $instance['title'] ); } else { $title = $defaults['title']; }
  217. if ( isset( $instance['count'] ) ) { $count = $instance['count']; } else { $count = $defaults['count']; }
  218. if ( isset( $instance['image'] ) ) { $image = $instance['image']; } else { $image = $defaults['image']; }
  219. ?>
  220. <p>
  221. <label for="<?php echo $this->get_field_name('title'); ?>"><?php _e( 'Title:', 'book-review-library' ); ?></label>
  222. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
  223. <span class="description"><?php _e('The title that displays above the widget.', 'book-review-library'); ?></span>
  224. </p>
  225. <p>
  226. <label for="<?php echo $this->get_field_name('count'); ?>"><?php _e( 'Count:', 'book-review-library' ); ?></label>
  227. <input id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo esc_attr( $count ); ?>" size="3" /><br />
  228. <span class="description"><?php _e( 'How many reviews to display.','book-review-library'); ?></span>
  229. </p>
  230. <p>
  231. <label for="<?php echo $this->get_field_name('image'); ?>"><?php _e( 'Display Images:', 'book-review-library' ); ?></label>
  232. <?php echo the_select_box($this->get_field_name('image'), $values, $instance['image']); ?><br />
  233. <span class="description"><?php _e('Display thumbnail images next to the titles?', 'book-review-library'); ?></span>
  234. </p>
  235. <?php
  236. }
  237. public function update( $new_instance, $old_instance ) {
  238. $instance = $old_instance;
  239. $instance['title'] = ( !empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
  240. $instance['count'] = ( !empty( $new_instance['count'] ) ) ? strip_tags( $new_instance['count'] ) : '';
  241. $instance['image'] = ( !empty( $new_instance['image'] ) ) ? strip_tags( $new_instance['image'] ) : '';
  242. return $instance;
  243. }
  244. }