PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.18.1
Independent Analytics – WordPress Analytics Plugin v1.18.1
2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / IAWP / campaign_builder.php
independent-analytics / IAWP Last commit date
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