Role Manager #9

Merged
Tetrakern merged 122 commits from role_manager into main 2023-08-15 05:44:27 +08:00
2 changed files with 146 additions and 69 deletions
Showing only changes of commit 88dfb71f09 - Show all commits

View File

@ -214,8 +214,8 @@ Fictioneer customizes WordPress by using as many standard action and filter hook
| `admin_enqueue_scripts` | `fictioneer_admin_scripts`, `fictioneer_admin_styles`, `fictioneer_disable_moderator_comment_edit`, `fictioneer_hide_contributor_comments_utilities`, `fictioneer_hide_editor_comments_utilities`, `fictioneer_hide_private_data`, `fictioneer_hide_author_comments_utilities`
| `admin_head` | `fictioneer_limit_update_notice`
| `admin_head-profile.php` | `fictioneer_hide_subscriber_profile_blocks`
| `admin_init` | `fictioneer_reduce_subscriber_profile`, `fictioneer_register_settings`
| `admin_menu` | `fictioneer_add_admin_menu`, `fictioneer_reduce_moderator_admin_panel`, `fictioneer_reduce_subscriber_admin_panel`, `fictioneer_remove_menu_pages`
| `admin_init` | `fictioneer_reduce_subscriber_profile`, `fictioneer_register_settings`, `fictioneer_skip_dashboard`
| `admin_menu` | `fictioneer_add_admin_menu`, `fictioneer_reduce_moderator_admin_panel`, `fictioneer_remove_dashboard`, `fictioneer_remove_menu_pages`
| `admin_notices` | `fictioneer_admin_profile_notices`, `fictioneer_admin_settings_notices`, `fictioneer_admin_update_notice`
| `admin_post_*` | `fictioneer_purge_all_epubs`, `admin_post_purge_all_seo_schemas`, `fictioneer_purge_seo_meta_caches`, `fictioneer_tools_add_moderator_role`, `fictioneer_tools_remove_moderator_role`, `fictioneer_tools_upgrade_author_role`, `fictioneer_tools_reset_author_role`, `fictioneer_tools_upgrade_contributor_role`, `fictioneer_tools_reset_contributor_role`, `fictioneer_tools_limit_editor_role`, `fictioneer_tools_reset_editor_role`, `fictioneer_tools_move_story_tags_to_genres`, `fictioneer_tools_duplicate_story_tags_to_genres`, `fictioneer_tools_purge_story_data_caches`, `fictioneer_tools_move_chapter_tags_to_genres`, `fictioneer_tools_duplicate_chapter_tags_to_genres`, `fictioneer_tools_append_default_genres`, `fictioneer_tools_append_default_tags`, `fictioneer_tools_remove_unused_tags`, `fictioneer_tools_reset_post_relationship_registry`, `fictioneer_tools_fix_users`, `fictioneer_tools_fix_stories`, `fictioneer_tools_fix_chapters`, `fictioneer_tools_fix_collections`, `fictioneer_tools_fix_pages`, `fictioneer_tools_fix_posts`, `fictioneer_tools_fix_recommendations`, `fictioneer_admin_profile_unset_oauth`, `fictioneer_admin_profile_clear_data_node`, `fictioneer_update_frontend_profile`, `fictioneer_cancel_frontend_email_change`
| `after_setup_theme` | `fictioneer_theme_setup`
@ -229,13 +229,13 @@ Fictioneer customizes WordPress by using as many standard action and filter hook
| `edit_user_profile` | `fictioneer_custom_profile_fields`
| `edit_user_profile_update` | `fictioneer_update_admin_user_profile`, `fictioneer_update_my_user_profile`
| `get_header` | `fictioneer_maintenance_mode`
| `init` | `fictioneer_add_character_taxonomy`, `fictioneer_add_content_warning_taxonomy`, `fictioneer_add_epub_download_endpoint`, `fictioneer_add_fandom_taxonomy`, `fictioneer_add_genre_taxonomy`, `fictioneer_add_logout_endpoint`, `fictioneer_add_oauth2_endpoint`, `fictioneer_prevent_admin_panel_access`, `fictioneer_disable_heartbeat`, `fictioneer_fcn_chapter_post_type`, `fictioneer_fcn_collection_post_type`, `fictioneer_fcn_recommendation_post_type`, `fictioneer_fcn_story_post_type`, `fictioneer_modify_allowed_tags`, `fictioneer_story_rss`
| `init` | `fictioneer_add_character_taxonomy`, `fictioneer_add_content_warning_taxonomy`, `fictioneer_add_epub_download_endpoint`, `fictioneer_add_fandom_taxonomy`, `fictioneer_add_genre_taxonomy`, `fictioneer_add_logout_endpoint`, `fictioneer_add_oauth2_endpoint`, `fictioneer_restrict_admin_panel`, `fictioneer_disable_heartbeat`, `fictioneer_fcn_chapter_post_type`, `fictioneer_fcn_collection_post_type`, `fictioneer_fcn_recommendation_post_type`, `fictioneer_fcn_story_post_type`, `fictioneer_modify_allowed_tags`, `fictioneer_story_rss`
| `kses_allowed_protocols` | `fictioneer_extend_allowed_protocols`
| `login_form` | `fictioneer_after_logout_cleanup`
| `manage_comments_custom_column` | `fictioneer_add_comments_report_column_content`
| `personal_options_update` | `fictioneer_update_admin_user_profile`, `fictioneer_update_my_user_profile`
| `pre_get_posts` | `fictioneer_extend_search_query`, `fictioneer_remove_unlisted_from_search`, `fictioneer_scope_media_to_uploader`
| `save_post` | `fictioneer_create_sitemap`, `fictioneer_refresh_chapters_schema`, `fictioneer_refresh_chapter_schema`, `fictioneer_refresh_collections_schema`, `fictioneer_refresh_post_caches`, `fictioneer_refresh_post_schema`, `fictioneer_refresh_recommendations_schema`, `fictioneer_refresh_recommendation_schema`, `fictioneer_refresh_stories_schema`, `fictioneer_refresh_story_schema`, `fictioneer_save_seo_metabox`, `fictioneer_save_word_count`, `fictioneer_track_chapter_and_story_updates`, `fictioneer_update_modified_date_on_story_for_chapter`, `fictioneer_update_shortcode_relationships`, `fictioneer_purge_cache_transients`
| `save_post` | `fictioneer_create_sitemap`, `fictioneer_refresh_chapters_schema`, `fictioneer_refresh_chapter_schema`, `fictioneer_refresh_collections_schema`, `fictioneer_refresh_post_caches`, `fictioneer_refresh_post_schema`, `fictioneer_refresh_recommendations_schema`, `fictioneer_refresh_recommendation_schema`, `fictioneer_refresh_stories_schema`, `fictioneer_refresh_story_schema`, `fictioneer_save_seo_metabox`, `fictioneer_save_word_count`, `fictioneer_track_chapter_and_story_updates`, `fictioneer_update_modified_date_on_story_for_chapter`, `fictioneer_update_shortcode_relationships`, `fictioneer_purge_cache_transients`, `fictioneer_restrict_page_templates`
| `show_user_profile` | `fictioneer_custom_profile_fields`
| `switch_theme` | `fictioneer_theme_deactivation`
| `template_redirect` | `fictioneer_disable_date_archives`, `fictioneer_generate_epub`, `fictioneer_handle_oauth`, `fictioneer_logout`, `fictioneer_disable_attachment_pages`
@ -243,8 +243,8 @@ Fictioneer customizes WordPress by using as many standard action and filter hook
| `untrash_post` | `fictioneer_refresh_post_caches`, `fictioneer_track_chapter_and_story_updates`, `fictioneer_update_modified_date_on_story_for_chapter`, `fictioneer_purge_cache_transients`
| `wp_ajax_*` | `fictioneer_ajax_clear_my_checkmarks`, `fictioneer_ajax_clear_my_comments`, `fictioneer_ajax_clear_my_comment_subscriptions`, `fictioneer_ajax_clear_my_follows`, `fictioneer_ajax_clear_my_reminders`, `fictioneer_ajax_delete_epub`, `fictioneer_ajax_delete_my_account`, `fictioneer_ajax_delete_my_comment`, `fictioneer_ajax_edit_comment`, `fictioneer_ajax_get_avatar`, `fictioneer_ajax_get_bookmarks`, `fictioneer_ajax_get_checkmarks`, `fictioneer_ajax_get_comment_form`, `fictioneer_ajax_get_comment_section`, `fictioneer_ajax_get_fingerprint`, `fictioneer_ajax_get_finished_list`, `fictioneer_ajax_get_follows`, `fictioneer_ajax_get_follows_list`, `fictioneer_ajax_get_follows_notifications`, `fictioneer_ajax_get_nonce`, `fictioneer_ajax_get_reminders`, `fictioneer_ajax_get_reminders_list`, `fictioneer_ajax_is_user_logged_in`, `fictioneer_ajax_mark_follows_read`, `fictioneer_ajax_moderate_comment`, `fictioneer_ajax_purge_schema`, `fictioneer_ajax_report_comment`, `fictioneer_ajax_save_bookmarks`, `fictioneer_ajax_set_checkmark`, `fictioneer_ajax_submit_comment`, `fictioneer_ajax_toggle_follow`, `fictioneer_ajax_toggle_reminder`, `fictioneer_ajax_unset_my_oauth`, `fictioneer_request_story_comments`
| `wp_ajax_nopriv_*` | `fictioneer_ajax_get_comment_form`, `fictioneer_ajax_get_comment_section`, `fictioneer_ajax_get_nonce`, `fictioneer_ajax_is_user_logged_in`, `fictioneer_ajax_submit_comment`, `fictioneer_request_story_comments`
| `wp_before_admin_bar_render` | `fictioneer_remove_admin_bar_links`
| `wp_dashboard_setup` | `fictioneer_reduce_contributor_dashboard_widgets`, `fictioneer_reduce_editor_dashboard_widgets`, `fictioneer_reduce_moderator_dashboard_widgets`, `fictioneer_reduce_subscriber_dashboard_widgets`, `fictioneer_reduce_author_dashboard_widgets`
| `wp_before_admin_bar_render` | `fictioneer_remove_admin_bar_links`, `fictioneer_remove_dashboard_from_admin_bar`
| `wp_dashboard_setup` | `fictioneer_reduce_contributor_dashboard_widgets`, `fictioneer_reduce_editor_dashboard_widgets`, `fictioneer_reduce_moderator_dashboard_widgets`, `fictioneer_remove_dashboard_widgets`, `fictioneer_reduce_author_dashboard_widgets`
| `wp_default_scripts` | `fictioneer_remove_jquery_migrate`
| `wp_enqueue_scripts` | `fictioneer_add_custom_scripts`, `fictioneer_customizer_queue`, `fictioneer_style_queue`
| `wp_head` | `fictioneer_output_head_seo`, `fictioneer_output_rss`, `fictioneer_output_schemas`, `fictioneer_add_fiction_css`
@ -294,6 +294,7 @@ Fictioneer customizes WordPress by using as many standard action and filter hook
| `show_admin_bar` | `__return_false`
| `the_content` | `fictioneer_embed_consent_wrappers`, `fictioneer_add_lightbox_to_post_images`, `fictioneer_add_chapter_paragraph_id`
| `the_password_form` | `fictioneer_password_form`
| `theme_page_templates` | `fictioneer_disallow_page_template_select`
| `user_contactmethods` | `fictioneer_user_contact_methods`
| `wp_list_comments_args` | `fictioneer_comment_list_args`
| `wp_is_application_passwords_available` | `__return_false`

