Frontend.php
144 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\NavigationMenuLinks; |
| 4 | |
| 5 | |
| 6 | if ( ! defined('ABSPATH')) { |
| 7 | exit; // Exit if accessed directly |
| 8 | } |
| 9 | |
| 10 | class Frontend |
| 11 | { |
| 12 | private static $instance = null; |
| 13 | |
| 14 | public function __construct() |
| 15 | { |
| 16 | /* The main code, this replace the #keyword# by the correct links with nonce ect */ |
| 17 | add_filter('wp_setup_nav_menu_item', [$this, 'setup_nav_menu_item']); |
| 18 | |
| 19 | add_filter('wp_nav_menu_objects', [$this, 'wp_nav_menu_objects']); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Used to return the correct title for the double login/logout menu item |
| 24 | */ |
| 25 | public function loginout_title($title) |
| 26 | { |
| 27 | $titles = explode('|', $title); |
| 28 | if ( ! is_user_logged_in()) { |
| 29 | return esc_html(isset($titles[0]) ? $titles[0] : $title); |
| 30 | } else { |
| 31 | return esc_html(isset($titles[1]) ? $titles[1] : $title); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | public function setup_nav_menu_item($item) |
| 36 | { |
| 37 | global $pagenow; |
| 38 | |
| 39 | if (is_customize_preview() && ! (is_home() || is_front_page() || is_singular() || is_archive() || is_tax())) { |
| 40 | return $item; |
| 41 | } |
| 42 | |
| 43 | if ($pagenow != 'nav-menus.php' && ! defined('DOING_AJAX') && isset($item->url) && strstr($item->url, '#pp-') != '' |
| 44 | ) { |
| 45 | $item_url = substr($item->url, 0, strpos($item->url, '#', 1)) . '#'; |
| 46 | |
| 47 | switch ($item_url) { |
| 48 | case '#pp-loginout#' : |
| 49 | $item->url = is_user_logged_in() ? wp_logout_url() : wp_login_url(); |
| 50 | $item->title = $this->loginout_title($item->title); |
| 51 | $item = apply_filters('pp_nav_loginout_item', $item); |
| 52 | break; |
| 53 | case '#pp-login#' : |
| 54 | if (is_user_logged_in()) { |
| 55 | $item->title = '#pp-login#'; |
| 56 | } else { |
| 57 | $item->url = wp_login_url(); |
| 58 | } |
| 59 | $item = apply_filters('pp_nav_login_item', $item); |
| 60 | break; |
| 61 | case '#pp-logout#' : |
| 62 | if (is_user_logged_in()) { |
| 63 | $item->url = wp_logout_url(); |
| 64 | } else { |
| 65 | $item->title = '#pp-logout#'; |
| 66 | } |
| 67 | $item = apply_filters('pp_nav_logout_item', $item); |
| 68 | break; |
| 69 | case '#pp-signup#' : |
| 70 | if (is_user_logged_in()) { |
| 71 | $item->title = '#pp-signup#'; |
| 72 | } else { |
| 73 | $item->url = wp_registration_url(); |
| 74 | } |
| 75 | $item = apply_filters('pp_nav_signup_item', $item); |
| 76 | break; |
| 77 | case '#pp-myprofile#' : |
| 78 | if (is_user_logged_in()) { |
| 79 | |
| 80 | if (function_exists('ppress_profile_url')) { |
| 81 | $item->url = ppress_profile_url(); |
| 82 | } |
| 83 | |
| 84 | if (function_exists('pp_profile_url')) { |
| 85 | $item->url = pp_profile_url(); |
| 86 | } |
| 87 | |
| 88 | } else { |
| 89 | $item->title = '#pp-myprofile#'; |
| 90 | } |
| 91 | $item = apply_filters('pp_nav_myprofile_item', $item); |
| 92 | break; |
| 93 | case '#pp-editprofile#' : |
| 94 | if (is_user_logged_in() && function_exists('ppress_edit_profile_url')) { |
| 95 | $item->url = ppress_edit_profile_url(); |
| 96 | } elseif (is_user_logged_in() && function_exists('pp_edit_profile_url')) { |
| 97 | $item->url = pp_edit_profile_url(); |
| 98 | } else { |
| 99 | $item->title = '#pp-editprofile#'; |
| 100 | } |
| 101 | $item = apply_filters('pp_nav_editprofile_item', $item); |
| 102 | break; |
| 103 | } |
| 104 | $item->url = esc_url_raw($item->url); |
| 105 | } |
| 106 | |
| 107 | return apply_filters('pp_nav_item', $item); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | /** |
| 112 | * Remove navigation item with URL and title that are the same. |
| 113 | * |
| 114 | * @param array $sorted_menu_items |
| 115 | * |
| 116 | * @return mixed |
| 117 | */ |
| 118 | public function wp_nav_menu_objects($sorted_menu_items) |
| 119 | { |
| 120 | // in self::setup_nav_menu_item we made the title and url of some nav items the same. |
| 121 | // an item that fall in this category get removed here. |
| 122 | foreach ($sorted_menu_items as $k => $item) { |
| 123 | if (isset($item->title, $item->url) && strlen($item->title) && ($item->title == $item->url)) { |
| 124 | unset($sorted_menu_items[$k]); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return $sorted_menu_items; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @return null|Frontend |
| 133 | */ |
| 134 | public static function get_instance() |
| 135 | { |
| 136 | // If the single instance hasn't been set, set it now. |
| 137 | if (null == self::$instance) { |
| 138 | self::$instance = new self; |
| 139 | } |
| 140 | |
| 141 | return self::$instance; |
| 142 | } |
| 143 | |
| 144 | } |