How can I show a collection of users in a table along with the number of views they have?

Viewed 13

Currently, we have a route called /views. This route is responsible for showing a table.

It's currently missing the users and the number of views they have.

Upon activating the plugin we create a table called wp_views.

This table is responsible for the relationship between a view and a user.

A user can have 0 or many views.

I'm not sure how to do the following.

  • Fetch all the users and the number of views they have. If they don't have a view, show 0?

This is my entire code below.

<?php
/**
 * Plugin Name:     Test Plugin
 * Plugin URI:      
 * Description:     
 * Author:          
 * Author URI:      
 * Text Domain:     
 * Version:         0.1.0
 *
 * @package         Reece_Hewitt
 */

 if ( ! defined('ABSPATH') ) {
     exit;
 }

global $rhit_version;
$rhit_version = '0.1.0';


function rhit_install() {
    $rhit_version = '0.1.0';
    $db_version = '0.0.1';
    
    if ($rhit_version <= $db_version)
    {
        rhit_install();
    }

    global $wpdb, $rhit_version;

    $table_name = $wpdb->prefix . 'views';
    $charset_collate = $wpdb->get_charset_collate();
    $sql = <<<SQL
    CREATE TABLE $table_name (
        `id` int(9) UNSIGNED NOT NULL AUTO_INCREMENT,
        `user_id` bigint(20) UNSIGNED NOT NULL,
        PRIMARY KEY (`id`)
    ) $charset_collate;
    SQL;

    require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    dbDelta( $sql );

    add_option( 'rhit_db_version', $rhit_version );

    

    
}

function rhit_install_data() {
    global $wpdb;

    require_once ABSPATH . 'wp-load.php';

    $users = [
        [
            'user_login' => 'johndoe',
            'user_pass' => 'password',
            'user_email' => 'john@doe.com'
        ],
        [
            'user_login' => 'joedoe',
            'user_pass' => 'password',
            'user_email' => 'joe@doe.com'
        ],
        [
            'user_login' => 'jilldoe',
            'user_pass' => 'password',
            'user_email' => 'jill@doe.com'
        ],
    ];

    $table_name = $wpdb->prefix . 'views';

    foreach ( $users as $user ) {
        wp_insert_user( $user );
        if ( $user['user_login'] === 'jilldoe' ) {
            continue;
        }
        $userDb = get_user_by( 'email', $user['user_email'] );

        $wpdb->insert($table_name, ['user_id' => $userDb->ID]);
    }
}

add_action( 'init', function () {
    add_rewrite_rule('views', 'index.php?views', 'top');
} );

add_filter( 'query_vars', function( $query_vars ) {
    $query_vars[] = 'views';
    return $query_vars;
} );

add_action( 'template_include', function( $template ) {
    if ( ! preg_match('/^\/views/', $_SERVER['REQUEST_URI']) ) {
        return $template;
    }
    $html = '<style>
table.GeneratedTable {
  width: 100%;
  background-color: #ffffff;
  border-collapse: collapse;
  border-width: 2px;
  border-color: #ffcc00;
  border-style: solid;
  color: #000000;
}

table.GeneratedTable td, table.GeneratedTable th {
  border-width: 2px;
  border-color: #ffcc00;
  border-style: solid;
  padding: 3px;
}

table.GeneratedTable thead {
  background-color: #ffcc00;
}
</style>
<table class="GeneratedTable">
  <thead>
    <tr>
      <th>User</th>
      <th>Count</th>
    </tr>
  </thead>
  <tbody>';

  foreach ($users ?? [] as $user) {
      $html .= '    <tr>
      <td>Cell</td>
      <td>Cell</td>
    </tr>';
  }


    echo $html;
} );


register_activation_hook( __FILE__, 'rhit_install' );
register_activation_hook( __FILE__, 'rhit_install_data' );
0 Answers
Related