Advertisement
Guest User

Untitled

a guest
Apr 28th, 2014
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4.  
  5.  * Plugin Name: Auto Group Join
  6.  
  7.  * Description: Members automatically are joined to groups based on a profile field, updated by GrantG182 for compatibility for WP 3.8.3 and BP 1.9.2.
  8.  
  9.  * Author: Brent Layman
  10.  
  11.  * Author URI: http://buglenotes.com
  12.  
  13.  * Plugin URI: http://buglenotes.com
  14.  
  15.  * Version: 1.1
  16.  
  17.  */
  18.  
  19.  
  20.  
  21. /*
  22.  
  23. Place this folder in your plugins folder.  In the admin panel, configure under "Site Admin -> Profile->Group Links"
  24.  
  25.  
  26.  
  27. If you know the profile field name and the group name are not exact matches, use the group_pre_regex and group_post_regex fields.
  28.  
  29.     a. For example, say you have a profile field for "Graduation Year" where an option is "1987".  The corresponding group is "USMA 1987".  So, in the
  30.  
  31.         group_pre_regex field put "USMA%".  (yes ... put the % into the db field).
  32.  
  33.     b. Another example, say you have a profile field for "Type of Fishing" where an option is "Fly".  The corresponding group is "Fly Fishing".  So, in the
  34.  
  35.         group_post_regex field put "%Fishing".
  36.  
  37.     c. If the profile options exactly match the group names, leave group_pre_regex and group_post_regex blank.
  38.  
  39. */
  40.  
  41. add_action('wp_head', 'addHeaderCode');    
  42.  
  43. function addHeaderCode() {
  44.  
  45.    echo '<link type="text/css" rel="stylesheet" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/auto-join-group/css/bn-auto-join-group.css" />' . "\n";
  46.  
  47.    }
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57. function auto_join_group_check_installed() {   
  58.  
  59.     global $wpdb, $bp;
  60.  
  61.    
  62.  
  63.     if ( is_site_admin() ) {
  64.  
  65.         /* Need to check db tables exist, activate hook no-worky in mu-plugins folder. */
  66.  
  67.         if ( !$wpdb->get_var("SHOW COLUMNS FROM {$bp->profile->table_name_fields} LIKE 'group_link'")  ) {
  68.  
  69.             $mysql = "ALTER TABLE {$bp->profile->table_name_fields} ADD `group_link` BINARY NOT NULL DEFAULT '0'";
  70.  
  71.             $wpdb->query($mysql);
  72.  
  73.         }
  74.  
  75.         if ( !$wpdb->get_var("SHOW COLUMNS FROM {$bp->profile->table_name_fields} LIKE 'group_pre_regex'")  ) {
  76.  
  77.             $mysql = "ALTER TABLE {$bp->profile->table_name_fields} ADD `group_pre_regex` varchar(100) NOT NULL";
  78.  
  79.             $wpdb->query($mysql);
  80.  
  81.         }
  82.  
  83.         if ( !$wpdb->get_var("SHOW COLUMNS FROM {$bp->profile->table_name_fields} LIKE 'group_post_regex'")  ) {
  84.  
  85.             $mysql = "ALTER TABLE {$bp->profile->table_name_fields} ADD `group_post_regex` varchar(100) NOT NULL";
  86.  
  87.             $wpdb->query($mysql);
  88.  
  89.         }
  90.  
  91.     }
  92.  
  93. }
  94.  
  95. add_action('admin_head', 'addHeaderCode');
  96.  
  97. add_action( 'admin_menu', 'auto_join_group_check_installed' );
  98.  
  99.  
  100.  
  101. // Now that we have the fields we need, let's do the admin menu
  102.  
  103. function auto_join_group_plugin_menu() {
  104.  
  105.   add_options_page( 'Auto Join Groups', 'Auto Join Groups', 'manage_options', __FILE__, 'auto_join_group_plugin_options' );
  106.  
  107.  // add_submenu_page( 'manage_options', __( 'Auto Join Groups', 'auto_join_group_plugin_options' ), __( 'Auto Join Groups', 'auto_join_group_plugin_options' ), 'manage_options', 'auto-join-groups' );
  108.  
  109. }
  110.  
  111.  
  112.  
  113. // Here's the actual admin menu
  114.  
  115. function auto_join_group_plugin_options() {
  116.  
  117.     global $wpdb, $bp;
  118.  
  119.    
  120.  
  121.     if ( isset($_GET['pre_regex']) && isset($_GET['post_regex']) && isset($_GET['mode']) && isset($_GET['field_id']) && 'save' == $_GET['mode'] ) {
  122.  
  123.         // save the changes to the database!
  124.  
  125.         if (isset($_GET['link'])) { $group_link = 1; } else { $group_link = 0; }
  126.  
  127.        
  128.  
  129.         $sql = $wpdb->prepare("UPDATE {$bp->profile->table_name_fields} SET group_link = %d, group_pre_regex = %s, group_post_regex = %s WHERE id = %d", $group_link, $_GET['pre_regex'], $_GET['post_regex'], $_GET['field_id']);
  130.  
  131.         if ( !$wpdb->query($sql) ) {
  132.  
  133.             echo "<strong>Update Failed (or no fields were changed)</strong><hr />";
  134.  
  135.         } else {
  136.  
  137.             echo "<strong>Update Successful!</strong><hr />";
  138.  
  139.             // Now ... let's link existing users to the group!!!!
  140.  
  141.                 // get list of users
  142.  
  143.                 if (isset($_GET['link'])) {
  144.  
  145.                     $users = $wpdb->get_results("SELECT user_id FROM {$bp->profile->table_name_data} WHERE field_id = " . $_GET['field_id']);
  146.  
  147.                     foreach ($users as $user) {
  148.  
  149.                         bn_auto_group_join($user->user_id, 'myid');
  150.  
  151.                     }
  152.  
  153.                 }
  154.  
  155.            
  156.  
  157.         }
  158.  
  159.     }
  160.  
  161.    
  162.  
  163.     if ( isset($_GET['mode']) && isset($_GET['field_id']) && 'edit' == $_GET['mode'] ) {
  164.  
  165.         // edit one of the profile linking options
  166.  
  167.         $profiles = $wpdb->get_results("SELECT name, id, group_link, group_pre_regex, group_post_regex FROM {$bp->profile->table_name_fields} WHERE id = " . $_GET['field_id']);
  168.  
  169.         ?>
  170.        
  171.         <?php
  172.        
  173.         $action_url = $_SERVER['REQUEST_URI'];
  174.  
  175.         echo '<form method="POST" name="auto-join" action="'.$action_url.'">'
  176.        
  177.         ?>
  178.            
  179.  
  180.         <input type="hidden" name="field_id" value="<?php echo $_GET['field_id']; ?>">
  181.  
  182.         <input type="hidden" name="mode" value="save">
  183.  
  184.         <input type="hidden" name="page" value="auto-join-groups/bn-auto-join-group.php">
  185.  
  186.         <table class="form-table"><?php
  187.  
  188.         foreach ($profiles as $profile)  { ?>
  189.  
  190.             <tr><td><strong>Profile Field Name:</strong></td><td align="left"><strong><?php echo $profile->name; ?></strong></td></tr>
  191.  
  192.             <tr><td>Link to Groups:</td><td align="left"><input type="checkbox" name="link" <?php if ($profile->group_link == 0) { echo "No"; } else { echo "checked"; } ?>></td></tr>
  193.  
  194.             <tr><td>Pre Matching:</td><td align="left"><input type="text" name="pre_regex" maxlength="100" size="15" value="<?php echo $profile->group_pre_regex; ?>"></td></tr>
  195.  
  196.             <tr><td>Post Matching:</td><td align="left"><input type="text" name="post_regex" maxlength="100" size="15" value="<?php echo $profile->group_post_regex; ?>"></td></tr>
  197.  
  198.             <tr><td colspan="2" align="left"><input type="submit" value="Save"></td></tr>
  199.  
  200.             <tr><td colspan="2" align="left"><strong>DIRECTIONS</strong><br />
  201.  
  202.             <strong>1.</strong> Check the "Link to Groups" box if you want to try to automatically join members to groups based on this profile field.<br /><br />
  203.  
  204.             <strong>2.</strong> If you know the profile field name and the group name are not exact matches, use the group_pre_regex and group_post_regex fields.<br />
  205.  
  206.                 &nbsp;&nbsp;<strong>a.</strong> For example, say you have a profile field for "Graduation Year" where an option is "1987".  The corresponding group is "USMA 1987".  So, in the
  207.  
  208.                     "Pre Matching" field put "USMA%".  (yes ... put the % into the db field).<br />
  209.  
  210.                 &nbsp;&nbsp;<strong>b.</strong> Another example, say you have a profile field for "Type of Fishing" where an option is "Fly".  The corresponding group is "Fly Fishing".  So, in the
  211.  
  212.                     "Post Matching" field put "%Fishing".<br />
  213.  
  214.                 &nbsp;&nbsp;<strong>c.</strong> If the profile options exactly match the group names, leave "Pre Matching" and "Post Matching" blank.<br /><br />
  215.  
  216.             <strong>3.</strong> Once you save your changes, if the "Link to Groups" box is checked, all existing wpmu members will be added to the matching groups!
  217.  
  218.             </td></tr>
  219.  
  220.          <?php
  221.  
  222.         }
  223.  
  224.         ?></table><?php
  225.  
  226.     } else {
  227.  
  228.         // get profile fields with group linking
  229.  
  230.         $profiles = $wpdb->get_results("SELECT name, id, group_link, group_pre_regex, group_post_regex FROM {$bp->profile->table_name_fields} WHERE parent_id = 0 AND (type = 'selectbox' OR type = 'textbox')");
  231.  
  232.         echo "<table class=\"widefat\"><thead><tr><td colspan='4''><strong>Click edit</strong> on the Profile field you'd like to link to a group.<br /><br />
  233.  
  234.             <strong>NOTE</strong> - currently only 'selectbox' and 'textbox' type profile fields are supported!</td></tr>
  235.  
  236.             <tr><td colspan='5'>&nbsp;</td></tr>
  237.  
  238.             <tr><th>Profile Field Name</th><th>Linked?</th><th>Pre Matching</th><th>Post Matching</th><th>&nbsp;</th></tr>";
  239.  
  240.         foreach ($profiles as $profile)  { ?>
  241.  
  242.             <tr>
  243.  
  244.                 <td><?php echo $profile->name; ?></td>
  245.  
  246.                 <td style="text-align:center;"><?php echo ($profile->group_link == 1) ? '<strong>YES</strong>' : 'No'; ?></td>
  247.  
  248.                 <td><?php echo $profile->group_pre_regex; ?></td>
  249.  
  250.                 <td><?php echo $profile->group_post_regex; ?></td>
  251.  
  252.                 <td><a href="options-general.php?page=auto-join-groups/bn-auto-join-group.php&amp;field_id=<?php echo $profile->id; ?>&amp;mode=edit"><?php _e( 'Edit', 'buddypress' ); ?></a></td>
  253.  
  254.             </tr>
  255.  
  256.              <?php
  257.  
  258.         }
  259.  
  260.         echo "</table>";
  261.  
  262.     }
  263.  
  264. }
  265.  
  266.  
  267.  
  268. add_action('admin_menu', 'auto_join_group_plugin_menu');
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276. // automatically join users to groups based on profile fields when the user modifies his profile or activates his account
  277.  
  278. function bn_auto_group_join($userid, $x = 0, $y = 0){
  279.  
  280.     global $wpdb, $bp, $user_id;
  281.  
  282.     // get the user id
  283.  
  284.     if ($bp->loggedin_user->id && $x !== 'myid') {
  285.  
  286.         $userid =  $bp->loggedin_user->id ; // current user if logged in.  On activation, userid sent with do_action
  287.  
  288.     }
  289.  
  290.    
  291.  
  292.     // get profile fields with group linking
  293.  
  294.     $profiles = $wpdb->get_results("SELECT * FROM {$bp->profile->table_name_fields} WHERE group_link = 1");
  295.  
  296.    
  297.  
  298.     foreach ($profiles as $profile)  {
  299.  
  300.                
  301.  
  302.         // see what the person has for that field
  303.  
  304.         $profileinfo = $wpdb->get_results("SELECT value FROM {$bp->profile->table_name_data} WHERE user_id = $userid AND field_id = $profile->id");
  305.  
  306.         foreach ($profileinfo as $profilevalue ) {
  307.  
  308.            
  309.  
  310.             // see if we can match the group
  311.  
  312.             $groupmatches = $wpdb->get_results("SELECT * FROM {$bp->groups->table_name} WHERE name like '$profile->group_pre_regex$profilevalue->value$profile->group_post_regex'");
  313.  
  314.             foreach ($groupmatches as $groupmatch) {
  315.  
  316.                
  317.  
  318.                 // see if the user is already in the group
  319.  
  320.                 if ( !BP_Groups_Member::check_is_member( $userid, $groupmatch->id ) ) {
  321.  
  322.                     // make sure the user isn't banned from the group!
  323.  
  324.                     if ( !groups_is_user_banned( $userid, $group->id ) ) {
  325.  
  326.                         // add the group already!
  327.  
  328.                             $group_id = $groupmatch->id;
  329.  
  330.                             $user_id = $userid;
  331.  
  332.                               if ( groups_check_user_has_invite( $user_id, $group_id ) ) {
  333.  
  334.                                 groups_delete_invite( $user_id, $group_id );
  335.  
  336.                                 }
  337.  
  338.                              
  339.  
  340.                              $new_member = new BP_Groups_Member;
  341.  
  342.                              $new_member->group_id = $group_id;
  343.  
  344.                              $new_member->inviter_id = 0;
  345.  
  346.                              $new_member->user_id = $user_id;
  347.  
  348.                              $new_member->is_admin = 0;
  349.  
  350.                              $new_member->user_title = '';
  351.  
  352.                              $new_member->date_modified = time();
  353.  
  354.                              $new_member->is_confirmed = 1;
  355.  
  356.                              
  357.  
  358.                              if ( !$new_member->save() ) {
  359.  
  360.                                 return false;
  361.  
  362.                                 }
  363.  
  364.                                
  365.  
  366.                             // Should I add this to the activity stream?  left off for now
  367.  
  368.                              
  369.  
  370.                             /* Modify group meta */
  371.  
  372.                             groups_update_groupmeta( $group_id, 'total_member_count', (int) groups_get_groupmeta( $group_id, 'total_member_count') + 1 );
  373.  
  374.                             groups_update_groupmeta( $group_id, 'last_activity', time() );
  375.  
  376.  
  377.  
  378.                     }
  379.  
  380.  
  381.  
  382.                 }
  383.  
  384.             }
  385.  
  386.         }
  387.  
  388.        
  389.  
  390.     }
  391.  
  392.    
  393.  
  394.  
  395.  
  396. }
  397.  
  398. // below hook is located in bp-xprofile.php.  Add user to group when profile is edited
  399.  
  400. add_action( 'xprofile_updated_profile', 'bn_auto_group_join', 1, 1 );
  401.  
  402. // below hook is located in wpmu-functions.php.  Add user to group when account is activated
  403.  
  404. add_action( 'wpmu_activate_user', 'bn_auto_group_join', 12, 3 );
  405.  
  406.  
  407.  
  408.  
  409.  
  410. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement