Skip to content

Commit

Permalink
Regression fixes and code refactoing #95
Browse files Browse the repository at this point in the history
  • Loading branch information
zahardev committed Apr 11, 2023
1 parent c349da3 commit 8acdf3a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 42 deletions.
13 changes: 13 additions & 0 deletions partials/stats-all-episodes-pagination.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
<?php
/**
* @see \SeriouslySimpleStats\Classes\All_Episode_Stats::render_all_episodes_stats()
*
* @var int $pagenum
* @var int $total_posts
* @var string $prev_page_url
* @var string $next_page
* @var string $next_page_url
* @var int $total_pages
*
* */
?>
<div class='tablenav bottom'>
<div class="tablenav-pages">
<span class="displaying-num"><?php echo $this->total_posts . ' ' . __( 'items', 'seriously-simple-stats' ) ?></span>
Expand Down
8 changes: 8 additions & 0 deletions partials/stats-all-episodes.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<?php
/**
* @see \SeriouslySimpleStats\Classes\All_Episode_Stats::render_all_episodes_stats()
*
* @var array $all_episodes_stats
* @var array $sort_order
* */
?>
<div class="postbox" id="last-three-months-container">
<h2 class="hndle ui-sortable-handle">
<span><?php echo __( 'All Episodes for the Last Three Months', 'seriously-simple-stats' ); ?></span>
Expand Down
94 changes: 54 additions & 40 deletions php/classes/class-all-episode-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ public function render_all_episodes_stats(){
$html = ob_get_clean();

//Only show page links if there's more than 25 episodes in the table
if ( $this->total_posts >= $this->total_per_page ) {
if ( $this->total_posts > $this->total_per_page ) {
if ( isset( $_GET['pagenum'] ) ) {
$pagenum = intval( $_GET['pagenum'] );
} else {
$pagenum = 1;
}
$total_pages = ceil( $this->total_posts / $this->total_per_page );
$total_pages = intval( ceil( $this->total_posts / $this->total_per_page ) );
$order_by = isset( $_GET['orderby'] ) ? '&orderby=' . sanitize_text_field( $_GET['orderby'] ) : "";
$order = isset( $_GET['order'] ) ? '&order=' . sanitize_text_field( $_GET['order'] ) : "";
$prev_page = ( $pagenum <= 1 ) ? 1 : $pagenum - 1;
Expand All @@ -90,56 +90,73 @@ public function render_all_episodes_stats(){
* @return array
*/
private function get_all_episode_stats() {
$order_by = isset( $_GET['orderby'] ) ? sanitize_text_field( $_GET['orderby'] ) : 'date';
$order = isset( $_GET['order'] ) ? sanitize_text_field( $_GET['order'] ) : 'desc';
$all_episodes_stats = $this->get_sorted_stats( $order_by, $order );

$this->total_posts = count( $all_episodes_stats );
$this->total_per_page = apply_filters( 'ssp_stats_three_months_per_page', 25 );

// Sets up pagination
if ( isset( $_GET['pagenum'] ) ) {
$pagenum = intval( $_GET['pagenum'] );
if ( $pagenum <= 1 ) {
$current_cursor = 0;
} else {
$current_cursor = ( $pagenum - 1 ) * $this->total_per_page;
}
$all_episodes_stats = array_slice( $all_episodes_stats, $current_cursor, $this->total_per_page, true );
} else {
$all_episodes_stats = array_slice( $all_episodes_stats, 0, $this->total_per_page, true );
}

$order_by = isset( $_GET['orderby'] ) ? sanitize_text_field( $_GET['orderby'] ) : 'date';
$order = isset( $_GET['order'] ) ? sanitize_text_field( $_GET['order'] ) : 'desc';
return $all_episodes_stats;
}

$all_episodes_stats = $this->fetch_all_episodes_stats();
/**
* @param string $order_by
* @param string $order
*
* @return array|void
*/
private function get_sorted_stats( $order_by, $order ) {

//24 because we're counting an array, which starts at zero
$this->total_per_page = apply_filters( 'ssp_stats_three_months_per_page', 24 );
$transient_key = sprintf( 'ssp_sorted_stats_three_months_%s_%s', $order_by, $order );

if ( ! empty( $all_episodes_stats ) && is_array( $all_episodes_stats ) ) {
//Sort array based on ordering fields passed
foreach ( $all_episodes_stats as $listen ) {
$listen_sorting[] = $listen[ $order_by ];
}
if ( $sorted_stats = get_transient( $transient_key ) ?: array() ) {
return $sorted_stats;
}

if ( 'desc' === $order ) {
array_multisort( $listen_sorting, SORT_DESC, $all_episodes_stats );
} else {
array_multisort( $listen_sorting, SORT_ASC, $all_episodes_stats );
}
$all_episodes_stats = $this->fetch_all_episodes_stats();

// Sets up pagination
if ( isset( $_GET['pagenum'] ) ) {
$pagenum = intval( $_GET['pagenum'] );
if ( $pagenum <= 1 ) {
$current_cursor = 0;
} else {
$current_cursor = ( $pagenum - 1 ) * $this->total_per_page;
}
$all_episodes_stats = array_slice( $all_episodes_stats, $current_cursor, $this->total_per_page, true );
} else {
$all_episodes_stats = array_slice( $all_episodes_stats, 0, $this->total_per_page, true );
}
if ( empty( $all_episodes_stats ) ) {
return array();
}

//Sort array based on ordering fields passed
foreach ( $all_episodes_stats as $listen ) {
$listen_sorting[] = $listen[ $order_by ];
}

if ( 'desc' === $order ) {
array_multisort( $listen_sorting, SORT_DESC, $all_episodes_stats );
} else {
array_multisort( $listen_sorting, SORT_ASC, $all_episodes_stats );
}

set_transient( $transient_key, $all_episodes_stats, MINUTE_IN_SECONDS );

return $all_episodes_stats;
}

/**
* Fetches all episodes stats for the last 3 months from the database
*
* @return array|mixed|void
* @return array
*/
private function fetch_all_episodes_stats(){

$all_episodes_stats = get_transient( 'ssp_stats_three_months' ) ?: array();

if ( $all_episodes_stats ) {
return $all_episodes_stats;
}
$all_episodes_stats = array();

global $wpdb;

Expand All @@ -155,8 +172,6 @@ private function fetch_all_episodes_stats(){
return $all_episodes_stats;
}

$this->total_posts = count( $total_stats );

$start_month_template = sprintf( '%s-%%s-01 00:00:00', date( 'Y' ) );
$end_month_template = sprintf( '%s-%%s-%s 23:59:59', date( 'Y' ), date( 't' ) );

Expand Down Expand Up @@ -186,6 +201,7 @@ private function fetch_all_episodes_stats(){
continue;
}
$episode_stats = array(
'id' => $post->ID,
'episode_name' => $post->post_title,
'date' => date( 'Y-m-d', strtotime( $post->post_date ) ),
'slug' => admin_url( 'post.php?post=' . $post->ID . '&action=edit' ),
Expand All @@ -200,9 +216,7 @@ private function fetch_all_episodes_stats(){
$all_episodes_stats[] = $episode_stats;
}

$all_episodes_stats = apply_filters( 'ssp_stats_three_months_all_episodes', $all_episodes_stats );

set_transient( 'ssp_stats_three_months', $all_episodes_stats, MINUTE_IN_SECONDS );
return apply_filters( 'ssp_stats_three_months_all_episodes', $all_episodes_stats );
}

/**
Expand Down
7 changes: 5 additions & 2 deletions php/classes/class-ssp-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,12 @@ class Stats {
*/
public $episode_id_where = '';

public $upgrade = null;
public $upgrade;

public $all_episode_stats = null;
/**
* @var All_Episode_Stats
* */
public $all_episode_stats;

/**
* Constructor function.
Expand Down

0 comments on commit 8acdf3a

Please sign in to comment.