| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class LP_Query_List_Table |
| 4 |
*/ |
| 5 |
class LP_Query_List_Table implements ArrayAccess { |
| 6 |
/** |
| 7 |
* @var array|null |
| 8 |
*/ |
| 9 |
protected $_data = null; |
| 10 |
|
| 11 |
/** |
| 12 |
* LP_Query_List_Table constructor. |
| 13 |
* |
| 14 |
* @param $data |
| 15 |
*/ |
| 16 |
public function __construct( $data ) { |
| 17 |
$this->_data = wp_parse_args( |
| 18 |
$data, |
| 19 |
array( |
| 20 |
'pages' => 0, |
| 21 |
'total' => 0, |
| 22 |
'items' => null, |
| 23 |
'paged' => 1, |
| 24 |
'limit' => 10, |
| 25 |
'nav_format' => '%#%/', |
| 26 |
'nav_base' => '', |
| 27 |
'single' => __( 'item', 'learnpress' ), |
| 28 |
'plural' => __( 'items', 'learnpress' ), |
| 29 |
'format' => '', |
| 30 |
) |
| 31 |
); |
| 32 |
|
| 33 |
global $wp; |
| 34 |
if ( ! empty( $wp->query_vars['view_id'] ) && empty( $data['paged'] ) ) { |
| 35 |
$this->_data['paged'] = absint( $wp->query_vars['view_id'] ); |
| 36 |
} |
| 37 |
|
| 38 |
$this->_init(); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* |
| 43 |
*/ |
| 44 |
protected function _init() { |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @return int |
| 50 |
*/ |
| 51 |
public function get_pages() { |
| 52 |
if ( empty( $this->_data['pages'] ) && $this->_data['total'] ) { |
| 53 |
$this->_data['pages'] = ceil( $this->_data['total'] / $this->_data['limit'] ); |
| 54 |
} |
| 55 |
|
| 56 |
return absint( $this->_data['pages'] ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* @return int |
| 61 |
*/ |
| 62 |
public function get_total() { |
| 63 |
return absint( $this->_data['total'] ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @return array |
| 68 |
*/ |
| 69 |
public function get_items() { |
| 70 |
return $this->_data['items']; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* @return int |
| 75 |
*/ |
| 76 |
public function get_paged() { |
| 77 |
return max( 1, absint( $this->_data['paged'] ) ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @return int |
| 82 |
*/ |
| 83 |
public function get_limit() { |
| 84 |
return absint( $this->_data['limit'] ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Pagination |
| 89 |
* |
| 90 |
* @param bool $echo |
| 91 |
* |
| 92 |
* @return string |
| 93 |
*/ |
| 94 |
public function get_nav_numbers( $echo = true, $base_url = '' ) { |
| 95 |
if ( ! $base_url ) { |
| 96 |
$base_url = learn_press_get_current_url(); |
| 97 |
} |
| 98 |
if ( ! empty( $this->_data['nav_base'] ) ) { |
| 99 |
if ( is_callable( $this->_data['nav_base'] ) ) { |
| 100 |
$base = call_user_func_array( $this->_data['nav_base'], array( $this->_data['nav_format'] ) ); |
| 101 |
} else { |
| 102 |
$base = $this->_data['nav_base']; |
| 103 |
} |
| 104 |
} else { |
| 105 |
$base = trailingslashit( preg_replace( '~\/[0-9]+\/?$~', '', $base_url ) ); |
| 106 |
} |
| 107 |
|
| 108 |
return learn_press_paging_nav( |
| 109 |
array( |
| 110 |
'num_pages' => $this->get_pages(), |
| 111 |
'paged' => $this->get_paged(), |
| 112 |
'echo' => $echo, |
| 113 |
'format' => $this->_data['nav_format'], |
| 114 |
'base' => $base, |
| 115 |
) |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Get range |
| 121 |
* |
| 122 |
* @return array |
| 123 |
*/ |
| 124 |
public function get_offset() { |
| 125 |
$from = ( $this->get_paged() - 1 ) * $this->get_limit() + 1; |
| 126 |
$to = $from + $this->get_limit() - 1; |
| 127 |
$to = min( $to, $this->get_total() ); |
| 128 |
if ( $this->get_total() < 1 ) { |
| 129 |
$from = 0; |
| 130 |
} |
| 131 |
|
| 132 |
return array( $from, $to ); |
| 133 |
} |
| 134 |
|
| 135 |
public function get_offset_text( $format = '', $echo = false ) { |
| 136 |
$offset = $this->get_offset(); |
| 137 |
$output = ''; |
| 138 |
|
| 139 |
if ( ! $format ) { |
| 140 |
if ( $this->_data['single'] && $this->_data['plural'] ) { |
| 141 |
$format = __( 'Displaying {{from}} to {{to}} of {{total}} {{item_name}}.', 'learnpress' ); |
| 142 |
} else { |
| 143 |
$format = __( 'Displaying {{from}} to {{to}} of {{total}}.', 'learnpress' ); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
$output = str_replace( |
| 148 |
array( '{{from}}', '{{to}}', '{{total}}', '{{item_name}}' ), |
| 149 |
array( |
| 150 |
$offset[0], |
| 151 |
$offset[1], |
| 152 |
$this->get_total(), |
| 153 |
$this->get_total() < 2 ? $this->_data['single'] : $this->_data['plural'], |
| 154 |
), |
| 155 |
$format |
| 156 |
); |
| 157 |
|
| 158 |
if ( $echo ) { |
| 159 |
echo wp_kses_post( $output ); |
| 160 |
} |
| 161 |
|
| 162 |
return $output; |
| 163 |
} |
| 164 |
|
| 165 |
public function get_nav( $format = '', $echo = false, $base_url = '' ) { |
| 166 |
$output = ''; |
| 167 |
$offset = $this->get_offset_text( empty( $format ) ? $this->_data['format'] : $format, false ); |
| 168 |
$numbers = $this->get_nav_numbers( false, $base_url ); |
| 169 |
|
| 170 |
if ( $offset && $numbers ) { |
| 171 |
$output = sprintf( '<div class="learn-press-nav-items">%s%s</div>', $offset, $numbers ); |
| 172 |
} |
| 173 |
|
| 174 |
if ( $echo ) { |
| 175 |
echo wp_kses_post( $output ); |
| 176 |
} |
| 177 |
|
| 178 |
return $output; |
| 179 |
} |
| 180 |
|
| 181 |
public function offsetExists( $offset ) { |
| 182 |
return array_key_exists( $offset, $this->_data ); |
| 183 |
} |
| 184 |
|
| 185 |
public function offsetGet( $offset ) { |
| 186 |
return array_key_exists( $offset, $this->_data ) ? $this->_data[ $offset ] : false; |
| 187 |
} |
| 188 |
|
| 189 |
public function offsetSet( $offset, $value ) { |
| 190 |
$this->_data[ $offset ] = $value; |
| 191 |
} |
| 192 |
|
| 193 |
public function offsetUnset( $offset ) { |
| 194 |
return false; |
| 195 |
} |
| 196 |
} |
| 197 |
|