View File

@ -208,35 +208,151 @@ function fictioneer_add_moderator_role() {
// APPLY CAPABILITY RULES
// =============================================================================
/**
* Admin bar
*/
if ( ! current_user_can( 'fcn_adminbar_access' ) ) {
add_filter( 'show_admin_bar', '__return_false' );
// Add templates ('name' => true) to the array you want to allow
if ( ! defined( 'CHILD_ALLOWED_PAGE_TEMPLATES' ) ) {
define( 'CHILD_ALLOWED_PAGE_TEMPLATES', [] );
}
/**
* Prevent users from accessing the admin panel
*
* @since Fictioneer 5.6.0
*/
// No restriction can be applied to administrators
if ( ! current_user_can( 'manage_options' ) ) {
function fictioneer_prevent_admin_panel_access() {
// Redirect back to Home (but always allow administrators)
if (
is_admin() &&
! current_user_can( 'manage_options' ) &&
! ( defined( 'DOING_AJAX' ) && DOING_AJAX )
) {
wp_redirect( home_url() );
exit;
/**
* Admin bar
*/
if ( ! current_user_can( 'fcn_adminbar_access' ) ) {
add_filter( 'show_admin_bar', '__return_false', 9999 );
}
/**
* Prevent access to the admin panel
*
* @since Fictioneer 5.6.0
*/
function fictioneer_restrict_admin_panel() {
// Redirect back to Home
if (
is_admin() &&
! ( defined( 'DOING_AJAX' ) && DOING_AJAX )
) {
wp_redirect( home_url() );
exit;
}
}
if ( ! current_user_can( 'fcn_admin_panel_access' ) ) {
add_filter( 'init', 'fictioneer_restrict_admin_panel', 9999 );
}
/**
* Remove admin dashboard widgets
*
* @since Fictioneer 5.6.0
*/
function fictioneer_remove_dashboard_widgets() {
global $wp_meta_boxes;
// Remove all
$wp_meta_boxes['dashboard']['normal']['core'] = [];
$wp_meta_boxes['dashboard']['side']['core'] = [];
// Remove actions
remove_action( 'welcome_panel', 'wp_welcome_panel' );
}
/**
* Remove the dashboard menu page
*
* @since Fictioneer 5.6.0
*/
function fictioneer_remove_dashboard() {
// Dashboard
remove_menu_page( 'index.php' );
}
/**
* Redirect from dashboard to user profile
*
* @since Fictioneer 5.6.0
*/
function fictioneer_skip_dashboard() {
global $pagenow;
// Unless it's AJAX or an administrator...
if (
$pagenow == 'index.php' &&
! ( defined( 'DOING_AJAX' ) && DOING_AJAX )
) {
// Skip dashboard, go to user profile
wp_redirect( home_url( '/wp-admin/profile.php' ) );
exit;
}
}
/**
* Remove dashboard from admin bar dropdown
*
* @since Fictioneer 5.6.0
*/
function fictioneer_remove_dashboard_from_admin_bar() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu( 'dashboard' );
}
if ( ! current_user_can( 'fcn_dashboard_access' ) ) {
add_action( 'wp_before_admin_bar_render', 'fictioneer_remove_dashboard_from_admin_bar', 9999 );
add_action( 'wp_dashboard_setup', 'fictioneer_remove_dashboard_widgets', 9999 );
add_action( 'admin_menu', 'fictioneer_remove_dashboard', 9999 );
add_action( 'admin_init', 'fictioneer_skip_dashboard', 9999 );
}
/**
* Filters the page template selection
*
* @param array $templates Array of templates ('name' => true).
*
* @return array Array of allowed templates.
*/
function fictioneer_disallow_page_template_select( $templates ) {
return array_intersect_key( $templates, CHILD_ALLOWED_PAGE_TEMPLATES );
}
/**
* Makes sure only allowed templates are selected
*
* @param int $post_id ID of the saved post.
*/
function fictioneer_restrict_page_templates( $post_id ) {
// Do nothing if...
if ( get_post_type( $post_id ) !== 'page' ) {
return;
}
// Get currently selected template
$selected_template = get_post_meta( $post_id, '_wp_page_template', true );
// Remove if not allowed
if ( ! in_array( $selected_template, CHILD_ALLOWED_PAGE_TEMPLATES ) ) {
update_post_meta( $post_id, '_wp_page_template', '' );
}
}
if ( ! current_user_can( 'fcn_select_page_template' ) ) {
add_action( 'save_post', 'fictioneer_restrict_page_templates' );
add_filter( 'theme_page_templates', 'fictioneer_disallow_page_template_select' );
}
}
if ( ! current_user_can( 'fcn_admin_panel_access' ) ) {
add_filter( 'init', 'fictioneer_prevent_admin_panel_access' );
}
@ -250,46 +366,6 @@ if ( ! current_user_can( 'fcn_admin_panel_access' ) ) {
// See ./includes/functions/_admin.php
// =============================================================================
// RESTRICT SUBSCRIBERS ROLE
// =============================================================================
/**
* Remove admin dashboard widgets for subscribers
*
* @since Fictioneer 5.0
* @link https://developer.wordpress.org/apis/handbook/dashboard-widgets/
*/
function fictioneer_reduce_subscriber_dashboard_widgets() {
global $wp_meta_boxes;
// Remove all
$wp_meta_boxes['dashboard']['normal']['core'] = [];
$wp_meta_boxes['dashboard']['side']['core'] = [];
// Remove actions
remove_action( 'welcome_panel', 'wp_welcome_panel' );
}
/**
* Remove admin menu pages for subscribers
*
* @since Fictioneer 5.0
*/
function fictioneer_reduce_subscriber_admin_panel() {
// Remove menu pages
remove_menu_page( 'index.php' ); // Dashboard
}
// Apply restrictions to subscribers
if ( fictioneer_has_role( get_current_user_id(), 'subscriber' ) ) {
add_action( 'wp_dashboard_setup', 'fictioneer_reduce_subscriber_dashboard_widgets' );
add_action( 'admin_menu', 'fictioneer_reduce_subscriber_admin_panel' );
}
// =============================================================================
// RESTRICT FICTIONEER MODERATOR ROLE
// =============================================================================