filter.php
99 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikWP - Libraries |
| 4 | * @subpackage adapter.output |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * Output filter for strings. |
| 16 | * |
| 17 | * @since 10.0 |
| 18 | */ |
| 19 | class JFilterOutput |
| 20 | { |
| 21 | /** |
| 22 | * This method processes a string and replaces all accented UTF-8 characters by unaccented |
| 23 | * ASCII-7 "equivalents", whitespaces are replaced by hyphens and the string is lowercase. |
| 24 | * |
| 25 | * @param string $string The string to process. |
| 26 | * @param string $language The language to use for transliteration. |
| 27 | * |
| 28 | * @return string The processed string. |
| 29 | */ |
| 30 | public static function stringURLSafe($string, $language = '') |
| 31 | { |
| 32 | // remove any '-' from the string since they will be used as concatenaters |
| 33 | $str = str_replace('-', ' ', $string); |
| 34 | |
| 35 | // transliterate on the language requested (fallback to current language if not specified) |
| 36 | $lang = $language == '' || $language == '*' ? JFactory::getLanguage() : JLanguage::getInstance($language); |
| 37 | $str = $lang->transliterate($str); |
| 38 | |
| 39 | // trim white spaces at beginning and end of alias and make lowercase |
| 40 | $str = trim(mb_strtolower($str)); |
| 41 | |
| 42 | // remove any duplicate whitespace, and ensure all characters are alphanumeric |
| 43 | $str = preg_replace('/(\s|[^A-Za-z0-9\-])+/', '-', $str); |
| 44 | |
| 45 | // trim dashes at beginning and end of alias |
| 46 | $str = trim($str, '-'); |
| 47 | |
| 48 | return $str; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Helper method to generate a WP shortcode. |
| 53 | * |
| 54 | * @param string $component The shortcode name (component). |
| 55 | * @param array $attrs The shortcode args. |
| 56 | * @param string $content The shortcode contents. |
| 57 | */ |
| 58 | public static function shortcode($component, $attrs = array(), $content = null) |
| 59 | { |
| 60 | $shortcode = ''; |
| 61 | |
| 62 | foreach ($attrs as $k => $v) |
| 63 | { |
| 64 | if (is_array($v)) |
| 65 | { |
| 66 | $v = implode(',', $v); |
| 67 | } |
| 68 | |
| 69 | if (is_scalar($v)) |
| 70 | { |
| 71 | // encode single and double quotes |
| 72 | $v = htmlentities($v, ENT_QUOTES); |
| 73 | // encode left brackets |
| 74 | $v = str_replace('[', '[', $v); |
| 75 | // encode right brackets |
| 76 | $v = str_replace(']', ']', $v); |
| 77 | |
| 78 | $shortcode .= " $k=\"$v\""; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if ($content && is_string($content)) |
| 83 | { |
| 84 | $v = $content; |
| 85 | |
| 86 | // encode single and double quotes |
| 87 | $v = htmlentities($v, ENT_QUOTES); |
| 88 | // encode left brackets |
| 89 | $v = str_replace('[', '[', $v); |
| 90 | // encode right brackets |
| 91 | $v = str_replace(']', ']', $v); |
| 92 | |
| 93 | $shortcode = " \"$v\""; |
| 94 | } |
| 95 | |
| 96 | return "[{$component}$shortcode]"; |
| 97 | } |
| 98 | } |
| 99 |