Block.php
2 years ago
BlockFinder.php
5 years ago
DynamicData.php
4 years ago
HasOneRelationship.php
5 years ago
Integration.php
5 years ago
Utility.php
4 years ago
DynamicData.php
75 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PrestoPlayer\Support; |
| 4 | |
| 5 | class DynamicData |
| 6 | { |
| 7 | /** |
| 8 | * Get values to replace. |
| 9 | * |
| 10 | * @return array |
| 11 | */ |
| 12 | public static function getValues() |
| 13 | { |
| 14 | $current_user = wp_get_current_user(); |
| 15 | |
| 16 | return apply_filters('presto-player/dynamic-data', [ |
| 17 | '{user.user_login}' => $current_user->user_login ?? '', |
| 18 | '{user.user_nicename}' => $current_user->user_nicename ?? '', |
| 19 | '{user.user_email}' => $current_user->user_email ?? '', |
| 20 | '{user.user_url}' => $current_user->user_url ?? '', |
| 21 | '{user.user_registered}' => $current_user->user_registered ?? '', |
| 22 | '{user.display_name}' => $current_user->display_name ?? '', |
| 23 | '{site.url}' => get_home_url(), |
| 24 | '{site.name}' => get_bloginfo(), |
| 25 | '{ip_address}' => self::getIP() |
| 26 | ]); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Replace dynamic data with actual data. |
| 31 | * |
| 32 | * @param array $items Array of items with ['text']. |
| 33 | * @return array |
| 34 | */ |
| 35 | public static function replaceItems($items, $key) |
| 36 | { |
| 37 | foreach ($items as $k => $item) { |
| 38 | $items[$k][$key] = self::replaceText($item[$key]); |
| 39 | } |
| 40 | |
| 41 | return $items; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Replace value in string with dynamic data. |
| 46 | * |
| 47 | * @param string $text String with dynamic data. |
| 48 | * @return string |
| 49 | */ |
| 50 | public static function replaceText($text) |
| 51 | { |
| 52 | return wp_kses_post(strtr($text, self::getValues())); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get the person's IP. |
| 57 | * |
| 58 | * @return string |
| 59 | */ |
| 60 | public static function getIP() |
| 61 | { |
| 62 | foreach (array('HTTP_CF_CONNECTING_IP', 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) { |
| 63 | if (array_key_exists($key, $_SERVER) === true) { |
| 64 | foreach (explode(',', $_SERVER[$key]) as $ip) { |
| 65 | $ip = trim($ip); // just to be safe |
| 66 | |
| 67 | if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) { |
| 68 | return $ip; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 |