.gitkeep
3 years ago
BasePageController.php
2 years ago
BuyPageController.php
2 years ago
CollectionPageController.php
2 years ago
DashboardController.php
3 years ago
ProductPageController.php
2 years ago
PurchaseController.php
3 years ago
SubscriptionsController.php
3 years ago
WebhookController.php
2 years ago
DashboardController.php
213 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Web; |
| 4 | |
| 5 | use SureCart\Models\CustomerLink; |
| 6 | use SureCart\Models\User; |
| 7 | |
| 8 | /** |
| 9 | * Thank you routes |
| 10 | */ |
| 11 | class DashboardController { |
| 12 | /** |
| 13 | * Get the enabled navigation. |
| 14 | * |
| 15 | * @return array |
| 16 | */ |
| 17 | public function getEnabledNavigation( $id ) { |
| 18 | return array_values( |
| 19 | array_filter( |
| 20 | [ 'orders', 'subscriptions', 'downloads', 'account', 'billing' ], |
| 21 | function( $name ) use ( $id ) { |
| 22 | if ( 'billing' === $name ) { |
| 23 | if ( ! get_post_meta( $id, '_surecart_dashboard_navigation_billing', true ) ) { |
| 24 | return false; |
| 25 | } |
| 26 | if ( empty( User::current()->customerId( 'live' ) ) && empty( User::current()->customerId( 'test' ) ) ) { |
| 27 | return false; |
| 28 | } |
| 29 | return true; |
| 30 | } |
| 31 | return get_post_meta( $id, '_surecart_dashboard_navigation_' . $name, true ); |
| 32 | } |
| 33 | ) |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Get data for the page. |
| 39 | * |
| 40 | * @return array |
| 41 | */ |
| 42 | public function getData() { |
| 43 | return [ |
| 44 | 'show_logo' => get_post_meta( get_the_ID(), '_surecart_dashboard_show_logo', true ), |
| 45 | 'logo_url' => \SureCart::account()->brand->logo_url, |
| 46 | 'logo_width' => get_post_meta( get_the_ID(), '_surecart_dashboard_logo_width', true ), |
| 47 | 'current_url' => ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], |
| 48 | 'user' => wp_get_current_user(), |
| 49 | 'dashboard_url' => get_permalink( get_the_ID() ), |
| 50 | 'logout_link' => wp_logout_url( get_permalink( get_the_ID() ) ), |
| 51 | 'active_tab' => esc_html( $_GET['model'] ?? 'dashboard' ), |
| 52 | 'navigation' => $this->getNavigation(), |
| 53 | 'account_navigation' => $this->getAccountNavigation(), |
| 54 | ]; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Is this tab active. |
| 59 | */ |
| 60 | public function isActive( $tab ) { |
| 61 | return esc_html( $_GET['model'] ?? 'dashboard' ) === $tab; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get the account navigation. |
| 66 | * |
| 67 | * @return array |
| 68 | */ |
| 69 | public function getAccountNavigation() { |
| 70 | $dashboard_url = get_permalink( get_the_ID() ); |
| 71 | $enabled_navigation = $this->getEnabledNavigation( get_the_ID() ); |
| 72 | $customer_id = ! empty( User::current()->customerId( 'live' ) ) ? User::current()->customerId( 'live' ) : User::current()->customerId( 'test' ); |
| 73 | return array_filter( |
| 74 | [ |
| 75 | 'account' => [ |
| 76 | 'icon_name' => 'user', |
| 77 | 'name' => __( 'Account', 'surecart' ), |
| 78 | 'active' => $this->isActive( 'account' ), |
| 79 | 'href' => add_query_arg( |
| 80 | [ |
| 81 | 'action' => 'edit', |
| 82 | 'model' => 'user', |
| 83 | ], |
| 84 | $dashboard_url |
| 85 | ), |
| 86 | ], |
| 87 | 'billing' => ! empty( $customer_id ) ? [ |
| 88 | 'icon_name' => 'credit-card', |
| 89 | 'name' => __( 'Billing', 'surecart' ), |
| 90 | 'active' => $this->isActive( 'billing' ), |
| 91 | 'href' => add_query_arg( |
| 92 | [ |
| 93 | 'action' => 'show', |
| 94 | 'model' => 'customer', |
| 95 | 'id' => $customer_id, |
| 96 | ], |
| 97 | $dashboard_url |
| 98 | ), |
| 99 | ] : false, |
| 100 | ], |
| 101 | function( $item, $key ) use ( $enabled_navigation ) { |
| 102 | return $item && in_array( $key, $enabled_navigation, true ); |
| 103 | }, |
| 104 | ARRAY_FILTER_USE_BOTH |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get the navigation. |
| 110 | * |
| 111 | * @return array |
| 112 | */ |
| 113 | public function getNavigation() { |
| 114 | $dashboard_url = get_permalink( get_the_ID() ); |
| 115 | $enabled_navigation = $this->getEnabledNavigation( get_the_ID() ); |
| 116 | |
| 117 | return array_filter( |
| 118 | [ |
| 119 | 'dashboard' => [ |
| 120 | 'icon_name' => 'server', |
| 121 | 'name' => __( 'Dashboard', 'surecart' ), |
| 122 | 'active' => $this->isActive( 'dashboard' ), |
| 123 | 'href' => $dashboard_url, |
| 124 | ], |
| 125 | 'orders' => [ |
| 126 | 'icon_name' => 'shopping-bag', |
| 127 | 'name' => __( 'Orders', 'surecart' ), |
| 128 | 'active' => $this->isActive( 'order' ), |
| 129 | 'href' => add_query_arg( |
| 130 | [ |
| 131 | 'action' => 'index', |
| 132 | 'model' => 'order', |
| 133 | ], |
| 134 | $dashboard_url |
| 135 | ), |
| 136 | ], |
| 137 | 'subscriptions' => [ |
| 138 | 'icon_name' => 'repeat', |
| 139 | 'name' => __( 'Plans', 'surecart' ), |
| 140 | 'active' => $this->isActive( 'subscription' ), |
| 141 | 'href' => add_query_arg( |
| 142 | [ |
| 143 | 'action' => 'index', |
| 144 | 'model' => 'subscription', |
| 145 | ], |
| 146 | $dashboard_url |
| 147 | ), |
| 148 | ], |
| 149 | 'downloads' => [ |
| 150 | 'icon_name' => 'download-cloud', |
| 151 | 'name' => __( 'Downloads', 'surecart' ), |
| 152 | 'active' => $this->isActive( 'download' ), |
| 153 | 'href' => add_query_arg( |
| 154 | [ |
| 155 | 'action' => 'index', |
| 156 | 'model' => 'download', |
| 157 | ], |
| 158 | $dashboard_url |
| 159 | ), |
| 160 | ], |
| 161 | ], |
| 162 | function( $item, $key ) use ( $enabled_navigation ) { |
| 163 | return $item && ( 'dashboard' === $key || in_array( $key, $enabled_navigation, true ) ); |
| 164 | }, |
| 165 | ARRAY_FILTER_USE_BOTH |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Show the dashboard. |
| 171 | */ |
| 172 | public function show( $request, $view ) { |
| 173 | // do not cache. |
| 174 | $request->noCache(); |
| 175 | return \SureCart::view( $view ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Get the link from the request. |
| 180 | * |
| 181 | * @param string $link_id The link id. |
| 182 | * @return string|\WP_Error |
| 183 | */ |
| 184 | public function getLink( $link_id ) { |
| 185 | $link = CustomerLink::find( $link_id ); |
| 186 | if ( is_wp_error( $link ) ) { |
| 187 | return $link; |
| 188 | } |
| 189 | return $link; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Login the user |
| 194 | * |
| 195 | * @param int|\WP_User $wp_user WordPress user. |
| 196 | * @return void |
| 197 | */ |
| 198 | public function loginUser( $wp_user ) { |
| 199 | if ( ! $wp_user ) { |
| 200 | return wp_die( esc_html__( 'This user could not be found.', 'surecart' ) ); |
| 201 | } |
| 202 | |
| 203 | $id = ! empty( $wp_user->ID ) ? $wp_user->ID : $wp_user; |
| 204 | if ( ! is_int( $id ) ) { |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | wp_clear_auth_cookie(); |
| 209 | wp_set_current_user( $id ); |
| 210 | wp_set_auth_cookie( $id ); |
| 211 | } |
| 212 | } |
| 213 |