social-menu.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Social Menu.
  4. *
  5. * This feature will only be activated for themes that declare their support.
  6. * This can be done by adding code similar to the following during the
  7. * 'after_setup_theme' action:
  8. *
  9. * add_theme_support( 'jetpack-social-menu' );
  10. */
  11. /**
  12. * Activate the Social Menu plugin.
  13. *
  14. * @uses current_theme_supports()
  15. */
  16. function jetpack_social_menu_init() {
  17. // Only load our code if our theme declares support
  18. if ( ! current_theme_supports( 'jetpack-social-menu' ) ) {
  19. return;
  20. }
  21. /*
  22. * Social Menu description.
  23. *
  24. * Rename the social menu description.
  25. *
  26. * @module theme-tools
  27. *
  28. * @since 3.9.0
  29. *
  30. * @param string $social_menu_description Social Menu description
  31. */
  32. $social_menu_description = apply_filters( 'jetpack_social_menu_description', __( 'Social Menu', 'jetpack' ) );
  33. // Register a new menu location
  34. register_nav_menus( array(
  35. 'jetpack-social-menu' => esc_html( $social_menu_description ),
  36. ) );
  37. // Enqueue CSS
  38. add_action( 'wp_enqueue_scripts', 'jetpack_social_menu_style' );
  39. // Load SVG icons related functions and filters
  40. if ( 'svg' === jetpack_social_menu_get_type() ) {
  41. require( dirname( __FILE__ ) . '/social-menu/icon-functions.php' );
  42. }
  43. }
  44. add_action( 'after_setup_theme', 'jetpack_social_menu_init', 99 );
  45. /**
  46. * Return the type of menu the theme is using.
  47. *
  48. * @uses get_theme_support()
  49. * @return null|string $menu_type
  50. */
  51. function jetpack_social_menu_get_type() {
  52. $options = get_theme_support( 'jetpack-social-menu' );
  53. if ( empty( $options ) ) {
  54. $menu_type = null;
  55. } else {
  56. $menu_type = ( in_array( $options[0], array( 'genericons', 'svg' ) ) ) ? $options[0] : 'genericons';
  57. }
  58. return $menu_type;
  59. }
  60. /**
  61. * Function to enqueue the CSS.
  62. */
  63. function jetpack_social_menu_style() {
  64. $menu_type = jetpack_social_menu_get_type();
  65. if ( ! $menu_type ) {
  66. return;
  67. }
  68. $deps = ( 'genericons' === $menu_type ) ? array( 'genericons' ) : null;
  69. if ( has_nav_menu( 'jetpack-social-menu' ) ) {
  70. wp_enqueue_style( 'jetpack-social-menu', plugins_url( 'social-menu/social-menu.css', __FILE__ ), $deps, '1.0' );
  71. }
  72. }
  73. /**
  74. * Create the function for the menu.
  75. */
  76. function jetpack_social_menu() {
  77. if ( has_nav_menu( 'jetpack-social-menu' ) ) :
  78. $menu_type = jetpack_social_menu_get_type();
  79. $link_after = '</span>';
  80. if ( 'svg' === $menu_type ) {
  81. $link_after .= jetpack_social_menu_get_svg( array( 'icon' => 'chain' ) );
  82. } ?>
  83. <nav class="jetpack-social-navigation jetpack-social-navigation-<?php echo esc_attr( $menu_type ); ?>" role="navigation" aria-label="<?php esc_html_e( 'Social Links Menu', 'jetpack' ); ?>">
  84. <?php
  85. wp_nav_menu( array(
  86. 'theme_location' => 'jetpack-social-menu',
  87. 'link_before' => '<span class="screen-reader-text">',
  88. 'link_after' => $link_after,
  89. 'depth' => 1,
  90. ) );
  91. ?>
  92. </nav><!-- .jetpack-social-navigation -->
  93. <?php endif;
  94. }