Source: icons.php
<?php if ( ! defined( 'ABSPATH' ) ) {
die( 'Cheatin’ uh?' );
}
/**
* SVG icons of the buttons (since 2.5.0).
*
* Core networks icons are SVG files in `skins/common/icons/{slug}.svg`, using `fill="currentColor"`:
* they take the color of the button text, like the former icon font did.
* The icon font is still available ("legacy" rendering mode) for custom skins built on it.
*/
if ( ! function_exists( 'jsps_get_icon_rendering_mode' ) ) {
/**
* The icons rendering mode used on the front: `svg` or `font`.
*
* The `juiz_sps_icon_rendering` option can be:
* - `auto`: SVG when the active skin supports it (all core skins, custom skins declaring `support_svg_icons`), else the icon font.
* - `svg`: always SVG icons.
* - `font`: always the icon font (legacy). Default for sites installed before 2.5.0.
*
* @return (string) `svg` or `font`.
*
* @since 2.5.0
*/
function jsps_get_icon_rendering_mode() {
static $mode = null;
if ( null !== $mode ) {
return $mode;
}
$options = jsps_get_option();
$option = is_array( $options ) && isset( $options['juiz_sps_icon_rendering'] ) ? $options['juiz_sps_icon_rendering'] : 'font';
$skin = is_array( $options ) && isset( $options['juiz_sps_style'] ) ? $options['juiz_sps_style'] : '';
if ( 'svg' === $option ) {
$resolved = 'svg';
} elseif ( 'auto' === $option ) {
// Plugin CSS not used: the site's own CSS is probably built on the icon font.
$own_css = ! apply_filters( 'juiz_sps_use_default_css', true ) || ( isset( $options['juiz_sps_css_files'] ) && in_array( $options['juiz_sps_css_files'], array( 'both', 'buttons' ), true ) );
$resolved = ! $own_css && jsps_skin_supports_svg_icons( $skin ) ? 'svg' : 'font';
} else {
$resolved = 'font';
}
/**
* The icons rendering mode used for the buttons.
*
* @hook jsps_icon_rendering_mode
*
* @since 2.5.0 First version
* @param {string} $mode `svg` or `font`.
* @param {string} $option The option value: `auto`, `svg` or `font`.
* @param {string} $skin The active skin slug.
* @return {string} `svg` or `font`.
*/
$mode = 'svg' === apply_filters( 'jsps_icon_rendering_mode', $resolved, $option, $skin ) ? 'svg' : 'font';
return $mode;
}
}
if ( ! function_exists( 'jsps_skin_supports_svg_icons' ) ) {
/**
* Whether a skin declares the support of SVG icons (`support_svg_icons` key of the skin).
*
* @param (string) $skin_slug The skin slug.
* @return (boolean)
*
* @since 2.5.0
*/
function jsps_skin_supports_svg_icons( $skin_slug ) {
$core = jsps_get_core_skins();
// Core skins: no theme folder scan needed.
$skins = isset( $core[ $skin_slug ] ) ? $core : $core + jsps_get_custom_skins();
return isset( $skins[ $skin_slug ]['support_svg_icons'] ) && true === $skins[ $skin_slug ]['support_svg_icons'];
}
}
if ( ! function_exists( 'jsps_get_core_svg_icon' ) ) {
/**
* The SVG markup of a core network icon.
*
* @param (string) $slug The network slug.
* @return (string) The `<svg>` markup, or '' if there is none.
*
* @since 2.5.0
*/
function jsps_get_core_svg_icon( $slug ) {
static $cache = array();
if ( isset( $cache[ $slug ] ) ) {
return $cache[ $slug ];
}
$file = plugin_dir_path( JUIZ_SPS_FILE ) . 'skins/common/icons/' . sanitize_key( $slug ) . '.svg';
$svg = is_readable( $file ) ? trim( (string) file_get_contents( $file ) ) : '';
/**
* The SVG markup of a core network icon. Use it to replace an icon by your own:
* a `<svg>` element with a `viewBox`, using `fill="currentColor"` to get the button color.
*
* @hook jsps_core_svg_icon
*
* @since 2.5.0 First version
* @param {string} $svg The `<svg>` markup ('' if the network has no core icon).
* @param {string} $slug The network slug.
* @return {string} The `<svg>` markup.
*/
$svg = (string) apply_filters( 'jsps_core_svg_icon', $svg, $slug );
$cache[ $slug ] = 0 === stripos( $svg, '<svg' ) ? $svg : '';
return $cache[ $slug ];
}
}
if ( ! function_exists( 'jsps_get_svg_icon_markup' ) ) {
/**
* The icon of a button in SVG mode.
*
* By default the first use of an icon in the page prints its `<symbol>` once, the next ones
* reference it with `<use>`: on archive pages, the same icon isn't repeated in the HTML.
*
* @param (string) $slug The network slug.
* @param (string) $delivery `sprite` or `inline`. Empty: the `jsps_svg_icon_delivery` filter decides.
* @return (string) The `<svg class="jsps-svg">` markup, or '' if there is no icon.
*
* @since 2.5.0
*/
function jsps_get_svg_icon_markup( $slug, $delivery = '' ) {
static $printed = array();
$svg = jsps_get_core_svg_icon( $slug );
if ( '' === $svg || ! preg_match( '#<svg\b([^>]*)>(.*)</svg>#is', $svg, $m ) ) {
return '';
}
$viewbox = preg_match( '#viewBox=["\']([^"\']+)["\']#i', $m[1], $vb ) ? $vb[1] : '0 0 24 24';
// Brand icons (24px grid) fill their whole box, the former font glyphs used ~77% of the em box:
// same optical size with a padded viewBox, so switching to SVG doesn't make the icons bigger.
if ( '0 0 24 24' === trim( $viewbox ) ) {
$viewbox = '-3.6 -3.6 31.2 31.2';
}
$inner = $m[2];
$id = 'jsps-i-' . sanitize_key( $slug );
$attrs = 'class="jsps-svg jsps-svg-' . esc_attr( sanitize_key( $slug ) ) . '" viewBox="' . esc_attr( $viewbox ) . '" fill="currentColor" aria-hidden="true" focusable="false"';
/**
* How SVG icons are printed: `sprite` (each icon printed once per page, then referenced with `<use>`)
* or `inline` (the full icon each time). Use `inline` if a cache or a script removes
* some buttons bars from the page, which would break the references.
*
* @hook jsps_svg_icon_delivery
*
* @since 2.5.0 First version
* @param {string} $delivery=sprite `sprite` or `inline`.
* @return {string} `sprite` or `inline`.
*/
if ( '' === $delivery ) {
$delivery = apply_filters( 'jsps_svg_icon_delivery', 'sprite' );
}
if ( 'inline' === $delivery ) {
return '<svg ' . $attrs . '>' . $inner . '</svg>';
}
// The <use> of a <symbol> fills the outer viewport from its origin: the outer viewBox must start at 0 0
// (the symbol keeps the real, padded one), else the icon is shifted.
$vb_parts = preg_split( '/[\s,]+/', trim( $viewbox ) );
$outer_vb = 4 === count( $vb_parts ) ? '0 0 ' . $vb_parts[2] . ' ' . $vb_parts[3] : $viewbox;
$use_attrs = str_replace( 'viewBox="' . esc_attr( $viewbox ) . '"', 'viewBox="' . esc_attr( $outer_vb ) . '"', $attrs );
if ( ! isset( $printed[ $id ] ) ) {
$printed[ $id ] = true;
return '<svg ' . $use_attrs . '><symbol id="' . esc_attr( $id ) . '" viewBox="' . esc_attr( $viewbox ) . '">' . $inner . '</symbol><use href="#' . esc_attr( $id ) . '"/></svg>';
}
return '<svg ' . $use_attrs . '><use href="#' . esc_attr( $id ) . '"/></svg>';
}
}
if ( ! function_exists( 'jsps_get_admin_svg_icon' ) ) {
/**
* The SVG icon of a core network for the administration (networks list, modals).
* Always printed inline: the admin markup can be copied by scripts (modal).
*
* @param (string) $slug The network slug.
* @return (string) `<i class="jsps-svg-icon">` + the `<svg>`, or '' if there is no core icon.
*
* @since 2.5.0
*/
function jsps_get_admin_svg_icon( $slug ) {
$svg = jsps_get_svg_icon_markup( $slug, 'inline' );
return '' === $svg ? '' : '<i class="jsps-svg-icon" role="presentation">' . $svg . '</i>';
}
}
Plugin and documentation by Geoffrey Crofte - All Right Reserved