PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.11
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.11
1.8.13.1 1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 All 212 releases
charitable / includes / public / class-charitable-user-dashboard.php

class-charitable-user-dashboard.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.11, at includes/public/class-charitable-user-dashboard.php

257 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Main class for setting up the Charitable User Dashboard.
4 *
5 * @package Charitable/Classes/Charitable_User_Dashboard
6 * @author David Bisset
7 * @copyright Copyright (c) 2022, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.0.0
10 * @version 1.6.42
11 */
12
13 /* Exit if accessed directly */
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_User_Dashboard' ) ) :
19
20 /**
21 * Charitable_User_Dashboard
22 *
23 * @since 1.0.0
24 */
25 class Charitable_User_Dashboard {
26
27 /**
28 * The single instance of this class.
29 *
30 * @var Charitable_User_Dashboard|null
31 */
32 private static $instance = null;
33
34 /**
35 * Stores whether the current request is in the user dashboard.
36 *
37 * @since 1.6.42
38 *
39 * @var boolean
40 */
41 private $on_user_dashboard_page;
42
43 /**
44 * Returns and/or create the single instance of this class.
45 *
46 * @since 1.2.0
47 *
48 * @return Charitable_User_Dashboard
49 */
50 public static function get_instance() {
51 if ( is_null( self::$instance ) ) {
52 self::$instance = new self();
53 }
54
55 return self::$instance;
56 }
57
58 /**
59 * Create class instance.
60 *
61 * @since 1.0.0
62 */
63 private function __construct() {
64 add_action( 'after_setup_theme', array( $this, 'register_menu' ), 100 );
65 add_action( 'template_include', array( $this, 'load_user_dashboard_template' ) );
66 add_action( 'wp_update_nav_menu', array( $this, 'flush_menu_object_cache' ) );
67 add_action( 'wp_update_nav_menu_item', array( $this, 'flush_menu_object_cache' ) );
68 add_filter( 'body_class', array( $this, 'add_body_class' ) );
69
70 do_action( 'charitable_user_dashboard_start', $this );
71 }
72
73 /**
74 * Register navigation menu for frontend dashboard.
75 *
76 * @since 1.0.0
77 *
78 * @return void
79 */
80 public function register_menu() {
81 register_nav_menu( 'charitable-dashboard', __( 'User Dashboard', 'charitable' ) );
82 }
83
84 /**
85 * Returns the user dashboard navigation menu.
86 *
87 * @uses wp_nav_menu
88 * @since 1.0.0
89 *
90 * @param array $args Additional arguments to pass to wp_nav_menu.
91 * @return string|false|void Menu output if $echo is false, false if there are no items or no menu was found.
92 */
93 public function nav( $args ) {
94 $defaults = array(
95 'theme_location' => 'charitable-dashboard',
96 'fallback_cb' => false,
97 );
98
99 $args = wp_parse_args( $args, $defaults );
100
101 return wp_nav_menu( $args );
102 }
103
104 /**
105 * Return the menu ID based on the theme location.
106 *
107 * @since 1.0.0
108 *
109 * @return int 0 if no menu found. Menu ID otherwise.
110 */
111 public function get_nav_id() {
112 $locations = get_nav_menu_locations();
113
114 if ( ! isset( $locations['charitable-dashboard'] ) ) {
115 return 0;
116 }
117
118 return wp_get_nav_menu_object( $locations['charitable-dashboard'] );
119 }
120
121 /**
122 * Returns all objects in the user dashboard navigation.
123 *
124 * @uses wp_get_nav_menu_items
125 * @since 1.0.0
126 *
127 * @return WP_Post[]
128 */
129 public function nav_objects() {
130 $objects = get_transient( 'charitable_user_dashboard_objects' );
131
132 if ( false === $objects ) {
133 $objects = array();
134 $nav_menu_items = wp_get_nav_menu_items( $this->get_nav_id() );
135
136 if ( is_array( $nav_menu_items ) ) {
137 foreach ( $nav_menu_items as $nav_menu_item ) {
138 switch ( $nav_menu_item->type ) {
139 case 'custom':
140 $identifier = trailingslashit( $nav_menu_item->url );
141 break;
142
143 default:
144 $identifier = apply_filters( 'charitable_nav_menu_object_identifier', $nav_menu_item->object_id, $nav_menu_item );
145 }
146
147 $objects[] = $identifier;
148 }
149 }
150
151 set_transient( 'charitable_user_dashboard_objects', $objects );
152 }
153
154 return $objects;
155 }
156
157 /**
158 * Flushes the menu object cache after updating a menu or menu item.
159 *
160 * @since 1.0.0
161 *
162 * @param int $menu_id The menu id.
163 * @return void
164 */
165 public function flush_menu_object_cache( $menu_id ) {
166 $dashboard_menu = $this->get_nav_id();
167
168 if ( ! $dashboard_menu ) {
169 delete_transient( 'charitable_user_dashboard_objects' );
170 return;
171 }
172
173 $nav_menu = wp_get_nav_menu_object( $dashboard_menu );
174
175 /* If $nav_menu is not set that means the location 'charitable-dashboard' is not checked. */
176 if ( $menu_id == $nav_menu->term_id ) {
177 delete_transient( 'charitable_user_dashboard_objects' );
178 }
179 }
180
181 /**
182 * Checks whether the current requested page is in the user dashboard nav.
183 *
184 * @since 1.0.0
185 *
186 * @return boolean
187 */
188 public function in_nav() {
189 global $wp;
190
191 if ( ! isset( $this->on_user_dashboard_page ) ) {
192 $current_url = trailingslashit( charitable_get_current_url() );
193
194 $this->on_user_dashboard_page = in_array( get_queried_object_id(), $this->nav_objects() ) || in_array( $current_url, $this->nav_objects() );
195
196 /**
197 * Set whether we're in the user dashboard.
198 *
199 * @param boolean $on_user_dashboard_page Whether we're in the user dashboard.
200 * @param array $nav_objects The navigation menu objects for the user dashboard.
201 */
202 $this->on_user_dashboard_page = apply_filters( 'charitable_is_in_user_dashboard', $this->on_user_dashboard_page, $this->nav_objects() );
203 }
204
205 return $this->on_user_dashboard_page;
206 }
207
208 /**
209 * Loads the user dashboard template.
210 *
211 * @since 1.0.0
212 *
213 * @param string $template The template to use for the user dashboard.
214 * @return string
215 */
216 public function load_user_dashboard_template( $template ) {
217 /**
218 * The user dashboard template is not loaded by default; this has to be enabled.
219 */
220 if ( false === apply_filters( 'charitable_force_user_dashboard_template', false ) ) {
221 return $template;
222 }
223
224 /**
225 * The current object isn't in the nav, so return the template.
226 */
227 if ( ! $this->in_nav() ) {
228 return $template;
229 }
230
231 do_action( 'charitable_is_user_dashboard' );
232
233 $new_template = apply_filters( 'charitable_user_dashboard_template', 'user-dashboard.php' );
234 $template = charitable_get_template_path( $new_template, $template );
235
236 return $template;
237 }
238
239 /**
240 * Add the user-dashboard class to the body if we're looking at it.
241 *
242 * @since 1.0.0
243 *
244 * @param array $classes Body classes.
245 * @return array
246 */
247 public function add_body_class( $classes ) {
248 if ( $this->in_nav() ) {
249 $classes[] = 'user-dashboard';
250 }
251
252 return $classes;
253 }
254 }
255
256 endif;
257