Created
May 8, 2019 20:14
-
-
Save Sean12mps/a5811875c87257c8fd290694bd67ca20 to your computer and use it in GitHub Desktop.
WhatsApp Link Converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * WhatsApp link converter. | |
| * | |
| * @since 1.0 | |
| * @author Sean12mps <sean.michael.piettojo@gmail.com> | |
| */ | |
| class WhatsApp_Link_Converter { | |
| /** | |
| * WhatsApp current URL. | |
| * | |
| * @since 1.0 | |
| */ | |
| const WA_URL = 'https://wa.me/'; | |
| /** | |
| * Target's phone number. Number character only. | |
| * | |
| * @since 1.0 | |
| * @access private | |
| * @var string eg: "123456789" | |
| */ | |
| private $phone_number; | |
| /** | |
| * Text formatting to be used. | |
| * | |
| * @since 1.0 | |
| * @access private | |
| * @var string eg: "Hello, my name is {name}, I love to {activity}" | |
| */ | |
| private $format; | |
| /** | |
| * Fields to be used on the format. | |
| * | |
| * @since 1.0 | |
| * @access private | |
| * @var array eg: array( 'name'=> 'Sean', 'activity'=> 'code' ) | |
| */ | |
| private $fields = array(); | |
| /** | |
| * Processed link. | |
| * | |
| * @since 1.0 | |
| * @access private | |
| * @var string eg: "https://wa.me/123456789?text=Hello%2C+my+name+is+Sean%2C+I+love+to+code" | |
| */ | |
| private $url_link; | |
| /** | |
| * Setup converter. | |
| * | |
| * @since 1.0 | |
| * @param $phone_number string Target's phone number. | |
| * $format string Text formatting. eg: "Hello, my name is {name}, I love to {activity}". | |
| * $fields array Fields to be used on the format. eg: array( 'name'=> 'Sean', 'activity'=> 'code' ). | |
| */ | |
| public function __construct( $phone_number, $format, $fields = array() ) { | |
| $this->set_phone_number( $phone_number ); | |
| $this->set_format( $format ); | |
| $this->set_fields( $fields ); | |
| $this->generate_link(); | |
| } | |
| /** | |
| * Setup phone number. | |
| * | |
| * @since 1.0 | |
| */ | |
| public function set_phone_number( $number ) { | |
| if ( ! is_string( $number ) ) throw new Exception( 'Phone Number is not a string.' ); | |
| $this->phone_number = str_replace( array( '-', '+', ' ' ), '', $number ); | |
| } | |
| /** | |
| * Setup text format. | |
| * | |
| * @since 1.0 | |
| */ | |
| public function set_format( $format ) { | |
| if ( ! is_string( $format ) ) throw new Exception( 'Text format is not a string.' ); | |
| $this->format = $format; | |
| } | |
| /** | |
| * Setup fields. | |
| * | |
| * @since 1.0 | |
| */ | |
| public function set_fields( $fields ) { | |
| if ( ! $fields ) return; | |
| $this->fields = $fields; | |
| } | |
| /** | |
| * Process Whatsapp link. | |
| * | |
| * @since 1.0 | |
| */ | |
| public function generate_link() { | |
| $text_message = $this->format; | |
| if ( $this->fields ) { | |
| foreach ( $this->fields as $field => $value ) { | |
| $text_message = str_replace( '{' . $field .'}', $value, $text_message ); | |
| } | |
| } | |
| $text = urlencode( $text_message ); | |
| $this->url_link = self::WA_URL . $this->phone_number . '?text=' . $text; | |
| } | |
| /** | |
| * Get processed Whatsapp link. | |
| * | |
| * @since 1.0 | |
| */ | |
| public function get_link() { | |
| return $this->url_link; | |
| } | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: