-
Notifications
You must be signed in to change notification settings - Fork 3
/
bp-automatic-friends.php
263 lines (219 loc) · 7.9 KB
/
bp-automatic-friends.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/*
Plugin Name: BuddyPress Automatic Friends
Plugin URI: http://www.stevenword.com/bp-automatic-friends/
Description: Automatically create and accept friendships for specified users upon new user registration. * Requires BuddyPress
Text Domain: bp-automatic-friends
Version: 2.0.7
Author: Steven Word
Author URI: http://stevenword.com/
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Copyright 2013 Steven K. Word
GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* BuddPress Automatic Friends Core
*/
class BPAF_Core {
const VERSION = '2.0.7';
const REVISION = '20141120';
const METAKEY = 'bpaf_global_friend';
const LEGACY_OPTION = 'skw_bpaf_options';
const NONCE = 'bpaf_nonce';
const NONCE_FAIL_MSG = 'Cheatin’ huh?';
const TEXT_DOMAIN = 'bp-automatic-friends';
/* Define and register singleton */
private static $instance = false;
public static function instance() {
if( ! self::$instance ) {
self::$instance = new self;
self::$instance->setup();
}
return self::$instance;
}
/**
* Constructor
*
* @since 2.0.0
*/
private function __construct() { }
/**
* Clone
*
* @since 2.0.0
*/
private function __clone() { }
/**
* Add actions and filters
*
* @uses add_action, add_filter
* @since 2.0.0
*/
function setup() {
$plugin_dir = basename( dirname( __FILE__ ) );
load_plugin_textdomain( self::TEXT_DOMAIN, false, $plugin_dir . '/languages/' );
add_action( 'bp_loaded', array( $this, 'action_bp_loaded' ) );
}
/**
* Loader function only fires if BuddyPress exists.
*
* @uses is_admin, add_action
* @action bp_loaded
* @return null
*/
function action_bp_loaded(){
/* Load the admin */
if ( is_admin() ){
if( function_exists('bp_is_active') && bp_is_active( 'friends' ) ) {
require_once( dirname(__FILE__) . '/inc/admin.php' );
require_once( dirname(__FILE__) . '/inc/update.php' );
} else {
add_action('admin_notices', array( $this, 'admin_notice' ) );
}
}
/* Do this the first time a new user logs in */
add_action( 'wp', array( $this, 'first_login' ) );
}
/**
* New method for creating friendships at first login.
*
* Prevents conflict with plugins such as "Disable Activation" that bypass the activation process.
*
* Hook into the 'wp' action and check if the user is logged in
* and if get_user_meta( $bp->loggedin_user->id, 'last_activity' ) is false.
* http://buddypress.trac.wordpress.org/ticket/3003
*/
function first_login() {
if( ! is_user_logged_in() ) {
return;
}
global $bp;
$last_login = get_user_meta( $bp->loggedin_user->id, 'last_activity', true );
// This needs to be re-added after debugging
if( ! isset( $last_login ) || empty( $last_login ) )
$this->create_friendships( $bp->loggedin_user->id );
}
/**
* Get Global Friends
*
* @return array|bool
*/
public static function get_global_friends() {
global $bp;
// The Query
$user_query = new WP_User_Query( array(
'meta_key' => BPAF_Core::METAKEY,
'meta_value' => true,
'fields' => 'ID'
) );
if ( ! empty( $user_query->results ) ) {
return $user_query->results;
} else {
return false;
}
}
/**
* Create friendships automatically
*
* When a initiator user registers for the blog, create initiator friendship with the specified user(s) and autoaccept those friendhips.
* @global bp
* @param initiator_user_id
* @uses get_userdata, get_option, explode, friends_add_friend, get_friend_user_ids, total_friend_count
* @return null
*/
public static function create_friendships( $initiator_user_id ) {
global $bp;
// Disable email notifications. In situations with hundreds of users, this can get SPAMMY fast
remove_action( 'friends_friendship_requested', 'friends_notification_new_request', 10 );
remove_action( 'friends_friendship_accepted', 'friends_notification_accepted_request', 10 );
/* Get the user data for the initiatorly registered user. */
$initiator_user_info = get_userdata( $initiator_user_id );
/* Get the friend users id(s) */
//$options = get_option( BPAF_Core::LEGACY_OPTION );
//$global_friend_user_ids = $options['s8d_bpaf_user_ids'];
$global_friend_user_ids = self::get_global_friends();
/* Check to see if the admin options are set*/
if ( isset( $global_friend_user_ids ) && ! empty( $global_friend_user_ids ) ){
// @legacy
//$friend_user_ids = explode( ',', $global_friend_user_ids );
$friend_user_ids = $global_friend_user_ids;
foreach ( $friend_user_ids as $friend_user_id ){
// If a friendship between these people already exists, we don't want to do this again
if( $initiator_user_id != $friend_user_id && 'not_friends' == BP_Friends_Friendship::check_is_friend( $initiator_user_id, $friend_user_id ) ) {
/* Request the friendship */
friends_add_friend( $initiator_user_id, $friend_user_id, $force_accept = true );
self::update_friendship_counts( $initiator_user_id );
}
}
}
}
/**
* Destroy Friendships
*
* @global bp
* @param initiator_user_id
* @uses get_userdata, get_option, explode, friends_add_friend, get_friend_user_ids, total_friend_count
* @return null
*/
public static function destroy_friendships( $initiator_user_id ) {
BP_Friends_Friendship::delete_all_for_user( $initiator_user_id );
self::update_friendship_counts( $initiator_user_id );
}
/**
* Update Friendship Counts
*
* @return null
*/
public static function update_friendship_counts( $initiator_user_id ) {
/* Get friends of $user_id */
$friend_ids = BP_Friends_Friendship::get_friend_user_ids( $initiator_user_id );
/* Loop through the initiator's friends and update their friend counts */
foreach ( (array) $friend_ids as $friend_id ) {
BP_Friends_Friendship::total_friend_count( $friend_id );
}
/* Update initiator friend counts */
BP_Friends_Friendship::total_friend_count( $initiator_user_id );
}
/**
* Notify the admin of why we can't load the plugin.
*
* @since 2.0.0
*/
function admin_notice() {
echo '<div class="error"><p>BuddyPress Automatic Friends cannot be loaded because Friend Connections are not enabled. <a href="' . admin_url('options-general.php?page=bp-components') . '">Click Here</a> to enable this BuddyPress component.</p></div>';
}
}
BPAF_Core::instance();
/* Wrappers */
if ( ! function_exists( 'bpaf_get_global_friends' ) ) {
function bpaf_get_global_friends() {
return BPAF_Core::get_global_friends();
}
}
if ( ! function_exists( 'bpaf_create_friendships' ) ) {
function bpaf_create_friendships( $initiator_user_id ) {
BPAF_Core::create_friendships( $initiator_user_id );
}
}
if ( ! function_exists( 'bpaf_destroy_friendships' ) ) {
function bpaf_destroy_friendships( $initiator_user_id ) {
BPAF_Core::destroy_friendships( $initiator_user_id );
}
}
if ( ! function_exists( 'bpaf_update_friendship_counts' ) ) {
function bpaf_update_friendship_counts( $initiator_user_id ) {
BPAF_Core::update_friendship_counts( $initiator_user_id );
}
}