Skip to content

Instantly share code, notes, and snippets.

@kimcoleman
Created December 12, 2024 15:26
Show Gist options
  • Save kimcoleman/8b706f0ab19686514472ffc98335d6bf to your computer and use it in GitHub Desktop.
Save kimcoleman/8b706f0ab19686514472ffc98335d6bf to your computer and use it in GitHub Desktop.
Shortcode for Units directory. Used for the Condo Owner's Association Demo at https://coa.pmproplugin.com/
<?php
/**
* Shortcode to display a table of units, owners, and residents.
*/
function condo_units_table_shortcode( $atts ) {
global $wpdb;
// Shortcode attributes with defaults
$atts = shortcode_atts( array(
'meta_key' => 'unit', // The usermeta key to retrieve unit numbers
), $atts, 'condo_units_table' );
// Sanitize the meta_key
$meta_key = sanitize_text_field( $atts['meta_key'] );
// Get all owners (Level ID 1)
$owners = $wpdb->get_results(
$wpdb->prepare(
"SELECT u.ID AS user_id, um.meta_value AS unit
FROM {$wpdb->users} u
INNER JOIN {$wpdb->usermeta} um ON u.ID = um.user_id
INNER JOIN {$wpdb->pmpro_memberships_users} mu ON u.ID = mu.user_id
WHERE um.meta_key = %s
AND mu.membership_id = 1
AND mu.status = 'active'
AND um.meta_value IS NOT NULL AND um.meta_value <> ''
ORDER BY unit ASC",
$meta_key
)
);
if ( empty( $owners ) ) {
return '<p>No owners found.</p>';
}
ob_start(); // Start output buffering
?>
<div class="pmpro"><div class="pmpro_card"><div class="pmpro_card_content">
<table class="pmpro_table condo_units_table">
<thead>
<tr>
<th class="pmpro_member_directory_unit">Unit</th>
<th>Owner Information</th>
<th>Status</th>
<th>Resident Information</th>
</tr>
</thead>
<tbody>
<?php foreach ( $owners as $owner ) :
$owner_data = get_userdata( $owner->user_id );
// Get the resident (Level ID 3) for this unit
$resident = $wpdb->get_row(
$wpdb->prepare(
"SELECT u.ID AS user_id
FROM {$wpdb->users} u
INNER JOIN {$wpdb->usermeta} um ON u.ID = um.user_id
INNER JOIN {$wpdb->pmpro_memberships_users} mu ON u.ID = mu.user_id
WHERE um.meta_key = %s
AND um.meta_value = %s
AND mu.membership_id = 3
AND mu.status = 'active'",
$meta_key,
$owner->unit
)
);
$resident_data = $resident ? get_userdata( $resident->user_id ) : null;
// Check if the owner is also the resident
$is_owner_occupied = $resident && $resident->user_id === $owner->user_id;
?>
<tr>
<td><?php echo esc_html( $owner->unit ); ?></td>
<td>
<?php echo esc_html( $owner_data->display_name ); ?><br>
<small><?php echo esc_html( $owner_data->user_email ); ?></small>
</td>
<td>
<?php echo esc_html( pmpromd_get_display_value( 'condo_status', $owner_data ) ); ?></td>
<td>
<?php if ( $is_owner_occupied ) : ?>
Owner Occupied
<?php elseif ( $resident_data ) : ?>
<?php echo esc_html( $resident_data->display_name ); ?><br>
<small><?php echo esc_html( $resident_data->user_email ); ?></small>
<?php else : ?>
Vacant
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div></div></div>
<?php
return ob_get_clean(); // Return the table HTML
}
add_shortcode( 'condo_units_table', 'condo_units_table_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment