AJAX
5 months ago
Admin_Page
5 months ago
Click_Tracking
5 months ago
ColumnOptions
6 months ago
Cron
9 months ago
Custom_WordPress_Columns
8 months ago
Data_Pruning
5 months ago
Date_Picker
5 months ago
Date_Range
5 months ago
Ecommerce
5 months ago
Email_Reports
5 months ago
Examiner
11 months ago
Favicon
5 months ago
Form_Submissions
5 months ago
Integrations
6 months ago
Interval
1 year ago
Journey
5 months ago
Migrations
5 months ago
Models
5 months ago
Overview
5 months ago
Public_API
5 months ago
Rows
5 months ago
Statistics
5 months ago
Tables
5 months ago
Utils
5 months ago
Views
5 months ago
WooCommerceOrderMetaBox
5 months ago
Admin_Bar_Stats.php
5 months ago
Appearance.php
1 year ago
Campaign_Builder.php
5 months ago
Capability_Manager.php
1 year ago
Chart.php
9 months ago
Chart_Data.php
1 year ago
Click_Tracking.php
1 year ago
Cron_Job.php
5 months ago
Cron_Manager.php
5 months ago
Current_Traffic_Finder.php
1 year ago
Dashboard_Options.php
6 months ago
Dashboard_Widget.php
2 years ago
Database.php
8 months ago
Database_Manager.php
5 months ago
Empty_Report_Option.php
2 years ago
Env.php
6 months ago
Examiner_Config.php
11 months ago
FetchFaviconsJob.php
5 months ago
Filters.php
5 months ago
Geo_Database_Health_Check_Job.php
9 months ago
Geo_Database_Manager.php
6 months ago
Geoposition.php
1 year ago
Icon_Directory.php
6 months ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
10 months ago
Independent_Analytics.php
5 months ago
Interrupt.php
1 year ago
Known_Referrers.php
6 months ago
MainWP.php
1 year ago
Map.php
9 months ago
Map_Data.php
1 year ago
Migration_Fixer_Job.php
5 months ago
Patch.php
1 year ago
Payload_Validator.php
9 months ago
Plugin_Conflict_Detector.php
6 months ago
Plugin_Group.php
6 months ago
Plugin_Group_Option.php
2 years ago
Query.php
1 year ago
Query_Taps.php
5 months ago
Quick_Stats.php
11 months ago
REST_API.php
6 months ago
Real_Time.php
5 months ago
Report.php
1 year ago
Report_Finder.php
6 months ago
Report_Options_Parser.php
9 months ago
Resource_Identifier.php
9 months ago
Settings.php
6 months ago
Sort_Configuration.php
9 months ago
Tables.php
8 months ago
Track_Resource_Changes.php
1 year ago
View_Counter.php
6 months ago
Views_Over_Time_Finder.php
1 year ago
VisitorSaltRefreshInterval.php
6 months ago
WP_Option_Cache_Bust.php
2 years ago
Campaign_Builder.php
102 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | use IAWPSCOPED\Carbon\CarbonImmutable; |
| 6 | use IAWP\Utils\Singleton; |
| 7 | use IAWP\Utils\String_Util; |
| 8 | use IAWP\Utils\URL; |
| 9 | use IAWPSCOPED\Illuminate\Support\Carbon; |
| 10 | use IAWPSCOPED\League\Uri\Uri; |
| 11 | /** @internal */ |
| 12 | class Campaign_Builder |
| 13 | { |
| 14 | use Singleton; |
| 15 | public function __construct() |
| 16 | { |
| 17 | } |
| 18 | public function render_campaign_builder() |
| 19 | { |
| 20 | echo \IAWPSCOPED\iawp_blade()->run('campaign-builder', ['campaigns' => $this->get_previously_created_campaigns()]); |
| 21 | } |
| 22 | public function create_campaign($path, $source, $medium, $campaign, $term, $content) |
| 23 | { |
| 24 | global $wpdb; |
| 25 | $has_errors = \false; |
| 26 | $path = \strlen($path) > 0 ? $path : ''; |
| 27 | $path_error = null; |
| 28 | $source_error = null; |
| 29 | $medium_error = null; |
| 30 | $campaign_error = null; |
| 31 | $term = \strlen($term) > 0 ? $term : null; |
| 32 | $content = \strlen($content) > 0 ? $content : null; |
| 33 | $path = String_Util::str_starts_with($path, '/') ? \substr($path, 1) : $path; |
| 34 | $url = new URL(\trailingslashit(\site_url()) . $path); |
| 35 | if (!$url->is_valid_url()) { |
| 36 | $has_errors = \true; |
| 37 | $path_error = 'path invalid'; |
| 38 | } |
| 39 | if (\strlen($source) === 0) { |
| 40 | $has_errors = \true; |
| 41 | $source_error = 'Source is required'; |
| 42 | } |
| 43 | if (\strlen($medium) === 0) { |
| 44 | $has_errors = \true; |
| 45 | $medium_error = 'Medium is required'; |
| 46 | } |
| 47 | if (\strlen($campaign) === 0) { |
| 48 | $has_errors = \true; |
| 49 | $campaign_error = 'Campaign is required'; |
| 50 | } |
| 51 | if ($has_errors) { |
| 52 | return \IAWPSCOPED\iawp_blade()->run('campaign-builder', ['path' => $path, 'path_error' => $path_error, 'utm_source' => $source, 'utm_source_error' => $source_error, 'utm_medium' => $medium, 'utm_medium_error' => $medium_error, 'utm_campaign' => $campaign, 'utm_campaign_error' => $campaign_error, 'utm_term' => $term, 'utm_content' => $content, 'campaigns' => $this->get_previously_created_campaigns()]); |
| 53 | } |
| 54 | $campaign_urls_table = \IAWP\Query::get_table_name(\IAWP\Query::CAMPAIGN_URLS); |
| 55 | $wpdb->insert($campaign_urls_table, ['path' => $path, 'utm_source' => $source, 'utm_medium' => $medium, 'utm_campaign' => $campaign, 'utm_term' => $term, 'utm_content' => $content, 'created_at' => CarbonImmutable::now('utc')->format('Y-m-d H:i:s')]); |
| 56 | $url = $this->build_url($path, $source, $medium, $campaign, $term, $content); |
| 57 | return \IAWPSCOPED\iawp_blade()->run('campaign-builder', ['path' => $path, 'utm_source' => $source, 'utm_medium' => $medium, 'utm_campaign' => $campaign, 'utm_term' => $term, 'utm_content' => $content, 'new_campaign_url' => $url, 'campaigns' => $this->get_previously_created_campaigns()]); |
| 58 | } |
| 59 | public function build_url($path, $source, $medium, $campaign, $term = null, $content = null) : string |
| 60 | { |
| 61 | $path = String_Util::str_starts_with($path, '/') ? \substr($path, 1) : $path; |
| 62 | $uri = Uri::createFromString(\trailingslashit(\site_url()) . $path); |
| 63 | $existing_query = $uri->getQuery(); |
| 64 | if (\is_null($existing_query)) { |
| 65 | $existing_query = []; |
| 66 | } else { |
| 67 | \parse_str($existing_query, $existing_query); |
| 68 | } |
| 69 | $existing_query['utm_source'] = $source; |
| 70 | $existing_query['utm_medium'] = $medium; |
| 71 | $existing_query['utm_campaign'] = $campaign; |
| 72 | if (isset($term)) { |
| 73 | $existing_query['utm_term'] = $term; |
| 74 | } |
| 75 | if (isset($content)) { |
| 76 | $existing_query['utm_content'] = $content; |
| 77 | } |
| 78 | return $uri->withQuery(\http_build_query($existing_query)); |
| 79 | } |
| 80 | private function get_previously_created_campaigns() |
| 81 | { |
| 82 | global $wpdb; |
| 83 | $campaign_urls_table = \IAWP\Query::get_table_name(\IAWP\Query::CAMPAIGN_URLS); |
| 84 | $results = $wpdb->get_results("\n SELECT * FROM {$campaign_urls_table} ORDER BY created_at DESC LIMIT 100\n "); |
| 85 | return \array_map(function ($result) { |
| 86 | $created_at = Carbon::parse($result->created_at, 'utc')->diffForHumans(); |
| 87 | return ['campaign_url_id' => $result->campaign_url_id, 'result' => \json_encode((array) $result), 'created_at' => $created_at, 'url' => $this->build_url($result->path, $result->utm_source, $result->utm_medium, $result->utm_campaign, $result->utm_term, $result->utm_content)]; |
| 88 | }, $results); |
| 89 | } |
| 90 | public static function has_campaigns() : bool |
| 91 | { |
| 92 | $campaign_builder = new \IAWP\Campaign_Builder(); |
| 93 | return \count($campaign_builder->get_previously_created_campaigns()) > 0; |
| 94 | } |
| 95 | public static function delete_campaign(string $campaign_url_id) |
| 96 | { |
| 97 | $campaign_urls_table = \IAWP\Query::get_table_name(\IAWP\Query::CAMPAIGN_URLS); |
| 98 | $delete_campaign = \IAWP\Illuminate_Builder::new(); |
| 99 | $delete_campaign->from($campaign_urls_table)->where('campaign_url_id', '=', $campaign_url_id)->delete(); |
| 100 | } |
| 101 | } |
| 102 |