PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0.3.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0.3.2
6.2.2 6.2.1 6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 All 155 releases
mainwp / pages / page-mainwp-user.php

page-mainwp-user.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.0.3.2, at pages/page-mainwp-user.php

2,356 lines 85.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Users Page
4 *
5 * This page is used to Manage Users on child sites.
6 *
7 * @package MainWP/User
8 */
9
10 namespace MainWP\Dashboard;
11
12 /**
13 * Class MainWP_User
14 *
15 * @uses page-mainwp-bulk-add::MainWP_Bulk_Add()
16 */
17 class MainWP_User {
18
19 /**
20 * Get Class Name
21 *
22 * @return string __CLASS__
23 */
24 public static function get_class_name() {
25 return __CLASS__;
26 }
27
28 /**
29 * Public static varable to hold Subpages information.
30 *
31 * @var array $subPages
32 */
33 public static $subPages;
34
35 /**
36 * Method init()
37 *
38 * Initiate hooks for the users page.
39 */
40 public static function init() {
41 /**
42 * This hook allows you to render the User page header via the 'mainwp-pageheader-user' action.
43 *
44 * @link http://codex.mainwp.com/#mainwp-pageheader-user
45 *
46 * This hook is normally used in the same context of 'mainwp-getsubpages-user'
47 * @link http://codex.mainwp.com/#mainwp-getsubpages-user
48 *
49 * @see \MainWP_User::render_header
50 */
51 add_action( 'mainwp-pageheader-user', array( self::get_class_name(), 'render_header' ) );
52
53 /**
54 * This hook allows you to render the User page footer via the 'mainwp-pagefooter-user' action.
55 *
56 * @link http://codex.mainwp.com/#mainwp-pagefooter-user
57 *
58 * This hook is normally used in the same context of 'mainwp-getsubpages-user'
59 * @link http://codex.mainwp.com/#mainwp-getsubpages-user
60 *
61 * @see \MainWP_User::render_footer
62 */
63 add_action( 'mainwp-pagefooter-user', array( self::get_class_name(), 'render_footer' ) );
64
65 add_action( 'mainwp_help_sidebar_content', array( self::get_class_name(), 'mainwp_help_content' ) );
66 }
67
68 /**
69 * Method init_menu()
70 *
71 * Initiate menu.
72 *
73 * @uses \MainWP\Dashboard\MainWP_Menu::is_disable_menu_item()
74 */
75 public static function init_menu() {
76 $_page = add_submenu_page(
77 'mainwp_tab',
78 __( 'Users', 'mainwp' ),
79 '<span id="mainwp-Users">' . esc_html__( 'Users', 'mainwp' ) . '</span>',
80 'read',
81 'UserBulkManage',
82 array(
83 self::get_class_name(),
84 'render',
85 )
86 );
87
88 add_action( 'load-' . $_page, array( self::get_class_name(), 'on_load_page' ) );
89
90 add_submenu_page(
91 'mainwp_tab',
92 __( 'Users', 'mainwp' ),
93 '<div class="mainwp-hidden">' . esc_html__( 'Add New', 'mainwp' ) . '</div>',
94 'read',
95 'UserBulkAdd',
96 array(
97 self::get_class_name(),
98 'render_bulk_add',
99 )
100 );
101
102 add_submenu_page(
103 'mainwp_tab',
104 __( 'Import Users', 'mainwp' ),
105 '<div class="mainwp-hidden">' . esc_html__( 'Import Users', 'mainwp' ) . '</div>',
106 'read',
107 'BulkImportUsers',
108 array(
109 self::get_class_name(),
110 'render_bulk_import_users',
111 )
112 );
113
114 /**
115 * This hook allows you to add extra sub pages to the User page via the 'mainwp-getsubpages-user' filter.
116 *
117 * @link http://codex.mainwp.com/#mainwp-getsubpages-user
118 */
119 $sub_pages = apply_filters_deprecated( 'mainwp-getsubpages-user', array( array() ), '4.0.7.2', 'mainwp_getsubpages_user' ); // @deprecated Use 'mainwp_getsubpages_user' instead.
120 self::$subPages = apply_filters( 'mainwp_getsubpages_user', $sub_pages );
121
122 if ( isset( self::$subPages ) && is_array( self::$subPages ) ) {
123 foreach ( self::$subPages as $subPage ) {
124 if ( MainWP_Menu::is_disable_menu_item( 3, 'UserBulk' . $subPage['slug'] ) ) {
125 continue;
126 }
127 add_submenu_page( 'mainwp_tab', $subPage['title'], '<div class="mainwp-hidden">' . esc_html( $subPage['title'] ) . '</div>', 'read', 'UserBulk' . $subPage['slug'], $subPage['callback'] );
128 }
129 }
130
131 self::init_left_menu( self::$subPages );
132 }
133
134 /**
135 * Initiates sub pages menu.
136 *
137 * @uses \MainWP\Dashboard\MainWP_Menu::is_disable_menu_item()
138 */
139 public static function init_subpages_menu() {
140 ?>
141 <div id="menu-mainwp-Users" class="mainwp-submenu-wrapper">
142 <div class="wp-submenu sub-open" style="">
143 <div class="mainwp_boxout">
144 <div class="mainwp_boxoutin"></div>
145 <?php if ( mainwp_current_user_have_right( 'dashboard', 'manage_users' ) ) { ?>
146 <a href="<?php echo esc_url( admin_url( 'admin.php?page=UserBulkManage' ) ); ?>" class="mainwp-submenu"><?php esc_html_e( 'Manage Users', 'mainwp' ); ?></a>
147 <?php } ?>
148 <?php if ( ! MainWP_Menu::is_disable_menu_item( 3, 'UserBulkAdd' ) ) { ?>
149 <a href="<?php echo esc_url( admin_url( 'admin.php?page=UserBulkAdd' ) ); ?>" class="mainwp-submenu"><?php esc_html_e( 'Add New', 'mainwp' ); ?></a>
150 <?php } ?>
151 <?php if ( ! MainWP_Menu::is_disable_menu_item( 3, 'BulkImportUsers' ) ) { ?>
152 <a href="<?php echo esc_url( admin_url( 'admin.php?page=BulkImportUsers' ) ); ?>" class="mainwp-submenu"><?php esc_html_e( 'Import Users', 'mainwp' ); ?></a>
153 <?php } ?>
154 <?php if ( ! MainWP_Menu::is_disable_menu_item( 3, 'UpdateAdminPasswords' ) ) { ?>
155 <a href="<?php echo esc_url( admin_url( 'admin.php?page=UpdateAdminPasswords' ) ); ?>" class="mainwp-submenu"><?php esc_html_e( 'Admin Passwords', 'mainwp' ); ?></a>
156 <?php } ?>
157 <?php
158 if ( isset( self::$subPages ) && is_array( self::$subPages ) ) {
159 foreach ( self::$subPages as $subPage ) {
160 if ( MainWP_Menu::is_disable_menu_item( 3, 'UserBulk' . $subPage['slug'] ) ) {
161 continue;
162 }
163 ?>
164 <a href="<?php echo esc_url( admin_url( 'admin.php?page=UserBulk' . $subPage['slug'] ) ); ?>" class="mainwp-submenu"><?php echo esc_html( $subPage['title'] ); ?></a>
165 <?php
166 }
167 }
168 ?>
169 </div>
170 </div>
171 </div>
172 <?php
173 }
174
175 /**
176 * Method get_manage_columns()
177 *
178 * Get columns to display.
179 *
180 * @return array $colums Array of columns to display on the page.
181 *
182 * @uses \MainWP\Dashboard\MainWP_Utility::enabled_wp_seo()
183 */
184 public static function get_manage_columns() {
185 $colums = array(
186 'name' => esc_html__( 'Name', 'mainwp' ),
187 'username' => esc_html__( 'Username', 'mainwp' ),
188 'email' => esc_html__( 'E-mail', 'mainwp' ),
189 'role' => esc_html__( 'Role', 'mainwp' ),
190 'posts' => esc_html__( 'Posts', 'mainwp' ),
191 'website' => esc_html__( 'Website', 'mainwp' ),
192 );
193 return $colums;
194 }
195
196 /**
197 * Method on_load_page()
198 *
199 * Used during init_menu() to get the class names of,
200 * admin_head and get_hidden_columns.
201 *
202 * @return void
203 */
204 public static function on_load_page() {
205 add_action( 'mainwp_screen_options_modal_bottom', array( self::get_class_name(), 'hook_screen_options_modal_bottom' ), 10, 2 );
206 }
207
208 /**
209 * Method hook_screen_options_modal_bottom()
210 *
211 * Render screen options modal bottom.
212 */
213 public static function hook_screen_options_modal_bottom() {
214 $page = isset( $_GET['page'] ) ? wp_unslash( $_GET['page'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
215 if ( 'UserBulkManage' === $page ) {
216
217 $show_columns = get_user_option( 'mainwp_manageusers_show_columns' );
218
219 if ( ! is_array( $show_columns ) ) {
220 $show_columns = array();
221 }
222
223 $cols = self::get_manage_columns();
224 MainWP_UI::render_showhide_columns_settings( $cols, $show_columns, 'user' );
225 }
226 }
227
228 /**
229 * Initiates Users menu.
230 *
231 * @param array $subPages Sub pages array.
232 *
233 * @uses \MainWP\Dashboard\MainWP_Menu::add_left_menu()
234 * @uses \MainWP\Dashboard\MainWP_Menu::init_subpages_left_menu()
235 * @uses \MainWP\Dashboard\MainWP_Menu::is_disable_menu_item()
236 */
237 public static function init_left_menu( $subPages = array() ) {
238 MainWP_Menu::add_left_menu(
239 array(
240 'title' => esc_html__( 'Users', 'mainwp' ),
241 'parent_key' => 'managesites',
242 'slug' => 'UserBulkManage',
243 'href' => 'admin.php?page=UserBulkManage',
244 'icon' => '<i class="user icon"></i>',
245 'desc' => 'Manage users on your child sites',
246 'leftsub_order' => 7,
247 ),
248 1
249 );
250
251 $init_sub_subleftmenu = array(
252 array(
253 'title' => esc_html__( 'Manage Users', 'mainwp' ),
254 'parent_key' => 'UserBulkManage',
255 'href' => 'admin.php?page=UserBulkManage',
256 'slug' => 'UserBulkManage',
257 'right' => 'manage_users',
258 ),
259 array(
260 'title' => esc_html__( 'Add New', 'mainwp' ),
261 'parent_key' => 'UserBulkManage',
262 'href' => 'admin.php?page=UserBulkAdd',
263 'slug' => 'UserBulkAdd',
264 'right' => '',
265 ),
266 array(
267 'title' => esc_html__( 'Import Users', 'mainwp' ),
268 'parent_key' => 'UserBulkManage',
269 'href' => 'admin.php?page=BulkImportUsers',
270 'slug' => 'BulkImportUsers',
271 'right' => '',
272 ),
273 array(
274 'title' => esc_html__( 'Admin Passwords', 'mainwp' ),
275 'parent_key' => 'UserBulkManage',
276 'href' => 'admin.php?page=UpdateAdminPasswords',
277 'slug' => 'UpdateAdminPasswords',
278 'right' => '',
279 ),
280 );
281
282 MainWP_Menu::init_subpages_left_menu( $subPages, $init_sub_subleftmenu, 'UserBulkManage', 'UserBulk' );
283
284 foreach ( $init_sub_subleftmenu as $item ) {
285 if ( MainWP_Menu::is_disable_menu_item( 3, $item['slug'] ) ) {
286 continue;
287 }
288 MainWP_Menu::add_left_menu( $item, 2 );
289 }
290 }
291
292 /**
293 * Method render_header()
294 *
295 * Render Users page header.
296 *
297 * @param string $shownPage The page slug shown at this moment.
298 *
299 * @uses \MainWP\Dashboard\MainWP_Menu::is_disable_menu_item()
300 * @uses \MainWP\Dashboard\MainWP_UI::render_top_header()
301 * @uses \MainWP\Dashboard\MainWP_UI::render_page_navigation()
302 */
303 public static function render_header( $shownPage = '' ) {
304 $params = array(
305 'title' => esc_html__( 'Users', 'mainwp' ),
306 );
307 MainWP_UI::render_top_header( $params );
308
309 $renderItems = array();
310
311 if ( mainwp_current_user_have_right( 'dashboard', 'manage_users' ) ) {
312 $renderItems[] = array(
313 'title' => esc_html__( 'Manage Users', 'mainwp' ),
314 'href' => 'admin.php?page=UserBulkManage',
315 'active' => ( '' === $shownPage ) ? true : false,
316 );
317 }
318
319 if ( ! MainWP_Menu::is_disable_menu_item( 3, 'UserBulkAdd' ) ) {
320 $renderItems[] = array(
321 'title' => esc_html__( 'Add New', 'mainwp' ),
322 'href' => 'admin.php?page=UserBulkAdd',
323 'active' => ( 'Add' === $shownPage ) ? true : false,
324 );
325 }
326
327 if ( ! MainWP_Menu::is_disable_menu_item( 3, 'BulkImportUsers' ) ) {
328 $renderItems[] = array(
329 'title' => esc_html__( 'Import Users', 'mainwp' ),
330 'href' => 'admin.php?page=BulkImportUsers',
331 'active' => ( 'Import' === $shownPage ) ? true : false,
332 );
333 }
334
335 if ( ! MainWP_Menu::is_disable_menu_item( 3, 'UpdateAdminPasswords' ) ) {
336 $renderItems[] = array(
337 'title' => esc_html__( 'Admin Passwords', 'mainwp' ),
338 'href' => 'admin.php?page=UpdateAdminPasswords',
339 'active' => ( 'UpdateAdminPasswords' === $shownPage ) ? true : false,
340 );
341 }
342
343 if ( isset( self::$subPages ) && is_array( self::$subPages ) ) {
344 foreach ( self::$subPages as $subPage ) {
345 if ( MainWP_Menu::is_disable_menu_item( 3, 'UserBulk' . $subPage['slug'] ) ) {
346 continue;
347 }
348
349 $item = array();
350 $item['title'] = $subPage['title'];
351 $item['href'] = 'admin.php?page=UserBulk' . $subPage['slug'];
352 $item['active'] = ( $subPage['slug'] === $shownPage ) ? true : false;
353 $renderItems[] = $item;
354 }
355 }
356
357 MainWP_UI::render_page_navigation( $renderItems );
358 }
359
360 /**
361 * Method render_footer()
362 *
363 * Render Users page footer. Closes the page container.
364 */
365 public static function render_footer() {
366 echo '</div>';
367 }
368
369 /**
370 * Renders manage users dashboard.
371 *
372 * @return void
373 *
374 * @uses \MainWP\Dashboard\MainWP_Cache::get_cached_context()
375 */
376 public static function render() {
377 if ( ! mainwp_current_user_have_right( 'dashboard', 'manage_users' ) ) {
378 mainwp_do_not_have_permissions( esc_html__( 'manage users', 'mainwp' ) );
379
380 return;
381 }
382
383 $cachedSearch = MainWP_Cache::get_cached_context( 'Users' );
384
385 $selected_sites = array();
386 $selected_groups = array();
387 $selected_clients = array();
388
389 if ( null !== $cachedSearch ) {
390 if ( is_array( $cachedSearch['sites'] ) ) {
391 $selected_sites = $cachedSearch['sites'];
392 } elseif ( is_array( $cachedSearch['groups'] ) ) {
393 $selected_groups = $cachedSearch['groups'];
394 } elseif ( is_array( $cachedSearch['clients'] ) ) {
395 $selected_clients = $cachedSearch['clients'];
396 }
397 }
398
399 self::render_header( '' );
400 ?>
401 <div id="mainwp-manage-users" class="ui alt segment">
402 <div class="mainwp-main-content">
403 <div class="mainwp-actions-bar ui mini form">
404 <div class="ui grid">
405 <div class="ui two column row">
406 <div class="column">
407 <select class="ui mini dropdown" id="mainwp-bulk-actions">
408 <option value=""><?php esc_html_e( 'Bulk Actions', 'mainwp' ); ?></option>
409 <option value="edit"><?php esc_html_e( 'Edit', 'mainwp' ); ?></option>
410 <option value="delete"><?php esc_html_e( 'Delete', 'mainwp' ); ?></option>
411 <?php
412 /**
413 * Action: mainwp_users_bulk_action
414 *
415 * Adds new Bulk Actions option under on Manage Users.
416 *
417 * Suggested HTML Markup:
418 * <option value="Your custom value">Your custom label</option>
419 *
420 * @since 4.1
421 */
422 do_action( 'mainwp_users_bulk_action' );
423 ?>
424 </select>
425 <button class="ui mini button" id="mainwp-do-users-bulk-actions"><?php esc_html_e( 'Apply', 'mainwp' ); ?></button>
426 <?php
427 /**
428 * Users actions bar (left)
429 *
430 * Fires at the left side of the actions bar on the Users screen, after the Bulk Actions menu.
431 *
432 * @since 4.0
433 */
434 do_action( 'mainwp_users_actions_bar_left' );
435 ?>
436 </div>
437 <div class="right aligned column">
438 <?php
439 /**
440 * Users actions bar (right)
441 *
442 * Fires at the right side of the actions bar on the Users screen.
443 *
444 * @since 4.0
445 */
446 do_action( 'mainwp_users_actions_bar_right' );
447 ?>
448 </div>
449 </div>
450 </div>
451 </div>
452 <div id="mainwp-loading-users-row" style="display: none;">
453 <div class="ui active inverted dimmer">
454 <div class="ui indeterminate large text loader"><?php esc_html_e( 'Loading Users...', 'mainwp' ); ?>
455 <span id="mainwp_users_loading_info" class="mainwp-grabbing-info-note"><br /><?php esc_html_e( 'Automatically refreshing to get up to date information.', 'mainwp' ); ?></span>
456 </div>
457 </div>
458 </div>
459 <div class="ui segment" id="mainwp_users_wrap_table">
460 <?php if ( MainWP_Utility::show_mainwp_message( 'notice', 'mainwp-manage-users-info-message' ) ) : ?>
461 <div class="ui info message">
462 <i class="close icon mainwp-notice-dismiss" notice-id="mainwp-manage-users-info-message"></i>
463 <?php printf( esc_html__( 'Manage existing users on your child sites. Here you can Delete, Edit or Change Role for existing users. For additional help, please check this %1$shelp documentation%2$s.', 'mainwp' ), '<a href="https://kb.mainwp.com/docs/manage-users/" target="_blank">', '</a>' ); ?>
464 </div>
465 <?php endif; ?>
466 <div class="ui message" id="mainwp-message-zone" style="display:none"></div>
467 <?php self::render_table( true ); ?>
468 </div>
469 <div id="mainwp-update-users-box" class="ui segment">
470 <?php self::render_update_users(); ?>
471 </div>
472 </div>
473 <div class="mainwp-side-content mainwp-no-padding">
474 <?php
475 /**
476 * Action: mainwp_manage_users_sidebar_top
477 *
478 * Fires on top of the sidebar on Manage Users page.
479 *
480 * @since 4.1
481 */
482 do_action( 'mainwp_manage_users_sidebar_top' );
483 ?>
484 <div class="mainwp-select-sites ui accordion mainwp-sidebar-accordion">
485 <?php
486 /**
487 * Action: mainwp_manage_users_before_select_sites
488 *
489 * Fires before the Select Sites section on Manage Users page.
490 *
491 * @since 4.1
492 */
493 do_action( 'mainwp_manage_users_before_select_sites' );
494 ?>
495 <div class="title active"><i class="dropdown icon"></i> <?php esc_html_e( 'Select Sites', 'mainwp' ); ?></div>
496 <div class="content active">
497 <?php
498 $sel_params = array(
499 'selected_sites' => $selected_sites,
500 'selected_groups' => $selected_groups,
501 'selected_clients' => $selected_clients,
502 'class' => 'mainwp_select_sites_box_left',
503 'show_client' => true,
504 );
505 MainWP_UI_Select_Sites::select_sites_box( $sel_params );
506 ?>
507 </div>
508 <?php
509 /**
510 * Action: mainwp_manage_users_after_select_sites
511 *
512 * Fires after the Select Sites section on Manage Users page.
513 *
514 * @since 4.1
515 */
516 do_action( 'mainwp_manage_users_after_select_sites' );
517 ?>
518 </div>
519
520 <?php
521 $user_roles = array(
522 'subscriber' => esc_html__( 'Subscriber', 'mainwp' ),
523 'administrator' => esc_html__( 'Administrator', 'mainwp' ),
524 'editor' => esc_html__( 'Editor', 'mainwp' ),
525 'author' => esc_html__( 'Author', 'mainwp' ),
526 'contributor' => esc_html__( 'Contributor', 'mainwp' ),
527 );
528
529 $user_roles = apply_filters_deprecated( 'mainwp-users-manage-roles', array( $user_roles ), '4.0.7.2', 'mainwp_users_manage_roles' ); // @deprecated Use 'mainwp_users_manage_roles' instead.
530 $user_roles = apply_filters( 'mainwp_users_manage_roles', $user_roles );
531
532 ?>
533
534 <div class="ui fitted divider"></div>
535 <div class="mainwp-search-options ui accordion mainwp-sidebar-accordion">
536 <div class="title active"><i class="dropdown icon"></i> <?php esc_html_e( 'Select Roles', 'mainwp' ); ?></div>
537 <div class="content active">
538 <?php
539 /**
540 * Action: mainwp_manage_users_before_search_options
541 *
542 * Fires before the Search Options section on Manage Users page.
543 *
544 * @since 4.1
545 */
546 do_action( 'mainwp_manage_users_before_search_options' );
547 ?>
548 <div class="ui mini form">
549 <div class="field" data-tooltip="<?php esc_attr_e( 'Select specific roles that you want to search for.', 'mainwp' ); ?>" data-inverted="" data-position="top right">
550 <select multiple="" class="ui fluid mini dropdown" id="mainwp_user_roles">
551 <option value=""><?php esc_html_e( 'Select wanted role(s)', 'mainwp' ); ?></option>
552 <?php
553 foreach ( $user_roles as $r => $n ) {
554 if ( empty( $r ) ) {
555 continue;
556 }
557 ?>
558 <option value="<?php echo esc_html( $r ); ?>"><?php echo esc_html( $n ); ?></option>
559 <?php
560 }
561 ?>
562 </select>
563 </div>
564 </div>
565 <?php
566 /**
567 * Action: mainwp_manage_users_after_search_options
568 *
569 * Fires after the Search Options section on Manage Users page.
570 *
571 * @since 4.1
572 */
573 do_action( 'mainwp_manage_users_after_search_options' );
574 ?>
575 </div>
576 </div>
577 <div class="ui fitted divider"></div>
578 <div class="mainwp-search-options ui accordion mainwp-sidebar-accordion">
579 <div class="title active"><i class="dropdown icon"></i> <?php esc_html_e( 'Search Options', 'mainwp' ); ?></div>
580 <div class="content active"><?php self::render_search_options(); ?></div>
581 </div>
582 <div class="ui fitted divider"></div>
583 <div class="mainwp-search-submit">
584 <?php
585 /**
586 * Action: mainwp_manage_users_before_submit_button
587 *
588 * Fires before the Submit Button on Manage Users page.
589 *
590 * @since 4.1
591 */
592 do_action( 'mainwp_manage_users_before_submit_button' );
593 ?>
594 <input type="button" name="mainwp_show_users" id="mainwp_show_users" class="ui green big fluid button" value="<?php esc_attr_e( 'Show Users', 'mainwp' ); ?>"/>
595 <?php
596 /**
597 * Action: mainwp_manage_users_after_submit_button
598 *
599 * Fires after the Submit Button on Manage Users page.
600 *
601 * @since 4.1
602 */
603 do_action( 'mainwp_manage_users_after_submit_button' );
604 ?>
605 </div>
606 <?php
607 /**
608 * Action: mainwp_manage_users_sidebar_bottom
609 *
610 * Fires at the bottom of the sidebar on Manage Users page.
611 *
612 * @since 4.1
613 */
614 do_action( 'mainwp_manage_users_sidebar_bottom' );
615 ?>
616 </div>
617 <div class="ui hidden clearing divider"></div>
618 </div>
619 <?php
620 self::render_footer( '' );
621 }
622
623 /**
624 * Method render_search_options()
625 *
626 * Render User page search.
627 *
628 * @uses \MainWP\Dashboard\MainWP_Cache::get_cached_context()
629 */
630 public static function render_search_options() {
631 $cachedSearch = MainWP_Cache::get_cached_context( 'Users' );
632 $statuses = isset( $cachedSearch['status'] ) ? $cachedSearch['status'] : array();
633 if ( $cachedSearch && isset( $cachedSearch['keyword'] ) ) {
634 $cachedSearch['keyword'] = trim( $cachedSearch['keyword'] );
635 }
636 ?>
637
638 <div class="ui mini form">
639 <div class="field" data-tooltip="<?php esc_attr_e( 'Enter specific username that you want to search for.', 'mainwp' ); ?>" data-inverted="" data-position="top right">
640 <div class="ui input fluid">
641 <input type="text" placeholder="<?php esc_attr_e( 'Username', 'mainwp' ); ?>" id="mainwp_search_users" class="text" value="<?php echo ( null !== $cachedSearch ) ? esc_attr( $cachedSearch['keyword'] ) : ''; ?>" />
642 </div>
643 </div>
644 </div>
645 <?php
646 if ( is_array( $statuses ) && 0 < count( $statuses ) ) {
647 $status = '';
648 foreach ( $statuses as $st ) {
649 $status .= "'" . esc_html( $st ) . "',";
650 }
651 $status = rtrim( $status, ',' );
652 ?>
653 <script type="text/javascript">
654 jQuery( document ).ready( function () {
655 jQuery( '#mainwp_user_roles' ).dropdown( 'set selected', [<?php echo $status; //phpcs:ignore -- safe output. ?>] );
656 } );
657 </script>
658 <?php
659 }
660 }
661
662 /**
663 * Renders Edit Users Modal window.
664 */
665 public static function render_update_users() {
666
667 $editable_roles = array(
668 'donotupdate' => esc_html__( 'Do not update', 'mainwp' ),
669 'administrator' => esc_html__( 'Administrator', 'mainwp' ),
670 'subscriber' => esc_html__( 'Subscriber', 'mainwp' ),
671 'contributor' => esc_html__( 'Contributor', 'mainwp' ),
672 'author' => esc_html__( 'Author', 'mainwp' ),
673 'editor' => esc_html__( 'Editor', 'mainwp' ),
674 );
675
676 $editable_roles = apply_filters_deprecated( 'mainwp-users-manage-roles', array( $editable_roles ), '4.0.7.2', 'mainwp_users_manage_roles' ); // @deprecated Use 'mainwp_users_manage_roles' instead.
677 $editable_roles = apply_filters( 'mainwp_users_manage_roles', $editable_roles );
678
679 $editable_roles[''] = esc_html__( '&mdash; No role for this site &mdash;', 'mainwp' );
680
681 ?>
682 <div id="mainwp-edit-users-modal" class="ui modal">
683 <i class="close icon"></i>
684 <div class="header"><?php esc_html_e( 'Edit User', 'mainwp' ); ?></div>
685 <div class="ui message"><?php esc_html_e( 'Empty fields will not be passed to child sites.', 'mainwp' ); ?></div>
686 <form id="update_user_profile">
687 <?php wp_nonce_field( 'mainwp-admin-nonce' ); ?>
688 <div class="ui segment">
689 <div class="ui form">
690 <h3><?php esc_html_e( 'Name', 'mainwp' ); ?></h3>
691 <div class="ui grid field">
692 <label class="six wide column middle aligned"><?php esc_html_e( 'Role', 'mainwp' ); ?></label>
693 <div class="ui six wide column">
694 <div class="ui left labeled input">
695 <select name="role" id="role">
696 <?php
697 foreach ( $editable_roles as $role_id => $role_name ) {
698 echo '<option value="' . esc_attr( $role_id ) . '" ' . ( 'donotupdate' === $role_id ? 'selected="selected"' : '' ) . '>' . esc_html( $role_name ) . '</option>';
699 }
700 ?>
701 </select>
702 </div>
703 </div>
704 </div>
705
706 <div class="ui grid field">
707 <label class="six wide column middle aligned"><?php esc_html_e( 'First Name', 'mainwp' ); ?></label>
708 <div class="ui six wide column">
709 <div class="ui left labeled input">
710 <input type="text" name="first_name" id="first_name" value="" class="regular-text" />
711 </div>
712 </div>
713 </div>
714
715 <div class="ui grid field">
716 <label class="six wide column middle aligned"><?php esc_html_e( 'Last Name', 'mainwp' ); ?></label>
717 <div class="ui six wide column">
718 <div class="ui left labeled input">
719 <input type="text" name="last_name" id="last_name" value="" class="regular-text" />
720 </div>
721 </div>
722 </div>
723
724 <div class="ui grid field">
725 <label class="six wide column middle aligned"><?php esc_html_e( 'Nickname', 'mainwp' ); ?></label>
726 <div class="ui six wide column">
727 <div class="ui left labeled input">
728 <input type="text" name="nickname" id="nickname" value="" class="regular-text" />
729 </div>
730 </div>
731 </div>
732
733 <div class="ui grid field">
734 <label class="six wide column middle aligned"><?php esc_html_e( 'Display name publicly as', 'mainwp' ); ?></label>
735 <div class="ui six wide column">
736 <div class="ui left labeled input">
737 <select name="display_name" id="display_name"></select>
738 </div>
739 </div>
740 </div>
741
742 <h3><?php esc_html_e( 'Contact Info', 'mainwp' ); ?></h3>
743
744 <div class="ui grid field">
745 <label class="six wide column middle aligned"><?php esc_html_e( 'Email', 'mainwp' ); ?></label>
746 <div class="ui six wide column">
747 <div class="ui left labeled input">
748 <input type="email" name="email" id="email" value="" class="regular-text ltr" />
749 </div>
750 </div>
751 </div>
752
753 <div class="ui grid field">
754 <label class="six wide column middle aligned"><?php esc_html_e( 'Website', 'mainwp' ); ?></label>
755 <div class="ui six wide column">
756 <div class="ui left labeled input">
757 <input type="url" name="url" id="url" value="" class="regular-text code" />
758 </div>
759 </div>
760 </div>
761
762 <h3><?php esc_html_e( 'About the user', 'mainwp' ); ?></h3>
763
764 <div class="ui grid field">
765 <label class="six wide column middle aligned"><?php esc_html_e( 'Biographical Info', 'mainwp' ); ?></label>
766 <div class="ui six wide column">
767 <div class="ui left labeled input">
768 <textarea name="description" id="description" rows="5" cols="30"></textarea>
769 <p class="description"><?php esc_html_e( 'Share a little biographical information to fill out your profile. This may be shown publicly.', 'mainwp' ); ?></p>
770 </div>
771 </div>
772 </div>
773
774 <h3><?php esc_html_e( 'Account Management', 'mainwp' ); ?></h3>
775
776 <div class="ui grid field">
777 <label class="six wide column middle aligned"><?php esc_html_e( 'Password', 'mainwp' ); ?></label>
778 <div class="ui six wide column">
779 <div class="ui left labeled action input">
780 <input class="hidden" value=" "/>
781 <input type="text" id="password" name="password" autocomplete="off" value="">
782 </div>
783 </div>
784 </div>
785 </div>
786 </div>
787 </form>
788 <?php
789 $is_demo = MainWP_Demo_Handle::is_demo_mode();
790 ?>
791 <div class="actions">
792 <div id="mainwp_update_password_error" style="display: none"></div>
793 <span id="mainwp_users_updating"><i class="ui active inline loader tiny"></i></span>
794 <?php
795 if ( $is_demo ) {
796 MainWP_Demo_Handle::get_instance()->render_demo_disable_button( '<input type="button" class="ui green button disabled" disabled="disabled" value="' . esc_attr__( 'Update', 'mainwp' ) . '">' );
797 } else {
798 ?>
799 <input type="button" class="ui green button" id="mainwp_btn_update_user" value="<?php esc_attr_e( 'Update', 'mainwp' ); ?>">
800 <?php } ?>
801 </div>
802 </div>
803 <?php
804 }
805
806 /**
807 * Renders Users Table.
808 *
809 * @param bool $cached true|false Cached or not.
810 * @param string $role Current user role.
811 * @param string $groups Current user groups.
812 * @param string $sites Current Child Sites the user is on.
813 * @param null $search Search field.
814 * @param mixed $clients Current Clients's Sites the user is on.
815 *
816 * @uses \MainWP\Dashboard\MainWP_Cache::echo_body()
817 */
818 public static function render_table( $cached = true, $role = '', $groups = '', $sites = '', $search = null, $clients = '' ) {
819
820 /**
821 * Action: mainwp_before_users_table
822 *
823 * Fires before the User table.
824 *
825 * @since 4.1
826 */
827 do_action( 'mainwp_before_users_table' );
828 ?>
829 <table id="mainwp-users-table" class="ui unstackable single line table" style="width:100%">
830 <thead>
831 <tr>
832 <th class="no-sort collapsing check-column"><span class="ui checkbox"><input id="cb-select-all-top" type="checkbox" /></span></th>
833 <?php do_action( 'mainwp_users_table_header' ); ?>
834 <th id="name"><?php esc_html_e( 'Name', 'mainwp' ); ?></th>
835 <th id="username"><?php esc_html_e( 'Username', 'mainwp' ); ?></th>
836 <th id="email"><?php esc_html_e( 'E-mail', 'mainwp' ); ?></th>
837 <th id="role"><?php esc_html_e( 'Role', 'mainwp' ); ?></th>
838 <th id="posts"><?php esc_html_e( 'Posts', 'mainwp' ); ?></th>
839 <th id="website"><?php esc_html_e( 'Website', 'mainwp' ); ?></th>
840 <th id="mainwp-users-actions" class="no-sort collapsing"></th>
841 </tr>
842 </thead>
843 <tbody id="mainwp-users-list">
844 <?php
845 if ( $cached ) {
846 MainWP_Cache::echo_body( 'Users' );
847 } else {
848 self::render_table_body( $role, $groups, $sites, $search, $clients );
849 }
850 ?>
851 </tbody>
852 </table>
853 <?php
854 /**
855 * Action: mainwp_after_users_table
856 *
857 * Fires after the User table.
858 *
859 * @since 4.1
860 */
861 do_action( 'mainwp_after_users_table' );
862
863 /**
864 * Filter: mainwp_users_table_fatures
865 *
866 * Filters the Manage Users table features.
867 *
868 * @since 4.1
869 */
870 $table_features = array(
871 'searching' => 'true',
872 'paging' => 'true',
873 'info' => 'true',
874 'stateSave' => 'true',
875 'scrollX' => 'true',
876 'responsive' => 'true',
877 'colReorder' => '{ fixedColumnsLeft: 1, fixedColumnsRight: 1 }',
878 'order' => '[]',
879 );
880 $table_features = apply_filters( 'mainwp_users_table_fatures', $table_features );
881 ?>
882 <script type="text/javascript">
883 var responsive = <?php echo esc_html( $table_features['responsive'] ); ?>;
884 if( jQuery( window ).width() > 1140 ) {
885 responsive = false;
886 }
887 jQuery( document ).ready( function () {
888 try {
889 jQuery( "#mainwp-users-table" ).DataTable().destroy(); // to fix re-init database issue.
890 $manage_users_table = jQuery( "#mainwp-users-table" ).DataTable( {
891 "responsive" : responsive,
892 "searching" : <?php echo esc_html( $table_features['searching'] ); ?>,
893 "colReorder" : <?php echo esc_html( $table_features['colReorder'] ); ?>,
894 "stateSave": <?php echo esc_html( $table_features['stateSave'] ); ?>,
895 "paging": <?php echo esc_html( $table_features['paging'] ); ?>,
896 "info": <?php echo esc_html( $table_features['info'] ); ?>,
897 "order": <?php echo esc_html( $table_features['order'] ); ?>,
898 "scrollX" : <?php echo esc_html( $table_features['scrollX'] ); ?>,
899 "lengthMenu": [ [10, 25, 50, 100, -1], [10, 25, 50, 100, "All"] ],
900 "language" : { "emptyTable": "<?php esc_html_e( 'Please use the search options to find wanted users.', 'mainwp' ); ?>" },
901 "columnDefs": [ {
902 "targets": 'no-sort',
903 "orderable": false
904 } ],
905 "preDrawCallback": function() {
906 jQuery('#mainwp-users-table .ui.dropdown').dropdown();
907 jQuery('#mainwp-users-table .ui.checkbox').checkbox();
908 mainwp_datatable_fix_menu_overflow();
909 mainwp_table_check_columns_init(); // ajax: to fix checkbox all.
910 }
911 } );
912 } catch ( err ) {
913 // to fix js error.
914 }
915
916 _init_manage_sites_screen = function() {
917 jQuery( '#mainwp-overview-screen-options-modal input[type=checkbox][id^="mainwp_show_column_"]' ).each( function() {
918 var col_id = jQuery( this ).attr( 'id' );
919 col_id = col_id.replace( "mainwp_show_column_", "" );
920 try {
921 $manage_users_table.column( '#' + col_id ).visible( jQuery(this).is( ':checked' ) );
922 } catch(err) {
923 // to fix js error.
924 }
925 } );
926 };
927 _init_manage_sites_screen();
928
929 } );
930 </script>
931 <?php
932 }
933
934 /**
935 * Renders the table body.
936 *
937 * @param string $role User Role.
938 * @param string $groups Usr Group.
939 * @param string $sites Users Sites.
940 * @param string $search Search field.
941 * @param mixed $clients Current Clients's Sites the user is on.
942 *
943 * @uses \MainWP\Dashboard\MainWP_Cache::init_cache()
944 * @uses \MainWP\Dashboard\MainWP_Cache::add_context()
945 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
946 * @uses \MainWP\Dashboard\MainWP_DB::query()
947 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
948 * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_by_group_id()
949 * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
950 * @uses \MainWP\Dashboard\MainWP_DB::free_result()
951 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
952 * @uses \MainWP\Dashboard\MainWP_Utility::map_site()
953 */
954 public static function render_table_body( $role = '', $groups = '', $sites = '', $search = '', $clients = '' ) { // phpcs:ignore -- current complexity required to achieve desired results. Pull request solutions appreciated.
955 MainWP_Cache::init_cache( 'Users' );
956
957 $output = new \stdClass();
958 $output->errors = array();
959 $output->users = 0;
960
961 $data_fields = MainWP_System_Utility::get_default_map_site_fields();
962 $data_fields[] = 'users';
963
964 if ( 1 === (int) get_option( 'mainwp_optimize', 1 ) || MainWP_Demo_Handle::is_demo_mode() ) {
965
966 $check_users_role = false;
967
968 if ( ! empty( $role ) ) {
969 $roles = explode( ',', $role );
970 if ( is_array( $roles ) ) {
971 $check_users_role = true;
972 }
973 }
974
975 $dbwebsites = array();
976 if ( ! empty( $sites ) ) {
977 foreach ( $sites as $k => $v ) {
978 if ( MainWP_Utility::ctype_digit( $v ) ) {
979 $website = MainWP_DB::instance()->get_website_by_id( $v );
980 if ( empty( $website->sync_errors ) && ! MainWP_System_Utility::is_suspended_site( $website ) ) {
981 $dbwebsites[ $website->id ] = $website;
982 }
983 }
984 }
985 }
986 if ( ! empty( $groups ) ) {
987 foreach ( $groups as $k => $v ) {
988 if ( MainWP_Utility::ctype_digit( $v ) ) {
989 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_by_group_id( $v ) );
990 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
991 if ( ! empty( $website->sync_errors ) || MainWP_System_Utility::is_suspended_site( $website ) ) {
992 continue;
993 }
994 $dbwebsites[ $website->id ] = $website;
995 }
996 MainWP_DB::free_result( $websites );
997 }
998 }
999 }
1000
1001 if ( ! empty( $clients ) && is_array( $clients ) ) {
1002 $websites = MainWP_DB_Client::instance()->get_websites_by_client_ids(
1003 $clients,
1004 array(
1005 'select_data' => $data_fields,
1006 )
1007 );
1008
1009 foreach ( $websites as $website ) {
1010 if ( ! empty( $website->sync_errors ) || MainWP_System_Utility::is_suspended_site( $website ) ) {
1011 continue;
1012 }
1013 $dbwebsites[ $website->id ] = $website;
1014 }
1015 }
1016
1017 if ( $dbwebsites ) {
1018 foreach ( $dbwebsites as $website ) {
1019 $allUsers = json_decode( $website->users, true );
1020 $allUsersCount = count( $allUsers );
1021 $search_user_role = array();
1022 if ( $check_users_role ) {
1023 for ( $i = 0; $i < $allUsersCount; $i++ ) {
1024 $user = $allUsers[ $i ];
1025 foreach ( $roles as $_role ) {
1026 if ( stristr( $user['role'], $_role ) ) {
1027 if ( ! in_array( $user['id'], $search_user_role ) ) {
1028 $search_user_role[] = $user['id'];
1029 }
1030 break;
1031 }
1032 }
1033 }
1034 }
1035 for ( $i = 0; $i < $allUsersCount; $i++ ) {
1036 $user = $allUsers[ $i ];
1037 if ( ! empty( $search ) && ! stristr( $user['login'], trim( $search ) ) && ! stristr( $user['display_name'], trim( $search ) ) && ! stristr( $user['email'], trim( $search ) ) ) {
1038 continue;
1039 }
1040
1041 if ( $check_users_role ) {
1042 if ( ! in_array( $user['id'], $search_user_role ) ) {
1043 continue;
1044 }
1045 }
1046
1047 $tmpUsers = array( $user );
1048 $output->users += self::users_search_handler_renderer( $tmpUsers, $website );
1049 }
1050 }
1051 }
1052 } else {
1053 $dbwebsites = array();
1054 if ( '' !== $sites ) {
1055 foreach ( $sites as $k => $v ) {
1056 if ( MainWP_Utility::ctype_digit( $v ) ) {
1057 $website = MainWP_DB::instance()->get_website_by_id( $v );
1058 if ( empty( $website->sync_errors ) && ! MainWP_System_Utility::is_suspended_site( $website ) ) {
1059 $dbwebsites[ $website->id ] = MainWP_Utility::map_site(
1060 $website,
1061 $data_fields
1062 );
1063 }
1064 }
1065 }
1066 }
1067 if ( '' !== $groups ) {
1068 foreach ( $groups as $k => $v ) {
1069 if ( MainWP_Utility::ctype_digit( $v ) ) {
1070 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_by_group_id( $v ) );
1071 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
1072 if ( '' !== $website->sync_errors || MainWP_System_Utility::is_suspended_site( $website ) ) {
1073 continue;
1074 }
1075 $dbwebsites[ $website->id ] = MainWP_Utility::map_site(
1076 $website,
1077 $data_fields
1078 );
1079 }
1080 MainWP_DB::free_result( $websites );
1081 }
1082 }
1083 }
1084
1085 if ( '' !== $clients && is_array( $clients ) ) {
1086 $websites = MainWP_DB_Client::instance()->get_websites_by_client_ids(
1087 $clients,
1088 array(
1089 'select_data' => $data_fields,
1090 )
1091 );
1092 if ( $websites ) {
1093 foreach ( $websites as $website ) {
1094 if ( '' !== $website->sync_errors || MainWP_System_Utility::is_suspended_site( $website ) ) {
1095 continue;
1096 }
1097 $dbwebsites[ $website->id ] = MainWP_Utility::map_site(
1098 $website,
1099 $data_fields
1100 );
1101 }
1102 }
1103 }
1104
1105 $post_data = array(
1106 'role' => $role,
1107 'search' => '*' . trim( $search ) . '*',
1108 'search_columns' => 'user_login,display_name,user_email',
1109 );
1110
1111 MainWP_Connect::fetch_urls_authed(
1112 $dbwebsites,
1113 'search_users',
1114 $post_data,
1115 array(
1116 self::get_class_name(),
1117 'users_search_handler',
1118 ),
1119 $output
1120 );
1121 }
1122
1123 MainWP_Cache::add_context(
1124 'Users',
1125 array(
1126 'count' => $output->users,
1127 'keyword' => $search,
1128 'status' => ( isset( $_POST['role'] ) ? sanitize_text_field( wp_unslash( $_POST['role'] ) ) : 'administrator' ), // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1129 'sites' => '' !== $sites ? $sites : '',
1130 'groups' => '' !== $groups ? $groups : '',
1131 'clients' => ( '' !== $clients ) ? $clients : '',
1132 )
1133 );
1134
1135 // Sort if required.
1136
1137 if ( empty( $output->users ) ) {
1138 self::render_cache_not_found();
1139 return;
1140 }
1141 }
1142
1143 /**
1144 * Renders when cache is not found.
1145 *
1146 * @uses \MainWP\Dashboard\MainWP_Cache::add_body()
1147 */
1148 public static function render_cache_not_found() {
1149 ob_start();
1150 $newOutput = ob_get_clean();
1151 echo $newOutput; // phpcs:ignore WordPress.Security.EscapeOutput
1152 MainWP_Cache::add_body( 'Users', $newOutput );
1153 }
1154
1155 /**
1156 * Gets the selected users current role.
1157 *
1158 * @param string $role Selected Users Role.
1159 */
1160 private static function get_role( $role ) {
1161 if ( is_array( $role ) ) {
1162 $allowed_roles = array( 'subscriber', 'administrator', 'editor', 'author', 'contributor' );
1163 $ret = '';
1164 foreach ( $role as $ro ) {
1165 if ( in_array( $ro, $allowed_roles ) ) {
1166 $ret .= ucfirst( $ro ) . ', ';
1167 }
1168 }
1169 $ret = rtrim( $ret, ', ' );
1170 if ( '' === $ret ) {
1171 $ret = 'None';
1172 }
1173
1174 return $ret;
1175 }
1176
1177 return ucfirst( $role );
1178 }
1179
1180 /**
1181 * Renders Search results.
1182 *
1183 * @param array $users Users array.
1184 * @param object $website Object containing the child site info.
1185 *
1186 * @return mixed Search results table.
1187 *
1188 * @uses \MainWP\Dashboard\MainWP_Cache::add_body()
1189 */
1190 protected static function users_search_handler_renderer( $users, $website ) {
1191 $return = 0;
1192 $is_demo = MainWP_Demo_Handle::is_demo_mode();
1193
1194 foreach ( $users as $user ) {
1195 if ( ! is_array( $user ) ) {
1196 continue;
1197 }
1198 ob_start();
1199 ?>
1200 <tr>
1201 <td class="check-column"><span class="ui checkbox"><input type="checkbox" name="user[]" value="1"></span></td>
1202 <?php do_action( 'mainwp_users_table_column', $user, $website ); ?>
1203 <td class="name column-name">
1204 <?php echo ! empty( $user['display_name'] ) ? esc_html( $user['display_name'] ) : '&nbsp;'; ?>
1205 </td>
1206 <td class="username column-username"><strong><abbr title="<?php echo esc_attr( $user['login'] ); ?>"><?php echo esc_html( $user['login'] ); ?></abbr></strong></td>
1207 <td class="email column-email"><a href="mailto:<?php echo esc_attr( $user['email'] ); ?>"><?php echo esc_html( $user['email'] ); ?></a></td>
1208 <td class="role column-role"><?php echo self::get_role( $user['role'] ); // phpcs:ignore WordPress.Security.EscapeOutput ?></td>
1209 <td class="posts column-posts"><a href="<?php echo esc_url( admin_url( 'admin.php?page=PostBulkManage&siteid=' . intval( $website->id ) ) . '&userid=' . $user['id'] ); ?>"><?php echo esc_html( $user['post_count'] ); ?></a></td>
1210 <td class="website column-website"><a href="<?php echo esc_url( $website->url ); ?>" target="_blank"><?php echo esc_html( $website->url ); ?></a></td>
1211 <td class="right aligned">
1212 <input class="userId" type="hidden" name="id" value="<?php echo esc_attr( $user['id'] ); ?>" />
1213 <input class="userName" type="hidden" name="name" value="<?php echo esc_attr( $user['login'] ); ?>" />
1214 <input class="websiteId" type="hidden" name="id" value="<?php echo intval( $website->id ); ?>" />
1215 <div class="ui left pointing dropdown icon mini basic green button" style="z-index: 999">
1216 <a href="javascript:void(0)"><i class="ellipsis horizontal icon"></i></a>
1217 <div class="menu">
1218 <a class="item user_getedit" href="#"><?php esc_html_e( 'Edit', 'mainwp' ); ?></a>
1219 <?php if ( ( 1 !== (int) $user['id'] ) && ( $user['login'] !== $website->adminname ) ) { ?>
1220 <a class="item user_submitdelete" href="#"><?php esc_html_e( 'Delete', 'mainwp' ); ?></a>
1221 <?php } elseif ( ( 1 === (int) $user['id'] ) || ( $user['login'] === $website->adminname ) ) { ?>
1222 <a href="javascript:void(0)" class="item" data-tooltip="This user is used for our secure link, it can not be deleted." data-inverted="" data-position="left center"><?php esc_html_e( 'Delete', 'mainwp' ); ?></a>
1223 <?php } ?>
1224 <?php if ( ! $is_demo ) : ?>
1225 <a class="item" href="<?php echo 'admin.php?page=SiteOpen&newWindow=yes&websiteid=' . intval( $website->id ); ?>&_opennonce=<?php echo esc_html( wp_create_nonce( 'mainwp-admin-nonce' ) ); ?>" data-tooltip="<?php esc_attr_e( 'Jump to the site WP Admin', 'mainwp' ); ?>" data-position="bottom right" data-inverted="" class="open_newwindow_wpadmin ui green basic icon button" target="_blank"><?php esc_html_e( 'Go to WP Admin', 'mainwp' ); ?></a>
1226 <?php else : ?>
1227 <a class="item" href="<?php echo esc_url( $website->url ) . 'wp-admin.html'; ?>" data-tooltip="<?php esc_attr_e( 'Jump to the site WP Admin', 'mainwp' ); ?>" data-position="bottom right" data-inverted="" class="open_newwindow_wpadmin ui green basic icon button" target="_blank"><?php esc_html_e( 'Go to WP Admin', 'mainwp' ); ?></a>
1228 <?php endif; ?>
1229 <?php
1230 /**
1231 * Action: mainwp_users_table_action
1232 *
1233 * Adds a new item in the Actions menu in Manage Users table.
1234 *
1235 * Suggested HTML markup:
1236 * <a class="item" href="Your custom URL">Your custom label</a>
1237 *
1238 * @param array $user Array containing the user data.
1239 * @param array $website Object containing the website data.
1240 *
1241 * @since 4.1
1242 */
1243 do_action( 'mainwp_users_table_action', $user, $website );
1244 ?>
1245 </div>
1246 </div>
1247 </td>
1248 </tr>
1249 <?php
1250 $newOutput = ob_get_clean();
1251 echo $newOutput; // phpcs:ignore WordPress.Security.EscapeOutput
1252 MainWP_Cache::add_body( 'Users', $newOutput );
1253 ++$return;
1254 }
1255
1256 return $return;
1257 }
1258
1259 /**
1260 * Handles user search.
1261 *
1262 * @param mixed $data Search data.
1263 * @param mixed $website Child Site.
1264 * @param mixed $output Output to pass to self::users_search_handler_renderer().
1265 *
1266 * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_error_message()
1267 * @uses \MainWP\Dashboard\MainWP_Exception
1268 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_child_response()
1269 */
1270 public static function users_search_handler( $data, $website, &$output ) {
1271 if ( MainWP_Demo_Handle::get_instance()->is_demo_website( $website ) ) {
1272 return;
1273 }
1274 if ( 0 < preg_match( '/<mainwp>(.*)<\/mainwp>/', $data, $results ) ) {
1275 $result = $results[1];
1276 $users = MainWP_System_Utility::get_child_response( base64_decode( $result ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible.
1277 unset( $results );
1278 $output->users += self::users_search_handler_renderer( $users, $website );
1279 unset( $users );
1280 } else {
1281 $output->errors[ $website->id ] = MainWP_Error_Helper::get_error_message( new MainWP_Exception( 'NOMAINWP', $website->url ) );
1282 }
1283 }
1284
1285 /**
1286 * Deletes user.
1287 */
1288 public static function delete() {
1289 self::action( 'delete' );
1290 die( wp_json_encode( array( 'result' => esc_html__( 'User has been deleted', 'mainwp' ) ) ) );
1291 }
1292
1293 /**
1294 * Edits user.
1295 */
1296 public static function edit() {
1297 $information = self::action( 'edit' );
1298 wp_send_json( $information );
1299 }
1300
1301 /**
1302 * Updates user.
1303 */
1304 public static function update_user() {
1305 self::action( 'update_user' );
1306 die( wp_json_encode( array( 'result' => esc_html__( 'User has been updated', 'mainwp' ) ) ) );
1307 }
1308
1309 /**
1310 * Updates users password.
1311 */
1312 public static function update_password() {
1313 self::action( 'update_password' );
1314 die( wp_json_encode( array( 'result' => esc_html__( 'User password has been updated', 'mainwp' ) ) ) );
1315 }
1316
1317 /**
1318 * Users actions.
1319 *
1320 * @param mixed $pAction Action to perform delete|update_user|update_password.
1321 * @param string $extra Additional Roles to add if any.
1322 *
1323 * @return mixed $information User update info that is returned.
1324 * @throws \Exception Error message.
1325 *
1326 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
1327 * @uses \MainWP\Dashboard\MainWP_DB::update_website_values()
1328 * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_error_message()
1329 * @uses \MainWP\Dashboard\MainWP_Exception
1330 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
1331 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
1332 */
1333 public static function action( $pAction, $extra = '' ) { // phpcs:ignore -- current complexity required to achieve desired results. Pull request solutions appreciated.
1334
1335 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1336 $userId = isset( $_POST['userId'] ) ? sanitize_text_field( wp_unslash( $_POST['userId'] ) ) : false;
1337 $userName = isset( $_POST['userName'] ) ? sanitize_text_field( wp_unslash( $_POST['userName'] ) ) : '';
1338 $websiteId = isset( $_POST['websiteId'] ) ? sanitize_text_field( wp_unslash( $_POST['websiteId'] ) ) : false;
1339 $pass = isset( $_POST['update_password'] ) ? rawurldecode( wp_unslash( $_POST['update_password'] ) ) : '';
1340
1341 if ( function_exists( '\mb_convert_encoding' ) ) {
1342 $pass = \mb_convert_encoding( $pass, 'ISO-8859-1', 'UTF-8' );
1343 } else {
1344 $pass = utf8_decode( $pass ); // to compatible.
1345 }
1346
1347 if ( empty( $userId ) || empty( $websiteId ) ) {
1348 die( wp_json_encode( array( 'error' => esc_html__( 'Site ID or user ID not found. Please reload the page and try again.', 'mainwp' ) ) ) );
1349 }
1350
1351 $website = MainWP_DB::instance()->get_website_by_id( $websiteId );
1352
1353 if ( MainWP_System_Utility::is_suspended_site( $website ) ) {
1354 die(
1355 wp_json_encode(
1356 array(
1357 'error' => esc_html__( 'Suspended site.', 'mainwp' ),
1358 'errorCode' => 'SUSPENDED_SITE',
1359 )
1360 )
1361 );
1362 }
1363
1364 if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
1365 die( wp_json_encode( array( 'error' => esc_html__( 'You can not edit this website!', 'mainwp' ) ) ) );
1366 }
1367
1368 if ( ( 'delete' === $pAction ) && ( $website->adminname === $userName ) ) {
1369 die( wp_json_encode( array( 'error' => esc_html__( 'This user is used for our secure link, it can not be deleted.', 'mainwp' ) ) ) );
1370 }
1371
1372 if ( 'update_user' === $pAction ) {
1373 $user_data = isset( $_POST['user_data'] ) ? wp_unslash( $_POST['user_data'] ) : '';
1374 parse_str( $user_data, $extra );
1375 if ( $website->adminname === $userName ) {
1376
1377 if ( is_array( $extra ) && isset( $extra['role'] ) ) {
1378 unset( $extra['role'] );
1379 }
1380 }
1381
1382 if ( ! empty( $pass ) ) {
1383 $extra['pass1'] = $pass;
1384 $extra['pass2'] = $pass;
1385 }
1386 }
1387 // phpcs:enable
1388
1389 $optimize = ( 1 === (int) get_option( 'mainwp_optimize', 1 ) ) ? 1 : 0;
1390
1391 /**
1392 * Action: mainwp_before_user_action
1393 *
1394 * Fires before user edit/delete/update_user/update_password actions.
1395 *
1396 * @since 4.1
1397 */
1398 do_action( 'mainwp_before_user_action', $pAction, $userId, $extra, $pass, $optimize, $website );
1399
1400 try {
1401 $information = MainWP_Connect::fetch_url_authed(
1402 $website,
1403 'user_action',
1404 array(
1405 'action' => $pAction,
1406 'id' => $userId,
1407 'extra' => $extra,
1408 'user_pass' => $pass,
1409 'optimize' => $optimize,
1410 )
1411 );
1412
1413 if ( is_array( $information ) && isset( $information['status'] ) && ( 'SUCCESS' === $information['status'] ) ) {
1414 $data = isset( $information['other_data']['users_data'] ) ? $information['other_data']['users_data'] : array(); // user actions data.
1415
1416 /**
1417 * Fires immediately after user action.
1418 *
1419 * @since 4.5.1.1
1420 */
1421 do_action( 'mainwp_user_action', $website, $pAction, $data, $extra, $optimize );
1422 }
1423 } catch ( MainWP_Exception $e ) {
1424 die( wp_json_encode( array( 'error' => MainWP_Error_Helper::get_error_message( $e ) ) ) );
1425 }
1426
1427 /**
1428 * Action: mainwp_after_user_action
1429 *
1430 * Fires after user edit/delete/update_user/update_password actions.
1431 *
1432 * @since 4.1
1433 */
1434 do_action( 'mainwp_after_user_action', $information, $pAction, $userId, $extra, $pass, $optimize, $website );
1435
1436 if ( is_array( $information ) && isset( $information['error'] ) ) {
1437 wp_send_json( array( 'error' => esc_html( $information['error'] ) ) );
1438 }
1439
1440 if ( ! isset( $information['status'] ) || ( 'SUCCESS' !== $information['status'] ) ) {
1441 die( wp_json_encode( array( 'error' => esc_html__( 'Unexpected error.', 'mainwp' ) ) ) );
1442 } elseif ( 'update_user' === $pAction ) {
1443 if ( $optimize && isset( $information['users'] ) ) {
1444 $websiteValues['users'] = wp_json_encode( $information['users'] );
1445 MainWP_DB::instance()->update_website_values( $websiteId, $websiteValues );
1446 }
1447 }
1448
1449 if ( 'edit' === $pAction ) {
1450 if ( $website->adminname === $userName ) {
1451 // This user is used for our secure link, you can not change the role.
1452 if ( is_array( $information ) && isset( $information['user_data'] ) ) {
1453 $information['is_secure_admin'] = 1;
1454 }
1455 }
1456 }
1457
1458 return $information;
1459 }
1460
1461 /**
1462 * Renders the Add New user form.
1463 */
1464 public static function render_bulk_add() {
1465
1466 /**
1467 * Filter: mainwp_new_user_password_complexity
1468 *
1469 * Filters the Password lenght for the Add New user, Password field.
1470 *
1471 * Since 4.1
1472 */
1473 $pass_complexity = apply_filters( 'mainwp_new_user_password_complexity', '24' );
1474 self::render_header( 'Add' );
1475 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1476 ?>
1477 <div class="ui alt segment" id="mainwp-add-users">
1478 <?php
1479 /**
1480 * Action: mainwp_before_new_user_form
1481 *
1482 * Fires before the Add New user form.
1483 *
1484 * @since 4.1
1485 */
1486 do_action( 'mainwp_before_new_user_form' );
1487 ?>
1488 <form action="" method="post" name="createuser" id="createuser" class="add:users: validate">
1489 <?php wp_nonce_field( 'mainwp-admin-nonce' ); ?>
1490 <div class="mainwp-main-content">
1491 <div class="ui hidden divider"></div>
1492 <?php if ( MainWP_Utility::show_mainwp_message( 'notice', 'mainwp-add-user-info-message' ) ) : ?>
1493 <div class="ui info message">
1494 <i class="close icon mainwp-notice-dismiss" notice-id="mainwp-add-user-info-message"></i>
1495 <?php printf( esc_html__( 'Use the provided form to create a new user on your child site. For additional help, please check this %1$shelp documentation%2$s.', 'mainwp' ), '<a href="https://kb.mainwp.com/docs/create-a-new-user/" target="_blank">', '</a>' ); ?>
1496 </div>
1497 <?php endif; ?>
1498 <div class="ui message" id="mainwp-message-zone" style="display:none;"></div>
1499 <div id="mainwp-add-new-user-form" >
1500 <?php
1501 /**
1502 * Action: mainwp_before_new_user_form_fields
1503 *
1504 * Fires before the Add New user form fields.
1505 *
1506 * @since 4.1
1507 */
1508 do_action( 'mainwp_before_new_user_form_fields' );
1509 ?>
1510 <div class="ui form">
1511 <div class="ui grid field">
1512 <label class="six wide column middle aligned"><?php esc_html_e( 'Username', 'mainwp' ); ?></label>
1513 <div class="ui six wide column">
1514 <div class="ui left labeled input">
1515 <input type="text" id="user_login" name="user_login" value="<?php echo ( isset( $_POST['user_login'] ) ) ? esc_html( sanitize_text_field( wp_unslash( $_POST['user_login'] ) ) ) : ''; ?>">
1516 </div>
1517 </div>
1518 </div>
1519 <div class="ui grid field">
1520 <label class="six wide column middle aligned"><?php esc_html_e( 'E-mail', 'mainwp' ); ?></label>
1521 <div class="ui six wide column">
1522 <div class="ui left labeled input">
1523 <input type="text" id="email" name="email" value="<?php echo ( isset( $_POST['email'] ) ) ? esc_html( sanitize_text_field( wp_unslash( $_POST['email'] ) ) ) : ''; ?>">
1524 </div>
1525 </div>
1526 </div>
1527 <div class="ui grid field">
1528 <label class="six wide column middle aligned"><?php esc_html_e( 'First Name', 'mainwp' ); ?></label>
1529 <div class="ui six wide column">
1530 <div class="ui left labeled input">
1531 <input type="text" id="first_name" name="first_name" value="<?php echo ( isset( $_POST['first_name'] ) ) ? esc_html( sanitize_text_field( wp_unslash( $_POST['first_name'] ) ) ) : ''; ?>">
1532 </div>
1533 </div>
1534 </div>
1535 <div class="ui grid field">
1536 <label class="six wide column middle aligned"><?php esc_html_e( 'Last Name', 'mainwp' ); ?></label>
1537 <div class="ui six wide column">
1538 <div class="ui left labeled input">
1539 <input type="text" id="last_name" name="last_name" value="<?php echo ( isset( $_POST['last_name'] ) ) ? esc_html( sanitize_text_field( wp_unslash( $_POST['last_name'] ) ) ) : ''; ?>">
1540 </div>
1541 </div>
1542 </div>
1543 <div class="ui grid field">
1544 <label class="six wide column middle aligned"><?php esc_html_e( 'Website', 'mainwp' ); ?></label>
1545 <div class="ui six wide column">
1546 <div class="ui left labeled input">
1547 <input type="text" id="url" name="url" value="<?php echo ( isset( $_POST['url'] ) ) ? esc_url_raw( wp_unslash( $_POST['url'] ) ) : ''; ?>">
1548 </div>
1549 </div>
1550 </div>
1551 <div class="ui grid field">
1552 <label class="six wide column middle aligned"><?php esc_html_e( 'Password', 'mainwp' ); ?></label>
1553 <div class="ui six wide column">
1554 <div class="ui left labeled action input">
1555 <input class="hidden" value=" "/>
1556 <input type="text" id="password" name="password" autocomplete="off" value="<?php echo esc_attr( wp_generate_password( $pass_complexity ) ); ?>">
1557 <button class="ui green right button wp-generate-pw"><?php esc_html_e( 'Generate Password', 'mainwp' ); ?></button>
1558 </div>
1559 </div>
1560 </div>
1561
1562 <div class="ui grid field">
1563 <label class="six wide column middle aligned"><?php esc_html_e( 'Send Password?', 'mainwp' ); ?></label>
1564 <div class="six wide column ui toggle checkbox">
1565 <input type="checkbox" name="send_password" id="send_password" <?php echo ( isset( $_POST['send_password'] ) ) ? esc_html( 'checked' ) : ''; ?> >
1566 </div>
1567 </div>
1568
1569 <?php
1570 $user_roles = array(
1571 'subscriber' => esc_html__( 'Subscriber', 'mainwp' ),
1572 'administrator' => esc_html__( 'Administrator', 'mainwp' ),
1573 'editor' => esc_html__( 'Editor', 'mainwp' ),
1574 'author' => esc_html__( 'Author', 'mainwp' ),
1575 'contributor' => esc_html__( 'Contributor', 'mainwp' ),
1576 );
1577 $user_roles = apply_filters_deprecated( 'mainwp-users-manage-roles', array( $user_roles ), '4.0.7.2', 'mainwp_users_manage_roles' ); // @deprecated Use 'mainwp_users_manage_roles' instead.
1578 $user_roles = apply_filters( 'mainwp_users_manage_roles', $user_roles );
1579 ?>
1580
1581 <div class="ui grid field">
1582 <label class="six wide column middle aligned"><?php esc_html_e( 'Role', 'mainwp' ); ?></label>
1583 <div class="six wide column">
1584 <select class="ui dropdown" name="role" id="role">
1585 <?php
1586 foreach ( $user_roles as $r => $n ) {
1587 if ( empty( $r ) ) {
1588 continue;
1589 }
1590 ?>
1591 <option value="<?php echo esc_html( $r ); ?>" <?php echo ( isset( $_POST['role'] ) && $_POST['role'] === $r ) ? esc_html( 'selected' ) : ''; ?>><?php echo esc_html( $n ); ?></option>
1592 <?php
1593 }
1594 ?>
1595 </select>
1596 </div>
1597 </div>
1598
1599 </div>
1600 <?php
1601 /**
1602 * Action: mainwp_after_new_user_form_fields
1603 *
1604 * Fires after the Add New user form fields.
1605 *
1606 * @since 4.1
1607 */
1608 do_action( 'mainwp_after_new_user_form_fields' );
1609 ?>
1610 </div>
1611 </div>
1612 <div class="mainwp-side-content mainwp-no-padding">
1613 <?php
1614 /**
1615 * Action: mainwp_add_new_user_sidebar_top
1616 *
1617 * Fires at the top of the sidebar on Add New user page.
1618 *
1619 * @since 4.1
1620 */
1621 do_action( 'mainwp_add_new_user_sidebar_top' );
1622 ?>
1623 <div class="mainwp-select-sites ui accordion mainwp-sidebar-accordion">
1624 <?php
1625 /**
1626 * Action: mainwp_add_new_user_before_select_sites
1627 *
1628 * Fires before the Select Sites section on the Add New user page.
1629 *
1630 * @since 4.1
1631 */
1632 do_action( 'mainwp_add_new_user_before_select_sites' );
1633 ?>
1634 <div class="title active"><i class="dropdown icon"></i> <?php esc_html_e( 'Select Sites', 'mainwp' ); ?></div>
1635 <div class="content active">
1636 <?php
1637 $sel_params = array(
1638 'show_client' => true,
1639 );
1640 MainWP_UI_Select_Sites::select_sites_box( $sel_params );
1641 ?>
1642 </div>
1643 <?php
1644 /**
1645 * Action: mainwp_add_new_user_after_select_sites
1646 *
1647 * Fires after the Select Sites section on the Add New user page.
1648 *
1649 * @since 4.1
1650 */
1651 do_action( 'mainwp_add_new_user_after_select_sites' );
1652 ?>
1653 </div>
1654 <div class="ui fitted divider"></div>
1655 <div class="mainwp-search-submit">
1656 <?php
1657 /**
1658 * Action: mainwp_add_new_user_before_submit_button
1659 *
1660 * Fires before the Submit button on the Add New user page.
1661 *
1662 * @since 4.1
1663 */
1664 do_action( 'mainwp_add_new_user_before_submit_button' );
1665
1666 $is_demo = MainWP_Demo_Handle::is_demo_mode();
1667 if ( $is_demo ) {
1668 MainWP_Demo_Handle::get_instance()->render_demo_disable_button( '<input type="button" class="ui green big button disabled" disabled="disabled" value="' . esc_attr__( 'Add New User', 'mainwp' ) . '"/>' );
1669 } else {
1670 ?>
1671 <input type="button" name="createuser" id="bulk_add_createuser" class="ui big green fluid button" value="<?php esc_attr_e( 'Add New User', 'mainwp' ); ?> "/>
1672 <?php
1673 }
1674 /**
1675 * Action: mainwp_add_new_user_after_submit_button
1676 *
1677 * Fires after the Submit button on the Add New user page.
1678 *
1679 * @since 4.1
1680 */
1681 do_action( 'mainwp_add_new_user_after_submit_button' );
1682 ?>
1683 </div>
1684 <?php
1685 /**
1686 * Action: mainwp_add_new_user_sidebar_bottom
1687 *
1688 * Fires at the bottom of the sidebar on Add New user page.
1689 *
1690 * @since 4.1
1691 */
1692 do_action( 'mainwp_add_new_user_sidebar_bottom' );
1693 ?>
1694 </div>
1695 <div style="clear:both"></div>
1696 </form>
1697 <?php
1698 /**
1699 * Action: mainwp_after_new_user_form
1700 *
1701 * Fires after the Add New user form.
1702 *
1703 * @since 4.1
1704 */
1705 do_action( 'mainwp_after_new_user_form' );
1706 ?>
1707 </div>
1708 <?php
1709 // phpcs:enable
1710
1711 self::render_footer( 'Add' );
1712 }
1713
1714 /**
1715 * Renders bulk Import Users form.
1716 *
1717 * @return void
1718 */
1719 public static function render_bulk_import_users() {
1720 if ( isset( $_FILES['import_user_file_bulkupload'] ) && isset( $_FILES['import_user_file_bulkupload']['error'] ) && UPLOAD_ERR_OK === $_FILES['import_user_file_bulkupload']['error'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing
1721 self::render_bulk_upload();
1722 return;
1723 }
1724 ?>
1725 <?php self::render_header( 'Import' ); ?>
1726 <div id="MainWP_Bulk_AddUser">
1727 <form action="" method="post" name="createuser" id="createuser" class="add:users: validate" enctype="multipart/form-data">
1728 <?php wp_nonce_field( 'mainwp-admin-nonce' ); ?>
1729 <?php self::render_import_users(); ?>
1730 </form>
1731 </div>
1732 <?php
1733 self::render_footer( 'Import' );
1734 }
1735
1736 /**
1737 * Method render_import_users()
1738 *
1739 * Render Import Users page.
1740 */
1741 public static function render_import_users() {
1742 ?>
1743 <div class="ui segment" id="mainwp-import-sites">
1744 <?php if ( MainWP_Utility::show_mainwp_message( 'notice', 'mainwp-import-users-info-message' ) ) : ?>
1745 <div class="ui info message">
1746 <i class="close icon mainwp-notice-dismiss" notice-id="mainwp-import-users-info-message"></i>
1747 <?php printf( esc_html__( 'Use the form to bulk import users. You can download the sample CSV file to see how to fomat the import file properly. For additional help, please check this %1$shelp documentation%2$s.', 'mainwp' ), '<a href="https://kb.mainwp.com/docs/import-users/" target="_blank">', '</a>' ); ?>
1748 </div>
1749 <?php endif; ?>
1750
1751 <div id="mainwp-message-zone" class="ui message" style="display:none"></div>
1752 <?php
1753 /**
1754 * Action: mainwp_before_import_users
1755 *
1756 * Fires above the Import Users section.
1757 *
1758 * @since 4.1
1759 */
1760 do_action( 'mainwp_before_import_users' );
1761 $is_demo = MainWP_Demo_Handle::is_demo_mode();
1762 ?>
1763 <div id="mainwp-message-zone" class="ui message" style="display:none"></div>
1764 <div class="ui form">
1765 <form method="POST" action="" enctype="multipart/form-data" id="mainwp_managesites_bulkadd_form">
1766 <?php wp_nonce_field( 'mainwp-admin-nonce' ); ?>
1767 <div class="ui grid field">
1768 <label class="six wide column middle aligned"><?php esc_html_e( 'Upload the CSV file', 'mainwp' ); ?></label>
1769 <div class="ten wide column" data-tooltip="<?php esc_attr_e( 'Click to upload the import file.', 'mainwp' ); ?>" data-inverted="" data-position="top left">
1770 <div class="ui file input">
1771 <input type="file" name="import_user_file_bulkupload" id="import_user_file_bulkupload" accept="text/comma-separated-values" />
1772 </div>
1773 </div>
1774 </div>
1775 <div class="ui grid field">
1776 <label class="six wide column middle aligned"><?php esc_html_e( 'CSV file contains a header', 'mainwp' ); ?></label>
1777 <div class="ui toggle checkbox" data-tooltip="<?php esc_attr_e( 'Enable if the import file contains a header.', 'mainwp' ); ?>" data-inverted="" data-position="top left">
1778 <input type="checkbox" name="import_user_chk_header_first" checked="checked" id="import_user_chk_header_first" value="1" />
1779 </div>
1780 </div>
1781 <div class="ui divider"></div>
1782 <?php
1783 if ( $is_demo ) {
1784 MainWP_Demo_Handle::get_instance()->render_demo_disable_button( '<input type="button" class="ui big green button disabled" disabled="disabled" value="' . esc_attr__( 'Import Users', 'mainwp' ) . '"/>' );
1785 } else {
1786 ?>
1787 <input type="button" name="createuser" id="bulk_import_createuser" class="ui big green button" value="<?php esc_attr_e( 'Import Users', 'mainwp' ); ?>" />
1788 <?php } ?>
1789 <a href="<?php echo esc_url( MAINWP_PLUGIN_URL . 'assets/csv/sample_users.csv' ); ?>" class="ui big green basic right floated button"><?php esc_html_e( 'Download Sample CSV file', 'mainwp' ); ?></a>
1790 </form>
1791 </div>
1792 <?php
1793 /**
1794 * Action: mainwp_after_import_users
1795 *
1796 * Fires under the Import Users section.
1797 *
1798 * @since 4.1
1799 */
1800 do_action( 'mainwp_after_import_users' );
1801 ?>
1802 </div>
1803 <?php
1804 }
1805
1806 /**
1807 * Method do_bulk_add()
1808 *
1809 * Bulk User addition $_POST Handler.
1810 *
1811 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
1812 * @uses \MainWP\Dashboard\MainWP_DB::query()
1813 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
1814 * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
1815 * @uses \MainWP\Dashboard\MainWP_DB::free_result()
1816 * @uses \MainWP\Dashboard\MainWP_Bulk_Add::get_class_name()
1817 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
1818 * @uses \MainWP\Dashboard\MainWP_Utility::map_site()
1819 */
1820 public static function do_bulk_add() { // phpcs:ignore -- Current complexity is required to achieve desired results. Pull request solutions appreciated.
1821 $errors = array();
1822 $errorFields = array();
1823
1824 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1825 if ( isset( $_POST['select_by'] ) ) {
1826 $selected_sites = ( isset( $_POST['selected_sites'] ) && is_array( $_POST['selected_sites'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['selected_sites'] ) ) : array();
1827 $selected_groups = ( isset( $_POST['selected_groups'] ) && is_array( $_POST['selected_groups'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['selected_groups'] ) ) : array();
1828 $selected_clients = ( isset( $_POST['selected_clients'] ) && is_array( $_POST['selected_clients'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['selected_clients'] ) ) : array();
1829
1830 if ( ( 'group' === $_POST['select_by'] && 0 === count( $selected_groups ) ) || ( 'site' === $_POST['select_by'] && 0 === count( $selected_sites ) ) || ( 'client' === $_POST['select_by'] && 0 === count( $selected_clients ) ) ) {
1831 $errors[] = esc_html__( 'Please select at least one website or group or client.', 'mainwp' );
1832 }
1833 } else {
1834 $errors[] = esc_html__( 'Please select at least one website or group or client.', 'mainwp' );
1835 }
1836
1837 if ( ! isset( $_POST['user_login'] ) || '' === $_POST['user_login'] ) {
1838 $errorFields[] = 'user_login';
1839 }
1840
1841 if ( ! isset( $_POST['email'] ) || '' === $_POST['email'] ) {
1842 $errorFields[] = 'email';
1843 }
1844
1845 $allowed_roles = array( 'subscriber', 'administrator', 'editor', 'author', 'contributor' );
1846 $cus_roles = array();
1847 $cus_roles = apply_filters_deprecated( 'mainwp-users-manage-roles', array( $cus_roles ), '4.0.7.2', 'mainwp_users_manage_roles' ); // @deprecated Use 'mainwp_users_manage_roles' instead.
1848 $cus_roles = apply_filters( 'mainwp_users_manage_roles', $cus_roles );
1849
1850 if ( is_array( $cus_roles ) && 0 < count( $cus_roles ) ) {
1851 $cus_roles = array_keys( $cus_roles );
1852 $allowed_roles = array_merge( $allowed_roles, $cus_roles );
1853 }
1854
1855 if ( ! isset( $_POST['role'] ) || ! in_array( $_POST['role'], $allowed_roles ) ) {
1856 $errorFields[] = 'role';
1857 }
1858
1859 $data_fields = MainWP_System_Utility::get_default_map_site_fields();
1860
1861 if ( ( 0 === count( $errors ) ) && ( 0 === count( $errorFields ) ) ) {
1862 $user_to_add = array(
1863 'user_pass' => isset( $_POST['pass1'] ) ? wp_unslash( $_POST['pass1'] ) : '',
1864 'user_login' => isset( $_POST['user_login'] ) ? sanitize_text_field( wp_unslash( $_POST['user_login'] ) ) : '',
1865 'user_url' => isset( $_POST['url'] ) ? esc_url_raw( wp_unslash( $_POST['url'] ) ) : '',
1866 'user_email' => isset( $_POST['email'] ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : '',
1867 'first_name' => isset( $_POST['first_name'] ) ? sanitize_text_field( wp_unslash( $_POST['first_name'] ) ) : '',
1868 'last_name' => isset( $_POST['last_name'] ) ? sanitize_text_field( wp_unslash( $_POST['last_name'] ) ) : '',
1869 'role' => isset( $_POST['role'] ) ? sanitize_text_field( wp_unslash( $_POST['role'] ) ) : '',
1870 );
1871
1872 $dbwebsites = array();
1873
1874 if ( isset( $_POST['select_by'] ) && 'site' === $_POST['select_by'] ) {
1875 foreach ( $selected_sites as $k ) {
1876 if ( MainWP_Utility::ctype_digit( $k ) ) {
1877 $website = MainWP_DB::instance()->get_website_by_id( $k );
1878 if ( empty( $website->sync_errors ) && ! MainWP_System_Utility::is_suspended_site( $website ) ) {
1879 $dbwebsites[ $website->id ] = MainWP_Utility::map_site(
1880 $website,
1881 $data_fields
1882 );
1883 }
1884 }
1885 }
1886 } elseif ( isset( $_POST['select_by'] ) && 'client' === $_POST['select_by'] ) {
1887 $websites = MainWP_DB_Client::instance()->get_websites_by_client_ids(
1888 $selected_clients,
1889 array(
1890 'select_data' => $data_fields,
1891 )
1892 );
1893
1894 if ( $websites ) {
1895 foreach ( $websites as $website ) {
1896 if ( '' !== $website->sync_errors || MainWP_System_Utility::is_suspended_site( $website ) ) {
1897 continue;
1898 }
1899 $dbwebsites[ $website->id ] = MainWP_Utility::map_site(
1900 $website,
1901 $data_fields
1902 );
1903 }
1904 }
1905 } else {
1906 foreach ( $selected_groups as $k ) {
1907 if ( MainWP_Utility::ctype_digit( $k ) ) {
1908 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_by_group_id( $k ) );
1909 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
1910 if ( '' !== $website->sync_errors || MainWP_System_Utility::is_suspended_site( $website ) ) {
1911 continue;
1912 }
1913 $dbwebsites[ $website->id ] = MainWP_Utility::map_site(
1914 $website,
1915 $data_fields
1916 );
1917 }
1918 MainWP_DB::free_result( $websites );
1919 }
1920 }
1921 }
1922
1923 $startTime = time();
1924 if ( 0 < count( $dbwebsites ) ) {
1925 $post_data = array(
1926 'new_user' => base64_encode( wp_json_encode( $user_to_add ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible.
1927 'send_password' => ( isset( $_POST['send_password'] ) ? intval( $_POST['send_password'] ) : '' ),
1928 );
1929 $output = new \stdClass();
1930 $output->ok = array();
1931 $output->errors = array();
1932
1933 /**
1934 * Action: mainwp_before_user_create
1935 *
1936 * Fires before user create.
1937 *
1938 * @since 4.1
1939 */
1940 do_action( 'mainwp_before_user_create', $post_data, $dbwebsites );
1941
1942 MainWP_Connect::fetch_urls_authed(
1943 $dbwebsites,
1944 'newuser',
1945 $post_data,
1946 array(
1947 MainWP_Bulk_Add::get_class_name(),
1948 'posting_bulk_handler',
1949 ),
1950 $output
1951 );
1952
1953 /**
1954 * Action: mainwp_after_user_create
1955 *
1956 * Fires after user create.
1957 *
1958 * @since 4.1
1959 */
1960 do_action( 'mainwp_after_user_create', $output, $post_data, $dbwebsites );
1961 }
1962
1963 $countSites = 0;
1964 $countRealItems = 0;
1965 foreach ( $dbwebsites as $website ) {
1966 if ( isset( $output->ok[ $website->id ] ) && 1 === (int) $output->ok[ $website->id ] ) {
1967 ++$countSites;
1968 ++$countRealItems;
1969 }
1970 }
1971 self::render_bulk_add_modal( $dbwebsites, $output );
1972 } else {
1973 echo wp_json_encode( array( $errorFields, $errors ) );
1974 }
1975 // phpcs:enable
1976 }
1977
1978 /**
1979 * Renders Bulk User addition Modal window.
1980 *
1981 * @param mixed $dbwebsites Child sites list.
1982 * @param mixed $output Modal window content.
1983 */
1984 public static function render_bulk_add_modal( $dbwebsites, $output ) {
1985 // phpcs:disable WordPress.Security.EscapeOutput
1986 ?>
1987 <div id="mainwp-creating-new-user-modal" class="ui modal">
1988 <i class="close icon"></i>
1989 <div class="header"><?php esc_html_e( 'New User', 'mainwp' ); ?></div>
1990 <div class="content">
1991 <div class="ui middle aligned divided selection list">
1992 <?php foreach ( $dbwebsites as $website ) : ?>
1993 <div class="item ui grid">
1994 <span class="content"><a href="<?php echo esc_url( admin_url( 'admin.php?page=managesites&dashboard=' . $website->id ) ); ?>"><?php echo esc_html( stripslashes( $website->name ) ); ?></a></span>
1995 <span class="right floated content"><?php echo( isset( $output->ok[ $website->id ] ) && 1 === (int) $output->ok[ $website->id ] ? '<i class="check green icon"></i> ' : '<i class="times red icon"></i> ' . $output->errors[ $website->id ] ); ?></span>
1996 </div>
1997 <?php endforeach; ?>
1998 </div>
1999 </div>
2000 <div class="actions">
2001 </div>
2002 </div>
2003 <?php
2004 // phpcs:enable
2005 }
2006
2007 /**
2008 * Renders Import Users Modal window.
2009 *
2010 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
2011 * @uses \MainWP\Dashboard\MainWP_Utility::starts_with()
2012 */
2013 public static function render_bulk_upload() {
2014 self::render_header( 'Import' );
2015 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2016 $errors = array();
2017 if ( isset( $_FILES['import_user_file_bulkupload']['error'] ) && UPLOAD_ERR_OK === $_FILES['import_user_file_bulkupload']['error'] ) {
2018 if ( isset( $_FILES['import_user_file_bulkupload']['tmp_name'] ) && is_uploaded_file( $_FILES['import_user_file_bulkupload']['tmp_name'] ) ) {
2019 $tmp_path = isset( $_FILES['import_user_file_bulkupload']['tmp_name'] ) ? sanitize_text_field( wp_unslash( $_FILES['import_user_file_bulkupload']['tmp_name'] ) ) : '';
2020 $wpFileSystem = MainWP_System_Utility::get_wp_file_system();
2021 // phpcs:enable
2022 /**
2023 * WordPress files system object.
2024 *
2025 * @global object
2026 */
2027 global $wp_filesystem;
2028
2029 $content = $wp_filesystem->get_contents( $tmp_path );
2030 $lines = explode( "\r\n", $content );
2031
2032 if ( is_array( $lines ) && 0 < count( $lines ) ) {
2033 $i = 0;
2034 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2035 if ( ! empty( $_POST['import_user_chk_header_first'] ) ) {
2036 $header_line = trim( $lines[0] ) . "\n";
2037 unset( $lines[0] );
2038 }
2039 // phpcs:enable
2040
2041 foreach ( $lines as $originalLine ) {
2042
2043 $line = trim( $originalLine );
2044
2045 if ( MainWP_Utility::starts_with( $line, '#' ) ) {
2046 continue;
2047 }
2048
2049 $items = str_getcsv( $line, ',' );
2050
2051 if ( 3 > count( $items ) ) {
2052 continue;
2053 }
2054
2055 $import_data = array(
2056 'user_login' => trim( $items[0] ),
2057 'email' => trim( $items[1] ),
2058 'first_name' => trim( $items[2] ),
2059 'last_name' => trim( $items[3] ),
2060 'url' => trim( $items[4] ),
2061 'pass1' => trim( $items[5] ),
2062 'send_password' => intval( $items[6] ),
2063 'role' => trim( strtolower( $items[7] ) ),
2064 'select_sites' => trim( $items[8] ),
2065 'select_groups' => trim( $items[9] ),
2066 );
2067 $encoded = wp_json_encode( $import_data );
2068 ?>
2069 <input type="hidden" id="user_import_csv_line_<?php echo intval( $i + 1 ); ?>" original-line="<?php echo esc_html( $line ); ?>" encoded-data="<?php echo esc_html( $encoded ); ?>" />
2070 <?php
2071 ++$i;
2072 }
2073 $header_line = trim( $header_line );
2074 ?>
2075 <div class="ui modal" id="mainwp-import-users-modal">
2076 <i class="close icon"></i>
2077 <div class="header"><?php esc_html_e( 'Importing new users and add them to your sites.', 'mainwp' ); ?></div>
2078 <div class="scrolling content">
2079 <?php
2080 /**
2081 * Action: mainwp_import_users_modal_top
2082 *
2083 * Fires on the top of the Import Users modal.
2084 *
2085 * @since 4.1
2086 */
2087 do_action( 'mainwp_import_users_modal_top' );
2088 ?>
2089 <div id="MainWPBulkUploadUserLoading" style="display: none;"><i class="ui active inline loader tiny"></i> <?php esc_html_e( 'Importing Users', 'mainwp' ); ?></div>
2090 <input type="hidden" id="import_user_do_import" value="1"/>
2091 <input type="hidden" id="import_user_total_import" value="<?php echo esc_attr( $i ); ?>"/>
2092 <p>
2093 <div class="import_user_import_listing" id="import_user_import_logging">
2094 <pre class="log"><?php echo esc_html( $header_line ) . "\n"; ?></pre>
2095 </div>
2096 </p>
2097 <div id="import_user_import_failed_rows" style="display: none;">
2098 <span><?php echo esc_html( $header_line ); ?></span>
2099 </div>
2100 <?php
2101 /**
2102 * Action: mainwp_import_users_modal_bottom
2103 *
2104 * Fires on the bottom of the Import Users modal.
2105 *
2106 * @since 4.1
2107 */
2108 do_action( 'mainwp_import_users_modal_bottom' );
2109 ?>
2110 </div>
2111 <div class="actions">
2112 <input type="button" name="import_user_btn_import" id="import_user_btn_import" class="ui basic button" value="<?php esc_attr_e( 'Pause', 'mainwp' ); ?>"/>
2113 <input type="button" name="import_user_btn_save_csv" id="import_user_btn_save_csv" style="display:none;" class="ui basic green button" value="<?php esc_attr_e( 'Save failed', 'mainwp' ); ?>"/>
2114 </div>
2115 </div>
2116 <script type="text/javascript">
2117 jQuery( document ).ready( function () {
2118 jQuery( "#mainwp-import-users-modal" ).modal( {
2119 closable: false,
2120 onHide: function() {
2121 location.href = 'admin.php?page=BulkImportUsers';
2122 }
2123 } ).modal( 'show' );
2124 } );
2125 </script>
2126
2127 <?php
2128
2129 } else {
2130 $errors[] = esc_html__( 'Invalid data. Please make sure that the Import file has been formated properly.', 'mainwp' );
2131 }
2132 } else {
2133 $errors[] = esc_html__( 'File could not be uploaded. Temporary file cold not be created. Please make sure that the tmpfile() PHP function is enabled on your server.', 'mainwp' );
2134 }
2135 } else {
2136 $errors[] = esc_html__( 'File could not be uploaded. Please try again. If process keeps failing, please review MainWP Knowledgebase, and if you still have issues, please let us know in the MainWP Community.', 'mainwp' );
2137 }
2138
2139 if ( 0 < count( $errors ) ) {
2140 ?>
2141 <?php foreach ( $errors as $error ) { ?>
2142 <div class="ui error message"><?php echo esc_html( $error ); ?></div>
2143 <?php } ?>
2144
2145 <a href="<?php echo esc_url( get_admin_url() ); ?>admin.php?page=UserBulkAdd" class="ui button"><?php esc_html_e( 'Add New', 'mainwp' ); ?></a>
2146 <a href="<?php echo esc_url( get_admin_url() ); ?>admin.php?page=mainwp_tab" class="ui button"><?php esc_html_e( 'Return to Overview', 'mainwp' ); ?></a>
2147 <?php
2148 }
2149
2150 self::render_footer( 'Import' );
2151 }
2152
2153 /**
2154 * User Import $_POST handler.
2155 *
2156 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
2157 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_group_by_name()
2158 * @uses \MainWP\Dashboard\MainWP_DB::query()
2159 * @uses \MainWP\Dashboard\MainWP_DB::get_websites_by_url()
2160 * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_by_group_id()
2161 * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
2162 * @uses \MainWP\Dashboard\MainWP_DB::free_result()
2163 * @uses \MainWP\Dashboard\MainWP_Bulk_Add::get_class_name()
2164 * @uses \MainWP\Dashboard\MainWP_Utility::map_site()
2165 */
2166 public static function do_import() { // phpcs:ignore -- Current complexity is required to achieve desired results. Pull request solutions appreciated.
2167
2168 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2169 $selected_sites = ( isset( $_POST['select_sites'] ) && is_array( $_POST['select_sites'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['select_sites'] ) ) : array();
2170 $selected_groups = ( isset( $_POST['select_groups'] ) && is_array( $_POST['select_groups'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['select_groups'] ) ) : array();
2171
2172 $data_fields = MainWP_System_Utility::get_default_map_site_fields();
2173 $data_fields[] = 'users';
2174
2175 $user_to_add = array(
2176 'user_pass' => isset( $_POST['pass1'] ) ? wp_unslash( $_POST['pass1'] ) : '',
2177 'user_login' => isset( $_POST['user_login'] ) ? sanitize_text_field( wp_unslash( $_POST['user_login'] ) ) : '',
2178 'user_url' => isset( $_POST['url'] ) ? wp_unslash( $_POST['url'] ) : '',
2179 'user_email' => isset( $_POST['email'] ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : '',
2180 'first_name' => isset( $_POST['first_name'] ) ? sanitize_text_field( wp_unslash( $_POST['first_name'] ) ) : '',
2181 'last_name' => isset( $_POST['last_name'] ) ? sanitize_text_field( wp_unslash( $_POST['last_name'] ) ) : '',
2182 'role' => isset( $_POST['role'] ) ? sanitize_text_field( wp_unslash( $_POST['role'] ) ) : '',
2183 );
2184
2185 $ret = array();
2186 $dbwebsites = array();
2187 $not_valid = array();
2188 $error_sites = '';
2189 if ( isset( $_POST['select_by'] ) && 'site' === $_POST['select_by'] ) {
2190 foreach ( $selected_sites as $url ) {
2191 if ( ! empty( $url ) ) {
2192 $website = MainWP_DB::instance()->get_websites_by_url( $url );
2193 if ( ! empty( $website ) ) {
2194 $website = current( $website );
2195 }
2196 if ( $website ) {
2197 if ( '' !== $website->sync_errors || MainWP_System_Utility::is_suspended_site( $website ) ) {
2198 if ( '' !== $website->sync_errors ) {
2199 $err_imp = esc_html__( 'Site disconnected:', 'mainwp' );
2200 } else {
2201 $err_imp = esc_html__( 'Site suspended:', 'mainwp' );
2202 }
2203 $not_valid[] = $err_imp . ' ' . $website->url;
2204 $error_sites .= $website->url . ';';
2205 continue;
2206 }
2207 $dbwebsites[ $website->id ] = MainWP_Utility::map_site(
2208 $website,
2209 $data_fields
2210 );
2211 } else {
2212 $not_valid[] = esc_html__( 'Unexisting website. Please try again.', 'mainwp' ) . ' ' . $url;
2213 $error_sites .= $url . ';';
2214 }
2215 }
2216 }
2217 } else {
2218 foreach ( $selected_groups as $group ) {
2219 if ( MainWP_DB_Common::instance()->get_group_by_name( $group ) ) {
2220 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_by_group_name( $group ) );
2221 if ( $websites ) {
2222 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
2223 if ( '' !== $website->sync_errors || MainWP_System_Utility::is_suspended_site( $website ) ) {
2224 if ( '' !== $website->sync_errors ) {
2225 $err_imp = esc_html__( 'Site disconnected:', 'mainwp' );
2226 } else {
2227 $err_imp = esc_html__( 'Site suspended:', 'mainwp' );
2228 }
2229 $not_valid[] = $err_imp . ' ' . $website->url;
2230 $error_sites .= $website->url . ';';
2231 continue;
2232 }
2233 $dbwebsites[ $website->id ] = MainWP_Utility::map_site(
2234 $website,
2235 $data_fields
2236 );
2237 }
2238 MainWP_DB::free_result( $websites );
2239 } else {
2240 $not_valid[] = esc_html__( 'No websites assigned to the selected group.', 'mainwp' ) . ' ' . $group;
2241 $error_sites .= $group . ';';
2242 }
2243 } else {
2244 $not_valid[] = esc_html__( 'Unexisting group selected. Please try again.', 'mainwp' ) . ' ' . $group;
2245 $error_sites .= $group . ';';
2246 }
2247 }
2248 }
2249
2250 if ( 0 < count( $dbwebsites ) ) {
2251 $post_data = array(
2252 'new_user' => base64_encode( wp_json_encode( $user_to_add ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible.
2253 'send_password' => ( isset( $_POST['send_password'] ) ? intval( $_POST['send_password'] ) : '' ),
2254 );
2255 $output = new \stdClass();
2256 $output->ok = array();
2257 $output->errors = array();
2258
2259 /**
2260 * Action: mainwp_before_user_create
2261 *
2262 * Fires before user create.
2263 *
2264 * @since 4.1
2265 */
2266 do_action( 'mainwp_before_user_create', $post_data, $dbwebsites );
2267
2268 MainWP_Connect::fetch_urls_authed(
2269 $dbwebsites,
2270 'newuser',
2271 $post_data,
2272 array(
2273 MainWP_Bulk_Add::get_class_name(),
2274 'posting_bulk_handler',
2275 ),
2276 $output
2277 );
2278
2279 /**
2280 * Action: mainwp_after_user_create
2281 *
2282 * Fires after user create.
2283 *
2284 * @since 4.1
2285 */
2286 do_action( 'mainwp_after_user_create', $output, $post_data, $dbwebsites );
2287 }
2288
2289 $ret['ok_list'] = array();
2290 $ret['error_list'] = array();
2291 foreach ( $dbwebsites as $website ) {
2292 if ( isset( $output->ok[ $website->id ] ) && 1 === (int) $output->ok[ $website->id ] ) {
2293 $ret['ok_list'][] = 'New user(s) created: ' . esc_html( stripslashes( $website->name ) );
2294 } else {
2295 $ret['error_list'][] = esc_html( $output->errors[ $website->id ] . ' ' . stripslashes( $website->name ) );
2296 $error_sites .= $website->url . ';';
2297 }
2298 }
2299
2300 foreach ( $not_valid as $val ) {
2301 $ret['error_list'][] = $val;
2302 }
2303
2304 $ret['failed_logging'] = '';
2305 if ( ! empty( $error_sites ) ) {
2306 $error_sites = rtrim( $error_sites, ';' );
2307
2308 $user_login = isset( $_POST['user_login'] ) ? sanitize_text_field( wp_unslash( $_POST['user_login'] ) ) : '';
2309 $email = isset( $_POST['email'] ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : '';
2310 $first_name = isset( $_POST['first_name'] ) ? sanitize_text_field( wp_unslash( $_POST['first_name'] ) ) : '';
2311 $last_name = isset( $_POST['last_name'] ) ? sanitize_text_field( wp_unslash( $_POST['last_name'] ) ) : '';
2312 $url = isset( $_POST['url'] ) ? sanitize_text_field( wp_unslash( $_POST['url'] ) ) : '';
2313 $pass1 = isset( $_POST['pass1'] ) ? wp_unslash( $_POST['pass1'] ) : '';
2314 $send_password = isset( $_POST['send_password'] ) ? intval( $_POST['send_password'] ) : 0;
2315 $role = isset( $_POST['role'] ) ? sanitize_text_field( wp_unslash( $_POST['role'] ) ) : '';
2316
2317 $ret['failed_logging'] = esc_html( $user_login . ',' . $email . ',' . $first_name . ',' . $last_name . ',' . $url . ',' . $pass1 . ',' . $send_password . ',' . $role . ',' . $error_sites . ',' );
2318 }
2319 $ret['line_number'] = isset( $_POST['line_number'] ) ? intval( $_POST['line_number'] ) : 0;
2320 // phpcs:enable
2321 die( wp_json_encode( $ret ) );
2322 }
2323
2324 /**
2325 * Hooks the section help content to the Help Sidebar element.
2326 */
2327 public static function mainwp_help_content() {
2328 if ( isset( $_GET['page'] ) && ( 'UserBulkManage' === $_GET['page'] || 'UserBulkAdd' === $_GET['page'] || 'UpdateAdminPasswords' === $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2329 ?>
2330 <p><?php esc_html_e( 'If you need help with managing users, please review following help documents', 'mainwp' ); ?></p>
2331 <div class="ui relaxed bulleted list">
2332 <div class="item"><a href="https://kb.mainwp.com/docs/manage-users/" target="_blank">Manage Users</a> <i class="external alternate icon"></i></div>
2333 <div class="item"><a href="https://kb.mainwp.com/docs/create-a-new-user/" target="_blank">Create a New User</a> <i class="external alternate icon"></i></div>
2334 <div class="item"><a href="https://kb.mainwp.com/docs/edit-an-existing-user/" target="_blank">Edit an Existing User</a> <i class="external alternate icon"></i></div>
2335 <div class="item"><a href="https://kb.mainwp.com/docs/bulk-update-administrator-passwords/" target="_blank">Bulk Update Administrator Passwords</a> <i class="external alternate icon"></i></div>
2336 <div class="item"><a href="https://kb.mainwp.com/docs/delete-users/" target="_blank">Delete User(s)</a> <i class="external alternate icon"></i></div>
2337 <?php
2338 /**
2339 * Action: mainwp_users_help_item
2340 *
2341 * Fires at the bottom of the help articles list in the Help sidebar on the Users page.
2342 *
2343 * Suggested HTML markup:
2344 *
2345 * <div class="item"><a href="Your custom URL">Your custom text</a></div>
2346 *
2347 * @since 4.1
2348 */
2349 do_action( 'mainwp_users_help_item' );
2350 ?>
2351 </div>
2352 <?php
2353 }
2354 }
2355 }
2356