| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Render an ajax list table |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
* |
| 17 |
* @param CT_Table|string $table CT_Table object or CT_Table name |
| 18 |
* @param array $query_args Query parameters |
| 19 |
* @param array $view_args View parameters |
| 20 |
*/ |
| 21 |
function ct_render_ajax_list_table( $table, $query_args = array(), $view_args = array() ) { |
| 22 |
|
| 23 |
global $ct_table, $ct_query, $ct_list_table, $ct_ajax_list_items_per_page; |
| 24 |
|
| 25 |
$ct_table = ct_setup_table( $table ); |
| 26 |
|
| 27 |
if( is_object( $ct_table ) ) { |
| 28 |
|
| 29 |
// Setup this constant to allow from CT_List_Table meet that this render comes from this plugin |
| 30 |
@define( 'IS_CT_AJAX_LIST_TABLE', true ); |
| 31 |
|
| 32 |
// Enqueue assets |
| 33 |
ct_ajax_list_table_enqueue_scripts(); |
| 34 |
|
| 35 |
// Setup query args |
| 36 |
$query_args = wp_parse_args( $query_args, array( |
| 37 |
'paged' => 1, |
| 38 |
'items_per_page' => 20, |
| 39 |
) ); |
| 40 |
|
| 41 |
// setup view args |
| 42 |
$view_args = wp_parse_args( $view_args, array( |
| 43 |
'views' => true, |
| 44 |
'search_box' => true, |
| 45 |
) ); |
| 46 |
|
| 47 |
// Add a filter to override the items per page user setting |
| 48 |
$ct_ajax_list_items_per_page = $query_args['items_per_page']; |
| 49 |
add_filter( 'edit_' . $ct_table->name . '_per_page', 'ct_ajax_list_override_items_per_page' ); |
| 50 |
|
| 51 |
// Set up vars |
| 52 |
$ct_query = new CT_Query( $query_args ); |
| 53 |
$ct_list_table = new CT_List_Table(); |
| 54 |
|
| 55 |
$ct_list_table->prepare_items(); |
| 56 |
|
| 57 |
?> |
| 58 |
|
| 59 |
<div class="wrap ct-ajax-list-table" data-object="<?php echo esc_attr( $ct_table->name ); ?>" data-query-args="<?php echo esc_attr( str_replace( '"', "'", json_encode( $query_args ) ) ); ?>"> |
| 60 |
|
| 61 |
<?php |
| 62 |
if( $view_args['views'] ) { |
| 63 |
$ct_list_table->views(); |
| 64 |
} |
| 65 |
?> |
| 66 |
|
| 67 |
<?php // <form id="ct-list-filter" method="get"> ?> |
| 68 |
|
| 69 |
<?php |
| 70 |
if( $view_args['search_box'] ) { |
| 71 |
$ct_list_table->search_box( $ct_table->labels->search_items, $ct_table->name ); |
| 72 |
} |
| 73 |
?> |
| 74 |
|
| 75 |
<?php ct_render_ajax_list_tablenav( $ct_list_table, 'top' ); ?> |
| 76 |
|
| 77 |
<table class="wp-list-table <?php echo implode( ' ', $ct_list_table->get_table_classes() ); ?>"> |
| 78 |
|
| 79 |
<thead> |
| 80 |
<tr> |
| 81 |
<?php $ct_list_table->print_column_headers(); ?> |
| 82 |
</tr> |
| 83 |
</thead> |
| 84 |
|
| 85 |
<?php $singular = $ct_list_table->_args['singular']; ?> |
| 86 |
<tbody id="the-list"<?php |
| 87 |
if ( $singular ) { |
| 88 |
echo " data-wp-lists='list:" . esc_attr( $singular ) . "'"; |
| 89 |
} ?>> |
| 90 |
<?php $ct_list_table->display_rows_or_placeholder(); ?> |
| 91 |
</tbody> |
| 92 |
|
| 93 |
<tfoot> |
| 94 |
<tr> |
| 95 |
<?php $ct_list_table->print_column_headers( false ); ?> |
| 96 |
</tr> |
| 97 |
</tfoot> |
| 98 |
|
| 99 |
</table> |
| 100 |
|
| 101 |
<?php ct_render_ajax_list_tablenav( $ct_list_table, 'bottom' ); ?> |
| 102 |
|
| 103 |
<?php // </form> ?> |
| 104 |
|
| 105 |
<div id="ajax-response"></div> |
| 106 |
<br class="clear" /> |
| 107 |
|
| 108 |
</div> |
| 109 |
|
| 110 |
<?php |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Overrides the items per page user setting |
| 118 |
* |
| 119 |
* @since 1.0.0 |
| 120 |
* |
| 121 |
* @param int $items_per_page |
| 122 |
* |
| 123 |
* @return int |
| 124 |
*/ |
| 125 |
function ct_ajax_list_override_items_per_page( $items_per_page ) { |
| 126 |
|
| 127 |
global $ct_ajax_list_items_per_page; |
| 128 |
|
| 129 |
if( absint( $ct_ajax_list_items_per_page ) !== 0 ) { |
| 130 |
return absint( $ct_ajax_list_items_per_page ); |
| 131 |
} |
| 132 |
|
| 133 |
return $items_per_page; |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Custom table nav function |
| 139 |
* |
| 140 |
* @param CT_List_Table $ct_list_table |
| 141 |
* @param string $which |
| 142 |
*/ |
| 143 |
function ct_render_ajax_list_tablenav( $ct_list_table, $which ) { |
| 144 |
?> |
| 145 |
<div class="tablenav <?php echo esc_attr( $which ); ?>"> |
| 146 |
|
| 147 |
<?php if ( $ct_list_table->has_items() ): ?> |
| 148 |
<div class="alignleft actions bulkactions"> |
| 149 |
<?php $ct_list_table->bulk_actions( $which ); ?> |
| 150 |
</div> |
| 151 |
<?php endif; |
| 152 |
$ct_list_table->extra_tablenav( $which ); |
| 153 |
$ct_list_table->pagination( $which ); |
| 154 |
?> |
| 155 |
|
| 156 |
<br class="clear" /> |
| 157 |
</div> |
| 158 |
<?php |
| 159 |
} |