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