Add officials manager role and event officials column

This commit is contained in:
2026-04-02 11:28:08 -05:00
parent b2d4c240ad
commit 2baebf9c30
3 changed files with 471 additions and 2 deletions

View File

@@ -7,6 +7,89 @@ if ( ! defined( 'ABSPATH' ) ) {
exit; exit;
} }
/**
* Add an Officials column to the event admin list.
*
* @param array $columns Existing columns.
* @return array
*/
function tony_sportspress_event_add_officials_column( $columns ) {
$updated = array();
foreach ( $columns as $key => $label ) {
$updated[ $key ] = $label;
if ( 'sp_team' === $key ) {
$updated['tony_sp_officials'] = esc_html__( 'Officials', 'tonys-sportspress-enhancements' );
}
}
if ( ! isset( $updated['tony_sp_officials'] ) ) {
$updated['tony_sp_officials'] = esc_html__( 'Officials', 'tonys-sportspress-enhancements' );
}
return $updated;
}
add_filter( 'manage_edit-sp_event_columns', 'tony_sportspress_event_add_officials_column', 20 );
/**
* Build a display-ready officials map for an event.
*
* @param int $post_id Post ID.
* @return array<int, array{name: string, officials: string[]}>
*/
function tony_sportspress_event_get_officials_display( $post_id ) {
$officials_by_duty = get_post_meta( $post_id, 'sp_officials', true );
if ( ! is_array( $officials_by_duty ) || empty( $officials_by_duty ) ) {
return array();
}
$duties = get_terms(
array(
'taxonomy' => 'sp_duty',
'hide_empty' => false,
)
);
$duty_names = array();
if ( is_array( $duties ) ) {
foreach ( $duties as $duty ) {
if ( isset( $duty->term_id, $duty->name ) ) {
$duty_names[ (int) $duty->term_id ] = $duty->name;
}
}
}
$rows = array();
foreach ( $officials_by_duty as $duty_id => $official_ids ) {
$duty_id = absint( $duty_id );
$official_ids = array_filter( array_map( 'absint', (array) $official_ids ) );
if ( $duty_id <= 0 || empty( $official_ids ) ) {
continue;
}
$names = array();
foreach ( $official_ids as $official_id ) {
$title = get_the_title( $official_id );
if ( is_string( $title ) && '' !== $title ) {
$names[] = $title;
}
}
if ( empty( $names ) ) {
continue;
}
$rows[] = array(
'name' => isset( $duty_names[ $duty_id ] ) ? $duty_names[ $duty_id ] : (string) $duty_id,
'officials' => $names,
);
}
return $rows;
}
/** /**
* Print hidden officials data on each event row for quick edit prefill. * Print hidden officials data on each event row for quick edit prefill.
* *
@@ -14,7 +97,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* @param int $post_id Post ID. * @param int $post_id Post ID.
*/ */
function tony_sportspress_event_quick_edit_officials_row_data( $column, $post_id ) { function tony_sportspress_event_quick_edit_officials_row_data( $column, $post_id ) {
if ( 'sp_team' !== $column ) { if ( 'tony_sp_officials' !== $column ) {
return; return;
} }
@@ -28,6 +111,18 @@ function tony_sportspress_event_quick_edit_officials_row_data( $column, $post_id
$serialized = '{}'; $serialized = '{}';
} }
$rows = tony_sportspress_event_get_officials_display( $post_id );
if ( empty( $rows ) ) {
echo '&mdash;';
} else {
foreach ( $rows as $row ) {
echo '<div class="tony-sp-event-official-row">';
echo '<strong>' . esc_html( $row['name'] ) . ':</strong> ';
echo esc_html( implode( ', ', $row['officials'] ) );
echo '</div>';
}
}
echo '<span class="hidden tony-event-officials-data" data-officials="' . esc_attr( $serialized ) . '"></span>'; echo '<span class="hidden tony-event-officials-data" data-officials="' . esc_attr( $serialized ) . '"></span>';
} }
add_action( 'manage_sp_event_posts_custom_column', 'tony_sportspress_event_quick_edit_officials_row_data', 20, 2 ); add_action( 'manage_sp_event_posts_custom_column', 'tony_sportspress_event_quick_edit_officials_row_data', 20, 2 );
@@ -39,7 +134,7 @@ add_action( 'manage_sp_event_posts_custom_column', 'tony_sportspress_event_quick
* @param string $post_type Post type key. * @param string $post_type Post type key.
*/ */
function tony_sportspress_event_quick_edit_officials_field( $column_name, $post_type ) { function tony_sportspress_event_quick_edit_officials_field( $column_name, $post_type ) {
if ( 'sp_event' !== $post_type || 'sp_team' !== $column_name ) { if ( 'sp_event' !== $post_type || 'tony_sp_officials' !== $column_name ) {
return; return;
} }

View File

@@ -0,0 +1,371 @@
<?php
/**
* Officials Manager role and capability restrictions.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! defined( 'TONY_SPORTSPRESS_OFFICIALS_MANAGER_ROLE' ) ) {
define( 'TONY_SPORTSPRESS_OFFICIALS_MANAGER_ROLE', 'sp_officials_manager' );
}
/**
* Build the primitive capabilities for a custom post type.
*
* @param string $singular Singular capability base.
* @param string $plural Plural capability base.
* @return string[]
*/
function tony_sportspress_build_post_type_caps( $singular, $plural ) {
return array(
"edit_{$singular}",
"read_{$singular}",
"delete_{$singular}",
"edit_{$plural}",
"edit_others_{$plural}",
"publish_{$plural}",
"read_private_{$plural}",
"delete_{$plural}",
"delete_private_{$plural}",
"delete_published_{$plural}",
"delete_others_{$plural}",
"edit_private_{$plural}",
"edit_published_{$plural}",
);
}
/**
* Get officials manager role capabilities.
*
* @return array<string, bool>
*/
function tony_sportspress_get_officials_manager_caps() {
$caps = array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
'edit_published_posts' => true,
);
foreach ( tony_sportspress_build_post_type_caps( 'sp_official', 'sp_officials' ) as $cap ) {
$caps[ $cap ] = true;
}
// Allow access to the event list and quick edit for assignments, without full event management.
$caps['read_sp_event'] = true;
$caps['edit_sp_event'] = true;
$caps['edit_sp_events'] = true;
$caps['edit_others_sp_events'] = true;
$caps['edit_published_sp_events'] = true;
$caps['read_private_sp_events'] = true;
return $caps;
}
/**
* Get the capabilities managed for the officials manager role.
*
* @return string[]
*/
function tony_sportspress_get_officials_manager_managed_caps() {
return array_keys( tony_sportspress_get_officials_manager_caps() );
}
/**
* Grant custom official caps to existing roles that already have matching event caps.
*
* This preserves existing access after moving officials off the `sp_event` capability type.
*
* @return void
*/
function tony_sportspress_sync_official_caps_to_existing_roles() {
global $wp_roles;
if ( ! class_exists( 'WP_Roles' ) ) {
return;
}
if ( ! isset( $wp_roles ) ) {
$wp_roles = wp_roles();
}
if ( ! $wp_roles instanceof WP_Roles ) {
return;
}
$cap_map = array(
'edit_sp_event' => 'edit_sp_official',
'read_sp_event' => 'read_sp_official',
'delete_sp_event' => 'delete_sp_official',
'edit_sp_events' => 'edit_sp_officials',
'edit_others_sp_events' => 'edit_others_sp_officials',
'publish_sp_events' => 'publish_sp_officials',
'read_private_sp_events' => 'read_private_sp_officials',
'delete_sp_events' => 'delete_sp_officials',
'delete_private_sp_events' => 'delete_private_sp_officials',
'delete_published_sp_events' => 'delete_published_sp_officials',
'delete_others_sp_events' => 'delete_others_sp_officials',
'edit_private_sp_events' => 'edit_private_sp_officials',
'edit_published_sp_events' => 'edit_published_sp_officials',
);
foreach ( $wp_roles->role_objects as $role ) {
if ( ! $role instanceof WP_Role ) {
continue;
}
foreach ( $cap_map as $event_cap => $official_cap ) {
if ( $role->has_cap( $event_cap ) ) {
$role->add_cap( $official_cap );
}
}
}
}
/**
* Create or update the officials manager role.
*
* @return void
*/
function tony_sportspress_sync_officials_manager_roles() {
$role = get_role( TONY_SPORTSPRESS_OFFICIALS_MANAGER_ROLE );
if ( ! $role ) {
$role = add_role(
TONY_SPORTSPRESS_OFFICIALS_MANAGER_ROLE,
__( 'Officials Manager', 'tonys-sportspress-enhancements' ),
array()
);
}
if ( ! $role instanceof WP_Role ) {
return;
}
$desired_caps = tony_sportspress_get_officials_manager_caps();
foreach ( tony_sportspress_get_officials_manager_managed_caps() as $cap ) {
if ( ! empty( $desired_caps[ $cap ] ) ) {
$role->add_cap( $cap );
} else {
$role->remove_cap( $cap );
}
}
tony_sportspress_sync_official_caps_to_existing_roles();
}
add_action( 'init', 'tony_sportspress_sync_officials_manager_roles' );
/**
* Assign custom capabilities to the officials post type.
*
* @param array $args Post type registration args.
* @return array
*/
function tony_sportspress_officials_post_type_caps( $args ) {
$args['capability_type'] = array( 'sp_official', 'sp_officials' );
$args['map_meta_cap'] = true;
$args['capabilities'] = array(
'create_posts' => 'edit_sp_officials',
);
return $args;
}
add_filter( 'sportspress_register_post_type_official', 'tony_sportspress_officials_post_type_caps' );
/**
* Determine whether the current user should be restricted to assignment-only event access.
*
* @return bool
*/
function tony_sportspress_is_officials_manager_user() {
$user = wp_get_current_user();
if ( ! $user instanceof WP_User || empty( $user->roles ) ) {
return false;
}
if ( ! in_array( TONY_SPORTSPRESS_OFFICIALS_MANAGER_ROLE, $user->roles, true ) ) {
return false;
}
if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_sportspress' ) ) {
return false;
}
return true;
}
/**
* Prevent assignment-only users from opening full event edit screens.
*
* @return void
*/
function tony_sportspress_lock_event_editor_for_officials_manager() {
global $pagenow;
if ( ! is_admin() || ! tony_sportspress_is_officials_manager_user() ) {
return;
}
if ( 'post-new.php' === $pagenow ) {
$post_type = isset( $_GET['post_type'] ) ? sanitize_key( wp_unslash( $_GET['post_type'] ) ) : 'post';
if ( 'sp_event' === $post_type ) {
wp_safe_redirect( admin_url( 'edit.php?post_type=sp_event&tse_event_editor_locked=1' ) );
exit;
}
}
if ( 'post.php' !== $pagenow ) {
return;
}
$post_id = isset( $_GET['post'] ) ? absint( wp_unslash( $_GET['post'] ) ) : 0;
if ( $post_id > 0 && 'sp_event' === get_post_type( $post_id ) ) {
wp_safe_redirect( admin_url( 'edit.php?post_type=sp_event&tse_event_editor_locked=1' ) );
exit;
}
}
add_action( 'admin_init', 'tony_sportspress_lock_event_editor_for_officials_manager' );
/**
* Show an admin notice when event editor access is blocked.
*
* @return void
*/
function tony_sportspress_officials_manager_admin_notice() {
if ( ! tony_sportspress_is_officials_manager_user() ) {
return;
}
if ( empty( $_GET['tse_event_editor_locked'] ) ) {
return;
}
echo '<div class="notice notice-info is-dismissible"><p>' . esc_html__( 'Officials Managers can assign officials from the events list via Quick Edit, but cannot open the full event editor.', 'tonys-sportspress-enhancements' ) . '</p></div>';
}
add_action( 'admin_notices', 'tony_sportspress_officials_manager_admin_notice' );
/**
* Remove event row actions that would expose broader editing.
*
* @param array $actions Row actions.
* @param WP_Post $post Post object.
* @return array
*/
function tony_sportspress_limit_event_row_actions_for_officials_manager( $actions, $post ) {
if ( ! tony_sportspress_is_officials_manager_user() || ! $post instanceof WP_Post || 'sp_event' !== $post->post_type ) {
return $actions;
}
$allowed = array();
if ( isset( $actions['inline hide-if-no-js'] ) ) {
$allowed['inline hide-if-no-js'] = $actions['inline hide-if-no-js'];
}
if ( isset( $actions['view'] ) ) {
$allowed['view'] = $actions['view'];
}
return $allowed;
}
add_filter( 'post_row_actions', 'tony_sportspress_limit_event_row_actions_for_officials_manager', 10, 2 );
/**
* Remove bulk actions from the events list for assignment-only users.
*
* @param array $actions Bulk actions.
* @return array
*/
function tony_sportspress_limit_event_bulk_actions_for_officials_manager( $actions ) {
if ( ! tony_sportspress_is_officials_manager_user() ) {
return $actions;
}
return array();
}
add_filter( 'bulk_actions-edit-sp_event', 'tony_sportspress_limit_event_bulk_actions_for_officials_manager' );
/**
* Remove the Add New events submenu for assignment-only users.
*
* @return void
*/
function tony_sportspress_limit_event_admin_menu_for_officials_manager() {
if ( ! tony_sportspress_is_officials_manager_user() ) {
return;
}
remove_submenu_page( 'edit.php?post_type=sp_event', 'post-new.php?post_type=sp_event' );
}
add_action( 'admin_menu', 'tony_sportspress_limit_event_admin_menu_for_officials_manager', 99 );
/**
* Hide event Add New buttons on the list screen for assignment-only users.
*
* @return void
*/
function tony_sportspress_limit_event_admin_ui_for_officials_manager() {
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
if ( ! $screen || 'edit-sp_event' !== $screen->id || ! tony_sportspress_is_officials_manager_user() ) {
return;
}
?>
<style>
.post-type-sp_event .page-title-action,
.post-type-sp_event .wrap .bulkactions {
display: none;
}
</style>
<?php
}
add_action( 'admin_head', 'tony_sportspress_limit_event_admin_ui_for_officials_manager' );
/**
* Preserve core event fields so assignment-only users cannot alter them via Quick Edit.
*
* @param array $data Sanitized post data.
* @param array $postarr Raw post array.
* @return array
*/
function tony_sportspress_protect_event_fields_for_officials_manager( $data, $postarr ) {
if ( ! is_admin() || ! tony_sportspress_is_officials_manager_user() ) {
return $data;
}
if ( empty( $postarr['ID'] ) || 'sp_event' !== $data['post_type'] ) {
return $data;
}
$existing_post = get_post( (int) $postarr['ID'], ARRAY_A );
if ( ! is_array( $existing_post ) ) {
return $data;
}
$protected_fields = array(
'post_author',
'post_content',
'post_content_filtered',
'post_date',
'post_date_gmt',
'post_excerpt',
'post_name',
'post_parent',
'post_password',
'post_status',
'post_title',
);
foreach ( $protected_fields as $field ) {
if ( isset( $existing_post[ $field ] ) ) {
$data[ $field ] = $existing_post[ $field ];
}
}
return $data;
}
add_filter( 'wp_insert_post_data', 'tony_sportspress_protect_event_fields_for_officials_manager', 20, 2 );

View File

@@ -29,6 +29,7 @@ if ( ! defined( 'TONY_SPORTSPRESS_ENHANCEMENTS_URL' ) ) {
} }
// Include other files here // Include other files here
require_once plugin_dir_path(__FILE__) . 'includes/sp-officials-manager-role.php';
require_once plugin_dir_path(__FILE__) . 'includes/open-graph-tags.php'; require_once plugin_dir_path(__FILE__) . 'includes/open-graph-tags.php';
require_once plugin_dir_path(__FILE__) . 'includes/featured-image-generator.php'; require_once plugin_dir_path(__FILE__) . 'includes/featured-image-generator.php';
require_once plugin_dir_path(__FILE__) . 'includes/sp-event-permalink.php'; require_once plugin_dir_path(__FILE__) . 'includes/sp-event-permalink.php';
@@ -41,3 +42,5 @@ require_once plugin_dir_path(__FILE__) . 'includes/sp-printable-calendars.php';
require_once plugin_dir_path(__FILE__) . 'includes/sp-url-builder.php'; require_once plugin_dir_path(__FILE__) . 'includes/sp-url-builder.php';
require_once plugin_dir_path(__FILE__) . 'includes/sp-schedule-exporter.php'; require_once plugin_dir_path(__FILE__) . 'includes/sp-schedule-exporter.php';
require_once plugin_dir_path(__FILE__) . 'includes/sp-venue-meta.php'; require_once plugin_dir_path(__FILE__) . 'includes/sp-venue-meta.php';
register_activation_hook( __FILE__, 'tony_sportspress_sync_officials_manager_roles' );