ShortLinksGenerator.php
52 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPDesk\FCF\Free\Service; |
| 4 | |
| 5 | use FcfVendor\WPDesk\PluginBuilder\Plugin\Hookable; |
| 6 | use FcfVendor\WPDesk\PluginBuilder\Plugin\HookablePluginDependant; |
| 7 | use FcfVendor\WPDesk\PluginBuilder\Plugin\PluginAccess; |
| 8 | |
| 9 | /** |
| 10 | * Creates helpers for short URLs. |
| 11 | */ |
| 12 | class ShortLinksGenerator implements Hookable, HookablePluginDependant { |
| 13 | |
| 14 | use PluginAccess; |
| 15 | |
| 16 | const SHORTENER_DOMAIN_PL = 'https://wpdesk.pl/sk/'; |
| 17 | |
| 18 | const SHORTENER_DOMAIN_EN = 'https://wpdesk.net/sk/'; |
| 19 | |
| 20 | /** |
| 21 | * {@inheritdoc} |
| 22 | */ |
| 23 | public function hooks() { |
| 24 | add_filter( 'flexible_checkout_fields/short_url', [ $this, 'generate_short_url' ], 10, 2 ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Generates short URL for link. |
| 29 | * |
| 30 | * @param string $default_value Default value for filter. |
| 31 | * @param string $short_path Path for short URL. |
| 32 | * |
| 33 | * @return string Short URL. |
| 34 | * @internal |
| 35 | */ |
| 36 | public function generate_short_url( string $default_value, string $short_path ): string { |
| 37 | if ( ! preg_match( '/^[a-z-]+$/i', $short_path ) ) { |
| 38 | return '#'; |
| 39 | } |
| 40 | |
| 41 | $locale = get_user_locale(); |
| 42 | $locale_url = $locale === 'pl_PL' ? self::SHORTENER_DOMAIN_PL : self::SHORTENER_DOMAIN_EN; |
| 43 | $short_url = $locale_url . $short_path; |
| 44 | switch ( $locale ) { |
| 45 | case 'pl_PL': |
| 46 | $short_url .= '-pl'; |
| 47 | break; |
| 48 | } |
| 49 | return $short_url; |
| 50 | } |
| 51 | } |
| 52 |