Skip to content

Commit 0198222

Browse files
Improve pagination on visualizer library
1 parent 3d4323b commit 0198222

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 );
@@ -608,6 +611,36 @@ public function registerAdminMenu() {
608611
array( $this, 'renderSupportPage' )
609612
);
610613
remove_submenu_page( Visualizer_Plugin::NAME, Visualizer_Plugin::NAME );
614+
615+
add_action( "load-{$this->_libraryPage}", array( $this, 'addScreenOptions' ) );
616+
}
617+
618+
/**
619+
* Adds the screen options for pagination.
620+
*/
621+
function addScreenOptions() {
622+
$screen = get_current_screen();
623+
624+
// bail if it's some other page.
625+
if ( ! is_object( $screen ) || $screen->id != $this->_libraryPage ) {
626+
return;
627+
}
628+
629+
$args = array(
630+
'label' => __( 'Number of charts per page:', 'visualizer' ),
631+
'default' => 6,
632+
'option' => 'visualizer_per_page_library',
633+
);
634+
add_screen_option( 'per_page', $args );
635+
}
636+
637+
/**
638+
* Returns the screen option for pagination.
639+
*/
640+
function setScreenOptions( $status, $option, $value ) {
641+
if ( 'visualizer_per_page_library' === $option ) {
642+
return $value;
643+
}
611644
}
612645

613646
/**
@@ -631,10 +664,25 @@ private function getQuery() {
631664
),
632665
)
633666
);
667+
668+
$per_page = 6;
669+
$screen = get_current_screen();
670+
if ( $screen ) {
671+
// retrieve the per_page option
672+
$screen_option = $screen->get_option( 'per_page', 'option' );
673+
// retrieve the value stored for the current user
674+
$user = get_current_user_id();
675+
$per_page = get_user_meta( $user, $screen_option, true );
676+
if ( empty( $per_page ) || $per_page < 1 ) {
677+
// nothing set, get the default value
678+
$per_page = $screen->get_option( 'per_page', 'default' );
679+
}
680+
}
681+
634682
// the initial query arguments to fetch charts
635683
$query_args = array(
636684
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
637-
'posts_per_page' => 6,
685+
'posts_per_page' => $per_page,
638686
'paged' => $page,
639687
);
640688
// add chart type filter to the query arguments

0 commit comments

Comments
 (0)