Last active
March 14, 2024 13:19
-
-
Save andostronaut/8389c81a88afec8baf74c51c3b91112d to your computer and use it in GitHub Desktop.
Create custom table with WP_Liste_Table for star
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: Star Candidats | |
* Description: Liste des candidats qui ont postuler sur les offres star. | |
* Plugin URI: https://gist.github.com/iamando/8389c81a88afec8baf74c51c3b91112d | |
* Author: Ando | |
* Author URI: https://github.com/iamando | |
* Version: 1.0 | |
* License: GPL2 | |
*/ | |
if (is_admin()) { | |
new Candidates_Wp_List_Table(); | |
} | |
/** | |
* Candidates_Wp_List_Table class will create the page to load the table | |
*/ | |
class Candidates_Wp_List_Table | |
{ | |
/** | |
* Constructor will create the menu item | |
*/ | |
public function __construct() | |
{ | |
add_action('admin_menu', array($this, 'add_menu_candidats_list_table_page')); | |
} | |
/** | |
* Menu item will allow us to load the page to display the table | |
*/ | |
public function add_menu_candidats_list_table_page() | |
{ | |
add_menu_page('Candidats', 'Candidats', 'manage_options', 'candidats', array($this, 'list_candidats_table_page'), 'dashicons-format-aside'); | |
} | |
/** | |
* Display the list table page | |
* | |
* @return Void | |
*/ | |
public function list_candidats_table_page() | |
{ | |
$candidatesListTable = new Candidats_List_Table(); | |
$candidatesListTable->prepare_items(); | |
?> | |
<div class="wrap"> | |
<div id="icon-users" class="icon32"></div> | |
<h2>Liste des candidats</h2> | |
<?php $candidatesListTable->display(); ?> | |
</div> | |
<?php | |
} | |
} | |
// WP_List_Table is not loaded automatically so we need to load it in our application | |
if (!class_exists('WP_List_Table')) { | |
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php'); | |
} | |
/** | |
* Create a new table class that will extend the WP_List_Table | |
*/ | |
class Candidats_List_Table extends WP_List_Table | |
{ | |
/** | |
* Prepare the items for the table to process | |
* | |
* @return Void | |
*/ | |
public function prepare_items() | |
{ | |
$columns = $this->get_columns(); | |
$hidden = $this->get_hidden_columns(); | |
$sortable = $this->get_sortable_columns(); | |
$data = $this->table_data(); | |
usort($data, array(&$this, 'sort_data')); | |
$perPage = 10; | |
$currentPage = $this->get_pagenum(); | |
$totalItems = count($data); | |
$this->set_pagination_args(array( | |
'total_items' => $totalItems, | |
'per_page' => $perPage | |
)); | |
$data = array_slice($data, (($currentPage - 1) * $perPage), $perPage); | |
$this->_column_headers = array($columns, $hidden, $sortable); | |
$this->items = $data; | |
} | |
/** | |
* Override the parent columns method. Defines the columns to use in your listing table | |
* | |
* @return Array | |
*/ | |
public function get_columns() | |
{ | |
$columns = array( | |
'id' => 'ID', | |
'name' => 'Nom', | |
'firstName' => 'Prenom', | |
'phone' => 'Telephone', | |
'email' => 'Email', | |
'fileUrl' => 'Fichier' | |
); | |
return $columns; | |
} | |
/** | |
* Define which columns are hidden | |
* | |
* @return Array | |
*/ | |
public function get_hidden_columns() | |
{ | |
return array(); | |
} | |
/** | |
* Define the sortable columns | |
* | |
* @return Array | |
*/ | |
public function get_sortable_columns() | |
{ | |
return array('name' => array('name', false)); | |
} | |
/** | |
* Get the table data | |
* | |
* @return Array | |
*/ | |
private function table_data() | |
{ | |
global $wpdb; | |
$table = $wpdb->prefix . 'candidates'; | |
$data = $wpdb->get_results("SELECT * FROM $table", ARRAY_A); | |
if ($data === false) { | |
echo 'Erreur lors de la requête SQL : ' . $wpdb->last_error; | |
return; | |
} | |
return $data; | |
} | |
/** | |
* Define what data to show on each column of the table | |
* | |
* @param Array $item Data | |
* @param String $column_name - Current column name | |
* | |
* @return Mixed | |
*/ | |
public function column_default($item, $column_name) | |
{ | |
switch ($column_name) { | |
case 'id': | |
case 'name': | |
case 'firstName': | |
case 'phone': | |
case 'email': | |
case 'fileUrl': | |
return $item[$column_name]; | |
default: | |
return print_r($item, true); | |
} | |
} | |
/** | |
* Allows you to sort the data by the variables set in the $_GET | |
* | |
* @return Mixed | |
*/ | |
private function sort_data($a, $b) | |
{ | |
// Set defaults | |
$orderby = 'name'; | |
$order = 'asc'; | |
// If orderby is set, use this as the sort column | |
if (!empty($_GET['orderby'])) { | |
$orderby = $_GET['orderby']; | |
} | |
// If order is set use this as the order | |
if (!empty($_GET['order'])) { | |
$order = $_GET['order']; | |
} | |
$result = strcmp($a[$orderby], $b[$orderby]); | |
if ($order === 'asc') { | |
return $result; | |
} | |
return -$result; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment