| 1 |
<?php |
| 2 |
|
| 3 |
class HappyForms_Email_Encoder { |
| 4 |
|
| 5 |
private static $instance; |
| 6 |
private $encoder; |
| 7 |
|
| 8 |
public static function instance() { |
| 9 |
if ( is_null( self::$instance ) ) { |
| 10 |
self::$instance = new self(); |
| 11 |
} |
| 12 |
|
| 13 |
return self::$instance; |
| 14 |
} |
| 15 |
|
| 16 |
public function __construct() { |
| 17 |
require_once( happyforms_get_core_folder() . '/lib/php-punycode/src/Exception/OutOfBoundsException.php' ); |
| 18 |
require_once( happyforms_get_core_folder() . '/lib/php-punycode/src/Exception/LabelOutOfBoundsException.php' ); |
| 19 |
require_once( happyforms_get_core_folder() . '/lib/php-punycode/src/Exception/DomainOutOfBoundsException.php' ); |
| 20 |
require_once( happyforms_get_core_folder() . '/lib/php-punycode/src/Punycode.php' ); |
| 21 |
|
| 22 |
$this->encoder = new TrueBV\Punycode(); |
| 23 |
} |
| 24 |
|
| 25 |
private function encode( $string ) { |
| 26 |
try { |
| 27 |
$string = $this->encoder->encode( $string ); |
| 28 |
} catch( Exception $e ) { |
| 29 |
error_log( $e->getMessage() ); |
| 30 |
} |
| 31 |
|
| 32 |
return $string; |
| 33 |
} |
| 34 |
|
| 35 |
private function decode( $string ) { |
| 36 |
try { |
| 37 |
$string = $this->encoder->decode( $string ); |
| 38 |
} catch( Exception $e ) { |
| 39 |
error_log( $e->getMessage() ); |
| 40 |
} |
| 41 |
|
| 42 |
return $string; |
| 43 |
} |
| 44 |
|
| 45 |
public function encode_email( $address ) { |
| 46 |
if ( ! apply_filters( 'happyforms_encode_puny_address', true, $address ) ) { |
| 47 |
return $address; |
| 48 |
} |
| 49 |
|
| 50 |
$parts = explode( '@', $address ); |
| 51 |
$parts = array_map( array( $this, 'encode' ), $parts ); |
| 52 |
$address = implode( '@', $parts ); |
| 53 |
|
| 54 |
return $address; |
| 55 |
} |
| 56 |
|
| 57 |
public function encode_url( $address ) { |
| 58 |
if ( ! apply_filters( 'happyforms_encode_puny_address', true, $address ) ) { |
| 59 |
return $address; |
| 60 |
} |
| 61 |
|
| 62 |
return $this->encode( $address ); |
| 63 |
} |
| 64 |
|
| 65 |
public function decode_email( $address ) { |
| 66 |
if ( ! apply_filters( 'happyforms_decode_puny_address', true, $address ) ) { |
| 67 |
return $address; |
| 68 |
} |
| 69 |
|
| 70 |
$parts = explode( '@', $address ); |
| 71 |
$parts = array_map( array( $this, 'decode' ), $parts ); |
| 72 |
$address = implode( '@', $parts ); |
| 73 |
|
| 74 |
return $address; |
| 75 |
} |
| 76 |
|
| 77 |
public function decode_url( $address ) { |
| 78 |
if ( ! apply_filters( 'happyforms_decode_puny_address', true, $address ) ) { |
| 79 |
return $address; |
| 80 |
} |
| 81 |
|
| 82 |
return $this->encoder->decode( $address ); |
| 83 |
} |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
if ( ! function_exists( 'happyforms_get_email_encoder' ) ): |
| 88 |
|
| 89 |
function happyforms_get_email_encoder() { |
| 90 |
return HappyForms_Email_Encoder::instance(); |
| 91 |
} |
| 92 |
|
| 93 |
endif; |
| 94 |
|
| 95 |
happyforms_get_email_encoder(); |
| 96 |
|