Skip to content

Commit 78222a4

Browse files
Merge pull request #633 from contactashish13/issue-626
Improve pagination on visualizer library
2 parents 7ceb076 + 79585e4 commit 78222a4

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

classes/Visualizer/Module/Admin.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public function __construct( Visualizer_Plugin $plugin ) {
6363
$this->_addFilter( 'visualizer_get_chart_counts', 'getChartCountsByTypeAndMeta' );
6464
$this->_addFilter( 'visualizer_feedback_review_trigger', 'feedbackReviewTrigger' );
6565

66+
// screen pagination
67+
$this->_addFilter( 'set-screen-option', 'setScreenOptions', 10, 3 );
68+
6669
// revision support.
6770
$this->_addFilter( 'wp_revisions_to_keep', 'limitRevisions', null, 10, 2 );
6871
$this->_addAction( '_wp_put_post_revision', 'addRevision', null, 10, 1 );
@@ -707,6 +710,36 @@ public function registerAdminMenu() {
707710
array( $this, 'renderSupportPage' )
708711
);
709712
remove_submenu_page( Visualizer_Plugin::NAME, Visualizer_Plugin::NAME );
713+
714+
add_action( "load-{$this->_libraryPage}", array( $this, 'addScreenOptions' ) );
715+
}
716+
717+
/**
718+
* Adds the screen options for pagination.
719+
*/
720+
function addScreenOptions() {
721+
$screen = get_current_screen();
722+
723+
// bail if it's some other page.
724+
if ( ! is_object( $screen ) || $screen->id !== $this->_libraryPage ) {
725+
return;
726+
}
727+
728+
$args = array(
729+
'label' => __( 'Number of charts per page:', 'visualizer' ),
730+
'default' => 6,
731+
'option' => 'visualizer_per_page_library',
732+
);
733+
add_screen_option( 'per_page', $args );
734+
}
735+
736+
/**
737+
* Returns the screen option for pagination.
738+
*/
739+
function setScreenOptions( $status, $option, $value ) {
740+
if ( 'visualizer_per_page_library' === $option ) {
741+
return $value;
742+
}
710743
}
711744

712745
/**
@@ -730,10 +763,25 @@ private function getQuery() {
730763
),
731764
)
732765
);
766+
767+
$per_page = 6;
768+
$screen = get_current_screen();
769+
if ( $screen ) {
770+
// retrieve the per_page option
771+
$screen_option = $screen->get_option( 'per_page', 'option' );
772+
// retrieve the value stored for the current user
773+
$user = get_current_user_id();
774+
$per_page = get_user_meta( $user, $screen_option, true );
775+
if ( empty( $per_page ) || $per_page < 1 ) {
776+
// nothing set, get the default value
777+
$per_page = $screen->get_option( 'per_page', 'default' );
778+
}
779+
}
780+
733781
// the initial query arguments to fetch charts
734782
$query_args = array(
735783
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
736-
'posts_per_page' => 6,
784+
'posts_per_page' => $per_page,
737785
'paged' => $page,
738786
);
739787
// add chart type filter to the query arguments

0 commit comments

Comments
 (0)