PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.2.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.2.1
6.2.3 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 All 156 releases
mainwp / pages / page-mainwp-manage-groups.php

page-mainwp-manage-groups.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.2.1, at pages/page-mainwp-manage-groups.php

811 lines 34.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Manage Groups.
4 *
5 * @package MainWP/Dashboard
6 */
7
8 namespace MainWP\Dashboard;
9
10 /**
11 * Class MainWP_Manage_Groups
12 *
13 * @package MainWP\Dashboard
14 */
15 class MainWP_Manage_Groups { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
16
17 /**
18 * Get Class Name
19 *
20 * @return string __CLASS__
21 */
22 public static function get_class_name() {
23 return __CLASS__;
24 }
25
26 /**
27 * Public static varable to hold Subpages information.
28 *
29 * @var array $subPages
30 */
31 public static $subPages;
32
33 /**
34 * Method init()
35 *
36 * Initiate hooks for the users page.
37 */
38 public static function init() {
39 /**
40 * This hook allows you to render the Tags page header via the 'mainwp_pageheader_tags' action.
41 *
42 * @link http://codex.mainwp.com/#mainwp-pageheader-tags
43 *
44 * This hook is normally used in the same context of 'mainwp_getsubpages_tags'
45 * @link http://codex.mainwp.com/#mainwp-getsubpages-tags
46 *
47 * @see \MainWP_Manage_Groups::render_header
48 */
49 add_action( 'mainwp_pageheader_tags', array( static::get_class_name(), 'render_header' ) );
50
51 /**
52 * This hook allows you to render the Tags page footer via the 'mainwp_pagefooter_tags' action.
53 *
54 * @link http://codex.mainwp.com/#mainwp-pagefooter-tags
55 *
56 * This hook is normally used in the same context of 'mainwp_getsubpages_tags'
57 * @link http://codex.mainwp.com/#mainwp-getsubpages-tags
58 *
59 * @see \MainWP_Manage_Groups::render_footer
60 */
61 add_action( 'mainwp_pagefooter_tags', array( static::get_class_name(), 'render_footer' ) );
62
63 add_action( 'mainwp_help_sidebar_content', array( static::get_class_name(), 'mainwp_help_content' ) );
64 }
65
66
67 /**
68 * Method init_menu()
69 *
70 * Add Groups Sub Menu.
71 */
72 public static function init_menu() {
73 add_submenu_page(
74 'mainwp_tab',
75 __( 'Tags', 'mainwp' ),
76 '<div class="mainwp-hidden">' . esc_html__( 'Tags', 'mainwp' ) . '</div>',
77 'read',
78 'ManageGroups',
79 array(
80 static::get_class_name(),
81 'render_all_groups',
82 )
83 );
84
85 /**
86 * This hook allows you to add extra sub pages to the Tags page via the 'mainwp-getsubpages-tags' filter.
87 *
88 * @link http://codex.mainwp.com/#mainwp-getsubpages-tags
89 */
90 static::$subPages = apply_filters( 'mainwp_getsubpages_tags', static::$subPages );
91
92 static::init_left_menu( static::$subPages );
93 }
94
95 /**
96 * Initiates Tags menu.
97 *
98 * @param array $subPages Sub pages array.
99 *
100 * @uses \MainWP\Dashboard\MainWP_Menu::add_left_menu()
101 * @uses \MainWP\Dashboard\MainWP_Menu::init_subpages_left_menu()
102 * @uses \MainWP\Dashboard\MainWP_Menu::is_disable_menu_item()
103 */
104 public static function init_left_menu( $subPages = array() ) {
105 MainWP_Menu::add_left_menu(
106 array(
107 'title' => esc_html__( 'Tags', 'mainwp' ),
108 'parent_key' => 'managesites',
109 'slug' => 'ManageGroups',
110 'href' => 'admin.php?page=ManageGroups',
111 'icon' => '<i class="tags icon"></i>',
112 'desc' => 'Manage tags on your MainWP Dashboard',
113 'leftsub_order' => 3,
114 ),
115 1
116 );
117
118 $init_sub_subleftmenu = array(
119 array(
120 'title' => esc_html__( 'Manage Tags', 'mainwp' ),
121 'parent_key' => 'ManageGroups',
122 'href' => 'admin.php?page=ManageGroups',
123 'slug' => 'ManageGroups',
124 'right' => 'manage_groups',
125 ),
126 );
127
128 MainWP_Menu::init_subpages_left_menu( $subPages, $init_sub_subleftmenu, 'ManageGroups', 'ManageGroups' );
129
130 foreach ( $init_sub_subleftmenu as $item ) {
131 if ( MainWP_Menu::is_disable_menu_item( 3, $item['slug'] ) ) {
132 continue;
133 }
134 MainWP_Menu::add_left_menu( $item, 2 );
135 }
136 }
137
138 /**
139 * Method render_header()
140 *
141 * Render Tags page header.
142 *
143 * @param string $shownPage The page slug shown at this moment.
144 *
145 * @uses \MainWP\Dashboard\MainWP_Menu::is_disable_menu_item()
146 * @uses \MainWP\Dashboard\MainWP_UI::render_top_header()
147 * @uses \MainWP\Dashboard\MainWP_UI::render_page_navigation()
148 */
149 public static function render_header( $shownPage = '' ) {
150 $params = array(
151 'title' => esc_html__( 'Tags', 'mainwp' ),
152 );
153 MainWP_UI::render_top_header( $params );
154
155 $renderItems = array();
156
157 if ( mainwp_current_user_have_right( 'dashboard', 'manage_groups' ) ) {
158 $renderItems[] = array(
159 'title' => esc_html__( 'Manage Tags', 'mainwp' ),
160 'href' => 'admin.php?page=ManageGroups',
161 'active' => ( 'ManageGroups' === $shownPage ) ? true : false,
162 );
163 }
164
165 if ( isset( static::$subPages ) && is_array( static::$subPages ) ) {
166 foreach ( static::$subPages as $subPage ) {
167 if ( MainWP_Menu::is_disable_menu_item( 3, 'ManageGroups' . $subPage['slug'] ) ) {
168 continue;
169 }
170
171 $item = array();
172 $item['title'] = $subPage['title'];
173 $item['href'] = 'admin.php?page=ManageGroups' . $subPage['slug'];
174 $item['active'] = ( $subPage['slug'] === $shownPage ) ? true : false;
175 $renderItems[] = $item;
176 }
177 }
178
179 MainWP_UI::render_page_navigation( $renderItems );
180 }
181
182 /**
183 * Method render_footer()
184 *
185 * Render Tags page footer. Closes the page container.
186 */
187 public static function render_footer() {
188 echo '</div>';
189 }
190
191 /**
192 * Method get_group_list_content()
193 *
194 * Get group list contents.
195 *
196 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_groups_and_count()
197 */
198 public static function get_group_list_content() {
199
200 $groups = MainWP_DB_Common::instance()->get_groups_and_count();
201
202 foreach ( $groups as $group ) {
203 static::create_group_item( $group );
204 }
205 }
206
207 /**
208 * Metod create_group_item()
209 *
210 * Group Data Table Row.
211 *
212 * @param array $group Array of group data.
213 */
214 private static function create_group_item( $group ) {
215 ?>
216 <a class="item" id="<?php echo intval( $group->id ); ?>" style="border-left: 4px solid <?php echo empty( $group->color ) ? '#fff' : esc_attr( $group->color ); ?>">
217 <div class="ui small label"><?php echo property_exists( $group, 'nrsites' ) ? intval( $group->nrsites ) : 0; ?></div>
218 <input type="hidden" value="<?php echo esc_html( stripslashes( $group->name ) ); ?>" id="mainwp-hidden-group-name">
219 <input type="hidden" value="<?php echo esc_html( $group->color ); ?>" id="mainwp-hidden-group-color">
220 <input type="hidden" value="<?php echo intval( $group->id ); ?>" id="mainwp-hidden-group-id">
221 <?php echo esc_html( stripslashes( $group->name ) ); ?>
222 </a>
223 <?php
224 }
225
226 /**
227 * Method get_website_list_content()
228 *
229 * Get the Child Site list content.
230 *
231 * @uses \MainWP\Dashboard\MainWP_DB::query()
232 * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user()
233 * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
234 * @uses \MainWP\Dashboard\MainWP_DB::free_result()
235 * @uses \MainWP\Dashboard\MainWP_Utility::get_nice_url()
236 */
237 public static function get_website_list_content() {
238 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user() );
239
240 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
241 $note = html_entity_decode( $website->note );
242 $esc_note = MainWP_Utility::esc_content( $note );
243 $strip_note = wp_strip_all_tags( $esc_note );
244 ?>
245 <tr id="<?php echo esc_attr( $website->id ); ?>">
246 <td class="check-column collapsing">
247 <div class="item ui checkbox">
248 <input type="checkbox" name="sites" class="mainwp-site-checkbox" value="<?php echo esc_attr( $website->id ); ?>" id="<?php echo 'site-' . esc_attr( $website->id ); ?>" >
249 </div>
250 </td>
251 <td><a href="admin.php?page=managesites&dashboard=<?php echo intval( $website->id ); ?>" data-tooltip="<?php esc_attr_e( 'Go to the site overview.', 'mainwp' ); ?>" data-position="right center" data-inverted=""><?php echo esc_html( stripslashes( $website->name ) ); ?></a></td>
252 <td>
253 <a href="admin.php?page=SiteOpen&newWindow=yes&websiteid=<?php echo 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="left center" data-inverted="" class="open_newwindow_wpadmin" target="_blank"><i class="sign in icon"></i></a>
254 </td>
255 <td><a href="<?php echo esc_url( $website->url ); ?>" target="_blank"><?php echo esc_html( $website->url ); ?></a></td>
256 <td><a href="<?php echo 'admin.php?page=ManageClients&client_id=' . intval( $website->client_id ); ?>" data-tooltip="<?php esc_attr_e( 'Jump to the client', 'mainwp' ); ?>" data-position="right center" data-inverted="" ><?php echo esc_html( $website->client_name ); ?></a></td>
257 <td class="not-selectable">
258 <span class="mainwp-preview-item" data-position="left center" data-inverted="" data-tooltip="<?php esc_attr_e( 'Click to see the site homepage screenshot . ', 'mainwp' ); ?>" preview-site-url="<?php echo esc_url( $website->url ); ?>" ><i class="camera icon"></i></span>
259 </td>
260 <td class="not-selectable">
261 <?php if ( empty( $website->note ) ) : ?>
262 <a href="javascript:void(0)" class="mainwp-edit-site-note" id="mainwp-notes-<?php echo intval( $website->id ); ?>" data-tooltip="<?php esc_attr_e( 'Click to add a note . ', 'mainwp' ); ?>" data-position="left center" data-inverted=""><i class="sticky note outline icon"></i></a>
263 <?php else : ?>
264 <a href="javascript:void(0)" class="mainwp-edit-site-note" id="mainwp-notes-<?php echo intval( $website->id ); ?>" data-tooltip="<?php echo substr( wp_unslash( $strip_note ), 0, 100 ); // phpcs:ignore WordPress.Security.EscapeOutput ?>" data-position="left center" data-inverted=""><i class="sticky green note icon"></i></a>
265 <?php endif; ?>
266 <span style="display: none" id="mainwp-notes-<?php echo intval( $website->id ); ?>-note"><?php echo wp_unslash( $esc_note ); // phpcs:ignore WordPress.Security.EscapeOutput ?></span>
267 </td>
268 </tr>
269 <?php
270 }
271 MainWP_DB::free_result( $websites );
272 }
273
274 /**
275 * Method render_all_groups()
276 *
277 * Render MainWP Groups Table.
278 *
279 * @return string MainWP Groups Table.
280 */
281 public static function render_all_groups() {
282 if ( ! mainwp_current_user_have_right( 'dashboard', 'manage_groups' ) ) {
283 mainwp_do_not_have_permissions( esc_html__( 'manage groups', 'mainwp' ) );
284
285 return;
286 }
287
288 $sidebarPosition = get_user_option( 'mainwp_sidebarPosition' );
289 if ( false === $sidebarPosition ) {
290 $sidebarPosition = 1;
291 }
292
293 /**
294 * Sites Page header
295 *
296 * Renders the tabs on the Sites screen.
297 *
298 * @since Unknown
299 */
300 do_action( 'mainwp_pageheader_tags', 'ManageGroups' );
301 ?>
302 <div id="mainwp-manage-groups" class="ui segment">
303 <div id="mainwp-message-zone" style="display: none;">
304 <div class="ui message green"><?php esc_html_e( 'Selection saved successfully . ', 'mainwp' ); ?></div>
305 </div>
306 <?php if ( MainWP_Utility::show_mainwp_message( 'notice', 'mainwp_groups_info' ) ) { ?>
307 <div class="ui message info">
308 <i class="close icon mainwp-notice-dismiss" notice-id="mainwp_groups_info"></i>
309 <div><?php esc_html_e( 'In case you are managing a large number of WordPress sites, it could be useful for you to mark them with different tags . Later, you will be able to make Site Selection by a tag that will speed up your work and makes it much easier.', 'mainwp' ); ?></div>
310 <div><?php esc_html_e( 'One child site can be assigned to multiple Tags at the same time.', 'mainwp' ); ?></div>
311 <div><?php printf( esc_html__( 'for more information check the %1$sKnowledge Base %2$s.', 'mainwp' ), '<a href="https://kb.mainwp.com/docs/manage-child-site-groups/" target="_blank">', '</a> <i class="external alternate icon"></i>' ); // NOSONAR - noopener - open safe. ?></div>
312 </div>
313 <?php } ?>
314 <?php
315 /**
316 * Action: mainwp_before_groups_table
317 *
318 * Fires before the Manage Groups table.
319 *
320 * @since 4.1
321 */
322 do_action( 'mainwp_before_groups_table' );
323 ?>
324 <div class="ui stackable grid">
325 <div class="<?php echo 1 === (int) $sidebarPosition ? 'twelve' : 'four'; ?> wide column">
326 <?php if ( 1 === (int) $sidebarPosition ) : ?>
327 <?php static::render_groups_sites_table_element(); ?>
328 <?php else : ?>
329 <?php static::render_groups_menu_element(); ?>
330 <?php endif; ?>
331 </div>
332 <div class="<?php echo 1 === (int) $sidebarPosition ? 'four' : 'twelve'; ?> wide column">
333 <?php if ( 1 === (int) $sidebarPosition ) : ?>
334 <?php static::render_groups_menu_element(); ?>
335 <?php else : ?>
336 <?php static::render_groups_sites_table_element(); ?>
337 <?php endif; ?>
338 </div>
339 <script type="text/javascript">
340 let responsive = true;
341 if( jQuery( window ).width() > 1140 ) {
342 responsive = false;
343 }
344 jQuery( document ).ready( function() {
345 jQuery( '#mainwp-manage-groups-sites-table' ).DataTable( {
346 'searching' : true,
347 'responsive' : responsive,
348 'colReorder' : true,
349 'stateSave': true,
350 'paging': false,
351 'info': true,
352 'order': [ [ 1, "asc" ] ],
353 'scrollX' : false,
354 'columnDefs': [ {
355 "targets": 'no-sort',
356 "orderable": false
357 } ],
358 'preDrawCallback': function( settings ) {
359 jQuery( '#mainwp-manage-groups-sites-table .ui.checkbox' ).checkbox();
360 },
361 'select': {
362 items: 'row',
363 style: 'multi+shift',
364 selector: 'tr>td:not(.not-selectable)'
365 }
366 }).on('select', function (e, dt, type, indexes) {
367 console.log('select');
368 if( 'row' == type ){
369 dt.rows(indexes)
370 .nodes()
371 .to$().find('td.check-column .ui.checkbox' ).checkbox('set checked');
372 }
373 }).on('deselect', function (e, dt, type, indexes) {
374 if( 'row' == type ){
375 dt.rows(indexes)
376 .nodes()
377 .to$().find('td.check-column .ui.checkbox' ).checkbox('set unchecked');
378 }
379 } ).on( 'columns-reordered', function ( e, settings, details ) {
380 console.log('columns-reordered');
381 setTimeout(() => {
382 jQuery( '#mainwp-manage-groups-sites-table .ui.checkbox' ).checkbox();
383 mainwp_datatable_fix_menu_overflow('#mainwp-manage-groups-sites-table' );
384 }, 1000);
385 } );;
386 mainwp_datatable_fix_to_update_rows_state('#mainwp-manage-groups-sites-table'); // to fix saved rows state.
387 } );
388 </script>
389 </div>
390 <?php MainWP_UI::render_modal_edit_notes(); ?>
391 <div class="ui mini modal" id="mainwp-create-group-modal">
392 <i class="close icon"></i>
393 <div class="header"><?php echo esc_html__( 'Create Tag', 'mainwp' ); ?></div>
394 <div class="content">
395 <div class="ui form">
396 <div class="field">
397 <label><?php esc_html_e( 'Enter tag name', 'mainwp' ); ?></label>
398 <input type="text" value="" name="mainwp-group-name" id="mainwp-group-name">
399 </div>
400 <div class="field">
401 <label><?php esc_html_e( 'Select tag color', 'mainwp' ); ?></label>
402 <input type="color" name="mainwp-new-tag-color" class="mainwp-color-picker-input" id="mainwp-new-tag-color" value="" />
403 </div>
404 </div>
405 </div>
406 <div class="actions">
407 <div class="ui two columns grid">
408 <div class="left aligned column">
409
410 </div>
411 <div class="right aligned column">
412 <a class="ui green button" id="mainwp-save-new-group-button" href="#"><?php echo esc_html__( 'Create Tag', 'mainwp' ); ?></a>
413 </div>
414 </div>
415 </div>
416 <style>
417 .mainwp-ui .ui.modal .wp-picker-clear {
418 display:none;
419 }
420 .mainwp-ui .ui.modal #mainwp-new-tag-color {
421 height: 28px;
422 margin-left: 5px;
423 }
424 </style>
425 </div>
426
427 <div class="ui mini modal" id="mainwp-rename-group-modal">
428 <i class="close icon"></i>
429 <div class="header"><?php echo esc_html__( 'Rename Tag', 'mainwp' ); ?></div>
430 <div class="content">
431 <div class="ui form">
432 <div class="field">
433 <label><?php esc_html_e( 'Enter tag name', 'mainwp' ); ?></label>
434 <input type="text" value="" name="mainwp-group-name" id="mainwp-group-name">
435 </div>
436 <div class="field">
437 <label><?php esc_html_e( 'Select tag color', 'mainwp' ); ?></label>
438 <input type="color" name="mainwp-new-tag-color mainwp-color-picker-input" class="mainwp-color-picker-input" id="mainwp-new-tag-color" value="" />
439 </div>
440 </div>
441 </div>
442 <div class="actions">
443 <div class="ui two columns stackable grid">
444 <div class="left aligned column">
445
446 </div>
447 <div class="right aligned column">
448 <a class="ui green button" id="mainwp-update-new-group-button" href="#"><?php echo esc_html__( 'Update Tag', 'mainwp' ); ?></a>
449 </div>
450 </div>
451 </div>
452 <style>
453 .mainwp-ui .ui.modal .wp-picker-clear {
454 display:none;
455 }
456 .mainwp-ui .ui.modal #mainwp-new-tag-color {
457 height: 28px;
458 margin-left: 5px;
459 }
460 </style>
461 </div>
462 <?php
463 /**
464 * Action: mainwp_after_groups_table
465 *
466 * Fires after the Manage Groups table.
467 *
468 * @since 4.1
469 */
470 do_action( 'mainwp_after_groups_table' );
471 ?>
472 </div>
473 <?php
474 /**
475 * Sites Page Footer
476 *
477 * Renders the footer on the Sites screen.
478 *
479 * @since Unknown
480 */
481 do_action( 'mainwp_pagefooter_tags', 'ManageGroups' );
482 }
483
484 /**
485 * Method render_groups_menu_element()
486 *
487 * Render the groups menu HTML element.
488 */
489 public static function render_groups_menu_element() {
490 $sidebarPosition = get_user_option( 'mainwp_sidebarPosition' );
491 if ( false === $sidebarPosition ) {
492 $sidebarPosition = 1;
493 }
494 ?>
495 <div class="ui fluid <?php echo 1 === (int) $sidebarPosition ? 'right' : ''; ?> pointing vertical menu sticky" id="mainwp-groups-menu" style="margin-top:52px">
496 <h4 class="item ui header"><?php esc_html_e( 'Tags', 'mainwp' ); ?></h4>
497 <?php static::get_group_list_content(); ?>
498 <div class="item">
499 <div class="ui two columns stackable grid">
500 <div class="left aligned column">
501 <a href="javascript:void(0);" class="ui tiny green button" id="mainwp-new-sites-group-button" data-inverted="" data-position="top left" data-tooltip="<?php esc_attr_e( 'Click here to create a new tag.', 'mainwp' ); ?>"><?php esc_html_e( 'New Tag', 'mainwp' ); ?></a>
502 </div>
503 <div class="right aligned column">
504 <a href="javascript:void(0);" class="ui tiny icon green basic button disabled" id="mainwp-rename-group-button" data-inverted="" data-position="top right" data-tooltip="<?php esc_attr_e( 'Edit selected tag.', 'mainwp' ); ?>"><i class="edit icon"></i></a>
505 <a href="javascript:void(0);" class="ui tiny icon button disabled" id="mainwp-delete-group-button" data-inverted="" data-position="top right" data-tooltip="<?php esc_attr_e( 'Delete selected tag.', 'mainwp' ); ?>"><i class="trash icon"></i></a>
506 </div>
507 </div>
508 </div>
509 </div>
510 <?php
511 }
512
513 /**
514 * Method render_groups_sites_table_element()
515 *
516 * Render the groups menu HTML element.
517 */
518 public static function render_groups_sites_table_element() {
519 ?>
520 <table class="ui very compact table mainwp-with-preview-table" id="mainwp-manage-groups-sites-table">
521 <thead>
522 <tr>
523 <th scope="col" class="no-sort collapsing check-column"><div class="ui checkbox" data-tooltip="<?php esc_attr_e( 'Click to select all sites.', 'mainwp' ); ?>" data-position="left center" data-inverted=""><input type="checkbox" id="cb-select-all-top" name="example"></div></th>
524 <th scope="col" ><?php esc_html_e( 'Sites', 'mainwp' ); ?></th>
525 <th scope="col" class="no-sort collapsing"><i class="sign in icon"></i></th>
526 <th scope="col" ><?php esc_html_e( 'URL', 'mainwp' ); ?></th>
527 <th scope="col" ><?php esc_html_e( 'Client', 'mainwp' ); ?></th>
528 <th scope="col" class="no-sort collapsing"><i class="camera icon"></i></th>
529 <th scope="col" class="no-sort collapsing"><i class="sticky note outline icon"></i></th>
530 </tr>
531 </thead>
532 <tbody>
533 <?php static::get_website_list_content(); ?>
534 </tbody>
535 <tfoot>
536 <tr>
537 <th scope="col" colspan="7">
538 <a href="#" class="ui tiny green button" id="mainwp-save-sites-groups-selection-button" data-inverted="" data-position="top left" data-tooltip="<?php esc_attr_e( 'Save the selected tag sites selection.', 'mainwp' ); ?>"><?php esc_html_e( 'Save Selection', 'mainwp' ); ?></a>
539 </th>
540 </tr>
541 </tfoot>
542 </table>
543 <div class="ui inverted dimmer">
544 <div class="ui loader"></div>
545 </div>
546 <?php
547 }
548
549 /**
550 * Method rename_group()
551 *
552 * Rename the selected group.
553 *
554 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_group_by_id()
555 * @uses \MainWP\Dashboard\MainWP_DB_Common::update_group()
556 */
557 public static function rename_group() { //phpcs:ignore -- NOSONAR - complex.
558 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
559 if ( isset( $_POST['groupId'] ) ) {
560 $group = MainWP_DB_Common::instance()->get_group_by_id( intval( $_POST['groupId'] ) );
561 if ( ! empty( $group ) ) {
562 $old_name = $group->name;
563 $name = isset( $_POST['newName'] ) ? sanitize_text_field( wp_unslash( $_POST['newName'] ) ) : '';
564 if ( empty( $name ) ) {
565 $name = $group->name;
566 }
567
568 $name = static::check_group_name( $name, $group->id );
569
570 if ( isset( $_POST['newColor'] ) && ! empty( $_POST['newColor'] ) ) {
571 $color = sanitize_hex_color( wp_unslash( $_POST['newColor'] ) );
572 if ( empty( $color ) ) {
573 $color = $group->color;
574 }
575 } else {
576 $color = '';
577 }
578 // update group.
579 MainWP_DB_Common::instance()->update_group( $group->id, $name, $color );
580
581 // Reload group.
582 $group = MainWP_DB_Common::instance()->get_group_by_id( $group->id );
583
584 $data = array(
585 'old_name' => $old_name,
586 );
587
588 /**
589 * Fires after a new sites tag has been created.
590 *
591 * @param object $group tag created.
592 * @param string tag action.
593 * @param array other data array.
594 */
595 do_action( 'mainwp_site_tag_action', $group, 'updated', $data );
596
597 die( wp_json_encode( array( 'result' => $group->name ) ) );
598 }
599 }
600 //phpcs:enable
601 }
602
603 /**
604 * Method delete_group()
605 *
606 * Delete the selected group.
607 *
608 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_group_by_id()
609 * @uses \MainWP\Dashboard\MainWP_DB_Common::remove_group()
610 */
611 public static function delete_group() {
612 //phpcs:disable WordPress.Security.NonceVerification.Missing
613 $groupid = isset( $_POST['groupId'] ) && ! empty( $_POST['groupId'] ) ? intval( $_POST['groupId'] ) : false;
614 //phpcs:enable
615 if ( $groupid ) {
616 $success = MainWP_DB_Common::instance()->remove_group( $groupid );
617 if ( $success ) {
618 die( 'OK' );
619 }
620 }
621 die( 'ERROR' );
622 }
623
624 /**
625 * Method check_group_name()
626 *
627 * Check if group name already exists
628 * if it does add a number to the end of it.
629 *
630 * @param mixed $groupName Given Group Name.
631 * @param null $groupId Group ID.
632 *
633 * @return string $groupName Group name + count # if group has bn found.
634 *
635 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_group_by_name()
636 */
637 public static function check_group_name( $groupName, $groupId = null ) {
638 if ( empty( $groupName ) ) {
639 $groupName = esc_html__( 'New tag', 'mainwp' );
640 }
641
642 $groupName = esc_html( $groupName );
643
644 $cnt = null;
645 if ( preg_match( '/(.*) \(\d\)/', $groupName, $matches ) ) {
646 $groupName = $matches[1];
647 }
648
649 $group = MainWP_DB_Common::instance()->get_group_by_name( $groupName );
650 while ( $group && ( ( null === $groupId ) || ( (int) $group->id !== (int) $groupId ) ) ) {
651 if ( null === $cnt ) {
652 $cnt = 1;
653 } else {
654 ++$cnt;
655 }
656
657 $group = MainWP_DB_Common::instance()->get_group_by_name( $groupName . ' (' . $cnt . ')' );
658 }
659
660 return $groupName . ( null === $cnt ? '' : ' (' . $cnt . ')' );
661 }
662
663 /**
664 * Method add_group()
665 *
666 * Add Group.
667 *
668 * @uses \MainWP\Dashboard\MainWP_DB_Common::add_group()
669 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_group_by_id()
670 */
671 public static function add_group() {
672
673 /**
674 * Current user global.
675 *
676 * @global string
677 */
678 global $current_user;
679
680 //phpcs:disable WordPress.Security.NonceVerification.Missing
681 $newName = isset( $_POST['newName'] ) ? sanitize_text_field( wp_unslash( $_POST['newName'] ) ) : '';
682 $newColor = isset( $_POST['newColor'] ) ? sanitize_hex_color( wp_unslash( $_POST['newColor'] ) ) : '';
683 //phpcs:enable
684
685 if ( ! empty( $newName ) ) {
686 $groupId = MainWP_DB_Common::instance()->add_group( $current_user->ID, static::check_group_name( $newName ), $newColor );
687
688 /**
689 * New Group Added
690 *
691 * Fires after a new sites group has been created.
692 *
693 * @param int $groupId Group ID.
694 */
695 do_action( 'mainwp_added_new_group', $groupId );
696 $group = MainWP_DB_Common::instance()->get_group_by_id( $groupId );
697 static::create_group_item( $group );
698 die();
699 }
700 die( wp_json_encode( array( 'error' => 1 ) ) );
701 }
702
703 /**
704 * Method add_group_sites()
705 *
706 * Add Group sites.
707 *
708 * @param string $gname Group Name.
709 * @param array $site_ids Sites IDs.
710 * @param string $gcolor Tag color.
711 */
712 public static function add_group_sites( $gname, $site_ids, $gcolor = '' ) {
713 /**
714 * Current user global.
715 *
716 * @global string
717 */
718 global $current_user;
719
720 $groupId = MainWP_DB_Common::instance()->add_group( $current_user->ID, static::check_group_name( $gname ), $gcolor );
721
722 if ( $groupId ) {
723 $group = MainWP_DB_Common::instance()->get_group_by_id( $groupId );
724 if ( ! empty( $group ) && ! empty( $site_ids ) ) {
725 foreach ( $site_ids as $websiteId ) {
726 $website = MainWP_DB::instance()->get_website_by_id( $websiteId );
727 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
728 MainWP_DB_Common::instance()->update_group_site( $group->id, $website->id );
729 }
730 }
731 }
732
733 /**
734 * New Group Added
735 *
736 * Fires after a new sites group has been created.
737 *
738 * @param int $groupId Group ID.
739 */
740 do_action( 'mainwp_added_new_group', $groupId );
741 return true;
742 }
743
744 return false;
745 }
746
747 /**
748 * Method update_group()
749 *
750 * Update groups Sites.
751 *
752 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_group_by_id()
753 * @uses \MainWP\Dashboard\MainWP_DB_Common::clear_group()
754 * @uses \MainWP\Dashboard\MainWP_DB_Common::update_group_site()
755 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
756 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
757 */
758 public static function update_group() { //phpcs:ignore -- NOSONAR - complex.
759 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
760 $groupid = isset( $_POST['groupId'] ) && ! empty( $_POST['groupId'] ) ? intval( $_POST['groupId'] ) : false;
761 if ( $groupid ) {
762 $group = MainWP_DB_Common::instance()->get_group_by_id( $groupid );
763 if ( ! empty( $group ) ) {
764 MainWP_DB_Common::instance()->clear_group( $group->id );
765 if ( isset( $_POST['websiteIds'] ) ) {
766 foreach ( array_map( 'sanitize_text_field', wp_unslash( $_POST['websiteIds'] ) ) as $websiteId ) {
767 $website = MainWP_DB::instance()->get_website_by_id( $websiteId );
768 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
769 MainWP_DB_Common::instance()->update_group_site( $group->id, $website->id );
770 }
771 }
772 }
773 die( wp_json_encode( array( 'result' => true ) ) );
774 }
775 }
776 // phpcs:enable
777
778 die( wp_json_encode( array( 'result' => false ) ) );
779 }
780
781 /**
782 * Method mainwp_help_content()
783 *
784 * Creates the MainWP Help Documentation List for the help component in the sidebar.
785 */
786 public static function mainwp_help_content() {
787 if ( isset( $_GET['page'] ) && 'ManageGroups' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification
788 ?>
789 <p><?php esc_html_e( 'If you need help with managing tags, please review following help documents', 'mainwp' ); ?></p>
790 <div class="ui list">
791 <div class="item"><i class="external alternate icon"></i> <a href="https://kb.mainwp.com/docs/manage-child-sites/#manage-child-site-tags" target="_blank">Manage Tags</a></div>
792 <?php
793 /**
794 * Action: mainwp_tags_help_item
795 *
796 * Fires at the bottom of the help articles list in the Help sidebar on the Tags page.
797 *
798 * Suggested HTML markup:
799 *
800 * <div class="item"><a href="Your custom URL">Your custom text</a></div>
801 *
802 * @since 4.1
803 */
804 do_action( 'mainwp_tags_help_item' );
805 ?>
806 </div>
807 <?php
808 }
809 }
810 }
811