Skip to content

Commit

Permalink
do not return NULL, if database is empty
Browse files Browse the repository at this point in the history
Null causes trouble during further processing, i.e. PHP warnings in the
API processing and later on problems building the dashboard widget in
Javascript, because all methods expect the array to have all keys.

Move the caching (transient) directive into the conditional, so we still
do not cache empty results and so not return NULL anymore.
  • Loading branch information
stklcode committed Oct 16, 2023
1 parent 0ec5f69 commit 4a614b0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
16 changes: 7 additions & 9 deletions inc/class-statify-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,14 @@ public static function get_stats( $force_refresh = false ) {
// Prepare data.
if ( ! empty( $data['visits'] ) ) {
$data['visits'] = array_reverse( $data['visits'] );
} else {
$data = null;
}

// Make cache.
set_transient(
'statify_data',
$data,
MINUTE_IN_SECONDS * 15
);
// Make cache.
set_transient(
'statify_data',
$data,
MINUTE_IN_SECONDS * 15
);
}

return $data;
}
Expand Down
10 changes: 9 additions & 1 deletion tests/test-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,15 @@ function ( $original ) use ( &$original_capture, &$override ) {
*/
public function test_get_stats() {
// Initially the database is empty.
$this->assertNull( $this->get_stats(), 'Expected NULL stats for empty database' );
$this->assertEquals(
array(
'referrer' => array(),
'target' => array(),
'visits' => array(),
),
$this->get_stats(),
'Expected empty dataset for empty database'
);

// Now insert data for the last 3 days.
$date1 = new DateTime();
Expand Down

0 comments on commit 4a614b0

Please sign in to comment.