| 1 |
<?php if ( ! defined( 'ABSPATH' ) ) exit; |
| 2 |
|
| 3 |
// WP_List_Table is not loaded automatically so we need to load it in our application |
| 4 |
if( ! class_exists( 'WP_List_Table' ) ) { |
| 5 |
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); |
| 6 |
} |
| 7 |
/** |
| 8 |
* Create a new table class that will extend the WP_List_Table |
| 9 |
*/ |
| 10 |
class Wow_List_Table extends WP_List_Table{ |
| 11 |
|
| 12 |
/** |
| 13 |
* Number of items per page |
| 14 |
* |
| 15 |
* @var int |
| 16 |
* @since 1.5 |
| 17 |
*/ |
| 18 |
public $per_page = 30; |
| 19 |
|
| 20 |
private $data; |
| 21 |
private $pluginname; |
| 22 |
private $shortcode; |
| 23 |
|
| 24 |
public function __construct( $data, $pluginname, $shortcode ) { |
| 25 |
$this->data = $data; |
| 26 |
$this->pluginname = $pluginname; |
| 27 |
$this->shortcode = $shortcode; |
| 28 |
|
| 29 |
// Set parent defaults |
| 30 |
parent::__construct( array( |
| 31 |
'ajax' => false, |
| 32 |
) ); |
| 33 |
$this->process_bulk_action(); |
| 34 |
} |
| 35 |
|
| 36 |
public function search_box( $text, $input_id ) { |
| 37 |
$input_id = $input_id . '-search-input'; |
| 38 |
if ( ! empty( $_REQUEST['orderby'] ) ) |
| 39 |
echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />'; |
| 40 |
if ( ! empty( $_REQUEST['order'] ) ) |
| 41 |
echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />'; |
| 42 |
?> |
| 43 |
<p class="search-box"> |
| 44 |
<label class="screen-reader-text" for="<?php echo esc_attr($input_id) ?>"><?php echo esc_html($text); ?>:</label> |
| 45 |
<input type="search" id="<?php echo esc_attr($input_id) ?>" name="s" value="<?php _admin_search_query(); ?>" /> |
| 46 |
<?php submit_button( $text, 'button', false, false, array('ID' => 'search-submit') ); ?> |
| 47 |
</p> |
| 48 |
<?php |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Define what data to show on each column of the table |
| 53 |
* |
| 54 |
* @param Array $item Data |
| 55 |
* @param String $column_name - Current column name |
| 56 |
* |
| 57 |
* @return Mixed |
| 58 |
*/ |
| 59 |
public function column_default( $item, $column_name ) |
| 60 |
{ |
| 61 |
if ($column_name === 'delete'){ |
| 62 |
$value = '<span style="color:red;">' . $item['delete'] . '</span>'; |
| 63 |
} |
| 64 |
elseif ($column_name === 'duplicate'){ |
| 65 |
$value = '<span style="color:green;">' . $item['duplicate'] . '</span>'; |
| 66 |
} |
| 67 |
|
| 68 |
else { |
| 69 |
$value = $item[ $column_name ]; |
| 70 |
} |
| 71 |
|
| 72 |
return $value; |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
/** |
| 78 |
* Override the parent columns method. Defines the columns to use in your listing table |
| 79 |
* |
| 80 |
* @return Array |
| 81 |
*/ |
| 82 |
public function get_columns() |
| 83 |
{ |
| 84 |
$columns = array( |
| 85 |
'cb' => '<input type="checkbox" />', |
| 86 |
'title' => 'Title', |
| 87 |
'code' => 'Shortcode', |
| 88 |
'code-alt' => 'Alternative Shortcode', |
| 89 |
'edit' => 'Edit', |
| 90 |
'delete' => 'Delete', |
| 91 |
'duplicate' => 'Duplicate', |
| 92 |
); |
| 93 |
return $columns; |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Define the sortable columns |
| 99 |
* |
| 100 |
* @return Array |
| 101 |
*/ |
| 102 |
public function get_sortable_columns() |
| 103 |
{ |
| 104 |
return array( |
| 105 |
'ID' => array( 'ID', false ), |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Gets the name of the primary column. |
| 111 |
* |
| 112 |
* @since 1.0 |
| 113 |
* @access protected |
| 114 |
* |
| 115 |
* @return string Name of the primary column. |
| 116 |
*/ |
| 117 |
protected function get_primary_column_name() { |
| 118 |
return 'ID'; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Retrieves the search query string |
| 123 |
* |
| 124 |
* @access public |
| 125 |
* @since 1.0 |
| 126 |
* @return mixed string If search is present, false otherwise |
| 127 |
*/ |
| 128 |
public function get_search() { |
| 129 |
return ! empty( $_POST['s'] ) ? urldecode( trim( $_POST['s'] ) ) : false; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
/** |
| 134 |
* Retrieve the current page number |
| 135 |
* |
| 136 |
* @access public |
| 137 |
* @since 1.5 |
| 138 |
* @return int Current page number |
| 139 |
*/ |
| 140 |
public function get_paged() { |
| 141 |
return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Prepare the items for the table to process |
| 146 |
* |
| 147 |
* @return Void |
| 148 |
*/ |
| 149 |
public function prepare_items() |
| 150 |
{ |
| 151 |
$columns = $this->get_columns(); |
| 152 |
$hidden = $this->get_hidden_columns(); |
| 153 |
$sortable = $this->get_sortable_columns(); |
| 154 |
$data = $this->table_data(); |
| 155 |
$perPage = 30; |
| 156 |
$currentPage = $this->get_pagenum(); |
| 157 |
if ($data){ |
| 158 |
usort( $data, array( &$this, 'sort_data' ) ); |
| 159 |
$data = array_slice( $data, ( ( $currentPage-1 ) * $perPage ), $perPage ); |
| 160 |
} |
| 161 |
$totalItems = $this->list_count(); |
| 162 |
$this->_column_headers = array( $columns, $hidden, $sortable ); |
| 163 |
$this->items = $data; |
| 164 |
$this->set_pagination_args( array( |
| 165 |
'total_items' => $totalItems, |
| 166 |
'per_page' => $perPage, |
| 167 |
'total_pages' => ceil( $totalItems / $perPage ), |
| 168 |
) ); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Define which columns are hidden |
| 173 |
* |
| 174 |
* @return Array |
| 175 |
*/ |
| 176 |
public function get_hidden_columns() |
| 177 |
{ |
| 178 |
return array(); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Render the checkbox column |
| 183 |
* |
| 184 |
* @access public |
| 185 |
* @since 1.0 |
| 186 |
* @param array $item Contains all the data for the checkbox column |
| 187 |
* @return string Displays a checkbox |
| 188 |
*/ |
| 189 |
public function column_cb( $item ) { |
| 190 |
return sprintf( |
| 191 |
'<input type="checkbox" name="%1$s[]" value="%2$s" />', |
| 192 |
'ID', |
| 193 |
$item['ID'] |
| 194 |
); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Get the table data |
| 199 |
* |
| 200 |
* @return Array |
| 201 |
*/ |
| 202 |
private function table_data() |
| 203 |
{ |
| 204 |
global $wpdb; |
| 205 |
$data = array(); |
| 206 |
$paged = $this->get_paged(); |
| 207 |
$offset = $this->per_page * ( $paged - 1 ); |
| 208 |
$search = $this->get_search(); |
| 209 |
|
| 210 |
$table = $this->data; |
| 211 |
|
| 212 |
if ( ! $search || empty( $search ) ) { |
| 213 |
$resultat = $wpdb->get_results( "SELECT * FROM " . $table . " order by id desc" ); |
| 214 |
} elseif ( is_numeric( $search ) ) { |
| 215 |
$query = $wpdb->prepare( "SELECT * FROM $table WHERE id=%d", absint( $search ) ); |
| 216 |
$resultat = $wpdb->get_results( $query ); |
| 217 |
} else { |
| 218 |
$query = $wpdb->prepare( "SELECT * FROM $table WHERE title=%s order by id desc", sanitize_text_field( $search ) ); |
| 219 |
$resultat = $wpdb->get_results( $query ); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
if ( $resultat ) { |
| 224 |
foreach ( $resultat as $key => $value ) { |
| 225 |
$title = !empty( $value->title ) ? $value->title : '<em>' . __('Untitle', 'wpcoder') . '</em>'; |
| 226 |
$delete_url = admin_url( '/admin.php?page=' . $this->pluginname . '&info=del&did=' . absint( $value->id ) ); |
| 227 |
|
| 228 |
$data[] = array( |
| 229 |
'ID' => $value->id, |
| 230 |
'title' => '<a href="admin.php?page=' . esc_attr($this->pluginname) . '&tab=add_new&act=update&id=' . absint($value->id) . '">' . $title . '</a>', |
| 231 |
'code' => '[' . esc_attr($this->shortcode) . ' id="' . absint($value->id) . '"]', |
| 232 |
'code-alt' => '[' . esc_attr($this->shortcode) . ' title="' . esc_attr($title) . '"]', |
| 233 |
'edit' => '<a href="admin.php?page=' . esc_attr($this->pluginname) . '&tab=add_new&act=update&id=' . absint($value->id) . '">edit</a>', |
| 234 |
'delete' => '<a href="'.esc_url( wp_nonce_url( $delete_url, 'wp_coder_nonce' ) ).'" style="color:red;">delete</a>', |
| 235 |
'duplicate' => '<a href="admin.php?page=' . esc_attr($this->pluginname) . '&tab=add_new&act=duplicate&id=' . absint($value->id) . '" style="color:green;">duplicate</a>', |
| 236 |
); |
| 237 |
} |
| 238 |
} |
| 239 |
return $data; |
| 240 |
} |
| 241 |
|
| 242 |
public function list_count() { |
| 243 |
global $wpdb; |
| 244 |
$data = $this->data; |
| 245 |
$resultat = $wpdb->get_results( "SELECT * FROM " . $data . " order by id asc" ); |
| 246 |
|
| 247 |
return count( $resultat ); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Allows you to sort the data by the variables set in the $_GET |
| 252 |
* |
| 253 |
* @return Mixed |
| 254 |
*/ |
| 255 |
private function sort_data( $a, $b ) |
| 256 |
{ |
| 257 |
// If no sort, default to title |
| 258 |
$orderby = ( ! empty( $_GET['orderby'] ) ) ? sanitize_text_field( $_GET['orderby'] ) : 'ID'; |
| 259 |
// If no order, default to asc |
| 260 |
$order = ( ! empty( $_GET['order'] ) ) ? sanitize_text_field( $_GET['order'] ) : 'desc'; |
| 261 |
// Determine sort order |
| 262 |
$result = strnatcmp( $a[$orderby], $b[$orderby] ); |
| 263 |
// Send final sort direction to usort |
| 264 |
return ( $order === 'asc' ) ? $result : -$result; |
| 265 |
|
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Retrieve the bulk actions |
| 270 |
* |
| 271 |
* @access public |
| 272 |
* @since 1.4 |
| 273 |
* @return array $actions Array of the bulk actions |
| 274 |
*/ |
| 275 |
public function get_bulk_actions() { |
| 276 |
return array( |
| 277 |
'wow-delete-items' => 'Delate', |
| 278 |
); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Process the bulk actions |
| 283 |
* |
| 284 |
* @access public |
| 285 |
* @since 1.4 |
| 286 |
* @return void |
| 287 |
*/ |
| 288 |
public function process_bulk_action() { |
| 289 |
$ids = isset( $_POST['ID'] ) ? map_deep($_POST['ID'], 'absint') : false; |
| 290 |
$action = $this->current_action(); |
| 291 |
if ( ! is_array( $ids ) ) { |
| 292 |
$ids = array( $ids ); |
| 293 |
} |
| 294 |
if ( empty( $action ) ) { |
| 295 |
return; |
| 296 |
} |
| 297 |
global $wpdb; |
| 298 |
$table = $this->data; |
| 299 |
foreach ( $ids as $id ) { |
| 300 |
if ( 'wow-delete-items' === $this->current_action() ) { |
| 301 |
$wpdb->query("delete from " . $table . " where id=" . $id); |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
} |
| 307 |
|