esc_html__( 'The Site ID is invalid or not found. Please try again.', 'mainwp' ) ) ) ); } if ( empty( $_POST['dtsstart'] ) || empty( $_POST['dtsstop'] ) ) { die( wp_json_encode( array( 'error' => esc_html__( 'Start and end dates cannot be empty. Please try again.', 'mainwp' ) ) ) ); } $params = array( 'start' => $_POST['dtsstart'] . ' 00:00:00', 'end' => $_POST['dtsstop'] . ' 23:59:59', 'issub' => 0, ); //phpcs:enable WordPress.Security.NonceVerification if ( strtotime( $params['start'] ) > strtotime( $params['end'] ) ) { die( wp_json_encode( array( 'error' => esc_html__( 'The start date must be earlier than the end date. Please try again.', 'mainwp' ) ) ) ); } $params['group_time_by'] = $this->prepare_group_time_option_for_ui_chart_data_only( $site_id, $params, true ); $results = MainWP_Uptime_Monitoring_Handle::instance()->get_site_response_time_chart_data( $site_id, $params ); $prepare_chart_dt = array(); if ( ! empty( $results['response_time_data_lists'] ) ) { $prepare_chart_dt = $this->prepare_response_time_ui_chart_data( $results['response_time_data_lists'], $params ); } $last_incidents_count = MainWP_DB_Uptime_Monitoring::instance()->get_site_count_last_incidents( $site_id, $days_number ); if ( ! is_array( $last_incidents_count ) ) { $last_incidents_count = array(); } $ratios_count = MainWP_DB_Uptime_Monitoring::instance()->get_last_site_uptime_ratios_values( $site_id, $days_number ); if ( ! is_array( $ratios_count ) ) { $ratios_count = array(); } $data_stats = ! empty( $results['resp_stats'] ) && is_array( $results['resp_stats'] ) ? $results['resp_stats'] : array(); $active_monitor = 0; $primary_monitor = MainWP_DB_Uptime_Monitoring::instance()->get_monitor_by( $site_id, 'issub', 0 ); if ( $primary_monitor ) { $global_settings = MainWP_Uptime_Monitoring_Handle::get_global_monitoring_settings(); $active_monitor = MainWP_Uptime_Monitoring_Connect::get_apply_setting( 'active', (int) $primary_monitor->active, $global_settings, -1, 60 ); } $data_stats['active_monitor'] = $active_monitor ? 1 : 0; $last = MainWP_DB_Uptime_Monitoring::instance()->get_last_site_heartbeat( $site_id, false ); if ( $last ) { $data_stats['current_status'] = $last->status; $data_stats['http_code'] = $last->http_code; } $data_stats['incidents_count'] = isset( $last_incidents_count['count'] ) ? $last_incidents_count['count'] : 'N/A'; if ( ! empty( $ratios_count['total_value'] ) ) { $data_stats['ratios_number'] = number_format( $ratios_count['up_value'] / $ratios_count['total_value'], 10 ); // 10 to fix. } die( wp_json_encode( array( 'data' => $prepare_chart_dt, 'data_stats' => $data_stats, ), ) ); } /** * Method get_valid_days_periods * * @param string|null $period period. * @return mixed */ public static function get_valid_days_periods( $period = null ) { $values = array( 'day' => 0, 'week' => 7, 'month' => 30, 'year' => 365, ); if ( null === $period ) { return $values; } return isset( $values[ $period ] ) ? $period : 'day'; } /** * Method get_days_number_by_period * * @param string|false $period period. * @return mixed */ public static function get_days_number_by_period( $period ) { if ( empty( $period ) || ! is_scalar( $period ) ) { return 0; } $values = static::get_valid_days_periods(); return isset( $values[ $period ] ) ? $values[ $period ] : 0; } /** * Renders the top widget. */ public static function render_top_widget() { $current_period = get_user_option( 'mainwp_uptime_monitoring_widget_stat_selected_period' ); $current_period = static::get_valid_days_periods( $current_period ); ?>