-
Notifications
You must be signed in to change notification settings - Fork 6
/
infinite-wp-list-tables.php
201 lines (183 loc) · 4.88 KB
/
infinite-wp-list-tables.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* Infinite WP List Tables
*
* @package InfiniteWPListTables
* @author Brady Vercher
* @link http://www.cedaro.com/
* @copyright Copyright (c) 2015 Cedaro, LLC
* @license GPL-2.0+
*
* @wordpress-plugin
* Plugin Name: Infinite WP List Tables
* Plugin URI: https://github.com/cedaro/infinite-wp-list-tables
* Description: Infinite scroll support for WP List Tables in the WordPress admin panel.
* Version: 2.0.0
* Author: Cedaro
* Author URI: http://www.cedaro.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: infinite-wp-list-tables
* Domain Path: /languages
* GitHub Plugin URI: cedaro/infinite-wp-list-tables
*/
/**
* Main plugin class.
*
* @package InfiniteWPListTables
* @since 2.0.0
*/
class Cedaro_Infinite_WP_List_Tables {
/**
* Load the plugin.
*
* @since 2.0.0
*/
public function load() {
$this->load_textdomain();
$this->register_hooks();
}
/**
* Localize the plugin's strings.
*
* @since 2.0.0
*/
public function load_textdomain() {
$plugin_rel_path = dirname( plugin_basename( __FILE__ ) ) . '/languages';
load_plugin_textdomain( 'infinite-wp-list-tables', false, $plugin_rel_path );
}
/**
* Register hooks.
*
* @since 2.0.0
*/
public function register_hooks() {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
add_action( 'admin_head', array( $this, 'print_css' ) );
add_action( 'admin_print_footer_scripts', array( $this, 'print_script' ) );
}
/**
* Enqueue and print assets to make WP List Tables support infinite scroll.
*
* @since 2.0.0
*/
public function enqueue_assets( $hook_suffix ) {
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_enqueue_script(
'jquery-infinite-scroll',
plugin_dir_url( __FILE__ ) . 'assets/js/vendor/jquery.infinitescroll' . $suffix . '.js',
array( 'jquery' ),
'2.1.0',
true
);
wp_localize_script( 'jquery-infinite-scroll', '_iwpltSettings', array(
'l10n' => array(
'loadingMessage' => __( 'Loading…', 'infinite-wp-list-tables' ),
),
'selectors' => $this->get_selectors(),
) );
}
/**
* Print CSS to support the Infinite Scroll script.
*
* @since 2.0.0
*/
public function print_css() {
?>
<style type="text/css">
tr#iwplt-loading td {
padding: 10px;
vertical-align: middle;
}
tr#iwplt-loading .spinner {
display: inline-block;
float: none;
margin-top: 0;
vertical-align: middle;
}
</style>
<?php
}
/**
* Print the script to initialize Infinite Scroll.
*
* @since 2.0.0
*/
public function print_script() {
?>
<script type="text/javascript">
jQuery(function( $ ) {
var settings = _iwpltSettings,
$msg = $( '<tr id="iwplt-loading"><td class="colspanchange"></td></tr>' ),
$table = $( settings.selectors.table );
// Ensure the loading message cell spans the appropriate number of columns.
$msg.find( '.colspanchange' )
.attr( 'colspan', $table.find( 'thead tr' ).children( ':visible' ).length )
.append( '<span class="spinner" /> ' )
.append( settings.l10n.loadingMessage );
$table.find( settings.selectors.content ).infinitescroll({
loading: {
finished: function() {
$( '#iwplt-loading' ).hide();
},
msg: $msg
},
navSelector: '.pagination-links',
nextSelector: '.next-page',
itemSelector: settings.selectors.item,
contentSelector: settings.selectors.content,
maxPage: $( '.total-pages' ).first().text()
}, function() {
// Keep the pagination links visible.
$( '.pagination-links' ).show();
});
});
</script>
<?php
}
/**
* Retrieve the selectors for the current screen.
*
* @since 2.0.0
*
* @return array
*/
protected function get_selectors() {
$screen_id = get_current_screen()->id;
$selectors = array(
'content' => '#the-list',
'item' => '#the-list tr',
'table' => '.wp-list-table',
);
// Comment List Table
if ( 'edit-comments' == $screen_id ) {
$selectors = array(
'content' => '#the-comment-list',
'item' => '#the-comment-list tr',
'table' => 'table.comments',
);
}
// BuddyPress Activity List Table
if ( 'toplevel_page_bp-activity' == $screen_id ) {
$selectors = array(
'content' => '#the-comment-list',
'item' => '#the-comment-list tr',
'table' => 'table.activities',
);
}
// BuddyPress Group List Table
if ( 'toplevel_page_bp-groups' == $screen_id ) {
$selectors = array(
'content' => '#the-comment-list',
'item' => '#the-comment-list tr',
'table' => 'table.groups',
);
}
return apply_filters( 'infinite_wp_list_tables_selectors', $selectors );
}
}
/**
* Initialize the plugin.
*/
$infinite_wp_list_tables = new Cedaro_Infinite_WP_List_Tables();
add_action( 'plugins_loaded', array( $infinite_wp_list_tables, 'load' ) );