Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mlbd/4aa0eb833b96ce610a9ef08b8b25b051 to your computer and use it in GitHub Desktop.
Save mlbd/4aa0eb833b96ce610a9ef08b8b25b051 to your computer and use it in GitHub Desktop.
/**
* Get the team_id for a user from the {prefix}fc_team_members table.
*
* @param int $user_id User ID.
* @return int|false Team ID or false if none.
*/
function owf_helmy_get_user_team_id( $user_id ) {
global $wpdb;
$table = $wpdb->prefix . 'fc_team_members';
$prepared = $wpdb->prepare( "SELECT team_id FROM {$table} WHERE user_id = %d LIMIT 1", (int) $user_id );
$team_id = $wpdb->get_var( $prepared );
return $team_id ? (int) $team_id : false;
}
/**
* Get the team name by team_id from {prefix}fc_teams table.
*
* @param int $team_id Team ID.
* @return string Team name or 'Unknown' if not found.
*/
function owf_helmy_get_team_name_by_id( $team_id ) {
global $wpdb;
$table = $wpdb->prefix . 'fc_teams';
$prepared = $wpdb->prepare( "SELECT name FROM {$table} WHERE id = %d", (int) $team_id );
$team_name = $wpdb->get_var( $prepared );
return $team_name ? $team_name : esc_html__( 'Unknown', 'your-text-domain' );
}
/**
* Helper: Get Oasis Workflow-enabled post types or fallback to 'post'.
*
* @return string[] Post type slugs.
*/
function owf_helmy_get_oasiswf_enabled_post_types() {
$types = get_option( 'oasiswf_show_wfsettings_on_post_types' );
return ( is_array( $types ) && ! empty( $types ) ) ? array_values( $types ) : array( 'post' );
}
/**
* Resolve which team_id to assign for a post.
* Prefer post author's team; fallback to current user's team.
*
* @param WP_Post $post Post object.
* @return int Team ID or 0 if none found.
*/
function owf_helmy_resolve_team_for_post( $post ) {
$author_id = (int) $post->post_author;
if ( $author_id ) {
$team_id = owf_helmy_get_user_team_id( $author_id );
if ( $team_id ) {
return (int) $team_id;
}
}
$current_id = get_current_user_id();
if ( $current_id ) {
$team_id = owf_helmy_get_user_team_id( $current_id );
if ( $team_id ) {
return (int) $team_id;
}
}
return 0;
}
/**
* On creation: set _team_id from author (fallback current user).
*
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated.
*/
function owf_helmy_save_post_team_id( $post_id, $post, $update ) {
// Skip autosaves and revisions.
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) {
return;
}
// Only for Oasis-enabled post types.
$oasis_post_types = owf_helmy_get_oasiswf_enabled_post_types();
if ( ! in_array( $post->post_type, $oasis_post_types, true ) ) {
return;
}
// On creation only, and only if not already set.
if ( ! $update && ! metadata_exists( 'post', $post_id, '_team_id' ) ) {
$team_id = owf_helmy_resolve_team_for_post( $post );
if ( $team_id ) {
update_post_meta( $post_id, '_team_id', (int) $team_id );
}
}
}
add_action( 'save_post', 'owf_helmy_save_post_team_id', 10, 3 );
/**
* When the post author changes, update _team_id accordingly.
* Prefer new author's team; fallback to current user's team.
* If neither has a team, keep existing meta unchanged.
*
* @param int $post_id Post ID.
* @param WP_Post $post_after Post object following the update.
* @param WP_Post $post_before Post object before the update.
*/
function owf_helmy_sync_team_on_author_change( $post_id, $post_after, $post_before ) {
// Skip autosaves and revisions.
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) {
return;
}
// Only for Oasis-enabled post types.
$oasis_post_types = owf_helmy_get_oasiswf_enabled_post_types();
if ( ! in_array( $post_after->post_type, $oasis_post_types, true ) ) {
return;
}
// Only act if author actually changed.
if ( (int) $post_after->post_author === (int) $post_before->post_author ) {
return;
}
$team_id = owf_helmy_resolve_team_for_post( $post_after );
if ( $team_id ) {
update_post_meta( $post_id, '_team_id', (int) $team_id );
}
}
add_action( 'post_updated', 'owf_helmy_sync_team_on_author_change', 10, 3 );
/**
* Restrict admin post list so users only see posts for their team.
* Applies to specific team roles and only for Oasis-enabled post types.
*
* @param WP_Query $query The WP_Query instance (passed by reference).
*/
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' );
$current_post_type = $current_post_type ? $current_post_type : 'post';
if ( ! in_array( $current_post_type, $oasis_post_types, true ) ) {
return;
}
$user = wp_get_current_user();
$restrict_roles = array(
'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;
}
$team_id = owf_helmy_get_user_team_id( $user->ID );
if ( $team_id ) {
$meta_query = $query->get( 'meta_query' );
$meta_query = is_array( $meta_query ) ? $meta_query : array();
$meta_query[] = array(
'key' => '_team_id',
'value' => (string) (int) $team_id,
'compare' => '=',
);
$query->set( 'meta_query', $meta_query );
} else {
// No team: return empty list.
$query->set( 'post__in', array( 0 ) );
}
}
add_action( 'pre_get_posts', 'owf_helmy_pre_get_posts_team_restrict' );
/* -------------------------------------------------------------------------------------------------
* OPTIONAL: Admin column to show Team name in list tables
* ------------------------------------------------------------------------------------------------- */
/**
* Add "Team" column to the post list for Oasis-enabled post types.
*
* @param array $columns Existing columns.
* @return array
*/
function owf_helmy_add_team_column( $columns ) {
$columns['team_id'] = esc_html__( 'Team', 'your-text-domain' );
return $columns;
}
/**
* Populate the "Team" column with the resolved team name.
*
* @param string $column Column key.
* @param int $post_id Post ID.
*/
function owf_helmy_render_team_column( $column, $post_id ) {
if ( 'team_id' !== $column ) {
return;
}
$team_id = get_post_meta( $post_id, '_team_id', true );
if ( $team_id ) {
echo esc_html( owf_helmy_get_team_name_by_id( (int) $team_id ) );
} else {
echo '–';
}
}
/**
* Register Team column hooks for all relevant post types.
*/
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