Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.70 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: bbPress New Topics
  4. Plugin URI: http://bavotasan.com/2014/bbpress-new-topics-plugin/
  5. Description: Displays a "new" label on topics that are unread or have unread replies for all keymasters and moderators.
  6. Author: c.bavota
  7. Version: 1.0.0
  8. Author URI: http://bavotasan.com
  9. Text Domain: new-topics
  10. Domain Path: /languages
  11. License: GPL2
  12. */
  13.  
  14. /*  Copyright 2014  c.bavota  (email : cbavota@gmail.com)
  15.  
  16.     This program is free software; you can redistribute it and/or modify
  17.     it under the terms of the GNU General Public License, version 2, as
  18.     published by the Free Software Foundation.
  19.  
  20.     This program is distributed in the hope that it will be useful,
  21.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.     GNU General Public License for more details.
  24.  
  25.     You should have received a copy of the GNU General Public License
  26.     along with this program; if not, write to the Free Software
  27.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  28. */
  29.  
  30. // Plugin version
  31. if ( ! defined( 'NEW_TOPICS_VERSION' ) ) {
  32.     define( 'NEW_TOPICS_VERSION', '1.0.0' );
  33. }
  34.  
  35. //update_user_meta( 1, 'bbp_new_topics', array( 2895 ) );
  36. if ( ! class_exists( 'BBP_New_Topics' ) ) {
  37.     class BBP_New_Topics {
  38.         /**
  39.          * Construct the plugin object
  40.          */
  41.         public function __construct() {
  42.             add_action( 'admin_init', array( $this, 'admin_init' ) );
  43.  
  44.             add_action( 'bbp_enqueue_scripts', array( $this, 'bbp_enqueue_scripts' ) );
  45.  
  46.             add_action( 'bbp_new_topic', array( $this, 'bbp_new_topic' ), 10, 4 );
  47.             add_action( 'bbp_new_reply', array( $this, 'bbp_new_reply' ), 10, 5 );
  48.  
  49.             add_filter( 'bbp_get_topic_class', array( $this, 'bbp_get_topic_class' ), 10, 2 );
  50.  
  51.             add_action( 'bbp_theme_before_topic_title', array( $this, 'bbp_theme_before_topic_title' ) );
  52.            
  53.             add_action( 'bbp_theme_before_forum_title', array( $this, 'bbp_theme_before_forum_title' ) );
  54.            
  55.             //add_action( 'bbp_theme_before_topic_author', array( $this, 'bbp_theme_before_topic_author' ) );
  56.            
  57.             //bbp_template_before_single_topic
  58.             add_action( 'bbp_template_before_single_topic', array( $this, 'bbp_template_before_single_topic' ) );
  59.         }
  60.  
  61.         public function admin_init() {
  62.             load_plugin_textdomain( 'new-topics', null, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
  63.         }
  64.  
  65.         /**
  66.          * Add stylesheet on bbPress pages
  67.          *
  68.          * This functions is attached to the 'bbp_enqueue_scripts' action hook
  69.          *
  70.          * @since 1.0.0
  71.          */
  72.         public function bbp_enqueue_scripts( $hook ) {
  73.             wp_enqueue_style( 'bbp_new_topics', plugins_url( 'css/new-topics.css', __FILE__ ), '', NEW_TOPICS_VERSION );
  74.         }
  75.  
  76.         /**
  77.          * Loops through all keymasters and moderators and creates an array
  78.          * of topic IDs for topics that are unread or have unread replies.
  79.          *
  80.          * @since 1.0.0
  81.          */
  82.         public function bbp_register_newest_topic( $topic_id, $author ) {
  83.             $bbp_admins = $this->bbp_get_admins();
  84.  
  85.             foreach ( $bbp_admins as $admin ) {
  86.                 if ( $author != $admin->ID ) {
  87.                     $new_topics_array = (array) get_user_meta( $admin->ID, 'bbp_new_topics', true );
  88.  
  89.                     if ( ! in_array( $topic_id, $new_topics_array ) ) {
  90.                         $new_topics_array[] = (int) $topic_id;
  91.  
  92.                         update_user_meta( $admin->ID, 'bbp_new_topics', $new_topics_array );
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.  
  98.         public function bbp_new_topic( $topic_id, $forum_id, $anonymous_data, $topic_author ) {
  99.             $this->bbp_register_newest_topic( $topic_id, $topic_author );
  100.         }
  101.  
  102.         public function bbp_new_reply( $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author ) {
  103.             $this->bbp_register_newest_topic( $topic_id, $reply_author );
  104.         }
  105.  
  106.         /**
  107.          * Adds the class 'new-topic' to all topics that appear in the unread topics array.
  108.          *
  109.          * @since 1.0.0
  110.          */
  111.         public function bbp_get_topic_class( $classes, $topic_id ) {
  112.             $user_id = get_current_user_id();
  113.             $new_topics_array = (array) get_user_meta( $user_id, 'bbp_new_topics', true );
  114.  
  115.             if ( in_array( $topic_id, $new_topics_array ) )
  116.                 $classes[] = 'new-topic';
  117.  
  118.             return $classes;
  119.         }
  120.  
  121.         /**
  122.          * Adds 'New' label to all topics that appear in the unread topics array.
  123.          *
  124.          * @since 1.0.0
  125.          */
  126.         public function bbp_theme_before_topic_title() {
  127.             $user_id = get_current_user_id();
  128.             $topic_id = bbp_get_topic_id();
  129.             $new_topics_array = (array) get_user_meta( $user_id, 'bbp_new_topics', true );
  130.  
  131.             if ( in_array( (int)$topic_id, $new_topics_array ) ) {
  132.                 if ( bbp_is_single_topic ()) {
  133.                     //echo '************************************************************************************************************************';
  134.                     unset( $new_topics_array[array_search( (int) $topic_id, $new_topics_array )] );
  135.  
  136.                     update_user_meta( $user_id, 'bbp_new_topics', (array) $new_topics_array );
  137.                 } else {
  138.                     echo '<span class="new-topic-notifier"><i class="fa fa-pencil-square-o"></i> ' . __( 'New', 'new-topics' ) . '</span> ';
  139.                 }
  140.             }
  141.         }
  142.  
  143.         public function bbp_theme_before_forum_title(){
  144.             $user_id = get_current_user_id();
  145.             $forum_id = bbp_get_forum_id();
  146.             $new_topics_array = (array) get_user_meta( $user_id, 'bbp_new_topics', true );
  147.  
  148.             global $wpdb;
  149.             $query = 'SELECT post_id FROM pct_postmeta WHERE meta_key = "_bbp_forum_id" AND meta_value = "' . $forum_id. '"';
  150.  
  151.             $result = $wpdb->get_results($query);
  152.            
  153.             foreach($result as $row){
  154.                 $post_id = $row->post_id;
  155.                 if(in_array((int)$post_id, $new_topics_array)){
  156.                         echo '<span class="new-topic-notifier"><i class="fa fa-pencil-square-o"></i> ' . __( 'New', 'new-topics' ) . '</span> ';
  157.                         break;
  158.                     }
  159.                 }
  160.            
  161.         }
  162.        
  163.         public function bbp_theme_before_forum_sub_forums(){
  164.             $user_id = get_current_user_id();
  165.             $forum_id = bbp_get_forum_id();
  166.             $new_topics_array = (array) get_user_meta( $user_id, 'bbp_new_topics', true );
  167.  
  168.             global $wpdb;
  169.             $query = 'SELECT post_id FROM pct_postmeta WHERE meta_key = "_bbp_forum_id" AND meta_value = "' . $forum_id. '"';
  170.  
  171.             $result = $wpdb->get_results($query);
  172.            
  173.             foreach($result as $row){
  174.                 $post_id = $row->post_id;
  175.                 if(in_array((int)$post_id, $new_topics_array)){
  176.                         echo '<span class="new-topic-notifier"><i class="fa fa-pencil-square-o"></i> ' . __( 'New', 'new-topics' ) . '</span> ';
  177.                         break;
  178.                     }
  179.                 }
  180.            
  181.         }
  182.        
  183.         //bbp_template_before_single_topic
  184.         public function bbp_template_before_single_topic() {
  185.             $user_id = get_current_user_id();
  186.             $topic_id = bbp_get_topic_id();
  187.             $new_topics_array = (array) get_user_meta( $user_id, 'bbp_new_topics', true );
  188.  
  189.             if ( in_array( (int)$topic_id, $new_topics_array ) ) {
  190.                 if ( bbp_is_single_topic ()) {
  191.                     //echo '************************************************************************************************************************';
  192.                     unset( $new_topics_array[array_search( (int) $topic_id, $new_topics_array )] );
  193.  
  194.                     update_user_meta( $user_id, 'bbp_new_topics', (array) $new_topics_array );
  195.                 } else {
  196.                     echo '<span class="new-topic-notifier"><i class="fa fa-pencil-square-o"></i> ' . __( 'New', 'new-topics' ) . '</span> ';
  197.                 }
  198.             }
  199.         }  
  200.        
  201.         /**
  202.          * Gathers all keymasters and moderators.
  203.          *
  204.          * @since 1.0.0
  205.          */
  206.         public function bbp_get_admins() {
  207.             $keymaster = get_users( array(
  208.                 'role' => 'bbp_keymaster',
  209.             ) );
  210.  
  211.             $moderators = get_users( array(
  212.                 'role' => 'bbp_moderator',
  213.             ) );
  214.  
  215.             $participant = get_users( array(
  216.                 'role' => 'bbp_participant',
  217.             ) );
  218.  
  219.             return array_merge( $keymaster, $moderators, $participant );
  220.         }
  221.     }
  222. }
  223. $bbp_new_topics = new BBP_New_Topics();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement