AJAX
4 months ago
Admin_Page
4 months ago
Click_Tracking
6 months ago
ColumnOptions
7 months ago
Cron
10 months ago
Custom_WordPress_Columns
9 months ago
Data_Pruning
6 months ago
Date_Picker
4 months ago
Date_Range
6 months ago
Ecommerce
6 months ago
Email_Reports
4 months ago
Examiner
4 months ago
Favicon
6 months ago
Form_Submissions
6 months ago
Integrations
4 months ago
Interval
1 year ago
Journey
6 months ago
Migrations
7 months ago
Models
6 months ago
Overview
4 months ago
Public_API
6 months ago
Rows
4 months ago
Statistics
5 months ago
Tables
4 months ago
Utils
6 months ago
Views
6 months ago
WooCommerceOrderMetaBox
6 months ago
ActivationLifecycle.php
4 months ago
Admin_Bar_Stats.php
5 months ago
Appearance.php
1 year ago
Campaign_Builder.php
4 months ago
Capability_Manager.php
4 months ago
Chart.php
4 months ago
Chart_Data.php
1 year ago
Click_Tracking.php
4 months ago
ComplianzIntegration.php
4 months ago
Cron_Job.php
6 months ago
Cron_Manager.php
6 months ago
Current_Traffic_Finder.php
1 year ago
Dashboard_Options.php
7 months ago
Dashboard_Widget.php
2 years ago
Database.php
10 months ago
Database_Manager.php
6 months ago
Empty_Report_Option.php
2 years ago
Env.php
7 months ago
Examiner_Config.php
1 year ago
FetchFaviconsJob.php
6 months ago
Filters.php
4 months ago
Geo_Database_Health_Check_Job.php
10 months ago
Geo_Database_Manager.php
7 months ago
Geoposition.php
1 year ago
Icon_Directory.php
7 months ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
1 year ago
Independent_Analytics.php
6 months ago
Interrupt.php
4 months ago
Known_Referrers.php
7 months ago
MainWP.php
1 year ago
Map.php
10 months ago
Map_Data.php
1 year ago
Migration_Fixer_Job.php
6 months ago
Patch.php
1 year ago
Payload_Validator.php
10 months ago
Plugin_Conflict_Detector.php
7 months ago
Plugin_Group.php
7 months ago
Plugin_Group_Option.php
2 years ago
Query.php
1 year ago
Query_Taps.php
5 months ago
Quick_Stats.php
4 months ago
REST_API.php
4 months ago
Real_Time.php
4 months ago
Report.php
1 year ago
Report_Finder.php
7 months ago
Report_Options_Parser.php
10 months ago
Resource_Identifier.php
10 months ago
Settings.php
4 months ago
Sort_Configuration.php
10 months ago
Tables.php
9 months ago
Track_Resource_Changes.php
1 year ago
View_Counter.php
7 months ago
Views_Over_Time_Finder.php
1 year ago
VisitorSaltRefreshInterval.php
7 months ago
WP_Option_Cache_Bust.php
2 years ago
Campaign_Builder.php
106 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_render('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 | $is_path_a_url = (new URL($path))->is_valid_url(); |
| 35 | $url = new URL(\trailingslashit(\site_url()) . $path); |
| 36 | if ($is_path_a_url) { |
| 37 | $has_errors = \true; |
| 38 | $path_error = \esc_html__('Path should not be a full URL', 'independent-analytics'); |
| 39 | } elseif (!$url->is_valid_url()) { |
| 40 | $has_errors = \true; |
| 41 | $path_error = \esc_html__('Path invalid', 'independent-analytics'); |
| 42 | } |
| 43 | if (\strlen($source) === 0) { |
| 44 | $has_errors = \true; |
| 45 | $source_error = \esc_html__('Source is required', 'independent-analytics'); |
| 46 | } |
| 47 | if (\strlen($medium) === 0) { |
| 48 | $has_errors = \true; |
| 49 | $medium_error = \esc_html__('Medium is required', 'independent-analytics'); |
| 50 | } |
| 51 | if (\strlen($campaign) === 0) { |
| 52 | $has_errors = \true; |
| 53 | $campaign_error = \esc_html__('Campaign is required', 'independent-analytics'); |
| 54 | } |
| 55 | if ($has_errors) { |
| 56 | return \IAWPSCOPED\iawp_render('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()]); |
| 57 | } |
| 58 | $campaign_urls_table = \IAWP\Query::get_table_name(\IAWP\Query::CAMPAIGN_URLS); |
| 59 | $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')]); |
| 60 | $url = $this->build_url($path, $source, $medium, $campaign, $term, $content); |
| 61 | return \IAWPSCOPED\iawp_render('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()]); |
| 62 | } |
| 63 | public function build_url($path, $source, $medium, $campaign, $term = null, $content = null) : string |
| 64 | { |
| 65 | $path = String_Util::str_starts_with($path, '/') ? \substr($path, 1) : $path; |
| 66 | $uri = Uri::createFromString(\trailingslashit(\site_url()) . $path); |
| 67 | $existing_query = $uri->getQuery(); |
| 68 | if (\is_null($existing_query)) { |
| 69 | $existing_query = []; |
| 70 | } else { |
| 71 | \parse_str($existing_query, $existing_query); |
| 72 | } |
| 73 | $existing_query['utm_source'] = $source; |
| 74 | $existing_query['utm_medium'] = $medium; |
| 75 | $existing_query['utm_campaign'] = $campaign; |
| 76 | if (isset($term)) { |
| 77 | $existing_query['utm_term'] = $term; |
| 78 | } |
| 79 | if (isset($content)) { |
| 80 | $existing_query['utm_content'] = $content; |
| 81 | } |
| 82 | return $uri->withQuery(\http_build_query($existing_query)); |
| 83 | } |
| 84 | private function get_previously_created_campaigns() |
| 85 | { |
| 86 | global $wpdb; |
| 87 | $campaign_urls_table = \IAWP\Query::get_table_name(\IAWP\Query::CAMPAIGN_URLS); |
| 88 | $results = $wpdb->get_results("\n SELECT * FROM {$campaign_urls_table} ORDER BY created_at DESC LIMIT 100\n "); |
| 89 | return \array_map(function ($result) { |
| 90 | $created_at = Carbon::parse($result->created_at, 'utc')->diffForHumans(); |
| 91 | 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)]; |
| 92 | }, $results); |
| 93 | } |
| 94 | public static function has_campaigns() : bool |
| 95 | { |
| 96 | $campaign_builder = new \IAWP\Campaign_Builder(); |
| 97 | return \count($campaign_builder->get_previously_created_campaigns()) > 0; |
| 98 | } |
| 99 | public static function delete_campaign(string $campaign_url_id) |
| 100 | { |
| 101 | $campaign_urls_table = \IAWP\Query::get_table_name(\IAWP\Query::CAMPAIGN_URLS); |
| 102 | $delete_campaign = \IAWP\Illuminate_Builder::new(); |
| 103 | $delete_campaign->from($campaign_urls_table)->where('campaign_url_id', '=', $campaign_url_id)->delete(); |
| 104 | } |
| 105 | } |
| 106 |