PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.1.6
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.1.6
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-manage-groups.php

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

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