PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/wp-content/themes/twentyeleven/inc/widgets.php

https://bitbucket.org/sergiohzlz/reportaprod
PHP | 164 lines | 104 code | 29 blank | 31 comment | 12 complexity | 889cd631e6a67ef93d8fba9943d6f068 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Makes a custom Widget for displaying Aside, Link, Status, and Quote Posts available with Twenty Eleven
  4. *
  5. * Learn more: http://codex.wordpress.org/Widgets_API#Developing_Widgets
  6. *
  7. * @package WordPress
  8. * @subpackage Twenty_Eleven
  9. * @since Twenty Eleven 1.0
  10. */
  11. class Twenty_Eleven_Ephemera_Widget extends WP_Widget {
  12. /**
  13. * Constructor
  14. *
  15. * @return void
  16. **/
  17. function Twenty_Eleven_Ephemera_Widget() {
  18. $widget_ops = array( 'classname' => 'widget_twentyeleven_ephemera', 'description' => __( 'Use this widget to list your recent Aside, Status, Quote, and Link posts', 'twentyeleven' ) );
  19. $this->WP_Widget( 'widget_twentyeleven_ephemera', __( 'Twenty Eleven Ephemera', 'twentyeleven' ), $widget_ops );
  20. $this->alt_option_name = 'widget_twentyeleven_ephemera';
  21. add_action( 'save_post', array(&$this, 'flush_widget_cache' ) );
  22. add_action( 'deleted_post', array(&$this, 'flush_widget_cache' ) );
  23. add_action( 'switch_theme', array(&$this, 'flush_widget_cache' ) );
  24. }
  25. /**
  26. * Outputs the HTML for this widget.
  27. *
  28. * @param array An array of standard parameters for widgets in this theme
  29. * @param array An array of settings for this widget instance
  30. * @return void Echoes it's output
  31. **/
  32. function widget( $args, $instance ) {
  33. $cache = wp_cache_get( 'widget_twentyeleven_ephemera', 'widget' );
  34. if ( !is_array( $cache ) )
  35. $cache = array();
  36. if ( ! isset( $args['widget_id'] ) )
  37. $args['widget_id'] = null;
  38. if ( isset( $cache[$args['widget_id']] ) ) {
  39. echo $cache[$args['widget_id']];
  40. return;
  41. }
  42. ob_start();
  43. extract( $args, EXTR_SKIP );
  44. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Ephemera', 'twentyeleven' ) : $instance['title'], $instance, $this->id_base);
  45. if ( ! isset( $instance['number'] ) )
  46. $instance['number'] = '10';
  47. if ( ! $number = absint( $instance['number'] ) )
  48. $number = 10;
  49. $ephemera_args = array(
  50. 'order' => 'DESC',
  51. 'posts_per_page' => $number,
  52. 'no_found_rows' => true,
  53. 'post_status' => 'publish',
  54. 'post__not_in' => get_option( 'sticky_posts' ),
  55. 'tax_query' => array(
  56. array(
  57. 'taxonomy' => 'post_format',
  58. 'terms' => array( 'post-format-aside', 'post-format-link', 'post-format-status', 'post-format-quote' ),
  59. 'field' => 'slug',
  60. 'operator' => 'IN',
  61. ),
  62. ),
  63. );
  64. $ephemera = new WP_Query( $ephemera_args );
  65. if ( $ephemera->have_posts() ) :
  66. echo $before_widget;
  67. echo $before_title;
  68. echo $title; // Can set this with a widget option, or omit altogether
  69. echo $after_title;
  70. ?>
  71. <ol>
  72. <?php while ( $ephemera->have_posts() ) : $ephemera->the_post(); ?>
  73. <?php if ( 'link' != get_post_format() ) : ?>
  74. <li class="widget-entry-title">
  75. <a href="<?php echo esc_url( get_permalink() ); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
  76. <span class="comments-link">
  77. <?php comments_popup_link( __( '0 <span class="reply">comments &rarr;</span>', 'twentyeleven' ), __( '1 <span class="reply">comment &rarr;</span>', 'twentyeleven' ), __( '% <span class="reply">comments &rarr;</span>', 'twentyeleven' ) ); ?>
  78. </span>
  79. </li>
  80. <?php else : ?>
  81. <li class="widget-entry-title">
  82. <?php
  83. // Grab first link from the post content. If none found, use the post permalink as fallback.
  84. $link_url = twentyeleven_url_grabber();
  85. if ( empty( $link_url ) )
  86. $link_url = get_permalink();
  87. ?>
  88. <a href="<?php echo esc_url( $link_url ); ?>" title="<?php printf( esc_attr__( 'Link to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?>&nbsp;<span>&rarr;</span></a>
  89. <span class="comments-link">
  90. <?php comments_popup_link( __( '0 <span class="reply">comments &rarr;</span>', 'twentyeleven' ), __( '1 <span class="reply">comment &rarr;</span>', 'twentyeleven' ), __( '% <span class="reply">comments &rarr;</span>', 'twentyeleven' ) ); ?>
  91. </span>
  92. </li>
  93. <?php endif; ?>
  94. <?php endwhile; ?>
  95. </ol>
  96. <?php
  97. echo $after_widget;
  98. // Reset the post globals as this query will have stomped on it
  99. wp_reset_postdata();
  100. // end check for ephemeral posts
  101. endif;
  102. $cache[$args['widget_id']] = ob_get_flush();
  103. wp_cache_set( 'widget_twentyeleven_ephemera', $cache, 'widget' );
  104. }
  105. /**
  106. * Deals with the settings when they are saved by the admin. Here is
  107. * where any validation should be dealt with.
  108. **/
  109. function update( $new_instance, $old_instance ) {
  110. $instance = $old_instance;
  111. $instance['title'] = strip_tags( $new_instance['title'] );
  112. $instance['number'] = (int) $new_instance['number'];
  113. $this->flush_widget_cache();
  114. $alloptions = wp_cache_get( 'alloptions', 'options' );
  115. if ( isset( $alloptions['widget_twentyeleven_ephemera'] ) )
  116. delete_option( 'widget_twentyeleven_ephemera' );
  117. return $instance;
  118. }
  119. function flush_widget_cache() {
  120. wp_cache_delete( 'widget_twentyeleven_ephemera', 'widget' );
  121. }
  122. /**
  123. * Displays the form for this widget on the Widgets page of the WP Admin area.
  124. **/
  125. function form( $instance ) {
  126. $title = isset( $instance['title']) ? esc_attr( $instance['title'] ) : '';
  127. $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 10;
  128. ?>
  129. <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'twentyeleven' ); ?></label>
  130. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
  131. <p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of posts to show:', 'twentyeleven' ); ?></label>
  132. <input id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" type="text" value="<?php echo esc_attr( $number ); ?>" size="3" /></p>
  133. <?php
  134. }
  135. }