Assets.php
6 hours ago
Cache.php
3 weeks ago
Date.php
6 hours ago
Debug.php
5 hours ago
FeedReader.php
6 hours ago
HTML.php
6 hours ago
Helper.php
3 weeks ago
JSON.php
3 weeks ago
Nonce.php
3 weeks ago
Notice.php
6 hours ago
Number.php
3 weeks ago
NumberConverter.php
6 hours ago
Param.php
3 weeks ago
Sanitizing.php
6 hours ago
Strip.php
3 weeks ago
Templates.php
3 weeks ago
User.php
3 weeks ago
Validating.php
3 weeks ago
WooCommerce.php
3 weeks ago
WordPress.php
6 hours ago
Assets.php
125 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPParsidate\Helper; |
| 4 | |
| 5 | defined( 'ABSPATH' ) || exit; |
| 6 | |
| 7 | class Assets { |
| 8 | /** |
| 9 | * Get assets version, if plugin or WP debug mode activated add timestamp to version string. |
| 10 | * |
| 11 | * @return string Assets Version string |
| 12 | */ |
| 13 | public static function getVersion(): string { |
| 14 | return WP_PARSI_VER . ( Debug::check() || SCRIPT_DEBUG ? time() : '' ); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Get asset url |
| 19 | * |
| 20 | * @param string $path Assets path |
| 21 | * |
| 22 | * @return string Asset url |
| 23 | */ |
| 24 | public static function url( string $path ): string { |
| 25 | return WP_PARSI_URL . 'assets/' . $path; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Determines whether the string is HTML image string |
| 30 | * |
| 31 | * @param string $string HTML image string |
| 32 | * |
| 33 | * @return bool |
| 34 | */ |
| 35 | public static function isImageString( string $string ): bool { |
| 36 | return str_starts_with( trim( $string ), '<img' ) !== false; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Determines whether the string is HTML image string |
| 41 | * |
| 42 | * @param string $string HTML image string |
| 43 | * @param bool $clean Clean HTML string before check |
| 44 | * |
| 45 | * @return bool |
| 46 | */ |
| 47 | public static function isSvgImageString( string $string, bool $clean = true ): bool { |
| 48 | if ( $clean ) { |
| 49 | $string = self::cleanSvgImageString( $string ); |
| 50 | } |
| 51 | |
| 52 | return str_starts_with( trim( $string ), '<svg' ) !== false; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Clean HTML SVG string |
| 57 | * |
| 58 | * @param string $svg HTML SVG string |
| 59 | * |
| 60 | * @return string |
| 61 | */ |
| 62 | public static function cleanSvgImageString( string $svg ): string { |
| 63 | $svg = Sanitizing::svg( $svg ); |
| 64 | $svg = Strip::removeHtmlComments( $svg ); |
| 65 | $svg = Strip::removeHtmlDoctype( $svg ); |
| 66 | |
| 67 | $svg = trim( $svg ); |
| 68 | $svg = str_replace( "\n", '', $svg ); |
| 69 | |
| 70 | return trim( $svg ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Set HTML SVG image string width and height |
| 75 | * |
| 76 | * @param string $svg |
| 77 | * @param int $width SVG width |
| 78 | * @param int|null $height SVG height, If not set equal to width |
| 79 | * @param bool $clean Clean HTML string before check |
| 80 | * |
| 81 | * @return string HTML SVG string |
| 82 | */ |
| 83 | public static function setSvgDimensions( string $svg, int $width, ?int $height = null, bool $clean = true ): string { |
| 84 | if ( is_null( $height ) ) { |
| 85 | $height = $width; |
| 86 | } |
| 87 | |
| 88 | if ( $clean ) { |
| 89 | $svg = self::cleanSvgImageString( $svg ); |
| 90 | } |
| 91 | |
| 92 | if ( ! empty( $svg ) && self::isSvgImageString( $svg ) ) { |
| 93 | $openingTag = $openTag = substr( $svg, 0, mb_strpos( $svg, '>' ) + 1 ); |
| 94 | $svgWidth = $svgHeight = null; |
| 95 | if ( $openingTag ) { |
| 96 | preg_match( '/width="(.+?)"/', $openingTag, $matches ); |
| 97 | if ( ! empty( $matches ) ) { |
| 98 | $svgWidth = $matches[0]; |
| 99 | } |
| 100 | |
| 101 | preg_match( '/height="(.+?)"/', $openingTag, $matches ); |
| 102 | if ( ! empty( $matches ) ) { |
| 103 | $svgHeight = $matches[0]; |
| 104 | } |
| 105 | |
| 106 | if ( is_null( $svgWidth ) ) { |
| 107 | $openTag = substr_replace( $openTag, ' width="' . $width . '"', mb_strlen( $openTag ) - 1, 0 ); |
| 108 | } else { |
| 109 | $openTag = str_replace( $svgWidth, 'width="' . $width . '"', $openTag ); |
| 110 | } |
| 111 | |
| 112 | if ( is_null( $svgHeight ) ) { |
| 113 | $openTag = substr_replace( $openTag, ' height="' . $height . '"', mb_strlen( $openTag ) - 1, 0 ); |
| 114 | } else { |
| 115 | $openTag = str_replace( $svgHeight, 'height="' . $height . '"', $openTag ); |
| 116 | } |
| 117 | |
| 118 | $svg = str_replace( $openingTag, $openTag, $svg ); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return $svg; |
| 123 | } |
| 124 | } |
| 125 |