-
Notifications
You must be signed in to change notification settings - Fork 0
/
bp-links-dtheme.php
424 lines (351 loc) · 13 KB
/
bp-links-dtheme.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
/*
* Display functions that are specific to the bp-links-default theme
*/
function bp_links_is_default_theme() {
return ( BP_LINKS_THEME == BP_LINKS_DEFAULT_THEME );
}
function bp_links_dtheme_enqueue_styles() {
wp_enqueue_style( 'bp-links-default', BP_LINKS_THEME_URL . '/_inc/css/default.css' );
wp_enqueue_style( 'bp-links-forms', BP_LINKS_THEME_URL . '/_inc/css/forms.css' );
wp_enqueue_style( 'colorbox', BP_LINKS_THEME_URL . '/_inc/css/colorbox/colorbox.css' );
}
function bp_links_dtheme_enqueue_scripts( $forms = false ) {
// load global ajax scripts
wp_enqueue_script( 'bp-links-ajax', BP_LINKS_THEME_URL_INC . '/global.js', array('jquery') );
// load color box JS
wp_enqueue_script( 'colorbox', BP_LINKS_THEME_URL_INC . '/jquery.colorbox-min.js', array('jquery') );
// load create forms ajax scripts if necessary
if ( $forms || bp_links_is_link_admin_page() ) {
wp_enqueue_script( 'bp-links-ajax-forms', BP_LINKS_THEME_URL_INC . '/forms.js', array('jquery') );
}
}
//
// Template Actions / Filters
//
function bp_links_dtheme_add_css() {
global $bp;
if ( !bp_links_is_default_theme() )
return false;
if ( bp_is_links_component() ) {
bp_links_dtheme_enqueue_styles();
}
do_action( 'bp_links_dtheme_add_css' );
}
add_action( 'wp_enqueue_scripts', 'bp_links_dtheme_add_css' );
function bp_links_dtheme_add_js() {
global $bp;
if ( !bp_links_is_default_theme() )
return false;
// leaving this debug code here on purpose
//var_dump( bp_current_component(), bp_current_action(), $bp->action_variables );
if ( bp_is_links_component() ) {
bp_links_dtheme_enqueue_scripts( ( $bp->current_action == 'create' ) );
}
do_action( 'bp_links_dtheme_add_js' );
}
add_action( 'wp_enqueue_scripts', 'bp_links_dtheme_add_js' );
function bp_links_dtheme_activity_type_tabs_setup() {
global $bp;
if ( !bp_links_is_default_theme() )
return false;
if ( is_user_logged_in() && bp_links_total_links_for_user( bp_loggedin_user_id() ) ) {
echo sprintf(
'<li id="activity-links"><a href="%s" title="%s">%s</a></li>',
bp_loggedin_user_domain() . BP_ACTIVITY_SLUG . '/' . bp_links_slug() . '/',
__( 'The activity of links I created.', 'buddypress-links' ),
__( 'My Links', 'buddypress-links' ) .
sprintf( ' <span>%s</span>', bp_links_total_links_for_user( bp_loggedin_user_id() ) )
);
}
}
add_action( 'bp_before_activity_type_tab_mentions', 'bp_links_dtheme_activity_type_tabs_setup' );
function bp_links_dtheme_activity_filter_options_setup() {
global $bp;
if ( !bp_links_is_default_theme() )
return false;
echo sprintf( '<option value="%s">%s</option>', BP_LINKS_ACTIVITY_ACTION_CREATE, __( 'Show Link Created', 'buddypress-links' ) );
echo sprintf( '<option value="%s">%s</option>', BP_LINKS_ACTIVITY_ACTION_COMMENT, __( 'Show Link Comments', 'buddypress-links' ) );
echo sprintf( '<option value="%s">%s</option>', BP_LINKS_ACTIVITY_ACTION_VOTE, __( 'Show Link Votes', 'buddypress-links' ) );
}
add_action( 'bp_activity_filter_options', 'bp_links_dtheme_activity_filter_options_setup' );
add_action( 'bp_link_activity_filter_options', 'bp_links_dtheme_activity_filter_options_setup' );
function bp_links_dtheme_screen_notification_settings() {
if ( !bp_links_is_default_theme() )
return false;
echo bp_links_notification_settings();
}
add_action( 'bp_notification_settings', 'bp_links_dtheme_screen_notification_settings' );
//
// Template Tags
//
function bp_links_dtheme_search_form() {
global $bp; ?>
<form action="" method="get" id="search-links-form">
<label><input type="text" name="s" id="links_search" value="<?php if ( isset( $_GET['s'] ) ) { echo attribute_escape( $_GET['s'] ); } ?>" /></label>
<input type="submit" id="links_search_submit" name="links_search_submit" value="<?php _e( 'Search', 'buddypress-links' ) ?>" />
</form> <?php
}
function bp_links_dtheme_creation_tabs() {
global $bp;
$href = sprintf( '%s/%s/create/', $bp->root_domain, bp_links_root_slug() ); ?>
<li class="current"><a href="<?php echo $href ?>"><?php _e( 'Create', 'buddypress-links' ) ?></a></li>
<li><a href="<?php echo $href ?>"><?php _e( 'Start Over', 'buddypress-links' ) ?></a></li> <?php
do_action( 'bp_links_dtheme_creation_tabs' );
}
function bp_links_dtheme_directory_mylinks_tab()
{
if (
true === BP_LINKS_ENABLE_MYLINKS_TAB &&
true === is_user_logged_in()
) {
// render tab ?>
<li id="links-mylinks"><a href="<?php echo bp_loggedin_user_domain() . bp_links_slug() . '/my-links/' ?>"><?php _e( 'My Links', 'buddypress-links' ) ?> <span><?php echo bp_links_total_links_for_user( bp_loggedin_user_id() ) ?></span></a></li><?php
}
}
add_action( 'bp_links_directory_link_types', 'bp_links_dtheme_directory_mylinks_tab' );
function bp_links_dtheme_directory_create_tab()
{
if ( true === is_user_logged_in() ) {
// render tab ?>
<li id="links-create"><a href="<?php echo bp_get_root_domain() . '/' . bp_links_root_slug() . '/create/'; ?>" class="no-ajax"><?php _e( 'Create a Link', 'buddypress-links' ) ?></a></li><?php
}
}
add_action( 'bp_links_directory_link_types', 'bp_links_dtheme_directory_create_tab', 99 );
function bp_links_dtheme_link_order_options_list()
{
// render list ?>
<li id="links-order-select" class="last filter">
<?php _e( 'Order By:', 'buddypress' ) ?>
<select id="links-order-by">
<?php bp_links_link_order_options() ?>
<?php do_action( 'bp_links_dtheme_link_order_options_list' ) ?>
</select>
</li><?php
}
add_action( 'bp_links_item_list_tabs', 'bp_links_dtheme_link_order_options_list' );
add_action( 'bp_links_group_item_list_filters', 'bp_links_dtheme_link_order_options_list' );
function bp_links_dtheme_link_category_filter_options_list()
{
// render list ?>
<li id="links-category-select" class="last filter">
<?php _e( 'Category:', 'buddypress-links' ) ?>
<select id="links-category-filter">
<option value="-1"><?php _e( 'All', 'buddypress' ) ?></option>
<?php bp_links_category_select_options() ?>
<?php do_action( 'bp_links_category_filter_options' ) ?>
</select>
</li><?php
}
add_action( 'bp_links_item_list_tabs', 'bp_links_dtheme_link_category_filter_options_list' );
add_action( 'bp_links_group_item_list_filters', 'bp_links_dtheme_link_category_filter_options_list' );
//
// AJAX Actions and Filters
//
/**
* Links Directory Hook
*/
function bp_links_dtheme_template_loader() {
bp_links_locate_template( array( 'links-loop.php' ), true );
die();
}
add_action( 'wp_ajax_links_filter', 'bp_links_dtheme_template_loader' );
add_action( 'wp_ajax_nopriv_links_filter', 'bp_links_dtheme_template_loader' );
/**
* Augment profile Links page sub-navigation
*/
function bp_links_dtheme_personal_links_subnav()
{
// are we the current component?
if ( BP_LINKS_SLUG === bp_current_component() ) {
// yep, spit out special subnav items
bp_links_dtheme_link_order_options_list();
bp_links_dtheme_link_category_filter_options_list();
}
}
add_action( 'bp_member_plugin_options_nav', 'bp_links_dtheme_personal_links_subnav' );
/**
* Parse the custom delimeted extras string into an array
*
* @param string $string
* @return array
*/
function bp_links_dtheme_parse_extras( $string )
{
// params to return
$params = array();
foreach( explode( '|', $string ) as $item ) {
$next = explode( ':', $item );
$params[ $next[0] ] = $next[ 0 ];
}
return $params;
}
/**
* Filter all AJAX bp_filter_request() calls for the category filter
*
* @param string $query_string
* @param string $object
* @return string
*/
function bp_links_dtheme_category_filter( $query_string, $object, $object_filter, $object_scope, $object_page, $object_search_terms, $object_extras ) {
global $bp;
$filter_enabled = ( bp_is_links_component() || 'links' == $object );
$filter_enabled = apply_filters( 'bp_links_dtheme_category_filter_enabled', $filter_enabled );
if ( true === $filter_enabled ) {
$extras = bp_links_dtheme_parse_extras( $object_extras );
$selected_category =
( isset( $extras['category_id'] ) ) ? $extras['category_id'] : null;
if ( !empty( $selected_category ) ) {
$args = array();
parse_str( $query_string, $args );
$args['category_id'] = $selected_category;
return http_build_query( $args );
}
}
return $query_string;
}
add_filter( 'bp_dtheme_ajax_querystring', 'bp_links_dtheme_category_filter', 1, 7 );
add_filter( 'bp_legacy_theme_ajax_querystring', 'bp_links_dtheme_category_filter', 1, 7 );
/**
* Filter all AJAX bp_filter_request() calls to user ids to profile page calls
*
* @param string $query_string
* @param string $object
* @param string $filter
* @param string $scope
* @return string
*/
function bp_links_dtheme_directory_filter( $query_string, $object, $filter, $scope ) {
global $bp;
// look for links component
if ( bp_is_links_component() ) {
// must be my links action or scope
if ( 'mylinks' == $scope || 'my-links' == $bp->current_action ) {
$args = array();
parse_str( $query_string, $args );
// inject user id
$args['user_id'] = ( bp_is_user() ) ? $bp->displayed_user->id : $bp->loggedin_user->id;
return http_build_query( $args );
} elseif ( 'cat_' == substr( $scope, 0, 4 ) ) {
// split at underscore
$parts = explode( '_', $scope );
// parse query string
$args = array();
parse_str( $query_string, $args );
// inject category id
$args['category_id'] = (integer) $parts[1];
// return modified query
return http_build_query( $args );
}
}
return $query_string;
}
add_filter( 'bp_dtheme_ajax_querystring', 'bp_links_dtheme_directory_filter', 1, 4 );
add_filter( 'bp_legacy_theme_ajax_querystring', 'bp_links_dtheme_directory_filter', 1, 4 );
/**
* Filter all AJAX bp_activity_request() calls for the 'activity' object with the 'links' scope
*
* @param string $query_string
* @param string $object
* @param string $filter
* @param string $scope
* @param integer $page
* @param string $search_terms
* @param string $extras
* @return string
*/
function bp_links_dtheme_activity_filter( $query_string, $object, $filter, $scope, $page, $search_terms, $extras )
{
global $bp;
$do_filter = false;
// only filter activity.
if ( bp_links_is_activity_enabled() && $bp->activity->id == $object ) {
if ( bp_is_user() ) {
// handle filtering for profile pages
// this nav does not use AJAX so don't rely on $scope
if ( bp_is_activity_component() && bp_links_slug() == $bp->current_action ) {
$do_filter = 'user';
}
} else {
// handle filtering for all non-profile, non-links pages
if ( !bp_current_component() || bp_is_activity_component() ) {
// filter under 'activity' component with 'links' scope
if ( bp_links_id() == $scope ) {
$do_filter = 'user';
}
} elseif ( bp_is_links_component() ) {
// filter 'links' component home pages
if ( $bp->is_single_item ) {
$do_filter = 'default';
}
}
}
}
if ( $do_filter ) {
// parse query string
$args = array();
parse_str( $query_string, $args );
switch ( $do_filter ) {
case 'user':
// override with links object
$args['object'] = bp_links_id();
// user_id must be empty to show OTHER user's actions for this user's links
$args['user_id'] = false;
// get recent link cloud ids for this user
$recent_ids = bp_links_recent_activity_item_ids_for_user();
// if there is activity, send the ids
if ( count( $recent_ids ) )
$args['primary_id'] = join( ',', $recent_ids );
break;
case 'default':
// override with links object
$args['object'] = bp_links_id();
// set primary id to current link id if applicable
if ( $bp->links->current_link ) {
$args['primary_id'] = $bp->links->current_link->id;
}
break;
}
// return modified query string
return http_build_query( $args );
}
// no filtering
return $query_string;
}
add_filter( 'bp_dtheme_ajax_querystring', 'bp_links_dtheme_activity_filter', 1, 7 );
add_filter( 'bp_legacy_theme_ajax_querystring', 'bp_links_dtheme_activity_filter', 1, 7 );
/**
* Return "my links" feed URL on activity home page
*
* @param string $feed_url
* @param string $scope
* @return string
*/
function bp_links_dtheme_activity_feed_url( $feed_url, $scope ) {
global $bp;
if ( !bp_links_is_activity_enabled() || empty( $scope ) || $scope != bp_links_id() )
return $feed_url;
return $bp->loggedin_user->domain . BP_ACTIVITY_SLUG . '/my-links/feed/';
}
add_filter( 'bp_dtheme_activity_feed_url', 'bp_links_dtheme_activity_feed_url', 11, 2 );
/**
* Handle creating a custom update to a Link
*
* @return integer|false Activity id that was created
*/
function bp_links_dtheme_activity_custom_update() {
// variable number of args workaround
// see https://buddypress.trac.wordpress.org/ticket/6630
if ( func_num_args() === 4 ) {
list( $activity_id, $object, $item_id, $content ) = func_get_args();
} else {
$activity_id = false;
list( $object, $item_id, $content ) = func_get_args();
}
// if object is links, try a custom update
if ( 'links' == $object ) {
return bp_links_post_update( array( 'type' => BP_LINKS_ACTIVITY_ACTION_COMMENT, 'link_id' => $item_id, 'content' => $content ) );
}
}
add_filter( 'bp_activity_custom_update', 'bp_links_dtheme_activity_custom_update', 10, 4 );
?>