Advertisement
Guest User

Untitled

a guest
Aug 20th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. /**
  2. * WPSE Category Specific Widget
  3. *
  4. * This widget will change post according to the current category being viewed.
  5. *
  6. * @package WPSE Category Specific Widget
  7. * @author Pieter Goosen
  8. * @license GPL-2.0+
  9. * @requires PHP 5.4+
  10. * @requires Categories Images Plugin https://wordpress.org/plugins/categories-images/
  11. * @ref http://wordpress.stackexchange.com/q/158337/31545
  12. *
  13. */
  14.  
  15. class WPSE_Category_Specific_Widget extends WP_Widget {
  16.  
  17. /**
  18. * wpse-category-specific-widget
  19. *
  20. * Unique identifier for your widget.
  21. *
  22. *
  23. * The variable name is used as the text domain when internationalizing strings
  24. * of text. Its value should match the Text Domain file header in the main
  25. * widget file.
  26. *
  27. * @since 1.0.0
  28. *
  29. * @var string
  30. */
  31. protected $widget_slug = 'wpse-category-specific-widget';
  32.  
  33. /*--------------------------------------------------*/
  34. /* Constructor
  35. /*--------------------------------------------------*/
  36.  
  37. /**
  38. * Specifies the class name and description, instantiates the widget,
  39. * loads localization files, and includes necessary stylesheets and JavaScript.
  40. */
  41. public function __construct() {
  42.  
  43. parent::__construct(
  44. $this->get_widget_slug(),
  45. __( 'WPSE Category Specific Widget', $this->get_widget_slug() ),
  46. array(
  47. 'classname' => $this->get_widget_slug().'-class',
  48. 'description' => __( 'This widget will change post according to the current category being viewed.', $this->get_widget_slug() )
  49. )
  50. );
  51.  
  52.  
  53. // Refreshing the widget's cached output with each new post
  54. add_action( 'save_post', array( $this, 'flush_widget_cache' ) );
  55. add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) );
  56. add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) );
  57.  
  58. } // end constructor
  59.  
  60.  
  61. /**
  62. * Return the widget slug.
  63. *
  64. * @since 1.0.0
  65. *
  66. */
  67. public function get_widget_slug() {
  68. return $this->widget_slug;
  69. }
  70.  
  71. /*--------------------------------------------------*/
  72. /* Widget API Functions
  73. /*--------------------------------------------------*/
  74.  
  75. /**
  76. * Outputs the content of the widget.
  77. *
  78. * @param array args The array of form elements
  79. * @param array instance The current instance of the widget
  80. */
  81. public function widget( $args, $instance ) {
  82.  
  83.  
  84. // Check if there is a cached output
  85. $cache = wp_cache_get( $this->get_widget_slug(), 'widget' );
  86.  
  87. if ( !is_array( $cache ) )
  88. $cache = array();
  89.  
  90. if ( ! isset ( $args['widget_id'] ) )
  91. $args['widget_id'] = $this->id;
  92.  
  93. if ( isset ( $cache[ $args['widget_id'] ] ) )
  94. return print $cache[ $args['widget_id'] ];
  95.  
  96. // go on with your widget logic, put everything into a string and …
  97.  
  98.  
  99. extract( $args, EXTR_SKIP );
  100.  
  101. $widget_string = $before_widget;
  102.  
  103.  
  104. ob_start();
  105.  
  106. if( is_category() || is_tax() ) {
  107.  
  108. // Get the image from the category or taxonomy term
  109. if (function_exists('z_taxonomy_image_url')) {
  110.  
  111. echo '<p><img src="' . z_taxonomy_image_url() . '"/></p>';
  112.  
  113. }
  114.  
  115. // Create the custom query
  116. $queried_object = get_queried_object( 'term' );
  117. $taxonomy = $queried_object->taxonomy;
  118. $term = $queried_object->term_id;
  119.  
  120. $args = [
  121. 'posts_per_page' => 1,
  122. 'order' => 'DESC',
  123. 'orderby' => 'rand',
  124. 'tax_query' => [
  125. [
  126. 'taxonomy' => $taxonomy,
  127. 'field' => 'term_id',
  128. 'terms' => $term
  129. ]
  130. ]
  131. ];
  132.  
  133. }elseif( !is_404() ) {
  134.  
  135. $args = [
  136. 'post_type' => 'any',
  137. 'posts_per_page' => 1,
  138. 'orderby' => 'rand'
  139. ];
  140. }
  141.  
  142.  
  143. $q = new WP_Query( $args );
  144.  
  145. if($q->have_posts()) {
  146. while($q->have_posts()) {
  147. $q->the_post(); ?>
  148.  
  149. <p><?php the_title(); // Display post title ?></p>
  150.  
  151. <?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
  152. the_post_thumbnail();
  153. } ?>
  154.  
  155. <p><?php the_excerpt(); // Show an excerpt of the post ?></p>
  156.  
  157. <?php }
  158. }
  159. wp_reset_postdata();
  160.  
  161. $widget_string .= ob_get_clean();
  162. $widget_string .= $after_widget;
  163.  
  164.  
  165. $cache[ $args['widget_id'] ] = $widget_string;
  166.  
  167. wp_cache_set( $this->get_widget_slug(), $cache, 'widget' );
  168.  
  169. print $widget_string;
  170.  
  171. } // end widget
  172.  
  173.  
  174. public function flush_widget_cache()
  175. {
  176. wp_cache_delete( $this->get_widget_slug(), 'widget' );
  177. }
  178. /**
  179. * Processes the widget's options to be saved.
  180. *
  181. * @param array new_instance The new instance of values to be generated via the update.
  182. * @param array old_instance The previous instance of values before the update.
  183. */
  184. public function update( $new_instance, $old_instance ) {
  185.  
  186. $instance = $old_instance;
  187.  
  188. // TODO: Here is where you update your widget's old values with the new, incoming values
  189.  
  190. return $instance;
  191.  
  192. } // end widget
  193.  
  194. /**
  195. * Generates the administration form for the widget.
  196. *
  197. * @param array instance The array of keys and values for the widget.
  198. */
  199. public function form( $instance ) {
  200.  
  201. // TODO: Define default values for your variables
  202. $instance = wp_parse_args(
  203. (array) $instance
  204. );
  205.  
  206.  
  207. } // end form
  208.  
  209.  
  210. } // end class
  211.  
  212. add_action( 'widgets_init', function(){
  213. register_widget( 'WPSE_Category_Specific_Widget' );
  214. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement