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