hedroc

<?php /** * Template tags to render in theme. * * Should be prefixed (keg_) to avoid collisions. * * @package hedroc */ use SVG\SVG; /** * Inlines a local SVG * * @param string|int $identifier Either the media ID if it's coming * from the media lib, or the file name (sans extension) if it lives in the repo.. */ function keg_get_svg( $identifier, $external = false ) { $path = $external ? get_attached_file( $identifier ) : get_template_directory() . '/img/svg/' . $identifier . '.svg'; $icon_svg = SVG::fromFile( $path ); $doc = $icon_svg->getDocument() ->removeAttribute( 'width' ) ->removeAttribute( 'height' ); $view_box = $doc->getViewBox(); $size = [ $view_box[2], $view_box[3] ]; $render_fn = function() use ( $icon_svg ) { echo $icon_svg; }; return [ $render_fn, $size ]; } function keg_svg( $identifier, $external = false ) { $render_fn = keg_get_svg( $identifier, $external )[0]; $render_fn(); } /** * Like get_template_part() put lets you pass args to the template file * Args are available in the template as $template_args array * * @param string $file The file to include. * @param mixed $template_args A wp_args style argument list. * @param mixed $cache_args A wp_args style argument list. * @return mixed * @link https://github.com/humanmade/hm-core/blob/master/hm-core.functions.php#L1237 */ function keg_get_template_part( $file, $template_args = array(), $cache_args = array() ) { $cache; $template_args = wp_parse_args( $template_args ); $cache_args = wp_parse_args( $cache_args ); if ( $cache_args ) { foreach ( $template_args as $key => $value ) { if ( is_scalar( $value ) || is_array( $value ) ) { $cache_args[ $key ] = $value; } elseif ( is_object( $value ) && method_exists( $value, 'get_id' ) ) { $cache_args[ $key ] = call_user_method( 'get_id', $value ); } } if ( ( $cache = wp_cache_get( $file, serialize( $cache_args ) ) ) !== false ) { if ( ! empty( $template_args['return'] ) ) { return $cache; } echo $cache; return; } } $template_args = new \Adbar\Dot( $template_args ); $file_handle = $file; do_action( 'start_operation', 'keg_get_template_part::' . $file_handle ); if ( file_exists( get_stylesheet_directory() . '/' . $file . '.php' ) ) { $file = get_stylesheet_directory() . '/' . $file . '.php'; } elseif ( file_exists( get_template_directory() . '/' . $file . '.php' ) ) { $file = get_template_directory() . '/' . $file . '.php'; } ob_start(); $return = require $file; $data = ob_get_clean(); do_action( 'end_operation', 'keg_get_template_part::' . $file_handle ); if ( $cache_args ) { wp_cache_set( $file, $data, serialize( $cache_args ), 3600 ); } if ( ! empty( $template_args['return'] ) ) { if ( false === $return ) { return false; } else { return $data; } } echo $data; } /** * A pale imitation of classnames * * @param string $base_classes Classes applied wholesale. * @param string[] $conditional_classes Classes to add conditionally. * @return string * @link https://github.com/JedWatson/classnames */ function keg_classlist( string $base_classes, array $conditional_classes ) : string { $filtered_classes = array_filter( $conditional_classes, function( $class_item ) { return $class_item; } ); return $base_classes . ' ' . join( ' ', array_keys( $filtered_classes ) ); } /** * Adds non-breaking space between last few words. * * @param string $text String to convert. * @param int $min_words Number of words to add nbsp to. * @return string */ function word_wrapper( $text = '', int $min_words = 3 ) : string { if ( ! $text ) { return ''; } $return = $text; $arr = explode( ' ', $text ); if ( count( $arr ) >= $min_words ) { $arr[ count( $arr ) - 2 ] .= '&nbsp;' . $arr[ count( $arr ) - 1 ]; array_pop( $arr ); $return = implode( ' ', $arr ); } return $return; } /** * Builds source string for img tag. * * @param int $image_id The id of the attachment. * @param array $sizes WP crop sizes, e.g. ['large']. * @todo Does this break with hard crops + small images? * @return string */ function keg_build_attachment_srcset( $image_id, $sizes ) { $src_strings = array_map( function( $size ) use ( $image_id ) { $image_data = wp_get_attachment_image_src( $image_id, $size ); return $image_data[0] . ' ' . $image_data[1] . 'w'; }, $sizes ); return join( ', ', array_unique( $src_strings ) ); } /** * Get size information for all currently-registered image sizes. * * @global $_wp_additional_image_sizes * @uses get_intermediate_image_sizes() * @return array $sizes Data for all currently-registered image sizes. * @link https://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes */ function keg_get_image_sizes() { static $image_sizes; global $_wp_additional_image_sizes; if ( ! is_null( $image_sizes ) ) { return $image_sizes; } $sizes = array(); foreach ( get_intermediate_image_sizes() as $_size ) { if ( in_array( $_size, array( 'thumbnail', 'medium', 'medium_large', 'large' ) ) ) { $sizes[ $_size ]['width'] = get_option( "{$_size}_size_w" ); $sizes[ $_size ]['height'] = get_option( "{$_size}_size_h" ); $sizes[ $_size ]['crop'] = (bool) get_option( "{$_size}_crop" ); } elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) { $sizes[ $_size ] = array( 'width' => $_wp_additional_image_sizes[ $_size ]['width'], 'height' => $_wp_additional_image_sizes[ $_size ]['height'], 'crop' => $_wp_additional_image_sizes[ $_size ]['crop'], ); } } return $sizes; } /** * Picks the first truthy value from an array of fn returns * * @param array $conditions An array of functions with explicit return values. * @return string */ function keg_evaluate_conditions( array $conditions ) { foreach ( $conditions as $user_fn ) { $val = $user_fn(); if ( $val ) { return $val; } } return null; } /** * Starts a new content section. */ function keg_section_start() { Hedroc_Section::instance()->start(); } /** * Ends a content section. */ function keg_section_end() { Hedroc_Section::instance()->end(); } /** * Gets the current heading level for the section. * * @return int */ function keg_get_section_level() { return Hedroc_Section::instance()->get_level(); } /** * Gets the width/height attributes from an HTML string. * * Useful for parsing an oembed to calculate the ratio. * * @param $str * @return array */ function keg_get_width_and_height_attr( $str ) { $width_pattern = '/width=\"([0-9]*)\"/'; $height_pattern = '/height=\"([0-9]*)\"/'; preg_match( $width_pattern, $str, $width_match ); preg_match( $height_pattern, $str, $height_match ); return [ 'width' => $width_match[1], 'height' => $height_match[1], ]; }