[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-content/themes/twentyseventeen/inc/ -> icon-functions.php (source)

   1  <?php
   2  /**
   3   * SVG icons related functions and filters
   4   *
   5   * @package WordPress
   6   * @subpackage Twenty_Seventeen
   7   * @since Twenty Seventeen 1.0
   8   */
   9  
  10  /**
  11   * Add SVG definitions to the footer.
  12   */
  13  function twentyseventeen_include_svg_icons() {
  14      // Define SVG sprite file.
  15      $svg_icons = get_parent_theme_file_path( '/assets/images/svg-icons.svg' );
  16  
  17      // If it exists, include it.
  18      if ( file_exists( $svg_icons ) ) {
  19          require_once $svg_icons;
  20      }
  21  }
  22  add_action( 'wp_footer', 'twentyseventeen_include_svg_icons', 9999 );
  23  
  24  /**
  25   * Return SVG markup.
  26   *
  27   * @param array $args {
  28   *     Parameters needed to display an SVG.
  29   *
  30   *     @type string $icon  Required SVG icon filename.
  31   *     @type string $title Optional SVG title.
  32   *     @type string $desc  Optional SVG description.
  33   * }
  34   * @return string SVG markup.
  35   */
  36  function twentyseventeen_get_svg( $args = array() ) {
  37      // Make sure $args are an array.
  38      if ( empty( $args ) ) {
  39          return __( 'Please define default parameters in the form of an array.', 'twentyseventeen' );
  40      }
  41  
  42      // Define an icon.
  43      if ( false === array_key_exists( 'icon', $args ) ) {
  44          return __( 'Please define an SVG icon filename.', 'twentyseventeen' );
  45      }
  46  
  47      // Set defaults.
  48      $defaults = array(
  49          'icon'     => '',
  50          'title'    => '',
  51          'desc'     => '',
  52          'fallback' => false,
  53      );
  54  
  55      // Parse args.
  56      $args = wp_parse_args( $args, $defaults );
  57  
  58      // Set aria hidden.
  59      $aria_hidden = ' aria-hidden="true"';
  60  
  61      // Set ARIA.
  62      $aria_labelledby = '';
  63  
  64      /*
  65       * Twenty Seventeen doesn't use the SVG title or description attributes; non-decorative icons are described with .screen-reader-text.
  66       *
  67       * However, child themes can use the title and description to add information to non-decorative SVG icons to improve accessibility.
  68       *
  69       * Example 1 with title: <?php echo twentyseventeen_get_svg( array( 'icon' => 'arrow-right', 'title' => __( 'This is the title', 'textdomain' ) ) ); ?>
  70       *
  71       * Example 2 with title and description: <?php echo twentyseventeen_get_svg( array( 'icon' => 'arrow-right', 'title' => __( 'This is the title', 'textdomain' ), 'desc' => __( 'This is the description', 'textdomain' ) ) ); ?>
  72       *
  73       * See https://www.paciellogroup.com/blog/2013/12/using-aria-enhance-svg-accessibility/.
  74       */
  75      if ( $args['title'] ) {
  76          $aria_hidden     = '';
  77          $unique_id       = twentyseventeen_unique_id();
  78          $aria_labelledby = ' aria-labelledby="title-' . $unique_id . '"';
  79  
  80          if ( $args['desc'] ) {
  81              $aria_labelledby = ' aria-labelledby="title-' . $unique_id . ' desc-' . $unique_id . '"';
  82          }
  83      }
  84  
  85      // Begin SVG markup.
  86      $svg = '<svg class="icon icon-' . esc_attr( $args['icon'] ) . '"' . $aria_hidden . $aria_labelledby . ' role="img">';
  87  
  88      // Display the title.
  89      if ( $args['title'] ) {
  90          $svg .= '<title id="title-' . $unique_id . '">' . esc_html( $args['title'] ) . '</title>';
  91  
  92          // Display the desc only if the title is already set.
  93          if ( $args['desc'] ) {
  94              $svg .= '<desc id="desc-' . $unique_id . '">' . esc_html( $args['desc'] ) . '</desc>';
  95          }
  96      }
  97  
  98      /*
  99       * Display the icon.
 100       *
 101       * The whitespace around `<use>` is intentional - it is a work around to a keyboard navigation bug in Safari 10.
 102       *
 103       * See https://core.trac.wordpress.org/ticket/38387.
 104       */
 105      $svg .= ' <use href="#icon-' . esc_html( $args['icon'] ) . '" xlink:href="#icon-' . esc_html( $args['icon'] ) . '"></use> ';
 106  
 107      // Add some markup to use as a fallback for browsers that do not support SVGs.
 108      if ( $args['fallback'] ) {
 109          $svg .= '<span class="svg-fallback icon-' . esc_attr( $args['icon'] ) . '"></span>';
 110      }
 111  
 112      $svg .= '</svg>';
 113  
 114      return $svg;
 115  }
 116  
 117  /**
 118   * Display SVG icons in social links menu.
 119   *
 120   * @param string   $item_output The menu item's starting HTML output.
 121   * @param WP_Post  $item        Menu item data object.
 122   * @param int      $depth       Depth of the menu. Used for padding.
 123   * @param stdClass $args        An object of wp_nav_menu() arguments.
 124   * @return string The menu item output with social icon.
 125   */
 126  function twentyseventeen_nav_menu_social_icons( $item_output, $item, $depth, $args ) {
 127      // Get supported social icons.
 128      $social_icons = twentyseventeen_social_links_icons();
 129  
 130      // Change SVG icon inside social links menu if there is supported URL.
 131      if ( 'social' === $args->theme_location ) {
 132          foreach ( $social_icons as $attr => $value ) {
 133              if ( false !== strpos( $item_output, $attr ) ) {
 134                  $item_output = str_replace( $args->link_after, '</span>' . twentyseventeen_get_svg( array( 'icon' => esc_attr( $value ) ) ), $item_output );
 135              }
 136          }
 137      }
 138  
 139      return $item_output;
 140  }
 141  add_filter( 'walker_nav_menu_start_el', 'twentyseventeen_nav_menu_social_icons', 10, 4 );
 142  
 143  /**
 144   * Add dropdown icon if menu item has children.
 145   *
 146   * @param string   $title The menu item's title.
 147   * @param WP_Post  $item  The current menu item.
 148   * @param stdClass $args  An object of wp_nav_menu() arguments.
 149   * @param int      $depth Depth of menu item. Used for padding.
 150   * @return string The menu item's title with dropdown icon.
 151   */
 152  function twentyseventeen_dropdown_icon_to_menu_link( $title, $item, $args, $depth ) {
 153      if ( 'top' === $args->theme_location ) {
 154          foreach ( $item->classes as $value ) {
 155              if ( 'menu-item-has-children' === $value || 'page_item_has_children' === $value ) {
 156                  $title = $title . twentyseventeen_get_svg( array( 'icon' => 'angle-down' ) );
 157              }
 158          }
 159      }
 160  
 161      return $title;
 162  }
 163  add_filter( 'nav_menu_item_title', 'twentyseventeen_dropdown_icon_to_menu_link', 10, 4 );
 164  
 165  /**
 166   * Returns an array of supported social links (URL and icon name).
 167   *
 168   * @return array Array of social links icons.
 169   */
 170  function twentyseventeen_social_links_icons() {
 171      // Supported social links icons.
 172      $social_links_icons = array(
 173          'behance.net'     => 'behance',
 174          'codepen.io'      => 'codepen',
 175          'deviantart.com'  => 'deviantart',
 176          'digg.com'        => 'digg',
 177          'docker.com'      => 'dockerhub',
 178          'dribbble.com'    => 'dribbble',
 179          'dropbox.com'     => 'dropbox',
 180          'facebook.com'    => 'facebook',
 181          'flickr.com'      => 'flickr',
 182          'foursquare.com'  => 'foursquare',
 183          'plus.google.com' => 'google-plus',
 184          'github.com'      => 'github',
 185          'instagram.com'   => 'instagram',
 186          'linkedin.com'    => 'linkedin',
 187          'mailto:'         => 'envelope-o',
 188          'medium.com'      => 'medium',
 189          'pinterest.com'   => 'pinterest-p',
 190          'pscp.tv'         => 'periscope',
 191          'getpocket.com'   => 'get-pocket',
 192          'reddit.com'      => 'reddit-alien',
 193          'skype.com'       => 'skype',
 194          'skype:'          => 'skype',
 195          'slideshare.net'  => 'slideshare',
 196          'snapchat.com'    => 'snapchat-ghost',
 197          'soundcloud.com'  => 'soundcloud',
 198          'spotify.com'     => 'spotify',
 199          'stumbleupon.com' => 'stumbleupon',
 200          't.me'            => 'telegram',
 201          'telegram.me'     => 'telegram',
 202          'tumblr.com'      => 'tumblr',
 203          'twitch.tv'       => 'twitch',
 204          'twitter.com'     => 'twitter',
 205          'vimeo.com'       => 'vimeo',
 206          'vine.co'         => 'vine',
 207          'vk.com'          => 'vk',
 208          'wa.me'           => 'whatsapp',
 209          'whatsapp.com'    => 'whatsapp',
 210          'wordpress.org'   => 'wordpress',
 211          'wordpress.com'   => 'wordpress',
 212          'yelp.com'        => 'yelp',
 213          'youtube.com'     => 'youtube',
 214      );
 215  
 216      /**
 217       * Filters Twenty Seventeen social links icons.
 218       *
 219       * @since Twenty Seventeen 1.0
 220       *
 221       * @param array $social_links_icons Array of social links icons.
 222       */
 223      return apply_filters( 'twentyseventeen_social_links_icons', $social_links_icons );
 224  }


Generated : Fri Apr 19 08:20:01 2024 Cross-referenced by PHPXref