From fe437cc880bbd60156dfa9b416af93f8139c3188 Mon Sep 17 00:00:00 2001 From: Ian Dunn Date: Fri, 1 Nov 2019 06:01:10 -0500 Subject: [PATCH] My Pledges: Hide menu item when logged out. --- plugins/wporg-5ftf/includes/contributor.php | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/plugins/wporg-5ftf/includes/contributor.php b/plugins/wporg-5ftf/includes/contributor.php index 65bb5a7..eddf5ce 100644 --- a/plugins/wporg-5ftf/includes/contributor.php +++ b/plugins/wporg-5ftf/includes/contributor.php @@ -14,6 +14,7 @@ const CPT_ID = FiveForTheFuture\PREFIX . '_' . SLUG; add_action( 'init', __NAMESPACE__ . '\register_custom_post_type', 0 ); add_filter( 'manage_edit-' . CPT_ID . '_columns', __NAMESPACE__ . '\add_list_table_columns' ); add_action( 'manage_' . CPT_ID . '_posts_custom_column', __NAMESPACE__ . '\populate_list_table_columns', 10, 2 ); +add_filter( 'wp_nav_menu_objects', __NAMESPACE__ . '\hide_my_pledges_when_logged_out', 10 ); add_shortcode( '5ftf_my_pledges', __NAMESPACE__ . '\render_my_pledges' ); @@ -242,6 +243,27 @@ function get_contributor_user_objects( array $contributor_posts ) { }, $contributor_posts ); } +/** + * Only show the My Pledges menu to users who are logged in. + * + * @param array $menu_items + * + * @return array + */ +function hide_my_pledges_when_logged_out( $menu_items ) { + if ( get_current_user_id() ) { + return $menu_items; + } + + foreach ( $menu_items as $key => $item ) { + if ( home_url( 'my-pledges/' ) === $item->url ) { + unset( $menu_items[ $key ] ); + } + } + + return $menu_items; +} + /** * Render the My Pledges shortcode. *