From 06491214065eb7ccd055ab7c9ece70af098325cb Mon Sep 17 00:00:00 2001 From: gogdzl Date: Thu, 11 Jul 2024 23:59:30 -0300 Subject: [PATCH 1/4] Stop relying on MySQLs timestamp conversion for months, weeks and days --- .../crm/admin/dashboard/dashboard.ajax.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/projects/plugins/crm/admin/dashboard/dashboard.ajax.php b/projects/plugins/crm/admin/dashboard/dashboard.ajax.php index 06a8ac2e8fae7..1eed1c4433ab9 100644 --- a/projects/plugins/crm/admin/dashboard/dashboard.ajax.php +++ b/projects/plugins/crm/admin/dashboard/dashboard.ajax.php @@ -73,13 +73,13 @@ function jetpackcrm_dash_refresh() { $sql = $wpdb->prepare( 'SELECT count(ID) as count, YEAR(FROM_UNIXTIME(zbsc_created)) as year FROM ' . $ZBSCRM_t['contacts'] . ' WHERE zbsc_created > %d AND zbsc_created < %d GROUP BY year ORDER BY year', $start_date, $end_date ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase $yearly = $wpdb->get_results( $sql ); - $sql = $wpdb->prepare( 'SELECT count(ID) as count, zbsc_created as ts, MONTH(FROM_UNIXTIME(zbsc_created)) as month, YEAR(FROM_UNIXTIME(zbsc_created)) as year FROM ' . $ZBSCRM_t['contacts'] . ' WHERE zbsc_created > %d AND zbsc_created < %d GROUP BY month, year ORDER BY year, month', $start_date, $end_date ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase + $sql = $wpdb->prepare( 'SELECT count(ID) as count, MONTH(FROM_UNIXTIME(zbsc_created)) as month, YEAR(FROM_UNIXTIME(zbsc_created)) as year FROM ' . $ZBSCRM_t['contacts'] . ' WHERE zbsc_created > %d AND zbsc_created < %d GROUP BY month, year ORDER BY year, month', $start_date, $end_date ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase $monthly = $wpdb->get_results( $sql ); - $sql = $wpdb->prepare( 'SELECT count(ID) as count, zbsc_created as ts, WEEK(FROM_UNIXTIME(zbsc_created), 1) as week, YEAR(FROM_UNIXTIME(zbsc_created)) as year FROM ' . $ZBSCRM_t['contacts'] . ' WHERE zbsc_created > %d AND zbsc_created < %d GROUP BY week, year ORDER BY year, week', $start_date, $end_date ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase + $sql = $wpdb->prepare( 'SELECT count(ID) as count, WEEK(FROM_UNIXTIME(zbsc_created), 1) as week, YEAR(FROM_UNIXTIME(zbsc_created)) as year FROM ' . $ZBSCRM_t['contacts'] . ' WHERE zbsc_created > %d AND zbsc_created < %d GROUP BY week, year ORDER BY year, week', $start_date, $end_date ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase $weekly = $wpdb->get_results( $sql ); - $sql = $wpdb->prepare( 'SELECT count(ID) as count, zbsc_created as ts, DAY(FROM_UNIXTIME(zbsc_created)) as day, MONTH(FROM_UNIXTIME(zbsc_created)) as month, YEAR(FROM_UNIXTIME(zbsc_created)) as year FROM ' . $ZBSCRM_t['contacts'] . ' WHERE zbsc_created > %d AND zbsc_created < %d GROUP BY day, month, year ORDER BY year, month, day', $start_date, $end_date ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase + $sql = $wpdb->prepare( 'SELECT count(ID) as count, DAY(FROM_UNIXTIME(zbsc_created)) as day, MONTH(FROM_UNIXTIME(zbsc_created)) as month, YEAR(FROM_UNIXTIME(zbsc_created)) as year FROM ' . $ZBSCRM_t['contacts'] . ' WHERE zbsc_created > %d AND zbsc_created < %d GROUP BY day, month, year ORDER BY year, month, day', $start_date, $end_date ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase $daily = $wpdb->get_results( $sql ); $zeros = jetpackcrm_create_zeros_array( $start_date, $end_date ); @@ -89,19 +89,20 @@ function jetpackcrm_dash_refresh() { $zeros['year'][ $v->year ] = $v->count; } - // convert the monthly array into a zero padded one foreach ( $monthly as $v ) { - $the_month = gmdate( 'M y', $v->ts ); + $datetime_month = DateTime::createFromFormat( '!m Y', $v->month . ' ' . $v->year ); + $the_month = $datetime_month->format( 'M y' ); $zeros['month'][ $the_month ] = $v->count; } foreach ( $weekly as $v ) { - $the_week = gmdate( 'W Y', $v->ts ); + $the_week = str_pad( $v->week, 2, '0', STR_PAD_LEFT ) . ' ' . $v->year; $zeros['week'][ $the_week ] = $v->count; } foreach ( $daily as $v ) { - $the_day = gmdate( 'd M y', $v->ts ); + $datetime_day = DateTime::createFromFormat( 'Y-m-d', $v->year . '-' . $v->month . '-' . $v->day ); + $the_day = $datetime_day->format( 'd M y' ); $zeros['day'][ $the_day ] = $v->count; } From 7535b25243840e41e01ecf09257956a23f9a0680 Mon Sep 17 00:00:00 2001 From: gogdzl Date: Fri, 12 Jul 2024 00:03:25 -0300 Subject: [PATCH 2/4] changelog --- .../crm/changelog/fix-crm-3468-dashboard-contact-graph-fix | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 projects/plugins/crm/changelog/fix-crm-3468-dashboard-contact-graph-fix diff --git a/projects/plugins/crm/changelog/fix-crm-3468-dashboard-contact-graph-fix b/projects/plugins/crm/changelog/fix-crm-3468-dashboard-contact-graph-fix new file mode 100644 index 0000000000000..99116cdb2a395 --- /dev/null +++ b/projects/plugins/crm/changelog/fix-crm-3468-dashboard-contact-graph-fix @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Dashboard: Fixed an issue where the CRM contacts graph displayed incorrect values in some instances From 7d0721aa3318b97dc6d2156542a4c40c9485ddcb Mon Sep 17 00:00:00 2001 From: gogdzl Date: Fri, 25 Oct 2024 16:54:15 -0300 Subject: [PATCH 3/4] Add timezone offset to MySQL --- projects/plugins/crm/admin/dashboard/dashboard.ajax.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/projects/plugins/crm/admin/dashboard/dashboard.ajax.php b/projects/plugins/crm/admin/dashboard/dashboard.ajax.php index 1eed1c4433ab9..f59311bba1641 100644 --- a/projects/plugins/crm/admin/dashboard/dashboard.ajax.php +++ b/projects/plugins/crm/admin/dashboard/dashboard.ajax.php @@ -69,6 +69,9 @@ function jetpackcrm_dash_refresh() { ); } + $tz_offset = jpcrm_get_wp_timezone_offset(); + $wpdb->query( 'SET time_zone = "' . $tz_offset . '";' ); + // next we want the contact chart which is total contacts between the dates grouped by day, week, month, year $sql = $wpdb->prepare( 'SELECT count(ID) as count, YEAR(FROM_UNIXTIME(zbsc_created)) as year FROM ' . $ZBSCRM_t['contacts'] . ' WHERE zbsc_created > %d AND zbsc_created < %d GROUP BY year ORDER BY year', $start_date, $end_date ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase $yearly = $wpdb->get_results( $sql ); From f7f40356e64a38f08e79cd2a5307544843b63c4b Mon Sep 17 00:00:00 2001 From: gogdzl Date: Fri, 25 Oct 2024 17:04:33 -0300 Subject: [PATCH 4/4] Escape SQL --- projects/plugins/crm/admin/dashboard/dashboard.ajax.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/plugins/crm/admin/dashboard/dashboard.ajax.php b/projects/plugins/crm/admin/dashboard/dashboard.ajax.php index f59311bba1641..1be95af6dd9b3 100644 --- a/projects/plugins/crm/admin/dashboard/dashboard.ajax.php +++ b/projects/plugins/crm/admin/dashboard/dashboard.ajax.php @@ -69,7 +69,7 @@ function jetpackcrm_dash_refresh() { ); } - $tz_offset = jpcrm_get_wp_timezone_offset(); + $tz_offset = esc_sql( (string) jpcrm_get_wp_timezone_offset() ); $wpdb->query( 'SET time_zone = "' . $tz_offset . '";' ); // next we want the contact chart which is total contacts between the dates grouped by day, week, month, year