PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.6.6
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.6.6
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.6.6, at includes/public/class-charitable-user-dashboard.php

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