-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathalumni.php
111 lines (96 loc) · 4.65 KB
/
alumni.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
<?php
/**
* Class for working with BB Press and alumni.
*/
class CC_Alumni {
private static $instance;
public $alumni_home_id;
public $alumni_login_id;
public $alumni;
function __construct() {
$this->alumni_home_id = get_field( 'alumni_home_page', 'options' );
$this->alumni_login_id = get_field( 'alumni_login_page', 'options' );
$this->alumni = $this->get_alumni();
$this->actions_manager();
}
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
$c = __CLASS__;
self::$instance = new $c();
}
return self::$instance;
}
public static function get_alumni() {
$key = 'cc_alumni';
if ( false === ( $results = get_transient( $key ) ) ) {
$results = get_users( array ( 'role__in' => array ( 'bbp_participant' ), 'fields' => array ( 'ID', 'user_email', 'display_name' ) ) );
set_transient( $key, $results, HOUR_IN_SECONDS / 4 );
}
return $results;
}
public function render_alumni() {
?>
<div class="alumni-members">
<?php foreach ($this->alumni as $single_alumni) { ?>
<a href="<?php echo bbp_get_user_profile_url( $single_alumni->ID ); ?>" class="alumni-member">
<img class="margin-bottom-small" src="<?php echo get_avatar_url( $single_alumni->ID, array ( 'size' => 120 ) ); ?>" alt="Kitten">
<span class="has-text-weight-bold"><?php echo $single_alumni->display_name; ?></span>
</a>
<?php } ?>
</div>
<?php
}
/**
* Display a notice in the WordPress back-end if the alumni pages aren't linked correctly.
*/
public function notify_if_pages_unlinked () {
?>
<div class="update-nag notice">
<p>One or more alumni pages are unlinked. Please go to the <a href="/wp-admin/admin.php?page=acf-options">options</a> page and connect them.</p>
</div>
<?php
}
/**
* Check if there is a logged in alumni
*/
public static function is_alumni () {
if ( function_exists( 'bbp_get_current_user_id' ) ) {
return ! empty( bbp_get_current_user_id() );
} else {
return is_user_logged_in();
}
}
/**
* Redirect from the alumni login page to the alumni home page if the user is already logged in.
*/
public function redirect_if_logged_in () {
if ( empty( $this->alumni_login_id ) || empty( $this->alumni_home_id ) ) return;
if ( $this->is_alumni() && is_page( $this->alumni_login_id ) ) {
wp_redirect( get_permalink( $this->alumni_home_id ) );
exit;
}
}
/*
* Render a simple dropdown menu for logged-in alumni
*/
public function show_alumni_menu_item () {
if ( $this->is_alumni() ) {
return '<div class="navbar-item has-dropdown is-hoverable"><a class="button alumni is-dropdown"><svg width="10" height="10" viewBox="0 0 10 10" fill="none" xmlns="http://www.w3.org/2000/svg" class="margin-right-small"><path d="M5 5.625C6.55273 5.625 7.8125 4.36523 7.8125 2.8125C7.8125 1.25977 6.55273 0 5 0C3.44727 0 2.1875 1.25977 2.1875 2.8125C2.1875 4.36523 3.44727 5.625 5 5.625ZM7.5 6.25H6.42383C5.99023 6.44922 5.50781 6.5625 5 6.5625C4.49219 6.5625 4.01172 6.44922 3.57617 6.25H2.5C1.11914 6.25 0 7.36914 0 8.75V9.0625C0 9.58008 0.419922 10 0.9375 10H9.0625C9.58008 10 10 9.58008 10 9.0625V8.75C10 7.36914 8.88086 6.25 7.5 6.25Z" fill="#767676"/></svg>
'.bbp_get_current_user_name().' <i class="icon caret-down"></i></a>
<div class="alumni-dropdown navbar-dropdown">
<a class="navbar-item" href="'. get_permalink( $this->alumni_home_id ) .'">Alumni</a>
<a class="navbar-item" href="'. bbp_get_user_profile_edit_url( bbp_get_current_user_id() ).'">Edit Profile</a>
<a class="navbar-item" href="'. wp_logout_url( home_url() ) . '">Sign Out</a>
</div>
</div>';
} else {
return '<div class="navbar-item"><a class="button alumni" href="' . get_permalink( $this->alumni_login_id ) . '"><svg class="margin-right-small" width="10" height="10" viewBox="0 0 10 10" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M5 5.625C6.55273 5.625 7.8125 4.36523 7.8125 2.8125C7.8125 1.25977 6.55273 0 5 0C3.44727 0 2.1875 1.25977 2.1875 2.8125C2.1875 4.36523 3.44727 5.625 5 5.625ZM7.5 6.25H6.42383C5.99023 6.44922 5.50781 6.5625 5 6.5625C4.49219 6.5625 4.01172 6.44922 3.57617 6.25H2.5C1.11914 6.25 0 7.36914 0 8.75V9.0625C0 9.58008 0.419922 10 0.9375 10H9.0625C9.58008 10 10 9.58008 10 9.0625V8.75C10 7.36914 8.88086 6.25 7.5 6.25Z" fill="#008000"/></svg>Alumni</a></div>';
}
}
public function actions_manager() {
add_action( 'template_redirect', array( $this, 'redirect_if_logged_in' ) );
if ( empty ( $this->alumni_home_id ) || empty( $this->alumni_login_id ) ) {
add_action( 'admin_notices', array( $this, 'notify_if_pages_unlinked' ) );
}
}
}