PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.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 / class / class-mainwp-manage-sites-filter-segment.php

class-mainwp-manage-sites-filter-segment.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.2.2, at class/class-mainwp-manage-sites-filter-segment.php

357 lines 16.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Manage Sites List Table.
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_Sites_Filter_Segment
17 *
18 * @package MainWP\Dashboard
19 */
20 class MainWP_Manage_Sites_Filter_Segment { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
21
22 /**
23 * Private static variable to hold the single instance of the class.
24 *
25 * @static
26 *
27 * @var mixed Default null
28 */
29 private static $instance = null;
30
31 /**
32 * MainWP_Manage_Sites_Filter_Segment constructor.
33 *
34 * Run each time the class is called.
35 * Add action to generate tabletop.
36 */
37 public function __construct() {
38 }
39
40 /**
41 * Method admin_init().
42 */
43 public function admin_init() {
44 MainWP_Post_Handler::instance()->add_action( 'mainwp_manage_sites_filter_save_segment', array( $this, 'ajax_sites_filter_save_segment' ) );
45 MainWP_Post_Handler::instance()->add_action( 'mainwp_manage_sites_filter_load_segments', array( $this, 'ajax_sites_filter_load_segments' ) );
46 MainWP_Post_Handler::instance()->add_action( 'mainwp_manage_sites_filter_delete_segment', array( $this, 'ajax_sites_filter_delete_segment' ) );
47 }
48
49 /**
50 * Create public static instance.
51 *
52 * @static
53 *
54 * @return instance.
55 */
56 public static function get_instance() {
57 if ( null === static::$instance ) {
58 static::$instance = new self();
59 }
60 return static::$instance;
61 }
62
63 /**
64 * Method render_filters_segment().
65 */
66 public function render_filters_segment() {
67 $saved_segments = static::set_get_manage_sites_filter_segments();
68 ?>
69 <div class="four wide right aligned column">
70 <a class="ui mini button" id="mainwp-manage-sites-filter-save-segment-button" selected-segment-id="" selected-segment-name=""><?php esc_html_e( 'Save Segment', 'mainwp' ); ?></a>
71 <?php if ( ! empty( $saved_segments ) ) : ?>
72 <a class="ui mini button mainwp_manage_sites_filter_choose_segment"><?php esc_html_e( 'Load Segment', 'mainwp' ); ?></a>
73 <?php endif; ?>
74 </div>
75
76 <script type="text/javascript">
77 jQuery( document ).ready( function( $ ) {
78 mainwp_load_sites_filter_segments = function () {
79 jQuery('#mainwp-common-filter-segment-select-fields').hide();
80 let data = mainwp_secure_data({
81 action: 'mainwp_manage_sites_filter_load_segments',
82 });
83 mainwpSegmentModalUiHandle.showStatus('<i class="notched circle loading icon"></i> ' + __('Loading segments. Please wait...'));
84 jQuery.post(ajaxurl, data, function (response) {
85 if (response.error != undefined) {
86 mainwpSegmentModalUiHandle.showStatus(response.error, 'red');
87 } else if (response.result) {
88 mainwpSegmentModalUiHandle.showResults(response.result);
89 } else {
90 mainwpSegmentModalUiHandle.showStatus(__('No saved segments.'), 'red');
91 }
92 }, 'json');
93 };
94
95 jQuery('#mainwp-manage-sites-filter-save-segment-button').on( 'click', function () {
96 mainwpSegmentModalUiHandle.showSegment(this);
97 } );
98 jQuery('.mainwp_manage_sites_filter_choose_segment').on( 'click', function () {
99 mainwpSegmentModalUiHandle.loadSegment(mainwp_load_sites_filter_segments);
100 } );
101
102 jQuery('#mainwp-common-filter-edit-segment-save').on( 'click', function () {
103
104 mainwpSegmentModalUiHandle.hideSegmentStatus();
105
106 let seg_name = jQuery('#mainwp-common-filter-edit-segment-name').val().trim();
107
108 if('' == seg_name){
109 jQuery('#mainwp-common-filter-edit-segment-status').html(__('Please enter segment name.')).addClass('red').show();
110 return false;
111 }
112
113 let data = mainwp_secure_data({
114 action: 'mainwp_manage_sites_filter_save_segment',
115 name: seg_name,
116 seg_is_not: $( '#mainwp-sites-filters-row #mainwp_is_not_site').dropdown('get value'),
117 seg_site_tags: $( '#mainwp-sites-filters-row #mainwp-filter-sites-group').dropdown('get value'),
118 seg_site_tags_logic: $( '#mainwp-sites-filters-row #mainwp-filter-sites-group-logic').dropdown('get value'),
119 seg_site_status: $( '#mainwp-sites-filters-row #mainwp-filter-sites-status').dropdown('get value'),
120 seg_site_clients: $( '#mainwp-sites-filters-row #mainwp-filter-clients').dropdown('get value'),
121 seg_id:$('#mainwp-manage-sites-filter-save-segment-button').attr('selected-segment-id'),
122 });
123
124 jQuery('#mainwp-common-filter-edit-segment-status').html('<i class="notched circle loading icon"></i> ' + __('Saving segment. Please wait...')).show();
125
126 jQuery.post(ajaxurl, data, function (response) {
127 if (response.error != undefined) {
128 jQuery('#mainwp-common-filter-edit-segment-status').html(response.error).addClass('red');
129 } else if (response.result == 'SUCCESS') {
130 jQuery('#mainwp-common-filter-edit-segment-status').html(__('Segment saved successfully.')).addClass('green');
131 setTimeout(function () {
132 jQuery('#mainwp-common-filter-edit-segment-status').fadeOut(300);
133 jQuery( '#mainwp-common-filter-segment-modal' ).modal('hide');
134 }, 2000);
135 } else {
136 jQuery('#mainwp-common-filter-edit-segment-status').html(__('Undefined error occurred while saving your segment!')).addClass('red');
137 }
138 }, 'json');
139 return false;
140 });
141
142 jQuery('#mainwp-common-filter-select-segment-choose-button').on( 'click', function () {
143 mainwpSegmentModalUiHandle.hideSegmentStatus();
144 let seg_id = jQuery( '#mainwp-common-filter-segment-select-fields .ui.dropdown').dropdown('get value');
145 let seg_values = '';
146 if('' != seg_id ) {
147 seg_values = jQuery( '#mainwp-common-filter-segment-select-fields select > option[value="' +seg_id+ '"]').attr('segment-filters');
148 }
149 let valErr = true;
150 let arrVal = '';
151 let fieldsAllows = [
152 'seg_is_not',
153 'seg_site_tags',
154 'seg_site_tags_logic',
155 'seg_site_status',
156 'seg_site_clients',
157 ];
158 if('' != seg_values ) {
159 try {
160 seg_values = JSON.parse(seg_values);
161 if('' != seg_values){
162 jQuery( '#mainwp-manage-sites-filter-save-segment-button' ).attr('selected-segment-id',seg_id);
163 jQuery( '#mainwp-manage-sites-filter-save-segment-button' ).attr('selected-segment-name',seg_values.name);
164
165 for (const [key, value] of Object.entries(seg_values)) {
166 try {
167 if(fieldsAllows.includes(key)){
168 jQuery( '#mainwp-sites-filters-row .ui.dropdown.' + key ).dropdown('clear');
169 arrVal = value.split(",");
170 jQuery( '#mainwp-sites-filters-row .ui.dropdown.' + key ).dropdown('set selected', arrVal);
171 }
172 } catch (err) {
173 console.log(err);
174 }
175 }
176 jQuery( '#mainwp-common-filter-segment-modal' ).modal('hide');
177 if(jQuery('.manage-sites-screenshots-filter-top').length > 0 ){
178 mainwp_screenshots_sites_filter();
179 }else {
180 mainwp_manage_sites_filter();
181 }
182 valErr = false;
183 }
184 } catch (err) {
185 console.log(err);
186 }
187 }
188 if(valErr){
189 jQuery('#mainwp-common-filter-edit-segment-status').html(__('Undefined error segment values! Please try again.')).addClass('red').show();
190 }
191 });
192
193
194 jQuery('#mainwp-common-filter-select-segment-delete-button').on( 'click', function () {
195 mainwpSegmentModalUiHandle.hideSegmentStatus();
196 let delBtn = this;
197 let seg_id = jQuery( '#mainwp-common-filter-segment-select-fields .ui.dropdown').dropdown('get value');
198 if('' == seg_id){
199 return false;
200 }
201
202 if('yes' === jQuery(delBtn).attr('running')){
203 return false;
204 }
205
206 jQuery(seg_id).attr('running', 'yes');
207 let data = mainwp_secure_data({
208 action: 'mainwp_manage_sites_filter_delete_segment',
209 seg_id: seg_id,
210 });
211 jQuery('#mainwp-common-filter-edit-segment-status').html('<i class="notched circle loading icon"></i> ' + __('Deleting segment. Please wait...')).show();
212 jQuery.post(ajaxurl, data, function (response) {
213
214 jQuery(delBtn).removeAttr('running');
215
216 if (response.error != undefined) {
217 jQuery('#mainwp-common-filter-edit-segment-status').html(response.error).addClass('red');
218 } else if (response.result == 'SUCCESS') {
219 jQuery('#mainwp-common-filter-edit-segment-status').html(__('Segment deleted successfully.')).addClass('green');
220 setTimeout(function () {
221 jQuery('#mainwp-common-filter-edit-segment-status').fadeOut(300);
222 jQuery( '#mainwp-common-filter-segment-modal' ).modal('hide');
223 }, 2000);
224 } else {
225 jQuery('#mainwp-common-filter-edit-segment-status').html(__('Undefined error occurred while deleting your segment!')).addClass('red');
226 }
227 }, 'json');
228
229 return false;
230 });
231
232 } );
233 </script>
234 <?php
235 }
236
237 /**
238 * Method set_get_manage_sites_filter_segments()
239 *
240 * @param bool $set_val Get or set value.
241 * @param array $saved_segments segments data.
242 * @param string $save_field segments data.
243 *
244 * @return array values
245 */
246 public static function set_get_manage_sites_filter_segments( $set_val = false, $saved_segments = array(), $save_field = 'manage_sites' ) {
247 global $current_user;
248
249 $field = 'mainwp_' . sanitize_text_field( $save_field ) . '_filter_saved_segments';
250
251 if ( $current_user && ! empty( $current_user->ID ) ) {
252 if ( $set_val ) {
253 update_user_option( $current_user->ID, $field, $saved_segments );
254 } else {
255 $values = get_user_option( $field ); // fix incorrect param.
256 if ( ! is_array( $values ) ) {
257 $values = array();
258 }
259 return $values;
260 }
261 }
262 return array();
263 }
264
265
266 /**
267 * Method ajax_sites_filter_save_segment()
268 *
269 * Post handler for save segment.
270 */
271 public function ajax_sites_filter_save_segment() {
272 MainWP_Post_Handler::instance()->check_security( 'mainwp_manage_sites_filter_save_segment' );
273 //phpcs:disable WordPress.Security.NonceVerification.Missing
274
275 $not_filters = array(
276 'seg_site_tags' => 'nogroups',
277 'seg_site_tags_logic' => 'or',
278 'seg_site_status' => 'all',
279 'seg_site_clients' => 'noclients',
280 );
281
282 $fields = array(
283 'name',
284 'seg_is_not',
285 'seg_site_tags',
286 'seg_site_tags_logic',
287 'seg_site_status',
288 'seg_site_clients',
289 );
290
291 $save_fields = array();
292
293 foreach ( $fields as $field ) {
294 if ( isset( $_POST[ $field ] ) ) {
295 $val_seg = sanitize_text_field( wp_unslash( $_POST[ $field ] ) );
296 if ( isset( $not_filters[ $field ] ) && false !== strpos( $val_seg, $not_filters[ $field ] ) ) {
297 $val_seg = '';
298 }
299 $save_fields[ $field ] = $val_seg;
300 }
301 }
302
303 $seg_id = ! empty( $_POST['seg_id'] ) ? sanitize_text_field( wp_unslash( $_POST['seg_id'] ) ) : time();
304 //phpcs:enable WordPress.Security.NonceVerification.Missing
305
306 $saved_segments = static::set_get_manage_sites_filter_segments();
307 if ( ! is_array( $saved_segments ) ) {
308 $saved_segments = array();
309 }
310 $saved_segments[ $seg_id ] = $save_fields;
311 static::set_get_manage_sites_filter_segments( true, $saved_segments );
312 die( wp_json_encode( array( 'result' => 'SUCCESS' ) ) );
313 }
314
315
316 /**
317 * Method ajax_sites_filter_load_segments()
318 *
319 * Post handler for save segment.
320 */
321 public function ajax_sites_filter_load_segments() {
322 MainWP_Post_Handler::instance()->check_security( 'mainwp_manage_sites_filter_load_segments' );
323 $saved_segments = static::set_get_manage_sites_filter_segments();
324 $list_segs = '';
325 if ( is_array( $saved_segments ) && ! empty( $saved_segments ) ) {
326 $list_segs .= '<select id="mainwp-edit-segment-filters" class="ui fluid dropdown">';
327 $list_segs .= '<option segment-filters="" value="">' . esc_html__( 'Select a segment', 'mainwp' ) . '</option>';
328 foreach ( $saved_segments as $sid => $values ) {
329 if ( empty( $values['name'] ) ) {
330 continue;
331 }
332 $list_segs .= '<option segment-filters="' . esc_attr( wp_json_encode( $values ) ) . '" value="' . esc_attr( $sid ) . '">' . esc_html( $values['name'] ) . '</option>';
333 }
334 $list_segs .= '</select>';
335 }
336 die( wp_json_encode( array( 'result' => $list_segs ) ) ); //phpcs:ignore -- ok.
337 }
338
339 /**
340 * Method ajax_sites_filter_delete_segment()
341 *
342 * Post handler for save segment.
343 */
344 public function ajax_sites_filter_delete_segment() {
345 MainWP_Post_Handler::instance()->check_security( 'mainwp_manage_sites_filter_delete_segment' );
346 $seg_id = ! empty( $_POST['seg_id'] ) ? sanitize_text_field( wp_unslash( $_POST['seg_id'] ) ) : 0; //phpcs:ignore -- ok.
347
348 $saved_segments = static::set_get_manage_sites_filter_segments();
349 if ( ! empty( $seg_id ) && is_array( $saved_segments ) && isset( $saved_segments[ $seg_id ] ) ) {
350 unset( $saved_segments[ $seg_id ] );
351 static::set_get_manage_sites_filter_segments( true, $saved_segments );
352 die( wp_json_encode( array( 'result' =>'SUCCESS' ) ) ); //phpcs:ignore -- ok.
353 }
354 die( wp_json_encode( array( 'error' => esc_html__( 'Segment not found. Please try again.', 'mainwp' ) ) ) ); //phpcs:ignore -- ok.
355 }
356 }
357