Created
August 8, 2025 08:20
-
-
Save mlbd/ac17b013a7e031a263349d5c101d5b7f to your computer and use it in GitHub Desktop.
This snippet restricts post visibility in the WordPress admin based on the user’s team (using Oasis Workflow Pro Teams table), and automatically tags posts with the creator’s team.
This file contains hidden or 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
/** | |
* Oasis Workflow: Per-Team Post Visibility & Admin Columns | |
* --------------------------------------------------------- | |
* - Restricts admin post list to show only posts for user's team (editors/authors) | |
* - Sets _team_id meta on new posts for configured post types | |
* - Shows "Team" column (with name) in admin for each Oasis-enabled post type | |
* | |
* @author Your Name | |
* @license GPL-2.0+ | |
*/ | |
// Get the team_id for a user from the fc_team_members table. | |
function owf_helmy_get_user_team_id($user_id) { | |
global $wpdb; | |
$table = $wpdb->prefix . 'fc_team_members'; | |
$query = $wpdb->prepare("SELECT team_id FROM $table WHERE user_id = %d LIMIT 1", $user_id); | |
$team_id = $wpdb->get_var($query); | |
return $team_id ? (int) $team_id : false; | |
} | |
// Get the team name by team_id from fc_teams table. | |
function owf_helmy_get_team_name_by_id($team_id) { | |
global $wpdb; | |
$table = $wpdb->prefix . 'fc_teams'; | |
$team_name = $wpdb->get_var($wpdb->prepare("SELECT name FROM $table WHERE id = %d", $team_id)); | |
return $team_name ?: 'Unknown'; | |
} | |
// Helper: Get Oasis Workflow-enabled post types or fallback to 'post' | |
function owf_helmy_get_oasiswf_enabled_post_types() { | |
$types = get_option('oasiswf_show_wfsettings_on_post_types'); | |
return (is_array($types) && !empty($types)) ? $types : ['post']; | |
} | |
// Automatically assign user's team_id to _team_id post meta on new post creation. | |
function owf_helmy_save_post_team_id($post_id, $post, $update) { | |
if ($update) return; | |
$oasis_post_types = owf_helmy_get_oasiswf_enabled_post_types(); | |
if (!in_array($post->post_type, $oasis_post_types, true)) return; | |
$user_id = get_current_user_id(); | |
$team_id = owf_helmy_get_user_team_id($user_id); | |
if ($team_id) { | |
update_post_meta($post_id, '_team_id', $team_id); | |
} | |
} | |
add_action('save_post', 'owf_helmy_save_post_team_id', 10, 3); | |
// Restrict admin post list so users only see posts for their team. | |
function owf_helmy_pre_get_posts_team_restrict($query) { | |
if (!is_admin() || ! $query->is_main_query() || current_user_can('manage_options')) { | |
return; | |
} | |
$oasis_post_types = owf_helmy_get_oasiswf_enabled_post_types(); | |
$current_post_type = $query->get('post_type') ?: 'post'; | |
if (!in_array($current_post_type, $oasis_post_types, true)) { | |
return; | |
} | |
$user = wp_get_current_user(); | |
$restrict_roles = [ | |
'team_a_content_editor', | |
'team_b_content_editor', | |
'team_c_content_editor', | |
'team_d_content_editor', | |
'team_e_content_editor', | |
'team_a_content_approver', | |
'team_b_content_approver', | |
'team_c_content_approver', | |
'team_d_content_approver', | |
'team_e_content_approver', | |
]; | |
if (!array_intersect($restrict_roles, (array) $user->roles)) { | |
return; | |
} | |
$user_id = $user->ID; | |
$team_id = owf_helmy_get_user_team_id($user_id); | |
if ($team_id) { | |
$meta_query = $query->get('meta_query', []); | |
$meta_query[] = [ | |
'key' => '_team_id', | |
'value' => strval($team_id), | |
'compare' => '=' | |
]; | |
$query->set('meta_query', $meta_query); | |
} else { | |
$query->set('post__in', [0]); | |
} | |
} | |
add_action('pre_get_posts', 'owf_helmy_pre_get_posts_team_restrict'); | |
/*---------------------------------------------------------------------------------------------------* | |
| NOTE: Below code is optional. Comment out if not needed Or remove if not needed. | |
*---------------------------------------------------------------------------------------------------*/ | |
// Add "Team" column to the post list for all Oasis Workflow-enabled post types. --- OPTIONAL | |
function owf_helmy_add_team_column($columns) { | |
$columns['team_id'] = 'Team'; | |
return $columns; | |
} | |
// Populate the "Team" column in the post list. -- OPTIONAL | |
function owf_helmy_render_team_column($column, $post_id) { | |
if ($column === 'team_id') { | |
$team_id = get_post_meta($post_id, '_team_id', true); | |
echo $team_id ? esc_html(owf_helmy_get_team_name_by_id($team_id)) : '-'; | |
} | |
} | |
// Register Team column hooks for all relevant post types. -- OPTIONAL | |
function owf_helmy_register_team_column_hooks() { | |
foreach (owf_helmy_get_oasiswf_enabled_post_types() as $post_type) { | |
add_filter("manage_{$post_type}_posts_columns", 'owf_helmy_add_team_column'); | |
add_action("manage_{$post_type}_posts_custom_column", 'owf_helmy_render_team_column', 10, 2); | |
} | |
} | |
add_action('admin_init', 'owf_helmy_register_team_column_hooks'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment