AJAX
3 years ago
Migrations
3 years ago
Models
3 years ago
Queries
3 years ago
Tables
3 years ago
Utils
3 years ago
Campaign_Builder.php
3 years ago
Capability_Manager.php
3 years ago
Chart.php
3 years ago
Chart_Geo.php
3 years ago
Chart_SVG.php
3 years ago
Current_Resource.php
3 years ago
Dashboard_Options.php
3 years ago
Dashboard_Widget.php
3 years ago
Email_Reports.php
3 years ago
Filters.php
3 years ago
Geo_Database.php
3 years ago
Geo_Database_Download_Job.php
3 years ago
Health_Check.php
3 years ago
Independent_Analytics.php
3 years ago
Known_Referrers.php
3 years ago
PDF.php
3 years ago
Query.php
3 years ago
Quick_Stats.php
3 years ago
REST_API.php
3 years ago
Real_Time.php
3 years ago
Settings.php
3 years ago
Track_Resource_Changes.php
3 years ago
View_Counter.php
3 years ago
WP_Option_Cache_Bust.php
3 years ago
Campaign_Builder.php
169 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | use IAWP\League\Uri\Uri; |
| 6 | use IAWP\Utils\Singleton; |
| 7 | use IAWP\Utils\String_Util; |
| 8 | use IAWP\Utils\URL; |
| 9 | |
| 10 | class Campaign_Builder |
| 11 | { |
| 12 | use Singleton; |
| 13 | |
| 14 | public function __construct() |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | private function time_ago($time) |
| 19 | { |
| 20 | $time_difference = time() - $time; |
| 21 | |
| 22 | if ($time_difference < 1) { |
| 23 | return 'less than 1 second ago'; |
| 24 | } |
| 25 | $condition = [12 * 30 * 24 * 60 * 60 => 'year', |
| 26 | 30 * 24 * 60 * 60 => 'month', |
| 27 | 24 * 60 * 60 => 'day', |
| 28 | 60 * 60 => 'hour', |
| 29 | 60 => 'minute', |
| 30 | 1 => 'second', |
| 31 | ]; |
| 32 | |
| 33 | foreach ($condition as $secs => $str) { |
| 34 | $d = $time_difference / $secs; |
| 35 | |
| 36 | if ($d >= 1) { |
| 37 | $t = round($d); |
| 38 | |
| 39 | return $t . ' ' . $str . ($t > 1 ? 's' : '') . ' ago'; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | private function get_previously_created_campaigns() |
| 45 | { |
| 46 | global $wpdb; |
| 47 | $campaign_urls_table = Query::get_table_name(Query::CAMPAIGN_URLS); |
| 48 | |
| 49 | $results = $wpdb->get_results(" |
| 50 | SELECT * FROM $campaign_urls_table ORDER BY created_at DESC LIMIT 100 |
| 51 | "); |
| 52 | |
| 53 | return array_map(function ($result) { |
| 54 | $created_at = new \DateTime($result->created_at); |
| 55 | $time_ago = $this->time_ago($created_at->getTimestamp()); |
| 56 | |
| 57 | return [ |
| 58 | 'result' => json_encode((array) $result), |
| 59 | 'created_at' => $time_ago, |
| 60 | 'url' => $this->build_url($result->path, $result->utm_source, $result->utm_medium, $result->utm_campaign, $result->utm_term, $result->utm_content), |
| 61 | ]; |
| 62 | }, $results); |
| 63 | } |
| 64 | |
| 65 | public function render_campaign_builder() |
| 66 | { |
| 67 | echo iawp()->templates()->render('campaign_builder', [ |
| 68 | 'campaigns' => $this->get_previously_created_campaigns(), |
| 69 | ]); |
| 70 | } |
| 71 | |
| 72 | public function create_campaign($path, $source, $medium, $campaign, $term, $content) |
| 73 | { |
| 74 | global $wpdb; |
| 75 | |
| 76 | $has_errors = false; |
| 77 | $path = strlen($path) > 0 ? $path : ''; |
| 78 | $path_error = null; |
| 79 | $source_error = null; |
| 80 | $medium_error = null; |
| 81 | $campaign_error = null; |
| 82 | $term = strlen($term) > 0 ? $term : null; |
| 83 | $content = strlen($content) > 0 ? $content : null; |
| 84 | |
| 85 | $url = new URL(site_url() . $path); |
| 86 | if (!$url->is_valid_url($path)) { |
| 87 | $has_errors = true; |
| 88 | $path_error = 'path invalid'; |
| 89 | } |
| 90 | |
| 91 | if (strlen($source) === 0) { |
| 92 | $has_errors = true; |
| 93 | $source_error = 'Source is required'; |
| 94 | } |
| 95 | |
| 96 | if (strlen($medium) === 0) { |
| 97 | $has_errors = true; |
| 98 | $medium_error = 'Medium is required'; |
| 99 | } |
| 100 | |
| 101 | if (strlen($campaign) === 0) { |
| 102 | $has_errors = true; |
| 103 | $campaign_error = 'Campaign is required'; |
| 104 | } |
| 105 | |
| 106 | if ($has_errors) { |
| 107 | return iawp()->templates()->render('campaign_builder', [ |
| 108 | 'path' => $path, |
| 109 | 'path_error' => $path_error, |
| 110 | 'utm_source' => $source, |
| 111 | 'utm_source_error' => $source_error, |
| 112 | 'utm_medium' => $medium, |
| 113 | 'utm_medium_error' => $medium_error, |
| 114 | 'utm_campaign' => $campaign, |
| 115 | 'utm_campaign_error' => $campaign_error, |
| 116 | 'utm_term' => $term, |
| 117 | 'utm_content' => $content, |
| 118 | 'campaigns' => $this->get_previously_created_campaigns(), |
| 119 | ]); |
| 120 | } |
| 121 | |
| 122 | $campaign_urls_table = Query::get_table_name(Query::CAMPAIGN_URLS); |
| 123 | $wpdb->insert($campaign_urls_table, [ |
| 124 | 'path' => $path, |
| 125 | 'utm_source' => $source, |
| 126 | 'utm_medium' => $medium, |
| 127 | 'utm_campaign' => $campaign, |
| 128 | 'utm_term' => $term, |
| 129 | 'utm_content' => $content, |
| 130 | 'created_at' => (new \DateTime())->format('Y-m-d H:i:s'), |
| 131 | ]); |
| 132 | |
| 133 | $url = $this->build_url($path, $source, $medium, $campaign, $term, $content); |
| 134 | |
| 135 | return iawp()->templates()->render('campaign_builder', [ |
| 136 | 'path' => $path, |
| 137 | 'utm_source' => $source, |
| 138 | 'utm_medium' => $medium, |
| 139 | 'utm_campaign' => $campaign, |
| 140 | 'utm_term' => $term, |
| 141 | 'utm_content' => $content, |
| 142 | 'new_campaign_url' => $url, |
| 143 | 'campaigns' => $this->get_previously_created_campaigns(), |
| 144 | ]); |
| 145 | } |
| 146 | |
| 147 | public function build_url($path, $source, $medium, $campaign, $term = null, $content = null): string |
| 148 | { |
| 149 | $path = String_Util::str_starts_with($path, '/') ? substr($path, 1) : $path; |
| 150 | $uri = Uri::createFromString(trailingslashit(site_url()) . $path); |
| 151 | $existing_query = $uri->getQuery(); |
| 152 | parse_str($existing_query, $existing_query); |
| 153 | |
| 154 | $existing_query['utm_source'] = $source; |
| 155 | $existing_query['utm_medium'] = $medium; |
| 156 | $existing_query['utm_campaign'] = $campaign; |
| 157 | |
| 158 | if (isset($term)) { |
| 159 | $existing_query['utm_term'] = $term; |
| 160 | } |
| 161 | |
| 162 | if (isset($content)) { |
| 163 | $existing_query['utm_content'] = $content; |
| 164 | } |
| 165 | |
| 166 | return $uri->withQuery(http_build_query($existing_query)); |
| 167 | } |
| 168 | } |
| 169 |