AJAX
3 years ago
Date_Range
3 years ago
Interval
3 years ago
Migrations
3 years ago
Models
3 years ago
Queries
3 years ago
Statistics
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
Illuminate_Builder.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
Reset_Database.php
3 years ago
Settings.php
3 years ago
Track_Resource_Changes.php
3 years ago
View.php
3 years ago
View_Counter.php
3 years ago
WP_Option_Cache_Bust.php
3 years ago
WooCommerce_Order.php
3 years ago
Campaign_Builder.php
99 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\IAWP; |
| 4 | |
| 5 | use IAWP_SCOPED\IAWP\Utils\Singleton; |
| 6 | use IAWP_SCOPED\IAWP\Utils\String_Util; |
| 7 | use IAWP_SCOPED\IAWP\Utils\URL; |
| 8 | use IAWP_SCOPED\League\Uri\Uri; |
| 9 | class Campaign_Builder |
| 10 | { |
| 11 | use Singleton; |
| 12 | public function __construct() |
| 13 | { |
| 14 | } |
| 15 | private function time_ago($time) |
| 16 | { |
| 17 | $time_difference = \time() - $time; |
| 18 | if ($time_difference < 1) { |
| 19 | return 'less than 1 second ago'; |
| 20 | } |
| 21 | $condition = [12 * 30 * 24 * 60 * 60 => 'year', 30 * 24 * 60 * 60 => 'month', 24 * 60 * 60 => 'day', 60 * 60 => 'hour', 60 => 'minute', 1 => 'second']; |
| 22 | foreach ($condition as $secs => $str) { |
| 23 | $d = $time_difference / $secs; |
| 24 | if ($d >= 1) { |
| 25 | $t = \round($d); |
| 26 | return $t . ' ' . $str . ($t > 1 ? 's' : '') . ' ago'; |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | private function get_previously_created_campaigns() |
| 31 | { |
| 32 | global $wpdb; |
| 33 | $campaign_urls_table = Query::get_table_name(Query::CAMPAIGN_URLS); |
| 34 | $results = $wpdb->get_results("\n SELECT * FROM {$campaign_urls_table} ORDER BY created_at DESC LIMIT 100\n "); |
| 35 | return \array_map(function ($result) { |
| 36 | $created_at = new \DateTime($result->created_at); |
| 37 | $time_ago = $this->time_ago($created_at->getTimestamp()); |
| 38 | return ['result' => \json_encode((array) $result), 'created_at' => $time_ago, 'url' => $this->build_url($result->path, $result->utm_source, $result->utm_medium, $result->utm_campaign, $result->utm_term, $result->utm_content)]; |
| 39 | }, $results); |
| 40 | } |
| 41 | public function render_campaign_builder() |
| 42 | { |
| 43 | echo \IAWP_SCOPED\iawp()->templates()->render('campaign_builder', ['campaigns' => $this->get_previously_created_campaigns()]); |
| 44 | } |
| 45 | public function create_campaign($path, $source, $medium, $campaign, $term, $content) |
| 46 | { |
| 47 | global $wpdb; |
| 48 | $has_errors = \false; |
| 49 | $path = \strlen($path) > 0 ? $path : ''; |
| 50 | $path_error = null; |
| 51 | $source_error = null; |
| 52 | $medium_error = null; |
| 53 | $campaign_error = null; |
| 54 | $term = \strlen($term) > 0 ? $term : null; |
| 55 | $content = \strlen($content) > 0 ? $content : null; |
| 56 | $url = new URL(\site_url() . $path); |
| 57 | if (!$url->is_valid_url()) { |
| 58 | $has_errors = \true; |
| 59 | $path_error = 'path invalid'; |
| 60 | } |
| 61 | if (\strlen($source) === 0) { |
| 62 | $has_errors = \true; |
| 63 | $source_error = 'Source is required'; |
| 64 | } |
| 65 | if (\strlen($medium) === 0) { |
| 66 | $has_errors = \true; |
| 67 | $medium_error = 'Medium is required'; |
| 68 | } |
| 69 | if (\strlen($campaign) === 0) { |
| 70 | $has_errors = \true; |
| 71 | $campaign_error = 'Campaign is required'; |
| 72 | } |
| 73 | if ($has_errors) { |
| 74 | return \IAWP_SCOPED\iawp()->templates()->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()]); |
| 75 | } |
| 76 | $campaign_urls_table = Query::get_table_name(Query::CAMPAIGN_URLS); |
| 77 | $wpdb->insert($campaign_urls_table, ['path' => $path, 'utm_source' => $source, 'utm_medium' => $medium, 'utm_campaign' => $campaign, 'utm_term' => $term, 'utm_content' => $content, 'created_at' => (new \DateTime())->format('Y-m-d H:i:s')]); |
| 78 | $url = $this->build_url($path, $source, $medium, $campaign, $term, $content); |
| 79 | return \IAWP_SCOPED\iawp()->templates()->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()]); |
| 80 | } |
| 81 | public function build_url($path, $source, $medium, $campaign, $term = null, $content = null) : string |
| 82 | { |
| 83 | $path = String_Util::str_starts_with($path, '/') ? \substr($path, 1) : $path; |
| 84 | $uri = Uri::createFromString(\trailingslashit(\site_url()) . $path); |
| 85 | $existing_query = $uri->getQuery(); |
| 86 | \parse_str($existing_query, $existing_query); |
| 87 | $existing_query['utm_source'] = $source; |
| 88 | $existing_query['utm_medium'] = $medium; |
| 89 | $existing_query['utm_campaign'] = $campaign; |
| 90 | if (isset($term)) { |
| 91 | $existing_query['utm_term'] = $term; |
| 92 | } |
| 93 | if (isset($content)) { |
| 94 | $existing_query['utm_content'] = $content; |
| 95 | } |
| 96 | return $uri->withQuery(\http_build_query($existing_query)); |
| 97 | } |
| 98 | } |
| 99 |