|
2 | 2 | /**
|
3 | 3 | * Class for working with BB Press and alumni.
|
4 | 4 | */
|
5 |
| - class CC_Alumni { |
6 |
| - private static $instance; |
| 5 | +class CC_Alumni { |
| 6 | + private static $instance; |
7 | 7 |
|
8 |
| - public $alumni_home_id; |
9 |
| - public $alumni_login_id; |
10 |
| - public $alumni; |
| 8 | + public $home_page_id; |
| 9 | + public $login_page_id; |
| 10 | + public $members_page_id; |
| 11 | + public $forum_page_id; |
11 | 12 |
|
12 |
| - function __construct() { |
13 |
| - $this->alumni_home_id = get_field( 'alumni_home_page', 'options' ); |
14 |
| - $this->alumni_login_id = get_field( 'alumni_login_page', 'options' ); |
15 |
| - $this->alumni = $this->get_alumni(); |
16 |
| - $this->actions_manager(); |
17 |
| - } |
| 13 | + public $alumni; |
18 | 14 |
|
19 |
| - public static function get_instance() { |
| 15 | + function __construct() { |
| 16 | + $this->home_page_id = get_field( 'alumni_home_page', 'options' ); |
| 17 | + $this->login_page_id = get_field( 'alumni_login_page', 'options' ); |
| 18 | + $this->members_page_id = get_field( 'alumni_members_page', 'options' ); |
| 19 | + $this->forum_page_id = get_field( 'alumni_forum_page', 'options' ); |
| 20 | + |
| 21 | + $this->alumni = $this->get_alumni(); |
| 22 | + $this->actions_manager(); |
| 23 | + } |
| 24 | + |
| 25 | + public static function get_instance() { |
20 | 26 | if ( ! isset( self::$instance ) ) {
|
21 | 27 | $c = __CLASS__;
|
22 | 28 | self::$instance = new $c();
|
23 | 29 | }
|
24 | 30 | return self::$instance;
|
25 | 31 | }
|
26 | 32 |
|
27 |
| - public static function get_alumni() { |
28 |
| - $key = 'cc_alumni'; |
| 33 | + public static function get_alumni() { |
| 34 | + $key = 'cc_alumni'; |
29 | 35 |
|
30 |
| - if ( false === ( $results = get_transient( $key ) ) ) { |
31 |
| - $results = get_users( array ( 'role__in' => array ( 'bbp_participant' ), 'fields' => array ( 'ID', 'user_email', 'display_name' ) ) ); |
32 |
| - set_transient( $key, $results, HOUR_IN_SECONDS / 4 ); |
33 |
| - } |
| 36 | + if ( false === ( $results = get_transient( $key ) ) ) { |
| 37 | + $results = get_users( |
| 38 | + array( |
| 39 | + 'role__in' => array( 'bbp_participant' ), |
| 40 | + 'fields' => array( |
| 41 | + 'ID', |
| 42 | + 'user_email', |
| 43 | + 'display_name', |
| 44 | + ), |
| 45 | + ) |
| 46 | + ); |
| 47 | + set_transient( $key, $results, HOUR_IN_SECONDS / 4 ); |
| 48 | + } |
34 | 49 |
|
35 |
| - return $results; |
36 |
| - } |
| 50 | + return $results; |
| 51 | + } |
37 | 52 |
|
38 |
| - public function render_alumni() { |
39 |
| - ?> |
40 |
| - <div class="alumni-members"> |
41 |
| - <?php foreach ($this->alumni as $single_alumni) { ?> |
| 53 | + public function render_alumni() { |
| 54 | + ?> |
| 55 | + <div class="alumni-members"> |
| 56 | + <?php foreach ( $this->alumni as $single_alumni ) { ?> |
42 | 57 | <a href="<?php echo bbp_get_user_profile_url( $single_alumni->ID ); ?>" class="alumni-member">
|
43 |
| - <img class="margin-bottom-small" src="<?php echo get_avatar_url( $single_alumni->ID, array ( 'size' => 120 ) ); ?>" alt="Kitten"> |
| 58 | + <img class="margin-bottom-small" src="<?php echo get_avatar_url( $single_alumni->ID, array( 'size' => 120 ) ); ?>" alt="Kitten"> |
44 | 59 | <span class="has-text-weight-bold"><?php echo $single_alumni->display_name; ?></span>
|
45 | 60 | </a>
|
46 |
| - <?php } ?> |
47 |
| - </div> |
48 |
| - <?php |
49 |
| - } |
50 |
| - |
51 |
| - /** |
52 |
| - * Display a notice in the WordPress back-end if the alumni pages aren't linked correctly. |
53 |
| - */ |
54 |
| - public function notify_if_pages_unlinked () { |
55 |
| - ?> |
56 |
| - <div class="update-nag notice"> |
57 |
| - <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> |
58 |
| - </div> |
59 |
| - <?php |
60 |
| - } |
61 |
| - |
62 |
| - /** |
63 |
| - * Check if there is a logged in alumni |
64 |
| - */ |
65 |
| - public static function is_alumni () { |
66 |
| - if ( function_exists( 'bbp_get_current_user_id' ) ) { |
67 |
| - return ! empty( bbp_get_current_user_id() ); |
68 |
| - } else { |
69 |
| - return is_user_logged_in(); |
70 |
| - } |
71 |
| - } |
72 |
| - |
73 |
| - /** |
74 |
| - * Redirect from the alumni login page to the alumni home page if the user is already logged in. |
75 |
| - */ |
76 |
| - public function redirect_if_logged_in () { |
77 |
| - if ( empty( $this->alumni_login_id ) || empty( $this->alumni_home_id ) ) return; |
78 |
| - |
79 |
| - if ( $this->is_alumni() && is_page( $this->alumni_login_id ) ) { |
80 |
| - wp_redirect( get_permalink( $this->alumni_home_id ) ); |
81 |
| - exit; |
82 |
| - } |
83 |
| - } |
84 |
| - |
85 |
| - /* |
86 |
| - * Render a simple dropdown menu for logged-in alumni |
| 61 | + <?php } ?> |
| 62 | + </div> |
| 63 | + <?php |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Display a notice in the WordPress back-end if the alumni pages aren't linked correctly. |
| 68 | + */ |
| 69 | + public function notify_if_pages_unlinked() { |
| 70 | + ?> |
| 71 | + <div class="update-nag notice"> |
| 72 | + <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> |
| 73 | + </div> |
| 74 | + <?php |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Check if there is a logged in alumni |
| 79 | + */ |
| 80 | + public static function is_alumni() { |
| 81 | + if ( function_exists( 'bbp_get_current_user_id' ) ) { |
| 82 | + return ! empty( bbp_get_current_user_id() ); |
| 83 | + } else { |
| 84 | + return is_user_logged_in(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Redirect from the alumni login page to the alumni home page if the user is already logged in. |
87 | 90 | */
|
88 |
| - public function show_alumni_menu_item () { |
89 |
| - if ( $this->is_alumni() ) { |
90 |
| - 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> |
| 91 | + public function redirect_if_logged_in() { |
| 92 | + if ( $this->is_alumni() && is_page( $this->login_page_id ) ) { |
| 93 | + wp_redirect( get_permalink( $this->alumni_home_id ) ); |
| 94 | + exit; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Redirect from alumni pages to the alumni login page if the user is not logged in. |
| 100 | + */ |
| 101 | + public function redirect_if_not_logged_in() { |
| 102 | + $is_alumni_page = is_page( $this->home_page_id ) || is_page( $this->members_page_id ) || is_page( $this->forum_page_id ); |
| 103 | + |
| 104 | + if ( ! $this->is_alumni() && $is_alumni_page ) { |
| 105 | + wp_redirect( get_permalink( $this->login_page_id ) ); |
| 106 | + exit; |
| 107 | + } |
| 108 | + } |
91 | 109 |
|
92 |
| - '.bbp_get_current_user_name().' <i class="icon caret-down"></i></a> |
| 110 | + /* |
| 111 | + * Render a simple dropdown menu for logged-in alumni |
| 112 | + */ |
| 113 | + public function show_alumni_menu_item() { |
| 114 | + if ( $this->is_alumni() ) { |
| 115 | + 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> |
| 116 | +
|
| 117 | + ' . bbp_get_current_user_name() . ' <i class="icon caret-down"></i></a> |
93 | 118 | <div class="alumni-dropdown navbar-dropdown">
|
94 |
| - <a class="navbar-item" href="'. get_permalink( $this->alumni_home_id ) .'">Alumni</a> |
95 |
| - <a class="navbar-item" href="'. bbp_get_user_profile_edit_url( bbp_get_current_user_id() ).'">Edit Profile</a> |
96 |
| - <a class="navbar-item" href="'. wp_logout_url( home_url() ) . '">Sign Out</a> |
| 119 | + <a class="navbar-item" href="' . get_permalink( $this->home_page_id ) . '">Alumni</a> |
| 120 | + <a class="navbar-item" href="' . bbp_get_user_profile_edit_url( bbp_get_current_user_id() ) . '">Edit Profile</a> |
| 121 | + <a class="navbar-item" href="' . wp_logout_url( home_url() ) . '">Sign Out</a> |
97 | 122 | </div>
|
98 | 123 | </div>';
|
99 |
| - } else { |
100 |
| - 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>'; |
101 |
| - } |
102 |
| - } |
103 |
| - |
104 |
| - public function actions_manager() { |
105 |
| - add_action( 'template_redirect', array( $this, 'redirect_if_logged_in' ) ); |
106 |
| - |
107 |
| - if ( empty ( $this->alumni_home_id ) || empty( $this->alumni_login_id ) ) { |
108 |
| - add_action( 'admin_notices', array( $this, 'notify_if_pages_unlinked' ) ); |
109 |
| - } |
110 |
| - } |
| 124 | + } else { |
| 125 | + return '<div class="navbar-item"><a class="button alumni" href="' . get_permalink( $this->login_page_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>'; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public function actions_manager() { |
| 130 | + add_action( 'template_redirect', array( $this, 'redirect_if_logged_in' ) ); |
| 131 | + add_action( 'template_redirect', array( $this, 'redirect_if_not_logged_in' ) ); |
| 132 | + |
| 133 | + if ( empty( $this->home_page_id ) || empty( $this->login_page_id ) ) { |
| 134 | + add_action( 'admin_notices', array( $this, 'notify_if_pages_unlinked' ) ); |
| 135 | + } |
| 136 | + } |
111 | 137 | }
|
0 commit comments