Skip to content

Instantly share code, notes, and snippets.

@edurodriguesdias
Last active July 23, 2019 17:56
Show Gist options
  • Select an option

  • Save edurodriguesdias/18db59ebd055138669ed0ac82febfcfa to your computer and use it in GitHub Desktop.

Select an option

Save edurodriguesdias/18db59ebd055138669ed0ac82febfcfa to your computer and use it in GitHub Desktop.

Revisions

  1. edurodriguesdias revised this gist Jul 23, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion list.php
    Original file line number Diff line number Diff line change
    @@ -202,7 +202,7 @@ function display_rows()
    require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
    }

    $wp_list_table = new Link_List_Table();
    $wp_list_table = new Lista_Table();
    $wp_list_table->prepare_items();
    ?>

  2. edurodriguesdias revised this gist Jul 23, 2019. 1 changed file with 119 additions and 68 deletions.
    187 changes: 119 additions & 68 deletions list.php
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,16 @@
    <?php
    <?php

    class Link_List_Table extends WP_List_Table {
    class Lista_Table extends WP_List_Table
    {

    public $items;

    function __construct() {
    function __construct()
    {
    parent::__construct(
    array(
    'singular'=> 'wp_list_text_link', //Singular label
    'plural' => 'wp_list_test_links', //plural label, also this well be one of the table css class
    'singular' => 'lb_list_professional', //Singular label
    'plural' => 'lb_list_professionals', //plural label, also this well be one of the table css class
    'ajax' => false //We won't support Ajax for this table
    )
    );
    @@ -18,96 +20,108 @@ function __construct() {
    * Add extra markup in the toolbars before or after the list
    * @param string $which, helps you decide if you add the markup after (bottom) or before (top) the list
    */
    function extra_tablenav( $which ) {
    function extra_tablenav($which)
    {

    if ( $which == "bottom" ){
    //The code that goes after the table is there
    echo"<small>Licenciamento Brasil &copy;</small>";
    if( $which == "top" ){
    include LBRASILPREST_PLUGIN_DIR . '/pages/admin/chunks/form_search.php';
    }


    if ($which == "bottom") {
    //The code that goes after the table is there
    echo "<small>Licenciamento Brasil &copy;</small>";
    }
    }

    /**
    * Define the columns that are going to be used in the table
    * @return array $columns, the array of columns to use with the table
    */
    function get_columns() {
    function get_columns()
    {
    return array(
    'ID'=>__('ID'),
    'NAME'=>__('NAME'),
    'col_link_url'=>__('Url'),
    'col_link_description'=>__('Description'),
    'col_link_visible'=>__('Visible')
    'NAME' => __('NAME'),
    'CNPJ' => __('CNPJ'),
    'RESPONSAVEL' => __('RESPONSAVEL'),
    'ESTADO' => __('ESTADO'),
    'CIDADE' => __('CIDADE'),
    );
    }
    }

    /**
    * Decide which columns to activate the sorting functionality on
    * @return array $sortable, the array of columns that can be sorted by the user
    */
    public function get_sortable_columns() {
    return $sortable = array(
    'id'=>'id',
    'col_link_name'=>'link_name',
    'col_link_visible'=>'link_visible'
    );
    }
    public function get_sortable_columns()
    {
    return array();
    }

    /**
    * Prepare the table with different parameters, pagination, columns and table elements
    */
    function prepare_items() {

    function prepare_items()
    {
    global $wpdb, $_wp_column_headers;

    $screen = get_current_screen();

    /* -- Preparing your query -- */
    // $query = "
    // SELECT * FROM $wpdb->users AS USR
    // INNER JOIN $wpdb->usermeta ON
    // ";
    $query = "
    SELECT * FROM $wpdb->users AS USR
    INNER JOIN $wpdb->usermeta AS USRM ON USR.ID = USRM.user_id
    WHERE USRM.meta_key = 'user_type' AND USRM.meta_value = 'lb_professional'
    ";

    /* -- Ordering parameters -- */
    //Parameters that are going to be used to order the result
    $orderby = !empty($_GET["orderby"]) ? $_GET["orderby"] : 'ASC';
    $order = !empty($_GET["order"]) ? $_GET["order"] : '';
    $orderby = !empty($_GET["orderby"]) ? $_GET["orderby"] : '';
    $order = !empty($_GET["order"]) ? $_GET["order"] : 'ASC';

    if(!empty($orderby) & !empty($order)){ $query.=' ORDER BY '.$orderby.' '.$order; }
    if (!empty($orderby) & !empty($order)) {
    $query .= ' ORDER BY ' . $orderby . ' ' . $order;
    }

    /* -- Pagination parameters -- */

    //Number of elements in your table?
    $totalitems = $wpdb->query($query); //return the total number of affected rows
    $totalitems = $wpdb->query($query);

    //How many to display per page?
    $perpage = 10;

    //Which page is this?
    $paged = !empty($_GET["paged"]) ? $_GET["paged"] : '1';
    $paged = !empty($_GET["paged"]) ? $_GET["paged"] : '';

    //Page Number
    if(empty($paged) || !is_numeric($paged) || $paged<=0 ){ $paged=1; } //How many pages do we have in total?
    $totalpages = ceil($totalitems/$perpage); //adjust the query to take pagination into account

    if(!empty($paged) && !empty($perpage)){ $offset=($paged-1)*$perpage;

    $query.=' LIMIT '.(int)$offset.','.(int)$perpage; }

    /* -- Register the pagination -- */
    $this->set_pagination_args( array(
    if (empty($paged) || !is_numeric($paged) || $paged <= 0) {
    $paged = 1;
    } //How many pages do we have in total?
    $totalpages = ceil($totalitems / $perpage); //adjust the query to take pagination into account

    if (!empty($paged) && !empty($perpage)) {
    $offset = ($paged - 1) * $perpage;

    $query .= ' LIMIT ' . (int) $offset . ',' . (int) $perpage;
    }

    /* -- Register the pagination -- */
    $this->set_pagination_args(array(
    "total_items" => $totalitems,
    "total_pages" => $totalpages,
    "per_page" => $perpage,
    ));

    //The pagination links are automatically built according to those parameters

    /* -- Register the Columns -- */
    /* Register the Columns */
    $columns = $this->get_columns();
    $_wp_column_headers[$screen->id]=$columns;

    $hidden = array();
    $sortable = $this->get_sortable_columns();
    $this->_column_headers = array($columns, $hidden, $sortable);


    /* -- Fetch the items -- */
    $this->items = $wpdb->get_results($query);
    }
    @@ -116,49 +130,86 @@ function prepare_items() {
    * Display the rows of records in the table
    * @return string, echo the markup of the rows
    */
    function display_rows() {
    function display_rows()
    {

    //Get the records registered in the prepare_items method
    $records = $this->items;

    //Get the columns registered in the get_columns and get_sortable_columns methods
    list($columns, $hidden ) = $this->get_column_info();
    list($columns, $hidden) = $this->get_column_info();

    $columns = $this->get_columns();


    //Loop for each record
    if(!empty($records)){

    foreach($records as $rec){
    if (!empty($records)) {

    foreach ($records as $rec) {

    echo '<tr id="record_' . $rec->ID . '">'; //Open the line

    echo '<tr id="record_'.$rec->ID.'">';//Open the line
    foreach ($columns as $column_name => $column_display_name) {

    foreach ( $columns as $column_name => $column_display_name ) {

    //Style attributes for each col
    $class = "class='$column_name column-$column_name'";
    $style = "";

    if ( in_array( $column_name, $hidden ) ) $style = ' style="display:none;"';
    if (in_array($column_name, $hidden)) $style = ' style="display:none;"';

    $attributes = $class . $style;
    $attributes = $class . $style;

    //edit link

    //Display the cell
    switch ( $column_name ) {
    case "ID": echo '<td '.$attributes.'>'.stripslashes($rec->ID).'</td>'; break;
    case "NAME": echo '<td '.$attributes.'>'.stripslashes($rec->display_name).'</td>'; break;
    switch ($column_name) {

    case "CNPJ":
    echo '<td ' . $attributes . '>' . (isset(get_user_meta($rec->ID, 'cnpj')[0]) ? get_user_meta($rec->ID, 'cnpj')[0] : 'N/D')
    . '</td>';
    break;

    case "RESPONSAVEL":
    echo '<td ' . $attributes . '>' . (isset(get_user_meta($rec->ID, 'holder_name')[0]) ? get_user_meta($rec->ID, 'holder_name')[0] : $rec->display_name)
    . '</td>';
    break;

    case "CIDADE":
    echo '<td ' . $attributes . '>' . (isset(get_user_meta($rec->ID, 'city')[0]) ? get_user_meta($rec->ID, 'city')[0] : $rec->display_name)
    . '</td>';
    break;

    case "ESTADO":
    echo '<td ' . $attributes . '>' . (isset(get_user_meta($rec->ID, 'state')[0]) ? get_user_meta($rec->ID, 'state')[0] : $rec->display_name)
    . '</td>';
    break;

    case "NAME":
    echo '<td ' . $attributes . '>' . (isset(get_user_meta($rec->ID, 'company_name')[0]) ? get_user_meta($rec->ID, 'company_name')[0] : $rec->display_name)
    . '</td>';
    break;
    }
    }

    echo'</tr>';//Close the line
    }}
    echo '</tr>'; //Close the line
    }
    }
    }
    }


    if (!class_exists('WP_List_Table')) {
    require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
    }

    $wp_list_table = new Link_List_Table();
    $wp_list_table->prepare_items();

    //Table of elements
    $wp_list_table->display();
    ?>

    <div class="wrap">
    <h2>Profissionais Cadastrados</h2>
    <?php
    //Table of elements
    $wp_list_table->display();
    ?>
    </div>
  3. edurodriguesdias created this gist Jul 23, 2019.
    164 changes: 164 additions & 0 deletions list.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,164 @@
    <?php

    class Link_List_Table extends WP_List_Table {

    public $items;

    function __construct() {
    parent::__construct(
    array(
    'singular'=> 'wp_list_text_link', //Singular label
    'plural' => 'wp_list_test_links', //plural label, also this well be one of the table css class
    'ajax' => false //We won't support Ajax for this table
    )
    );
    }

    /**
    * Add extra markup in the toolbars before or after the list
    * @param string $which, helps you decide if you add the markup after (bottom) or before (top) the list
    */
    function extra_tablenav( $which ) {

    if ( $which == "bottom" ){
    //The code that goes after the table is there
    echo"<small>Licenciamento Brasil &copy;</small>";
    }

    }

    /**
    * Define the columns that are going to be used in the table
    * @return array $columns, the array of columns to use with the table
    */
    function get_columns() {
    return array(
    'ID'=>__('ID'),
    'NAME'=>__('NAME'),
    'col_link_url'=>__('Url'),
    'col_link_description'=>__('Description'),
    'col_link_visible'=>__('Visible')
    );
    }

    /**
    * Decide which columns to activate the sorting functionality on
    * @return array $sortable, the array of columns that can be sorted by the user
    */
    public function get_sortable_columns() {
    return $sortable = array(
    'id'=>'id',
    'col_link_name'=>'link_name',
    'col_link_visible'=>'link_visible'
    );
    }

    /**
    * Prepare the table with different parameters, pagination, columns and table elements
    */
    function prepare_items() {

    global $wpdb, $_wp_column_headers;

    $screen = get_current_screen();

    /* -- Preparing your query -- */
    // $query = "
    // SELECT * FROM $wpdb->users AS USR
    // INNER JOIN $wpdb->usermeta ON
    // ";

    /* -- Ordering parameters -- */
    //Parameters that are going to be used to order the result
    $orderby = !empty($_GET["orderby"]) ? $_GET["orderby"] : 'ASC';
    $order = !empty($_GET["order"]) ? $_GET["order"] : '';

    if(!empty($orderby) & !empty($order)){ $query.=' ORDER BY '.$orderby.' '.$order; }

    /* -- Pagination parameters -- */

    //Number of elements in your table?
    $totalitems = $wpdb->query($query); //return the total number of affected rows

    //How many to display per page?
    $perpage = 10;

    //Which page is this?
    $paged = !empty($_GET["paged"]) ? $_GET["paged"] : '1';

    //Page Number
    if(empty($paged) || !is_numeric($paged) || $paged<=0 ){ $paged=1; } //How many pages do we have in total?
    $totalpages = ceil($totalitems/$perpage); //adjust the query to take pagination into account

    if(!empty($paged) && !empty($perpage)){ $offset=($paged-1)*$perpage;

    $query.=' LIMIT '.(int)$offset.','.(int)$perpage; }

    /* -- Register the pagination -- */
    $this->set_pagination_args( array(
    "total_items" => $totalitems,
    "total_pages" => $totalpages,
    "per_page" => $perpage,
    ));

    //The pagination links are automatically built according to those parameters

    /* -- Register the Columns -- */
    $columns = $this->get_columns();
    $_wp_column_headers[$screen->id]=$columns;

    /* -- Fetch the items -- */
    $this->items = $wpdb->get_results($query);
    }

    /**
    * Display the rows of records in the table
    * @return string, echo the markup of the rows
    */
    function display_rows() {

    //Get the records registered in the prepare_items method
    $records = $this->items;

    //Get the columns registered in the get_columns and get_sortable_columns methods
    list($columns, $hidden ) = $this->get_column_info();
    $columns = $this->get_columns();

    //Loop for each record
    if(!empty($records)){

    foreach($records as $rec){

    echo '<tr id="record_'.$rec->ID.'">';//Open the line

    foreach ( $columns as $column_name => $column_display_name ) {

    //Style attributes for each col
    $class = "class='$column_name column-$column_name'";
    $style = "";

    if ( in_array( $column_name, $hidden ) ) $style = ' style="display:none;"';

    $attributes = $class . $style;

    //edit link

    //Display the cell
    switch ( $column_name ) {
    case "ID": echo '<td '.$attributes.'>'.stripslashes($rec->ID).'</td>'; break;
    case "NAME": echo '<td '.$attributes.'>'.stripslashes($rec->display_name).'</td>'; break;
    }
    }

    echo'</tr>';//Close the line
    }}
    }
    }

    $wp_list_table = new Link_List_Table();
    $wp_list_table->prepare_items();

    //Table of elements
    $wp_list_table->display();
    ?>