Edit File by line
/home/zeestwma/ceyloniy.../wp-conte.../plugins/wpforms-.../src/Admin/Helpers
File: Chart.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace WPForms\Admin\Helpers;
[2] Fix | Delete
[3] Fix | Delete
use DateInterval;
[4] Fix | Delete
use DatePeriod;
[5] Fix | Delete
use DateTimeImmutable;
[6] Fix | Delete
[7] Fix | Delete
/**
[8] Fix | Delete
* Chart dataset processing helper methods.
[9] Fix | Delete
*
[10] Fix | Delete
* @since 1.8.2
[11] Fix | Delete
*/
[12] Fix | Delete
class Chart {
[13] Fix | Delete
[14] Fix | Delete
/**
[15] Fix | Delete
* Default date format.
[16] Fix | Delete
*
[17] Fix | Delete
* @since 1.8.2
[18] Fix | Delete
*/
[19] Fix | Delete
const DATE_FORMAT = 'Y-m-d';
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* Default date-time format.
[23] Fix | Delete
*
[24] Fix | Delete
* @since 1.8.2
[25] Fix | Delete
*/
[26] Fix | Delete
const DATETIME_FORMAT = 'Y-m-d H:i:s';
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* Processes the provided dataset to make sure the formatting needed for the "Chart.js" instance is provided.
[30] Fix | Delete
*
[31] Fix | Delete
* @since 1.8.2
[32] Fix | Delete
*
[33] Fix | Delete
* @param array $query Dataset retrieved from the database.
[34] Fix | Delete
* @param DateTimeImmutable $start_date Start date for the timespan.
[35] Fix | Delete
* @param DateTimeImmutable $end_date End date for the timespan.
[36] Fix | Delete
*
[37] Fix | Delete
* @return array
[38] Fix | Delete
*/
[39] Fix | Delete
public static function process_chart_dataset_data( $query, $start_date, $end_date ) {
[40] Fix | Delete
[41] Fix | Delete
// Bail early if the given query contains no records to iterate.
[42] Fix | Delete
if ( ! is_array( $query ) || empty( $query ) ) {
[43] Fix | Delete
return [ 0, [] ];
[44] Fix | Delete
}
[45] Fix | Delete
[46] Fix | Delete
$dataset = [];
[47] Fix | Delete
$timezone = wp_timezone(); // Retrieve the timezone object for the site.
[48] Fix | Delete
$mysql_timezone = timezone_open( 'UTC' ); // In the database, all datetime are stored in UTC.
[49] Fix | Delete
[50] Fix | Delete
foreach ( $query as $row ) {
[51] Fix | Delete
[52] Fix | Delete
$row_day = isset( $row['day'] ) ? sanitize_text_field( $row['day'] ) : '';
[53] Fix | Delete
$row_count = isset( $row['count'] ) ? abs( (float) $row['count'] ) : 0;
[54] Fix | Delete
[55] Fix | Delete
// Skip the rest of the current iteration if the date (day) is unavailable.
[56] Fix | Delete
if ( empty( $row_day ) ) {
[57] Fix | Delete
continue;
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
// Since we won’t need the initial datetime instances after the query,
[61] Fix | Delete
// there is no need to create immutable date objects.
[62] Fix | Delete
$row_datetime = date_create_from_format( self::DATETIME_FORMAT, $row_day, $mysql_timezone );
[63] Fix | Delete
[64] Fix | Delete
// Skip the rest of the current iteration if the date creation function fails.
[65] Fix | Delete
if ( ! $row_datetime ) {
[66] Fix | Delete
continue;
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
$row_datetime->setTimezone( $timezone );
[70] Fix | Delete
[71] Fix | Delete
$row_date_formatted = $row_datetime->format( self::DATE_FORMAT );
[72] Fix | Delete
[73] Fix | Delete
// We must take into account entries submitted at different hours of the day,
[74] Fix | Delete
// because it is possible that more than one entry could be submitted on a given day.
[75] Fix | Delete
if ( ! isset( $dataset[ $row_date_formatted ] ) ) {
[76] Fix | Delete
$dataset[ $row_date_formatted ] = $row_count;
[77] Fix | Delete
[78] Fix | Delete
continue;
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
$dataset_count = $dataset[ $row_date_formatted ];
[82] Fix | Delete
$dataset[ $row_date_formatted ] = $dataset_count + $row_count;
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
return self::format_chart_dataset_data( $dataset, $start_date, $end_date );
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
/**
[89] Fix | Delete
* Format given forms dataset to ensure correct data structure is parsed for serving the "chart.js" instance.
[90] Fix | Delete
* i.e., [ '2023-02-11' => [ 'day' => '2023-02-11', 'count' => 12 ] ].
[91] Fix | Delete
*
[92] Fix | Delete
* @since 1.8.2
[93] Fix | Delete
*
[94] Fix | Delete
* @param array $dataset Dataset for the chart.
[95] Fix | Delete
* @param DateTimeImmutable $start_date Start date for the timespan.
[96] Fix | Delete
* @param DateTimeImmutable $end_date End date for the timespan.
[97] Fix | Delete
*
[98] Fix | Delete
* @return array
[99] Fix | Delete
*/
[100] Fix | Delete
private static function format_chart_dataset_data( $dataset, $start_date, $end_date ) {
[101] Fix | Delete
[102] Fix | Delete
// In the event that there is no dataset to process, leave early.
[103] Fix | Delete
if ( empty( $dataset ) ) {
[104] Fix | Delete
return [ 0, [] ];
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
$interval = new DateInterval( 'P1D' ); // Variable that store the date interval of period 1 day.
[108] Fix | Delete
$period = new DatePeriod( $start_date, $interval, $end_date ); // Used for iteration between start and end date period.
[109] Fix | Delete
$data = []; // Placeholder for the actual chart dataset data.
[110] Fix | Delete
$total_entries = 0;
[111] Fix | Delete
$has_non_zero_count = false;
[112] Fix | Delete
[113] Fix | Delete
// Use loop to store date into array.
[114] Fix | Delete
foreach ( $period as $date ) {
[115] Fix | Delete
[116] Fix | Delete
$date_formatted = $date->format( self::DATE_FORMAT );
[117] Fix | Delete
$count = isset( $dataset[ $date_formatted ] ) ? (float) $dataset[ $date_formatted ] : 0;
[118] Fix | Delete
$total_entries += $count;
[119] Fix | Delete
$data[ $date_formatted ] = [
[120] Fix | Delete
'day' => $date_formatted,
[121] Fix | Delete
'count' => $count,
[122] Fix | Delete
];
[123] Fix | Delete
[124] Fix | Delete
// This check helps determine whether there is at least one non-zero count value in the dataset being processed.
[125] Fix | Delete
// It's used to optimize the function's behavior and to decide whether to include certain data in the returned result.
[126] Fix | Delete
if ( $count > 0 && ! $has_non_zero_count ) {
[127] Fix | Delete
$has_non_zero_count = true;
[128] Fix | Delete
}
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
return [
[132] Fix | Delete
$total_entries,
[133] Fix | Delete
$has_non_zero_count ? $data : [], // We will return an empty array to indicate that there is no data to display.
[134] Fix | Delete
];
[135] Fix | Delete
}
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function