PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / trunk
MainWP Dashboard: Self-hosted WordPress Management for Agencies vtrunk
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 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-ui-select-sites.php

class-mainwp-ui-select-sites.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies trunk, at class/class-mainwp-ui-select-sites.php

527 lines 25.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP UI Select Sites.
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_UI_Select_Sites
17 *
18 * @package MainWP\Dashboard
19 */
20 class MainWP_UI_Select_Sites { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
21
22 /**
23 * Method get_class_name()
24 *
25 * Get Class Name.
26 *
27 * @return object
28 */
29 public static function get_class_name() {
30 return __CLASS__;
31 }
32
33 /**
34 * Method select_sites_box()
35 *
36 * Select sites box.
37 *
38 * @param array $params {
39 * An array of params for sites selection. Default empty array.
40 * @type string $type Input type, radio.
41 * @type bool $show_group Whether or not to show group, Default: true.
42 * @type bool $show_select_all Whether to show select all.
43 * @type string $class Default = ''.
44 * @type string $style Default = ''.
45 * @type array $selected_sites Selected Child Sites.
46 * @type array $selected_groups Selected Groups.
47 * @type bool $enableOfflineSites (bool) True, if offline sites is enabled. False if not.
48 * @type integer $postId Post Meta ID.
49 * @type bool $show_client (bool) True, if show clients. False if not.
50 * @type bool $enable_suspended_clients (bool) True, if enable suspended clients. False if not.
51 * @type array $selected_clients Selected Clients.
52 * }
53 *
54 * @uses \MainWP\Dashboard\MainWP_System_Utility::maybe_unserialyze()
55 */
56 public static function select_sites_box( $params = array() ) { // phpcs:ignore -- NOSONAR - complex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated.
57
58 $type = isset( $params['type'] ) ? $params['type'] : 'checkbox';
59 $show_group = isset( $params['show_group'] ) ? $params['show_group'] : true;
60 $show_select_all = isset( $params['show_select_all'] ) ? $params['show_select_all'] : true;
61 $show_selectall_disc = isset( $params['show_select_all_disconnect'] ) ? $params['show_select_all_disconnect'] : false;
62 $show_new_tag = isset( $params['show_create_tag'] ) ? $params['show_create_tag'] : true;
63 $selected_sites = isset( $params['selected_sites'] ) ? $params['selected_sites'] : array();
64 $selected_groups = isset( $params['selected_groups'] ) ? $params['selected_groups'] : array();
65 $enableOfflineSites = isset( $params['enable_offline_sites'] ) ? $params['enable_offline_sites'] : false;
66 $postId = isset( $params['post_id'] ) ? $params['post_id'] : 0;
67 $show_client = isset( $params['show_client'] ) ? $params['show_client'] : false;
68 $enableSuspendedClients = isset( $params['enable_suspended_clients'] ) ? $params['enable_suspended_clients'] : false;
69 $selected_clients = isset( $params['selected_clients'] ) ? $params['selected_clients'] : array();
70 $add_edit_client_id = isset( $params['add_edit_client_id'] ) ? $params['add_edit_client_id'] : false;
71
72 if ( $postId ) {
73
74 $sites_val = get_post_meta( $postId, '_selected_sites', true );
75 $selected_sites = MainWP_System_Utility::maybe_unserialyze( $sites_val );
76
77 if ( empty( $selected_sites ) ) {
78 $selected_sites = array();
79 }
80
81 $groups_val = get_post_meta( $postId, '_selected_groups', true );
82 $selected_groups = MainWP_System_Utility::maybe_unserialyze( $groups_val );
83
84 if ( empty( $selected_groups ) ) {
85 $selected_groups = array();
86 }
87
88 $selected_clients = get_post_meta( $postId, '_selected_clients', true );
89
90 if ( empty( $selected_clients ) ) {
91 $selected_clients = array();
92 }
93 }
94
95 if ( empty( $selected_sites ) && isset( $_GET['selected_sites'] ) && ! empty( $_GET['selected_sites'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
96 $selected_sites = explode( '-', sanitize_text_field( wp_unslash( $_GET['selected_sites'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitize ok.
97 $selected_sites = array_map( 'intval', $selected_sites );
98 $selected_sites = array_filter( $selected_sites );
99 }
100
101 /**
102 * Action: mainwp_before_seclect_sites
103 *
104 * Fires before the Select Sites box.
105 *
106 * @since 4.1
107 */
108 do_action( 'mainwp_before_seclect_sites' );
109 $params = array(
110 'type' => $type,
111 'show_group' => $show_group,
112 'show_select_all' => $show_select_all,
113 'show_select_all_disconnect' => $show_selectall_disc,
114 'show_create_tag' => $show_new_tag,
115 'selected_sites' => $selected_sites,
116 'selected_groups' => $selected_groups,
117 'enable_offline_sites' => $enableOfflineSites,
118 'post_id' => $postId,
119 'show_client' => $show_client,
120 'enable_suspended_clients' => $enableSuspendedClients,
121 'selected_clients' => $selected_clients,
122 'add_edit_client_id' => $add_edit_client_id,
123 );
124 ?>
125 <div id="mainwp-select-sites" class="mainwp_select_sites_wrapper">
126 <?php static::select_sites_box_body( $params ); ?>
127 </div>
128 <?php
129 /**
130 * Action: mainwp_after_seclect_sites
131 *
132 * Fires after the Select Sites box.
133 *
134 * @since 4.1
135 */
136 do_action( 'mainwp_after_seclect_sites' );
137 }
138
139 /**
140 * Method select_sites_box_body()
141 *
142 * Select sites box Body.
143 *
144 * @param array $params {
145 *
146 * An array of params for sites selection. Default empty array.
147 *
148 * @type string $type Input type, radio.
149 * @type bool $show_group Whether or not to show group, Default: true.
150 * @type bool $show_select_all Whether to show select all.
151 * @type array $selected_sites Selected Child Sites.
152 * @type array $selected_groups Selected Groups.
153 * @type bool $enableOfflineSites (bool) True, if offline sites is enabled. False if not.
154 * @type integer $postId Post Meta ID.
155 * @type bool $show_client (bool) True, if show clients. False if not.
156 * @type array $selected_clients Selected clients.
157 * @type bool $enable_suspended_clients (bool) True, if suspended clients is enabled. False if not.
158 * }
159 */
160 public static function select_sites_box_body( $params = array() ) { // phpcs:ignore -- NOSONAR - complex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated.
161
162 $type = isset( $params['type'] ) ? $params['type'] : 'checkbox';
163 $show_group = isset( $params['show_group'] ) ? $params['show_group'] : true;
164 $show_select_all = isset( $params['show_select_all'] ) ? $params['show_select_all'] : true;
165 $show_selectall_disc = isset( $params['show_select_all_disconnect'] ) ? $params['show_select_all_disconnect'] : false;
166 $show_new_tag = isset( $params['show_create_tag'] ) ? $params['show_create_tag'] : true;
167 $selected_sites = isset( $params['selected_sites'] ) ? $params['selected_sites'] : array();
168 $selected_groups = isset( $params['selected_groups'] ) ? $params['selected_groups'] : array();
169 $enableOfflineSites = isset( $params['enable_offline_sites'] ) ? $params['enable_offline_sites'] : false;
170 $postId = isset( $params['post_id'] ) ? $params['post_id'] : 0;
171 $show_client = isset( $params['show_client'] ) ? $params['show_client'] : false;
172 $enableSuspendedClients = isset( $params['enable_suspended_clients'] ) ? $params['enable_suspended_clients'] : false;
173 $selected_clients = isset( $params['selected_clients'] ) ? $params['selected_clients'] : array();
174 $add_edit_client_id = isset( $params['add_edit_client_id'] ) ? $params['add_edit_client_id'] : false;
175
176 if ( ! \mainwp_current_user_can( 'dashboard', 'manage_clients' ) ) {
177 $show_client = false;
178 }
179
180 if ( 'all' !== $selected_sites && ! is_array( $selected_sites ) ) {
181 $selected_sites = array();
182 }
183
184 if ( ! is_array( $selected_groups ) ) {
185 $selected_groups = array();
186 }
187
188 $selectedby = 'site';
189 if ( ! empty( $selected_groups ) ) {
190 $selectedby = 'group';
191 } elseif ( ! empty( $selected_clients ) ) {
192 $selectedby = 'client';
193 }
194
195 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.name' ) );
196 $groups = MainWP_DB_Common::instance()->get_not_empty_groups( null, $enableOfflineSites );
197
198 // support staging extension.
199 $staging_enabled = is_plugin_active( 'mainwp-staging-extension/mainwp-staging-extension.php' ) || is_plugin_active( 'mainwp-timecapsule-extension/mainwp-timecapsule-extension.php' );
200
201 $edit_site_id = false;
202 if ( $postId ) {
203 $edit_site_id = get_post_meta( $postId, '_mainwp_edit_post_site_id', true );
204 $edit_site_id = intval( $edit_site_id );
205 }
206
207 if ( $edit_site_id ) {
208 $show_group = false;
209 }
210 // to fix layout with multi sites selector.
211 $tab_id = wp_rand();
212
213 MainWP_UI::render_select_sites_header( $tab_id, $staging_enabled, $selectedby, $show_group, $show_client );
214
215 $select_all_disconnected = '';
216
217 if ( $show_selectall_disc ) {
218 $select_all_disconnected = '<div onClick="return mainwp_ss_select_disconnected( this, true )" class="mainwp-ss-select-disconnected"><i class="square outline icon"></i> ' . esc_attr__( 'Select All Disconnected', 'mainwp' ) . '</div>';
219 $select_all_disconnected .= '<div onClick="return mainwp_ss_select_disconnected( this, false )" class="mainwp-ss-deselect-disconnected" style="display:none;padding-top:0;"><i class="check square outline icon"></i> ' . esc_attr__( 'Deselect All Disconnected', 'mainwp' ) . '</div>';
220 }
221
222 static $rendered_count = 0;
223
224 $rand_id_prefix = '';
225
226 if ( ! empty( $rendered_count ) ) {
227 $rand_id_prefix = '-' . MainWP_Utility::gen_rand_id();
228 }
229
230 ++$rendered_count;
231
232 ?>
233 <div id="mainwp-select-sites-select-all-actions<?php echo esc_attr( $rand_id_prefix ); ?>" class="mainwp-select-sites-select-all-actions">
234 <div class="ui grid">
235 <div class="ten wide middle aligned column">
236 <span class="ui small grey text"><?php esc_html_e( 'Select:', 'mainwp' ); ?></span>
237 <?php if ( $show_select_all ) : ?>
238 <a href="javascript:void(0)" onClick="return mainwp_ss_select( this, true )" class="mainwp-ss-select"><span class="ui small green text"><?php esc_html_e( 'All', 'mainwp' ); ?></span></a>
239 <span class="mainwp-ss-select ui small grey text"> · </span>
240 <a href="javascript:void(0)" onClick="return mainwp_ss_select( this, false )" class="mainwp-ss-deselect"><span class="ui small green text"><?php esc_html_e( 'None', 'mainwp' ); ?></span></a>
241 <?php endif; ?>
242 </div>
243 <div class="six wide right aligned middle aligned column">
244 <?php if ( $show_new_tag ) : ?>
245 <span class="mainwp-selection-counter-wrapper" id="mainwp-selection-counter-wrapper<?php echo esc_attr( $rand_id_prefix ); ?>" style="cursor:default;">
246 <span class="mainwp-sites-selection-counter ui small grey text">0</span> <span class="ui small grey text mainwp-selected-text"><?php esc_html_e( 'selected', 'mainwp' ); ?></span>
247 </span>
248 <?php else : ?>
249 <span class="mainwp-sites-selection-counter ui small grey text">0</span> <span class="ui small grey text"><?php esc_html_e( 'selected', 'mainwp' ); ?></span>
250 <?php endif; ?>
251 </div>
252 </div>
253 </div>
254 <?php
255 ?>
256 <div class="ui tab <?php echo 'site' === $selectedby ? 'active' : ''; ?>" data-tab="mainwp-select-sites-<?php echo esc_attr( $tab_id ); ?>" id="mainwp-select-sites">
257 <?php
258 MainWP_UI::render_select_sites( $websites, $type, $selected_sites, $enableOfflineSites, $edit_site_id, $show_select_all, $add_edit_client_id, $show_selectall_disc );
259 ?>
260 </div>
261 <?php if ( $staging_enabled ) { ?>
262 <div class="ui tab <?php echo 'staging' === $selectedby ? 'active' : ''; ?>" data-tab="mainwp-select-staging-sites-<?php echo esc_attr( $tab_id ); ?>">
263 <?php
264 MainWP_UI::render_select_sites_staging( $selected_sites, $edit_site_id, $type );
265 ?>
266 </div>
267 <?php
268 }
269
270 if ( $show_group ) {
271 ?>
272 <div class="ui tab <?php echo 'group' === $selectedby ? 'active' : ''; ?>" data-tab="mainwp-select-groups-<?php echo esc_attr( $tab_id ); ?>" id="mainwp-select-groups">
273 <?php
274 MainWP_UI::render_select_sites_group( $groups, $selected_groups, $type );
275 ?>
276 </div>
277 <?php
278 }
279
280 if ( $show_client ) {
281 $clients = MainWP_DB_Client::instance()->get_wp_client_by( 'all' );
282 $params = array(
283 'clients' => $clients,
284 'type' => $type,
285 'selected_clients' => $selected_clients,
286 'enable_suspended_clients' => $enableSuspendedClients,
287 );
288 ?>
289 <div class="ui tab <?php echo 'client' === $selectedby ? 'active' : ''; ?>" data-tab="mainwp-select-clients-<?php echo esc_attr( $tab_id ); ?>" id="mainwp-select-clients">
290 <?php
291 static::render_select_clients( $params );
292 ?>
293 </div>
294 <?php
295 }
296
297 if ( $show_new_tag ) {
298 static::render_create_tag_modal();
299 }
300 ?>
301 <script type="text/javascript">
302 jQuery( document ).ready( function () {
303 // Create a new group (Select Sites UI)
304 jQuery( document ).on( 'click', '.mainwp-save-new-tag-button', function () {
305 let newName = jQuery( '.mainwp-create-group-sites-modal' ).find( 'input[name="mainwp-group-name"]' ).val().trim();
306 let newColor = jQuery( '.mainwp-create-group-sites-modal' ).find( 'input[name="mainwp-group-color"]' ).val();
307 if('' == newName ){
308 return false;
309 }
310 jQuery(this).attr('disabled', 'disabled');
311 let selected_sites = [ ];
312 jQuery( "input[name='selected_sites[]']:checked" ).each( function () {
313 selected_sites.push( jQuery( this ).val() );
314 } );
315 let data = mainwp_secure_data( {
316 action: 'mainwp_group_sites_add',
317 selected_sites: selected_sites,
318 newName: newName,
319 newColor: newColor
320 } );
321 jQuery.post( ajaxurl, data, function ( response ) {
322 try {
323 if ( response.error != undefined ){
324 jQuery('.mainwp-message-zone-tag').show().find('.ui.message').html(response.error);
325 return;
326 }
327 } catch ( err ) {
328 // to fix js error.
329 }
330 jQuery( '.mainwp-create-group-sites-modal' ).modal( 'hide' );
331 }, 'json' );
332 return false;
333 } );
334
335 if ( typeof mainwp_update_selection_counter === 'function' ) {
336 mainwp_update_selection_counter();
337 }
338
339 jQuery( document ).on( 'click', '.mainwp-selection-counter-wrapper', function() {
340 let counter = jQuery( this ).find( '.mainwp-sites-selection-counter' );
341 let count = Number.parseInt( counter.text() );
342 if ( count > 0 ) {
343 jQuery( '.mainwp-create-group-sites-modal' ).modal( {
344 onHide: function () {
345 mainwp_forceReload();
346 return false;
347 },
348 } ).modal( 'show' );
349 }
350 } );
351
352 jQuery( '#mainwp-select-sites-menu .item.tab' ).on( 'click', function() {
353 setTimeout( function() {
354 if ( typeof mainwp_update_selection_counter === 'function' ) {
355 mainwp_update_selection_counter();
356 }
357 }, 100 );
358 } );
359 } );
360 </script>
361 <?php
362 }
363
364 /**
365 * Method render_select_clients()
366 *
367 * @param array $params {
368 *
369 * An array of params for clients selection. Default empty array.
370 *
371 * @type object $clients Object containing Clients info.
372 * @type string $type Selector type.
373 * @type array $selected_clients Selected clients.
374 * @type bool $enable_suspended_clients (bool) True, if suspended clients is enabled. False if not.
375 * @type mixed $tab_id Datatab ID.
376 * }
377 *
378 * @return void Render Select Clients html.
379 */
380 public static function render_select_clients( $params = array() ) { // phpcs:ignore -- NOSONAR - complex.
381
382 $clients = isset( $params['clients'] ) ? $params['clients'] : array();
383 $type = isset( $params['type'] ) ? $params['type'] : 'checkbox';
384 $selected_clients = isset( $params['selected_clients'] ) ? $params['selected_clients'] : array();
385 $enableSuspendedClients = isset( $params['enable_suspended_clients'] ) ? $params['enable_suspended_clients'] : false;
386
387 if ( ! is_array( $clients ) ) {
388 $clients = array();
389 }
390
391 if ( ! is_array( $selected_clients ) && ( 'all' !== $selected_clients ) ) {
392 $selected_clients = array();
393 }
394
395 /**
396 * Action: mainwp_before_select_clients_list
397 *
398 * Fires before the Select Clients list.
399 *
400 * @param object $clients Object containing Clients info.
401 *
402 * @since 4.1
403 */
404 do_action( 'mainwp_before_select_clients_list', $clients );
405
406 static $rendered_count = 0;
407
408 $rand_id_prefix = '';
409
410 if ( ! empty( $rendered_count ) ) {
411 $rand_id_prefix = '-' . MainWP_Utility::gen_rand_id();
412 }
413
414 ++$rendered_count;
415
416 ?>
417 <div id="mainwp-select-sites-body<?php echo esc_attr( $rand_id_prefix ); ?>" class="mainwp-select-sites-items-body">
418 <div class="ui relaxed selection list" id="mainwp-select-clients-list">
419 <?php if ( ! $clients ) : ?>
420 <h2 class="ui icon header">
421 <i class="folder open outline icon"></i>
422 <div class="content"><?php esc_html_e( 'No Clients!', 'mainwp' ); ?></div>
423 <div class="ui divider hidden"></div>
424 <a href="admin.php?page=ClientAddNew" class="ui green button basic"><?php esc_html_e( 'Add Client', 'mainwp' ); ?></a>
425 </h2>
426 <?php
427 else :
428 foreach ( $clients as $client ) {
429 $selected = false;
430 if ( 0 === (int) $client->suspended || $enableSuspendedClients ) {
431 $selected = ( 'all' === $selected_clients || in_array( $client->client_id, $selected_clients ) );
432 ?>
433 <div title="<?php echo esc_html( $client->name ); ?>" class="mainwp_selected_clients_item ui <?php echo esc_html( $type ); ?> item <?php echo $selected ? 'selected_clients_item_checked' : ''; ?>">
434 <input type="<?php echo esc_attr( $type ); ?>" name="<?php echo 'radio' === $type ? 'selected_clients' : 'selected_clients[]'; ?>" siteid="<?php echo intval( $client->client_id ); ?>" value="<?php echo intval( $client->client_id ); ?>" id="selected_clients_<?php echo intval( $client->client_id ); ?>" <?php echo $selected ? 'checked="true"' : ''; ?> />
435 <label for="selected_clients_<?php echo intval( $client->client_id ); ?>">
436 <span class="client-contact-name"><?php echo esc_html( $client->name ); ?></span>
437 </label>
438 </div>
439 <?php
440 } else {
441 ?>
442 <div title="<?php echo esc_html( $client->name ); ?>" class="mainwp_selected_clients_item item ui <?php echo esc_html( $type ); ?> <?php echo $selected ? 'selected_clients_item_checked' : ''; ?>">
443 <input type="<?php echo esc_html( $type ); ?>" disabled="disabled"/>
444 <label for="selected_clients_<?php echo intval( $client->client_id ); ?>">
445 <span class="client-contact-name"><?php echo esc_html( stripslashes( $client->name ) ); ?></span>
446 </label>
447 </div>
448 <?php
449 }
450 }
451 endif;
452 ?>
453 </div>
454 </div>
455 <?php
456 /**
457 * Action: mainwp_after_select_clients_list
458 *
459 * Fires after the Select Clients list.
460 *
461 * @param object $clients Object containing Clients info.
462 *
463 * @since 4.1
464 */
465 do_action( 'mainwp_after_select_clients_list', $clients );
466 }
467
468 /**
469 * Method render_create_tag_modal()
470 *
471 * Renders the Create Tag modal.
472 */
473 public static function render_create_tag_modal() {
474
475 static $rendered_count = 0;
476
477 $rand_id_prefix = '';
478
479 if ( ! empty( $rendered_count ) ) {
480 $rand_id_prefix = '-' . MainWP_Utility::gen_rand_id();
481 }
482
483 ++$rendered_count;
484
485 ?>
486 <div class="ui mini modal mainwp-create-group-sites-modal" id="mainwp-create-group-sites-modal<?php echo esc_attr( $rand_id_prefix ); ?>">
487 <i class="close icon"></i>
488 <div class="header"><?php echo esc_html__( 'Create Tag', 'mainwp' ); ?></div>
489 <div class="content">
490 <div class="mainwp-message-zone-tag" style="display: none;">
491 <div class="ui message red"></div>
492 </div>
493 <div class="ui form">
494 <div class="field">
495 <label><?php esc_html_e( 'Enter tag name', 'mainwp' ); ?></label>
496 <input type="text" value="" name="mainwp-group-name" id="mainwp-group-name<?php echo esc_attr( $rand_id_prefix ); ?>">
497 </div>
498 <div class="field">
499 <label><?php esc_html_e( 'Select tag color', 'mainwp' ); ?></label>
500 <input type="color" name="mainwp-group-color" class="mainwp-color-picker-input mainwp-group-color-picker" id="mainwp-group-color<?php echo esc_attr( $rand_id_prefix ); ?>" value="#ffffff" />
501 </div>
502 </div>
503 </div>
504 <div class="actions">
505 <div class="ui two columns grid">
506 <div class="left aligned column">
507
508 </div>
509 <div class="right aligned column">
510 <a class="ui green button mainwp-save-new-tag-button" href="javascript:void(0);"><?php echo esc_html__( 'Create 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-group-color-picker {
519 height: 28px;
520 margin-left: 5px;
521 }
522 </style>
523 </div>
524 <?php
525 }
526 }
527