__( 'Allows you to showcase your team members.', 'p2020' ), 'customize_selective_refresh' => true, ] ); if ( is_active_widget( false, false, self::WIDGET_ID_BASE ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) { add_action( 'wp_print_styles', [ $this, 'enqueue_style' ] ); } $this->default_title = __( 'Team', 'p2020' ); } public function enqueue_style() { wp_enqueue_style( 'widget-grid-and-list' ); } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. */ public function form( $instance ) { $instance = $this->extend_default_options( $instance ); $title = $instance['title']; if ( false === $title ) { $title = $this->default_title; } $limit = (int) $instance['limit']; global $wp_roles; $all_roles = array_keys( $wp_roles->roles ); $selected_roles = $instance['roles'] ?? $all_roles; ?>

roles ); if ( empty( $new_instance['roles'] ) || ! is_array( $new_instance['roles'] ) ) { // If the new roles are not what we expect, then make them an empty array. $instance['roles'] = []; } // Make sure submitted roles are valid $instance['roles'] = array_intersect( $all_roles, $new_instance['roles'] ); return $instance; } // phpcs:enable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable // phpcs:enable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassAfterLastUsed /** * Front-end display of widget. * * @see WP_Widget::widget() * * @param array $args Widget arguments. * @param array $instance Saved values from database. * * @return void */ public function widget( $args, $instance ) { $instance = $this->extend_default_options( $instance ); $title = $instance['title']; $title = apply_filters( 'widget_title', $title ); $limit = (int) $instance['limit']; $roles = $instance['roles']; // phpcs:ignore WordPress.Security.EscapeOutput -- HTML from theme echo $args['before_widget']; $my_team = $this->get_team_info( $limit, $roles ); $current_user_id = get_current_user_id(); $current_blog_id = get_current_blog_id(); if ( ! empty( $title ) ) { if ( is_user_member_of_blog( $current_user_id, $current_blog_id ) && current_user_can_for_blog( $current_blog_id, 'remove_users' ) ) { $manage_team_url = P2_Core\Navigation\Urls::get_manage_team_url( $current_blog_id ); $simple_menu = new P2_Core\Navigation\Simple_Menu( 'my-team-widget-menu', __( 'More', 'p2' ) ); $simple_menu->add_item( __( "Manage P2's members", 'p2020' ), $manage_team_url ); } // phpcs:ignore WordPress.Security.EscapeOutput -- HTML from theme echo $args['before_title']; echo esc_html( $title ); if ( ! empty( $simple_menu ) ) { get_template_part( 'partials/ellipsis-menu', $simple_menu->get_type(), [ 'simple_menu' => $simple_menu ], ); } // phpcs:ignore WordPress.Security.EscapeOutput -- HTML from theme echo $args['after_title']; } $my_team_members = $my_team['members']; if ( ! empty( $my_team_members ) ) { ?>
[], 'size' => 0, ]; } $exclude_users = apply_filters( 'p2_widgets_team_user_exclude_list', [ get_current_user_id() ] ); $user_query_param = [ 'blog_id' => $blog_id, 'role__in' => $roles, 'exclude' => $exclude_users, ]; if ( ! empty( $limit ) ) { $user_query_param['number'] = $limit; } $users_data = $this->get_users_data( $user_query_param ); $team_info['size'] = $users_data['total']; $users = $users_data['users']; // Check if current user is part of team // If yes, place in front $me = get_users( [ 'blog_id' => $blog_id, 'role__in' => $roles, 'include' => [ get_current_user_id() ], ] ); if ( ! empty( $me ) ) { $users = array_merge( $me, array_slice( $users, 0, $limit - 1 ) ); $team_info['size'] += 1; } $team_info['members'] = $users; return $team_info; } protected function extend_default_options( array $options = [] ): array { global $wp_roles; $all_roles = array_keys( $wp_roles->roles ); $defaults = [ 'title' => $this->default_title, 'limit' => 14, 'roles' => $all_roles, ]; $merged = array_merge( $defaults, $options ); if ( $merged['limit'] < 1 ) { $merged['limit'] = 10; } return $merged; } private function get_users_data( $args = [] ) { $args = wp_parse_args( $args ); $args['count_total'] = true; $user_search = new \WP_User_Query( $args ); return [ 'total' => $user_search->get_total(), 'users' => $user_search->get_results(), ]; } }