PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0.2
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.0.2, at pages/page-mainwp-manage-groups.php

818 lines 27.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 {
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( self::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( self::get_class_name(), 'render_footer' ) );
62
63 add_action( 'mainwp_help_sidebar_content', array( self::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 self::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 self::$subPages = apply_filters( 'mainwp_getsubpages_tags', self::$subPages );
91
92 self::init_left_menu( self::$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( self::$subPages ) && is_array( self::$subPages ) ) {
166 foreach ( self::$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 self::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>
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>
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>
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>' ); ?></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 self::render_groups_sites_table_element(); ?>
328 <?php else : ?>
329 <?php self::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 self::render_groups_menu_element(); ?>
335 <?php else : ?>
336 <?php self::render_groups_sites_table_element(); ?>
337 <?php endif; ?>
338 </div>
339 <script type="text/javascript">
340 var 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 } );
362 } );
363 </script>
364 </div>
365 <?php MainWP_UI::render_modal_edit_notes(); ?>
366 <div class="ui mini modal" id="mainwp-create-group-modal">
367 <i class="close icon"></i>
368 <div class="header"><?php echo esc_html__( 'Create Tag', 'mainwp' ); ?></div>
369 <div class="content">
370 <div class="ui form">
371 <div class="field">
372 <label><?php esc_html_e( 'Enter tag name', 'mainwp' ); ?></label>
373 <input type="text" value="" name="mainwp-group-name" id="mainwp-group-name">
374 </div>
375 <div class="field">
376 <label><?php esc_html_e( 'Select tag color', 'mainwp' ); ?></label>
377 <input type="text" name="mainwp-new-tag-color" class="mainwp-tag-color-picker" id="mainwp-new-tag-color" value="" />
378 </div>
379 </div>
380 </div>
381 <div class="actions">
382 <div class="ui two columns grid">
383 <div class="left aligned column">
384
385 </div>
386 <div class="right aligned column">
387 <a class="ui green button" id="mainwp-save-new-group-button" href="#"><?php echo esc_html__( 'Create Tag', 'mainwp' ); ?></a>
388 </div>
389 </div>
390 </div>
391 <style>
392 .mainwp-ui .ui.modal .wp-picker-clear {
393 display:none;
394 }
395 .mainwp-ui .ui.modal #mainwp-new-tag-color {
396 height: 28px;
397 margin-left: 5px;
398 }
399 </style>
400 <script type="text/javascript">
401 jQuery( document ).ready( function() {
402 jQuery('.mainwp-tag-color-picker').wpColorPicker({
403 hide: true,
404 clear: false,
405 palettes: [ '#18a4e0','#0253b3','#7fb100','#446200','#ad0000','#ffd300','#2d3b44','#6435c9','#e03997','#00b5ad' ],
406 });
407 } );
408 </script>
409 </div>
410
411 <div class="ui mini modal" id="mainwp-rename-group-modal">
412 <i class="close icon"></i>
413 <div class="header"><?php echo esc_html__( 'Rename Tag', 'mainwp' ); ?></div>
414 <div class="content">
415 <div class="ui form">
416 <div class="field">
417 <label><?php esc_html_e( 'Enter tag name', 'mainwp' ); ?></label>
418 <input type="text" value="" name="mainwp-group-name" id="mainwp-group-name">
419 </div>
420 <div class="field">
421 <label><?php esc_html_e( 'Select tag color', 'mainwp' ); ?></label>
422 <input type="text" name="mainwp-new-tag-color" class="mainwp-tag-color-picker" id="mainwp-new-tag-color" value="" />
423 </div>
424 </div>
425 </div>
426 <div class="actions">
427 <div class="ui two columns stackable grid">
428 <div class="left aligned column">
429
430 </div>
431 <div class="right aligned column">
432 <a class="ui green button" id="mainwp-update-new-group-button" href="#"><?php echo esc_html__( 'Update Tag', 'mainwp' ); ?></a>
433 </div>
434 </div>
435 </div>
436 <style>
437 .mainwp-ui .ui.modal .wp-picker-clear {
438 display:none;
439 }
440 .mainwp-ui .ui.modal #mainwp-new-tag-color {
441 height: 28px;
442 margin-left: 5px;
443 }
444 </style>
445 <script type="text/javascript">
446 jQuery( document ).ready( function() {
447 jQuery('.mainwp-tag-color-picker').wpColorPicker({
448 hide: true,
449 clear: false,
450 palettes: [ '#18a4e0','#0253b3','#7fb100','#446200','#ad0000','#ffd300','#2d3b44','#6435c9','#e03997','#00b5ad' ],
451 });
452 } );
453 </script>
454 </div>
455 <?php
456 /**
457 * Action: mainwp_after_groups_table
458 *
459 * Fires after the Manage Groups table.
460 *
461 * @since 4.1
462 */
463 do_action( 'mainwp_after_groups_table' );
464 ?>
465 </div>
466 <?php
467 /**
468 * Sites Page Footer
469 *
470 * Renders the footer on the Sites screen.
471 *
472 * @since Unknown
473 */
474 do_action( 'mainwp_pagefooter_tags', 'ManageGroups' );
475 }
476
477 /**
478 * Method render_groups_menu_element()
479 *
480 * Render the groups menu HTML element.
481 */
482 public static function render_groups_menu_element() {
483 $sidebarPosition = get_user_option( 'mainwp_sidebarPosition' );
484 if ( false === $sidebarPosition ) {
485 $sidebarPosition = 1;
486 }
487 ?>
488 <div class="ui fluid <?php echo 1 === (int) $sidebarPosition ? 'right' : ''; ?> pointing vertical menu sticky" id="mainwp-groups-menu" style="margin-top:52px">
489 <h4 class="item ui header"><?php esc_html_e( 'Tags', 'mainwp' ); ?></h4>
490 <?php self::get_group_list_content(); ?>
491 <div class="item">
492 <div class="ui two columns stackable grid">
493 <div class="left aligned column">
494 <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>
495 </div>
496 <div class="right aligned column">
497 <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>
498 <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>
499 </div>
500 </div>
501 </div>
502 </div>
503 <?php
504 }
505
506 /**
507 * Method render_groups_sites_table_element()
508 *
509 * Render the groups menu HTML element.
510 */
511 public static function render_groups_sites_table_element() {
512 ?>
513 <table class="ui very compact table mainwp-with-preview-table" id="mainwp-manage-groups-sites-table">
514 <thead>
515 <tr>
516 <th class="no-sort collapsing"><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" name="example"></div></th>
517 <th><?php esc_html_e( 'Sites', 'mainwp' ); ?></th>
518 <th class="no-sort collapsing"><i class="sign in icon"></i></th>
519 <th><?php esc_html_e( 'URL', 'mainwp' ); ?></th>
520 <th><?php esc_html_e( 'Client', 'mainwp' ); ?></th>
521 <th class="no-sort collapsing"><i class="camera icon"></i></th>
522 <th class="no-sort collapsing"><i class="sticky note outline icon"></i></th>
523 </tr>
524 </thead>
525 <tbody>
526 <?php self::get_website_list_content(); ?>
527 </tbody>
528 <tfoot>
529 <tr>
530 <th colspan="7">
531 <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>
532 </th>
533 </tr>
534 </tfoot>
535 </table>
536 <div class="ui inverted dimmer">
537 <div class="ui loader"></div>
538 </div>
539 <?php
540 }
541
542 /**
543 * Method rename_group()
544 *
545 * Rename the selected group.
546 *
547 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_group_by_id()
548 * @uses \MainWP\Dashboard\MainWP_DB_Common::update_group()
549 */
550 public static function rename_group() {
551 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
552 if ( isset( $_POST['groupId'] ) ) {
553 $group = MainWP_DB_Common::instance()->get_group_by_id( intval( $_POST['groupId'] ) );
554 if ( ! empty( $group ) ) {
555 $old_name = $group->name;
556 $name = isset( $_POST['newName'] ) ? sanitize_text_field( wp_unslash( $_POST['newName'] ) ) : '';
557 if ( empty( $name ) ) {
558 $name = $group->name;
559 }
560
561 $name = self::check_group_name( $name, $group->id );
562
563 if ( isset( $_POST['newColor'] ) && ! empty( $_POST['newColor'] ) ) {
564 $color = sanitize_hex_color( wp_unslash( $_POST['newColor'] ) );
565 if ( empty( $color ) ) {
566 $color = $group->color;
567 }
568 } else {
569 $color = '';
570 }
571
572 // update group.
573 $nr = MainWP_DB_Common::instance()->update_group( $group->id, $name, $color );
574
575 // Reload group.
576 $group = MainWP_DB_Common::instance()->get_group_by_id( $group->id );
577
578 $data = array(
579 'old_name' => $old_name,
580 );
581
582 /**
583 * Fires after a new sites tag has been created.
584 *
585 * @param object $group tag created.
586 * @param string tag action.
587 * @param array other data array.
588 */
589 do_action( 'mainwp_site_tag_action', $group, 'updated', $data );
590
591 die( wp_json_encode( array( 'result' => $group->name ) ) );
592 }
593 }
594 //phpcs:enable
595 }
596
597 /**
598 * Method delete_group()
599 *
600 * Delete the selected group.
601 *
602 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_group_by_id()
603 * @uses \MainWP\Dashboard\MainWP_DB_Common::remove_group()
604 */
605 public static function delete_group() {
606 //phpcs:disable WordPress.Security.NonceVerification.Missing
607 $groupid = isset( $_POST['groupId'] ) && ! empty( $_POST['groupId'] ) ? intval( $_POST['groupId'] ) : false;
608 //phpcs:enable
609 if ( $groupid ) {
610 $group = MainWP_DB_Common::instance()->get_group_by_id( $groupid );
611 if ( ! empty( $group ) ) {
612 // Remove from DB.
613 $nr = MainWP_DB_Common::instance()->remove_group( $group->id );
614
615 /**
616 * Fires after a tag has been deleted.
617 *
618 * @param object $group group created.
619 * @param string group action.
620 */
621 do_action( 'mainwp_site_tag_action', $group, 'deleted' );
622
623 if ( $nr > 0 ) {
624 die( 'OK' );
625 }
626 }
627 }
628 die( 'ERROR' );
629 }
630
631 /**
632 * Method check_group_name()
633 *
634 * Check if group name already exists
635 * if it does add a number to the end of it.
636 *
637 * @param mixed $groupName Given Group Name.
638 * @param null $groupId Group ID.
639 *
640 * @return string $groupName Group name + count # if group has bn found.
641 *
642 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_group_by_name()
643 */
644 public static function check_group_name( $groupName, $groupId = null ) {
645 if ( empty( $groupName ) ) {
646 $groupName = esc_html__( 'New tag', 'mainwp' );
647 }
648
649 $groupName = esc_html( $groupName );
650
651 $cnt = null;
652 if ( preg_match( '/(.*) \(\d\)/', $groupName, $matches ) ) {
653 $groupName = $matches[1];
654 }
655
656 $group = MainWP_DB_Common::instance()->get_group_by_name( $groupName );
657 while ( $group && ( ( null === $groupId ) || ( (int) $group->id !== (int) $groupId ) ) ) {
658 if ( null === $cnt ) {
659 $cnt = 1;
660 } else {
661 ++$cnt;
662 }
663
664 $group = MainWP_DB_Common::instance()->get_group_by_name( $groupName . ' (' . $cnt . ')' );
665 }
666
667 return $groupName . ( null === $cnt ? '' : ' (' . $cnt . ')' );
668 }
669
670 /**
671 * Method add_group()
672 *
673 * Add Group.
674 *
675 * @uses \MainWP\Dashboard\MainWP_DB_Common::add_group()
676 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_group_by_id()
677 */
678 public static function add_group() {
679
680 /**
681 * Current user global.
682 *
683 * @global string
684 */
685 global $current_user;
686
687 //phpcs:disable WordPress.Security.NonceVerification.Missing
688 $newName = isset( $_POST['newName'] ) ? sanitize_text_field( wp_unslash( $_POST['newName'] ) ) : '';
689 $newColor = isset( $_POST['newColor'] ) ? sanitize_hex_color( wp_unslash( $_POST['newColor'] ) ) : '';
690 //phpcs:enable
691
692 if ( ! empty( $newName ) ) {
693 $groupId = MainWP_DB_Common::instance()->add_group( $current_user->ID, self::check_group_name( $newName ), $newColor );
694
695 /**
696 * New Group Added
697 *
698 * Fires after a new sites group has been created.
699 *
700 * @param int $groupId Group ID.
701 */
702 do_action( 'mainwp_added_new_group', $groupId );
703 $group = MainWP_DB_Common::instance()->get_group_by_id( $groupId );
704 self::create_group_item( $group );
705 die();
706 }
707 die( wp_json_encode( array( 'error' => 1 ) ) );
708 }
709
710 /**
711 * Method add_group_sites()
712 *
713 * Add Group sites.
714 *
715 * @param string $gname Group Name.
716 * @param array $site_ids Sites IDs.
717 * @param string $gcolor Tag color.
718 */
719 public static function add_group_sites( $gname, $site_ids, $gcolor = '' ) {
720 /**
721 * Current user global.
722 *
723 * @global string
724 */
725 global $current_user;
726
727 $groupId = MainWP_DB_Common::instance()->add_group( $current_user->ID, self::check_group_name( $gname ), $gcolor );
728
729 if ( $groupId ) {
730 $group = MainWP_DB_Common::instance()->get_group_by_id( $groupId );
731 if ( ! empty( $group ) ) {
732 if ( ! empty( $site_ids ) ) {
733 foreach ( $site_ids as $websiteId ) {
734 $website = MainWP_DB::instance()->get_website_by_id( $websiteId );
735 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
736 MainWP_DB_Common::instance()->update_group_site( $group->id, $website->id );
737 }
738 }
739 }
740 }
741
742 /**
743 * New Group Added
744 *
745 * Fires after a new sites group has been created.
746 *
747 * @param int $groupId Group ID.
748 */
749 do_action( 'mainwp_added_new_group', $groupId );
750 return true;
751 }
752
753 return false;
754 }
755
756 /**
757 * Method update_group()
758 *
759 * Update groups Sites.
760 *
761 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_group_by_id()
762 * @uses \MainWP\Dashboard\MainWP_DB_Common::clear_group()
763 * @uses \MainWP\Dashboard\MainWP_DB_Common::update_group_site()
764 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
765 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
766 */
767 public static function update_group() {
768 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
769 $groupid = isset( $_POST['groupId'] ) && ! empty( $_POST['groupId'] ) ? intval( $_POST['groupId'] ) : false;
770 if ( $groupid ) {
771 $group = MainWP_DB_Common::instance()->get_group_by_id( $groupid );
772 if ( ! empty( $group ) ) {
773 MainWP_DB_Common::instance()->clear_group( $group->id );
774 if ( isset( $_POST['websiteIds'] ) ) {
775 foreach ( array_map( 'sanitize_text_field', wp_unslash( $_POST['websiteIds'] ) ) as $websiteId ) {
776 $website = MainWP_DB::instance()->get_website_by_id( $websiteId );
777 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
778 MainWP_DB_Common::instance()->update_group_site( $group->id, $website->id );
779 }
780 }
781 }
782 die( wp_json_encode( array( 'result' => true ) ) );
783 }
784 }
785 // phpcs:enable
786
787 die( wp_json_encode( array( 'result' => false ) ) );
788 }
789
790 /**
791 * Hooks the section help content to the Help Sidebar element.
792 */
793 public static function mainwp_help_content() {
794 if ( isset( $_GET['page'] ) && 'ManageGroups' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification
795 ?>
796 <p><?php esc_html_e( 'If you need help with managing tags, please review following help documents', 'mainwp' ); ?></p>
797 <div class="ui relaxed bulleted list">
798 <div class="item"><a href="" target="_blank">Manage Tags</a></div>
799 <?php
800 /**
801 * Action: mainwp_tags_help_item
802 *
803 * Fires at the bottom of the help articles list in the Help sidebar on the Users page.
804 *
805 * Suggested HTML markup:
806 *
807 * <div class="item"><a href="Your custom URL">Your custom text</a></div>
808 *
809 * @since 4.1
810 */
811 do_action( 'mainwp_tags_help_item' );
812 ?>
813 </div>
814 <?php
815 }
816 }
817 }
818