| 1 |
<?php |
| 2 |
/** |
| 3 |
* Manage filter segment Logs List Table. |
| 4 |
* |
| 5 |
* @package MainWP/Dashboard |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace MainWP\Dashboard\Module\Log; |
| 9 |
|
| 10 |
use MainWP\Dashboard\MainWP_Manage_Sites_Filter_Segment; |
| 11 |
use MainWP\Dashboard\MainWP_Post_Handler; |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
/** |
| 20 |
* Class Log_Events_Filter_Segment |
| 21 |
* |
| 22 |
* @package MainWP\Dashboard |
| 23 |
*/ |
| 24 |
class Log_Events_Filter_Segment { |
| 25 |
|
| 26 |
/** |
| 27 |
* Private static variable to hold the single instance of the class. |
| 28 |
* |
| 29 |
* @static |
| 30 |
* |
| 31 |
* @var mixed Default null |
| 32 |
*/ |
| 33 |
private static $instance = null; |
| 34 |
|
| 35 |
/** |
| 36 |
* MainWP_Manage_Sites_Filter_Segment constructor. |
| 37 |
* |
| 38 |
* Run each time the class is called. |
| 39 |
* Add action to generate tabletop. |
| 40 |
*/ |
| 41 |
public function __construct() { |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Create public static instance. |
| 46 |
* |
| 47 |
* @static |
| 48 |
* |
| 49 |
* @return instance. |
| 50 |
*/ |
| 51 |
public static function get_instance() { |
| 52 |
if ( null === static::$instance ) { |
| 53 |
static::$instance = new self(); |
| 54 |
} |
| 55 |
return static::$instance; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Method admin_init(). |
| 60 |
*/ |
| 61 |
public function admin_init() { |
| 62 |
MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_filter_save_segment', array( $this, 'ajax_log_filter_save_segment' ) ); |
| 63 |
MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_filter_load_segments', array( $this, 'ajax_log_filter_load_segments' ) ); |
| 64 |
MainWP_Post_Handler::instance()->add_action( 'mainwp_module_log_filter_delete_segment', array( $this, 'ajax_log_filter_delete_segment' ) ); |
| 65 |
} |
| 66 |
|
| 67 |
// @NO_SONAR_START@ - duplicated issue. |
| 68 |
/** |
| 69 |
* Method render_filters_segment(). |
| 70 |
* |
| 71 |
* @param string $filter_name Filter name. |
| 72 |
*/ |
| 73 |
public function render_filters_segment( $filter_name = 'module_log' ) { |
| 74 |
$saved_segments = MainWP_Manage_Sites_Filter_Segment::set_get_manage_sites_filter_segments( false, array(), $filter_name ); |
| 75 |
?> |
| 76 |
<input type="hidden" id="mainwp-common-filter-option-name" value="<?php echo esc_attr( $filter_name ); ?>"/> |
| 77 |
<button class="ui mini green button" id="mainwp-module-log-filter-save-segment-button" selected-segment-id="" selected-segment-name=""><?php esc_html_e( 'Save Segment', 'mainwp' ); ?></button> |
| 78 |
<?php if ( ! empty( $saved_segments ) ) : ?> |
| 79 |
<button class="ui mini button mainwp_module_log_filter_choose_segment"><?php esc_html_e( 'Load Segment', 'mainwp' ); ?></button> |
| 80 |
<?php endif; ?> |
| 81 |
|
| 82 |
<script type="text/javascript"> |
| 83 |
jQuery( document ).ready( function( $ ) { |
| 84 |
mainwp_load_logs_filter_segments = function () { |
| 85 |
jQuery('#mainwp-common-filter-segment-select-fields').hide(); |
| 86 |
var data = mainwp_secure_data({ |
| 87 |
action: 'mainwp_module_log_filter_load_segments', |
| 88 |
filter_opt_name: jQuery('#mainwp-common-filter-option-name').val().trim(), |
| 89 |
}); |
| 90 |
jQuery('#mainwp-common-filter-edit-segment-status').html('<i class="notched circle loading icon"></i> ' + __('Loading segments. Please wait...')).show(); |
| 91 |
jQuery.post(ajaxurl, data, function (response) { |
| 92 |
if (response.error != undefined) { |
| 93 |
jQuery('#mainwp-common-filter-edit-segment-status').html(response.error).addClass('red'); |
| 94 |
} else if (response.result) { |
| 95 |
mainwpSegmentModalUiHandle.showResults(response.result); |
| 96 |
} else { |
| 97 |
jQuery('#mainwp-common-filter-edit-segment-status').html(__('No saved segments.')).addClass('red'); |
| 98 |
} |
| 99 |
}, 'json'); |
| 100 |
}; |
| 101 |
|
| 102 |
jQuery('#mainwp-module-log-filter-save-segment-button').on( 'click', function () { |
| 103 |
mainwpSegmentModalUiHandle.showSegment(this); |
| 104 |
} ); |
| 105 |
jQuery('.mainwp_module_log_filter_choose_segment').on( 'click', function () { |
| 106 |
mainwpSegmentModalUiHandle.loadSegment( mainwp_load_logs_filter_segments ); |
| 107 |
} ); |
| 108 |
|
| 109 |
jQuery('#mainwp-common-filter-edit-segment-save').on( 'click', function () { |
| 110 |
|
| 111 |
mainwpSegmentModalUiHandle.hideSegmentStatus(); |
| 112 |
|
| 113 |
var seg_name = jQuery('#mainwp-common-filter-edit-segment-name').val().trim(); |
| 114 |
|
| 115 |
if('' == seg_name){ |
| 116 |
jQuery('#mainwp-common-filter-edit-segment-status').html(__('Please enter segment name.')).addClass('red').show(); |
| 117 |
return false; |
| 118 |
} |
| 119 |
const filter_opt = jQuery('#mainwp-common-filter-option-name').val().trim(); |
| 120 |
var data = mainwp_secure_data({ |
| 121 |
action: 'mainwp_module_log_filter_save_segment', |
| 122 |
name: seg_name, |
| 123 |
filter_opt_name:filter_opt, |
| 124 |
seg_ranges: $( '#mainwp-module-log-filter-ranges').dropdown('get value'), |
| 125 |
seg_dtsstart: $( '#mainwp-module-log-filter-dtsstart input[type=text]').val(), |
| 126 |
seg_dtsstop: $( '#mainwp-module-log-filter-dtsstop input[type=text]').val(), |
| 127 |
seg_groups: $( '#mainwp-module-log-filter-groups').dropdown('get value'), |
| 128 |
seg_clients:$('#mainwp-module-log-filter-clients').dropdown('get value'), |
| 129 |
seg_users:$('#mainwp-module-log-filter-users').dropdown('get value'), |
| 130 |
}); |
| 131 |
|
| 132 |
if('module_log_manage' === filter_opt){ |
| 133 |
data.seg_source =$('#mainwp-module-log-filter-source').dropdown('get value'); |
| 134 |
data.seg_sites =$('#mainwp-module-log-filter-sites').dropdown('get value'); |
| 135 |
data.seg_events =$('#mainwp-module-log-filter-events').dropdown('get value'); |
| 136 |
} |
| 137 |
|
| 138 |
jQuery('#mainwp-common-filter-edit-segment-status').html('<i class="notched circle loading icon"></i> ' + __('Saving segment. Please wait...')).show(); |
| 139 |
|
| 140 |
jQuery.post(ajaxurl, data, function (response) { |
| 141 |
if (response.error != undefined) { |
| 142 |
jQuery('#mainwp-common-filter-edit-segment-status').html(response.error).addClass('red'); |
| 143 |
} else if (response.result == 'SUCCESS') { |
| 144 |
jQuery('#mainwp-common-filter-edit-segment-status').html(__('Segment saved successfully.')).addClass('green'); |
| 145 |
setTimeout(function () { |
| 146 |
jQuery('#mainwp-common-filter-edit-segment-status').fadeOut(300); |
| 147 |
jQuery( '#mainwp-common-filter-segment-modal' ).modal('hide'); |
| 148 |
}, 2000); |
| 149 |
} else { |
| 150 |
jQuery('#mainwp-common-filter-edit-segment-status').html(__('Undefined error occurred while saving your segment!')).addClass('red'); |
| 151 |
} |
| 152 |
}, 'json'); |
| 153 |
return false; |
| 154 |
}); |
| 155 |
|
| 156 |
jQuery('#mainwp-common-filter-select-segment-choose-button').on( 'click', function () { |
| 157 |
mainwpSegmentModalUiHandle.hideSegmentStatus(); |
| 158 |
var seg_id = jQuery( '#mainwp-common-filter-segment-select-fields .ui.dropdown').dropdown('get value'); |
| 159 |
var seg_values = ''; |
| 160 |
if('' != seg_id ) { |
| 161 |
seg_values = jQuery( '#mainwp-common-filter-segment-select-fields select > option[value="' +seg_id+ '"]').attr('segment-filters'); |
| 162 |
} |
| 163 |
var valErr = true; |
| 164 |
var arrVal = ''; |
| 165 |
var fieldsAllows = [ |
| 166 |
'seg_ranges', |
| 167 |
'seg_dtsstart', |
| 168 |
'seg_dtsstop', |
| 169 |
'seg_groups', |
| 170 |
'seg_clients', |
| 171 |
'seg_users', |
| 172 |
'seg_source', |
| 173 |
'seg_sites', |
| 174 |
'seg_events', |
| 175 |
]; |
| 176 |
|
| 177 |
if('' != seg_values ) { |
| 178 |
try { |
| 179 |
seg_values = JSON.parse(seg_values); |
| 180 |
if('' != seg_values){ |
| 181 |
jQuery( '#mainwp-module-log-filter-save-segment-button' ).attr('selected-segment-id',seg_id); |
| 182 |
jQuery( '#mainwp-module-log-filter-save-segment-button' ).attr('selected-segment-name',seg_values.name); |
| 183 |
|
| 184 |
for (const [key, value] of Object.entries(seg_values)) { |
| 185 |
try { |
| 186 |
if(fieldsAllows.includes(key)){ |
| 187 |
if( 'seg_dtsstart' !== key && 'seg_dtsstop' !== key ){ |
| 188 |
if('seg_ranges' != key){ // to fix onChange filter-ranges issue. |
| 189 |
jQuery( '#mainwp-module-log-filters-row .ui.dropdown.' + key ).dropdown('clear'); |
| 190 |
} |
| 191 |
arrVal = value.split(","); |
| 192 |
|
| 193 |
if(jQuery( '#mainwp-module-log-filters-row .ui.dropdown.' + key ).length){ |
| 194 |
jQuery( '#mainwp-module-log-filters-row .ui.dropdown.' + key ).dropdown('set selected', arrVal); |
| 195 |
} |
| 196 |
} else { |
| 197 |
jQuery( '#mainwp-module-log-filters-row .ui.calendar.' + key ).calendar('set date', value ); |
| 198 |
} |
| 199 |
} |
| 200 |
} catch (err) { |
| 201 |
console.log(err); |
| 202 |
} |
| 203 |
} |
| 204 |
jQuery( '#mainwp-common-filter-segment-modal' ).modal('hide'); |
| 205 |
mainwp_module_log_overview_content_filter(); |
| 206 |
valErr = false; |
| 207 |
} |
| 208 |
} catch (err) { |
| 209 |
console.log(err); |
| 210 |
} |
| 211 |
} |
| 212 |
if(valErr){ |
| 213 |
jQuery('#mainwp-common-filter-edit-segment-status').html(__('Undefined error segment values! Please try again.')).addClass('red').show(); |
| 214 |
} |
| 215 |
}); |
| 216 |
|
| 217 |
|
| 218 |
jQuery('#mainwp-common-filter-select-segment-delete-button').on( 'click', function () { |
| 219 |
mainwpSegmentModalUiHandle.hideSegmentStatus(); |
| 220 |
var delBtn = this; |
| 221 |
var seg_id = jQuery( '#mainwp-common-filter-segment-select-fields .ui.dropdown').dropdown('get value'); |
| 222 |
if('' == seg_id){ |
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
if('yes' === jQuery(delBtn).attr('running')){ |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
jQuery(seg_id).attr('running', 'yes'); |
| 231 |
var data = mainwp_secure_data({ |
| 232 |
action: 'mainwp_module_log_filter_delete_segment', |
| 233 |
seg_id: seg_id, |
| 234 |
filter_opt_name: jQuery('#mainwp-common-filter-option-name').val().trim(), |
| 235 |
}); |
| 236 |
jQuery('#mainwp-common-filter-edit-segment-status').html('<i class="notched circle loading icon"></i> ' + __('Deleting segment. Please wait...')).show(); |
| 237 |
jQuery.post(ajaxurl, data, function (response) { |
| 238 |
|
| 239 |
jQuery(delBtn).removeAttr('running'); |
| 240 |
|
| 241 |
if (response.error != undefined) { |
| 242 |
jQuery('#mainwp-common-filter-edit-segment-status').html(response.error).addClass('red'); |
| 243 |
} else if (response.result == 'SUCCESS') { |
| 244 |
jQuery('#mainwp-common-filter-edit-segment-status').html(__('Segment deleted successfully.')).addClass('green'); |
| 245 |
setTimeout(function () { |
| 246 |
jQuery('#mainwp-common-filter-edit-segment-status').fadeOut(300); |
| 247 |
jQuery( '#mainwp-common-filter-segment-modal' ).modal('hide'); |
| 248 |
}, 2000); |
| 249 |
} else { |
| 250 |
jQuery('#mainwp-common-filter-edit-segment-status').html(__('Undefined error occurred while deleting your segment!')).addClass('red'); |
| 251 |
} |
| 252 |
}, 'json'); |
| 253 |
|
| 254 |
return false; |
| 255 |
}); |
| 256 |
|
| 257 |
} ); |
| 258 |
</script> |
| 259 |
<?php |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Method ajax_log_filter_save_segment() |
| 264 |
* |
| 265 |
* Post handler for save segment. |
| 266 |
*/ |
| 267 |
public function ajax_log_filter_save_segment() { |
| 268 |
MainWP_Post_Handler::instance()->check_security( 'mainwp_module_log_filter_save_segment' ); |
| 269 |
|
| 270 |
//phpcs:disable WordPress.Security.NonceVerification.Missing |
| 271 |
$opt_name = isset( $_POST['filter_opt_name'] ) ? sanitize_text_field( wp_unslash( $_POST['filter_opt_name'] ) ) : 'module_log'; |
| 272 |
|
| 273 |
$not_filters = array( |
| 274 |
'seg_groups' => 'alltags', |
| 275 |
'seg_clients' => 'allclients', |
| 276 |
'seg_users' => 'allusers', |
| 277 |
); |
| 278 |
|
| 279 |
$fields = array( |
| 280 |
'name', |
| 281 |
'seg_ranges', |
| 282 |
'seg_dtsstart', |
| 283 |
'seg_dtsstop', |
| 284 |
'seg_groups', |
| 285 |
'seg_clients', |
| 286 |
'seg_users', |
| 287 |
); |
| 288 |
|
| 289 |
if ( 'module_log_manage' === $opt_name ) { |
| 290 |
$fields[] = 'seg_source'; |
| 291 |
$fields[] = 'seg_sites'; |
| 292 |
$fields[] = 'seg_events'; |
| 293 |
} |
| 294 |
|
| 295 |
$save_fields = array(); |
| 296 |
|
| 297 |
foreach ( $fields as $field ) { |
| 298 |
if ( isset( $_POST[ $field ] ) ) { |
| 299 |
$val_seg = sanitize_text_field( wp_unslash( $_POST[ $field ] ) ); |
| 300 |
if ( isset( $not_filters[ $field ] ) && false !== strpos( $val_seg, $not_filters[ $field ] ) ) { |
| 301 |
$val_seg = ''; |
| 302 |
} |
| 303 |
$save_fields[ $field ] = $val_seg; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
$seg_id = ! empty( $_POST['seg_id'] ) ? sanitize_text_field( wp_unslash( $_POST['seg_id'] ) ) : time(); |
| 308 |
//phpcs:enable WordPress.Security.NonceVerification.Missing |
| 309 |
|
| 310 |
$saved_segments = MainWP_Manage_Sites_Filter_Segment::set_get_manage_sites_filter_segments( false, array(), $opt_name ); |
| 311 |
if ( ! is_array( $saved_segments ) ) { |
| 312 |
$saved_segments = array(); |
| 313 |
} |
| 314 |
$saved_segments[ $seg_id ] = $save_fields; |
| 315 |
MainWP_Manage_Sites_Filter_Segment::set_get_manage_sites_filter_segments( true, $saved_segments, $opt_name ); |
| 316 |
die( wp_json_encode( array( 'result' => 'SUCCESS' ) ) ); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
/** |
| 321 |
* Method ajax_log_filter_load_segments() |
| 322 |
* |
| 323 |
* Post handler for save segment. |
| 324 |
*/ |
| 325 |
public function ajax_log_filter_load_segments() { |
| 326 |
MainWP_Post_Handler::instance()->check_security( 'mainwp_module_log_filter_load_segments' ); |
| 327 |
|
| 328 |
//phpcs:disable WordPress.Security.NonceVerification.Missing |
| 329 |
$opt_name = isset( $_POST['filter_opt_name'] ) ? sanitize_text_field( wp_unslash( $_POST['filter_opt_name'] ) ) : 'module_log'; |
| 330 |
|
| 331 |
$saved_segments = MainWP_Manage_Sites_Filter_Segment::set_get_manage_sites_filter_segments( false, array(), $opt_name ); |
| 332 |
$list_segs = ''; |
| 333 |
if ( is_array( $saved_segments ) && ! empty( $saved_segments ) ) { |
| 334 |
$list_segs .= '<select id="mainwp_module_log_edit_payment_type" class="ui fluid dropdown">'; |
| 335 |
$list_segs .= '<option segment-filters="" value="">' . esc_html__( 'Select a segment', 'mainwp' ) . '</option>'; |
| 336 |
foreach ( $saved_segments as $sid => $values ) { |
| 337 |
if ( empty( $values['name'] ) ) { |
| 338 |
continue; |
| 339 |
} |
| 340 |
$list_segs .= '<option segment-filters="' . esc_attr( wp_json_encode( $values ) ) . '" value="' . esc_attr( $sid ) . '">' . esc_html( $values['name'] ) . '</option>'; |
| 341 |
} |
| 342 |
$list_segs .= '</select>'; |
| 343 |
} |
| 344 |
die( wp_json_encode( array( 'result' => $list_segs ) ) ); //phpcs:ignore -- ok. |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Method ajax_log_filter_delete_segment() |
| 349 |
* |
| 350 |
* Post handler for save segment. |
| 351 |
*/ |
| 352 |
public function ajax_log_filter_delete_segment() { |
| 353 |
MainWP_Post_Handler::instance()->check_security( 'mainwp_module_log_filter_delete_segment' ); |
| 354 |
|
| 355 |
//phpcs:disable WordPress.Security.NonceVerification.Missing |
| 356 |
$opt_name = isset( $_POST['filter_opt_name'] ) ? sanitize_text_field( wp_unslash( $_POST['filter_opt_name'] ) ) : 'module_log'; |
| 357 |
|
| 358 |
$seg_id = ! empty( $_POST['seg_id'] ) ? sanitize_text_field( wp_unslash( $_POST['seg_id'] ) ) : 0; //phpcs:ignore -- ok. |
| 359 |
//phpcs:enable WordPress.Security.NonceVerification.Missing |
| 360 |
|
| 361 |
$saved_segments = MainWP_Manage_Sites_Filter_Segment::set_get_manage_sites_filter_segments( false, array(), $opt_name ); |
| 362 |
if ( ! empty( $seg_id ) && is_array( $saved_segments ) && isset( $saved_segments[ $seg_id ] ) ) { |
| 363 |
unset( $saved_segments[ $seg_id ] ); |
| 364 |
MainWP_Manage_Sites_Filter_Segment::set_get_manage_sites_filter_segments( true, $saved_segments, $opt_name ); |
| 365 |
die( wp_json_encode( array( 'result' =>'SUCCESS' ) ) ); //phpcs:ignore -- ok. |
| 366 |
} |
| 367 |
die( wp_json_encode( array( 'error' => esc_html__( 'Segment not found. Please try again.', 'mainwp' ) ) ) ); //phpcs:ignore -- ok. |
| 368 |
} |
| 369 |
// @NO_SONAR_END@ . |
| 370 |
} |
| 371 |
|