<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[438] sites/trunk/profiles.wordpress.org/public_html/wp-content/plugins: Profiles: add association handler plugin</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://meta.trac.wordpress.org/changeset/438">438</a></dd>
<dt>Author</dt> <dd>coffee2code</dd>
<dt>Date</dt> <dd>2014-02-26 20:08:22 +0000 (Wed, 26 Feb 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Profiles: add association handler plugin</pre>

<h3>Added Paths</h3>
<ul>
<li>sites/trunk/profiles.wordpress.org/public_html/wp-content/plugins/wporg-profiles-association-handler/</li>
<li><a href="#sitestrunkprofileswordpressorgpublic_htmlwpcontentpluginswporgprofilesassociationhandlerwporgprofilesassociationhandlerphp">sites/trunk/profiles.wordpress.org/public_html/wp-content/plugins/wporg-profiles-association-handler/wporg-profiles-association-handler.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkprofileswordpressorgpublic_htmlwpcontentpluginswporgprofilesassociationhandlerwporgprofilesassociationhandlerphp"></a>
<div class="addfile"><h4>Added: sites/trunk/profiles.wordpress.org/public_html/wp-content/plugins/wporg-profiles-association-handler/wporg-profiles-association-handler.php (0 => 438)</h4>
<pre class="diff"><span>
<span class="info">--- sites/trunk/profiles.wordpress.org/public_html/wp-content/plugins/wporg-profiles-association-handler/wporg-profiles-association-handler.php                              (rev 0)
+++ sites/trunk/profiles.wordpress.org/public_html/wp-content/plugins/wporg-profiles-association-handler/wporg-profiles-association-handler.php 2014-02-26 20:08:22 UTC (rev 438)
</span><span class="lines">@@ -0,0 +1,200 @@
</span><ins>+<?php
+/*
+Plugin Name: WordPress.org Profiles Association Handler
+Plugin URI: http://wordpress.org
+Author: Scott Reilly
+License: GPL2
+Version: 1.0
+Description: Handles the associations sent from other services in the .org ecosystem (WP, WordCamp, WP, etc).
+*/
+
+defined( 'ABSPATH' ) or die();
+
+if ( ! class_exists( 'WPOrg_Profiles_Association_Handler' ) ) {
+
+       class WPOrg_Profiles_Association_Handler {
+
+               /**
+                * Constructor.
+                */
+               public function __construct() {
+                       add_filter( 'bp_groups_global_tables', array( $this, 'change_table_names' ) );
+                       add_filter( 'bp_active_components',    array( $this, 'activate_groups_component' ) );
+                       add_action( 'plugins_loaded',          array( $this, 'plugins_loaded' ) );
+               }
+
+               /**
+                * Actions to run on the 'plugins_loaded' filter.
+                */
+               public function plugins_loaded() {
+                       add_action( 'wp_ajax_nopriv_wporg_handle_association', array( $this, 'handle_association' ) );
+
+                       // Approve users added to groups
+//                     add_action( 'groups_join_group', array( $this, 'auto_accept_group_invite_for_user' ), 10, 2 );
+
+                       // Disable activity reporting related to groups
+                       add_filter( 'bp_activity_component_before_save',       array( $this, 'disable_group_activity_reporting' ) );
+                       remove_action( 'bp_register_activity_actions',         'groups_register_activity_actions' );
+
+                       // Workaround (temp?) for wp-admin being considered the current user
+                       add_filter( 'bp_displayed_user_id', array( $this, 'wp_admin_is_not_a_user' ) );
+               }
+
+               /**
+                * Changes the table names to use custom wporg groups tables rather
+                * than the ones for BuddyPress.org.
+                *
+                * @param  array $tables The default tables.
+                * @return array
+                */
+               public function change_table_names( $tables ) {
+                       global $bp;
+
+                       return array(
+                               'table_name'           => $bp->table_prefix . 'wporg_groups',
+                               'table_name_members'   => $bp->table_prefix . 'wporg_groups_members',
+                               'table_name_groupmeta' => $bp->table_prefix . 'wporg_groups_groupmeta',
+                       );
+               }
+
+               /**
+                * Ensures that the groups component is activated.
+                *
+                * @param  array $activated Array of activated components.
+                * @return array
+                */
+               public function activate_groups_component( $activated ) {
+                       if ( ! isset( $activated['groups'] ) || '1' != $activated['groups'] ) {
+                               $activated['groups'] = '1';
+                       }
+
+                       return $activated;
+               }
+
+               /**
+                * Disables ALL group-related activities being reported to activity stream.
+                *
+                * @param string $component Component.
+                */
+               public function disable_group_activity_reporting( $component ) {
+                       if ( buddypress()->groups->id == $component ) {
+                               $component = false;
+                       }
+
+                       return $component;
+               }
+
+               /**
+                * Accept all invitations to a group on behalf of the user.
+                *
+                * @param int $group_id Group ID
+                * @param int $user_id  User ID
+                */
+               public function auto_accept_group_invite_for_user( $group_id, $user_id ) {
+                       //TODO
+               }
+
+               /**
+                * BP thinks wp-admin is the slug for a user being displayed (which happens
+                * to actually exist on .org), so clear that until a proper fix is pursued.
+                *
+                * @param  int $user_id The user ID of the displayed_user
+                * @return int
+                */
+               public function wp_admin_is_not_a_user( $user_id ) {
+                       if ( '440141' == $user_id ) {
+                               $user_id = 0;
+                       }
+                       return $user_id;
+               }
+
+               /**
+                * Primary AJAX handler.
+                *
+                * Funnels incoming requests to appropriate sub-handler based on
+                * $_POST['source'] value.
+                *
+                * By default (and for security), this does nothing. The filter
+                * 'wporg_is_valid_association_request' must be hooked in order to provide
+                * the appropriate validity checks on the request to permit the incoming
+                * association notification to be handled.
+                *
+                * TODO: Make this a generic handler and require sub-handlers to
+                * register themselves.
+                */
+               public function handle_association() {
+                       // Return error if not a valid association request.
+                       if ( true !== apply_filters( 'wporg_is_valid_association_request', false ) ) {
+                               die( '-1 Not a valid association request.' );
+                       }
+
+                       // Return error if activities are not enabled.
+                       if ( ! bp_is_active( 'groups' ) ) {
+                               die( '-1 Group component not activated.' );
+                       }
+
+                       $source = $_POST['source'];
+
+                       switch ( $source ) {
+                               case 'wordcamp':
+                                       $association_id = $this->handle_wordcamp_association();
+                                       break;
+                               default:
+                                       $association_id = '-1 Unrecognized association source.';
+                                       break;
+                       }
+
+                       if ( false === $association_id ) {
+                               $association_id = '-1 Unable to save association.';
+                       }
+
+                       $success = intval( $association_id ) > 0 ? '1' : $association_id;
+                       die( $success );
+               }
+
+               /**
+                * Handles incoming associations for WordCamp.
+                *
+                * Payload: (beyond 'action' and 'source')
+                *  user_id:     User ID
+                *  association: Slug for group/association
+                *  command:     Either 'add' or 'remove'
+                */
+               private function handle_wordcamp_association() {
+                       $user = get_user_by( 'id', $_POST['user_id'] );
+
+                       if ( ! $user ) {
+                               return '-1 Association reported for unrecognized user: ' . sanitize_text_field( $_POST['user_id'] );
+                       }
+
+                       $association = sanitize_key( $_POST['association'] );
+
+                       $associated_associations = array( 'wordcamp-organizer', 'wordcamp-speaker' );
+
+                       if ( ! in_array( $association, $associated_associations ) ) {
+                               return '-1 Unrecognized association type';
+                       }
+
+                       if ( ! $group_id = BP_Groups_Group::group_exists( $association ) ) {
+                               return '-1 Association does not exist: ' . $association;
+                       }
+
+                       if ( 'add' == $_POST['command'] ) {
+                               groups_join_group( $group_id, $user->ID );
+                               groups_accept_invite( $user->ID, $group_id );
+                       } elseif ( 'remove' == $_POST['command'] ) {
+                               groups_leave_group( $group_id, $user->ID );
+                       } else {
+                               return '-1 Unknown association command';
+                       }
+
+                       return 1;
+               }
+
+       } /* /class WPOrg_Profiles_Association_Handler */
+} /* if class_exists */
+
+if ( class_exists( 'WPOrg_Profiles_Association_Handler' ) ) {
+       new WPOrg_Profiles_Association_Handler();
+}
+
</ins></span></pre>
</div>
</div>

</body>
</html>