Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 132
0.00% covered (danger)
0.00%
0 / 4
CRAP
n/a
0 / 0
jetpack_social_menu_include_svg_icons
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
30
jetpack_social_menu_get_svg
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
jetpack_social_menu_nav_menu_social_icons
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
56
jetpack_social_menu_social_links_icons
0.00% covered (danger)
0.00%
0 / 68
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * SVG icons related functions and filters
4 *
5 * @package automattic/jetpack
6 */
7
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12if ( ! function_exists( 'jetpack_social_menu_include_svg_icons' ) ) {
13    /**
14     * Add SVG definitions to the footer.
15     */
16    function jetpack_social_menu_include_svg_icons() {
17        _deprecated_function( __FUNCTION__, 'jetpack-13.7' );
18        // Return early if Social Menu doesn't exist.
19        if ( ! has_nav_menu( 'jetpack-social-menu' ) ) {
20            return;
21        }
22        // Define SVG sprite file.
23        $svg_icons = __DIR__ . '/social-menu.svg';
24        // If it exists and we use the SVG menu type, include it.
25        if ( file_exists( $svg_icons ) && 'svg' === jetpack_social_menu_get_type() ) {
26            $svg_contents = file_get_contents( $svg_icons ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Only reading a local file.
27        }
28
29        if ( ! empty( $svg_contents ) ) {
30            $allowed_tags = array(
31                'svg'    => array(
32                    'style'       => true,
33                    'version'     => true,
34                    'xmlns'       => true,
35                    'xmlns:xlink' => true,
36                ),
37                'defs'   => array(),
38                'symbol' => array(
39                    'id'      => true,
40                    'viewbox' => true,
41                ),
42                'path'   => array(
43                    'd'     => true,
44                    'style' => true,
45                ),
46            );
47            echo wp_kses( $svg_contents, $allowed_tags );
48        }
49    }
50    add_action( 'wp_footer', 'jetpack_social_menu_include_svg_icons', 9999 );
51}
52
53if ( ! function_exists( 'jetpack_social_menu_get_svg' ) ) {
54    /**
55     * Return SVG markup.
56     *
57     * @param array $args {
58     *     Parameters needed to display an SVG.
59     *
60     *     @type string $icon  Required SVG icon filename.
61     * }
62     * @return string SVG markup.
63     */
64    function jetpack_social_menu_get_svg( $args = array() ) {
65        _deprecated_function( __FUNCTION__, 'jetpack-13.7' );
66        // Make sure $args are an array.
67        if ( empty( $args ) ) {
68            return esc_html__( 'Please define default parameters in the form of an array.', 'jetpack' );
69        }
70
71        // Define an icon.
72        if ( false === array_key_exists( 'icon', $args ) ) {
73            return esc_html__( 'Please define an SVG icon filename.', 'jetpack' );
74        }
75
76        // Set defaults.
77        $defaults = array(
78            'icon'     => '',
79            'fallback' => false,
80        );
81
82        // Parse args.
83        $args = wp_parse_args( $args, $defaults );
84
85        // Set aria hidden.
86        $aria_hidden = ' aria-hidden="true"';
87
88        // Begin SVG markup.
89        $svg = '<svg class="icon icon-' . esc_attr( $args['icon'] ) . '"' . $aria_hidden . ' role="img">';
90
91        /*
92         * Display the icon.
93         *
94         * The whitespace around `<use>` is intentional - it is a work around to a keyboard navigation bug in Safari 10.
95         *
96         * See https://core.trac.wordpress.org/ticket/38387.
97         */
98        $svg .= ' <use href="#icon-' . esc_html( $args['icon'] ) . '" xlink:href="#icon-' . esc_html( $args['icon'] ) . '"></use> ';
99
100        // Add some markup to use as a fallback for browsers that do not support SVGs.
101        if ( $args['fallback'] ) {
102            $svg .= '<span class="svg-fallback icon-' . esc_attr( $args['icon'] ) . '"></span>';
103        }
104
105        $svg .= '</svg>';
106
107        return $svg;
108    }
109}
110
111if ( ! function_exists( 'jetpack_social_menu_nav_menu_social_icons' ) ) {
112    /**
113     * Display SVG icons in social links menu.
114     *
115     * @param  string  $item_output The menu item output.
116     * @param  WP_Post $item        Menu item object.
117     * @param  int     $depth       Depth of the menu.
118     * @param  array   $args        wp_nav_menu() arguments.
119     * @return string  $item_output The menu item output with social icon.
120     */
121    function jetpack_social_menu_nav_menu_social_icons( $item_output, $item, $depth, $args ) {
122        _deprecated_function( __FUNCTION__, 'jetpack-13.7' );
123        // Get supported social icons.
124        $social_icons = jetpack_social_menu_social_links_icons();
125
126        // Change SVG icon inside social links menu if there is supported URL.
127        if ( 'jetpack-social-menu' === $args->theme_location ) {
128            foreach ( $social_icons as $attr => $value ) {
129                /*
130                 * attr can be a URL host, or a regex, starting with #.
131                 * Let's check for both scenarios.
132                 */
133                if (
134                    // First Regex.
135                    (
136                        str_starts_with( $attr, '#' ) && str_ends_with( $attr, '#' )
137                        && preg_match( $attr, $item_output )
138                    )
139                    // Then, regular host name.
140                    || str_contains( $item_output, $attr )
141                ) {
142                    $item_output = str_replace(
143                        $args->link_after,
144                        '</span>' . jetpack_social_menu_get_svg( array( 'icon' => esc_attr( $value ) ) ),
145                        $item_output
146                    );
147                }
148            }
149        }
150
151        return $item_output;
152    }
153    add_filter( 'walker_nav_menu_start_el', 'jetpack_social_menu_nav_menu_social_icons', 10, 4 );
154}
155
156if ( ! function_exists( 'jetpack_social_menu_social_links_icons' ) ) {
157    /**
158     * Returns an array of supported social links (URL / regex and icon name).
159     * For regex, use the # delimiter.
160     *
161     * @return array $social_links_icons
162     */
163    function jetpack_social_menu_social_links_icons() {
164        _deprecated_function( __FUNCTION__, 'jetpack-13.7' );
165        // Supported social links icons.
166        $social_links_icons = array(
167            '#https?:\/\/(www\.)?amazon\.(com|cn|in|fr|de|it|nl|es|co|ca)\/#' => 'amazon',
168            '500px.com'         => '500px',
169            'apple.com'         => 'apple',
170            'itunes.com'        => 'apple',
171            'bandcamp.com'      => 'bandcamp',
172            'behance.net'       => 'behance',
173            'blogger.com'       => 'blogger',
174            'blogspot.com'      => 'blogger',
175            'bsky.app'          => 'bluesky',
176            'codepen.io'        => 'codepen',
177            'deviantart.com'    => 'deviantart',
178            'discord.gg'        => 'discord',
179            'discordapp.com'    => 'discord',
180            'digg.com'          => 'digg',
181            'dribbble.com'      => 'dribbble',
182            'dropbox.com'       => 'dropbox',
183            'etsy.com'          => 'etsy',
184            'eventbrite.com'    => 'eventbrite',
185            'facebook.com'      => 'facebook',
186            '/feed/'            => 'feed',
187            'flickr.com'        => 'flickr',
188            'foursquare.com'    => 'foursquare',
189            'ghost.org'         => 'ghost',
190            'goodreads.com'     => 'goodreads',
191            'google.com'        => 'google',
192            'github.com'        => 'github',
193            'instagram.com'     => 'instagram',
194            'linkedin.com'      => 'linkedin',
195            'mailto:'           => 'mail',
196            'meetup.com'        => 'meetup',
197            'medium.com'        => 'medium',
198            'nextdoor.com'      => 'nextdoor',
199            'patreon.com'       => 'patreon',
200            'pinterest.'        => 'pinterest',
201            'getpocket.com'     => 'pocket',
202            'ravelry.com'       => 'ravelry',
203            'reddit.com'        => 'reddit',
204            'slideshare.net'    => 'slideshare',
205            'sms:'              => 'sms',
206            'snapchat.com'      => 'snapchat',
207            'soundcloud.com'    => 'soundcloud',
208            'spotify.com'       => 'spotify',
209            'stackoverflow.com' => 'stackoverflow',
210            'strava.com'        => 'strava',
211            'stumbleupon.com'   => 'stumbleupon',
212            'telegram.me'       => 'telegram',
213            'threads.net'       => 'threads',
214            'tiktok.com'        => 'tiktok',
215            'tumblr.com'        => 'tumblr',
216            'twitch.tv'         => 'twitch',
217            'twitter.com'       => 'twitter',
218            'vimeo.com'         => 'vimeo',
219            'vk.com'            => 'vk',
220            'whatsapp.com'      => 'whatsapp',
221            'woocommerce.com'   => 'woocommerce',
222            'wordpress.org'     => 'wordpress',
223            'wordpress.com'     => 'wordpress',
224            'yelp.com'          => 'yelp',
225            'x.com'             => 'x',
226            'xanga.com'         => 'xanga',
227            'youtube.com'       => 'youtube',
228        );
229
230        /*
231         * Add Mastodon instances to this array.
232         */
233        $mastodon_instance_list = jetpack_mastodon_get_instance_list();
234        foreach ( $mastodon_instance_list as $instance ) {
235            $social_links_icons[ $instance ] = 'mastodon';
236        }
237
238        return $social_links_icons;
239    }
240}