-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
182 lines (145 loc) · 5.08 KB
/
plugin.php
File metadata and controls
182 lines (145 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* Plugin Name: UserAccount
* Plugin URI: https://github.com/uptimizt/user-account
* Description: personal account on the site by WordPress. shortcode [user-account]
* Author: WPCraft
* Author URI: https://wpcraft.ru/
* Developer: WPCraft
* Developer URI: https://wpcraft.ru/
* Text Domain: useraccount
* Domain Path: /languages
* PHP requires at least: 5.6
* WP requires at least: 5.0
* Tested up to: 5.6
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Version: 0.1
*/
namespace UserAccount;
class Core {
public static function init(){
add_shortcode('user-account', [__CLASS__, 'render_shortcode']);
add_action('user_account_navigation', [__CLASS__, 'render_nav']);
add_action('user_account_content', [__CLASS__, 'account_content']);
}
public static function render_nav(){
do_action( 'user_before_account_navigation' );
?>
<nav class="user-account-navigation">
<ul>
<?php foreach ( self::get_account_menu_items() as $endpoint => $label ) : ?>
<li class="<?php echo self::get_account_menu_item_classes( $endpoint ); ?>">
<a href="<?php echo esc_url( self::get_account_endpoint_url( $endpoint ) ); ?>"><?php echo esc_html( $label ); ?></a>
</li>
<?php endforeach; ?>
</ul>
</nav>
<?php
do_action( 'user_after_account_navigation' );
}
/**
* Get account endpoint URL.
*
* @param string $endpoint Endpoint.
* @return string
*/
function get_account_endpoint_url( $endpoint ) {
if ( 'dashboard' === $endpoint ) {
return self::get_account_page_permalink();
}
if ( 'customer-logout' === $endpoint ) {
return wp_logout_url();
}
return self::get_account_page_permalink() . $endpoint;
}
/**
* My Account content output.
*/
function account_content() {
global $wp;
if ( ! empty( $wp->query_vars ) ) {
foreach ( $wp->query_vars as $key => $value ) {
// Ignore pagename param.
if ( 'pagename' === $key ) {
continue;
}
if ( has_action( 'user_account_' . $key . '_endpoint' ) ) {
do_action( 'user_account_' . $key . '_endpoint', $value );
return;
}
}
}
// No endpoint found? Default to dashboard.
echo 'dasboard content';
}
public static function get_account_page_permalink(){
//todo - make
return 'https://ya.ru';
}
public static function render_shortcode(){
do_action( 'user_account_navigation' ); ?>
<div class="user-account-content-wrapper">
<?php
/**
* My Account content.
*
* @since 2.6.0
*/
do_action( 'user_account_content' );
?>
</div>
<?php
}
/**
* Get account menu item classes.
*
* @param string $endpoint Endpoint.
* @return string
*/
function get_account_menu_item_classes( $endpoint ) {
global $wp;
$classes = array(
'woocommerce-MyAccount-navigation-link',
'woocommerce-MyAccount-navigation-link--' . $endpoint,
);
// Set current item class.
$current = isset( $wp->query_vars[ $endpoint ] );
if ( 'dashboard' === $endpoint && ( isset( $wp->query_vars['page'] ) || empty( $wp->query_vars ) ) ) {
$current = true; // Dashboard is not an endpoint, so needs a custom check.
} elseif ( 'orders' === $endpoint && isset( $wp->query_vars['view-order'] ) ) {
$current = true; // When looking at individual order, highlight Orders list item (to signify where in the menu the user currently is).
} elseif ( 'payment-methods' === $endpoint && isset( $wp->query_vars['add-payment-method'] ) ) {
$current = true;
}
if ( $current ) {
$classes[] = 'is-active';
}
$classes = apply_filters( 'woocommerce_account_menu_item_classes', $classes, $endpoint );
return implode( ' ', array_map( 'sanitize_html_class', $classes ) );
}
/**
* Get My Account menu items.
*
* @return array
*/
public static function get_account_menu_items() {
$endpoints = array(
'edit-account' => get_option( 'user_myaccount_edit_account_endpoint', 'edit-account' ),
'customer-logout' => get_option( 'user_logout_endpoint', 'customer-logout' ),
);
$items = array(
'dashboard' => __( 'Dashboard', 'useraccount' ),
'edit-account' => __( 'Account details', 'useraccount' ),
'customer-logout' => __( 'Logout', 'useraccount' ),
);
// Remove missing endpoints.
foreach ( $endpoints as $endpoint_id => $endpoint ) {
if ( empty( $endpoint ) ) {
unset( $items[ $endpoint_id ] );
}
}
return apply_filters( 'user_account_menu_items', $items, $endpoints );
}
}
Core::init();