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 ); ?>

render_content_response_time_widget( $site_id ); ?>
$num ) { $select_dates[ $period ] = array( 'start' => gmdate( 'Y-m-d', $end_dts - $num * DAY_IN_SECONDS ), 'end' => gmdate( 'Y-m-d', $end_dts ), ); } ?> get_monitor_by( $site_id, 'issub', 0 ); if ( $primary_monitor ) { $apply_interval = MainWP_Uptime_Monitoring_Connect::get_apply_setting( 'interval', (int) $primary_monitor->interval, false, -1, 60 ); } if ( false !== $apply_interval ) { $length_time = $end_ts - $start_ts; if ( $ajax_working ) { $is_get_all = $length_time <= DAY_IN_SECONDS && $apply_interval <= 60; } elseif ( $apply_interval <= 60 ) { $is_get_all = true; } $is_group_hours = $length_time <= 3 * DAY_IN_SECONDS && $length_time > DAY_IN_SECONDS && $apply_interval >= 5 ? true : false; if ( ! $is_group_hours && $length_time <= 7 * DAY_IN_SECONDS && $apply_interval >= 60 ) { $is_group_hours = 'hour'; } if ( $is_get_all ) { $group_time = 'get_all'; } elseif ( $is_group_hours ) { $group_time = 'hour'; } } return $group_time; } /** * Prepare response time for ui chart data. * * @param array $chart_data array: date and value. * @param array $params params. * @param string $slug for date format hook. * @return array prepared. */ public function prepare_response_time_ui_chart_data( $chart_data, $params = array(), $slug = 'uptime' ) { $format_date = ''; // need to be empty for process. if ( ! empty( $params['group_time_by'] ) ) { if ( 'hour' === $params['group_time_by'] ) { $format_date = 'd H a'; // 09 am. } elseif ( 'get_all' === $params['group_time_by'] ) { $format_date = 'H:i'; // 10:15. } } $format_date = apply_filters( 'mainwp_widgets_chart_date_format', $format_date, $params, $slug ); if ( is_array( $chart_data ) ) { $chart_data = array_map( function ( $value ) use ( $format_date ) { $local_datetime = MainWP_Utility::get_timestamp( strtotime( $value['date'] ) ); if ( ! empty( $format_date ) ) { $value['date'] = gmdate( $format_date, $local_datetime ); } else { $value['date'] = MainWP_Utility::format_date( $local_datetime ); } return $value; }, $chart_data // map data. ); } else { $chart_data = array(); } return $chart_data; } }