Advertisement
Guest User

Update - ThemeSphere

a guest
Sep 18th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. bbps - support functions
  4. Contains all the functions that generate and update the topic status.
  5. */
  6.  
  7. /* @TODO rename / rework this function so it makes sense - what a noob */
  8. function bbps_get_update_capabilities(){
  9.     global $current_user;
  10.     $current_user = wp_get_current_user();
  11.     $user_id = $current_user->ID;
  12.  
  13.     $topic_author_id = bbp_get_topic_author_id();
  14.     $permissions = get_option('_bbps_status_permissions');
  15.     $can_edit = "";
  16.     //check the users permission this is easy
  17.     if( $permissions['admin'] == 1 && current_user_can('administrator') || $permissions['mod'] == 1 && current_user_can('bbp_moderator') ){
  18.         $can_edit = true;
  19.     }
  20.     //now check the current user against the topic creator are they they same person and can they cahnge the status?
  21.     if ( $user_id == $topic_author_id && $permissions['user'] == 1 ){
  22.         $can_edit = true;}
  23.     return $can_edit;
  24.    
  25. }
  26. // add_action( 'bbp_template_before_single_topic' , 'bbps_get_update_capabilities' );  
  27.  
  28. /* @TODO ASAP */
  29. /* split this function up as its getting way to big now with all these extra features */
  30.  
  31.  add_action('bbp_template_before_single_topic', 'bbps_add_support_forum_features');
  32. function bbps_add_support_forum_features(){
  33.    
  34.  
  35.  
  36.     //only display all this stuff if the support forum option has been selected.
  37.     // if (bbps_is_support_forum(bbp_get_forum_id())){
  38.         $can_edit = bbps_get_update_capabilities();
  39.         $topic_id = bbp_get_topic_id();
  40.         $status = bbps_get_topic_status($topic_id);
  41.         $forum_id = bbp_get_forum_id();
  42.         $user_id = get_current_user_id();
  43.        
  44.        
  45.         ?> <div id="bbps_support_forum_options"> <?php
  46.         //get out the option to tell us who is allowed to view and update the drop down list.
  47.         if ( $can_edit == true ){ ?>
  48.             <?php bbps_generate_status_options($topic_id,$status);
  49.         }else{
  50.         ?>
  51.             This topic is: <?php echo $status ;
  52.         }
  53.         ?> </div> <?php
  54.         //has the user enabled the move topic feature?
  55.         if( (get_option('_bbps_enable_topic_move') == 1) && (current_user_can('administrator') || current_user_can('bbp_moderator')) ) {
  56.         ?>
  57.         <div id ="bbps_support_forum_move">
  58.             <form id="bbps-topic-move" name="bbps_support_topic_move" action="" method="post">
  59.                 <label for="bbp_forum_id">Move topic to: </label><?php bbp_dropdown(); ?>
  60.                 <input type="submit" value="Move" name="bbps_topic_move_submit" />
  61.                 <input type="hidden" value="bbps_move_topic" name="bbps_action"/>
  62.                 <input type="hidden" value="<?php echo $topic_id ?>" name="bbps_topic_id" />
  63.                 <input type="hidden" value="<?php echo $forum_id ?>" name="bbp_old_forum_id" />
  64.             </form>
  65.         </div>  <?php
  66.            
  67.         }
  68.     // }
  69. }
  70.  
  71. function bbps_get_topic_status($topic_id){
  72.     $default = get_option('_bbps_default_status');
  73.     $status = get_post_meta( $topic_id, '_bbps_topic_status', true );  
  74.     //to do not hard code these if we let the users add their own satus
  75.     if ($status)
  76.         $switch = $status;
  77.     else
  78.         $switch = $default;
  79.        
  80.     switch($switch){
  81.         case 1:
  82.             return "not resolved";
  83.             break;
  84.         case 2:
  85.             return "resolved";
  86.             break;
  87.         case 3:
  88.             return "not a support question";
  89.             break;
  90.     }
  91. }
  92.  
  93. //generates a drop down list with the support forum topic status only for admin and moderators tho.
  94. function bbps_generate_status_options($topic_id){
  95.    
  96.     $dropdown_options = get_option( '_bbps_used_status' );
  97.     $status = get_post_meta( $topic_id, '_bbps_topic_status', true );
  98.     $default = get_option('_bbps_default_status');
  99.  
  100.     //only use the default value as selected if the topic doesnt ahve a status set
  101.     if ($status)
  102.         $value = $status;
  103.     else
  104.         $value = $default;
  105.     ?>
  106.     <form id="bbps-topic-status" name="bbps_support" action="" method="post">
  107.         <label for="bbps_support_options">This topic is: </label>
  108.         <select name="bbps_support_option" id="bbps_support_options">
  109.             <?php
  110.             //we only want to display the options the user has selected. the long term goal is to let users add their own forum statuses
  111.             if ( $dropdown_options['res'] == 1 ){ ?> <option value="1" <?php selected( $value,1 ) ; ?> >not resolved</option> <?php }  
  112.             if ( $dropdown_options['notres'] == 1 ) {?> <option value="2" <?php selected( $value,2 ) ; ?> >resolved</option> <?php }
  113.             if ( $dropdown_options['notsup'] == 1 ) {?> <option value="3" <?php selected( $value,3 ) ; ?> >not a support question</option> <?php } ?>
  114.         </select>
  115.         <input type="submit" value="Update" name="bbps_support_submit" />
  116.         <input type="hidden" value="bbps_update_status" name="bbps_action"/>
  117.         <input type="hidden" value="<?php echo $topic_id ?>" name="bbps_topic_id" />
  118.     </form>
  119.     <?php
  120. }
  121.  
  122. function bbps_update_status(){
  123.     $topic_id = $_POST['bbps_topic_id'];
  124.     $status = $_POST['bbps_support_option'];
  125.     //check if the topic already has resolved meta - if it does then delete it before readding
  126.     //we do this so that any topic updates will have a new meta id for sorting recently resolved etc
  127.     $has_status = get_post_meta($topic_id, '_bbps_topic_status',true);
  128.     $is_urgent = get_post_meta($topic_id, '_bbps_urgent_topic',true);
  129.     $is_claimed = get_post_meta($topic_id, '_bbps_topic_claimed',true);
  130.    
  131.     if($has_status)
  132.         delete_post_meta($topic_id, '_bbps_topic_status');
  133.    
  134.     //if the status is going to resolved we need to check for claimed and urgent meta and delete this to
  135.     // 2 == resolved status :)
  136.     if ($status == 2){
  137.         if($is_urgent)
  138.             delete_post_meta($topic_id, '_bbps_urgent_topic');
  139.         if($is_claimed)
  140.             delete_post_meta($topic_id, '_bbps_topic_claimed');
  141.        
  142.     }
  143.    
  144.     update_post_meta( $topic_id, '_bbps_topic_status', $status );
  145. }
  146.  
  147. function bbps_move_topic(){
  148.     global $wpdb;
  149.     $topic_id = $_POST['bbps_topic_id'];
  150.     $new_forum_id = $_POST['bbp_forum_id'];
  151.     $old_forum_id = $_POST['bbp_old_forum_id'];
  152.        
  153.     //move the topics we will need to run a recount to after this is done
  154.     if ($topic_id != '' && $new_forum_id !=''){
  155.         $wpdb->update( 'wp_posts', array('post_parent' => $new_forum_id), array('ID' => $topic_id) );
  156.         update_post_meta( $topic_id, '_bbp_forum_id', $new_forum_id ); 
  157.         // update all the forum meta and counts for the old forum and the new forum
  158.         bbp_update_forum( array('forum_id' => $new_forum_id) );
  159.         bbp_update_forum( array('forum_id' => $old_forum_id) );
  160.     }
  161. }
  162.  
  163.  
  164.  
  165. //Urgent topic code starts
  166. /*
  167. function bbps_urgent_topic_link
  168. Checks the status of the option and generates and displays
  169. a link based on if the topic is already marked as urgent
  170. */
  171. function bbps_urgent_topic_link(){
  172.     //bail if option not set or user permission not up to scratch or if the forum has not been set as a support forum
  173.     if( (get_option('_bbps_status_permissions_urgent') == 1) && (current_user_can('administrator') || current_user_can('bbp_moderator')) && (bbps_is_support_forum(bbp_get_forum_id())) ) {
  174.     $topic_id = bbp_get_topic_id();
  175.         //1 = urgent topic 0 or nothing is topic not urgent so we give the admin / mods the chance to make it urgent
  176.         if ( get_post_meta($topic_id, '_bbps_urgent_topic', true) != 1 ){
  177.             $urgent_uri = add_query_arg( array( 'action' => 'bbps_make_topic_urgent', 'topic_id' => $topic_id ) );
  178.             echo '<span class="bbp-admin-links bbps-links"><a href="' . $urgent_uri . '">Urgent</a> | </span>';
  179.         }
  180.  
  181.     }
  182.     return;
  183. }
  184.  
  185. add_action('bbp_theme_after_reply_admin_links', 'bbps_urgent_topic_link');
  186.  
  187. //check if the url generated above has been clicked and generated
  188. if ( (isset($_GET['action']) && isset($_GET['topic_id']) && $_GET['action'] == 'bbps_make_topic_urgent')  )
  189.     bbps_urgent_topic();
  190.  
  191. if ( (isset($_GET['action']) && isset($_GET['topic_id']) && $_GET['action'] == 'bbps_make_topic_not_urgent')  )
  192.     bbps_not_urgent_topic();
  193.        
  194.        
  195. function bbps_urgent_topic(){
  196.     $topic_id = $_GET['topic_id'];
  197.     update_post_meta($topic_id, '_bbps_urgent_topic', 1);
  198. }
  199.  
  200. function bbps_not_urgent_topic(){
  201.     $topic_id = $_GET['topic_id'];
  202.     delete_post_meta($topic_id, '_bbps_urgent_topic');
  203. }
  204.  
  205. //display a message to all admin on the single topic view so they know a topic is urgent also give them a link to check it as not urgent
  206. function display_urgent_message(){
  207.     //only display to the correct people
  208.     if( (get_option('_bbps_status_permissions_urgent') == 1) && (current_user_can('administrator') || current_user_can('bbp_moderator') ) &&  (bbps_is_support_forum( bbp_get_forum_id() )) ) {
  209.         $topic_id = bbp_get_topic_id();
  210.         //topic is urgent so make a link
  211.         if(get_post_meta($topic_id, '_bbps_urgent_topic', true) == 1){
  212.             $urgent_uri = add_query_arg( array( 'action' => 'bbps_make_topic_not_urgent', 'topic_id' => $topic_id ) );
  213.             echo "<div class='bbps-support-forums-message'> This topic is currently marked as urgent change the status to " . '<a href="' . $urgent_uri . '">Not Urgent?</a></div>';
  214.         }
  215.     }
  216.  
  217. }
  218. add_action( 'bbp_template_before_single_topic' , 'display_urgent_message' );
  219.  
  220.  
  221. //Topic Claim code starts here
  222.  
  223. function bbps_claim_topic_link(){
  224.     //bail if option not set or user permission not up to scratch or if the forum has not been set as a support forum
  225.     if( (get_option('_bbps_claim_topic') == 1) && (current_user_can('administrator') || current_user_can('bbp_moderator')) && (bbps_is_support_forum(bbp_get_forum_id())) ) {
  226.     $topic_id = bbp_get_topic_id();
  227.     global $current_user;
  228.     get_currentuserinfo();
  229.     $user_id = $current_user->ID;
  230.         //anything greater than one will be claimed as it saves the claimed user id and will set this back to 0 if a topic is unclaimed
  231.         if ( get_post_meta($topic_id, '_bbps_topic_claimed', true) < 1 ){
  232.             $urgent_uri = add_query_arg( array( 'action' => 'bbps_claim_topic', 'topic_id' => $topic_id, 'user_id' => $user_id ) );
  233.             echo '<span class="bbp-admin-links bbps-links"><a href="' . $urgent_uri . '">Claim </a> | </span>';
  234.         }
  235.  
  236.     }
  237.     return;
  238. }
  239.  
  240. add_action('bbp_theme_after_reply_admin_links', 'bbps_claim_topic_link');
  241.  
  242. //check for the link to be clicked
  243. if ( (isset($_GET['action']) && isset($_GET['topic_id']) && isset($_GET['user_id']) && $_GET['action'] == 'bbps_claim_topic')  )
  244.     bbps_claim_topic();
  245.    
  246.     if ( (isset($_GET['action']) && isset($_GET['topic_id']) && isset($_GET['user_id']) && $_GET['action'] == 'bbps_unclaim_topic')  )
  247.     bbps_unclaim_topic();
  248.    
  249. function bbps_claim_topic(){
  250.     $user_id = $_GET['user_id'];
  251.     $topic_id = $_GET['topic_id'];
  252.     //subscribe the user to the topic - this is a bbpress function
  253.     bbp_add_user_subscription( $user_id, $topic_id );
  254.     //record who has claimed the topic in postmeta for use within this plugin
  255.     update_post_meta($topic_id, '_bbps_topic_claimed', $user_id);
  256. }
  257.  
  258. function bbps_unclaim_topic(){
  259.     $user_id = $_GET['user_id'];
  260.     $topic_id = $_GET['topic_id'];
  261.     //subscribe the user to the topic - this is a bbpress function
  262.     bbp_remove_user_subscription( $user_id, $topic_id );
  263.     //reupdate the postmeta with an id of 0 this is unclaimed now
  264.     delete_post_meta($topic_id, '_bbps_topic_claimed' );
  265. }
  266.  
  267. function bbps_display_claimed_message(){
  268.     $topic_author_id = bbp_get_topic_author_id();
  269.     global $current_user;
  270.     get_currentuserinfo();
  271.     $user_id = $current_user->ID;
  272.     //we want to display the claimed topic message to the topic owner to
  273.     if( (get_option('_bbps_claim_topic') == 1) && (current_user_can('administrator') || current_user_can('bbp_moderator') || $topic_author_id == $user_id ) && (bbps_is_support_forum(bbp_get_forum_id())) ) {
  274.        
  275.         $topic_id = bbp_get_topic_id();
  276.         $claimed_user_id = get_post_meta($topic_id, '_bbps_topic_claimed', true);
  277.         if($claimed_user_id > 0){
  278.             $user_info = get_userdata ($claimed_user_id);
  279.             $claimed_user_name = $user_info->user_login;
  280.         }
  281.         if($claimed_user_id > 0 && $claimed_user_id != $user_id){
  282.             echo "<div class='bbps-support-forums-message'>This topic is currently claimed by " .$claimed_user_name .", they will be working on it now. </div>";
  283.         }
  284.         //the person who claimed it can unclaim it this will also unsubscribe them when they do
  285.         if ($claimed_user_id == $user_id){
  286.             $urgent_uri = add_query_arg( array( 'action' => 'bbps_unclaim_topic', 'topic_id' => $topic_id, 'user_id' => $user_id ) );
  287.             echo '<div class="bbps-support-forums-message"> You currently own this topic would you like to <a href="' . $urgent_uri . '">Unclame</a> it?</div>';
  288.         }
  289.     }
  290. }
  291.  
  292. add_action( 'bbp_template_before_single_topic' , 'bbps_display_claimed_message' ); 
  293.  
  294. //asign to another user code here:
  295. /*
  296.     $user_id = $_GET['user_id'];
  297.     $topic_id = $_GET['topic_id'];
  298.     //subscribe the user to the topic - this is a bbpress function
  299.     bbp_add_user_subscription( $user_id, $topic_id );
  300.     //record who has claimed the topic in postmeta for use within this plugin
  301.     update_post_meta($topic_id, '_bbps_topic_claimed', $user_id);
  302. */
  303. function bbps_assign_topic_form(){
  304.  
  305.     if( (get_option('_bbps_topic_assign') == 1) && (current_user_can('administrator') || current_user_can('bbp_moderator')) ) {
  306.         $topic_id = bbp_get_topic_id();
  307.         $topic_assigned = get_post_meta($topic_id, 'bbps_topic_assigned', true);
  308.         global $current_user;
  309.         get_currentuserinfo();
  310.         $current_user_id = $current_user->ID;
  311.     ?>  <div id="bbps_support_forum_options"> <?php
  312.            
  313.             $user_login = $current_user->user_login;
  314.             if(!empty($topic_assigned)){
  315.                 if($topic_assigned == $current_user_id){
  316.                     ?> <div class='bbps-support-forums-message'> This topic is assigned to you!</div><?php
  317.                 }
  318.                 else{
  319.                     $user_info = get_userdata ($topic_assigned);
  320.                     $assigned_user_name = $user_info->user_login;
  321.                 ?> <div class='bbps-support-forums-message'> This topic is already assigned to: <?php echo $assigned_user_name; ?></div><?php  
  322.                 }
  323.         }
  324.    
  325.         ?>
  326.         <div id ="bbps_support_topic_assign">
  327.             <form id="bbps-topic-assign" name="bbps_support_topic_assign" action="" method="post">
  328.             <?php   bbps_user_assign_dropdown(); ?>
  329.                 <input type="submit" value="Assign" name="bbps_support_topic_assign" />
  330.                 <input type="hidden" value="bbps_assign_topic" name="bbps_action"/>
  331.                 <input type="hidden" value="<?php echo $topic_id ?>" name="bbps_topic_id" />
  332.             </form>
  333.         </div></div>  <?php
  334.        
  335.        
  336.    
  337.     }
  338.    
  339. }
  340.  
  341. add_action( 'bbp_template_before_single_topic' , 'bbps_assign_topic_form' );   
  342.  
  343. function bbps_user_assign_dropdown(){
  344.  
  345.     //http://codex.wordpress.org/Class_Reference/WP_User_Query
  346.     $args = array();
  347.     $args[0] = 'ID';
  348.     $args[1] = 'user_login';
  349.     $args[2] = 'user_email';
  350.    
  351.     $wp_user_search = new WP_User_Query( array( 'role' => 'administrator', 'fields' => $args ) );
  352.     $admins = $wp_user_search->get_results();
  353.    
  354.     $wp_user_search = new WP_User_Query( array( 'role' => 'bbp_moderator', 'fields' => $args ) );
  355.     $moderators = $wp_user_search->get_results();
  356.    
  357.     $all_users = array_merge($moderators,$admins);
  358.     $topic_id = bbp_get_topic_id();
  359.     $claimed_user_id = get_post_meta($topic_id, 'bbps_topic_assigned', true);
  360.    
  361.     if ( !empty($all_users) ){
  362.         if ( $claimed_user_id > 0 ){
  363.             $text = "Reassign topic to: ";
  364.         }else{
  365.             $text = "Assign topic to: ";
  366.         }
  367.    
  368.         echo $text;
  369.             ?>
  370.         <select name="bbps_assign_list" id="bbps_support_options">
  371.         <option value="">Unassigned</option><?php
  372.         foreach ($all_users as $user){
  373.         ?>
  374.             <option value="<?php echo $user->ID; ?>"> <?php echo $user->user_login; ?></option>
  375.         <?php
  376.         }
  377.         ?> </select> <?php
  378.     }
  379.  
  380. }
  381.  
  382.  
  383. function bbps_assign_topic(){
  384.     $user_id = $_POST['bbps_assign_list'];
  385.     $topic_id = $_POST['bbps_topic_id'];
  386.    
  387.     if ($user_id > 0){
  388.         $userinfo = get_userdata($user_id);
  389.         $user_email = $userinfo->user_email;
  390.         $post_link = get_permalink( $topic_id );
  391.         //add the user as a subscriber to the topic and send them an email to let them know they have been assigned to a topic
  392.         bbp_add_user_subscription( $user_id, $topic_id );
  393.         /*update the post meta with the assigned users id*/
  394.         $assigned = update_post_meta($topic_id, 'bbps_topic_assigned', $user_id);
  395.         $message = <<< EMAILMSG
  396.         You have been assigned to the following topic, by another forum moderator or the site administrator. Please take a look at it when you get a chance.
  397.         $post_link
  398. EMAILMSG;
  399.         if ($assigned == true){
  400.             wp_mail($user_email,'A forum topic has been assigned to you', $message);
  401.         }
  402.     }
  403. }
  404.  
  405. // I believe this Problem is because your Plugin is loading at the wrong time, and can be fixed by wrapping your plugin in a wrapper class.
  406. //need to find a hook or think of the best way to do this
  407.     if (!empty($_POST['bbps_support_topic_assign'])){
  408.         bbps_assign_topic($_POST);
  409.     }
  410.    
  411.     if (!empty($_POST['bbps_support_submit'])){
  412.         bbps_update_status($_POST);
  413.     }
  414.    
  415.     if (!empty($_POST['bbps_topic_move_submit'])){
  416.         bbps_move_topic($_POST);
  417.     }
  418.  
  419. // adds a class and status to the front of the topic title
  420. function bbps_modify_title($title, $topic_id = 0){
  421.     $topic_id = bbp_get_topic_id( $topic_id );
  422.     $title = "";
  423.     $topic_author_id = bbp_get_topic_author_id();
  424.     global $current_user;
  425.     get_currentuserinfo();
  426.     $user_id = $current_user->ID;
  427.    
  428.     $claimed_user_id = get_post_meta($topic_id, '_bbps_topic_claimed', true);
  429.         if($claimed_user_id > 0){
  430.             $user_info = get_userdata ($claimed_user_id);
  431.             $claimed_user_name = $user_info->user_login;
  432.         }
  433.  
  434.     //2 is the resolved status ID
  435.     if (get_post_meta( $topic_id, '_bbps_topic_status', true ) == 2)
  436.         echo '<span class="resolved"> [Resolved] </span>';
  437.     //we only want to display the urgent topic status to admin and moderators
  438.     if (get_post_meta( $topic_id, '_bbps_urgent_topic', true ) == 1 && (current_user_can('administrator') || current_user_can('bbp_moderator')))
  439.         echo '<span class="urgent"> [Urgent] </span>';
  440.     //claimed topics also only get shown to admin and moderators and the person who owns the topic
  441.     if (get_post_meta( $topic_id, '_bbps_topic_claimed', true ) > 0 && (current_user_can('administrator') || current_user_can('bbp_moderator') || $topic_author_id == $user_id ) ){
  442.         //if this option == 1 we display the users name not [claimed]
  443.         if( get_option( '_bbps_claim_topic_display' ) == 1)
  444.             echo '<span class="claimed">['. $claimed_user_name . ']</span>';
  445.         else
  446.             echo '<span class="claimed"> [Claimed] </span>';
  447.     }
  448. }
  449.  
  450.    
  451. add_action('bbp_theme_before_topic_title', 'bbps_modify_title');
  452. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement