Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2020
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.72 KB | None | 0 0
  1. <?php
  2. class Widget_Recent_Comments extends WP_Widget
  3. {
  4.   /**
  5.    * Widget_Recent_Comments with  pre_get_posts filter.
  6.    *
  7.    * @see WP_Widget::__construct()
  8.    */
  9.   function __construct()
  10.   {
  11.     unregister_widget('WP_Widget_Recent_Posts');
  12.     $widget_ops = array(
  13.       'classname'                   => 'widget_recent_comments',
  14.       'description'                 => __('Your site&#8217;s most recent comments.') . ' filtrés',
  15.       'classname'                   => 'widget_recent_comments',
  16.       'customize_selective_refresh' => true,
  17.     );
  18.     parent::__construct(false, __('Recent Comments') . ' filtrés', $widget_ops);
  19.     $this->alt_option_name = 'widget_recent_comments';
  20.   }
  21.  
  22.   public function recent_comments_style()
  23.   {
  24.     if (
  25.       !current_theme_supports('widgets')  || !apply_filters('show_recent_comments_widget_style', true, $this->id_base)
  26.     ) {
  27.       return;
  28.     }
  29.     $type_attr = current_theme_supports('html5', 'style') ? '' : ' type="text/css"';
  30.     printf(
  31.       '<style%s>.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>',
  32.       $type_attr
  33.     );
  34.   }
  35.  
  36.  
  37.   /**
  38.    * The widget's HTML output.
  39.    */
  40.   function widget($args, $instance)
  41.   {
  42.     static $first_instance = true;
  43.     if (!isset($args['widget_id'])) {
  44.       $args['widget_id'] = $this->id;
  45.     }
  46.  
  47.     $output = '';
  48.     $title = (!empty($instance['title'])) ? $instance['title'] : __('Recent Comments');
  49.     /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  50.     $title = apply_filters('widget_title', $title, $instance, $this->id_base);
  51.  
  52.     $number = (!empty($instance['number'])) ? absint($instance['number']) : 5;
  53.     if (!$number) {
  54.       $number = 5;
  55.     }
  56.  
  57.     /* checkbox  $instance['filter'] */
  58.     $filter = (!empty($instance['filter'])) ? true : false;
  59.  
  60.     if($filter === true) {
  61.       $URR = new UserRoleRestrict;
  62.      
  63.      
  64.       /**
  65.        * array of roles
  66.        * $posts_filter = ['public', 'subscriber', ... , 'administrator'];
  67.        *
  68.        */
  69.       $posts_filter = $URR->get_filter($URR->my_capabilities());
  70.      
  71.  
  72.       $comments = get_comments(
  73.         apply_filters(
  74.           'widget_comments_args',
  75.           array(
  76.             'number'      => $number,
  77.             'status'      => 'approve',
  78.             'post_status' => 'publish',
  79.             'meta_query'  => array(
  80.               'meta_key'     => 'target',
  81.               'meta_value'   => $posts_filter,
  82.             ),
  83.           ),
  84.           $instance
  85.         )
  86.       );
  87.     }
  88.     else {
  89.       $comments = get_comments(
  90.         /**
  91.          * Filters the arguments for the Recent Comments widget.
  92.          *
  93.          * @since 3.4.0
  94.          * @since 4.9.0 Added the `$instance` parameter.
  95.          *
  96.          * @see WP_Comment_Query::query() for information on accepted arguments.
  97.          *
  98.          * @param array $comment_args An array of arguments used to retrieve the recent comments.
  99.          * @param array $instance     Array of settings for the current widget.
  100.          */
  101.         apply_filters(
  102.           'widget_comments_args',
  103.           array(
  104.             'number'      => $number,
  105.             'status'      => 'approve',
  106.             'post_status' => 'publish',
  107.           ),
  108.           $instance
  109.         )
  110.       );
  111.     }
  112.     $output .= $args['before_widget'];
  113.     if ($title) {
  114.       $output .= $args['before_title'] . $title . $args['after_title'];
  115.     }
  116.  
  117.     $recent_comments_id = ($first_instance) ? 'recentcomments' : "recentcomments-{$this->number}";
  118.     $first_instance     = false;
  119.  
  120.     $output .= '<ul id="' . esc_attr($recent_comments_id) . '">';
  121.     if (is_array($comments) && $comments) {
  122.       // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)
  123.       $post_ids = array_unique(wp_list_pluck($comments, 'comment_post_ID'));
  124.       _prime_post_caches($post_ids, strpos(get_option('permalink_structure'), '%category%'), false);
  125.  
  126.       foreach ((array) $comments as $comment) {
  127.         $output .= '<li class="recentcomments">';
  128.         $output .= sprintf(
  129.           /* translators: Comments widget. 1: Comment author, 2: Post link. */
  130.           _x('%1$s', 'widgets'),
  131.           '<a href="' . esc_url(get_comment_link($comment)) . '">' . get_the_title($comment->comment_post_ID) . '</a>'
  132.         );
  133.         $output .= '</li>';
  134.       }
  135.     }
  136.     $output .= '</ul>';
  137.     $output .= $args['after_widget'];
  138.  
  139.     echo $output;
  140.   }
  141.  
  142.   /**
  143.    * The widget update handler.
  144.    *
  145.    * @see WP_Widget::update()
  146.    *
  147.    * @param array $new_instance The new instance of the widget.
  148.    * @param array $old_instance The old instance of the widget.
  149.    * @return array The updated instance of the widget.
  150.    */
  151.   function update($new_instance, $old_instance)
  152.   {
  153.     $instance           = $old_instance;
  154.     $instance['title']  = sanitize_text_field($new_instance['title']);
  155.     $instance['number'] = absint($new_instance['number']);
  156.     $instance['filter'] = absint($new_instance['filter']);
  157.     return $instance;
  158.   }
  159.  
  160.   /**
  161.    * Output the admin widget options form HTML.
  162.    *
  163.    * @param array $instance The current widget settings.
  164.    * @return string The HTML markup for the form.
  165.    */
  166.   function form($instance)
  167.   {
  168.     $title  = isset($instance['title'])  ? $instance['title'] : '';
  169.     $number = isset($instance['number']) ? absint($instance['number']) : 5;
  170.     $filter = isset($instance['filter']) ? $instance['filter'] : false;
  171.  
  172.     ?>
  173.     <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  174.       <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); ?>" /></p>
  175.  
  176.     <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of comments to show:'); ?></label>
  177.       <input class="tiny-text" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="number" step="1" min="1" value="<?php echo $number; ?>" size="3" /></p>
  178.  
  179.     <p><input <?php checked($filter, true)?> class="checkbox" type="checkbox" id="<?php echo $this->get_field_name('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" value="1">
  180.       <label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Filter'); ?></label></p>
  181.  
  182.     <?php
  183.   }
  184.  
  185.   public function flush_widget_cache()
  186.   {
  187.     _deprecated_function(__METHOD__, '4.4.0');
  188.   }
  189. } // end of Class
  190.  
  191. /**
  192.  * Register the new widget.
  193.  *
  194.  * @see 'widgets_init'
  195.  */
  196. add_action('widgets_init', 'Widget_Recent_Comments');
  197. function Widget_Recent_Comments()
  198. {
  199.   register_widget('Widget_Recent_Comments');
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement