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-manage-groups.php

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

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