class-setup.php
9.51 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
namespace um\core;
use WP_User_Query;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'um\core\Setup' ) ) {
/**
* Class Setup
*
* @package um\core
*/
class Setup {
/**
* Run setup.
*/
public function run_setup() {
$this->create_db();
$this->install_basics();
$this->install_default_forms();
$this->set_default_settings();
$this->set_default_role_meta();
$this->set_default_user_status();
}
/**
* Create custom DB tables.
*/
public function create_db() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE {$wpdb->prefix}um_metadata (
umeta_id bigint(20) unsigned NOT NULL auto_increment,
user_id bigint(20) unsigned NOT NULL default '0',
um_key varchar(255) default NULL,
um_value longtext default NULL,
PRIMARY KEY (umeta_id),
KEY user_id_indx (user_id),
KEY meta_key_indx (um_key),
KEY meta_value_indx (um_value(191))
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}
/**
* Basics.
*/
public function install_basics() {
if ( ! get_option( '__ultimatemember_sitekey' ) ) {
update_option( '__ultimatemember_sitekey', str_replace( array( 'http://', 'https://' ), '', sanitize_user( get_bloginfo( 'url' ) ) ) . '-' . wp_generate_password( 20, false ) );
}
}
/**
* Default Forms.
*/
public function install_default_forms() {
if ( current_user_can( 'manage_options' ) && ! get_option( 'um_is_installed' ) ) {
$options = get_option( 'um_options', array() );
update_option( 'um_is_installed', 1 );
//Install default options
foreach ( UM()->config()->settings_defaults as $key => $value ) {
$options[ $key ] = $value;
}
// Install Core Forms.
foreach ( UM()->config()->core_forms as $id ) {
// If page does not exist - create it.
$page_exists = UM()->query()->find_post_id( 'um_form', '_um_core', $id );
if ( ! $page_exists ) {
if ( 'register' === $id ) {
$title = 'Default Registration';
} elseif ( 'login' === $id ) {
$title = 'Default Login';
} else {
$title = 'Default Profile';
}
$form = array(
'post_type' => 'um_form',
'post_title' => $title,
'post_status' => 'publish',
'post_author' => get_current_user_id(),
);
$form_id = wp_insert_post( $form );
foreach ( UM()->config()->core_form_meta[ $id ] as $key => $value ) {
if ( '_um_custom_fields' === $key ) {
$array = maybe_unserialize( $value );
update_post_meta( $form_id, $key, $array );
} else {
update_post_meta( $form_id, $key, $value );
}
}
$core_forms[ $id ] = $form_id;
}
/** DONE **/
}
if ( isset( $core_forms ) ) {
update_option( 'um_core_forms', $core_forms );
}
// Install Core Directories.
foreach ( UM()->config()->core_directories as $id ) {
// If page does not exist - create it.
$page_exists = UM()->query()->find_post_id( 'um_directory', '_um_core', $id );
if ( ! $page_exists ) {
$title = 'Members';
$form = array(
'post_type' => 'um_directory',
'post_title' => $title,
'post_status' => 'publish',
'post_author' => get_current_user_id(),
);
$form_id = wp_insert_post( $form );
foreach ( UM()->config()->core_directory_meta[ $id ] as $key => $value ) {
if ( '_um_custom_fields' === $key ) {
$array = maybe_unserialize( $value );
update_post_meta( $form_id, $key, $array );
} else {
update_post_meta( $form_id, $key, $value );
}
}
$core_directories[ $id ] = $form_id;
}
/** DONE **/
}
if ( isset( $core_directories ) ) {
update_option( 'um_core_directories', $core_directories );
}
update_option( 'um_options', $options );
}
}
/**
* Install Pre-defined pages with shortcodes.
*/
public function install_default_pages() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$core_forms = get_option( 'um_core_forms', array() );
$core_directories = get_option( 'um_core_directories', array() );
$setup_shortcodes = array_merge( $core_forms, $core_directories );
// Install Core Pages.
$core_pages = array();
foreach ( UM()->config()->core_pages as $slug => $array ) {
$page_exists = UM()->query()->find_post_id( 'page', '_um_core', $slug );
if ( $page_exists ) {
$core_pages[ $slug ] = $page_exists;
continue;
}
// If page does not exist - create it.
$content = '';
if ( 'logout' === $slug ) {
$content = '';
} elseif ( 'account' === $slug ) {
$content = '[ultimatemember_account]';
} elseif ( 'password-reset' === $slug ) {
$content = '[ultimatemember_password]';
} elseif ( 'user' === $slug ) {
$content = '[ultimatemember form_id="' . $setup_shortcodes['profile'] . '"]';
} elseif ( ! empty( $setup_shortcodes[ $slug ] ) ) {
$content = '[ultimatemember form_id="' . $setup_shortcodes[ $slug ] . '"]';
}
/** This filter is documented in includes/core/class-setup.php */
$content = apply_filters( 'um_setup_predefined_page_content', $content, $slug );
$user_page = array(
'post_title' => $array['title'],
'post_content' => $content,
'post_name' => $slug,
'post_type' => 'page',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'comment_status' => 'closed',
);
$post_id = wp_insert_post( $user_page );
update_post_meta( $post_id, '_um_core', $slug );
$core_pages[ $slug ] = $post_id;
}
$options = get_option( 'um_options', array() );
foreach ( $core_pages as $slug => $page_id ) {
$key = UM()->options()->get_core_page_id( $slug );
$options[ $key ] = $page_id;
}
update_option( 'um_options', $options );
// reset rewrite rules after first install of core pages
UM()->rewrite()->reset_rules();
}
public function predefined_page( $slug, $with_rewrite = true ) {
$page_exists = UM()->query()->find_post_id( 'page', '_um_core', $slug );
if ( $page_exists ) {
return;
}
$predefined_pages = UM()->config()->get( 'predefined_pages' );
if ( empty( $predefined_pages ) || ! array_key_exists( $slug, $predefined_pages ) ) {
return;
}
$data = $predefined_pages[ $slug ];
if ( empty( $data['title'] ) ) {
return;
}
$content = ! empty( $data['content'] ) ? $data['content'] : '';
/**
* Filters Ultimate Member predefined pages content when set up the predefined page.
*
* @param {string} $content Predefined page content.
* @param {string} $slug Predefined page slug (key).
*
* @return {string} Predefined page content.
*
* @since 2.1.0
* @hook um_setup_predefined_page_content
*
* @example <caption>Set Ultimate Member predefined pages content with key = 'my_page_key'.</caption>
* function my_um_setup_predefined_page_content( $content, $slug ) {
* // your code here
* if ( 'my_page_key' === $slug ) {
* $content = __( 'My Page content', 'my-translate-key' );
* }
* return $pages;
* }
* add_filter( 'um_setup_predefined_page_content', 'my_um_setup_predefined_page_content' );
*/
$content = apply_filters( 'um_setup_predefined_page_content', $content, $slug );
$user_page = array(
'post_title' => $data['title'],
'post_content' => $content,
'post_name' => $slug,
'post_type' => 'page',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'comment_status' => 'closed',
);
$post_id = wp_insert_post( $user_page );
if ( empty( $post_id ) || is_wp_error( $post_id ) ) {
return;
}
update_post_meta( $post_id, '_um_core', $slug );
UM()->options()->update( UM()->options()->get_predefined_page_option_key( $slug ), $post_id );
if ( $with_rewrite ) {
// Reset rewrite rules after page creation and option upgrade.
UM()->rewrite()->reset_rules();
}
}
/**
* Set default UM settings.
*/
public function set_default_settings() {
$options = get_option( 'um_options', array() );
foreach ( UM()->config()->settings_defaults as $key => $value ) {
//set new options to default
if ( ! isset( $options[ $key ] ) ) {
$options[ $key ] = $value;
}
}
update_option( 'um_options', $options );
}
/**
* Set UM roles meta to Default WP roles.
*/
public function set_default_role_meta() {
foreach ( UM()->config()->default_roles_metadata as $role => $meta ) {
add_option( "um_role_{$role}_meta", $meta );
}
}
/**
* Set accounts without account_status meta to 'approved' status.
*
* @since 2.4.2
*/
public function set_default_user_status() {
$result = get_transient( 'um_count_users_unassigned' );
if ( false === $result ) {
$args = array(
'fields' => 'ids',
'number' => 0,
'meta_query' => array(
array(
'key' => 'account_status',
'compare' => 'NOT EXISTS',
),
),
'um_custom_user_query' => true,
);
$users = new WP_User_Query( $args );
if ( empty( $users ) || is_wp_error( $users ) ) {
$result = array();
} else {
$result = $users->get_results();
}
set_transient( 'um_count_users_unassigned', $result, DAY_IN_SECONDS );
}
if ( empty( $result ) ) {
return;
}
foreach ( $result as $user_id ) {
update_user_meta( $user_id, 'account_status', 'approved' );
}
}
}
}