class-admin.php
6.16 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* Adds the Account Tabs sumbenu to the Ultimate Member admin menu.
*
* @package um_ext\um_account_tabs\admin
*/
namespace um_ext\um_account_tabs\admin;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'um_ext\um_account_tabs\admin\Admin' ) ) {
/**
* Class Admin.
*
* @package um_ext\um_account_tabs\admin
*/
class Admin {
/**
* Admin constructor.
*/
public function __construct() {
add_action( 'admin_menu', array( &$this, 'create_admin_submenu' ), 1001 );
add_action( 'load-post.php', array( &$this, 'init_metaboxes' ), 9 );
add_action( 'load-post-new.php', array( &$this, 'init_metaboxes' ), 9 );
add_filter( 'um_is_ultimatememeber_admin_screen', array( &$this, 'is_um_screen' ), 10, 1 );
add_filter( 'wp_insert_post_data', array( &$this, 'filter_post_data' ), 10, 4 );
}
/**
* Add submenu for Account Tabs.
*/
public function create_admin_submenu() {
add_submenu_page(
'ultimatemember',
__( 'Account Tabs', 'um-account-tabs' ),
__( 'Account Tabs', 'um-account-tabs' ),
'manage_options',
'edit.php?post_type=um_account_tabs'
);
}
/**
* Enqueue admin scripts.
*
* @global object $current_screen
*/
public function enqueue() {
global $current_screen;
if ( isset( $current_screen ) && 'um_account_tabs' === $current_screen->id ) {
wp_enqueue_script(
'um-account-tabs-admin',
um_account_tabs_url . '/assets/js/um-account-tabs-admin.js',
array( 'jquery' ),
um_account_tabs_version,
true
);
}
}
/**
* Validates the account tab name and slug to be not empty.
*
* @param array $data An array of slashed, sanitized, and processed post data.
* @param array $postarr An array of sanitized (and slashed) but otherwise unmodified post data.
* @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data.
* @param bool $update Whether this is an existing post being updated.
* @return array
*/
public function filter_post_data( $data, $postarr, $unsanitized_postarr, $update ) {
if ( isset( $data['post_type'] ) && 'um_account_tabs' === $data['post_type'] && isset( $unsanitized_postarr['post_status'] ) && 'auto-draft' !== $unsanitized_postarr['post_status'] ) {
if ( empty( $data['post_title'] ) ) {
$data['post_title'] = 'Account Tab';
}
if ( empty( $data['post_name'] ) ) {
$tab_id = empty( $unsanitized_postarr['ID'] ) ? time() : $unsanitized_postarr['ID'];
$data['post_name'] = 'account-tab-' . $tab_id;
}
}
return $data;
}
/**
* Extends UM admin pages for enqueue scripts.
*
* @param bool $is_um Whether this screen is a part of the Ultimate Member.
*
* @return bool
*/
public function is_um_screen( $is_um ) {
global $current_screen;
if ( ! empty( $current_screen ) && strstr( $current_screen->id, 'um_account_tabs' ) ) {
$is_um = true;
}
return $is_um;
}
/**
* Add metaboxes to Add/Edit Account Tab screen.
*/
public function init_metaboxes() {
global $current_screen;
if ( isset( $current_screen ) && 'um_account_tabs' === $current_screen->id ) {
add_action( 'admin_enqueue_scripts', array( &$this, 'enqueue' ) );
add_action( 'add_meta_boxes', array( &$this, 'add_metaboxes' ), 1 );
add_action( 'save_post_um_account_tabs', array( &$this, 'save_metaboxes_data' ), 10, 3 );
}
}
/**
* Add metaboxes.
*/
public function add_metaboxes() {
// don't show metaboxes for translations.
if ( UM()->external_integrations()->is_wpml_active() ) {
global $post, $sitepress;
$tab_id = $sitepress->get_object_id( $post->ID, 'um_account_tabs', true, $sitepress->get_default_language() );
if ( $tab_id && $tab_id !== $post->ID ) {
return;
}
}
add_meta_box(
'um-admin-custom-account-tab/um-form{' . um_account_tabs_path . '}',
__( 'Pre-defined content', 'um-account-tabs' ),
array( UM()->metabox(), 'load_metabox_custom' ),
'um_account_tabs',
'normal',
'default'
);
add_meta_box(
'um-admin-custom-account-tab/access{' . um_account_tabs_path . '}',
__( 'Display Settings', 'um-account-tabs' ),
array( UM()->metabox(), 'load_metabox_custom' ),
'um_account_tabs',
'side',
'default'
);
add_meta_box(
'um-admin-custom-account-tab/icon{' . um_account_tabs_path . '}',
__( 'Customize this tab', 'um-account-tabs' ),
array( UM()->metabox(), 'load_metabox_custom' ),
'um_account_tabs',
'side',
'default'
);
}
/**
* Save settings in metaboxes.
*
* @param int $post_id Post ID.
* @param \WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated.
*/
public function save_metaboxes_data( $post_id, $post, $update ) {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( empty( $_POST ) ) {
return;
}
check_admin_referer( 'update-post_' . $post_id );
if ( UM()->external_integrations()->is_wpml_active() ) {
global $sitepress;
$tab_id = $sitepress->get_object_id( $post_id, 'um_account_tabs', true, $sitepress->get_default_language() );
if ( $tab_id && $tab_id !== $post_id ) {
return;
}
}
if ( empty( $_POST['um_account_tab'] ) ) {
return;
}
$input = map_deep( wp_unslash( $_POST['um_account_tab'] ), 'sanitize_text_field' );
$form = '';
if ( isset( $input['_um_form'] ) ) {
$form = absint( $input['_um_form'] );
}
update_post_meta( $post_id, '_um_form', $form );
$roles = array();
if ( isset( $input['_can_have_this_tab_roles'] ) && is_array( $input['_can_have_this_tab_roles'] ) ) {
$roles = $input['_can_have_this_tab_roles'];
}
update_post_meta( $post_id, '_can_have_this_tab_roles', $roles );
$icon = '';
if ( isset( $input['_icon'] ) ) {
$icon = sanitize_key( $input['_icon'] );
}
update_post_meta( $post_id, '_icon', $icon );
$icon = '';
if ( isset( $input['_position'] ) ) {
$icon = sanitize_key( $input['_position'] );
}
update_post_meta( $post_id, '_position', $icon );
}
}
}