Skip to content

Instantly share code, notes, and snippets.

@adamcapriola
Created April 27, 2014 23:37
Show Gist options
  • Save adamcapriola/11358139 to your computer and use it in GitHub Desktop.
Save adamcapriola/11358139 to your computer and use it in GitHub Desktop.
<?php
/**
* Sync Groups from WordPress to Discourse
*
* First creates 'groups' meta_key (which should be customized to your needs)
* Then it monitors for changes in the 'groups' meta_value and connects to Discourse API to sync groups
*
* WordPress References
* https://core.trac.wordpress.org/browser/tags/3.9/src/wp-includes/meta.php?order=name#L235
* http://wordpress.stackexchange.com/questions/16835/how-to-hook-update-post-meta-and-delete-post-meta
* Genesis theme /lib/admin/user-meta.php
*
* Discourse References
* https://github.com/discoursehosting/discourse-api-php/blob/master/lib/DiscourseAPI.php
* https://github.com/discourse/wp-discourse/blob/master/wp-discourse.php#L354
* https://meta.discourse.org/t/add-users-to-and-remove-from-groups-via-api/15093/2?u=adamcapriola
*
*/
//
// Add group fields to user profile
//
add_action( 'show_user_profile', 'ac_user_groups_fields', 9 );
add_action( 'edit_user_profile', 'ac_user_groups_fields', 9 );
function ac_user_groups_fields( $user ) {
// The group name here is "cats"
if ( ! current_user_can( 'edit_users', $user->ID ) )
return false;
$groups = get_user_meta( $user->ID, 'groups', true );
if ( empty( $groups ) ) $groups = array( 'cats' => 0 );
?>
<h3>Groups</h3>
<table class="form-table">
<tbody>
<tr>
<th scope="row" valign="top">Groups</th>
<td>
<input id="groups[cats]" name="groups[cats]" type="checkbox" value="1" <?php checked( $groups['cats'] ); ?> />
<label for="groups[cats]">Cats</label><br />
<input id="groups[registered]" name="groups[registered]" type="hidden" value="1">
</td>
</tr>
</tbody>
</table>
<?php
}
//
// Save custom user profile fields
//
add_action( 'personal_options_update', 'ac_user_groups_save' );
add_action( 'edit_user_profile_update', 'ac_user_groups_save' );
function ac_user_groups_save( $user_id ) {
if ( ! current_user_can( 'edit_users', $user_id ) )
return;
if ( ! isset( $_POST['groups'] ) || ! is_array( $_POST['groups'] ) )
return;
$groups = $_POST['groups'];
update_user_meta( $user_id, 'groups', $groups );
}
//
// Listen for changes to groups
//
add_action( 'added_user_meta', 'ac_discourse_groups_sync', 10, 4 );
add_action( 'updated_user_meta', 'ac_discourse_groups_sync', 10, 4 );
function ac_discourse_groups_sync( $meta_id, $user_id, $meta_key, $meta_value ) {
if ( $meta_key == 'groups' ) {
$userdata = get_userdata( $user_id );
$username = $userdata->user_login;
// If user is a member of the group, try to add them on Discourse, otherwise remove
if ( ! empty( $meta_value['cats'] ) ) $action = 'add';
else $action = 'remove';
//
// Define variables
//
$discourse_url = 'http://discourse.example.com'; // Note: No trailing slash!
$api_key = 'meow'; // API key
$api_username = 'system'; // API username (should be an admin)
$group_name = 'cats'; // Name of group
$group_number = '43'; // Need to run browser console and log network to find this
//
// Get usernames of all group members
//
// URL
$url = sprintf(
'%s/groups/%s/members.json',
$discourse_url,
$group_name
);
// cURL
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$body = curl_exec( $ch );
curl_close( $ch );
// Intrepret results
$json = json_decode( $body, true );
$usernames = array();
foreach ( $json as $key => $value ) $usernames[] = $value['username'];
// Probably need some kind of error check
// if ( empty( $usernames ) ) {}
//
// Add or remove username from array
//
if ( $action == 'add' && !in_array( $username, $usernames ) ) {
$usernames[] = $username;
}
elseif ( $action == 'remove' && in_array( $username, $usernames ) ) {
$key = array_search( $username, $usernames);
unset( $usernames[$key] );
}
else {
return;
}
// Prepare usernames for API call
if ( !empty( $usernames ) ) $usernames = implode ( ',', $usernames );
//
// Manipulate group members
//
// URL
$url = sprintf(
'%s/admin/groups/%s?api_key=%s&api_username=%s',
$discourse_url,
$group_number,
$api_key,
$api_username
);
// Parameters
$paramArray = array(
'group[name]' => $group_name,
'group[alias_level]' => 99,
'group[visible]' => 'true',
'group[usernames]' => $usernames
);
// cURL
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $paramArray ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
$body = curl_exec( $ch );
curl_close( $ch );
// Intrepret results
$json = json_decode( $body, true );
// Probably need some kind of error check
// if ( empty( $json ) || $json['success'] != 'OK' ) {}
}
}
@zsmhub
Copy link

zsmhub commented Mar 31, 2016

Thanks for your share!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment