-
Notifications
You must be signed in to change notification settings - Fork 800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CRM: Fix dashboard contact graph #38316
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0649121
Stop relying on MySQLs timestamp conversion for months, weeks and days
gogdzl 7535b25
changelog
gogdzl e92a96f
Merge branch 'trunk' into fix/crm/3468-dashboard-contact-graph-fix
gogdzl 7d0721a
Add timezone offset to MySQL
gogdzl f7f4035
Escape SQL
gogdzl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,17 +69,20 @@ function jetpackcrm_dash_refresh() { | |
); | ||
} | ||
|
||
$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 | ||
$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 +92,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' ); | ||
Comment on lines
+96
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think the timestamp way is more readable than this way, though that'd require keeping that column in the database. |
||
$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; | ||
} | ||
|
||
|
4 changes: 4 additions & 0 deletions
4
projects/plugins/crm/changelog/fix-crm-3468-dashboard-contact-graph-fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: fixed | ||
|
||
Dashboard: Fixed an issue where the CRM contacts graph displayed incorrect values in some instances |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a blocker, but the cast should be unnecessary, as
wp_timezone_string()
should always return a valid string, and everything else uses built-ins.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm escaping this because we are not calling
wp_timezone_string
directly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I was referring to the cast. :^)