utm-url.php
75 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Classes\Http; |
| 5 | |
| 6 | use Jet_Form_Builder\Addons\Manager; |
| 7 | use Jet_Form_Builder\Classes\Theme\Theme_Info; |
| 8 | |
| 9 | class Utm_Url { |
| 10 | |
| 11 | private $check_license = false; |
| 12 | private $source = ''; |
| 13 | private $medium = ''; |
| 14 | private $campaign = ''; |
| 15 | |
| 16 | public function __construct( string $source = '' ) { |
| 17 | if ( $source ) { |
| 18 | $this->set_source( $source ); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | public function set_source( string $source ): Utm_Url { |
| 23 | $this->source = rawurlencode( $source ); |
| 24 | |
| 25 | return $this; |
| 26 | } |
| 27 | |
| 28 | public function set_license( bool $check_license ): Utm_Url { |
| 29 | $this->check_license = $check_license; |
| 30 | |
| 31 | return $this; |
| 32 | } |
| 33 | |
| 34 | public function get_medium(): string { |
| 35 | if ( ! $this->medium ) { |
| 36 | $this->medium = $this->get_raw_medium(); |
| 37 | } |
| 38 | |
| 39 | return $this->medium; |
| 40 | } |
| 41 | |
| 42 | public function add_query( string $url ): string { |
| 43 | return add_query_arg( |
| 44 | array( |
| 45 | 'utm_source' => $this->source, |
| 46 | 'utm_medium' => $this->get_medium(), |
| 47 | 'utm_campaign' => $this->campaign, |
| 48 | ), |
| 49 | $url |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | public function set_campaign( string $campaign ): Utm_Url { |
| 54 | $this->campaign = $campaign; |
| 55 | |
| 56 | return $this; |
| 57 | } |
| 58 | |
| 59 | private function get_raw_medium(): string { |
| 60 | $page = jet_fb_current_page(); |
| 61 | if ( ! $page ) { |
| 62 | $author_slug = ( new Theme_Info() )->author_slug(); |
| 63 | } else { |
| 64 | $author_slug = $page->theme()->author_slug(); |
| 65 | } |
| 66 | $license = $this->check_license |
| 67 | ? jet_form_builder()->addons_manager->get_slug() |
| 68 | : Manager::NOT_ACTIVE; |
| 69 | |
| 70 | return rawurlencode( "$license/$author_slug" ); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | } |
| 75 |