| 1 |
<?php |
| 2 |
/** |
| 3 |
* This is the Subscriptions list |
| 4 |
* |
| 5 |
* @version 1.0 |
| 6 |
* @package Friends |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! empty( $friends_args ) && is_array( $friends_args ) ) { |
| 10 |
$args = array_merge( $friends_args, $args ); |
| 11 |
} |
| 12 |
$args['title'] = __( 'Following', 'friends' ); |
| 13 |
$args['no-bottom-margin'] = true; |
| 14 |
|
| 15 |
$filter = isset( $_GET['filter'] ) ? sanitize_key( $_GET['filter'] ) : 'all'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 16 |
$valid_filters = array( 'all', 'starred' ); |
| 17 |
|
| 18 |
// Add folder filters dynamically. |
| 19 |
$folders = Friends\Subscription::get_folders(); |
| 20 |
$folder_map = array(); |
| 21 |
foreach ( $folders as $folder ) { |
| 22 |
$folder_key = 'folder-' . $folder->term_id; |
| 23 |
$valid_filters[] = $folder_key; |
| 24 |
$folder_map[ $folder_key ] = $folder; |
| 25 |
} |
| 26 |
if ( ! in_array( $filter, $valid_filters, true ) ) { |
| 27 |
$filter = 'all'; |
| 28 |
} |
| 29 |
|
| 30 |
$sort = isset( $_GET['sort'] ) ? sanitize_key( $_GET['sort'] ) : 'name'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 31 |
if ( ! in_array( $sort, array( 'name', 'newest', 'oldest', 'posts' ), true ) ) { |
| 32 |
$sort = 'name'; |
| 33 |
} |
| 34 |
|
| 35 |
$search_term = isset( $_GET['q'] ) ? trim( wp_unslash( $_GET['q'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 36 |
|
| 37 |
Friends\Friends::template_loader()->get_template_part( 'frontend/header', null, $args ); |
| 38 |
|
| 39 |
?> |
| 40 |
<section class="subscriptions"> |
| 41 |
<?php |
| 42 |
$all_subscriptions = Friends\User_Query::all_subscriptions()->get_results(); |
| 43 |
$total = count( $all_subscriptions ); |
| 44 |
|
| 45 |
// Filter. |
| 46 |
if ( 'starred' === $filter ) { |
| 47 |
$filtered = array_filter( |
| 48 |
$all_subscriptions, |
| 49 |
function ( $s ) { |
| 50 |
return $s instanceof Friends\Subscription && $s->is_starred(); |
| 51 |
} |
| 52 |
); |
| 53 |
} elseif ( isset( $folder_map[ $filter ] ) ) { |
| 54 |
$folder_term_id = $folder_map[ $filter ]->term_id; |
| 55 |
$filtered = array_filter( |
| 56 |
$all_subscriptions, |
| 57 |
function ( $s ) use ( $folder_term_id ) { |
| 58 |
if ( ! ( $s instanceof Friends\Subscription ) ) { |
| 59 |
return false; |
| 60 |
} |
| 61 |
$folder = $s->get_folder(); |
| 62 |
return $folder && $folder->term_id === $folder_term_id; |
| 63 |
} |
| 64 |
); |
| 65 |
} else { |
| 66 |
$filtered = $all_subscriptions; |
| 67 |
} |
| 68 |
|
| 69 |
// Search (applied after category filter so the top-of-page totals stay accurate). |
| 70 |
if ( '' !== $search_term ) { |
| 71 |
$search_term_lc = function_exists( 'mb_strtolower' ) ? mb_strtolower( $search_term ) : strtolower( $search_term ); |
| 72 |
$filtered = array_filter( |
| 73 |
$filtered, |
| 74 |
function ( $s ) use ( $search_term_lc ) { |
| 75 |
$haystack = ''; |
| 76 |
if ( isset( $s->display_name ) ) { |
| 77 |
$haystack .= $s->display_name . ' '; |
| 78 |
} |
| 79 |
if ( isset( $s->user_login ) ) { |
| 80 |
$haystack .= $s->user_login . ' '; |
| 81 |
} |
| 82 |
if ( isset( $s->user_url ) ) { |
| 83 |
$haystack .= $s->user_url . ' '; |
| 84 |
} |
| 85 |
if ( isset( $s->description ) ) { |
| 86 |
$haystack .= wp_strip_all_tags( $s->description ); |
| 87 |
} |
| 88 |
$haystack = function_exists( 'mb_strtolower' ) ? mb_strtolower( $haystack ) : strtolower( $haystack ); |
| 89 |
return false !== strpos( $haystack, $search_term_lc ); |
| 90 |
} |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
// Sort. |
| 95 |
$filtered = array_values( $filtered ); |
| 96 |
if ( 'newest' === $sort ) { |
| 97 |
usort( |
| 98 |
$filtered, |
| 99 |
function ( $a, $b ) { |
| 100 |
return strtotime( $b->user_registered ) - strtotime( $a->user_registered ); |
| 101 |
} |
| 102 |
); |
| 103 |
} elseif ( 'oldest' === $sort ) { |
| 104 |
usort( |
| 105 |
$filtered, |
| 106 |
function ( $a, $b ) { |
| 107 |
return strtotime( $a->user_registered ) - strtotime( $b->user_registered ); |
| 108 |
} |
| 109 |
); |
| 110 |
} elseif ( 'posts' === $sort ) { |
| 111 |
usort( |
| 112 |
$filtered, |
| 113 |
function ( $a, $b ) { |
| 114 |
$a_stats = $a->get_post_stats(); |
| 115 |
$b_stats = $b->get_post_stats(); |
| 116 |
return intval( $b_stats['post_count'] ) - intval( $a_stats['post_count'] ); |
| 117 |
} |
| 118 |
); |
| 119 |
} else { |
| 120 |
usort( |
| 121 |
$filtered, |
| 122 |
function ( $a, $b ) { |
| 123 |
return strnatcasecmp( $a->display_name, $b->display_name ); |
| 124 |
} |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
// Paginate. |
| 129 |
$filtered_total = count( $filtered ); |
| 130 |
$subscriptions_per_page = 20; |
| 131 |
$current_page = isset( $_GET['spage'] ) ? max( 1, absint( $_GET['spage'] ) ) : 1; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 132 |
$page_subscriptions = array_slice( $filtered, ( $current_page - 1 ) * $subscriptions_per_page, $subscriptions_per_page ); |
| 133 |
|
| 134 |
// Categorize subscriptions: people (ActivityPub) vs feeds (RSS/Atom). |
| 135 |
$people_count = 0; |
| 136 |
$feed_count = 0; |
| 137 |
foreach ( $all_subscriptions as $sub ) { |
| 138 |
$is_person = false; |
| 139 |
if ( $sub instanceof Friends\Subscription ) { |
| 140 |
foreach ( $sub->get_active_feeds() as $active_feed ) { |
| 141 |
if ( 'activitypub' === $active_feed->get_parser() ) { |
| 142 |
$is_person = true; |
| 143 |
break; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
if ( $is_person ) { |
| 148 |
++$people_count; |
| 149 |
} else { |
| 150 |
++$feed_count; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
$base_url = strtok( $_SERVER['REQUEST_URI'], '?' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
| 155 |
$link_args = array(); |
| 156 |
if ( 'all' !== $filter ) { |
| 157 |
$link_args['filter'] = $filter; |
| 158 |
} |
| 159 |
if ( 'name' !== $sort ) { |
| 160 |
$link_args['sort'] = $sort; |
| 161 |
} |
| 162 |
if ( '' !== $search_term ) { |
| 163 |
$link_args['q'] = $search_term; |
| 164 |
} |
| 165 |
?> |
| 166 |
<p> |
| 167 |
<?php |
| 168 |
echo esc_html( |
| 169 |
sprintf( |
| 170 |
// translators: %s is the number of people. |
| 171 |
_n( 'You are subscribed to %s person.', 'You are subscribed to %s people.', $people_count, 'friends' ), |
| 172 |
number_format_i18n( $people_count ) |
| 173 |
) |
| 174 |
); |
| 175 |
echo ' '; |
| 176 |
echo esc_html( |
| 177 |
sprintf( |
| 178 |
// translators: %s is the number of feeds. |
| 179 |
_n( 'You are subscribed to %s feed.', 'You are subscribed to %s feeds.', $feed_count, 'friends' ), |
| 180 |
number_format_i18n( $feed_count ) |
| 181 |
) |
| 182 |
); |
| 183 |
?> |
| 184 |
</p> |
| 185 |
<form method="get" action="<?php echo esc_url( $base_url ); ?>" class="friends-search" role="search"> |
| 186 |
<label class="friends-search-label"> |
| 187 |
<?php esc_html_e( 'Search:', 'friends' ); ?> |
| 188 |
<input type="search" name="q" value="<?php echo esc_attr( $search_term ); ?>" placeholder="<?php esc_attr_e( 'Name, handle, or description', 'friends' ); ?>" class="friends-search-input" /> |
| 189 |
</label> |
| 190 |
<button type="submit" class="button friends-search-submit"><?php esc_html_e( 'Search', 'friends' ); ?></button> |
| 191 |
<?php if ( 'all' !== $filter ) : ?> |
| 192 |
<input type="hidden" name="filter" value="<?php echo esc_attr( $filter ); ?>" /> |
| 193 |
<?php endif; ?> |
| 194 |
<?php if ( 'name' !== $sort ) : ?> |
| 195 |
<input type="hidden" name="sort" value="<?php echo esc_attr( $sort ); ?>" /> |
| 196 |
<?php endif; ?> |
| 197 |
<?php |
| 198 |
if ( '' !== $search_term ) : |
| 199 |
$clear_args = $link_args; |
| 200 |
unset( $clear_args['q'] ); |
| 201 |
$clear_url = $clear_args ? add_query_arg( $clear_args, $base_url ) : $base_url; |
| 202 |
?> |
| 203 |
<a href="<?php echo esc_url( $clear_url ); ?>" class="friends-search-clear"><?php esc_html_e( 'Clear', 'friends' ); ?></a> |
| 204 |
<?php endif; ?> |
| 205 |
</form> |
| 206 |
<?php if ( '' !== $search_term ) : ?> |
| 207 |
<p> |
| 208 |
<?php |
| 209 |
echo esc_html( |
| 210 |
sprintf( |
| 211 |
// translators: %1$s is the number of matches, %2$s is the search term. |
| 212 |
_n( '%1$s match for "%2$s"', '%1$s matches for "%2$s"', count( $filtered ), 'friends' ), |
| 213 |
number_format_i18n( count( $filtered ) ), |
| 214 |
$search_term |
| 215 |
) |
| 216 |
); |
| 217 |
?> |
| 218 |
</p> |
| 219 |
<?php endif; ?> |
| 220 |
<p> |
| 221 |
<?php esc_html_e( 'Filter:', 'friends' ); ?> |
| 222 |
<?php |
| 223 |
$filters = array( |
| 224 |
'all' => __( 'All', 'friends' ), |
| 225 |
'starred' => __( 'Starred', 'friends' ), |
| 226 |
); |
| 227 |
foreach ( $folders as $folder ) { |
| 228 |
$filters[ 'folder-' . $folder->term_id ] = $folder->name; |
| 229 |
} |
| 230 |
|
| 231 |
$filter_links = array(); |
| 232 |
foreach ( $filters as $filter_key => $filter_label ) { |
| 233 |
$url = $base_url; |
| 234 |
$f_args = $link_args; |
| 235 |
if ( 'all' !== $filter_key ) { |
| 236 |
$f_args['filter'] = $filter_key; |
| 237 |
} else { |
| 238 |
unset( $f_args['filter'] ); |
| 239 |
} |
| 240 |
if ( $f_args ) { |
| 241 |
$url = add_query_arg( $f_args, $url ); |
| 242 |
} |
| 243 |
if ( $filter === $filter_key ) { |
| 244 |
$filter_links[] = '<strong>' . esc_html( $filter_label ) . '</strong>'; |
| 245 |
} else { |
| 246 |
$filter_links[] = '<a href="' . esc_url( $url ) . '">' . esc_html( $filter_label ) . '</a>'; |
| 247 |
} |
| 248 |
} |
| 249 |
echo wp_kses( |
| 250 |
implode( ' | ', $filter_links ), |
| 251 |
array( |
| 252 |
'strong' => array(), |
| 253 |
'a' => array( 'href' => array() ), |
| 254 |
) |
| 255 |
); |
| 256 |
?> |
| 257 |
|
| 258 |
|
| 259 |
<?php esc_html_e( 'Sort:', 'friends' ); ?> |
| 260 |
<?php |
| 261 |
$sorts = array( |
| 262 |
'name' => __( 'Name', 'friends' ), |
| 263 |
'newest' => __( 'Newest', 'friends' ), |
| 264 |
'oldest' => __( 'Oldest', 'friends' ), |
| 265 |
'posts' => __( 'Most Posts', 'friends' ), |
| 266 |
); |
| 267 |
$sort_links = array(); |
| 268 |
foreach ( $sorts as $sort_key => $sort_label ) { |
| 269 |
$url = $base_url; |
| 270 |
$s_args = $link_args; |
| 271 |
if ( 'name' !== $sort_key ) { |
| 272 |
$s_args['sort'] = $sort_key; |
| 273 |
} else { |
| 274 |
unset( $s_args['sort'] ); |
| 275 |
} |
| 276 |
if ( $s_args ) { |
| 277 |
$url = add_query_arg( $s_args, $url ); |
| 278 |
} |
| 279 |
if ( $sort === $sort_key ) { |
| 280 |
$sort_links[] = '<strong>' . esc_html( $sort_label ) . '</strong>'; |
| 281 |
} else { |
| 282 |
$sort_links[] = '<a href="' . esc_url( $url ) . '">' . esc_html( $sort_label ) . '</a>'; |
| 283 |
} |
| 284 |
} |
| 285 |
echo wp_kses( |
| 286 |
implode( ' | ', $sort_links ), |
| 287 |
array( |
| 288 |
'strong' => array(), |
| 289 |
'a' => array( 'href' => array() ), |
| 290 |
) |
| 291 |
); |
| 292 |
?> |
| 293 |
</p> |
| 294 |
<?php if ( empty( $page_subscriptions ) ) : ?> |
| 295 |
<p> |
| 296 |
<?php |
| 297 |
if ( 'all' === $filter ) { |
| 298 |
esc_html_e( "You're not following anyone yet.", 'friends' ); |
| 299 |
} else { |
| 300 |
esc_html_e( 'No results match this filter.', 'friends' ); |
| 301 |
} |
| 302 |
?> |
| 303 |
</p> |
| 304 |
<?php else : ?> |
| 305 |
<ul> |
| 306 |
<?php |
| 307 |
foreach ( $page_subscriptions as $subscription ) { |
| 308 |
$avatar = $subscription->get_avatar_url(); |
| 309 |
$page_url = $subscription->get_local_friends_page_url(); |
| 310 |
$stats = $subscription->get_post_stats(); |
| 311 |
$post_count = intval( $stats['post_count'] ); |
| 312 |
$starred = $subscription instanceof Friends\Subscription && $subscription->is_starred(); |
| 313 |
$folder = $subscription instanceof Friends\Subscription ? $subscription->get_folder() : null; |
| 314 |
$active_feeds = $subscription instanceof Friends\Subscription ? $subscription->get_active_feeds() : array(); |
| 315 |
$unfriend_link = Friends\Admin::get_unfriend_link( $subscription ); |
| 316 |
?> |
| 317 |
<li class="subscription-item"> |
| 318 |
<a href="<?php echo esc_url( $page_url ); ?>" class="subscription-link"> |
| 319 |
<?php if ( $avatar ) : ?> |
| 320 |
<img width="40" height="40" src="<?php echo esc_url( $avatar ); ?>" loading="lazy" class="avatar" /> |
| 321 |
<?php else : ?> |
| 322 |
<img width="40" height="40" src="<?php echo esc_url( get_avatar_url( $subscription->user_login, array( 'size' => 40 ) ) ); ?>" loading="lazy" class="avatar" /> |
| 323 |
<?php endif; ?> |
| 324 |
<span class="subscription-info"> |
| 325 |
<strong class="subscription-name"><?php echo esc_html( $subscription->display_name ); ?></strong> |
| 326 |
<?php if ( $starred ) : ?> |
| 327 |
<span class="starred" title="<?php esc_attr_e( 'Starred', 'friends' ); ?>">★</span> |
| 328 |
<?php endif; ?> |
| 329 |
<?php if ( $folder ) : ?> |
| 330 |
<span class="subscription-folder"><?php echo esc_html( $folder->name ); ?></span> |
| 331 |
<?php endif; ?> |
| 332 |
</span> |
| 333 |
</a> |
| 334 |
<span class="subscription-meta"> |
| 335 |
<?php |
| 336 |
echo esc_html( |
| 337 |
sprintf( |
| 338 |
// translators: %s is the number of posts. |
| 339 |
_n( '%s post', '%s posts', $post_count, 'friends' ), |
| 340 |
number_format_i18n( $post_count ) |
| 341 |
) |
| 342 |
); |
| 343 |
?> |
| 344 |
<?php if ( $subscription->user_registered ) : ?> |
| 345 |
· |
| 346 |
<?php |
| 347 |
echo esc_html( |
| 348 |
sprintf( |
| 349 |
// translators: %s is a date. |
| 350 |
__( 'since %s', 'friends' ), |
| 351 |
date_i18n( get_option( 'date_format' ), strtotime( $subscription->user_registered ) ) |
| 352 |
) |
| 353 |
); |
| 354 |
?> |
| 355 |
<?php endif; ?> |
| 356 |
<?php if ( ! empty( $active_feeds ) ) : ?> |
| 357 |
· |
| 358 |
<?php |
| 359 |
$parser_names = array(); |
| 360 |
foreach ( $active_feeds as $feed ) { |
| 361 |
$parser = $feed->get_parser(); |
| 362 |
if ( $parser && ! in_array( $parser, $parser_names, true ) ) { |
| 363 |
$parser_names[] = $parser; |
| 364 |
} |
| 365 |
} |
| 366 |
echo esc_html( implode( ', ', $parser_names ) ); |
| 367 |
?> |
| 368 |
<?php endif; ?> |
| 369 |
</span> |
| 370 |
<?php if ( $subscription->description ) : ?> |
| 371 |
<p class="subscription-description"><?php echo esc_html( wp_trim_words( $subscription->description, 20 ) ); ?></p> |
| 372 |
<?php endif; ?> |
| 373 |
<?php if ( $unfriend_link ) : ?> |
| 374 |
<span class="subscription-actions"> |
| 375 |
<a href="<?php echo esc_url( $unfriend_link ); ?>" class="subscription-action subscription-unfollow" title="<?php esc_attr_e( 'Unfollow', 'friends' ); ?>"> |
| 376 |
<span class="dashicons dashicons-dismiss"></span> <?php esc_html_e( 'Unfollow', 'friends' ); ?> |
| 377 |
</a> |
| 378 |
</span> |
| 379 |
<?php endif; ?> |
| 380 |
<?php do_action( 'friends_subscription_actions', $subscription ); ?> |
| 381 |
</li> |
| 382 |
<?php |
| 383 |
} |
| 384 |
?> |
| 385 |
</ul> |
| 386 |
<?php |
| 387 |
$total_pages = ceil( $filtered_total / $subscriptions_per_page ); |
| 388 |
if ( $total_pages > 1 ) { |
| 389 |
$pagination_args = array( |
| 390 |
'base' => add_query_arg( 'spage', '%#%' ), |
| 391 |
'format' => '', |
| 392 |
'current' => $current_page, |
| 393 |
'total' => $total_pages, |
| 394 |
); |
| 395 |
if ( $link_args ) { |
| 396 |
$pagination_args['add_args'] = $link_args; |
| 397 |
} |
| 398 |
echo '<nav class="pagination">'; |
| 399 |
echo wp_kses_post( paginate_links( $pagination_args ) ); |
| 400 |
echo '</nav>'; |
| 401 |
} |
| 402 |
?> |
| 403 |
<?php endif; ?> |
| 404 |
</section> |
| 405 |
<?php |
| 406 |
Friends\Friends::template_loader()->get_template_part( |
| 407 |
'frontend/footer', |
| 408 |
null, |
| 409 |
$args |
| 410 |
); |
| 411 |
|