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