class-account.php
4.57 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* Modifies the Account page.
*
* @package um_ext\um_account_tabs\core
*/
namespace um_ext\um_account_tabs\core;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Account.
*
* @package um_ext\um_account_tabs\core
*/
class Account {
/**
* Custom tabs.
*
* @var array
*/
protected $tabs = null;
/**
* Account constructor.
*/
public function __construct() {
add_filter( 'um_account_page_default_tabs_hook', array( &$this, 'add_tabs' ), 100, 1 );
}
/**
* Get all custom account tabs like posts array.
*
* @return array Posts.
*/
public function get_tabs() {
if ( ! is_array( $this->tabs ) ) {
$this->tabs = get_posts(
array(
'post_type' => 'um_account_tabs',
'posts_per_page' => -1,
)
);
}
return $this->tabs;
}
/**
* Add custom account tabs.
*
* @param array $tabs All account tabs.
*
* @return array
*/
public function add_tabs( $tabs ) {
foreach ( $this->get_tabs() as $tab ) {
if ( ! $this->can_have_tab( $tab->ID ) ) {
continue;
}
$icon = empty( $tab->_icon ) ? 'um-icon-plus' : $tab->_icon;
$position = absint( $tab->_position );
// Add tab to menu.
$tabs[ $position ][ $tab->post_name ] = array(
'icon' => $icon,
'title' => $tab->post_title,
'custom' => true,
'show_button' => ! empty( $tab->_um_form ),
'submit_title' => __( 'Update', 'um-account-tabs' ),
);
// Show tab content.
add_action(
'um_account_content_hook_' . $tab->post_name,
function( $args ) use ( $tab ) {
$output = '';
$userdata = get_userdata( get_current_user_id() );
$placeholders = array(
'{user_id}' => get_current_user_id(),
'{first_name}' => $userdata->first_name,
'{last_name}' => $userdata->last_name,
'{user_email}' => $userdata->user_email,
'{display_name}' => $userdata->display_name,
'[ultimatemember form_id=' => '[',
);
$tab_content = str_replace( array_keys( $placeholders ), array_values( $placeholders ), $tab->post_content );
// Fix conflict that may appear if the tab contains Elementor template.
if ( class_exists( '\Elementor\Plugin' ) ) {
\Elementor\Plugin::instance()->frontend->remove_content_filter();
$output .= apply_filters( 'the_content', $tab_content );
\Elementor\Plugin::instance()->frontend->add_content_filter();
} else {
$output .= apply_filters( 'the_content', $tab_content );
}
$output .= $this->um_custom_tab_form( $tab->ID, $tab->_um_form );
return $output;
}
);
}
return $tabs;
}
/**
* Check if user has the current tab by role.
*
* @param string $tab_id Tab key.
*
* @return bool
*/
public function can_have_tab( $tab_id ) {
$can_have = get_post_meta( $tab_id, '_can_have_this_tab_roles', true );
if ( empty( $can_have ) ) {
return true;
}
$current_user_roles = UM()->roles()->get_all_user_roles( get_current_user_id() );
if ( ! is_array( $current_user_roles ) ) {
$current_user_roles = array();
}
if ( array_intersect( $current_user_roles, $can_have ) ) {
return true;
}
return false;
}
/**
* Generate content for custom tabs.
*
* @param string $tab_id Tab ID.
* @param int $form_id Form ID.
*
* @return string
*/
public function um_custom_tab_form( $tab_id, $form_id = 0 ) {
if ( empty( $form_id ) ) {
return '';
}
$user_id = get_current_user_id();
// save profile settings.
$set_id = UM()->fields()->set_id;
$set_mode = UM()->fields()->set_mode;
$editing = UM()->fields()->editing;
$viewing = UM()->fields()->viewing;
// set profile settings.
UM()->fields()->set_id = $form_id;
UM()->fields()->set_mode = get_post_meta( $form_id, '_um_mode', true );
UM()->fields()->editing = true;
UM()->fields()->viewing = false;
$args = array(
'form_id' => $form_id,
);
ob_start();
do_action( 'um_before_profile_fields', $args );
do_action( 'um_main_profile_fields', $args );
do_action( 'um_after_form_fields', $args );
?>
<input type="hidden" name="is_signup" value="1">
<input type="hidden" name="profile_nonce" value="<?php echo esc_attr( wp_create_nonce( 'um-profile-nonce' . $user_id ) ); ?>">
<input type="hidden" name="user_id" value="<?php echo esc_attr( $user_id ); ?>">
<?php
$contents = ob_get_clean();
// restore default account settings.
UM()->fields()->set_id = $set_id;
UM()->fields()->set_mode = $set_mode;
UM()->fields()->editing = $editing;
UM()->fields()->viewing = $viewing;
return $contents;
}
}