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