<?php

/**
 * ReadyLaunch Profile Menu Block
 *
 * Displays the user profile menu with avatar, name, email and dropdown links.
 *
 * @package memberpress
 */

if (!defined('ABSPATH')) {
    die('You are not allowed to call this page directly.');
}
?>
<?php if ($user) : ?>
  <div <?php echo $wrapper_attributes; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
    <div class="wp-block-memberpress-pro-profile-menu__button-group">
      <button type="button" class="wp-block-memberpress-pro-profile-menu__button --is-tablet" id="user-menu-button"
        aria-expanded="false" aria-haspopup="true">
        <img class="wp-block-memberpress-pro-profile-menu__avatar"
          src="<?php echo esc_url_raw(get_avatar_url($user->ID, ['size' => '51'])) ?>"
          alt="">

        <div class="wp-block-memberpress-pro-profile-menu__text">
          <span>
            <?php echo esc_html($user->full_name()); ?>
          </span>
          <span class="wp-block-memberpress-pro-profile-menu__text--small"><?php echo esc_html($user->user_email); ?></span>
        </div>

        <svg class="wp-block-memberpress-pro-profile-menu__arrow-down" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"
          fill="currentColor" aria-hidden="true">
          <path fill-rule="evenodd"
            d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
            clip-rule="evenodd"></path>
        </svg>
      </button>

      <button
        class="wp-block-memberpress-pro-profile-menu__button --is-mobile"
        type="button"
        aria-expanded="false"
        aria-haspopup="true"
        aria-label="<?php esc_attr_e('User menu', 'memberpress'); ?>"
      >
        <?php
        $mepr_default_mobile_icon = '<svg xmlns="http://www.w3.org/2000/svg" class="wp-block-memberpress-pro-profile-menu__hamburger" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M4 6h16M4 12h16M4 18h16" /></svg>';

        /**
         * Filter the icon rendered inside the FSE profile-menu mobile button.
         *
         * Default is the 3-bar hamburger SVG used by the shared ReadyLaunch
         * header. Addons that surface their own hamburger affordance in the
         * same header (for example the classroom sidebar toggle on FSE
         * courses routes) can replace this with an avatar `<img>` to avoid
         * two hamburgers sitting next to each other.
         *
         * Filter return value MUST be already-safe markup; it is echoed
         * unescaped. The default itself is a hardcoded SVG. Add-on authors
         * are responsible for returning trusted, pre-sanitized markup here
         * (note wp_kses_post() would strip SVG, so it can't be applied
         * blanket to this output).
         *
         * @param string   $icon HTML for the icon inside the mobile button.
         * @param MeprUser $user Current user.
         */
        $mepr_mobile_icon = MeprHooks::apply_filters(
            'mepr_pro_profile_menu_mobile_icon',
            $mepr_default_mobile_icon,
            $user
        );
        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- icon markup is either the hardcoded default SVG or addon-provided trusted markup.
        echo $mepr_mobile_icon;
        ?>
      </button>

      <div class="wp-block-memberpress-pro-profile-menu__dropdown" style="display: none;">
        <?php
        /**
         * Filter the items rendered in the FSE profile-menu dropdown.
         *
         * Items are an associative array keyed by a stable id (so addons can
         * insert relative to a built-in item or remove one with `unset()`).
         * Each item has:
         *   - label (string, required) Plain-text label, already translated.
         *   - url   (string, required) Destination URL.
         *   - attrs (array,  optional) Extra HTML attributes for the <a>,
         *     keyed by attribute name, e.g. `['target' => '_blank']`.
         *
         * @param array     $items Items keyed by stable id.
         * @param MeprUser  $user  Current user.
         */
        $mepr_profile_menu_items = MeprHooks::apply_filters(
            'mepr_pro_profile_menu_items',
            [
                'account'         => [
                    'label' => _x('Account', 'ui', 'memberpress'),
                    'url'   => $account_url,
                ],
                'change-password' => [
                    'label' => _x('Change Password', 'ui', 'memberpress'),
                    'url'   => $change_password_url,
                ],
                'logout'          => [
                    'label' => _x('Logout', 'ui', 'memberpress'),
                    'url'   => $logout_url,
                ],
            ],
            $user
        );

        foreach ($mepr_profile_menu_items as $mepr_profile_menu_item) :
            if (empty($mepr_profile_menu_item['url']) || empty($mepr_profile_menu_item['label'])) {
                continue;
            }

            $mepr_profile_menu_attrs = '';
            if (!empty($mepr_profile_menu_item['attrs']) && is_array($mepr_profile_menu_item['attrs'])) {
                foreach ($mepr_profile_menu_item['attrs'] as $mepr_profile_menu_attr_key => $mepr_profile_menu_attr_val) {
                    $mepr_profile_menu_attrs .= sprintf(
                        ' %s="%s"',
                        sanitize_key($mepr_profile_menu_attr_key),
                        esc_attr($mepr_profile_menu_attr_val)
                    );
                }
            }
            ?>
        <a class="wp-block-memberpress-pro-profile-menu__dropdown-item"
          href="<?php echo esc_url($mepr_profile_menu_item['url']); ?>"<?php
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- attrs escaped per-pair above.
                echo $mepr_profile_menu_attrs;
            ?>><?php echo esc_html($mepr_profile_menu_item['label']); ?></a>
            <?php
        endforeach;
        ?>
      </div>

    </div>
  </div>
<?php endif; ?>

